blob: 1d3daaf679f869bfa00ad89b57c2c9aca71bd2db [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 Samsbc9dc272015-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 Samsbc9dc272015-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
139
Jason Samsbad80742011-03-16 16:29:28 -0700140void Allocation::updateCache() {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700141 const Type *type = mHal.state.type;
Jason Samsa572aca2013-01-09 11:52:26 -0800142 mHal.state.yuv = type->getDimYuv();
Jason Samsbad80742011-03-16 16:29:28 -0700143 mHal.state.hasFaces = type->getDimFaces();
144 mHal.state.hasMipmaps = type->getDimLOD();
145 mHal.state.elementSizeBytes = type->getElementSizeBytes();
146 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -0700147}
148
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800149Allocation::~Allocation() {
Jason Samsddceab92013-08-07 13:02:32 -0700150#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
151 if (mGrallocConsumer.get()) {
152 mGrallocConsumer->unlockBuffer();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700153 mGrallocConsumer = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700154 }
155#endif
156
Jason Samsc7cec1e2011-08-18 18:01:33 -0700157 freeChildrenUnlocked();
Jason Samseb4fe182011-05-26 16:33:01 -0700158 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700159}
160
Jason Sams366c9c82010-12-08 16:14:36 -0800161void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -0700162 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -0800163}
164
Jason Samsb8a94e22014-02-24 17:52:32 -0800165void * Allocation::getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face,
166 uint32_t z, uint32_t array, size_t *stride) {
167
168 if ((lod >= mHal.drvState.lodCount) ||
169 (z && (z >= mHal.drvState.lod[lod].dimZ)) ||
170 ((face != RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X) && !mHal.state.hasFaces) ||
171 (array != 0)) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700172 return nullptr;
Jason Samsb8a94e22014-02-24 17:52:32 -0800173 }
174
175 size_t s = 0;
176 //void *ptr = mRSC->mHal.funcs.allocation.lock1D(rsc, this);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700177 if ((stride != nullptr) && mHal.drvState.lod[0].dimY) {
Jason Samsb8a94e22014-02-24 17:52:32 -0800178 *stride = mHal.drvState.lod[lod].stride;
179 }
180 return mHal.drvState.lod[lod].mallocPtr;
181}
182
Jason Sams4b45b892010-12-29 14:31:29 -0800183void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800184 uint32_t count, const void *data, size_t sizeBytes) {
185 const size_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -0700186
Jason Samseb4fe182011-05-26 16:33:01 -0700187 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800188 char buf[1024];
189 sprintf(buf, "Allocation::subData called with mismatched size expected %zu, got %zu",
190 (count * eSize), sizeBytes);
191 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Samsbad80742011-03-16 16:29:28 -0700192 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700193 return;
194 }
Jason Samse3929c92010-08-09 18:13:33 -0700195
Jason Samseb4fe182011-05-26 16:33:01 -0700196 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
197 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700198}
199
Jason Sams4b45b892010-12-29 14:31:29 -0800200void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800201 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Tim Murray358747a2012-11-26 13:52:04 -0800202 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Samseb4fe182011-05-26 16:33:01 -0700203 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700204}
205
Jason Sams236385b2011-01-12 14:53:25 -0800206void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700207 uint32_t lod,
208 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
209 rsc->mHal.funcs.allocation.data3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
210 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700211}
212
Jason Sams807fdc42012-07-25 17:55:39 -0700213void Allocation::read(Context *rsc, uint32_t xoff, uint32_t lod,
Tim Murray358747a2012-11-26 13:52:04 -0800214 uint32_t count, void *data, size_t sizeBytes) {
Jason Sams807fdc42012-07-25 17:55:39 -0700215 const size_t eSize = mHal.state.type->getElementSizeBytes();
216
217 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800218 char buf[1024];
219 sprintf(buf, "Allocation::read called with mismatched size expected %zu, got %zu",
220 (count * eSize), sizeBytes);
221 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams807fdc42012-07-25 17:55:39 -0700222 mHal.state.type->dumpLOGV("type info");
223 return;
224 }
225
226 rsc->mHal.funcs.allocation.read1D(rsc, this, xoff, lod, count, data, sizeBytes);
227}
228
Tim Murray358747a2012-11-26 13:52:04 -0800229void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
230 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
231 const size_t eSize = mHal.state.elementSizeBytes;
232 const size_t lineSize = eSize * w;
233 if (!stride) {
234 stride = lineSize;
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700235 } else {
236 if ((lineSize * h) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800237 char buf[1024];
238 sprintf(buf, "Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
239 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700240 return;
241 }
Tim Murray358747a2012-11-26 13:52:04 -0800242 }
243
244 rsc->mHal.funcs.allocation.read2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams807fdc42012-07-25 17:55:39 -0700245}
246
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700247void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
248 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride) {
249 const size_t eSize = mHal.state.elementSizeBytes;
250 const size_t lineSize = eSize * w;
251 if (!stride) {
252 stride = lineSize;
253 }
254
255 rsc->mHal.funcs.allocation.read3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
256
Jason Sams807fdc42012-07-25 17:55:39 -0700257}
258
Miao Wangcc8cea72015-02-19 18:14:46 -0800259void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
260 const void *data, uint32_t cIdx, size_t sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800261 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700262
Jason Samsa572aca2013-01-09 11:52:26 -0800263 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700264 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
265 return;
266 }
267
Miao Wangcc8cea72015-02-19 18:14:46 -0800268 if (y > 0 && y >= mHal.drvState.lod[0].dimY) {
269 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Y offset out of range.");
270 return;
271 }
272
273 if (z > 0 && z >= mHal.drvState.lod[0].dimZ) {
274 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Z offset out of range.");
275 return;
276 }
277
278 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
279 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
280 return;
281 }
282
Jason Samsbad80742011-03-16 16:29:28 -0700283 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800284 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
285 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700286 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
287 return;
288 }
289
Miao Wangcc8cea72015-02-19 18:14:46 -0800290 rsc->mHal.funcs.allocation.elementData(rsc, this, x, y, z, data, cIdx, sizeBytes);
Jason Samseb4fe182011-05-26 16:33:01 -0700291 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700292}
293
Miao Wangcc8cea72015-02-19 18:14:46 -0800294void Allocation::elementRead(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
295 void *data, uint32_t cIdx, size_t sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800296 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700297
Jason Samsa572aca2013-01-09 11:52:26 -0800298 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700299 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
300 return;
301 }
302
Miao Wangcc8cea72015-02-19 18:14:46 -0800303 if (y > 0 && y >= mHal.drvState.lod[0].dimY) {
304 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Y offset out of range.");
305 return;
306 }
307
308 if (z > 0 && z >= mHal.drvState.lod[0].dimZ) {
309 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Z offset out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700310 return;
311 }
312
Jason Samsbad80742011-03-16 16:29:28 -0700313 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Yang Nib8353c52015-02-14 18:00:59 -0800314 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700315 return;
316 }
317
Jason Samsbad80742011-03-16 16:29:28 -0700318 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Yang Nib8353c52015-02-14 18:00:59 -0800319 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800320 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700321 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
322 return;
323 }
324
Miao Wangcc8cea72015-02-19 18:14:46 -0800325 rsc->mHal.funcs.allocation.elementRead(rsc, this, x, y, z, data, cIdx, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700326}
327
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800328void Allocation::addProgramToDirty(const Program *p) {
Yang Nib8353c52015-02-14 18:00:59 -0800329 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700330}
Jason Sams326e0dd2009-05-22 14:03:28 -0700331
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800332void Allocation::removeProgramToDirty(const Program *p) {
Yang Nib8353c52015-02-14 18:00:59 -0800333 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
334 if (mToDirtyList[ct] == p) {
335 mToDirtyList.removeAt(ct);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700336 return;
337 }
338 }
339 rsAssert(0);
340}
341
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800342void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800343 ObjectBase::dumpLOGV(prefix);
Jason Sams48ecf6a2013-07-09 15:35:29 -0700344 char buf[1024];
Jason Samsc21cf402009-11-17 17:26:46 -0800345
Jason Sams48ecf6a2013-07-09 15:35:29 -0700346 if ((strlen(prefix) + 10) < sizeof(buf)) {
347 sprintf(buf, "%s type ", prefix);
348 if (mHal.state.type) {
349 mHal.state.type->dumpLOGV(buf);
350 }
Jason Samsc21cf402009-11-17 17:26:46 -0800351 }
Steve Block65982012011-10-20 11:56:00 +0100352 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Yang Nib8353c52015-02-14 18:00:59 -0800353 prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800354}
Jason Sams326e0dd2009-05-22 14:03:28 -0700355
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800356uint32_t Allocation::getPackedSize() const {
Jason Sams61656a72013-09-03 16:21:18 -0700357 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800358 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
359}
360
Jason Samse3150cf2012-07-24 18:10:20 -0700361void Allocation::writePackedData(Context *rsc, const Type *type,
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800362 uint8_t *dst, const uint8_t *src, bool dstPadded) {
363 const Element *elem = type->getElement();
364 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
365 uint32_t paddedBytes = elem->getSizeBytes();
Jason Sams61656a72013-09-03 16:21:18 -0700366 uint32_t numItems = type->getPackedSizeBytes() / paddedBytes;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800367
368 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
369 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
370
371 // no sub-elements
372 uint32_t fieldCount = elem->getFieldCount();
373 if (fieldCount == 0) {
374 for (uint32_t i = 0; i < numItems; i ++) {
375 memcpy(dst, src, unpaddedBytes);
376 src += srcInc;
377 dst += dstInc;
378 }
379 return;
380 }
381
382 // Cache offsets
383 uint32_t *offsetsPadded = new uint32_t[fieldCount];
384 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
385 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
386
387 for (uint32_t i = 0; i < fieldCount; i++) {
388 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
389 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
390 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
391 }
392
393 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
394 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
395
396 // complex elements, need to copy subelem after subelem
397 for (uint32_t i = 0; i < numItems; i ++) {
398 for (uint32_t fI = 0; fI < fieldCount; fI++) {
399 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
400 }
401 src += srcInc;
402 dst += dstInc;
403 }
404
405 delete[] offsetsPadded;
406 delete[] offsetsUnpadded;
407 delete[] sizeUnpadded;
408}
409
Jason Samse3150cf2012-07-24 18:10:20 -0700410void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800411 const uint8_t *src = (const uint8_t*)data;
Jason Sams61a4bb72012-07-25 19:33:43 -0700412 uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800413
Jason Samse3150cf2012-07-24 18:10:20 -0700414 writePackedData(rsc, getType(), dst, src, true);
Jason Sams61a4bb72012-07-25 19:33:43 -0700415 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800416}
417
Jason Samse3150cf2012-07-24 18:10:20 -0700418void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800419 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
420 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
Jason Sams61656a72013-09-03 16:21:18 -0700421 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800422
Jason Sams61a4bb72012-07-25 19:33:43 -0700423 const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800424 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
425
Jason Samse3150cf2012-07-24 18:10:20 -0700426 writePackedData(rsc, getType(), dst, src, false);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800427 stream->addByteArray(dst, getPackedSize());
428
429 delete[] dst;
Jason Sams61a4bb72012-07-25 19:33:43 -0700430 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800431}
432
Jason Samse3150cf2012-07-24 18:10:20 -0700433void Allocation::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700434 // Need to identify ourselves
435 stream->addU32((uint32_t)getClassId());
Jason Sams48ecf6a2013-07-09 15:35:29 -0700436 stream->addString(getName());
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700437
438 // First thing we need to serialize is the type object since it will be needed
439 // to initialize the class
Jason Samse3150cf2012-07-24 18:10:20 -0700440 mHal.state.type->serialize(rsc, stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700441
Jason Sams61656a72013-09-03 16:21:18 -0700442 uint32_t dataSize = mHal.state.type->getPackedSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800443 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
444 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700445 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800446 stream->addU32(packedSize);
447 if (dataSize == packedSize) {
448 // Now write the data
Jason Sams61a4bb72012-07-25 19:33:43 -0700449 stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize);
450 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800451 } else {
452 // Now write the data
Jason Samse3150cf2012-07-24 18:10:20 -0700453 packVec3Allocation(rsc, stream);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800454 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700455}
456
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800457Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700458 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700459 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800460 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Jason Samsa2737932014-01-14 12:28:33 -0800461 rsc->setError(RS_ERROR_FATAL_DRIVER,
462 "allocation loading failed due to corrupt file. (invalid id)\n");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700463 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700464 }
465
Jason Sams48ecf6a2013-07-09 15:35:29 -0700466 const char *name = stream->loadString();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700467
468 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800469 if (!type) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700470 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700471 }
472 type->compute();
473
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800474 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
475 type->decUserRef();
476
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700477 // Number of bytes we wrote out for this allocation
478 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800479 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
480 uint32_t packedSize = alloc->getPackedSize();
Jason Sams61656a72013-09-03 16:21:18 -0700481 if (dataSize != type->getPackedSizeBytes() &&
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800482 dataSize != packedSize) {
Jason Samsa2737932014-01-14 12:28:33 -0800483 rsc->setError(RS_ERROR_FATAL_DRIVER,
484 "allocation loading failed due to corrupt file. (invalid size)\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800485 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700486 ObjectBase::checkDelete(type);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700487 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700488 }
489
Jason Sams48ecf6a2013-07-09 15:35:29 -0700490 alloc->assignName(name);
Jason Sams61656a72013-09-03 16:21:18 -0700491 if (dataSize == type->getPackedSizeBytes()) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800492 uint32_t count = dataSize / type->getElementSizeBytes();
493 // Read in all of our allocation data
494 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
495 } else {
Jason Samse3150cf2012-07-24 18:10:20 -0700496 alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800497 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700498 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700499
500 return alloc;
501}
502
Jason Samseb4fe182011-05-26 16:33:01 -0700503void Allocation::sendDirty(const Context *rsc) const {
Jason Sams93eacc72012-12-18 14:26:57 -0800504#ifndef RS_COMPATIBILITY_LIB
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700505 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
506 mToDirtyList[ct]->forceDirty();
507 }
Jason Sams93eacc72012-12-18 14:26:57 -0800508#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700509 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700510}
Jason Sams326e0dd2009-05-22 14:03:28 -0700511
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800512void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700513 mHal.state.type->incRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700514}
515
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800516void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700517 if (!mHal.state.hasReferences || !getIsScript()) {
518 return;
519 }
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700520 mHal.state.type->decRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700521}
522
Jason Samsa36c50a2014-06-17 12:06:06 -0700523void Allocation::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700524 if (rsc->mHal.funcs.allocation.updateCachedObject != nullptr) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700525 rsc->mHal.funcs.allocation.updateCachedObject(rsc, this, (rs_allocation *)dstObj);
526 } else {
527 *((const void **)dstObj) = this;
528 }
529}
530
531
Jason Samsc7cec1e2011-08-18 18:01:33 -0700532void Allocation::freeChildrenUnlocked () {
Jason Sams61a4bb72012-07-25 19:33:43 -0700533 void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this);
Jason Sams61656a72013-09-03 16:21:18 -0700534 decRefs(ptr, mHal.state.type->getCellCount(), 0);
Jason Sams61a4bb72012-07-25 19:33:43 -0700535 mRSC->mHal.funcs.allocation.unlock1D(mRSC, this);
Jason Samsc7cec1e2011-08-18 18:01:33 -0700536}
537
538bool Allocation::freeChildren() {
539 if (mHal.state.hasReferences) {
540 incSysRef();
541 freeChildrenUnlocked();
542 return decSysRef();
543 }
544 return false;
545}
546
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800547void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700548}
549
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800550void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsa572aca2013-01-09 11:52:26 -0800551 uint32_t oldDimX = mHal.drvState.lod[0].dimX;
Jason Sams96abf812010-10-05 13:32:49 -0700552 if (dimX == oldDimX) {
553 return;
554 }
555
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700556 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700557 if (dimX < oldDimX) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700558 decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX);
559 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Jason Sams96abf812010-10-05 13:32:49 -0700560 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700561 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700562 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700563 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700564}
565
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800566void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Samsa2737932014-01-14 12:28:33 -0800567 rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700568}
569
Jason Samsddceab92013-08-07 13:02:32 -0700570#ifndef RS_COMPATIBILITY_LIB
571void Allocation::NewBufferListener::onFrameAvailable() {
572 intptr_t ip = (intptr_t)alloc;
Tim Murraye4ed0872014-08-18 16:13:08 -0700573 rsc->sendMessageToClient(&ip, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, 0, sizeof(ip), true);
Jason Samsddceab92013-08-07 13:02:32 -0700574}
575#endif
576
Jason Sams733396b2013-02-22 12:46:18 -0800577void * Allocation::getSurface(const Context *rsc) {
Jason Samsddceab92013-08-07 13:02:32 -0700578#ifndef RS_COMPATIBILITY_LIB
579 // Configure GrallocConsumer to be in asynchronous mode
Dan Stoza03c155b2014-03-12 12:06:18 -0700580 sp<IGraphicBufferProducer> bp;
581 sp<IGraphicBufferConsumer> bc;
582 BufferQueue::createBufferQueue(&bp, &bc);
Jason Sams1c19f052014-11-10 12:11:37 -0800583 mGrallocConsumer = new GrallocConsumer(this, bc, mHal.drvState.grallocFlags);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700584 bp->incStrong(nullptr);
Jason Samsddceab92013-08-07 13:02:32 -0700585
586 mBufferListener = new NewBufferListener();
587 mBufferListener->rsc = rsc;
588 mBufferListener->alloc = this;
589
590 mGrallocConsumer->setFrameAvailableListener(mBufferListener);
591 return bp.get();
592#else
Chris Wailes44bef6f2014-08-12 13:51:10 -0700593 return nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700594#endif
595 //return rsc->mHal.funcs.allocation.getSurface(rsc, this);
Jason Sams41e373d2012-01-13 14:01:20 -0800596}
597
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800598void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
599 ANativeWindow *nw = (ANativeWindow *)sur;
Jason Sams733396b2013-02-22 12:46:18 -0800600 rsc->mHal.funcs.allocation.setSurface(rsc, this, nw);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800601}
602
603void Allocation::ioSend(const Context *rsc) {
604 rsc->mHal.funcs.allocation.ioSend(rsc, this);
605}
606
607void Allocation::ioReceive(const Context *rsc) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700608 void *ptr = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700609 size_t stride = 0;
610#ifndef RS_COMPATIBILITY_LIB
611 if (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
612 status_t ret = mGrallocConsumer->lockNextBuffer();
613
614 if (ret == OK) {
615 rsc->mHal.funcs.allocation.ioReceive(rsc, this);
616 } else if (ret == BAD_VALUE) {
617 // No new frame, don't do anything
618 } else {
619 rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer.");
620 }
621
622 }
623#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800624}
625
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700626bool Allocation::hasSameDims(const Allocation *other) const {
627 const Type *type0 = this->getType(),
628 *type1 = other->getType();
629
630 return (type0->getCellCount() == type1->getCellCount()) &&
631 (type0->getDimLOD() == type1->getDimLOD()) &&
632 (type0->getDimFaces() == type1->getDimFaces()) &&
633 (type0->getDimYuv() == type1->getDimYuv()) &&
634 (type0->getDimX() == type1->getDimX()) &&
635 (type0->getDimY() == type1->getDimY()) &&
636 (type0->getDimZ() == type1->getDimZ());
637}
638
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800639
Jason Sams326e0dd2009-05-22 14:03:28 -0700640/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700641//
Stephen Hines6a121812011-03-01 17:34:59 -0800642
Jason Sams326e0dd2009-05-22 14:03:28 -0700643namespace android {
644namespace renderscript {
645
Jason Sams366c9c82010-12-08 16:14:36 -0800646void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
647 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700648 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800649 a->syncAll(rsc, src);
650}
651
Jason Samsa2371512011-01-12 13:28:37 -0800652void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700653 Allocation *alloc = static_cast<Allocation *>(va);
654 rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc);
Jason Sams837e3882010-12-10 16:03:15 -0800655}
656
Jason Sams807fdc42012-07-25 17:55:39 -0700657void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
658 Allocation *a = static_cast<Allocation *>(va);
659 const Type * t = a->getType();
660 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700661 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700662}
663
Jason Sams4b45b892010-12-29 14:31:29 -0800664void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700665 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700666 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800667 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700668}
669
Miao Wangcc8cea72015-02-19 18:14:46 -0800670void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x,
671 uint32_t lod, const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700672 Allocation *a = static_cast<Allocation *>(va);
Miao Wangcc8cea72015-02-19 18:14:46 -0800673 a->elementData(rsc, x, 0, 0, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700674}
675
Miao Wangcc8cea72015-02-19 18:14:46 -0800676void rsi_AllocationElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
677 uint32_t lod, const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700678 Allocation *a = static_cast<Allocation *>(va);
Miao Wangcc8cea72015-02-19 18:14:46 -0800679 a->elementData(rsc, x, y, z, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700680}
681
Jason Sams4b45b892010-12-29 14:31:29 -0800682void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800683 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700684 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800685 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams326e0dd2009-05-22 14:03:28 -0700686}
687
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700688void rsi_Allocation3DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
689 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
690 Allocation *a = static_cast<Allocation *>(va);
691 a->data(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
692}
693
694
Jason Sams807fdc42012-07-25 17:55:39 -0700695void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
Jason Samse579df42009-08-10 14:55:26 -0700696 Allocation *a = static_cast<Allocation *>(va);
Jason Sams807fdc42012-07-25 17:55:39 -0700697 const Type * t = a->getType();
Miao Wangcc8cea72015-02-19 18:14:46 -0800698 if(t->getDimZ()) {
699 a->read(rsc, 0, 0, 0, 0, t->getDimX(), t->getDimY(), t->getDimZ(),
700 data, sizeBytes, 0);
701 } else if(t->getDimY()) {
Jason Sams807fdc42012-07-25 17:55:39 -0700702 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700703 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Jason Sams807fdc42012-07-25 17:55:39 -0700704 } else {
705 a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes);
706 }
707
Jason Samse579df42009-08-10 14:55:26 -0700708}
709
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800710void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700711 Allocation *a = static_cast<Allocation *>(va);
712 a->resize1D(rsc, dimX);
713}
714
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800715void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700716 Allocation *a = static_cast<Allocation *>(va);
717 a->resize2D(rsc, dimX, dimY);
718}
719
Jason Samsc975cf42011-04-28 18:26:48 -0700720RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800721 RsAllocationMipmapControl mipmaps,
Tim Murray099bc262013-03-20 16:54:03 -0700722 uint32_t usages, uintptr_t ptr) {
Stephen Hines8f615d62013-12-20 12:23:32 -0800723 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mipmaps, (void*)ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700724 if (!alloc) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700725 return nullptr;
Jason Samseb4fe182011-05-26 16:33:01 -0700726 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700727 alloc->incUserRef();
728 return alloc;
729}
730
Jason Samsc975cf42011-04-28 18:26:48 -0700731RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800732 RsAllocationMipmapControl mipmaps,
Jason Sams807fdc42012-07-25 17:55:39 -0700733 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800734 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700735
Stephen Hines8f615d62013-12-20 12:23:32 -0800736 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700737 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700738 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000739 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700740 return nullptr;
Jason Samsf0c1df42010-10-26 13:09:17 -0700741 }
742
Jason Sams807fdc42012-07-25 17:55:39 -0700743 texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray60c27962013-02-07 16:15:23 -0800744 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Stephen Hines8f615d62013-12-20 12:23:32 -0800745 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700746 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700747 }
748
Jason Samseb4fe182011-05-26 16:33:01 -0700749 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700750 return texAlloc;
751}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800752
Jason Samsc975cf42011-04-28 18:26:48 -0700753RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800754 RsAllocationMipmapControl mipmaps,
Jason Sams807fdc42012-07-25 17:55:39 -0700755 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800756 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800757
758 // Cubemap allocation's faces should be Width by Width each.
759 // Source data should have 6 * Width by Width pixels
760 // Error checking is done in the java layer
Stephen Hines8f615d62013-12-20 12:23:32 -0800761 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800762 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700763 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000764 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700765 return nullptr;
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800766 }
767
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800768 uint32_t faceSize = t->getDimX();
769 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
770 uint32_t copySize = faceSize * t->getElementSizeBytes();
771
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800772 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800773 for (uint32_t face = 0; face < 6; face ++) {
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800774 for (uint32_t dI = 0; dI < faceSize; dI ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700775 texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face,
Tim Murray60c27962013-02-07 16:15:23 -0800776 t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize, 0);
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800777 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800778
Jason Sams366c9c82010-12-08 16:14:36 -0800779 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800780 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800781 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800782
Stephen Hines8f615d62013-12-20 12:23:32 -0800783 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700784 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800785 }
786
Jason Samseb4fe182011-05-26 16:33:01 -0700787 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800788 return texAlloc;
789}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800790
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700791void rsi_AllocationCopy2DRange(Context *rsc,
792 RsAllocation dstAlloc,
793 uint32_t dstXoff, uint32_t dstYoff,
794 uint32_t dstMip, uint32_t dstFace,
795 uint32_t width, uint32_t height,
796 RsAllocation srcAlloc,
797 uint32_t srcXoff, uint32_t srcYoff,
798 uint32_t srcMip, uint32_t srcFace) {
799 Allocation *dst = static_cast<Allocation *>(dstAlloc);
800 Allocation *src= static_cast<Allocation *>(srcAlloc);
801 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
802 (RsAllocationCubemapFace)dstFace,
803 width, height,
804 src, srcXoff, srcYoff,srcMip,
805 (RsAllocationCubemapFace)srcFace);
806}
807
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700808void rsi_AllocationCopy3DRange(Context *rsc,
809 RsAllocation dstAlloc,
810 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
811 uint32_t dstMip,
812 uint32_t width, uint32_t height, uint32_t depth,
813 RsAllocation srcAlloc,
814 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
815 uint32_t srcMip) {
816 Allocation *dst = static_cast<Allocation *>(dstAlloc);
817 Allocation *src= static_cast<Allocation *>(srcAlloc);
818 rsc->mHal.funcs.allocation.allocData3D(rsc, dst, dstXoff, dstYoff, dstZoff, dstMip,
819 width, height, depth,
820 src, srcXoff, srcYoff, srcZoff, srcMip);
821}
822
823
Jason Sams733396b2013-02-22 12:46:18 -0800824void * rsi_AllocationGetSurface(Context *rsc, RsAllocation valloc) {
Jason Sams41e373d2012-01-13 14:01:20 -0800825 Allocation *alloc = static_cast<Allocation *>(valloc);
Jason Sams733396b2013-02-22 12:46:18 -0800826 void *s = alloc->getSurface(rsc);
827 return s;
Jason Sams3522f402012-03-23 11:47:26 -0700828}
829
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800830void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
831 Allocation *alloc = static_cast<Allocation *>(valloc);
832 alloc->setSurface(rsc, sur);
833}
834
835void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
836 Allocation *alloc = static_cast<Allocation *>(valloc);
837 alloc->ioSend(rsc);
838}
839
840void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
841 Allocation *alloc = static_cast<Allocation *>(valloc);
842 alloc->ioReceive(rsc);
843}
844
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700845void *rsi_AllocationGetPointer(Context *rsc, RsAllocation valloc,
Jason Samsb8a94e22014-02-24 17:52:32 -0800846 uint32_t lod, RsAllocationCubemapFace face,
847 uint32_t z, uint32_t array, size_t *stride, size_t strideLen) {
848 Allocation *alloc = static_cast<Allocation *>(valloc);
Tim Murray0e61af92014-02-26 13:28:52 -0800849 rsAssert(strideLen == sizeof(size_t));
Jason Samsb8a94e22014-02-24 17:52:32 -0800850
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700851 return alloc->getPointer(rsc, lod, face, z, array, stride);
Jason Samsb8a94e22014-02-24 17:52:32 -0800852}
853
Tim Murray509ea5c2012-11-13 11:56:40 -0800854void rsi_Allocation1DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
855 uint32_t count, void *data, size_t sizeBytes) {
856 Allocation *a = static_cast<Allocation *>(va);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700857 rsc->mHal.funcs.allocation.read1D(rsc, a, xoff, lod, count, data, sizeBytes);
Tim Murray509ea5c2012-11-13 11:56:40 -0800858}
859
Miao Wangcc8cea72015-02-19 18:14:46 -0800860void rsi_AllocationElementRead(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
861 uint32_t lod, void *data, size_t sizeBytes, size_t eoff) {
862 Allocation *a = static_cast<Allocation *>(va);
863 a->elementRead(rsc, x, y, z, data, eoff, sizeBytes);
864}
865
Tim Murray7b3e3092012-11-16 13:32:24 -0800866void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff,
867 uint32_t lod, RsAllocationCubemapFace face, uint32_t w,
Tim Murray358747a2012-11-26 13:52:04 -0800868 uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Tim Murray7b3e3092012-11-16 13:32:24 -0800869 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800870 a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Tim Murray7b3e3092012-11-16 13:32:24 -0800871}
872
Miao Wangcc8cea72015-02-19 18:14:46 -0800873void rsi_Allocation3DRead(Context *rsc, RsAllocation va,
874 uint32_t xoff, uint32_t yoff, uint32_t zoff,
875 uint32_t lod, uint32_t w, uint32_t h, uint32_t d,
876 void *data, size_t sizeBytes, size_t stride) {
877 Allocation *a = static_cast<Allocation *>(va);
878 a->read(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
879}
880
Jason Samsbc9dc272015-02-09 12:50:22 -0800881RsAllocation rsi_AllocationAdapterCreate(Context *rsc, RsType vwindow, RsAllocation vbase) {
882
883
884 Allocation * alloc = Allocation::createAdapter(rsc,
885 static_cast<Allocation *>(vbase), static_cast<Type *>(vwindow));
886 if (!alloc) {
887 return nullptr;
888 }
889 alloc->incUserRef();
890 return alloc;
891}
892
893void rsi_AllocationAdapterOffset(Context *rsc, RsAllocation va, const uint32_t *offsets, size_t len) {
894}
895
896
Jason Samsc975cf42011-04-28 18:26:48 -0700897}
898}
899
Tim Murrayc2ce7072013-07-17 18:38:53 -0700900extern "C" const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Jason Samsc975cf42011-04-28 18:26:48 -0700901 Allocation *a = static_cast<Allocation *>(va);
902 a->getType()->incUserRef();
903
904 return a->getType();
905}