rsAllocation.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANDROID_STRUCTURED_ALLOCATION_H
  17. #define ANDROID_STRUCTURED_ALLOCATION_H
  18. #include "rsType.h"
  19. #include <vector>
  20. struct AHardwareBuffer;
  21. // ---------------------------------------------------------------------------
  22. namespace android {
  23. namespace renderscript {
  24. class Program;
  25. class GrallocConsumer;
  26. /*****************************************************************************
  27. * CAUTION
  28. *
  29. * Any layout changes for this class may require a corresponding change to be
  30. * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
  31. * a partial copy of the information below.
  32. *
  33. *****************************************************************************/
  34. class Allocation : public ObjectBase {
  35. // The graphics equivalent of malloc. The allocation contains a structure of elements.
  36. public:
  37. const static int MAX_LOD = 16;
  38. // The mininum alignment requirement for RenderScript. Must be power of 2 and larger than 0.
  39. const static size_t kMinimumRSAlignment = 16;
  40. // The maximun number of Allocations that can share a single BufferQueue;
  41. const static uint32_t MAX_NUM_ALLOC = 16;
  42. struct Hal {
  43. void * drv;
  44. struct State {
  45. const Type * type;
  46. uint32_t usageFlags;
  47. RsAllocationMipmapControl mipmapControl;
  48. // Cached fields from the Type and Element
  49. // to prevent pointer chasing in critical loops.
  50. uint32_t yuv;
  51. uint32_t elementSizeBytes;
  52. bool hasMipmaps;
  53. bool hasFaces;
  54. bool hasReferences;
  55. void * userProvidedPtr;
  56. int32_t surfaceTextureID;
  57. AHardwareBuffer *nativeBuffer;
  58. int64_t timestamp;
  59. // Allocation adapter state
  60. const Allocation *baseAlloc;
  61. uint32_t originX;
  62. uint32_t originY;
  63. uint32_t originZ;
  64. uint32_t originLOD;
  65. uint32_t originFace;
  66. uint32_t originArray[Type::mMaxArrays];
  67. };
  68. State state;
  69. struct DrvState {
  70. struct LodState {
  71. void * mallocPtr;
  72. size_t stride;
  73. uint32_t dimX;
  74. uint32_t dimY;
  75. uint32_t dimZ;
  76. } lod[android::renderscript::Allocation::MAX_LOD];
  77. size_t faceOffset;
  78. uint32_t lodCount;
  79. uint32_t faceCount;
  80. struct YuvState {
  81. uint32_t shift;
  82. uint32_t step;
  83. } yuv;
  84. int grallocFlags;
  85. uint32_t dimArray[Type::mMaxArrays];
  86. };
  87. mutable DrvState drvState;
  88. };
  89. Hal mHal;
  90. void operator delete(void* ptr);
  91. static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
  92. RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
  93. void *ptr = 0);
  94. static Allocation * createAllocationStrided(Context *rsc, const Type *, uint32_t usages,
  95. RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
  96. void *ptr = 0, size_t byteAligned = 16);
  97. static Allocation * createAdapter(Context *rsc, const Allocation *alloc, const Type *type);
  98. virtual ~Allocation();
  99. void updateCache();
  100. const Type * getType() const {return mHal.state.type;}
  101. void syncAll(Context *rsc, RsAllocationUsageType src);
  102. void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
  103. void resize1D(Context *rsc, uint32_t dimX);
  104. void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
  105. void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes);
  106. void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
  107. uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride);
  108. void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
  109. uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride);
  110. void read(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes);
  111. void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
  112. uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride);
  113. void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
  114. uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride);
  115. void elementData(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
  116. const void *data, uint32_t elementOff, size_t sizeBytes);
  117. void elementRead(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
  118. void *data, uint32_t elementOff, size_t sizeBytes);
  119. void addProgramToDirty(const Program *);
  120. void removeProgramToDirty(const Program *);
  121. virtual void dumpLOGV(const char *prefix) const;
  122. virtual void serialize(Context *rsc, OStream *stream) const;
  123. virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
  124. static Allocation *createFromStream(Context *rsc, IStream *stream);
  125. bool getIsScript() const {
  126. return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
  127. }
  128. bool getIsTexture() const {
  129. return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
  130. }
  131. bool getIsRenderTarget() const {
  132. return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
  133. }
  134. bool getIsBufferObject() const {
  135. return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
  136. }
  137. void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
  138. void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
  139. virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const;
  140. virtual bool freeChildren();
  141. void sendDirty(const Context *rsc) const;
  142. bool getHasGraphicsMipmaps() const {
  143. return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
  144. }
  145. void setupGrallocConsumer(const Context *rsc, uint32_t numAlloc);
  146. void shareBufferQueue(const Context *rsc, const Allocation *alloc);
  147. void * getSurface(const Context *rsc);
  148. void setSurface(const Context *rsc, RsNativeWindow sur);
  149. void ioSend(const Context *rsc);
  150. void ioReceive(const Context *rsc);
  151. int64_t getTimeStamp() {return mHal.state.timestamp;}
  152. void adapterOffset(Context *rsc, const uint32_t *offsets, size_t len);
  153. void * getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face,
  154. uint32_t z, uint32_t array, size_t *stride);
  155. void * getPointerUnchecked(uint32_t x, uint32_t y,
  156. uint32_t z = 0, uint32_t lod = 0,
  157. RsAllocationCubemapFace face = RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
  158. uint32_t a1 = 0, uint32_t a2 = 0, uint32_t a3 = 0, uint32_t a4 = 0) const {
  159. uint8_t * p = (uint8_t *) mHal.drvState.lod[lod].mallocPtr;
  160. p += x * getType()->getElementSizeBytes();
  161. p += y * mHal.drvState.lod[lod].stride;
  162. p += z * mHal.drvState.lod[lod].stride * mHal.drvState.lod[lod].dimY;
  163. // Todo: arrays
  164. return p;
  165. }
  166. bool hasSameDims(const Allocation *Other) const;
  167. protected:
  168. std::vector<const Program *> mToDirtyList;
  169. ObjectBaseRef<const Type> mType;
  170. void setType(const Type *t) {
  171. mType.set(t);
  172. mHal.state.type = t;
  173. }
  174. #ifndef RS_COMPATIBILITY_LIB
  175. GrallocConsumer *mGrallocConsumer = nullptr;
  176. bool mBufferQueueInited = false;
  177. uint32_t mCurrentIdx;
  178. #endif
  179. private:
  180. void freeChildrenUnlocked();
  181. Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
  182. Allocation(Context *rsc, const Allocation *, const Type *);
  183. uint32_t getPackedSize() const;
  184. static void writePackedData(Context *rsc, const Type *type, uint8_t *dst,
  185. const uint8_t *src, bool dstPadded);
  186. void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize);
  187. void packVec3Allocation(Context *rsc, OStream *stream) const;
  188. };
  189. } // namespace renderscript
  190. } // namespace android
  191. #endif