blob: dffa440c0f064fced7fe9cb8e865a11d88361661 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Jason Samsbc0ca6b2013-02-15 18:13:43 -08002 * Copyright (C) 2013 The Android Open Source Project
Jason Sams326e0dd2009-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
22// ---------------------------------------------------------------------------
23namespace android {
Jason Sams3522f402012-03-23 11:47:26 -070024
Jason Sams326e0dd2009-05-22 14:03:28 -070025namespace renderscript {
26
Jason Sams5c3e3bc2009-10-26 15:19:28 -070027class Program;
Jason Sams326e0dd2009-05-22 14:03:28 -070028
Stephen Hines1e5149f2011-08-08 15:06:40 -070029/*****************************************************************************
30 * CAUTION
31 *
32 * Any layout changes for this class may require a corresponding change to be
33 * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
34 * a partial copy of the information below.
35 *
36 *****************************************************************************/
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080037class Allocation : public ObjectBase {
Stephen Hines1e5149f2011-08-08 15:06:40 -070038 // The graphics equivalent of malloc. The allocation contains a structure of elements.
Jason Sams326e0dd2009-05-22 14:03:28 -070039
Jason Sams326e0dd2009-05-22 14:03:28 -070040public:
Jason Sams807fdc42012-07-25 17:55:39 -070041 const static int MAX_LOD = 16;
42
Jason Samsbad80742011-03-16 16:29:28 -070043 struct Hal {
44 void * drv;
45
46 struct State {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070047 const Type * type;
Jason Samsbad80742011-03-16 16:29:28 -070048
49 uint32_t usageFlags;
50 RsAllocationMipmapControl mipmapControl;
51
52 // Cached fields from the Type and Element
53 // to prevent pointer chasing in critical loops.
Jason Samsa572aca2013-01-09 11:52:26 -080054 uint32_t yuv;
Jason Samsbad80742011-03-16 16:29:28 -070055 uint32_t elementSizeBytes;
56 bool hasMipmaps;
57 bool hasFaces;
58 bool hasReferences;
Tim Murray2e1a94d2012-11-29 13:12:25 -080059 void * userProvidedPtr;
Jason Samscf27eb42012-02-10 13:24:18 -080060 int32_t surfaceTextureID;
Tim Murray3a25fdd2013-02-22 17:56:56 -080061 void *deprecated01;
62 void *deprecated02;
Jason Samsbad80742011-03-16 16:29:28 -070063 };
64 State state;
Jason Samseb4fe182011-05-26 16:33:01 -070065
66 struct DrvState {
Jason Sams709a0972012-11-15 18:18:04 -080067 struct LodState {
68 void * mallocPtr;
69 size_t stride;
70 uint32_t dimX;
71 uint32_t dimY;
72 uint32_t dimZ;
73 } lod[android::renderscript::Allocation::MAX_LOD];
74 size_t faceOffset;
75 uint32_t lodCount;
76 uint32_t faceCount;
77 };
78 mutable DrvState drvState;
Jason Samseb4fe182011-05-26 16:33:01 -070079
Jason Samsbad80742011-03-16 16:29:28 -070080 };
81 Hal mHal;
82
Tim Murray34689382013-03-11 12:12:03 -070083 void operator delete(void* ptr);
84
Jason Samseb4fe182011-05-26 16:33:01 -070085 static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080086 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
87 void *ptr = 0);
Jason Sams326e0dd2009-05-22 14:03:28 -070088 virtual ~Allocation();
Jason Samsbad80742011-03-16 16:29:28 -070089 void updateCache();
Jason Sams326e0dd2009-05-22 14:03:28 -070090
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070091 const Type * getType() const {return mHal.state.type;}
Jason Sams326e0dd2009-05-22 14:03:28 -070092
Jason Sams366c9c82010-12-08 16:14:36 -080093 void syncAll(Context *rsc, RsAllocationUsageType src);
94
Jason Sams96abf812010-10-05 13:32:49 -070095 void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
96
97 void resize1D(Context *rsc, uint32_t dimX);
98 void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
Jason Sams326e0dd2009-05-22 14:03:28 -070099
Stephen Hines6ae039b2012-01-18 18:46:27 -0800100 void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes);
Jason Sams4b45b892010-12-29 14:31:29 -0800101 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800102 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700103 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
104 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride);
Jason Sams326e0dd2009-05-22 14:03:28 -0700105
Jason Sams807fdc42012-07-25 17:55:39 -0700106 void read(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes);
Tim Murray358747a2012-11-26 13:52:04 -0800107 void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
108 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700109 void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
110 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride);
Jason Sams807fdc42012-07-25 17:55:39 -0700111
Jason Sams4b45b892010-12-29 14:31:29 -0800112 void elementData(Context *rsc, uint32_t x,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800113 const void *data, uint32_t elementOff, size_t sizeBytes);
Jason Sams4b45b892010-12-29 14:31:29 -0800114 void elementData(Context *rsc, uint32_t x, uint32_t y,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800115 const void *data, uint32_t elementOff, size_t sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700116
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700117 void addProgramToDirty(const Program *);
118 void removeProgramToDirty(const Program *);
Jason Samse5ffb872009-08-09 17:01:55 -0700119
Jason Samsc21cf402009-11-17 17:26:46 -0800120 virtual void dumpLOGV(const char *prefix) const;
Jason Samse3150cf2012-07-24 18:10:20 -0700121 virtual void serialize(Context *rsc, OStream *stream) const;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700122 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700123 static Allocation *createFromStream(Context *rsc, IStream *stream);
Jason Samsc21cf402009-11-17 17:26:46 -0800124
Jason Samsb89b0b72010-12-13 17:11:21 -0800125 bool getIsScript() const {
Jason Samsbad80742011-03-16 16:29:28 -0700126 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
Jason Samsb89b0b72010-12-13 17:11:21 -0800127 }
Jason Samsebc50192010-12-13 15:32:35 -0800128 bool getIsTexture() const {
Jason Samsbad80742011-03-16 16:29:28 -0700129 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
Jason Samsebc50192010-12-13 15:32:35 -0800130 }
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -0700131 bool getIsRenderTarget() const {
132 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
133 }
Jason Samsebc50192010-12-13 15:32:35 -0800134 bool getIsBufferObject() const {
Jason Samsbad80742011-03-16 16:29:28 -0700135 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
Jason Samsebc50192010-12-13 15:32:35 -0800136 }
Jason Sams760f1f72010-06-25 12:45:41 -0700137
Jason Sams96abf812010-10-05 13:32:49 -0700138 void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
139 void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
Jason Samsc7cec1e2011-08-18 18:01:33 -0700140 virtual bool freeChildren();
Jason Samse3929c92010-08-09 18:13:33 -0700141
Jason Samseb4fe182011-05-26 16:33:01 -0700142 void sendDirty(const Context *rsc) const;
Jason Samsb89b0b72010-12-13 17:11:21 -0800143 bool getHasGraphicsMipmaps() const {
Jason Samsbad80742011-03-16 16:29:28 -0700144 return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
Jason Samsb89b0b72010-12-13 17:11:21 -0800145 }
146
Jason Sams733396b2013-02-22 12:46:18 -0800147 void * getSurface(const Context *rsc);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800148 void setSurface(const Context *rsc, RsNativeWindow sur);
149 void ioSend(const Context *rsc);
150 void ioReceive(const Context *rsc);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700151
Alex Sakhartchouk1e5168d2010-09-01 16:34:48 -0700152protected:
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700153 Vector<const Program *> mToDirtyList;
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700154 ObjectBaseRef<const Type> mType;
155 void setType(const Type *t) {
156 mType.set(t);
157 mHal.state.type = t;
158 }
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700159
Jason Samsfa84da22010-03-01 15:31:04 -0800160private:
Jason Samsc7cec1e2011-08-18 18:01:33 -0700161 void freeChildrenUnlocked();
Jason Sams179e9a42011-11-23 15:02:15 -0800162 Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800163
164 uint32_t getPackedSize() const;
Jason Sams807fdc42012-07-25 17:55:39 -0700165 static void writePackedData(Context *rsc, const Type *type, uint8_t *dst,
166 const uint8_t *src, bool dstPadded);
Jason Samse3150cf2012-07-24 18:10:20 -0700167 void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize);
168 void packVec3Allocation(Context *rsc, OStream *stream) const;
Jason Sams326e0dd2009-05-22 14:03:28 -0700169};
170
171}
172}
173#endif
174