blob: 59967e9bc1c8dac7f2469c7246619088f000d495 [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");
58 return NULL;
59 }
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;
66 return NULL;
67 }
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();
85 mGrallocConsumer = NULL;
86 }
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)) {
104 return NULL;
105 }
106
107 size_t s = 0;
108 //void *ptr = mRSC->mHal.funcs.allocation.lock1D(rsc, this);
109 if ((stride != NULL) && mHal.drvState.lod[0].dimY) {
110 *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) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700226 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
227 return;
228 }
229
Jason Samsbad80742011-03-16 16:29:28 -0700230 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700231 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
232 return;
233 }
234
Jason Samsbad80742011-03-16 16:29:28 -0700235 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800236 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
237 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700238 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
239 return;
240 }
241
Jason Samseb4fe182011-05-26 16:33:01 -0700242 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
243 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700244}
245
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800246void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700247 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700248}
Jason Sams326e0dd2009-05-22 14:03:28 -0700249
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800250void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700251 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
252 if (mToDirtyList[ct] == p) {
253 mToDirtyList.removeAt(ct);
254 return;
255 }
256 }
257 rsAssert(0);
258}
259
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800260void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800261 ObjectBase::dumpLOGV(prefix);
Jason Sams48ecf6a2013-07-09 15:35:29 -0700262 char buf[1024];
Jason Samsc21cf402009-11-17 17:26:46 -0800263
Jason Sams48ecf6a2013-07-09 15:35:29 -0700264 if ((strlen(prefix) + 10) < sizeof(buf)) {
265 sprintf(buf, "%s type ", prefix);
266 if (mHal.state.type) {
267 mHal.state.type->dumpLOGV(buf);
268 }
Jason Samsc21cf402009-11-17 17:26:46 -0800269 }
Steve Block65982012011-10-20 11:56:00 +0100270 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Jason Sams709a0972012-11-15 18:18:04 -0800271 prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800272}
Jason Sams326e0dd2009-05-22 14:03:28 -0700273
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800274uint32_t Allocation::getPackedSize() const {
Jason Sams61656a72013-09-03 16:21:18 -0700275 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800276 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
277}
278
Jason Samse3150cf2012-07-24 18:10:20 -0700279void Allocation::writePackedData(Context *rsc, const Type *type,
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800280 uint8_t *dst, const uint8_t *src, bool dstPadded) {
281 const Element *elem = type->getElement();
282 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
283 uint32_t paddedBytes = elem->getSizeBytes();
Jason Sams61656a72013-09-03 16:21:18 -0700284 uint32_t numItems = type->getPackedSizeBytes() / paddedBytes;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800285
286 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
287 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
288
289 // no sub-elements
290 uint32_t fieldCount = elem->getFieldCount();
291 if (fieldCount == 0) {
292 for (uint32_t i = 0; i < numItems; i ++) {
293 memcpy(dst, src, unpaddedBytes);
294 src += srcInc;
295 dst += dstInc;
296 }
297 return;
298 }
299
300 // Cache offsets
301 uint32_t *offsetsPadded = new uint32_t[fieldCount];
302 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
303 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
304
305 for (uint32_t i = 0; i < fieldCount; i++) {
306 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
307 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
308 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
309 }
310
311 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
312 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
313
314 // complex elements, need to copy subelem after subelem
315 for (uint32_t i = 0; i < numItems; i ++) {
316 for (uint32_t fI = 0; fI < fieldCount; fI++) {
317 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
318 }
319 src += srcInc;
320 dst += dstInc;
321 }
322
323 delete[] offsetsPadded;
324 delete[] offsetsUnpadded;
325 delete[] sizeUnpadded;
326}
327
Jason Samse3150cf2012-07-24 18:10:20 -0700328void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800329 const uint8_t *src = (const uint8_t*)data;
Jason Sams61a4bb72012-07-25 19:33:43 -0700330 uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800331
Jason Samse3150cf2012-07-24 18:10:20 -0700332 writePackedData(rsc, getType(), dst, src, true);
Jason Sams61a4bb72012-07-25 19:33:43 -0700333 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800334}
335
Jason Samse3150cf2012-07-24 18:10:20 -0700336void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800337 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
338 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
Jason Sams61656a72013-09-03 16:21:18 -0700339 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800340
Jason Sams61a4bb72012-07-25 19:33:43 -0700341 const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800342 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
343
Jason Samse3150cf2012-07-24 18:10:20 -0700344 writePackedData(rsc, getType(), dst, src, false);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800345 stream->addByteArray(dst, getPackedSize());
346
347 delete[] dst;
Jason Sams61a4bb72012-07-25 19:33:43 -0700348 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800349}
350
Jason Samse3150cf2012-07-24 18:10:20 -0700351void Allocation::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700352 // Need to identify ourselves
353 stream->addU32((uint32_t)getClassId());
Jason Sams48ecf6a2013-07-09 15:35:29 -0700354 stream->addString(getName());
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700355
356 // First thing we need to serialize is the type object since it will be needed
357 // to initialize the class
Jason Samse3150cf2012-07-24 18:10:20 -0700358 mHal.state.type->serialize(rsc, stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700359
Jason Sams61656a72013-09-03 16:21:18 -0700360 uint32_t dataSize = mHal.state.type->getPackedSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800361 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
362 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700363 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800364 stream->addU32(packedSize);
365 if (dataSize == packedSize) {
366 // Now write the data
Jason Sams61a4bb72012-07-25 19:33:43 -0700367 stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize);
368 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800369 } else {
370 // Now write the data
Jason Samse3150cf2012-07-24 18:10:20 -0700371 packVec3Allocation(rsc, stream);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800372 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700373}
374
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800375Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700376 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700377 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800378 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Jason Samsa2737932014-01-14 12:28:33 -0800379 rsc->setError(RS_ERROR_FATAL_DRIVER,
380 "allocation loading failed due to corrupt file. (invalid id)\n");
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700381 return NULL;
382 }
383
Jason Sams48ecf6a2013-07-09 15:35:29 -0700384 const char *name = stream->loadString();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700385
386 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800387 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700388 return NULL;
389 }
390 type->compute();
391
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800392 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
393 type->decUserRef();
394
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700395 // Number of bytes we wrote out for this allocation
396 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800397 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
398 uint32_t packedSize = alloc->getPackedSize();
Jason Sams61656a72013-09-03 16:21:18 -0700399 if (dataSize != type->getPackedSizeBytes() &&
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800400 dataSize != packedSize) {
Jason Samsa2737932014-01-14 12:28:33 -0800401 rsc->setError(RS_ERROR_FATAL_DRIVER,
402 "allocation loading failed due to corrupt file. (invalid size)\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800403 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700404 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700405 return NULL;
406 }
407
Jason Sams48ecf6a2013-07-09 15:35:29 -0700408 alloc->assignName(name);
Jason Sams61656a72013-09-03 16:21:18 -0700409 if (dataSize == type->getPackedSizeBytes()) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800410 uint32_t count = dataSize / type->getElementSizeBytes();
411 // Read in all of our allocation data
412 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
413 } else {
Jason Samse3150cf2012-07-24 18:10:20 -0700414 alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800415 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700416 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700417
418 return alloc;
419}
420
Jason Samseb4fe182011-05-26 16:33:01 -0700421void Allocation::sendDirty(const Context *rsc) const {
Jason Sams93eacc72012-12-18 14:26:57 -0800422#ifndef RS_COMPATIBILITY_LIB
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700423 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
424 mToDirtyList[ct]->forceDirty();
425 }
Jason Sams93eacc72012-12-18 14:26:57 -0800426#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700427 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700428}
Jason Sams326e0dd2009-05-22 14:03:28 -0700429
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800430void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700431 mHal.state.type->incRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700432}
433
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800434void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700435 if (!mHal.state.hasReferences || !getIsScript()) {
436 return;
437 }
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700438 mHal.state.type->decRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700439}
440
Jason Samsa36c50a2014-06-17 12:06:06 -0700441void Allocation::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
442 if (rsc->mHal.funcs.allocation.updateCachedObject != NULL) {
443 rsc->mHal.funcs.allocation.updateCachedObject(rsc, this, (rs_allocation *)dstObj);
444 } else {
445 *((const void **)dstObj) = this;
446 }
447}
448
449
Jason Samsc7cec1e2011-08-18 18:01:33 -0700450void Allocation::freeChildrenUnlocked () {
Jason Sams61a4bb72012-07-25 19:33:43 -0700451 void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this);
Jason Sams61656a72013-09-03 16:21:18 -0700452 decRefs(ptr, mHal.state.type->getCellCount(), 0);
Jason Sams61a4bb72012-07-25 19:33:43 -0700453 mRSC->mHal.funcs.allocation.unlock1D(mRSC, this);
Jason Samsc7cec1e2011-08-18 18:01:33 -0700454}
455
456bool Allocation::freeChildren() {
457 if (mHal.state.hasReferences) {
458 incSysRef();
459 freeChildrenUnlocked();
460 return decSysRef();
461 }
462 return false;
463}
464
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800465void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700466}
467
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800468void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsa572aca2013-01-09 11:52:26 -0800469 uint32_t oldDimX = mHal.drvState.lod[0].dimX;
Jason Sams96abf812010-10-05 13:32:49 -0700470 if (dimX == oldDimX) {
471 return;
472 }
473
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700474 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700475 if (dimX < oldDimX) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700476 decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX);
477 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Jason Sams96abf812010-10-05 13:32:49 -0700478 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700479 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700480 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700481 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700482}
483
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800484void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Samsa2737932014-01-14 12:28:33 -0800485 rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700486}
487
Jason Samsddceab92013-08-07 13:02:32 -0700488#ifndef RS_COMPATIBILITY_LIB
489void Allocation::NewBufferListener::onFrameAvailable() {
490 intptr_t ip = (intptr_t)alloc;
491 rsc->sendMessageToClient(NULL, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, ip, 0, true);
492}
493#endif
494
Jason Sams733396b2013-02-22 12:46:18 -0800495void * Allocation::getSurface(const Context *rsc) {
Jason Samsddceab92013-08-07 13:02:32 -0700496#ifndef RS_COMPATIBILITY_LIB
497 // Configure GrallocConsumer to be in asynchronous mode
498 sp<BufferQueue> bq = new BufferQueue();
499 mGrallocConsumer = new GrallocConsumer(this, bq);
500 sp<IGraphicBufferProducer> bp = bq;
501 bp->incStrong(NULL);
502
503 mBufferListener = new NewBufferListener();
504 mBufferListener->rsc = rsc;
505 mBufferListener->alloc = this;
506
507 mGrallocConsumer->setFrameAvailableListener(mBufferListener);
508 return bp.get();
509#else
510 return NULL;
511#endif
512 //return rsc->mHal.funcs.allocation.getSurface(rsc, this);
Jason Sams41e373d2012-01-13 14:01:20 -0800513}
514
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800515void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
516 ANativeWindow *nw = (ANativeWindow *)sur;
Jason Sams733396b2013-02-22 12:46:18 -0800517 rsc->mHal.funcs.allocation.setSurface(rsc, this, nw);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800518}
519
520void Allocation::ioSend(const Context *rsc) {
521 rsc->mHal.funcs.allocation.ioSend(rsc, this);
522}
523
524void Allocation::ioReceive(const Context *rsc) {
Jason Samsddceab92013-08-07 13:02:32 -0700525 void *ptr = NULL;
526 size_t stride = 0;
527#ifndef RS_COMPATIBILITY_LIB
528 if (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
529 status_t ret = mGrallocConsumer->lockNextBuffer();
530
531 if (ret == OK) {
532 rsc->mHal.funcs.allocation.ioReceive(rsc, this);
533 } else if (ret == BAD_VALUE) {
534 // No new frame, don't do anything
535 } else {
536 rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer.");
537 }
538
539 }
540#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800541}
542
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700543bool Allocation::hasSameDims(const Allocation *other) const {
544 const Type *type0 = this->getType(),
545 *type1 = other->getType();
546
547 return (type0->getCellCount() == type1->getCellCount()) &&
548 (type0->getDimLOD() == type1->getDimLOD()) &&
549 (type0->getDimFaces() == type1->getDimFaces()) &&
550 (type0->getDimYuv() == type1->getDimYuv()) &&
551 (type0->getDimX() == type1->getDimX()) &&
552 (type0->getDimY() == type1->getDimY()) &&
553 (type0->getDimZ() == type1->getDimZ());
554}
555
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800556
Jason Sams326e0dd2009-05-22 14:03:28 -0700557/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700558//
Stephen Hines6a121812011-03-01 17:34:59 -0800559
Jason Sams326e0dd2009-05-22 14:03:28 -0700560namespace android {
561namespace renderscript {
562
Jason Sams366c9c82010-12-08 16:14:36 -0800563void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
564 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700565 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800566 a->syncAll(rsc, src);
567}
568
Jason Samsa2371512011-01-12 13:28:37 -0800569void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700570 Allocation *alloc = static_cast<Allocation *>(va);
571 rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc);
Jason Sams837e3882010-12-10 16:03:15 -0800572}
573
Jason Sams807fdc42012-07-25 17:55:39 -0700574void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
575 Allocation *a = static_cast<Allocation *>(va);
576 const Type * t = a->getType();
577 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700578 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700579}
580
Jason Sams4b45b892010-12-29 14:31:29 -0800581void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700582 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700583 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800584 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700585}
586
Jason Sams4b45b892010-12-29 14:31:29 -0800587void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800588 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700589 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800590 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700591}
592
Jason Sams4b45b892010-12-29 14:31:29 -0800593void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800594 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700595 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800596 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700597}
598
Jason Sams4b45b892010-12-29 14:31:29 -0800599void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800600 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700601 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800602 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams326e0dd2009-05-22 14:03:28 -0700603}
604
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700605void rsi_Allocation3DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
606 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
607 Allocation *a = static_cast<Allocation *>(va);
608 a->data(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
609}
610
611
Jason Sams807fdc42012-07-25 17:55:39 -0700612void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
Jason Samse579df42009-08-10 14:55:26 -0700613 Allocation *a = static_cast<Allocation *>(va);
Jason Sams807fdc42012-07-25 17:55:39 -0700614 const Type * t = a->getType();
615 if(t->getDimY()) {
616 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700617 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Jason Sams807fdc42012-07-25 17:55:39 -0700618 } else {
619 a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes);
620 }
621
Jason Samse579df42009-08-10 14:55:26 -0700622}
623
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800624void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700625 Allocation *a = static_cast<Allocation *>(va);
626 a->resize1D(rsc, dimX);
627}
628
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800629void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700630 Allocation *a = static_cast<Allocation *>(va);
631 a->resize2D(rsc, dimX, dimY);
632}
633
Jason Samsc975cf42011-04-28 18:26:48 -0700634RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800635 RsAllocationMipmapControl mipmaps,
Tim Murray099bc262013-03-20 16:54:03 -0700636 uint32_t usages, uintptr_t ptr) {
Stephen Hines8f615d62013-12-20 12:23:32 -0800637 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mipmaps, (void*)ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700638 if (!alloc) {
639 return NULL;
640 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700641 alloc->incUserRef();
642 return alloc;
643}
644
Jason Samsc975cf42011-04-28 18:26:48 -0700645RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800646 RsAllocationMipmapControl mipmaps,
Jason Sams807fdc42012-07-25 17:55:39 -0700647 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800648 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700649
Stephen Hines8f615d62013-12-20 12:23:32 -0800650 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700651 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
652 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000653 ALOGE("Memory allocation failure");
Jason Samsf0c1df42010-10-26 13:09:17 -0700654 return NULL;
655 }
656
Jason Sams807fdc42012-07-25 17:55:39 -0700657 texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray60c27962013-02-07 16:15:23 -0800658 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Stephen Hines8f615d62013-12-20 12:23:32 -0800659 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700660 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700661 }
662
Jason Samseb4fe182011-05-26 16:33:01 -0700663 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700664 return texAlloc;
665}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800666
Jason Samsc975cf42011-04-28 18:26:48 -0700667RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800668 RsAllocationMipmapControl mipmaps,
Jason Sams807fdc42012-07-25 17:55:39 -0700669 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800670 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800671
672 // Cubemap allocation's faces should be Width by Width each.
673 // Source data should have 6 * Width by Width pixels
674 // Error checking is done in the java layer
Stephen Hines8f615d62013-12-20 12:23:32 -0800675 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800676 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
677 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000678 ALOGE("Memory allocation failure");
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800679 return NULL;
680 }
681
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800682 uint32_t faceSize = t->getDimX();
683 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
684 uint32_t copySize = faceSize * t->getElementSizeBytes();
685
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800686 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800687 for (uint32_t face = 0; face < 6; face ++) {
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800688 for (uint32_t dI = 0; dI < faceSize; dI ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700689 texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face,
Tim Murray60c27962013-02-07 16:15:23 -0800690 t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize, 0);
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800691 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800692
Jason Sams366c9c82010-12-08 16:14:36 -0800693 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800694 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800695 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800696
Stephen Hines8f615d62013-12-20 12:23:32 -0800697 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700698 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800699 }
700
Jason Samseb4fe182011-05-26 16:33:01 -0700701 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800702 return texAlloc;
703}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800704
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700705void rsi_AllocationCopy2DRange(Context *rsc,
706 RsAllocation dstAlloc,
707 uint32_t dstXoff, uint32_t dstYoff,
708 uint32_t dstMip, uint32_t dstFace,
709 uint32_t width, uint32_t height,
710 RsAllocation srcAlloc,
711 uint32_t srcXoff, uint32_t srcYoff,
712 uint32_t srcMip, uint32_t srcFace) {
713 Allocation *dst = static_cast<Allocation *>(dstAlloc);
714 Allocation *src= static_cast<Allocation *>(srcAlloc);
715 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
716 (RsAllocationCubemapFace)dstFace,
717 width, height,
718 src, srcXoff, srcYoff,srcMip,
719 (RsAllocationCubemapFace)srcFace);
720}
721
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700722void rsi_AllocationCopy3DRange(Context *rsc,
723 RsAllocation dstAlloc,
724 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
725 uint32_t dstMip,
726 uint32_t width, uint32_t height, uint32_t depth,
727 RsAllocation srcAlloc,
728 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
729 uint32_t srcMip) {
730 Allocation *dst = static_cast<Allocation *>(dstAlloc);
731 Allocation *src= static_cast<Allocation *>(srcAlloc);
732 rsc->mHal.funcs.allocation.allocData3D(rsc, dst, dstXoff, dstYoff, dstZoff, dstMip,
733 width, height, depth,
734 src, srcXoff, srcYoff, srcZoff, srcMip);
735}
736
737
Jason Sams733396b2013-02-22 12:46:18 -0800738void * rsi_AllocationGetSurface(Context *rsc, RsAllocation valloc) {
Jason Sams41e373d2012-01-13 14:01:20 -0800739 Allocation *alloc = static_cast<Allocation *>(valloc);
Jason Sams733396b2013-02-22 12:46:18 -0800740 void *s = alloc->getSurface(rsc);
741 return s;
Jason Sams3522f402012-03-23 11:47:26 -0700742}
743
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800744void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
745 Allocation *alloc = static_cast<Allocation *>(valloc);
746 alloc->setSurface(rsc, sur);
747}
748
749void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
750 Allocation *alloc = static_cast<Allocation *>(valloc);
751 alloc->ioSend(rsc);
752}
753
754void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
755 Allocation *alloc = static_cast<Allocation *>(valloc);
756 alloc->ioReceive(rsc);
757}
758
Jason Samsb8a94e22014-02-24 17:52:32 -0800759void rsi_AllocationGetPointer(Context *rsc, RsAllocation valloc,
760 uint32_t lod, RsAllocationCubemapFace face,
761 uint32_t z, uint32_t array, size_t *stride, size_t strideLen) {
762 Allocation *alloc = static_cast<Allocation *>(valloc);
Tim Murraya4744742014-02-26 13:28:52 -0800763 rsAssert(strideLen == sizeof(size_t));
Jason Samsb8a94e22014-02-24 17:52:32 -0800764
765 alloc->getPointer(rsc, lod, face, z, array, stride);
766}
767
Tim Murray509ea5c2012-11-13 11:56:40 -0800768void rsi_Allocation1DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
769 uint32_t count, void *data, size_t sizeBytes) {
770 Allocation *a = static_cast<Allocation *>(va);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700771 rsc->mHal.funcs.allocation.read1D(rsc, a, xoff, lod, count, data, sizeBytes);
Tim Murray509ea5c2012-11-13 11:56:40 -0800772}
773
Tim Murray7b3e3092012-11-16 13:32:24 -0800774void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff,
775 uint32_t lod, RsAllocationCubemapFace face, uint32_t w,
Tim Murray358747a2012-11-26 13:52:04 -0800776 uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Tim Murray7b3e3092012-11-16 13:32:24 -0800777 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800778 a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Tim Murray7b3e3092012-11-16 13:32:24 -0800779}
780
Jason Samsc975cf42011-04-28 18:26:48 -0700781}
782}
783
Tim Murrayc2ce7072013-07-17 18:38:53 -0700784extern "C" const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Jason Samsc975cf42011-04-28 18:26:48 -0700785 Allocation *a = static_cast<Allocation *>(va);
786 a->getType()->incUserRef();
787
788 return a->getType();
789}