blob: 6cf2c6c32d67401f191d02abf947b2385aebf419 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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// ---------------------------------------------------------------------------
23namespace android {
24namespace renderscript {
25
Jason Sams5c3e3bc2009-10-26 15:19:28 -070026class Program;
Jason Sams326e0dd2009-05-22 14:03:28 -070027
Stephen Hines1e5149f2011-08-08 15:06:40 -070028/*****************************************************************************
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 Sakhartchoukafb743a2010-11-09 17:00:54 -080036class Allocation : public ObjectBase {
Stephen Hines1e5149f2011-08-08 15:06:40 -070037 // The graphics equivalent of malloc. The allocation contains a structure of elements.
Jason Sams326e0dd2009-05-22 14:03:28 -070038
Jason Sams326e0dd2009-05-22 14:03:28 -070039public:
Jason Samsbad80742011-03-16 16:29:28 -070040 struct Hal {
41 void * drv;
42
43 struct State {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070044 const Type * type;
Jason Samsbad80742011-03-16 16:29:28 -070045
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 Sams41e373d2012-01-13 14:01:20 -080058 int32_t surfaceTextureID;
Jason Sams179e9a42011-11-23 15:02:15 -080059
60 void * usrPtr;
Jason Samsbad80742011-03-16 16:29:28 -070061 };
62 State state;
Jason Samseb4fe182011-05-26 16:33:01 -070063
64 struct DrvState {
65 void * mallocPtr;
66 } drvState;
67
Jason Samsbad80742011-03-16 16:29:28 -070068 };
69 Hal mHal;
70
Jason Samseb4fe182011-05-26 16:33:01 -070071 static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080072 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
73 void *ptr = 0);
Jason Sams326e0dd2009-05-22 14:03:28 -070074 virtual ~Allocation();
Jason Samsbad80742011-03-16 16:29:28 -070075 void updateCache();
Jason Sams326e0dd2009-05-22 14:03:28 -070076
Jason Samseb4fe182011-05-26 16:33:01 -070077 void * getPtr() const {return mHal.drvState.mallocPtr;}
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070078 const Type * getType() const {return mHal.state.type;}
Jason Sams326e0dd2009-05-22 14:03:28 -070079
Jason Sams366c9c82010-12-08 16:14:36 -080080 void syncAll(Context *rsc, RsAllocationUsageType src);
81
Jason Sams96abf812010-10-05 13:32:49 -070082 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 Sams326e0dd2009-05-22 14:03:28 -070086
Jason Sams4b45b892010-12-29 14:31:29 -080087 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 Sams9397e302009-08-27 20:23:34 -070089 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes);
Jason Sams4b45b892010-12-29 14:31:29 -080090 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
Jason Sams9397e302009-08-27 20:23:34 -070091 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -070092
Jason Sams4b45b892010-12-29 14:31:29 -080093 void elementData(Context *rsc, uint32_t x,
Jason Sams5f0c84c2010-08-31 13:50:42 -070094 const void *data, uint32_t elementOff, uint32_t sizeBytes);
Jason Sams4b45b892010-12-29 14:31:29 -080095 void elementData(Context *rsc, uint32_t x, uint32_t y,
Jason Sams5f0c84c2010-08-31 13:50:42 -070096 const void *data, uint32_t elementOff, uint32_t sizeBytes);
97
Jason Samse579df42009-08-10 14:55:26 -070098 void read(void *data);
99
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700100 void addProgramToDirty(const Program *);
101 void removeProgramToDirty(const Program *);
Jason Samse5ffb872009-08-09 17:01:55 -0700102
Jason Samsc21cf402009-11-17 17:26:46 -0800103 virtual void dumpLOGV(const char *prefix) const;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700104 virtual void serialize(OStream *stream) const;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700105 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700106 static Allocation *createFromStream(Context *rsc, IStream *stream);
Jason Samsc21cf402009-11-17 17:26:46 -0800107
Jason Samsb89b0b72010-12-13 17:11:21 -0800108 bool getIsScript() const {
Jason Samsbad80742011-03-16 16:29:28 -0700109 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
Jason Samsb89b0b72010-12-13 17:11:21 -0800110 }
Jason Samsebc50192010-12-13 15:32:35 -0800111 bool getIsTexture() const {
Jason Samsbad80742011-03-16 16:29:28 -0700112 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
Jason Samsebc50192010-12-13 15:32:35 -0800113 }
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -0700114 bool getIsRenderTarget() const {
115 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
116 }
Jason Samsebc50192010-12-13 15:32:35 -0800117 bool getIsBufferObject() const {
Jason Samsbad80742011-03-16 16:29:28 -0700118 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
Jason Samsebc50192010-12-13 15:32:35 -0800119 }
Jason Sams760f1f72010-06-25 12:45:41 -0700120
Jason Sams96abf812010-10-05 13:32:49 -0700121 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 Samsc7cec1e2011-08-18 18:01:33 -0700123 virtual bool freeChildren();
Jason Samse3929c92010-08-09 18:13:33 -0700124
Jason Samseb4fe182011-05-26 16:33:01 -0700125 void sendDirty(const Context *rsc) const;
Jason Samsb89b0b72010-12-13 17:11:21 -0800126 bool getHasGraphicsMipmaps() const {
Jason Samsbad80742011-03-16 16:29:28 -0700127 return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
Jason Samsb89b0b72010-12-13 17:11:21 -0800128 }
129
Jason Sams41e373d2012-01-13 14:01:20 -0800130 int32_t getSurfaceTextureID(const Context *rsc);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700131
Alex Sakhartchouk1e5168d2010-09-01 16:34:48 -0700132protected:
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700133 Vector<const Program *> mToDirtyList;
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700134 ObjectBaseRef<const Type> mType;
135 void setType(const Type *t) {
136 mType.set(t);
137 mHal.state.type = t;
138 }
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700139
Jason Samsfa84da22010-03-01 15:31:04 -0800140private:
Jason Samsc7cec1e2011-08-18 18:01:33 -0700141 void freeChildrenUnlocked();
Jason Sams179e9a42011-11-23 15:02:15 -0800142 Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800143
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 Sams326e0dd2009-05-22 14:03:28 -0700148};
149
150}
151}
152#endif
153