blob: 739ab8182c9567e74a79bb6d40826667957a1ce1 [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
Jason Samsddceab92013-08-07 13:02:32 -070022#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
Stephen Hines16647802013-08-15 16:28:01 -070023#include <ui/GraphicBuffer.h>
Jason Samsddceab92013-08-07 13:02:32 -070024#include "rsGrallocConsumer.h"
25#include "gui/CpuConsumer.h"
26#include "gui/GLConsumer.h"
Stephen Hines16647802013-08-15 16:28:01 -070027#else
28struct ANativeWindowBuffer;
Jason Samsddceab92013-08-07 13:02:32 -070029#endif
30
Jason Sams326e0dd2009-05-22 14:03:28 -070031// ---------------------------------------------------------------------------
32namespace android {
Jason Sams3522f402012-03-23 11:47:26 -070033
Jason Sams326e0dd2009-05-22 14:03:28 -070034namespace renderscript {
35
Jason Sams5c3e3bc2009-10-26 15:19:28 -070036class Program;
Jason Sams326e0dd2009-05-22 14:03:28 -070037
Stephen Hines1e5149f2011-08-08 15:06:40 -070038/*****************************************************************************
39 * CAUTION
40 *
41 * Any layout changes for this class may require a corresponding change to be
42 * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
43 * a partial copy of the information below.
44 *
45 *****************************************************************************/
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080046class Allocation : public ObjectBase {
Stephen Hines1e5149f2011-08-08 15:06:40 -070047 // The graphics equivalent of malloc. The allocation contains a structure of elements.
Jason Sams326e0dd2009-05-22 14:03:28 -070048
Jason Sams326e0dd2009-05-22 14:03:28 -070049public:
Jason Sams807fdc42012-07-25 17:55:39 -070050 const static int MAX_LOD = 16;
Miao Wang47a58812015-07-23 21:59:16 -070051 // The mininum alignment requirement for RenderScript. Must be power of 2 and larger than 0.
52 const static size_t kMinimumRSAlignment = 16;
Miao Wang75474682015-10-26 16:50:13 -070053 // The maximun number of Allocations that can share a single BufferQueue;
54 const static uint32_t MAX_NUM_ALLOC = 16;
Jason Sams807fdc42012-07-25 17:55:39 -070055
Jason Samsbad80742011-03-16 16:29:28 -070056 struct Hal {
57 void * drv;
58
59 struct State {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070060 const Type * type;
Jason Samsbad80742011-03-16 16:29:28 -070061
62 uint32_t usageFlags;
63 RsAllocationMipmapControl mipmapControl;
64
65 // Cached fields from the Type and Element
66 // to prevent pointer chasing in critical loops.
Jason Samsa572aca2013-01-09 11:52:26 -080067 uint32_t yuv;
Jason Samsbad80742011-03-16 16:29:28 -070068 uint32_t elementSizeBytes;
69 bool hasMipmaps;
70 bool hasFaces;
71 bool hasReferences;
Tim Murray2e1a94d2012-11-29 13:12:25 -080072 void * userProvidedPtr;
Jason Samscf27eb42012-02-10 13:24:18 -080073 int32_t surfaceTextureID;
Jason Samsddceab92013-08-07 13:02:32 -070074 ANativeWindowBuffer *nativeBuffer;
75 int64_t timestamp;
Jason Samscfea6c12015-02-09 12:50:22 -080076
77 // Allocation adapter state
78 const Allocation *baseAlloc;
79 uint32_t originX;
80 uint32_t originY;
81 uint32_t originZ;
82 uint32_t originLOD;
83 uint32_t originFace;
84 uint32_t originArray[Type::mMaxArrays];
Jason Samsbad80742011-03-16 16:29:28 -070085 };
86 State state;
Jason Samseb4fe182011-05-26 16:33:01 -070087
88 struct DrvState {
Jason Sams709a0972012-11-15 18:18:04 -080089 struct LodState {
90 void * mallocPtr;
91 size_t stride;
92 uint32_t dimX;
93 uint32_t dimY;
94 uint32_t dimZ;
95 } lod[android::renderscript::Allocation::MAX_LOD];
96 size_t faceOffset;
97 uint32_t lodCount;
98 uint32_t faceCount;
Jason Sams61656a72013-09-03 16:21:18 -070099
100 struct YuvState {
101 uint32_t shift;
102 uint32_t step;
103 } yuv;
Jason Samse49da132014-10-23 17:32:27 -0700104
105 int grallocFlags;
Jason Samscfea6c12015-02-09 12:50:22 -0800106 uint32_t dimArray[Type::mMaxArrays];
Jason Sams709a0972012-11-15 18:18:04 -0800107 };
108 mutable DrvState drvState;
Jason Samseb4fe182011-05-26 16:33:01 -0700109
Jason Samsbad80742011-03-16 16:29:28 -0700110 };
111 Hal mHal;
112
Tim Murray34689382013-03-11 12:12:03 -0700113 void operator delete(void* ptr);
114
Jason Samseb4fe182011-05-26 16:33:01 -0700115 static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -0800116 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
117 void *ptr = 0);
Miao Wang47a58812015-07-23 21:59:16 -0700118 static Allocation * createAllocationStrided(Context *rsc, const Type *, uint32_t usages,
119 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
120 void *ptr = 0, size_t byteAligned = 16);
Jason Samscfea6c12015-02-09 12:50:22 -0800121 static Allocation * createAdapter(Context *rsc, const Allocation *alloc, const Type *type);
122
123
Jason Sams326e0dd2009-05-22 14:03:28 -0700124 virtual ~Allocation();
Jason Samsbad80742011-03-16 16:29:28 -0700125 void updateCache();
Jason Sams326e0dd2009-05-22 14:03:28 -0700126
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700127 const Type * getType() const {return mHal.state.type;}
Jason Sams326e0dd2009-05-22 14:03:28 -0700128
Jason Sams366c9c82010-12-08 16:14:36 -0800129 void syncAll(Context *rsc, RsAllocationUsageType src);
130
Jason Sams96abf812010-10-05 13:32:49 -0700131 void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
132
133 void resize1D(Context *rsc, uint32_t dimX);
134 void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
Jason Sams326e0dd2009-05-22 14:03:28 -0700135
Stephen Hines6ae039b2012-01-18 18:46:27 -0800136 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 -0800137 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800138 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700139 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
140 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 -0700141
Jason Sams807fdc42012-07-25 17:55:39 -0700142 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 -0800143 void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
144 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700145 void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
146 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride);
Jason Sams807fdc42012-07-25 17:55:39 -0700147
Miao Wangcc8cea72015-02-19 18:14:46 -0800148 void elementData(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800149 const void *data, uint32_t elementOff, size_t sizeBytes);
Miao Wangcc8cea72015-02-19 18:14:46 -0800150
151 void elementRead(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
152 void *data, uint32_t elementOff, size_t sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700153
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700154 void addProgramToDirty(const Program *);
155 void removeProgramToDirty(const Program *);
Jason Samse5ffb872009-08-09 17:01:55 -0700156
Jason Samsc21cf402009-11-17 17:26:46 -0800157 virtual void dumpLOGV(const char *prefix) const;
Jason Samse3150cf2012-07-24 18:10:20 -0700158 virtual void serialize(Context *rsc, OStream *stream) const;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700159 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700160 static Allocation *createFromStream(Context *rsc, IStream *stream);
Jason Samsc21cf402009-11-17 17:26:46 -0800161
Jason Samsb89b0b72010-12-13 17:11:21 -0800162 bool getIsScript() const {
Jason Samsbad80742011-03-16 16:29:28 -0700163 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
Jason Samsb89b0b72010-12-13 17:11:21 -0800164 }
Jason Samsebc50192010-12-13 15:32:35 -0800165 bool getIsTexture() const {
Jason Samsbad80742011-03-16 16:29:28 -0700166 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
Jason Samsebc50192010-12-13 15:32:35 -0800167 }
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -0700168 bool getIsRenderTarget() const {
169 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
170 }
Jason Samsebc50192010-12-13 15:32:35 -0800171 bool getIsBufferObject() const {
Jason Samsbad80742011-03-16 16:29:28 -0700172 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
Jason Samsebc50192010-12-13 15:32:35 -0800173 }
Jason Sams760f1f72010-06-25 12:45:41 -0700174
Jason Sams96abf812010-10-05 13:32:49 -0700175 void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
176 void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
Jason Samsa36c50a2014-06-17 12:06:06 -0700177 virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const;
Jason Samsc7cec1e2011-08-18 18:01:33 -0700178 virtual bool freeChildren();
Jason Samse3929c92010-08-09 18:13:33 -0700179
Jason Samseb4fe182011-05-26 16:33:01 -0700180 void sendDirty(const Context *rsc) const;
Jason Samsb89b0b72010-12-13 17:11:21 -0800181 bool getHasGraphicsMipmaps() const {
Jason Samsbad80742011-03-16 16:29:28 -0700182 return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
Jason Samsb89b0b72010-12-13 17:11:21 -0800183 }
184
Miao Wang75474682015-10-26 16:50:13 -0700185 void setupGrallocConsumer(const Context *rsc, uint32_t numAlloc);
186 void shareBufferQueue(const Context *rsc, const Allocation *alloc);
Jason Sams733396b2013-02-22 12:46:18 -0800187 void * getSurface(const Context *rsc);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800188 void setSurface(const Context *rsc, RsNativeWindow sur);
189 void ioSend(const Context *rsc);
190 void ioReceive(const Context *rsc);
Miao Wang75474682015-10-26 16:50:13 -0700191 int64_t getTimeStamp() {return mHal.state.timestamp;}
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700192
Jason Sams442b7ff2015-03-06 16:50:11 -0800193 void adapterOffset(Context *rsc, const uint32_t *offsets, size_t len);
194
Jason Samsb8a94e22014-02-24 17:52:32 -0800195 void * getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face,
196 uint32_t z, uint32_t array, size_t *stride);
197
Jason Samsc0d68472015-01-20 14:29:52 -0800198 void * getPointerUnchecked(uint32_t x, uint32_t y,
199 uint32_t z = 0, uint32_t lod = 0,
200 RsAllocationCubemapFace face = RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
201 uint32_t a1 = 0, uint32_t a2 = 0, uint32_t a3 = 0, uint32_t a4 = 0) const {
202
203 uint8_t * p = (uint8_t *) mHal.drvState.lod[lod].mallocPtr;
204 p += x * getType()->getElementSizeBytes();
205 p += y * mHal.drvState.lod[lod].stride;
206 p += z * mHal.drvState.lod[lod].stride * mHal.drvState.lod[lod].dimY;
207
208 // Todo: arrays
209
210 return p;
211 }
212
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700213 bool hasSameDims(const Allocation *Other) const;
214
Alex Sakhartchouk1e5168d2010-09-01 16:34:48 -0700215protected:
Yang Nib8353c52015-02-14 18:00:59 -0800216 Vector<const Program *> mToDirtyList;
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700217 ObjectBaseRef<const Type> mType;
218 void setType(const Type *t) {
219 mType.set(t);
220 mHal.state.type = t;
221 }
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700222
Miao Wang62237212017-02-27 15:19:36 -0800223#ifndef RS_COMPATIBILITY_LIB
Jason Samsddceab92013-08-07 13:02:32 -0700224 class NewBufferListener : public android::ConsumerBase::FrameAvailableListener {
225 public:
Chih-Hung Hsieh10ab8bb2016-07-01 12:20:20 -0700226 explicit NewBufferListener(uint32_t numAlloc);
Miao Wang75474682015-10-26 16:50:13 -0700227 virtual ~NewBufferListener();
Jason Samsddceab92013-08-07 13:02:32 -0700228 const android::renderscript::Context *rsc;
Miao Wang75474682015-10-26 16:50:13 -0700229 const android::renderscript::Allocation **alloc;
Jason Samsddceab92013-08-07 13:02:32 -0700230
Dan Stozaf0d7aa22014-11-04 11:38:47 -0800231 virtual void onFrameAvailable(const BufferItem& item);
Miao Wang75474682015-10-26 16:50:13 -0700232 private:
233 uint32_t mNumAlloc;
Jason Samsddceab92013-08-07 13:02:32 -0700234 };
235
236 sp<NewBufferListener> mBufferListener;
237 sp< GrallocConsumer > mGrallocConsumer;
Miao Wang75474682015-10-26 16:50:13 -0700238 sp<IGraphicBufferProducer> mGraphicBufferProducer;
239 bool mBufferQueueInited = false;
240 uint32_t mCurrentIdx;
Jason Samsddceab92013-08-07 13:02:32 -0700241#endif
242
243
Jason Samsfa84da22010-03-01 15:31:04 -0800244private:
Jason Samsc7cec1e2011-08-18 18:01:33 -0700245 void freeChildrenUnlocked();
Jason Sams179e9a42011-11-23 15:02:15 -0800246 Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
Jason Samscfea6c12015-02-09 12:50:22 -0800247 Allocation(Context *rsc, const Allocation *, const Type *);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800248
249 uint32_t getPackedSize() const;
Jason Sams807fdc42012-07-25 17:55:39 -0700250 static void writePackedData(Context *rsc, const Type *type, uint8_t *dst,
251 const uint8_t *src, bool dstPadded);
Jason Samse3150cf2012-07-24 18:10:20 -0700252 void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize);
253 void packVec3Allocation(Context *rsc, OStream *stream) const;
Jason Sams326e0dd2009-05-22 14:03:28 -0700254};
255
Rahul Chaudhry7974fc02017-02-09 12:33:28 -0800256} // namespace renderscript
257} // namespace android
Jason Sams326e0dd2009-05-22 14:03:28 -0700258#endif