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