v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
loader.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * Sifteo SDK
4  *
5  * Copyright <c> 2012 Sifteo, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #pragma once
27 #ifdef NOT_USERSPACE
28 # error This is a userspace-only header, not allowed by the current build.
29 #endif
30 
31 #include <sifteo/asset/group.h>
32 #include <sifteo/math.h>
33 #include <sifteo/array.h>
34 #include <sifteo/abi.h>
35 
36 namespace Sifteo {
37 
43 class Volume;
44 
53  _SYSAssetConfiguration sys;
54 
55  /*
56  * @brief Initialize this node with an AssetSlot and AssetGroup
57  *
58  * This initializes the AssetConfigurationNode with instructions for
59  * the AssetLoader to make sure the AssetGroup 'group' is available in the AssetSlot
60  * 'slot' by the time the load finishes.
61  *
62  * If the AssetGroup is part of a different Volume, you must provide that volume
63  * handle as the 'volume' parameter, and the 'group' must be mapped via MappedVolume.
64  * If you are loading assets packaged with your own game, you do not need to provide
65  * a volume.
66  */
67  void init(_SYSAssetSlot slot, AssetGroup &group, _SYSVolumeHandle volume = 0)
68  {
69  sys.pGroup = reinterpret_cast<uintptr_t>(&group);
70  sys.volume = volume;
71  sys.dataSize = group.compressedSize();
72  sys.numTiles = group.numTiles();
73  sys.ordinal = group.sysHeader()->ordinal;
74  sys.slot = slot;
75  }
76 
80  AssetSlot slot() const {
81  return AssetSlot(sys.slot);
82  }
83 
87  AssetGroup *group() const {
88  return reinterpret_cast<AssetGroup*>(sys.pGroup);
89  }
90 
94  _SYSVolumeHandle volume() const {
95  return sys.volume;
96  }
97 
101  unsigned numTiles() const {
102  return sys.numTiles;
103  }
104 
110  unsigned tileAllocation() const {
111  return roundup<unsigned>(numTiles(), _SYS_ASSET_GROUP_SIZE_UNIT);
112  }
113 
117  unsigned compressedSize() const {
118  return sys.dataSize;
119  }
120 };
121 
122 
137 template <unsigned tCapacity>
138 class AssetConfiguration : public Array<AssetConfigurationNode, tCapacity, uint8_t> {
140 public:
141 
154  void append(_SYSAssetSlot slot, AssetGroup &group, _SYSVolumeHandle volume = 0)
155  {
156  super::append().init(slot, group, volume);
157  }
158 };
159 
160 
228 struct AssetLoader {
229  _SYSAssetLoader sys;
230  _SYSAssetLoaderCube cubes[CUBE_ALLOCATION];
231 
233  operator const _SYSAssetLoader& () const { return sys; }
234  operator _SYSAssetLoader& () { return sys; }
235  operator const _SYSAssetLoader* () const { return &sys; }
236  operator _SYSAssetLoader* () { return &sys; }
237 
246  void init() {
247  bzero(*this);
248  }
249 
259  void finish() {
260  _SYS_asset_loadFinish(*this);
261  }
262 
280  void cancel(_SYSCubeIDVector cubes = -1) {
281  _SYS_asset_loadCancel(*this, cubes);
282  }
283 
316  template < typename T >
317  void start(T& configuration, _SYSCubeIDVector cubes = -1)
318  {
319  // Limit to cubes that we've allocated _SYSAssetLoaderCubes for
320  STATIC_ASSERT(CUBE_ALLOCATION <= _SYS_NUM_CUBE_SLOTS);
321  cubes &= 0xFFFFFFFF << (32 - CUBE_ALLOCATION);
322  _SYS_asset_loadStart(*this, &configuration.begin()->sys, configuration.count(), cubes);
323  }
324 
337  unsigned cubeProgress(_SYSCubeID cubeID, unsigned max) const
338  {
339  ASSERT(cubeID < CUBE_ALLOCATION);
340  // NB: Division by zero on ARM (udiv) yields zero.
341  return cubes[cubeID].progress * max / cubes[cubeID].total;
342  }
343 
352  float cubeProgress(_SYSCubeID cubeID) const
353  {
354  ASSERT(cubeID < CUBE_ALLOCATION);
355  unsigned progress = cubes[cubeID].progress;
356  unsigned total = cubes[cubeID].total;
357  return total ? (progress / float(total)) : 0.0f;
358  }
359 
372  unsigned averageProgress(unsigned max) const
373  {
374  unsigned progress = 0, total = 0;
375  for (unsigned i = 0; i < CUBE_ALLOCATION; ++i) {
376  progress += cubes[i].progress;
377  total += cubes[i].total;
378  }
379  return progress * max / total;
380  }
381 
390  float averageProgress() const
391  {
392  unsigned progress = 0, total = 0;
393  for (unsigned i = 0; i < CUBE_ALLOCATION; ++i) {
394  progress += cubes[i].progress;
395  total += cubes[i].total;
396  }
397  return total ? (progress / float(total)) : 0.0f;
398  }
399 
403  _SYSCubeIDVector busyCubes() const {
404  return sys.busyCubes;
405  }
406 
418  bool isComplete(_SYSCubeIDVector vec) const {
419  return (sys.busyCubes & vec) == 0;
420  }
421 
427  bool isComplete(_SYSCubeID cubeID) const {
428  return isComplete(_SYSCubeIDVector(0x80000000 >> cubeID));
429  }
430 
434  bool isComplete() const {
435  return sys.busyCubes == 0;
436  }
437 };
438 
439 
450 public:
451  ScopedAssetLoader() { init(); }
452  ~ScopedAssetLoader() { finish(); }
453 };
454 
459 }; // namespace Sifteo
AssetSlots are numbered containers, in a cube's flash memory, which can hold AssetGroups.
Definition: group.h:181
void cancel(_SYSCubeIDVector cubes=-1)
End any in-progress asset loading operations without finishing them.
Definition: loader.h:280
A bundle of compressed tile data, for use by AssetImages.
Definition: group.h:54
unsigned cubeProgress(_SYSCubeID cubeID, unsigned max) const
Measures progress on a single cube, as an integer.
Definition: loader.h:337
ALWAYS_INLINE const _SYSAssetGroupHeader * sysHeader(bool requireConst=false) const
Get a pointer to the read-only system data for this asset group.
Definition: group.h:72
_SYSVolumeHandle volume() const
Return the Volume handle referenced by this node.
Definition: loader.h:94
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
void finish()
Ensure that the system is no longer using this AssetLoader object.
Definition: loader.h:259
unsigned compressedSize() const
Get the compressed size of this asset group, in bytes.
Definition: loader.h:117
An AssetConfiguration represents an arrangement of AssetGroups to load.
Definition: loader.h:138
void bzero(void *s, unsigned count)
Write 'n' zero bytes to memory.
Definition: memory.h:114
An AssetLoader subclass which automatically calls init() and finish() in the constructor and destruct...
Definition: loader.h:449
#define CUBE_ALLOCATION
Compile-time cube limit for the current application.
Definition: limits.h:54
void init()
Initialize this object.
Definition: loader.h:246
bool isComplete() const
Are all asset installations from this asset loading session complete?
Definition: loader.h:434
AssetSlot slot() const
Return the AssetSlot this node will be loaded into.
Definition: loader.h:80
unsigned compressedSize() const
Get the compressed size of this asset group, in bytes.
Definition: group.h:100
bool isComplete(_SYSCubeID cubeID) const
Is the asset install for "cubeID" finished?
Definition: loader.h:427
One node in an AssetConfiguration list.
Definition: loader.h:52
unsigned numTiles() const
Get the size of this asset group, in tiles.
Definition: group.h:84
A statically sized array.
Definition: array.h:51
unsigned numTiles() const
Get the size of this asset group, in tiles.
Definition: loader.h:101
AssetGroup * group() const
Return the AssetGroup referenced by this node.
Definition: loader.h:87
float averageProgress() const
Measures total progress on all cubes, as a floating point value.
Definition: loader.h:390
Definition: array.h:34
void start(T &configuration, _SYSCubeIDVector cubes=-1)
Start installing an AssetConfiguration.
Definition: loader.h:317
unsigned averageProgress(unsigned max) const
Measures total progress on all cubes, as an integer.
Definition: loader.h:372
unsigned tileAllocation() const
How many tiles will this group use up in its AssetSlot?
Definition: loader.h:110
#define STATIC_ASSERT(_x)
Definition: macros.h:342
_SYSCubeIDVector busyCubes() const
Which cubes are still busy loading?
Definition: loader.h:403
AssetConfigurationNode & append()
Synonym for push_back()
Definition: array.h:120
An AssetLoader coordinates asset loading operations on one or more cubes.
Definition: loader.h:228
void append(_SYSAssetSlot slot, AssetGroup &group, _SYSVolumeHandle volume=0)
Add an AssetGroup to this configuration.
Definition: loader.h:154
Vector2< T > vec(T x, T y)
Create a Vector2, from a set of (x,y) coordinates.
Definition: math.h:658
float cubeProgress(_SYSCubeID cubeID) const
Measures progress on a single cube, as a floating point value.
Definition: loader.h:352
bool isComplete(_SYSCubeIDVector vec) const
Is the asset install finished for all cubes in the specified vector?
Definition: loader.h:418