Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
Stephen Hines | 4cbe25a | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 2 | * Copyright (C) 2009-2012 The Android Open Source Project |
Jason Sams | d19f10d | 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 | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 22 | struct ANativeWindow; |
| 23 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 24 | // --------------------------------------------------------------------------- |
| 25 | namespace android { |
Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame^] | 26 | class SurfaceTexture; |
| 27 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 28 | namespace renderscript { |
| 29 | |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 30 | class Program; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 31 | |
Stephen Hines | e004058 | 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 | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 40 | class Allocation : public ObjectBase { |
Stephen Hines | e004058 | 2011-08-08 15:06:40 -0700 | [diff] [blame] | 41 | // The graphics equivalent of malloc. The allocation contains a structure of elements. |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 42 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 43 | public: |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 44 | struct Hal { |
| 45 | void * drv; |
| 46 | |
| 47 | struct State { |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 48 | const Type * type; |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 49 | |
| 50 | uint32_t usageFlags; |
| 51 | RsAllocationMipmapControl mipmapControl; |
| 52 | |
| 53 | // Cached fields from the Type and Element |
| 54 | // to prevent pointer chasing in critical loops. |
| 55 | uint32_t dimensionX; |
| 56 | uint32_t dimensionY; |
| 57 | uint32_t dimensionZ; |
| 58 | uint32_t elementSizeBytes; |
| 59 | bool hasMipmaps; |
| 60 | bool hasFaces; |
| 61 | bool hasReferences; |
Jason Sams | 857d0c7 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 62 | void * usrPtr; |
Jason Sams | 532efd3 | 2012-02-10 13:24:18 -0800 | [diff] [blame] | 63 | int32_t surfaceTextureID; |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 64 | ANativeWindow *wndSurface; |
Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame^] | 65 | SurfaceTexture *surfaceTexture; |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 66 | }; |
| 67 | State state; |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 68 | |
| 69 | struct DrvState { |
| 70 | void * mallocPtr; |
| 71 | } drvState; |
| 72 | |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 73 | }; |
| 74 | Hal mHal; |
| 75 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 76 | static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages, |
Jason Sams | 857d0c7 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 77 | RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE, |
| 78 | void *ptr = 0); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 79 | virtual ~Allocation(); |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 80 | void updateCache(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 81 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 82 | void * getPtr() const {return mHal.drvState.mallocPtr;} |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 83 | const Type * getType() const {return mHal.state.type;} |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 84 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 85 | void syncAll(Context *rsc, RsAllocationUsageType src); |
| 86 | |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 87 | void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len); |
| 88 | |
| 89 | void resize1D(Context *rsc, uint32_t dimX); |
| 90 | void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 91 | |
Stephen Hines | 4cbe25a | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 92 | void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 93 | void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Stephen Hines | 4cbe25a | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 94 | uint32_t w, uint32_t h, const void *data, size_t sizeBytes); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 95 | void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face, |
Stephen Hines | 4cbe25a | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 96 | uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 97 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 98 | void elementData(Context *rsc, uint32_t x, |
Stephen Hines | 4cbe25a | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 99 | const void *data, uint32_t elementOff, size_t sizeBytes); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 100 | void elementData(Context *rsc, uint32_t x, uint32_t y, |
Stephen Hines | 4cbe25a | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 101 | const void *data, uint32_t elementOff, size_t sizeBytes); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 102 | |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 103 | void read(void *data); |
| 104 | |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 105 | void addProgramToDirty(const Program *); |
| 106 | void removeProgramToDirty(const Program *); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 107 | |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 108 | virtual void dumpLOGV(const char *prefix) const; |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 109 | virtual void serialize(OStream *stream) const; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 110 | virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; } |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 111 | static Allocation *createFromStream(Context *rsc, IStream *stream); |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 112 | |
Jason Sams | 5e0035a | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 113 | bool getIsScript() const { |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 114 | return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0; |
Jason Sams | 5e0035a | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 115 | } |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 116 | bool getIsTexture() const { |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 117 | return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0; |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 118 | } |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 119 | bool getIsRenderTarget() const { |
| 120 | return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0; |
| 121 | } |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 122 | bool getIsBufferObject() const { |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 123 | return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0; |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 124 | } |
Jason Sams | eeeaccc | 2010-06-25 12:45:41 -0700 | [diff] [blame] | 125 | |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 126 | void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const; |
| 127 | void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const; |
Jason Sams | 777ec26 | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 128 | virtual bool freeChildren(); |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 129 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 130 | void sendDirty(const Context *rsc) const; |
Jason Sams | 5e0035a | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 131 | bool getHasGraphicsMipmaps() const { |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 132 | return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE; |
Jason Sams | 5e0035a | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 135 | int32_t getSurfaceTextureID(const Context *rsc); |
Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame^] | 136 | void setSurfaceTexture(const Context *rsc, SurfaceTexture *st); |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 137 | void setSurface(const Context *rsc, RsNativeWindow sur); |
| 138 | void ioSend(const Context *rsc); |
| 139 | void ioReceive(const Context *rsc); |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 140 | |
Alex Sakhartchouk | 8e95466 | 2010-09-01 16:34:48 -0700 | [diff] [blame] | 141 | protected: |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 142 | Vector<const Program *> mToDirtyList; |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 143 | ObjectBaseRef<const Type> mType; |
| 144 | void setType(const Type *t) { |
| 145 | mType.set(t); |
| 146 | mHal.state.type = t; |
| 147 | } |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 148 | |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 149 | private: |
Jason Sams | 777ec26 | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 150 | void freeChildrenUnlocked(); |
Jason Sams | 857d0c7 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 151 | Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr); |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 152 | |
| 153 | uint32_t getPackedSize() const; |
| 154 | static void writePackedData(const Type *type, uint8_t *dst, const uint8_t *src, bool dstPadded); |
Stephen Hines | 4cbe25a | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 155 | void unpackVec3Allocation(const void *data, size_t dataSize); |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 156 | void packVec3Allocation(OStream *stream) const; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | } |
| 160 | } |
| 161 | #endif |
| 162 | |