blob: 9b83ebba186ccc21d06678a3d7b9f9a3709ba617 [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
Tim Murray34689382013-03-11 12:12:03 -070044void Allocation::operator delete(void* ptr) {
45 if (ptr) {
46 Allocation *a = (Allocation*) ptr;
47 a->getContext()->mHal.funcs.freeRuntimeMem(ptr);
48 }
49}
50
Jason Samseb4fe182011-05-26 16:33:01 -070051Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080052 RsAllocationMipmapControl mc, void * ptr) {
Tim Murray34689382013-03-11 12:12:03 -070053 // Allocation objects must use allocator specified by the driver
54 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Allocation), 0);
55
56 if (!allocMem) {
57 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation");
Chris Wailes44bef6f2014-08-12 13:51:10 -070058 return nullptr;
Tim Murray34689382013-03-11 12:12:03 -070059 }
60
61 Allocation *a = new (allocMem) Allocation(rsc, type, usages, mc, ptr);
Jason Samseb4fe182011-05-26 16:33:01 -070062
63 if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
64 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
65 delete a;
Chris Wailes44bef6f2014-08-12 13:51:10 -070066 return nullptr;
Jason Samseb4fe182011-05-26 16:33:01 -070067 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -080068
Jason Samseb4fe182011-05-26 16:33:01 -070069 return a;
70}
71
Jason Samsbad80742011-03-16 16:29:28 -070072void Allocation::updateCache() {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070073 const Type *type = mHal.state.type;
Jason Samsa572aca2013-01-09 11:52:26 -080074 mHal.state.yuv = type->getDimYuv();
Jason Samsbad80742011-03-16 16:29:28 -070075 mHal.state.hasFaces = type->getDimFaces();
76 mHal.state.hasMipmaps = type->getDimLOD();
77 mHal.state.elementSizeBytes = type->getElementSizeBytes();
78 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -070079}
80
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080081Allocation::~Allocation() {
Jason Samsddceab92013-08-07 13:02:32 -070082#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
83 if (mGrallocConsumer.get()) {
84 mGrallocConsumer->unlockBuffer();
Chris Wailes44bef6f2014-08-12 13:51:10 -070085 mGrallocConsumer = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -070086 }
87#endif
88
Jason Samsc7cec1e2011-08-18 18:01:33 -070089 freeChildrenUnlocked();
Jason Samseb4fe182011-05-26 16:33:01 -070090 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070091}
92
Jason Sams366c9c82010-12-08 16:14:36 -080093void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -070094 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -080095}
96
Jason Samsb8a94e22014-02-24 17:52:32 -080097void * Allocation::getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face,
98 uint32_t z, uint32_t array, size_t *stride) {
99
100 if ((lod >= mHal.drvState.lodCount) ||
101 (z && (z >= mHal.drvState.lod[lod].dimZ)) ||
102 ((face != RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X) && !mHal.state.hasFaces) ||
103 (array != 0)) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700104 return nullptr;
Jason Samsb8a94e22014-02-24 17:52:32 -0800105 }
106
107 size_t s = 0;
108 //void *ptr = mRSC->mHal.funcs.allocation.lock1D(rsc, this);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700109 if ((stride != nullptr) && mHal.drvState.lod[0].dimY) {
Jason Samsb8a94e22014-02-24 17:52:32 -0800110 *stride = mHal.drvState.lod[lod].stride;
111 }
112 return mHal.drvState.lod[lod].mallocPtr;
113}
114
Jason Sams4b45b892010-12-29 14:31:29 -0800115void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800116 uint32_t count, const void *data, size_t sizeBytes) {
117 const size_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -0700118
Jason Samseb4fe182011-05-26 16:33:01 -0700119 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800120 char buf[1024];
121 sprintf(buf, "Allocation::subData called with mismatched size expected %zu, got %zu",
122 (count * eSize), sizeBytes);
123 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Samsbad80742011-03-16 16:29:28 -0700124 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700125 return;
126 }
Jason Samse3929c92010-08-09 18:13:33 -0700127
Jason Samseb4fe182011-05-26 16:33:01 -0700128 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
129 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700130}
131
Jason Sams4b45b892010-12-29 14:31:29 -0800132void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800133 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Tim Murray358747a2012-11-26 13:52:04 -0800134 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Samseb4fe182011-05-26 16:33:01 -0700135 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700136}
137
Jason Sams236385b2011-01-12 14:53:25 -0800138void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700139 uint32_t lod,
140 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
141 rsc->mHal.funcs.allocation.data3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
142 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700143}
144
Jason Sams807fdc42012-07-25 17:55:39 -0700145void Allocation::read(Context *rsc, uint32_t xoff, uint32_t lod,
Tim Murray358747a2012-11-26 13:52:04 -0800146 uint32_t count, void *data, size_t sizeBytes) {
Jason Sams807fdc42012-07-25 17:55:39 -0700147 const size_t eSize = mHal.state.type->getElementSizeBytes();
148
149 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800150 char buf[1024];
151 sprintf(buf, "Allocation::read called with mismatched size expected %zu, got %zu",
152 (count * eSize), sizeBytes);
153 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams807fdc42012-07-25 17:55:39 -0700154 mHal.state.type->dumpLOGV("type info");
155 return;
156 }
157
158 rsc->mHal.funcs.allocation.read1D(rsc, this, xoff, lod, count, data, sizeBytes);
159}
160
Tim Murray358747a2012-11-26 13:52:04 -0800161void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
162 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
163 const size_t eSize = mHal.state.elementSizeBytes;
164 const size_t lineSize = eSize * w;
165 if (!stride) {
166 stride = lineSize;
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700167 } else {
168 if ((lineSize * h) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800169 char buf[1024];
170 sprintf(buf, "Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
171 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700172 return;
173 }
Tim Murray358747a2012-11-26 13:52:04 -0800174 }
175
176 rsc->mHal.funcs.allocation.read2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams807fdc42012-07-25 17:55:39 -0700177}
178
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700179void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
180 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride) {
181 const size_t eSize = mHal.state.elementSizeBytes;
182 const size_t lineSize = eSize * w;
183 if (!stride) {
184 stride = lineSize;
185 }
186
187 rsc->mHal.funcs.allocation.read3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
188
Jason Sams807fdc42012-07-25 17:55:39 -0700189}
190
Jason Sams4b45b892010-12-29 14:31:29 -0800191void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800192 uint32_t cIdx, size_t sizeBytes) {
193 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700194
Jason Samsbad80742011-03-16 16:29:28 -0700195 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700196 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
197 return;
198 }
199
Jason Samsa572aca2013-01-09 11:52:26 -0800200 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700201 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
202 return;
203 }
204
Jason Samsbad80742011-03-16 16:29:28 -0700205 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800206 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
207 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700208 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
209 return;
210 }
211
Jason Samseb4fe182011-05-26 16:33:01 -0700212 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
213 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700214}
215
Jason Sams4b45b892010-12-29 14:31:29 -0800216void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800217 const void *data, uint32_t cIdx, size_t sizeBytes) {
218 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700219
Jason Samsa572aca2013-01-09 11:52:26 -0800220 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700221 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
222 return;
223 }
224
Jason Samsa572aca2013-01-09 11:52:26 -0800225 if (y >= mHal.drvState.lod[0].dimY) {
Chris Wailes93d6bc82014-07-28 16:54:38 -0700226 rsc->setError(RS_ERROR_BAD_VALUE,
227 "subElementData X offset out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700228 return;
229 }
230
Jason Samsbad80742011-03-16 16:29:28 -0700231 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Chris Wailes93d6bc82014-07-28 16:54:38 -0700232 rsc->setError(RS_ERROR_BAD_VALUE,
233 "subElementData component out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700234 return;
235 }
236
Jason Samsbad80742011-03-16 16:29:28 -0700237 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Chris Wailes93d6bc82014-07-28 16:54:38 -0700238 uint32_t elemArraySize =
239 mHal.state.type->getElement()->getFieldArraySize(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800240 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700241 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
242 return;
243 }
244
Chris Wailes93d6bc82014-07-28 16:54:38 -0700245 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx,
246 sizeBytes);
Jason Samseb4fe182011-05-26 16:33:01 -0700247 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700248}
249
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800250void Allocation::addProgramToDirty(const Program *p) {
Chris Wailes93d6bc82014-07-28 16:54:38 -0700251 mToDirtyList.push_back(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700252}
Jason Sams326e0dd2009-05-22 14:03:28 -0700253
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800254void Allocation::removeProgramToDirty(const Program *p) {
Chris Wailes93d6bc82014-07-28 16:54:38 -0700255 for (auto entryIter = mToDirtyList.begin(), endIter = mToDirtyList.end();
256 entryIter != endIter; entryIter++) {
257
258 if (p == *entryIter) {
259 mToDirtyList.erase(entryIter);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700260 return;
261 }
262 }
263 rsAssert(0);
264}
265
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800266void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800267 ObjectBase::dumpLOGV(prefix);
Jason Sams48ecf6a2013-07-09 15:35:29 -0700268 char buf[1024];
Jason Samsc21cf402009-11-17 17:26:46 -0800269
Jason Sams48ecf6a2013-07-09 15:35:29 -0700270 if ((strlen(prefix) + 10) < sizeof(buf)) {
271 sprintf(buf, "%s type ", prefix);
272 if (mHal.state.type) {
273 mHal.state.type->dumpLOGV(buf);
274 }
Jason Samsc21cf402009-11-17 17:26:46 -0800275 }
Steve Block65982012011-10-20 11:56:00 +0100276 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Chris Wailes93d6bc82014-07-28 16:54:38 -0700277 prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags,
278 mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800279}
Jason Sams326e0dd2009-05-22 14:03:28 -0700280
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800281uint32_t Allocation::getPackedSize() const {
Jason Sams61656a72013-09-03 16:21:18 -0700282 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800283 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
284}
285
Jason Samse3150cf2012-07-24 18:10:20 -0700286void Allocation::writePackedData(Context *rsc, const Type *type,
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800287 uint8_t *dst, const uint8_t *src, bool dstPadded) {
288 const Element *elem = type->getElement();
289 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
290 uint32_t paddedBytes = elem->getSizeBytes();
Jason Sams61656a72013-09-03 16:21:18 -0700291 uint32_t numItems = type->getPackedSizeBytes() / paddedBytes;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800292
293 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
294 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
295
296 // no sub-elements
297 uint32_t fieldCount = elem->getFieldCount();
298 if (fieldCount == 0) {
299 for (uint32_t i = 0; i < numItems; i ++) {
300 memcpy(dst, src, unpaddedBytes);
301 src += srcInc;
302 dst += dstInc;
303 }
304 return;
305 }
306
307 // Cache offsets
308 uint32_t *offsetsPadded = new uint32_t[fieldCount];
309 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
310 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
311
312 for (uint32_t i = 0; i < fieldCount; i++) {
313 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
314 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
315 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
316 }
317
318 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
319 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
320
321 // complex elements, need to copy subelem after subelem
322 for (uint32_t i = 0; i < numItems; i ++) {
323 for (uint32_t fI = 0; fI < fieldCount; fI++) {
324 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
325 }
326 src += srcInc;
327 dst += dstInc;
328 }
329
330 delete[] offsetsPadded;
331 delete[] offsetsUnpadded;
332 delete[] sizeUnpadded;
333}
334
Jason Samse3150cf2012-07-24 18:10:20 -0700335void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800336 const uint8_t *src = (const uint8_t*)data;
Jason Sams61a4bb72012-07-25 19:33:43 -0700337 uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800338
Jason Samse3150cf2012-07-24 18:10:20 -0700339 writePackedData(rsc, getType(), dst, src, true);
Jason Sams61a4bb72012-07-25 19:33:43 -0700340 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800341}
342
Jason Samse3150cf2012-07-24 18:10:20 -0700343void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800344 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
345 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
Jason Sams61656a72013-09-03 16:21:18 -0700346 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800347
Jason Sams61a4bb72012-07-25 19:33:43 -0700348 const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800349 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
350
Jason Samse3150cf2012-07-24 18:10:20 -0700351 writePackedData(rsc, getType(), dst, src, false);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800352 stream->addByteArray(dst, getPackedSize());
353
354 delete[] dst;
Jason Sams61a4bb72012-07-25 19:33:43 -0700355 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800356}
357
Jason Samse3150cf2012-07-24 18:10:20 -0700358void Allocation::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700359 // Need to identify ourselves
360 stream->addU32((uint32_t)getClassId());
Jason Sams48ecf6a2013-07-09 15:35:29 -0700361 stream->addString(getName());
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700362
363 // First thing we need to serialize is the type object since it will be needed
364 // to initialize the class
Jason Samse3150cf2012-07-24 18:10:20 -0700365 mHal.state.type->serialize(rsc, stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700366
Jason Sams61656a72013-09-03 16:21:18 -0700367 uint32_t dataSize = mHal.state.type->getPackedSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800368 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
369 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700370 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800371 stream->addU32(packedSize);
372 if (dataSize == packedSize) {
373 // Now write the data
Jason Sams61a4bb72012-07-25 19:33:43 -0700374 stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize);
375 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800376 } else {
377 // Now write the data
Jason Samse3150cf2012-07-24 18:10:20 -0700378 packVec3Allocation(rsc, stream);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800379 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700380}
381
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800382Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700383 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700384 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800385 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Jason Samsa2737932014-01-14 12:28:33 -0800386 rsc->setError(RS_ERROR_FATAL_DRIVER,
387 "allocation loading failed due to corrupt file. (invalid id)\n");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700388 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700389 }
390
Jason Sams48ecf6a2013-07-09 15:35:29 -0700391 const char *name = stream->loadString();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700392
393 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800394 if (!type) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700395 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700396 }
397 type->compute();
398
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800399 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
400 type->decUserRef();
401
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700402 // Number of bytes we wrote out for this allocation
403 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800404 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
405 uint32_t packedSize = alloc->getPackedSize();
Jason Sams61656a72013-09-03 16:21:18 -0700406 if (dataSize != type->getPackedSizeBytes() &&
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800407 dataSize != packedSize) {
Jason Samsa2737932014-01-14 12:28:33 -0800408 rsc->setError(RS_ERROR_FATAL_DRIVER,
409 "allocation loading failed due to corrupt file. (invalid size)\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800410 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700411 ObjectBase::checkDelete(type);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700412 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700413 }
414
Jason Sams48ecf6a2013-07-09 15:35:29 -0700415 alloc->assignName(name);
Jason Sams61656a72013-09-03 16:21:18 -0700416 if (dataSize == type->getPackedSizeBytes()) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800417 uint32_t count = dataSize / type->getElementSizeBytes();
418 // Read in all of our allocation data
419 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
420 } else {
Jason Samse3150cf2012-07-24 18:10:20 -0700421 alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800422 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700423 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700424
425 return alloc;
426}
427
Jason Samseb4fe182011-05-26 16:33:01 -0700428void Allocation::sendDirty(const Context *rsc) const {
Jason Sams93eacc72012-12-18 14:26:57 -0800429#ifndef RS_COMPATIBILITY_LIB
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700430 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
431 mToDirtyList[ct]->forceDirty();
432 }
Jason Sams93eacc72012-12-18 14:26:57 -0800433#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700434 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700435}
Jason Sams326e0dd2009-05-22 14:03:28 -0700436
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800437void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700438 mHal.state.type->incRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700439}
440
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800441void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700442 if (!mHal.state.hasReferences || !getIsScript()) {
443 return;
444 }
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700445 mHal.state.type->decRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700446}
447
Jason Samsa36c50a2014-06-17 12:06:06 -0700448void Allocation::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700449 if (rsc->mHal.funcs.allocation.updateCachedObject != nullptr) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700450 rsc->mHal.funcs.allocation.updateCachedObject(rsc, this, (rs_allocation *)dstObj);
451 } else {
452 *((const void **)dstObj) = this;
453 }
454}
455
456
Jason Samsc7cec1e2011-08-18 18:01:33 -0700457void Allocation::freeChildrenUnlocked () {
Jason Sams61a4bb72012-07-25 19:33:43 -0700458 void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this);
Jason Sams61656a72013-09-03 16:21:18 -0700459 decRefs(ptr, mHal.state.type->getCellCount(), 0);
Jason Sams61a4bb72012-07-25 19:33:43 -0700460 mRSC->mHal.funcs.allocation.unlock1D(mRSC, this);
Jason Samsc7cec1e2011-08-18 18:01:33 -0700461}
462
463bool Allocation::freeChildren() {
464 if (mHal.state.hasReferences) {
465 incSysRef();
466 freeChildrenUnlocked();
467 return decSysRef();
468 }
469 return false;
470}
471
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800472void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700473}
474
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800475void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsa572aca2013-01-09 11:52:26 -0800476 uint32_t oldDimX = mHal.drvState.lod[0].dimX;
Jason Sams96abf812010-10-05 13:32:49 -0700477 if (dimX == oldDimX) {
478 return;
479 }
480
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700481 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700482 if (dimX < oldDimX) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700483 decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX);
484 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Jason Sams96abf812010-10-05 13:32:49 -0700485 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700486 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700487 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700488 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700489}
490
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800491void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Samsa2737932014-01-14 12:28:33 -0800492 rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700493}
494
Jason Samsddceab92013-08-07 13:02:32 -0700495#ifndef RS_COMPATIBILITY_LIB
496void Allocation::NewBufferListener::onFrameAvailable() {
497 intptr_t ip = (intptr_t)alloc;
Tim Murraye4ed0872014-08-18 16:13:08 -0700498 rsc->sendMessageToClient(&ip, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, 0, sizeof(ip), true);
Jason Samsddceab92013-08-07 13:02:32 -0700499}
500#endif
501
Jason Sams733396b2013-02-22 12:46:18 -0800502void * Allocation::getSurface(const Context *rsc) {
Jason Samsddceab92013-08-07 13:02:32 -0700503#ifndef RS_COMPATIBILITY_LIB
504 // Configure GrallocConsumer to be in asynchronous mode
Dan Stoza03c155b2014-03-12 12:06:18 -0700505 sp<IGraphicBufferProducer> bp;
506 sp<IGraphicBufferConsumer> bc;
507 BufferQueue::createBufferQueue(&bp, &bc);
Jason Sams1c19f052014-11-10 12:11:37 -0800508 mGrallocConsumer = new GrallocConsumer(this, bc, mHal.drvState.grallocFlags);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700509 bp->incStrong(nullptr);
Jason Samsddceab92013-08-07 13:02:32 -0700510
511 mBufferListener = new NewBufferListener();
512 mBufferListener->rsc = rsc;
513 mBufferListener->alloc = this;
514
515 mGrallocConsumer->setFrameAvailableListener(mBufferListener);
516 return bp.get();
517#else
Chris Wailes44bef6f2014-08-12 13:51:10 -0700518 return nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700519#endif
520 //return rsc->mHal.funcs.allocation.getSurface(rsc, this);
Jason Sams41e373d2012-01-13 14:01:20 -0800521}
522
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800523void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
524 ANativeWindow *nw = (ANativeWindow *)sur;
Jason Sams733396b2013-02-22 12:46:18 -0800525 rsc->mHal.funcs.allocation.setSurface(rsc, this, nw);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800526}
527
528void Allocation::ioSend(const Context *rsc) {
529 rsc->mHal.funcs.allocation.ioSend(rsc, this);
530}
531
532void Allocation::ioReceive(const Context *rsc) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700533 void *ptr = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700534 size_t stride = 0;
535#ifndef RS_COMPATIBILITY_LIB
536 if (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
537 status_t ret = mGrallocConsumer->lockNextBuffer();
538
539 if (ret == OK) {
540 rsc->mHal.funcs.allocation.ioReceive(rsc, this);
541 } else if (ret == BAD_VALUE) {
542 // No new frame, don't do anything
543 } else {
544 rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer.");
545 }
546
547 }
548#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800549}
550
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700551bool Allocation::hasSameDims(const Allocation *other) const {
552 const Type *type0 = this->getType(),
553 *type1 = other->getType();
554
555 return (type0->getCellCount() == type1->getCellCount()) &&
556 (type0->getDimLOD() == type1->getDimLOD()) &&
557 (type0->getDimFaces() == type1->getDimFaces()) &&
558 (type0->getDimYuv() == type1->getDimYuv()) &&
559 (type0->getDimX() == type1->getDimX()) &&
560 (type0->getDimY() == type1->getDimY()) &&
561 (type0->getDimZ() == type1->getDimZ());
562}
563
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800564
Jason Sams326e0dd2009-05-22 14:03:28 -0700565/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700566//
Stephen Hines6a121812011-03-01 17:34:59 -0800567
Jason Sams326e0dd2009-05-22 14:03:28 -0700568namespace android {
569namespace renderscript {
570
Jason Sams366c9c82010-12-08 16:14:36 -0800571void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
572 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700573 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800574 a->syncAll(rsc, src);
575}
576
Jason Samsa2371512011-01-12 13:28:37 -0800577void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700578 Allocation *alloc = static_cast<Allocation *>(va);
579 rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc);
Jason Sams837e3882010-12-10 16:03:15 -0800580}
581
Jason Sams807fdc42012-07-25 17:55:39 -0700582void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
583 Allocation *a = static_cast<Allocation *>(va);
584 const Type * t = a->getType();
585 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700586 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700587}
588
Jason Sams4b45b892010-12-29 14:31:29 -0800589void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700590 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700591 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800592 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700593}
594
Jason Sams4b45b892010-12-29 14:31:29 -0800595void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800596 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700597 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800598 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700599}
600
Jason Sams4b45b892010-12-29 14:31:29 -0800601void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800602 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700603 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800604 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700605}
606
Jason Sams4b45b892010-12-29 14:31:29 -0800607void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800608 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700609 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800610 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams326e0dd2009-05-22 14:03:28 -0700611}
612
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700613void rsi_Allocation3DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
614 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
615 Allocation *a = static_cast<Allocation *>(va);
616 a->data(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
617}
618
619
Jason Sams807fdc42012-07-25 17:55:39 -0700620void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
Jason Samse579df42009-08-10 14:55:26 -0700621 Allocation *a = static_cast<Allocation *>(va);
Jason Sams807fdc42012-07-25 17:55:39 -0700622 const Type * t = a->getType();
623 if(t->getDimY()) {
624 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700625 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Jason Sams807fdc42012-07-25 17:55:39 -0700626 } else {
627 a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes);
628 }
629
Jason Samse579df42009-08-10 14:55:26 -0700630}
631
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800632void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700633 Allocation *a = static_cast<Allocation *>(va);
634 a->resize1D(rsc, dimX);
635}
636
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800637void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700638 Allocation *a = static_cast<Allocation *>(va);
639 a->resize2D(rsc, dimX, dimY);
640}
641
Jason Samsc975cf42011-04-28 18:26:48 -0700642RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800643 RsAllocationMipmapControl mipmaps,
Tim Murray099bc262013-03-20 16:54:03 -0700644 uint32_t usages, uintptr_t ptr) {
Stephen Hines8f615d62013-12-20 12:23:32 -0800645 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mipmaps, (void*)ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700646 if (!alloc) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700647 return nullptr;
Jason Samseb4fe182011-05-26 16:33:01 -0700648 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700649 alloc->incUserRef();
650 return alloc;
651}
652
Jason Samsc975cf42011-04-28 18:26:48 -0700653RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800654 RsAllocationMipmapControl mipmaps,
Jason Sams807fdc42012-07-25 17:55:39 -0700655 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800656 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700657
Stephen Hines8f615d62013-12-20 12:23:32 -0800658 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700659 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700660 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000661 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700662 return nullptr;
Jason Samsf0c1df42010-10-26 13:09:17 -0700663 }
664
Jason Sams807fdc42012-07-25 17:55:39 -0700665 texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray60c27962013-02-07 16:15:23 -0800666 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Stephen Hines8f615d62013-12-20 12:23:32 -0800667 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700668 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700669 }
670
Jason Samseb4fe182011-05-26 16:33:01 -0700671 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700672 return texAlloc;
673}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800674
Jason Samsc975cf42011-04-28 18:26:48 -0700675RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800676 RsAllocationMipmapControl mipmaps,
Jason Sams807fdc42012-07-25 17:55:39 -0700677 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800678 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800679
680 // Cubemap allocation's faces should be Width by Width each.
681 // Source data should have 6 * Width by Width pixels
682 // Error checking is done in the java layer
Stephen Hines8f615d62013-12-20 12:23:32 -0800683 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800684 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700685 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000686 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700687 return nullptr;
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800688 }
689
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800690 uint32_t faceSize = t->getDimX();
691 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
692 uint32_t copySize = faceSize * t->getElementSizeBytes();
693
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800694 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800695 for (uint32_t face = 0; face < 6; face ++) {
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800696 for (uint32_t dI = 0; dI < faceSize; dI ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700697 texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face,
Tim Murray60c27962013-02-07 16:15:23 -0800698 t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize, 0);
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800699 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800700
Jason Sams366c9c82010-12-08 16:14:36 -0800701 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800702 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800703 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800704
Stephen Hines8f615d62013-12-20 12:23:32 -0800705 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700706 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800707 }
708
Jason Samseb4fe182011-05-26 16:33:01 -0700709 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800710 return texAlloc;
711}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800712
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700713void rsi_AllocationCopy2DRange(Context *rsc,
714 RsAllocation dstAlloc,
715 uint32_t dstXoff, uint32_t dstYoff,
716 uint32_t dstMip, uint32_t dstFace,
717 uint32_t width, uint32_t height,
718 RsAllocation srcAlloc,
719 uint32_t srcXoff, uint32_t srcYoff,
720 uint32_t srcMip, uint32_t srcFace) {
721 Allocation *dst = static_cast<Allocation *>(dstAlloc);
722 Allocation *src= static_cast<Allocation *>(srcAlloc);
723 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
724 (RsAllocationCubemapFace)dstFace,
725 width, height,
726 src, srcXoff, srcYoff,srcMip,
727 (RsAllocationCubemapFace)srcFace);
728}
729
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700730void rsi_AllocationCopy3DRange(Context *rsc,
731 RsAllocation dstAlloc,
732 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
733 uint32_t dstMip,
734 uint32_t width, uint32_t height, uint32_t depth,
735 RsAllocation srcAlloc,
736 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
737 uint32_t srcMip) {
738 Allocation *dst = static_cast<Allocation *>(dstAlloc);
739 Allocation *src= static_cast<Allocation *>(srcAlloc);
740 rsc->mHal.funcs.allocation.allocData3D(rsc, dst, dstXoff, dstYoff, dstZoff, dstMip,
741 width, height, depth,
742 src, srcXoff, srcYoff, srcZoff, srcMip);
743}
744
745
Jason Sams733396b2013-02-22 12:46:18 -0800746void * rsi_AllocationGetSurface(Context *rsc, RsAllocation valloc) {
Jason Sams41e373d2012-01-13 14:01:20 -0800747 Allocation *alloc = static_cast<Allocation *>(valloc);
Jason Sams733396b2013-02-22 12:46:18 -0800748 void *s = alloc->getSurface(rsc);
749 return s;
Jason Sams3522f402012-03-23 11:47:26 -0700750}
751
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800752void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
753 Allocation *alloc = static_cast<Allocation *>(valloc);
754 alloc->setSurface(rsc, sur);
755}
756
757void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
758 Allocation *alloc = static_cast<Allocation *>(valloc);
759 alloc->ioSend(rsc);
760}
761
762void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
763 Allocation *alloc = static_cast<Allocation *>(valloc);
764 alloc->ioReceive(rsc);
765}
766
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700767void *rsi_AllocationGetPointer(Context *rsc, RsAllocation valloc,
Jason Samsb8a94e22014-02-24 17:52:32 -0800768 uint32_t lod, RsAllocationCubemapFace face,
769 uint32_t z, uint32_t array, size_t *stride, size_t strideLen) {
770 Allocation *alloc = static_cast<Allocation *>(valloc);
Tim Murray0e61af92014-02-26 13:28:52 -0800771 rsAssert(strideLen == sizeof(size_t));
Jason Samsb8a94e22014-02-24 17:52:32 -0800772
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700773 return alloc->getPointer(rsc, lod, face, z, array, stride);
Jason Samsb8a94e22014-02-24 17:52:32 -0800774}
775
Tim Murray509ea5c2012-11-13 11:56:40 -0800776void rsi_Allocation1DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
777 uint32_t count, void *data, size_t sizeBytes) {
778 Allocation *a = static_cast<Allocation *>(va);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700779 rsc->mHal.funcs.allocation.read1D(rsc, a, xoff, lod, count, data, sizeBytes);
Tim Murray509ea5c2012-11-13 11:56:40 -0800780}
781
Tim Murray7b3e3092012-11-16 13:32:24 -0800782void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff,
783 uint32_t lod, RsAllocationCubemapFace face, uint32_t w,
Tim Murray358747a2012-11-26 13:52:04 -0800784 uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Tim Murray7b3e3092012-11-16 13:32:24 -0800785 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800786 a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Tim Murray7b3e3092012-11-16 13:32:24 -0800787}
788
Jason Samsc975cf42011-04-28 18:26:48 -0700789}
790}
791
Tim Murrayc2ce7072013-07-17 18:38:53 -0700792extern "C" const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Jason Samsc975cf42011-04-28 18:26:48 -0700793 Allocation *a = static_cast<Allocation *>(va);
794 a->getType()->incUserRef();
795
796 return a->getType();
797}