Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
| 17 | #ifndef ANDROID_STRUCTURED_ALLOCATION_H |
| 18 | #define ANDROID_STRUCTURED_ALLOCATION_H |
| 19 | |
| 20 | #include "rsType.h" |
| 21 | |
| 22 | // --------------------------------------------------------------------------- |
| 23 | namespace android { |
| 24 | namespace renderscript { |
| 25 | |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 26 | class Program; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | |
Stephen Hines | 1e5149f | 2011-08-08 15:06:40 -0700 | [diff] [blame] | 28 | /***************************************************************************** |
| 29 | * CAUTION |
| 30 | * |
| 31 | * Any layout changes for this class may require a corresponding change to be |
| 32 | * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains |
| 33 | * a partial copy of the information below. |
| 34 | * |
| 35 | *****************************************************************************/ |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 36 | class Allocation : public ObjectBase { |
Stephen Hines | 1e5149f | 2011-08-08 15:06:40 -0700 | [diff] [blame] | 37 | // The graphics equivalent of malloc. The allocation contains a structure of elements. |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 38 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 39 | public: |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 40 | struct Hal { |
| 41 | void * drv; |
| 42 | |
| 43 | struct State { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 44 | const Type * type; |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 45 | |
| 46 | uint32_t usageFlags; |
| 47 | RsAllocationMipmapControl mipmapControl; |
| 48 | |
| 49 | // Cached fields from the Type and Element |
| 50 | // to prevent pointer chasing in critical loops. |
| 51 | uint32_t dimensionX; |
| 52 | uint32_t dimensionY; |
| 53 | uint32_t dimensionZ; |
| 54 | uint32_t elementSizeBytes; |
| 55 | bool hasMipmaps; |
| 56 | bool hasFaces; |
| 57 | bool hasReferences; |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame^] | 58 | int32_t surfaceTextureID; |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 59 | |
| 60 | void * usrPtr; |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 61 | }; |
| 62 | State state; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 63 | |
| 64 | struct DrvState { |
| 65 | void * mallocPtr; |
| 66 | } drvState; |
| 67 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 68 | }; |
| 69 | Hal mHal; |
| 70 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 71 | static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages, |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 72 | RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE, |
| 73 | void *ptr = 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 74 | virtual ~Allocation(); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 75 | void updateCache(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 76 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 77 | void * getPtr() const {return mHal.drvState.mallocPtr;} |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 78 | const Type * getType() const {return mHal.state.type;} |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 79 | |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 80 | void syncAll(Context *rsc, RsAllocationUsageType src); |
| 81 | |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 82 | void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len); |
| 83 | |
| 84 | void resize1D(Context *rsc, uint32_t dimX); |
| 85 | void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 86 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 87 | void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, uint32_t sizeBytes); |
| 88 | void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 89 | uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes); |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 90 | void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face, |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 91 | uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 92 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 93 | void elementData(Context *rsc, uint32_t x, |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 94 | const void *data, uint32_t elementOff, uint32_t sizeBytes); |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 95 | void elementData(Context *rsc, uint32_t x, uint32_t y, |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 96 | const void *data, uint32_t elementOff, uint32_t sizeBytes); |
| 97 | |
Jason Sams | e579df4 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 98 | void read(void *data); |
| 99 | |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 100 | void addProgramToDirty(const Program *); |
| 101 | void removeProgramToDirty(const Program *); |
Jason Sams | e5ffb87 | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 102 | |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 103 | virtual void dumpLOGV(const char *prefix) const; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 104 | virtual void serialize(OStream *stream) const; |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 105 | virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; } |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 106 | static Allocation *createFromStream(Context *rsc, IStream *stream); |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 107 | |
Jason Sams | b89b0b7 | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 108 | bool getIsScript() const { |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 109 | return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0; |
Jason Sams | b89b0b7 | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 110 | } |
Jason Sams | ebc5019 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 111 | bool getIsTexture() const { |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 112 | return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0; |
Jason Sams | ebc5019 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 113 | } |
Alex Sakhartchouk | 7d9c5ff | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 114 | bool getIsRenderTarget() const { |
| 115 | return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0; |
| 116 | } |
Jason Sams | ebc5019 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 117 | bool getIsBufferObject() const { |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 118 | return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0; |
Jason Sams | ebc5019 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 119 | } |
Jason Sams | 760f1f7 | 2010-06-25 12:45:41 -0700 | [diff] [blame] | 120 | |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 121 | void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const; |
| 122 | void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const; |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 123 | virtual bool freeChildren(); |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 124 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 125 | void sendDirty(const Context *rsc) const; |
Jason Sams | b89b0b7 | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 126 | bool getHasGraphicsMipmaps() const { |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 127 | return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE; |
Jason Sams | b89b0b7 | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame^] | 130 | int32_t getSurfaceTextureID(const Context *rsc); |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 131 | |
Alex Sakhartchouk | 1e5168d | 2010-09-01 16:34:48 -0700 | [diff] [blame] | 132 | protected: |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 133 | Vector<const Program *> mToDirtyList; |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 134 | ObjectBaseRef<const Type> mType; |
| 135 | void setType(const Type *t) { |
| 136 | mType.set(t); |
| 137 | mHal.state.type = t; |
| 138 | } |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 139 | |
Jason Sams | fa84da2 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 140 | private: |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 141 | void freeChildrenUnlocked(); |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 142 | Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 143 | |
| 144 | uint32_t getPackedSize() const; |
| 145 | static void writePackedData(const Type *type, uint8_t *dst, const uint8_t *src, bool dstPadded); |
| 146 | void unpackVec3Allocation(const void *data, uint32_t dataSize); |
| 147 | void packVec3Allocation(OStream *stream) const; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | } |
| 151 | } |
| 152 | #endif |
| 153 | |