blob: 2da2dbf71b01b856e2a5fc58450bc8a0b4779b51 [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 {
Jason Sams3522f402012-03-23 11:47:26 -070026class SurfaceTexture;
27
Jason Sams326e0dd2009-05-22 14:03:28 -070028namespace renderscript {
29
Jason Sams5c3e3bc2009-10-26 15:19:28 -070030class Program;
Jason Sams326e0dd2009-05-22 14:03:28 -070031
Stephen Hines1e5149f2011-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 Sakhartchoukafb743a2010-11-09 17:00:54 -080040class Allocation : public ObjectBase {
Stephen Hines1e5149f2011-08-08 15:06:40 -070041 // The graphics equivalent of malloc. The allocation contains a structure of elements.
Jason Sams326e0dd2009-05-22 14:03:28 -070042
Jason Sams326e0dd2009-05-22 14:03:28 -070043public:
Jason Samsbad80742011-03-16 16:29:28 -070044 struct Hal {
45 void * drv;
46
47 struct State {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070048 const Type * type;
Jason Samsbad80742011-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 Sams2275e9c2012-04-16 17:01:26 -070062 void * unused_01;
Jason Samscf27eb42012-02-10 13:24:18 -080063 int32_t surfaceTextureID;
Jason Sams7ac2a4d2012-02-15 12:04:24 -080064 ANativeWindow *wndSurface;
Jason Sams3522f402012-03-23 11:47:26 -070065 SurfaceTexture *surfaceTexture;
Jason Samsbad80742011-03-16 16:29:28 -070066 };
67 State state;
Jason Samseb4fe182011-05-26 16:33:01 -070068
69 struct DrvState {
Jason Sams2275e9c2012-04-16 17:01:26 -070070 mutable void * mallocPtr;
71 mutable uint32_t stride;
Jason Samseb4fe182011-05-26 16:33:01 -070072 } drvState;
73
Jason Samsbad80742011-03-16 16:29:28 -070074 };
75 Hal mHal;
76
Jason Samseb4fe182011-05-26 16:33:01 -070077 static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080078 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
79 void *ptr = 0);
Jason Sams326e0dd2009-05-22 14:03:28 -070080 virtual ~Allocation();
Jason Samsbad80742011-03-16 16:29:28 -070081 void updateCache();
Jason Sams326e0dd2009-05-22 14:03:28 -070082
Jason Samseb4fe182011-05-26 16:33:01 -070083 void * getPtr() const {return mHal.drvState.mallocPtr;}
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070084 const Type * getType() const {return mHal.state.type;}
Jason Sams326e0dd2009-05-22 14:03:28 -070085
Jason Sams366c9c82010-12-08 16:14:36 -080086 void syncAll(Context *rsc, RsAllocationUsageType src);
87
Jason Sams96abf812010-10-05 13:32:49 -070088 void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
89
90 void resize1D(Context *rsc, uint32_t dimX);
91 void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
Jason Sams326e0dd2009-05-22 14:03:28 -070092
Stephen Hines6ae039b2012-01-18 18:46:27 -080093 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 -080094 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -080095 uint32_t w, uint32_t h, const void *data, size_t sizeBytes);
Jason Sams4b45b892010-12-29 14:31:29 -080096 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 -080097 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -070098
Jason Sams4b45b892010-12-29 14:31:29 -080099 void elementData(Context *rsc, uint32_t x,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800100 const void *data, uint32_t elementOff, size_t sizeBytes);
Jason Sams4b45b892010-12-29 14:31:29 -0800101 void elementData(Context *rsc, uint32_t x, uint32_t y,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800102 const void *data, uint32_t elementOff, size_t sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700103
Jason Samse579df42009-08-10 14:55:26 -0700104 void read(void *data);
105
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700106 void addProgramToDirty(const Program *);
107 void removeProgramToDirty(const Program *);
Jason Samse5ffb872009-08-09 17:01:55 -0700108
Jason Samsc21cf402009-11-17 17:26:46 -0800109 virtual void dumpLOGV(const char *prefix) const;
Jason Samse3150cf2012-07-24 18:10:20 -0700110 virtual void serialize(Context *rsc, OStream *stream) const;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700111 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700112 static Allocation *createFromStream(Context *rsc, IStream *stream);
Jason Samsc21cf402009-11-17 17:26:46 -0800113
Jason Samsb89b0b72010-12-13 17:11:21 -0800114 bool getIsScript() const {
Jason Samsbad80742011-03-16 16:29:28 -0700115 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
Jason Samsb89b0b72010-12-13 17:11:21 -0800116 }
Jason Samsebc50192010-12-13 15:32:35 -0800117 bool getIsTexture() const {
Jason Samsbad80742011-03-16 16:29:28 -0700118 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
Jason Samsebc50192010-12-13 15:32:35 -0800119 }
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -0700120 bool getIsRenderTarget() const {
121 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
122 }
Jason Samsebc50192010-12-13 15:32:35 -0800123 bool getIsBufferObject() const {
Jason Samsbad80742011-03-16 16:29:28 -0700124 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
Jason Samsebc50192010-12-13 15:32:35 -0800125 }
Jason Sams760f1f72010-06-25 12:45:41 -0700126
Jason Sams96abf812010-10-05 13:32:49 -0700127 void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
128 void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
Jason Samsc7cec1e2011-08-18 18:01:33 -0700129 virtual bool freeChildren();
Jason Samse3929c92010-08-09 18:13:33 -0700130
Jason Samseb4fe182011-05-26 16:33:01 -0700131 void sendDirty(const Context *rsc) const;
Jason Samsb89b0b72010-12-13 17:11:21 -0800132 bool getHasGraphicsMipmaps() const {
Jason Samsbad80742011-03-16 16:29:28 -0700133 return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
Jason Samsb89b0b72010-12-13 17:11:21 -0800134 }
135
Jason Sams41e373d2012-01-13 14:01:20 -0800136 int32_t getSurfaceTextureID(const Context *rsc);
Jason Sams3522f402012-03-23 11:47:26 -0700137 void setSurfaceTexture(const Context *rsc, SurfaceTexture *st);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800138 void setSurface(const Context *rsc, RsNativeWindow sur);
139 void ioSend(const Context *rsc);
140 void ioReceive(const Context *rsc);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700141
Alex Sakhartchouk1e5168d2010-09-01 16:34:48 -0700142protected:
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700143 Vector<const Program *> mToDirtyList;
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700144 ObjectBaseRef<const Type> mType;
145 void setType(const Type *t) {
146 mType.set(t);
147 mHal.state.type = t;
148 }
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700149
Jason Samsfa84da22010-03-01 15:31:04 -0800150private:
Jason Samsc7cec1e2011-08-18 18:01:33 -0700151 void freeChildrenUnlocked();
Jason Sams179e9a42011-11-23 15:02:15 -0800152 Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800153
154 uint32_t getPackedSize() const;
Jason Samse3150cf2012-07-24 18:10:20 -0700155 static void writePackedData(Context *rsc, const Type *type, uint8_t *dst, const uint8_t *src, bool dstPadded);
156 void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize);
157 void packVec3Allocation(Context *rsc, OStream *stream) const;
Jason Sams326e0dd2009-05-22 14:03:28 -0700158};
159
160}
161}
162#endif
163