blob: ca65a4069c18b8862d5a64a9001e4420c0a0c268 [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 */
Jason Sams326e0dd2009-05-22 14:03:28 -070016
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080017#include "rsContext.h"
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080018#include "rsAllocation.h"
19#include "rsAdapter.h"
Jason Samseb4fe182011-05-26 16:33:01 -070020#include "rs_hal.h"
21
Stephen Hinesb0934b62013-07-03 17:27:38 -070022#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
Jason Sams7ac2a4d2012-02-15 12:04:24 -080023#include "system/window.h"
Andy McFadden58fd6a52012-12-18 09:50:03 -080024#include "gui/GLConsumer.h"
Tim Murray0b575de2013-03-15 15:56:43 -070025#endif
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070026
Jason Sams326e0dd2009-05-22 14:03:28 -070027using namespace android;
28using namespace android::renderscript;
29
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080030Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080031 RsAllocationMipmapControl mc, void * ptr)
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080032 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080033
Jason Samseb4fe182011-05-26 16:33:01 -070034 memset(&mHal, 0, sizeof(mHal));
35 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsbad80742011-03-16 16:29:28 -070036 mHal.state.usageFlags = usages;
37 mHal.state.mipmapControl = mc;
Tim Murray2e1a94d2012-11-29 13:12:25 -080038 mHal.state.userProvidedPtr = ptr;
Jason Sams366c9c82010-12-08 16:14:36 -080039
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070040 setType(type);
Jason Samsbad80742011-03-16 16:29:28 -070041 updateCache();
42}
Jason Samsfa84da22010-03-01 15:31:04 -080043
Jason Samscfea6c12015-02-09 12:50:22 -080044Allocation::Allocation(Context *rsc, const Allocation *alloc, const Type *type)
45 : ObjectBase(rsc) {
46
47 memset(&mHal, 0, sizeof(mHal));
48
49
50 mHal.state.baseAlloc = alloc;
51 mHal.state.type = type;
52 mHal.state.usageFlags = alloc->mHal.state.usageFlags;
53 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
54
55 setType(type);
56 updateCache();
57
58
59
60
61 struct Hal {
62 void * drv;
63
64 struct DrvState {
65 struct LodState {
66 void * mallocPtr;
67 size_t stride;
68 uint32_t dimX;
69 uint32_t dimY;
70 uint32_t dimZ;
71 } lod[android::renderscript::Allocation::MAX_LOD];
72 size_t faceOffset;
73 uint32_t lodCount;
74 uint32_t faceCount;
75
76 struct YuvState {
77 uint32_t shift;
78 uint32_t step;
79 } yuv;
80
81 int grallocFlags;
82 uint32_t dimArray[Type::mMaxArrays];
83 };
84 mutable DrvState drvState;
85
86 };
87 Hal mHal;
88
89}
90
Tim Murray34689382013-03-11 12:12:03 -070091void Allocation::operator delete(void* ptr) {
92 if (ptr) {
93 Allocation *a = (Allocation*) ptr;
94 a->getContext()->mHal.funcs.freeRuntimeMem(ptr);
95 }
96}
97
Jason Samseb4fe182011-05-26 16:33:01 -070098Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080099 RsAllocationMipmapControl mc, void * ptr) {
Tim Murray34689382013-03-11 12:12:03 -0700100 // Allocation objects must use allocator specified by the driver
101 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Allocation), 0);
102
103 if (!allocMem) {
104 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700105 return nullptr;
Tim Murray34689382013-03-11 12:12:03 -0700106 }
107
108 Allocation *a = new (allocMem) Allocation(rsc, type, usages, mc, ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700109
110 if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
111 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
112 delete a;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700113 return nullptr;
Jason Samseb4fe182011-05-26 16:33:01 -0700114 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800115
Jason Samseb4fe182011-05-26 16:33:01 -0700116 return a;
117}
118
Jason Samscfea6c12015-02-09 12:50:22 -0800119Allocation * Allocation::createAdapter(Context *rsc, const Allocation *alloc, const Type *type) {
120 // Allocation objects must use allocator specified by the driver
121 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Allocation), 0);
122
123 if (!allocMem) {
124 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation");
125 return nullptr;
126 }
127
128 Allocation *a = new (allocMem) Allocation(rsc, alloc, type);
129
130 if (!rsc->mHal.funcs.allocation.initAdapter(rsc, a)) {
131 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
132 delete a;
133 return nullptr;
134 }
135
136 return a;
137}
138
Jason Sams442b7ff2015-03-06 16:50:11 -0800139void Allocation::adapterOffset(Context *rsc, const uint32_t *offsets, size_t len) {
140 if (len >= sizeof(uint32_t) * 9) {
141 mHal.state.originX = offsets[0];
142 mHal.state.originY = offsets[1];
143 mHal.state.originZ = offsets[2];
144 mHal.state.originLOD = offsets[3];
145 mHal.state.originFace = offsets[4];
146 mHal.state.originArray[0] = offsets[5];
147 mHal.state.originArray[1] = offsets[6];
148 mHal.state.originArray[2] = offsets[7];
149 mHal.state.originArray[3] = offsets[8];
150 }
151
152 rsc->mHal.funcs.allocation.adapterOffset(rsc, this);
153}
154
155
Jason Samscfea6c12015-02-09 12:50:22 -0800156
Jason Samsbad80742011-03-16 16:29:28 -0700157void Allocation::updateCache() {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700158 const Type *type = mHal.state.type;
Jason Samsa572aca2013-01-09 11:52:26 -0800159 mHal.state.yuv = type->getDimYuv();
Jason Samsbad80742011-03-16 16:29:28 -0700160 mHal.state.hasFaces = type->getDimFaces();
161 mHal.state.hasMipmaps = type->getDimLOD();
162 mHal.state.elementSizeBytes = type->getElementSizeBytes();
163 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -0700164}
165
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800166Allocation::~Allocation() {
Jason Samsddceab92013-08-07 13:02:32 -0700167#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
168 if (mGrallocConsumer.get()) {
169 mGrallocConsumer->unlockBuffer();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700170 mGrallocConsumer = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700171 }
172#endif
173
Jason Samsc7cec1e2011-08-18 18:01:33 -0700174 freeChildrenUnlocked();
Jason Samseb4fe182011-05-26 16:33:01 -0700175 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700176}
177
Jason Sams366c9c82010-12-08 16:14:36 -0800178void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -0700179 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -0800180}
181
Jason Samsb8a94e22014-02-24 17:52:32 -0800182void * Allocation::getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face,
183 uint32_t z, uint32_t array, size_t *stride) {
184
185 if ((lod >= mHal.drvState.lodCount) ||
186 (z && (z >= mHal.drvState.lod[lod].dimZ)) ||
187 ((face != RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X) && !mHal.state.hasFaces) ||
188 (array != 0)) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700189 return nullptr;
Jason Samsb8a94e22014-02-24 17:52:32 -0800190 }
191
192 size_t s = 0;
193 //void *ptr = mRSC->mHal.funcs.allocation.lock1D(rsc, this);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700194 if ((stride != nullptr) && mHal.drvState.lod[0].dimY) {
Jason Samsb8a94e22014-02-24 17:52:32 -0800195 *stride = mHal.drvState.lod[lod].stride;
196 }
197 return mHal.drvState.lod[lod].mallocPtr;
198}
199
Jason Sams4b45b892010-12-29 14:31:29 -0800200void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800201 uint32_t count, const void *data, size_t sizeBytes) {
202 const size_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -0700203
Jason Samseb4fe182011-05-26 16:33:01 -0700204 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800205 char buf[1024];
206 sprintf(buf, "Allocation::subData called with mismatched size expected %zu, got %zu",
207 (count * eSize), sizeBytes);
208 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Samsbad80742011-03-16 16:29:28 -0700209 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700210 return;
211 }
Jason Samse3929c92010-08-09 18:13:33 -0700212
Jason Samseb4fe182011-05-26 16:33:01 -0700213 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
214 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700215}
216
Jason Sams4b45b892010-12-29 14:31:29 -0800217void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800218 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Tim Murray358747a2012-11-26 13:52:04 -0800219 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Samseb4fe182011-05-26 16:33:01 -0700220 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700221}
222
Jason Sams236385b2011-01-12 14:53:25 -0800223void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700224 uint32_t lod,
225 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
226 rsc->mHal.funcs.allocation.data3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
227 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700228}
229
Jason Sams807fdc42012-07-25 17:55:39 -0700230void Allocation::read(Context *rsc, uint32_t xoff, uint32_t lod,
Tim Murray358747a2012-11-26 13:52:04 -0800231 uint32_t count, void *data, size_t sizeBytes) {
Jason Sams807fdc42012-07-25 17:55:39 -0700232 const size_t eSize = mHal.state.type->getElementSizeBytes();
233
234 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800235 char buf[1024];
236 sprintf(buf, "Allocation::read called with mismatched size expected %zu, got %zu",
237 (count * eSize), sizeBytes);
238 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams807fdc42012-07-25 17:55:39 -0700239 mHal.state.type->dumpLOGV("type info");
240 return;
241 }
242
243 rsc->mHal.funcs.allocation.read1D(rsc, this, xoff, lod, count, data, sizeBytes);
244}
245
Tim Murray358747a2012-11-26 13:52:04 -0800246void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
247 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
248 const size_t eSize = mHal.state.elementSizeBytes;
249 const size_t lineSize = eSize * w;
250 if (!stride) {
251 stride = lineSize;
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700252 } else {
253 if ((lineSize * h) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800254 char buf[1024];
255 sprintf(buf, "Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
256 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700257 return;
258 }
Tim Murray358747a2012-11-26 13:52:04 -0800259 }
260
261 rsc->mHal.funcs.allocation.read2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams807fdc42012-07-25 17:55:39 -0700262}
263
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700264void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
265 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride) {
266 const size_t eSize = mHal.state.elementSizeBytes;
267 const size_t lineSize = eSize * w;
268 if (!stride) {
269 stride = lineSize;
270 }
271
272 rsc->mHal.funcs.allocation.read3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
273
Jason Sams807fdc42012-07-25 17:55:39 -0700274}
275
Miao Wangcc8cea72015-02-19 18:14:46 -0800276void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
277 const void *data, uint32_t cIdx, size_t sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800278 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700279
Jason Samsa572aca2013-01-09 11:52:26 -0800280 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700281 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
282 return;
283 }
284
Miao Wangcc8cea72015-02-19 18:14:46 -0800285 if (y > 0 && y >= mHal.drvState.lod[0].dimY) {
286 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Y offset out of range.");
287 return;
288 }
289
290 if (z > 0 && z >= mHal.drvState.lod[0].dimZ) {
291 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Z offset out of range.");
292 return;
293 }
294
295 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
296 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
297 return;
298 }
299
Jason Samsbad80742011-03-16 16:29:28 -0700300 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800301 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
302 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700303 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
304 return;
305 }
306
Miao Wangcc8cea72015-02-19 18:14:46 -0800307 rsc->mHal.funcs.allocation.elementData(rsc, this, x, y, z, data, cIdx, sizeBytes);
Jason Samseb4fe182011-05-26 16:33:01 -0700308 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700309}
310
Miao Wangcc8cea72015-02-19 18:14:46 -0800311void Allocation::elementRead(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
312 void *data, uint32_t cIdx, size_t sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800313 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700314
Jason Samsa572aca2013-01-09 11:52:26 -0800315 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700316 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
317 return;
318 }
319
Miao Wangcc8cea72015-02-19 18:14:46 -0800320 if (y > 0 && y >= mHal.drvState.lod[0].dimY) {
321 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Y offset out of range.");
322 return;
323 }
324
325 if (z > 0 && z >= mHal.drvState.lod[0].dimZ) {
326 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Z offset out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700327 return;
328 }
329
Jason Samsbad80742011-03-16 16:29:28 -0700330 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Yang Nib8353c52015-02-14 18:00:59 -0800331 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700332 return;
333 }
334
Jason Samsbad80742011-03-16 16:29:28 -0700335 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Yang Nib8353c52015-02-14 18:00:59 -0800336 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800337 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700338 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
339 return;
340 }
341
Miao Wangcc8cea72015-02-19 18:14:46 -0800342 rsc->mHal.funcs.allocation.elementRead(rsc, this, x, y, z, data, cIdx, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700343}
344
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800345void Allocation::addProgramToDirty(const Program *p) {
Yang Nib8353c52015-02-14 18:00:59 -0800346 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700347}
Jason Sams326e0dd2009-05-22 14:03:28 -0700348
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800349void Allocation::removeProgramToDirty(const Program *p) {
Yang Nib8353c52015-02-14 18:00:59 -0800350 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
351 if (mToDirtyList[ct] == p) {
352 mToDirtyList.removeAt(ct);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700353 return;
354 }
355 }
356 rsAssert(0);
357}
358
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800359void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800360 ObjectBase::dumpLOGV(prefix);
Jason Sams48ecf6a2013-07-09 15:35:29 -0700361 char buf[1024];
Jason Samsc21cf402009-11-17 17:26:46 -0800362
Jason Sams48ecf6a2013-07-09 15:35:29 -0700363 if ((strlen(prefix) + 10) < sizeof(buf)) {
364 sprintf(buf, "%s type ", prefix);
365 if (mHal.state.type) {
366 mHal.state.type->dumpLOGV(buf);
367 }
Jason Samsc21cf402009-11-17 17:26:46 -0800368 }
Steve Block65982012011-10-20 11:56:00 +0100369 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Yang Nib8353c52015-02-14 18:00:59 -0800370 prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800371}
Jason Sams326e0dd2009-05-22 14:03:28 -0700372
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800373uint32_t Allocation::getPackedSize() const {
Jason Sams61656a72013-09-03 16:21:18 -0700374 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800375 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
376}
377
Jason Samse3150cf2012-07-24 18:10:20 -0700378void Allocation::writePackedData(Context *rsc, const Type *type,
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800379 uint8_t *dst, const uint8_t *src, bool dstPadded) {
380 const Element *elem = type->getElement();
381 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
382 uint32_t paddedBytes = elem->getSizeBytes();
Jason Sams61656a72013-09-03 16:21:18 -0700383 uint32_t numItems = type->getPackedSizeBytes() / paddedBytes;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800384
385 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
386 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
387
388 // no sub-elements
389 uint32_t fieldCount = elem->getFieldCount();
390 if (fieldCount == 0) {
391 for (uint32_t i = 0; i < numItems; i ++) {
392 memcpy(dst, src, unpaddedBytes);
393 src += srcInc;
394 dst += dstInc;
395 }
396 return;
397 }
398
399 // Cache offsets
400 uint32_t *offsetsPadded = new uint32_t[fieldCount];
401 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
402 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
403
404 for (uint32_t i = 0; i < fieldCount; i++) {
405 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
406 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
407 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
408 }
409
410 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
411 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
412
413 // complex elements, need to copy subelem after subelem
414 for (uint32_t i = 0; i < numItems; i ++) {
415 for (uint32_t fI = 0; fI < fieldCount; fI++) {
416 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
417 }
418 src += srcInc;
419 dst += dstInc;
420 }
421
422 delete[] offsetsPadded;
423 delete[] offsetsUnpadded;
424 delete[] sizeUnpadded;
425}
426
Jason Samse3150cf2012-07-24 18:10:20 -0700427void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800428 const uint8_t *src = (const uint8_t*)data;
Jason Sams61a4bb72012-07-25 19:33:43 -0700429 uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800430
Jason Samse3150cf2012-07-24 18:10:20 -0700431 writePackedData(rsc, getType(), dst, src, true);
Jason Sams61a4bb72012-07-25 19:33:43 -0700432 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800433}
434
Jason Samse3150cf2012-07-24 18:10:20 -0700435void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800436 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
437 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
Jason Sams61656a72013-09-03 16:21:18 -0700438 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800439
Jason Sams61a4bb72012-07-25 19:33:43 -0700440 const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800441 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
442
Jason Samse3150cf2012-07-24 18:10:20 -0700443 writePackedData(rsc, getType(), dst, src, false);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800444 stream->addByteArray(dst, getPackedSize());
445
446 delete[] dst;
Jason Sams61a4bb72012-07-25 19:33:43 -0700447 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800448}
449
Jason Samse3150cf2012-07-24 18:10:20 -0700450void Allocation::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700451 // Need to identify ourselves
452 stream->addU32((uint32_t)getClassId());
Jason Sams48ecf6a2013-07-09 15:35:29 -0700453 stream->addString(getName());
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700454
455 // First thing we need to serialize is the type object since it will be needed
456 // to initialize the class
Jason Samse3150cf2012-07-24 18:10:20 -0700457 mHal.state.type->serialize(rsc, stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700458
Jason Sams61656a72013-09-03 16:21:18 -0700459 uint32_t dataSize = mHal.state.type->getPackedSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800460 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
461 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700462 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800463 stream->addU32(packedSize);
464 if (dataSize == packedSize) {
465 // Now write the data
Jason Sams61a4bb72012-07-25 19:33:43 -0700466 stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize);
467 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800468 } else {
469 // Now write the data
Jason Samse3150cf2012-07-24 18:10:20 -0700470 packVec3Allocation(rsc, stream);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800471 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700472}
473
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800474Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700475 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700476 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800477 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Jason Samsa2737932014-01-14 12:28:33 -0800478 rsc->setError(RS_ERROR_FATAL_DRIVER,
479 "allocation loading failed due to corrupt file. (invalid id)\n");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700480 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700481 }
482
Jason Sams48ecf6a2013-07-09 15:35:29 -0700483 const char *name = stream->loadString();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700484
485 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800486 if (!type) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700487 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700488 }
489 type->compute();
490
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800491 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
492 type->decUserRef();
493
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700494 // Number of bytes we wrote out for this allocation
495 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800496 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
497 uint32_t packedSize = alloc->getPackedSize();
Jason Sams61656a72013-09-03 16:21:18 -0700498 if (dataSize != type->getPackedSizeBytes() &&
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800499 dataSize != packedSize) {
Jason Samsa2737932014-01-14 12:28:33 -0800500 rsc->setError(RS_ERROR_FATAL_DRIVER,
501 "allocation loading failed due to corrupt file. (invalid size)\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800502 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700503 ObjectBase::checkDelete(type);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700504 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700505 }
506
Jason Sams48ecf6a2013-07-09 15:35:29 -0700507 alloc->assignName(name);
Jason Sams61656a72013-09-03 16:21:18 -0700508 if (dataSize == type->getPackedSizeBytes()) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800509 uint32_t count = dataSize / type->getElementSizeBytes();
510 // Read in all of our allocation data
511 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
512 } else {
Jason Samse3150cf2012-07-24 18:10:20 -0700513 alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800514 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700515 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700516
517 return alloc;
518}
519
Jason Samseb4fe182011-05-26 16:33:01 -0700520void Allocation::sendDirty(const Context *rsc) const {
Jason Sams93eacc72012-12-18 14:26:57 -0800521#ifndef RS_COMPATIBILITY_LIB
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700522 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
523 mToDirtyList[ct]->forceDirty();
524 }
Jason Sams93eacc72012-12-18 14:26:57 -0800525#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700526 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700527}
Jason Sams326e0dd2009-05-22 14:03:28 -0700528
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800529void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700530 mHal.state.type->incRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700531}
532
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800533void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700534 if (!mHal.state.hasReferences || !getIsScript()) {
535 return;
536 }
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700537 mHal.state.type->decRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700538}
539
Jason Samsa36c50a2014-06-17 12:06:06 -0700540void Allocation::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700541 if (rsc->mHal.funcs.allocation.updateCachedObject != nullptr) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700542 rsc->mHal.funcs.allocation.updateCachedObject(rsc, this, (rs_allocation *)dstObj);
543 } else {
544 *((const void **)dstObj) = this;
545 }
546}
547
548
Jason Samsc7cec1e2011-08-18 18:01:33 -0700549void Allocation::freeChildrenUnlocked () {
Jason Sams61a4bb72012-07-25 19:33:43 -0700550 void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this);
Jason Sams61656a72013-09-03 16:21:18 -0700551 decRefs(ptr, mHal.state.type->getCellCount(), 0);
Jason Sams61a4bb72012-07-25 19:33:43 -0700552 mRSC->mHal.funcs.allocation.unlock1D(mRSC, this);
Jason Samsc7cec1e2011-08-18 18:01:33 -0700553}
554
555bool Allocation::freeChildren() {
556 if (mHal.state.hasReferences) {
557 incSysRef();
558 freeChildrenUnlocked();
559 return decSysRef();
560 }
561 return false;
562}
563
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800564void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700565}
566
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800567void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsa572aca2013-01-09 11:52:26 -0800568 uint32_t oldDimX = mHal.drvState.lod[0].dimX;
Jason Sams96abf812010-10-05 13:32:49 -0700569 if (dimX == oldDimX) {
570 return;
571 }
572
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700573 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700574 if (dimX < oldDimX) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700575 decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX);
576 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Jason Sams96abf812010-10-05 13:32:49 -0700577 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700578 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700579 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700580 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700581}
582
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800583void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Samsa2737932014-01-14 12:28:33 -0800584 rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700585}
586
Jason Samsddceab92013-08-07 13:02:32 -0700587#ifndef RS_COMPATIBILITY_LIB
Dan Stozaf0d7aa22014-11-04 11:38:47 -0800588void Allocation::NewBufferListener::onFrameAvailable(const BufferItem& /* item */) {
Jason Samsddceab92013-08-07 13:02:32 -0700589 intptr_t ip = (intptr_t)alloc;
Tim Murraye4ed0872014-08-18 16:13:08 -0700590 rsc->sendMessageToClient(&ip, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, 0, sizeof(ip), true);
Jason Samsddceab92013-08-07 13:02:32 -0700591}
592#endif
593
Jason Sams733396b2013-02-22 12:46:18 -0800594void * Allocation::getSurface(const Context *rsc) {
Jason Samsddceab92013-08-07 13:02:32 -0700595#ifndef RS_COMPATIBILITY_LIB
596 // Configure GrallocConsumer to be in asynchronous mode
Dan Stoza03c155b2014-03-12 12:06:18 -0700597 sp<IGraphicBufferProducer> bp;
598 sp<IGraphicBufferConsumer> bc;
599 BufferQueue::createBufferQueue(&bp, &bc);
Jason Samse49da132014-10-23 17:32:27 -0700600 mGrallocConsumer = new GrallocConsumer(this, bc, mHal.drvState.grallocFlags);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700601 bp->incStrong(nullptr);
Jason Samsddceab92013-08-07 13:02:32 -0700602
603 mBufferListener = new NewBufferListener();
604 mBufferListener->rsc = rsc;
605 mBufferListener->alloc = this;
606
607 mGrallocConsumer->setFrameAvailableListener(mBufferListener);
608 return bp.get();
609#else
Chris Wailes44bef6f2014-08-12 13:51:10 -0700610 return nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700611#endif
612 //return rsc->mHal.funcs.allocation.getSurface(rsc, this);
Jason Sams41e373d2012-01-13 14:01:20 -0800613}
614
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800615void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
616 ANativeWindow *nw = (ANativeWindow *)sur;
Jason Sams733396b2013-02-22 12:46:18 -0800617 rsc->mHal.funcs.allocation.setSurface(rsc, this, nw);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800618}
619
620void Allocation::ioSend(const Context *rsc) {
621 rsc->mHal.funcs.allocation.ioSend(rsc, this);
622}
623
624void Allocation::ioReceive(const Context *rsc) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700625 void *ptr = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700626 size_t stride = 0;
627#ifndef RS_COMPATIBILITY_LIB
628 if (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
629 status_t ret = mGrallocConsumer->lockNextBuffer();
630
631 if (ret == OK) {
632 rsc->mHal.funcs.allocation.ioReceive(rsc, this);
633 } else if (ret == BAD_VALUE) {
634 // No new frame, don't do anything
635 } else {
636 rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer.");
637 }
638
639 }
640#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800641}
642
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700643bool Allocation::hasSameDims(const Allocation *other) const {
644 const Type *type0 = this->getType(),
645 *type1 = other->getType();
646
647 return (type0->getCellCount() == type1->getCellCount()) &&
648 (type0->getDimLOD() == type1->getDimLOD()) &&
649 (type0->getDimFaces() == type1->getDimFaces()) &&
650 (type0->getDimYuv() == type1->getDimYuv()) &&
651 (type0->getDimX() == type1->getDimX()) &&
652 (type0->getDimY() == type1->getDimY()) &&
653 (type0->getDimZ() == type1->getDimZ());
654}
655
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800656
Jason Sams326e0dd2009-05-22 14:03:28 -0700657/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700658//
Stephen Hines6a121812011-03-01 17:34:59 -0800659
Jason Sams326e0dd2009-05-22 14:03:28 -0700660namespace android {
661namespace renderscript {
662
Jason Sams366c9c82010-12-08 16:14:36 -0800663void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
664 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700665 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800666 a->syncAll(rsc, src);
667}
668
Jason Samsa2371512011-01-12 13:28:37 -0800669void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700670 Allocation *alloc = static_cast<Allocation *>(va);
671 rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc);
Jason Sams837e3882010-12-10 16:03:15 -0800672}
673
Jason Sams807fdc42012-07-25 17:55:39 -0700674void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
675 Allocation *a = static_cast<Allocation *>(va);
676 const Type * t = a->getType();
677 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700678 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700679}
680
Jason Sams4b45b892010-12-29 14:31:29 -0800681void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700682 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700683 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800684 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700685}
686
Miao Wangcc8cea72015-02-19 18:14:46 -0800687void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x,
688 uint32_t lod, const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700689 Allocation *a = static_cast<Allocation *>(va);
Miao Wangcc8cea72015-02-19 18:14:46 -0800690 a->elementData(rsc, x, 0, 0, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700691}
692
Miao Wangcc8cea72015-02-19 18:14:46 -0800693void rsi_AllocationElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
694 uint32_t lod, const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700695 Allocation *a = static_cast<Allocation *>(va);
Miao Wangcc8cea72015-02-19 18:14:46 -0800696 a->elementData(rsc, x, y, z, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700697}
698
Jason Sams4b45b892010-12-29 14:31:29 -0800699void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800700 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700701 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800702 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams326e0dd2009-05-22 14:03:28 -0700703}
704
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700705void rsi_Allocation3DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
706 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
707 Allocation *a = static_cast<Allocation *>(va);
708 a->data(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
709}
710
711
Jason Sams807fdc42012-07-25 17:55:39 -0700712void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
Jason Samse579df42009-08-10 14:55:26 -0700713 Allocation *a = static_cast<Allocation *>(va);
Jason Sams807fdc42012-07-25 17:55:39 -0700714 const Type * t = a->getType();
Miao Wangcc8cea72015-02-19 18:14:46 -0800715 if(t->getDimZ()) {
716 a->read(rsc, 0, 0, 0, 0, t->getDimX(), t->getDimY(), t->getDimZ(),
717 data, sizeBytes, 0);
718 } else if(t->getDimY()) {
Jason Sams807fdc42012-07-25 17:55:39 -0700719 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700720 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Jason Sams807fdc42012-07-25 17:55:39 -0700721 } else {
722 a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes);
723 }
724
Jason Samse579df42009-08-10 14:55:26 -0700725}
726
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800727void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700728 Allocation *a = static_cast<Allocation *>(va);
729 a->resize1D(rsc, dimX);
730}
731
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800732void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700733 Allocation *a = static_cast<Allocation *>(va);
734 a->resize2D(rsc, dimX, dimY);
735}
736
Jason Samsc975cf42011-04-28 18:26:48 -0700737RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800738 RsAllocationMipmapControl mipmaps,
Tim Murray099bc262013-03-20 16:54:03 -0700739 uint32_t usages, uintptr_t ptr) {
Stephen Hines8f615d62013-12-20 12:23:32 -0800740 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mipmaps, (void*)ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700741 if (!alloc) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700742 return nullptr;
Jason Samseb4fe182011-05-26 16:33:01 -0700743 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700744 alloc->incUserRef();
745 return alloc;
746}
747
Jason Samsc975cf42011-04-28 18:26:48 -0700748RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800749 RsAllocationMipmapControl mipmaps,
Jason Sams807fdc42012-07-25 17:55:39 -0700750 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800751 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700752
Stephen Hines8f615d62013-12-20 12:23:32 -0800753 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700754 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700755 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000756 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700757 return nullptr;
Jason Samsf0c1df42010-10-26 13:09:17 -0700758 }
759
Jason Sams807fdc42012-07-25 17:55:39 -0700760 texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray60c27962013-02-07 16:15:23 -0800761 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Stephen Hines8f615d62013-12-20 12:23:32 -0800762 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700763 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700764 }
765
Jason Samseb4fe182011-05-26 16:33:01 -0700766 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700767 return texAlloc;
768}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800769
Jason Samsc975cf42011-04-28 18:26:48 -0700770RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800771 RsAllocationMipmapControl mipmaps,
Jason Sams807fdc42012-07-25 17:55:39 -0700772 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800773 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800774
775 // Cubemap allocation's faces should be Width by Width each.
776 // Source data should have 6 * Width by Width pixels
777 // Error checking is done in the java layer
Stephen Hines8f615d62013-12-20 12:23:32 -0800778 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800779 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700780 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000781 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700782 return nullptr;
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800783 }
784
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800785 uint32_t faceSize = t->getDimX();
786 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
787 uint32_t copySize = faceSize * t->getElementSizeBytes();
788
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800789 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800790 for (uint32_t face = 0; face < 6; face ++) {
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800791 for (uint32_t dI = 0; dI < faceSize; dI ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700792 texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face,
Tim Murray60c27962013-02-07 16:15:23 -0800793 t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize, 0);
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800794 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800795
Jason Sams366c9c82010-12-08 16:14:36 -0800796 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800797 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800798 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800799
Stephen Hines8f615d62013-12-20 12:23:32 -0800800 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700801 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800802 }
803
Jason Samseb4fe182011-05-26 16:33:01 -0700804 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800805 return texAlloc;
806}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800807
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700808void rsi_AllocationCopy2DRange(Context *rsc,
809 RsAllocation dstAlloc,
810 uint32_t dstXoff, uint32_t dstYoff,
811 uint32_t dstMip, uint32_t dstFace,
812 uint32_t width, uint32_t height,
813 RsAllocation srcAlloc,
814 uint32_t srcXoff, uint32_t srcYoff,
815 uint32_t srcMip, uint32_t srcFace) {
816 Allocation *dst = static_cast<Allocation *>(dstAlloc);
817 Allocation *src= static_cast<Allocation *>(srcAlloc);
818 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
819 (RsAllocationCubemapFace)dstFace,
820 width, height,
821 src, srcXoff, srcYoff,srcMip,
822 (RsAllocationCubemapFace)srcFace);
823}
824
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700825void rsi_AllocationCopy3DRange(Context *rsc,
826 RsAllocation dstAlloc,
827 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
828 uint32_t dstMip,
829 uint32_t width, uint32_t height, uint32_t depth,
830 RsAllocation srcAlloc,
831 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
832 uint32_t srcMip) {
833 Allocation *dst = static_cast<Allocation *>(dstAlloc);
834 Allocation *src= static_cast<Allocation *>(srcAlloc);
835 rsc->mHal.funcs.allocation.allocData3D(rsc, dst, dstXoff, dstYoff, dstZoff, dstMip,
836 width, height, depth,
837 src, srcXoff, srcYoff, srcZoff, srcMip);
838}
839
840
Jason Sams733396b2013-02-22 12:46:18 -0800841void * rsi_AllocationGetSurface(Context *rsc, RsAllocation valloc) {
Jason Sams41e373d2012-01-13 14:01:20 -0800842 Allocation *alloc = static_cast<Allocation *>(valloc);
Jason Sams733396b2013-02-22 12:46:18 -0800843 void *s = alloc->getSurface(rsc);
844 return s;
Jason Sams3522f402012-03-23 11:47:26 -0700845}
846
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800847void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
848 Allocation *alloc = static_cast<Allocation *>(valloc);
849 alloc->setSurface(rsc, sur);
850}
851
852void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
853 Allocation *alloc = static_cast<Allocation *>(valloc);
854 alloc->ioSend(rsc);
855}
856
857void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
858 Allocation *alloc = static_cast<Allocation *>(valloc);
859 alloc->ioReceive(rsc);
860}
861
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700862void *rsi_AllocationGetPointer(Context *rsc, RsAllocation valloc,
Jason Samsb8a94e22014-02-24 17:52:32 -0800863 uint32_t lod, RsAllocationCubemapFace face,
864 uint32_t z, uint32_t array, size_t *stride, size_t strideLen) {
865 Allocation *alloc = static_cast<Allocation *>(valloc);
Tim Murray0e61af92014-02-26 13:28:52 -0800866 rsAssert(strideLen == sizeof(size_t));
Jason Samsb8a94e22014-02-24 17:52:32 -0800867
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700868 return alloc->getPointer(rsc, lod, face, z, array, stride);
Jason Samsb8a94e22014-02-24 17:52:32 -0800869}
870
Tim Murray509ea5c2012-11-13 11:56:40 -0800871void rsi_Allocation1DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
872 uint32_t count, void *data, size_t sizeBytes) {
873 Allocation *a = static_cast<Allocation *>(va);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700874 rsc->mHal.funcs.allocation.read1D(rsc, a, xoff, lod, count, data, sizeBytes);
Tim Murray509ea5c2012-11-13 11:56:40 -0800875}
876
Miao Wangcc8cea72015-02-19 18:14:46 -0800877void rsi_AllocationElementRead(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
878 uint32_t lod, void *data, size_t sizeBytes, size_t eoff) {
879 Allocation *a = static_cast<Allocation *>(va);
880 a->elementRead(rsc, x, y, z, data, eoff, sizeBytes);
881}
882
Tim Murray7b3e3092012-11-16 13:32:24 -0800883void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff,
884 uint32_t lod, RsAllocationCubemapFace face, uint32_t w,
Tim Murray358747a2012-11-26 13:52:04 -0800885 uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Tim Murray7b3e3092012-11-16 13:32:24 -0800886 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800887 a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Tim Murray7b3e3092012-11-16 13:32:24 -0800888}
889
Miao Wangcc8cea72015-02-19 18:14:46 -0800890void rsi_Allocation3DRead(Context *rsc, RsAllocation va,
891 uint32_t xoff, uint32_t yoff, uint32_t zoff,
892 uint32_t lod, uint32_t w, uint32_t h, uint32_t d,
893 void *data, size_t sizeBytes, size_t stride) {
894 Allocation *a = static_cast<Allocation *>(va);
895 a->read(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
896}
897
Jason Samscfea6c12015-02-09 12:50:22 -0800898RsAllocation rsi_AllocationAdapterCreate(Context *rsc, RsType vwindow, RsAllocation vbase) {
899
900
901 Allocation * alloc = Allocation::createAdapter(rsc,
902 static_cast<Allocation *>(vbase), static_cast<Type *>(vwindow));
903 if (!alloc) {
904 return nullptr;
905 }
906 alloc->incUserRef();
907 return alloc;
908}
909
910void rsi_AllocationAdapterOffset(Context *rsc, RsAllocation va, const uint32_t *offsets, size_t len) {
Jason Sams442b7ff2015-03-06 16:50:11 -0800911 Allocation *a = static_cast<Allocation *>(va);
912 a->adapterOffset(rsc, offsets, len);
Jason Samscfea6c12015-02-09 12:50:22 -0800913}
914
915
Jason Samsc975cf42011-04-28 18:26:48 -0700916}
917}
918
Tim Murrayc2ce7072013-07-17 18:38:53 -0700919extern "C" const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Jason Samsc975cf42011-04-28 18:26:48 -0700920 Allocation *a = static_cast<Allocation *>(va);
921 a->getType()->incUserRef();
922
923 return a->getType();
924}