blob: 58a6fcacf6ace12186e6e66e4fe1a7c2fa87a470 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Stephen Hines6ae039b2012-01-18 18:46:27 -08002 * Copyright (C) 2009-2012 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
Jason Sams7ac2a4d2012-02-15 12:04:24 -080022struct ANativeWindow;
23
Jason Sams326e0dd2009-05-22 14:03:28 -070024// ---------------------------------------------------------------------------
25namespace android {
26namespace renderscript {
27
Jason Sams5c3e3bc2009-10-26 15:19:28 -070028class Program;
Jason Sams326e0dd2009-05-22 14:03:28 -070029
Stephen Hines1e5149f2011-08-08 15:06:40 -070030/*****************************************************************************
31 * CAUTION
32 *
33 * Any layout changes for this class may require a corresponding change to be
34 * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
35 * a partial copy of the information below.
36 *
37 *****************************************************************************/
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080038class Allocation : public ObjectBase {
Stephen Hines1e5149f2011-08-08 15:06:40 -070039 // The graphics equivalent of malloc. The allocation contains a structure of elements.
Jason Sams326e0dd2009-05-22 14:03:28 -070040
Jason Sams326e0dd2009-05-22 14:03:28 -070041public:
Jason Samsbad80742011-03-16 16:29:28 -070042 struct Hal {
43 void * drv;
44
45 struct State {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070046 const Type * type;
Jason Samsbad80742011-03-16 16:29:28 -070047
48 uint32_t usageFlags;
49 RsAllocationMipmapControl mipmapControl;
50
51 // Cached fields from the Type and Element
52 // to prevent pointer chasing in critical loops.
53 uint32_t dimensionX;
54 uint32_t dimensionY;
55 uint32_t dimensionZ;
56 uint32_t elementSizeBytes;
57 bool hasMipmaps;
58 bool hasFaces;
59 bool hasReferences;
Jason Sams179e9a42011-11-23 15:02:15 -080060 void * usrPtr;
Jason Samscf27eb42012-02-10 13:24:18 -080061 int32_t surfaceTextureID;
Jason Sams7ac2a4d2012-02-15 12:04:24 -080062 ANativeWindow *wndSurface;
Jason Samsbad80742011-03-16 16:29:28 -070063 };
64 State state;
Jason Samseb4fe182011-05-26 16:33:01 -070065
66 struct DrvState {
67 void * mallocPtr;
68 } drvState;
69
Jason Samsbad80742011-03-16 16:29:28 -070070 };
71 Hal mHal;
72
Jason Samseb4fe182011-05-26 16:33:01 -070073 static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080074 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
75 void *ptr = 0);
Jason Sams326e0dd2009-05-22 14:03:28 -070076 virtual ~Allocation();
Jason Samsbad80742011-03-16 16:29:28 -070077 void updateCache();
Jason Sams326e0dd2009-05-22 14:03:28 -070078
Jason Samseb4fe182011-05-26 16:33:01 -070079 void * getPtr() const {return mHal.drvState.mallocPtr;}
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070080 const Type * getType() const {return mHal.state.type;}
Jason Sams326e0dd2009-05-22 14:03:28 -070081
Jason Sams366c9c82010-12-08 16:14:36 -080082 void syncAll(Context *rsc, RsAllocationUsageType src);
83
Jason Sams96abf812010-10-05 13:32:49 -070084 void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
85
86 void resize1D(Context *rsc, uint32_t dimX);
87 void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
Jason Sams326e0dd2009-05-22 14:03:28 -070088
Stephen Hines6ae039b2012-01-18 18:46:27 -080089 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 -080090 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -080091 uint32_t w, uint32_t h, const void *data, size_t sizeBytes);
Jason Sams4b45b892010-12-29 14:31:29 -080092 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -080093 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -070094
Jason Sams4b45b892010-12-29 14:31:29 -080095 void elementData(Context *rsc, uint32_t x,
Stephen Hines6ae039b2012-01-18 18:46:27 -080096 const void *data, uint32_t elementOff, size_t sizeBytes);
Jason Sams4b45b892010-12-29 14:31:29 -080097 void elementData(Context *rsc, uint32_t x, uint32_t y,
Stephen Hines6ae039b2012-01-18 18:46:27 -080098 const void *data, uint32_t elementOff, size_t sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -070099
Jason Samse579df42009-08-10 14:55:26 -0700100 void read(void *data);
101
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700102 void addProgramToDirty(const Program *);
103 void removeProgramToDirty(const Program *);
Jason Samse5ffb872009-08-09 17:01:55 -0700104
Jason Samsc21cf402009-11-17 17:26:46 -0800105 virtual void dumpLOGV(const char *prefix) const;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700106 virtual void serialize(OStream *stream) const;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700107 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700108 static Allocation *createFromStream(Context *rsc, IStream *stream);
Jason Samsc21cf402009-11-17 17:26:46 -0800109
Jason Samsb89b0b72010-12-13 17:11:21 -0800110 bool getIsScript() const {
Jason Samsbad80742011-03-16 16:29:28 -0700111 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
Jason Samsb89b0b72010-12-13 17:11:21 -0800112 }
Jason Samsebc50192010-12-13 15:32:35 -0800113 bool getIsTexture() const {
Jason Samsbad80742011-03-16 16:29:28 -0700114 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
Jason Samsebc50192010-12-13 15:32:35 -0800115 }
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -0700116 bool getIsRenderTarget() const {
117 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
118 }
Jason Samsebc50192010-12-13 15:32:35 -0800119 bool getIsBufferObject() const {
Jason Samsbad80742011-03-16 16:29:28 -0700120 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
Jason Samsebc50192010-12-13 15:32:35 -0800121 }
Jason Sams760f1f72010-06-25 12:45:41 -0700122
Jason Sams96abf812010-10-05 13:32:49 -0700123 void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
124 void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
Jason Samsc7cec1e2011-08-18 18:01:33 -0700125 virtual bool freeChildren();
Jason Samse3929c92010-08-09 18:13:33 -0700126
Jason Samseb4fe182011-05-26 16:33:01 -0700127 void sendDirty(const Context *rsc) const;
Jason Samsb89b0b72010-12-13 17:11:21 -0800128 bool getHasGraphicsMipmaps() const {
Jason Samsbad80742011-03-16 16:29:28 -0700129 return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
Jason Samsb89b0b72010-12-13 17:11:21 -0800130 }
131
Jason Sams41e373d2012-01-13 14:01:20 -0800132 int32_t getSurfaceTextureID(const Context *rsc);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800133 void setSurface(const Context *rsc, RsNativeWindow sur);
134 void ioSend(const Context *rsc);
135 void ioReceive(const Context *rsc);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700136
Alex Sakhartchouk1e5168d2010-09-01 16:34:48 -0700137protected:
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700138 Vector<const Program *> mToDirtyList;
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700139 ObjectBaseRef<const Type> mType;
140 void setType(const Type *t) {
141 mType.set(t);
142 mHal.state.type = t;
143 }
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700144
Jason Samsfa84da22010-03-01 15:31:04 -0800145private:
Jason Samsc7cec1e2011-08-18 18:01:33 -0700146 void freeChildrenUnlocked();
Jason Sams179e9a42011-11-23 15:02:15 -0800147 Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800148
149 uint32_t getPackedSize() const;
150 static void writePackedData(const Type *type, uint8_t *dst, const uint8_t *src, bool dstPadded);
Stephen Hines6ae039b2012-01-18 18:46:27 -0800151 void unpackVec3Allocation(const void *data, size_t dataSize);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800152 void packVec3Allocation(OStream *stream) const;
Jason Sams326e0dd2009-05-22 14:03:28 -0700153};
154
155}
156}
157#endif
158