blob: 0e742423b5a775b83e1ec86909906c08e33f528a [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Jason Samsbc0ca6b2013-02-15 18:13:43 -08002 * Copyright (C) 2013 The Android Open Source Project
Jason Sams326e0dd2009-05-22 14:03:28 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jason Sams326e0dd2009-05-22 14:03:28 -070016
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080017#include "rsContext.h"
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080018#include "rsAllocation.h"
19#include "rsAdapter.h"
Jason Samseb4fe182011-05-26 16:33:01 -070020#include "rs_hal.h"
21
Stephen Hinesb0934b62013-07-03 17:27:38 -070022#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
Jason Sams7ac2a4d2012-02-15 12:04:24 -080023#include "system/window.h"
Andy McFadden58fd6a52012-12-18 09:50:03 -080024#include "gui/GLConsumer.h"
Tim Murray0b575de2013-03-15 15:56:43 -070025#endif
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070026
Jason Sams326e0dd2009-05-22 14:03:28 -070027using namespace android;
28using namespace android::renderscript;
29
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080030Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080031 RsAllocationMipmapControl mc, void * ptr)
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080032 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080033
Jason Samseb4fe182011-05-26 16:33:01 -070034 memset(&mHal, 0, sizeof(mHal));
35 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsbad80742011-03-16 16:29:28 -070036 mHal.state.usageFlags = usages;
37 mHal.state.mipmapControl = mc;
Tim Murray2e1a94d2012-11-29 13:12:25 -080038 mHal.state.userProvidedPtr = ptr;
Jason Sams366c9c82010-12-08 16:14:36 -080039
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070040 setType(type);
Jason Samsbad80742011-03-16 16:29:28 -070041 updateCache();
42}
Jason Samsfa84da22010-03-01 15:31:04 -080043
Jason Samscfea6c12015-02-09 12:50:22 -080044Allocation::Allocation(Context *rsc, const Allocation *alloc, const Type *type)
45 : ObjectBase(rsc) {
46
47 memset(&mHal, 0, sizeof(mHal));
Jason Samscfea6c12015-02-09 12:50:22 -080048 mHal.state.baseAlloc = alloc;
Jason Samscfea6c12015-02-09 12:50:22 -080049 mHal.state.usageFlags = alloc->mHal.state.usageFlags;
50 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
51
52 setType(type);
53 updateCache();
Jason Samscfea6c12015-02-09 12:50:22 -080054}
55
Tim Murray34689382013-03-11 12:12:03 -070056void Allocation::operator delete(void* ptr) {
57 if (ptr) {
58 Allocation *a = (Allocation*) ptr;
59 a->getContext()->mHal.funcs.freeRuntimeMem(ptr);
60 }
61}
62
Jason Samseb4fe182011-05-26 16:33:01 -070063Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080064 RsAllocationMipmapControl mc, void * ptr) {
Tim Murray34689382013-03-11 12:12:03 -070065 // Allocation objects must use allocator specified by the driver
66 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Allocation), 0);
67
68 if (!allocMem) {
69 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation");
Chris Wailes44bef6f2014-08-12 13:51:10 -070070 return nullptr;
Tim Murray34689382013-03-11 12:12:03 -070071 }
72
73 Allocation *a = new (allocMem) Allocation(rsc, type, usages, mc, ptr);
Jason Samseb4fe182011-05-26 16:33:01 -070074
75 if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
76 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
77 delete a;
Chris Wailes44bef6f2014-08-12 13:51:10 -070078 return nullptr;
Jason Samseb4fe182011-05-26 16:33:01 -070079 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -080080
Jason Samseb4fe182011-05-26 16:33:01 -070081 return a;
82}
83
Jason Samscfea6c12015-02-09 12:50:22 -080084Allocation * Allocation::createAdapter(Context *rsc, const Allocation *alloc, const Type *type) {
85 // Allocation objects must use allocator specified by the driver
86 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Allocation), 0);
87
88 if (!allocMem) {
89 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation");
90 return nullptr;
91 }
92
93 Allocation *a = new (allocMem) Allocation(rsc, alloc, type);
94
95 if (!rsc->mHal.funcs.allocation.initAdapter(rsc, a)) {
96 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
97 delete a;
98 return nullptr;
99 }
100
101 return a;
102}
103
Jason Sams442b7ff2015-03-06 16:50:11 -0800104void Allocation::adapterOffset(Context *rsc, const uint32_t *offsets, size_t len) {
105 if (len >= sizeof(uint32_t) * 9) {
106 mHal.state.originX = offsets[0];
107 mHal.state.originY = offsets[1];
108 mHal.state.originZ = offsets[2];
109 mHal.state.originLOD = offsets[3];
110 mHal.state.originFace = offsets[4];
111 mHal.state.originArray[0] = offsets[5];
112 mHal.state.originArray[1] = offsets[6];
113 mHal.state.originArray[2] = offsets[7];
114 mHal.state.originArray[3] = offsets[8];
115 }
116
117 rsc->mHal.funcs.allocation.adapterOffset(rsc, this);
118}
119
120
Jason Samscfea6c12015-02-09 12:50:22 -0800121
Jason Samsbad80742011-03-16 16:29:28 -0700122void Allocation::updateCache() {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700123 const Type *type = mHal.state.type;
Jason Samsa572aca2013-01-09 11:52:26 -0800124 mHal.state.yuv = type->getDimYuv();
Jason Samsbad80742011-03-16 16:29:28 -0700125 mHal.state.hasFaces = type->getDimFaces();
126 mHal.state.hasMipmaps = type->getDimLOD();
127 mHal.state.elementSizeBytes = type->getElementSizeBytes();
128 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -0700129}
130
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800131Allocation::~Allocation() {
Jason Samsddceab92013-08-07 13:02:32 -0700132#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
133 if (mGrallocConsumer.get()) {
134 mGrallocConsumer->unlockBuffer();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700135 mGrallocConsumer = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700136 }
137#endif
138
Jason Samsc7cec1e2011-08-18 18:01:33 -0700139 freeChildrenUnlocked();
Jason Samseb4fe182011-05-26 16:33:01 -0700140 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700141}
142
Jason Sams366c9c82010-12-08 16:14:36 -0800143void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -0700144 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -0800145}
146
Jason Samsb8a94e22014-02-24 17:52:32 -0800147void * Allocation::getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face,
148 uint32_t z, uint32_t array, size_t *stride) {
149
150 if ((lod >= mHal.drvState.lodCount) ||
151 (z && (z >= mHal.drvState.lod[lod].dimZ)) ||
152 ((face != RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X) && !mHal.state.hasFaces) ||
153 (array != 0)) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700154 return nullptr;
Jason Samsb8a94e22014-02-24 17:52:32 -0800155 }
156
157 size_t s = 0;
158 //void *ptr = mRSC->mHal.funcs.allocation.lock1D(rsc, this);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700159 if ((stride != nullptr) && mHal.drvState.lod[0].dimY) {
Jason Samsb8a94e22014-02-24 17:52:32 -0800160 *stride = mHal.drvState.lod[lod].stride;
161 }
162 return mHal.drvState.lod[lod].mallocPtr;
163}
164
Jason Sams4b45b892010-12-29 14:31:29 -0800165void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800166 uint32_t count, const void *data, size_t sizeBytes) {
167 const size_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -0700168
Jason Samseb4fe182011-05-26 16:33:01 -0700169 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800170 char buf[1024];
171 sprintf(buf, "Allocation::subData called with mismatched size expected %zu, got %zu",
172 (count * eSize), sizeBytes);
173 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Samsbad80742011-03-16 16:29:28 -0700174 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700175 return;
176 }
Jason Samse3929c92010-08-09 18:13:33 -0700177
Jason Samseb4fe182011-05-26 16:33:01 -0700178 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
179 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700180}
181
Jason Sams4b45b892010-12-29 14:31:29 -0800182void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800183 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Tim Murray358747a2012-11-26 13:52:04 -0800184 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Samseb4fe182011-05-26 16:33:01 -0700185 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700186}
187
Jason Sams236385b2011-01-12 14:53:25 -0800188void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700189 uint32_t lod,
190 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
191 rsc->mHal.funcs.allocation.data3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
192 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700193}
194
Jason Sams807fdc42012-07-25 17:55:39 -0700195void Allocation::read(Context *rsc, uint32_t xoff, uint32_t lod,
Tim Murray358747a2012-11-26 13:52:04 -0800196 uint32_t count, void *data, size_t sizeBytes) {
Jason Sams807fdc42012-07-25 17:55:39 -0700197 const size_t eSize = mHal.state.type->getElementSizeBytes();
198
199 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800200 char buf[1024];
201 sprintf(buf, "Allocation::read called with mismatched size expected %zu, got %zu",
202 (count * eSize), sizeBytes);
203 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams807fdc42012-07-25 17:55:39 -0700204 mHal.state.type->dumpLOGV("type info");
205 return;
206 }
207
208 rsc->mHal.funcs.allocation.read1D(rsc, this, xoff, lod, count, data, sizeBytes);
209}
210
Tim Murray358747a2012-11-26 13:52:04 -0800211void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
212 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
213 const size_t eSize = mHal.state.elementSizeBytes;
214 const size_t lineSize = eSize * w;
215 if (!stride) {
216 stride = lineSize;
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700217 } else {
218 if ((lineSize * h) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800219 char buf[1024];
220 sprintf(buf, "Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
221 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700222 return;
223 }
Tim Murray358747a2012-11-26 13:52:04 -0800224 }
225
226 rsc->mHal.funcs.allocation.read2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams807fdc42012-07-25 17:55:39 -0700227}
228
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700229void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
230 uint32_t w, uint32_t h, uint32_t d, 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;
235 }
236
237 rsc->mHal.funcs.allocation.read3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
238
Jason Sams807fdc42012-07-25 17:55:39 -0700239}
240
Miao Wangcc8cea72015-02-19 18:14:46 -0800241void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
242 const void *data, uint32_t cIdx, size_t sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800243 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700244
Jason Samsa572aca2013-01-09 11:52:26 -0800245 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700246 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
247 return;
248 }
249
Miao Wangcc8cea72015-02-19 18:14:46 -0800250 if (y > 0 && y >= mHal.drvState.lod[0].dimY) {
251 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Y offset out of range.");
252 return;
253 }
254
255 if (z > 0 && z >= mHal.drvState.lod[0].dimZ) {
256 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Z offset out of range.");
257 return;
258 }
259
260 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
261 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
262 return;
263 }
264
Jason Samsbad80742011-03-16 16:29:28 -0700265 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800266 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
267 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700268 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
269 return;
270 }
271
Miao Wangcc8cea72015-02-19 18:14:46 -0800272 rsc->mHal.funcs.allocation.elementData(rsc, this, x, y, z, data, cIdx, sizeBytes);
Jason Samseb4fe182011-05-26 16:33:01 -0700273 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700274}
275
Miao Wangcc8cea72015-02-19 18:14:46 -0800276void Allocation::elementRead(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
277 void *data, uint32_t cIdx, size_t sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800278 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700279
Jason Samsa572aca2013-01-09 11:52:26 -0800280 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700281 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
282 return;
283 }
284
Miao Wangcc8cea72015-02-19 18:14:46 -0800285 if (y > 0 && y >= mHal.drvState.lod[0].dimY) {
286 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Y offset out of range.");
287 return;
288 }
289
290 if (z > 0 && z >= mHal.drvState.lod[0].dimZ) {
291 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Z offset out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700292 return;
293 }
294
Jason Samsbad80742011-03-16 16:29:28 -0700295 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Yang Nib8353c52015-02-14 18:00:59 -0800296 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700297 return;
298 }
299
Jason Samsbad80742011-03-16 16:29:28 -0700300 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Yang Nib8353c52015-02-14 18:00:59 -0800301 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800302 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700303 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
304 return;
305 }
306
Miao Wangcc8cea72015-02-19 18:14:46 -0800307 rsc->mHal.funcs.allocation.elementRead(rsc, this, x, y, z, data, cIdx, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700308}
309
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800310void Allocation::addProgramToDirty(const Program *p) {
Yang Nib8353c52015-02-14 18:00:59 -0800311 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700312}
Jason Sams326e0dd2009-05-22 14:03:28 -0700313
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800314void Allocation::removeProgramToDirty(const Program *p) {
Yang Nib8353c52015-02-14 18:00:59 -0800315 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
316 if (mToDirtyList[ct] == p) {
317 mToDirtyList.removeAt(ct);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700318 return;
319 }
320 }
321 rsAssert(0);
322}
323
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800324void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800325 ObjectBase::dumpLOGV(prefix);
Jason Sams48ecf6a2013-07-09 15:35:29 -0700326 char buf[1024];
Jason Samsc21cf402009-11-17 17:26:46 -0800327
Jason Sams48ecf6a2013-07-09 15:35:29 -0700328 if ((strlen(prefix) + 10) < sizeof(buf)) {
329 sprintf(buf, "%s type ", prefix);
330 if (mHal.state.type) {
331 mHal.state.type->dumpLOGV(buf);
332 }
Jason Samsc21cf402009-11-17 17:26:46 -0800333 }
Steve Block65982012011-10-20 11:56:00 +0100334 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Yang Nib8353c52015-02-14 18:00:59 -0800335 prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800336}
Jason Sams326e0dd2009-05-22 14:03:28 -0700337
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800338uint32_t Allocation::getPackedSize() const {
Jason Sams61656a72013-09-03 16:21:18 -0700339 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800340 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
341}
342
Jason Samse3150cf2012-07-24 18:10:20 -0700343void Allocation::writePackedData(Context *rsc, const Type *type,
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800344 uint8_t *dst, const uint8_t *src, bool dstPadded) {
345 const Element *elem = type->getElement();
346 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
347 uint32_t paddedBytes = elem->getSizeBytes();
Jason Sams61656a72013-09-03 16:21:18 -0700348 uint32_t numItems = type->getPackedSizeBytes() / paddedBytes;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800349
350 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
351 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
352
353 // no sub-elements
354 uint32_t fieldCount = elem->getFieldCount();
355 if (fieldCount == 0) {
356 for (uint32_t i = 0; i < numItems; i ++) {
357 memcpy(dst, src, unpaddedBytes);
358 src += srcInc;
359 dst += dstInc;
360 }
361 return;
362 }
363
364 // Cache offsets
365 uint32_t *offsetsPadded = new uint32_t[fieldCount];
366 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
367 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
368
369 for (uint32_t i = 0; i < fieldCount; i++) {
370 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
371 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
372 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
373 }
374
375 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
376 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
377
378 // complex elements, need to copy subelem after subelem
379 for (uint32_t i = 0; i < numItems; i ++) {
380 for (uint32_t fI = 0; fI < fieldCount; fI++) {
381 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
382 }
383 src += srcInc;
384 dst += dstInc;
385 }
386
387 delete[] offsetsPadded;
388 delete[] offsetsUnpadded;
389 delete[] sizeUnpadded;
390}
391
Jason Samse3150cf2012-07-24 18:10:20 -0700392void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800393 const uint8_t *src = (const uint8_t*)data;
Jason Sams61a4bb72012-07-25 19:33:43 -0700394 uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800395
Jason Samse3150cf2012-07-24 18:10:20 -0700396 writePackedData(rsc, getType(), dst, src, true);
Jason Sams61a4bb72012-07-25 19:33:43 -0700397 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800398}
399
Jason Samse3150cf2012-07-24 18:10:20 -0700400void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800401 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
402 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
Jason Sams61656a72013-09-03 16:21:18 -0700403 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800404
Jason Sams61a4bb72012-07-25 19:33:43 -0700405 const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800406 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
407
Jason Samse3150cf2012-07-24 18:10:20 -0700408 writePackedData(rsc, getType(), dst, src, false);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800409 stream->addByteArray(dst, getPackedSize());
410
411 delete[] dst;
Jason Sams61a4bb72012-07-25 19:33:43 -0700412 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800413}
414
Jason Samse3150cf2012-07-24 18:10:20 -0700415void Allocation::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700416 // Need to identify ourselves
417 stream->addU32((uint32_t)getClassId());
Jason Sams48ecf6a2013-07-09 15:35:29 -0700418 stream->addString(getName());
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700419
420 // First thing we need to serialize is the type object since it will be needed
421 // to initialize the class
Jason Samse3150cf2012-07-24 18:10:20 -0700422 mHal.state.type->serialize(rsc, stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700423
Jason Sams61656a72013-09-03 16:21:18 -0700424 uint32_t dataSize = mHal.state.type->getPackedSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800425 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
426 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700427 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800428 stream->addU32(packedSize);
429 if (dataSize == packedSize) {
430 // Now write the data
Jason Sams61a4bb72012-07-25 19:33:43 -0700431 stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize);
432 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800433 } else {
434 // Now write the data
Jason Samse3150cf2012-07-24 18:10:20 -0700435 packVec3Allocation(rsc, stream);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800436 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700437}
438
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800439Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700440 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700441 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800442 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Jason Samsa2737932014-01-14 12:28:33 -0800443 rsc->setError(RS_ERROR_FATAL_DRIVER,
444 "allocation loading failed due to corrupt file. (invalid id)\n");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700445 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700446 }
447
Jason Sams48ecf6a2013-07-09 15:35:29 -0700448 const char *name = stream->loadString();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700449
450 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800451 if (!type) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700452 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700453 }
454 type->compute();
455
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800456 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
457 type->decUserRef();
458
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700459 // Number of bytes we wrote out for this allocation
460 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800461 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
462 uint32_t packedSize = alloc->getPackedSize();
Jason Sams61656a72013-09-03 16:21:18 -0700463 if (dataSize != type->getPackedSizeBytes() &&
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800464 dataSize != packedSize) {
Jason Samsa2737932014-01-14 12:28:33 -0800465 rsc->setError(RS_ERROR_FATAL_DRIVER,
466 "allocation loading failed due to corrupt file. (invalid size)\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800467 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700468 ObjectBase::checkDelete(type);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700469 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700470 }
471
Jason Sams48ecf6a2013-07-09 15:35:29 -0700472 alloc->assignName(name);
Jason Sams61656a72013-09-03 16:21:18 -0700473 if (dataSize == type->getPackedSizeBytes()) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800474 uint32_t count = dataSize / type->getElementSizeBytes();
475 // Read in all of our allocation data
476 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
477 } else {
Jason Samse3150cf2012-07-24 18:10:20 -0700478 alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800479 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700480 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700481
482 return alloc;
483}
484
Jason Samseb4fe182011-05-26 16:33:01 -0700485void Allocation::sendDirty(const Context *rsc) const {
Jason Sams93eacc72012-12-18 14:26:57 -0800486#ifndef RS_COMPATIBILITY_LIB
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700487 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
488 mToDirtyList[ct]->forceDirty();
489 }
Jason Sams93eacc72012-12-18 14:26:57 -0800490#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700491 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700492}
Jason Sams326e0dd2009-05-22 14:03:28 -0700493
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800494void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700495 mHal.state.type->incRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700496}
497
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800498void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700499 if (!mHal.state.hasReferences || !getIsScript()) {
500 return;
501 }
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700502 mHal.state.type->decRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700503}
504
Jason Samsa36c50a2014-06-17 12:06:06 -0700505void Allocation::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700506 if (rsc->mHal.funcs.allocation.updateCachedObject != nullptr) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700507 rsc->mHal.funcs.allocation.updateCachedObject(rsc, this, (rs_allocation *)dstObj);
508 } else {
509 *((const void **)dstObj) = this;
510 }
511}
512
513
Jason Samsc7cec1e2011-08-18 18:01:33 -0700514void Allocation::freeChildrenUnlocked () {
Jason Sams61a4bb72012-07-25 19:33:43 -0700515 void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this);
Jason Sams61656a72013-09-03 16:21:18 -0700516 decRefs(ptr, mHal.state.type->getCellCount(), 0);
Jason Sams61a4bb72012-07-25 19:33:43 -0700517 mRSC->mHal.funcs.allocation.unlock1D(mRSC, this);
Jason Samsc7cec1e2011-08-18 18:01:33 -0700518}
519
520bool Allocation::freeChildren() {
521 if (mHal.state.hasReferences) {
522 incSysRef();
523 freeChildrenUnlocked();
524 return decSysRef();
525 }
526 return false;
527}
528
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800529void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700530}
531
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800532void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsa572aca2013-01-09 11:52:26 -0800533 uint32_t oldDimX = mHal.drvState.lod[0].dimX;
Jason Sams96abf812010-10-05 13:32:49 -0700534 if (dimX == oldDimX) {
535 return;
536 }
537
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700538 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700539 if (dimX < oldDimX) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700540 decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX);
541 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Jason Sams96abf812010-10-05 13:32:49 -0700542 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700543 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700544 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700545 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700546}
547
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800548void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Samsa2737932014-01-14 12:28:33 -0800549 rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700550}
551
Jason Samsddceab92013-08-07 13:02:32 -0700552#ifndef RS_COMPATIBILITY_LIB
Dan Stozaf0d7aa22014-11-04 11:38:47 -0800553void Allocation::NewBufferListener::onFrameAvailable(const BufferItem& /* item */) {
Jason Samsddceab92013-08-07 13:02:32 -0700554 intptr_t ip = (intptr_t)alloc;
Tim Murraye4ed0872014-08-18 16:13:08 -0700555 rsc->sendMessageToClient(&ip, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, 0, sizeof(ip), true);
Jason Samsddceab92013-08-07 13:02:32 -0700556}
557#endif
558
Jason Sams733396b2013-02-22 12:46:18 -0800559void * Allocation::getSurface(const Context *rsc) {
Jason Samsddceab92013-08-07 13:02:32 -0700560#ifndef RS_COMPATIBILITY_LIB
561 // Configure GrallocConsumer to be in asynchronous mode
Dan Stoza03c155b2014-03-12 12:06:18 -0700562 sp<IGraphicBufferProducer> bp;
563 sp<IGraphicBufferConsumer> bc;
564 BufferQueue::createBufferQueue(&bp, &bc);
Jason Samse49da132014-10-23 17:32:27 -0700565 mGrallocConsumer = new GrallocConsumer(this, bc, mHal.drvState.grallocFlags);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700566 bp->incStrong(nullptr);
Jason Samsddceab92013-08-07 13:02:32 -0700567
568 mBufferListener = new NewBufferListener();
569 mBufferListener->rsc = rsc;
570 mBufferListener->alloc = this;
571
572 mGrallocConsumer->setFrameAvailableListener(mBufferListener);
573 return bp.get();
574#else
Chris Wailes44bef6f2014-08-12 13:51:10 -0700575 return nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700576#endif
577 //return rsc->mHal.funcs.allocation.getSurface(rsc, this);
Jason Sams41e373d2012-01-13 14:01:20 -0800578}
579
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800580void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
581 ANativeWindow *nw = (ANativeWindow *)sur;
Jason Sams733396b2013-02-22 12:46:18 -0800582 rsc->mHal.funcs.allocation.setSurface(rsc, this, nw);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800583}
584
585void Allocation::ioSend(const Context *rsc) {
586 rsc->mHal.funcs.allocation.ioSend(rsc, this);
587}
588
589void Allocation::ioReceive(const Context *rsc) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700590 void *ptr = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700591 size_t stride = 0;
592#ifndef RS_COMPATIBILITY_LIB
593 if (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
594 status_t ret = mGrallocConsumer->lockNextBuffer();
595
596 if (ret == OK) {
597 rsc->mHal.funcs.allocation.ioReceive(rsc, this);
598 } else if (ret == BAD_VALUE) {
599 // No new frame, don't do anything
600 } else {
601 rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer.");
602 }
603
604 }
605#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800606}
607
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700608bool Allocation::hasSameDims(const Allocation *other) const {
609 const Type *type0 = this->getType(),
610 *type1 = other->getType();
611
612 return (type0->getCellCount() == type1->getCellCount()) &&
613 (type0->getDimLOD() == type1->getDimLOD()) &&
614 (type0->getDimFaces() == type1->getDimFaces()) &&
615 (type0->getDimYuv() == type1->getDimYuv()) &&
616 (type0->getDimX() == type1->getDimX()) &&
617 (type0->getDimY() == type1->getDimY()) &&
618 (type0->getDimZ() == type1->getDimZ());
619}
620
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800621
Jason Sams326e0dd2009-05-22 14:03:28 -0700622/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700623//
Stephen Hines6a121812011-03-01 17:34:59 -0800624
Jason Sams326e0dd2009-05-22 14:03:28 -0700625namespace android {
626namespace renderscript {
627
Jason Sams366c9c82010-12-08 16:14:36 -0800628void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
629 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700630 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800631 a->syncAll(rsc, src);
632}
633
Jason Samsa2371512011-01-12 13:28:37 -0800634void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700635 Allocation *alloc = static_cast<Allocation *>(va);
636 rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc);
Jason Sams837e3882010-12-10 16:03:15 -0800637}
638
Jason Sams807fdc42012-07-25 17:55:39 -0700639void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
640 Allocation *a = static_cast<Allocation *>(va);
641 const Type * t = a->getType();
642 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700643 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700644}
645
Jason Sams4b45b892010-12-29 14:31:29 -0800646void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700647 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700648 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800649 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700650}
651
Miao Wangcc8cea72015-02-19 18:14:46 -0800652void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x,
653 uint32_t lod, const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700654 Allocation *a = static_cast<Allocation *>(va);
Miao Wangcc8cea72015-02-19 18:14:46 -0800655 a->elementData(rsc, x, 0, 0, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700656}
657
Miao Wangcc8cea72015-02-19 18:14:46 -0800658void rsi_AllocationElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
659 uint32_t lod, const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700660 Allocation *a = static_cast<Allocation *>(va);
Miao Wangcc8cea72015-02-19 18:14:46 -0800661 a->elementData(rsc, x, y, z, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700662}
663
Jason Sams4b45b892010-12-29 14:31:29 -0800664void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800665 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700666 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800667 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams326e0dd2009-05-22 14:03:28 -0700668}
669
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700670void rsi_Allocation3DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
671 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
672 Allocation *a = static_cast<Allocation *>(va);
673 a->data(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
674}
675
676
Jason Sams807fdc42012-07-25 17:55:39 -0700677void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
Jason Samse579df42009-08-10 14:55:26 -0700678 Allocation *a = static_cast<Allocation *>(va);
Jason Sams807fdc42012-07-25 17:55:39 -0700679 const Type * t = a->getType();
Miao Wangcc8cea72015-02-19 18:14:46 -0800680 if(t->getDimZ()) {
681 a->read(rsc, 0, 0, 0, 0, t->getDimX(), t->getDimY(), t->getDimZ(),
682 data, sizeBytes, 0);
683 } else if(t->getDimY()) {
Jason Sams807fdc42012-07-25 17:55:39 -0700684 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700685 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Jason Sams807fdc42012-07-25 17:55:39 -0700686 } else {
687 a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes);
688 }
689
Jason Samse579df42009-08-10 14:55:26 -0700690}
691
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800692void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700693 Allocation *a = static_cast<Allocation *>(va);
694 a->resize1D(rsc, dimX);
695}
696
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800697void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700698 Allocation *a = static_cast<Allocation *>(va);
699 a->resize2D(rsc, dimX, dimY);
700}
701
Jason Samsc975cf42011-04-28 18:26:48 -0700702RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800703 RsAllocationMipmapControl mipmaps,
Tim Murray099bc262013-03-20 16:54:03 -0700704 uint32_t usages, uintptr_t ptr) {
Stephen Hines8f615d62013-12-20 12:23:32 -0800705 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mipmaps, (void*)ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700706 if (!alloc) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700707 return nullptr;
Jason Samseb4fe182011-05-26 16:33:01 -0700708 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700709 alloc->incUserRef();
710 return alloc;
711}
712
Jason Samsc975cf42011-04-28 18:26:48 -0700713RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800714 RsAllocationMipmapControl mipmaps,
Jason Sams807fdc42012-07-25 17:55:39 -0700715 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800716 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700717
Stephen Hines8f615d62013-12-20 12:23:32 -0800718 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700719 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700720 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000721 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700722 return nullptr;
Jason Samsf0c1df42010-10-26 13:09:17 -0700723 }
724
Jason Sams807fdc42012-07-25 17:55:39 -0700725 texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray60c27962013-02-07 16:15:23 -0800726 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Stephen Hines8f615d62013-12-20 12:23:32 -0800727 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700728 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700729 }
730
Jason Samseb4fe182011-05-26 16:33:01 -0700731 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700732 return texAlloc;
733}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800734
Jason Samsc975cf42011-04-28 18:26:48 -0700735RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800736 RsAllocationMipmapControl mipmaps,
Jason Sams807fdc42012-07-25 17:55:39 -0700737 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800738 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800739
740 // Cubemap allocation's faces should be Width by Width each.
741 // Source data should have 6 * Width by Width pixels
742 // Error checking is done in the java layer
Stephen Hines8f615d62013-12-20 12:23:32 -0800743 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800744 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700745 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000746 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700747 return nullptr;
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800748 }
749
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800750 uint32_t faceSize = t->getDimX();
751 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
752 uint32_t copySize = faceSize * t->getElementSizeBytes();
753
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800754 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800755 for (uint32_t face = 0; face < 6; face ++) {
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800756 for (uint32_t dI = 0; dI < faceSize; dI ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700757 texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face,
Tim Murray60c27962013-02-07 16:15:23 -0800758 t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize, 0);
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800759 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800760
Jason Sams366c9c82010-12-08 16:14:36 -0800761 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800762 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800763 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800764
Stephen Hines8f615d62013-12-20 12:23:32 -0800765 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700766 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800767 }
768
Jason Samseb4fe182011-05-26 16:33:01 -0700769 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800770 return texAlloc;
771}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800772
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700773void rsi_AllocationCopy2DRange(Context *rsc,
774 RsAllocation dstAlloc,
775 uint32_t dstXoff, uint32_t dstYoff,
776 uint32_t dstMip, uint32_t dstFace,
777 uint32_t width, uint32_t height,
778 RsAllocation srcAlloc,
779 uint32_t srcXoff, uint32_t srcYoff,
780 uint32_t srcMip, uint32_t srcFace) {
781 Allocation *dst = static_cast<Allocation *>(dstAlloc);
782 Allocation *src= static_cast<Allocation *>(srcAlloc);
783 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
784 (RsAllocationCubemapFace)dstFace,
785 width, height,
786 src, srcXoff, srcYoff,srcMip,
787 (RsAllocationCubemapFace)srcFace);
788}
789
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700790void rsi_AllocationCopy3DRange(Context *rsc,
791 RsAllocation dstAlloc,
792 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
793 uint32_t dstMip,
794 uint32_t width, uint32_t height, uint32_t depth,
795 RsAllocation srcAlloc,
796 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
797 uint32_t srcMip) {
798 Allocation *dst = static_cast<Allocation *>(dstAlloc);
799 Allocation *src= static_cast<Allocation *>(srcAlloc);
800 rsc->mHal.funcs.allocation.allocData3D(rsc, dst, dstXoff, dstYoff, dstZoff, dstMip,
801 width, height, depth,
802 src, srcXoff, srcYoff, srcZoff, srcMip);
803}
804
805
Jason Sams733396b2013-02-22 12:46:18 -0800806void * rsi_AllocationGetSurface(Context *rsc, RsAllocation valloc) {
Jason Sams41e373d2012-01-13 14:01:20 -0800807 Allocation *alloc = static_cast<Allocation *>(valloc);
Jason Sams733396b2013-02-22 12:46:18 -0800808 void *s = alloc->getSurface(rsc);
809 return s;
Jason Sams3522f402012-03-23 11:47:26 -0700810}
811
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800812void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
813 Allocation *alloc = static_cast<Allocation *>(valloc);
814 alloc->setSurface(rsc, sur);
815}
816
817void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
818 Allocation *alloc = static_cast<Allocation *>(valloc);
819 alloc->ioSend(rsc);
820}
821
822void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
823 Allocation *alloc = static_cast<Allocation *>(valloc);
824 alloc->ioReceive(rsc);
825}
826
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700827void *rsi_AllocationGetPointer(Context *rsc, RsAllocation valloc,
Jason Samsb8a94e22014-02-24 17:52:32 -0800828 uint32_t lod, RsAllocationCubemapFace face,
829 uint32_t z, uint32_t array, size_t *stride, size_t strideLen) {
830 Allocation *alloc = static_cast<Allocation *>(valloc);
Tim Murray0e61af92014-02-26 13:28:52 -0800831 rsAssert(strideLen == sizeof(size_t));
Jason Samsb8a94e22014-02-24 17:52:32 -0800832
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700833 return alloc->getPointer(rsc, lod, face, z, array, stride);
Jason Samsb8a94e22014-02-24 17:52:32 -0800834}
835
Tim Murray509ea5c2012-11-13 11:56:40 -0800836void rsi_Allocation1DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
837 uint32_t count, void *data, size_t sizeBytes) {
838 Allocation *a = static_cast<Allocation *>(va);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700839 rsc->mHal.funcs.allocation.read1D(rsc, a, xoff, lod, count, data, sizeBytes);
Tim Murray509ea5c2012-11-13 11:56:40 -0800840}
841
Miao Wangcc8cea72015-02-19 18:14:46 -0800842void rsi_AllocationElementRead(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
843 uint32_t lod, void *data, size_t sizeBytes, size_t eoff) {
844 Allocation *a = static_cast<Allocation *>(va);
845 a->elementRead(rsc, x, y, z, data, eoff, sizeBytes);
846}
847
Tim Murray7b3e3092012-11-16 13:32:24 -0800848void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff,
849 uint32_t lod, RsAllocationCubemapFace face, uint32_t w,
Tim Murray358747a2012-11-26 13:52:04 -0800850 uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Tim Murray7b3e3092012-11-16 13:32:24 -0800851 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800852 a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Tim Murray7b3e3092012-11-16 13:32:24 -0800853}
854
Miao Wangcc8cea72015-02-19 18:14:46 -0800855void rsi_Allocation3DRead(Context *rsc, RsAllocation va,
856 uint32_t xoff, uint32_t yoff, uint32_t zoff,
857 uint32_t lod, uint32_t w, uint32_t h, uint32_t d,
858 void *data, size_t sizeBytes, size_t stride) {
859 Allocation *a = static_cast<Allocation *>(va);
860 a->read(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
861}
862
Jason Samscfea6c12015-02-09 12:50:22 -0800863RsAllocation rsi_AllocationAdapterCreate(Context *rsc, RsType vwindow, RsAllocation vbase) {
864
865
866 Allocation * alloc = Allocation::createAdapter(rsc,
867 static_cast<Allocation *>(vbase), static_cast<Type *>(vwindow));
868 if (!alloc) {
869 return nullptr;
870 }
871 alloc->incUserRef();
872 return alloc;
873}
874
875void rsi_AllocationAdapterOffset(Context *rsc, RsAllocation va, const uint32_t *offsets, size_t len) {
Jason Sams442b7ff2015-03-06 16:50:11 -0800876 Allocation *a = static_cast<Allocation *>(va);
877 a->adapterOffset(rsc, offsets, len);
Jason Samscfea6c12015-02-09 12:50:22 -0800878}
879
880
Jason Samsc975cf42011-04-28 18:26:48 -0700881}
882}
883
Tim Murrayc2ce7072013-07-17 18:38:53 -0700884extern "C" const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Jason Samsc975cf42011-04-28 18:26:48 -0700885 Allocation *a = static_cast<Allocation *>(va);
886 a->getType()->incUserRef();
887
888 return a->getType();
889}