blob: e2783d23c07d67b0a1bb5bdfb8cdc8687978bbf0 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
Stephen Hines4cbe25a2012-01-18 18:46:27 -08002 * Copyright (C) 2009-2012 The Android Open Source Project
Jason Samsd19f10d2009-05-22 14:03:28 -07003 *
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 Sams163766c2012-02-15 12:04:24 -080022struct ANativeWindow;
23
Jason Samsd19f10d2009-05-22 14:03:28 -070024// ---------------------------------------------------------------------------
25namespace android {
Jason Samsfe1d5ff2012-03-23 11:47:26 -070026class SurfaceTexture;
27
Jason Samsd19f10d2009-05-22 14:03:28 -070028namespace renderscript {
29
Jason Sams83f1c632009-10-26 15:19:28 -070030class Program;
Jason Samsd19f10d2009-05-22 14:03:28 -070031
Stephen Hinese0040582011-08-08 15:06:40 -070032/*****************************************************************************
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 Sakhartchouked9f2102010-11-09 17:00:54 -080040class Allocation : public ObjectBase {
Stephen Hinese0040582011-08-08 15:06:40 -070041 // The graphics equivalent of malloc. The allocation contains a structure of elements.
Jason Samsd19f10d2009-05-22 14:03:28 -070042
Jason Samsd19f10d2009-05-22 14:03:28 -070043public:
Jason Samse4a06c52011-03-16 16:29:28 -070044 struct Hal {
45 void * drv;
46
47 struct State {
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070048 const Type * type;
Jason Samse4a06c52011-03-16 16:29:28 -070049
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 Sams857d0c72011-11-23 15:02:15 -080062 void * usrPtr;
Jason Sams532efd32012-02-10 13:24:18 -080063 int32_t surfaceTextureID;
Jason Sams163766c2012-02-15 12:04:24 -080064 ANativeWindow *wndSurface;
Jason Samsfe1d5ff2012-03-23 11:47:26 -070065 SurfaceTexture *surfaceTexture;
Jason Samse4a06c52011-03-16 16:29:28 -070066 };
67 State state;
Jason Sams7e8aae72011-05-26 16:33:01 -070068
69 struct DrvState {
70 void * mallocPtr;
71 } drvState;
72
Jason Samse4a06c52011-03-16 16:29:28 -070073 };
74 Hal mHal;
75
Jason Sams7e8aae72011-05-26 16:33:01 -070076 static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
Jason Sams857d0c72011-11-23 15:02:15 -080077 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
78 void *ptr = 0);
Jason Samsd19f10d2009-05-22 14:03:28 -070079 virtual ~Allocation();
Jason Samse4a06c52011-03-16 16:29:28 -070080 void updateCache();
Jason Samsd19f10d2009-05-22 14:03:28 -070081
Jason Sams7e8aae72011-05-26 16:33:01 -070082 void * getPtr() const {return mHal.drvState.mallocPtr;}
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070083 const Type * getType() const {return mHal.state.type;}
Jason Samsd19f10d2009-05-22 14:03:28 -070084
Jason Sams5476b452010-12-08 16:14:36 -080085 void syncAll(Context *rsc, RsAllocationUsageType src);
86
Jason Sams5edc6082010-10-05 13:32:49 -070087 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 Samsd19f10d2009-05-22 14:03:28 -070091
Stephen Hines4cbe25a2012-01-18 18:46:27 -080092 void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes);
Jason Sams49a05d72010-12-29 14:31:29 -080093 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines4cbe25a2012-01-18 18:46:27 -080094 uint32_t w, uint32_t h, const void *data, size_t sizeBytes);
Jason Sams49a05d72010-12-29 14:31:29 -080095 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines4cbe25a2012-01-18 18:46:27 -080096 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -070097
Jason Sams49a05d72010-12-29 14:31:29 -080098 void elementData(Context *rsc, uint32_t x,
Stephen Hines4cbe25a2012-01-18 18:46:27 -080099 const void *data, uint32_t elementOff, size_t sizeBytes);
Jason Sams49a05d72010-12-29 14:31:29 -0800100 void elementData(Context *rsc, uint32_t x, uint32_t y,
Stephen Hines4cbe25a2012-01-18 18:46:27 -0800101 const void *data, uint32_t elementOff, size_t sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700102
Jason Sams40a29e82009-08-10 14:55:26 -0700103 void read(void *data);
104
Jason Sams83f1c632009-10-26 15:19:28 -0700105 void addProgramToDirty(const Program *);
106 void removeProgramToDirty(const Program *);
Jason Sams1bada8c2009-08-09 17:01:55 -0700107
Jason Sams715333b2009-11-17 17:26:46 -0800108 virtual void dumpLOGV(const char *prefix) const;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700109 virtual void serialize(OStream *stream) const;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700110 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700111 static Allocation *createFromStream(Context *rsc, IStream *stream);
Jason Sams715333b2009-11-17 17:26:46 -0800112
Jason Sams5e0035a2010-12-13 17:11:21 -0800113 bool getIsScript() const {
Jason Samse4a06c52011-03-16 16:29:28 -0700114 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
Jason Sams5e0035a2010-12-13 17:11:21 -0800115 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800116 bool getIsTexture() const {
Jason Samse4a06c52011-03-16 16:29:28 -0700117 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
Jason Samsd4b23b52010-12-13 15:32:35 -0800118 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700119 bool getIsRenderTarget() const {
120 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
121 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800122 bool getIsBufferObject() const {
Jason Samse4a06c52011-03-16 16:29:28 -0700123 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
Jason Samsd4b23b52010-12-13 15:32:35 -0800124 }
Jason Samseeeaccc2010-06-25 12:45:41 -0700125
Jason Sams5edc6082010-10-05 13:32:49 -0700126 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 Sams777ec262011-08-18 18:01:33 -0700128 virtual bool freeChildren();
Jason Samsb28ca96f2010-08-09 18:13:33 -0700129
Jason Sams7e8aae72011-05-26 16:33:01 -0700130 void sendDirty(const Context *rsc) const;
Jason Sams5e0035a2010-12-13 17:11:21 -0800131 bool getHasGraphicsMipmaps() const {
Jason Samse4a06c52011-03-16 16:29:28 -0700132 return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
Jason Sams5e0035a2010-12-13 17:11:21 -0800133 }
134
Jason Sams615e7ce2012-01-13 14:01:20 -0800135 int32_t getSurfaceTextureID(const Context *rsc);
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700136 void setSurfaceTexture(const Context *rsc, SurfaceTexture *st);
Jason Sams163766c2012-02-15 12:04:24 -0800137 void setSurface(const Context *rsc, RsNativeWindow sur);
138 void ioSend(const Context *rsc);
139 void ioReceive(const Context *rsc);
Jason Sams83f1c632009-10-26 15:19:28 -0700140
Alex Sakhartchouk8e954662010-09-01 16:34:48 -0700141protected:
Jason Sams83f1c632009-10-26 15:19:28 -0700142 Vector<const Program *> mToDirtyList;
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -0700143 ObjectBaseRef<const Type> mType;
144 void setType(const Type *t) {
145 mType.set(t);
146 mHal.state.type = t;
147 }
Jason Sams83f1c632009-10-26 15:19:28 -0700148
Jason Sams8a64743f2010-03-01 15:31:04 -0800149private:
Jason Sams777ec262011-08-18 18:01:33 -0700150 void freeChildrenUnlocked();
Jason Sams857d0c72011-11-23 15:02:15 -0800151 Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800152
153 uint32_t getPackedSize() const;
154 static void writePackedData(const Type *type, uint8_t *dst, const uint8_t *src, bool dstPadded);
Stephen Hines4cbe25a2012-01-18 18:46:27 -0800155 void unpackVec3Allocation(const void *data, size_t dataSize);
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800156 void packVec3Allocation(OStream *stream) const;
Jason Samsd19f10d2009-05-22 14:03:28 -0700157};
158
159}
160}
161#endif
162