blob: a2104352b99a6e9c760efd5ce01ed29d2697a5d6 [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"
Jason Samseb4fe182011-05-26 16:33:01 -070019#include "rs_hal.h"
20
Miao Wangf750c532017-03-03 16:03:44 -080021#ifndef RS_COMPATIBILITY_LIB
22#include "rsGrallocConsumer.h"
23#endif
24
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -080025namespace android {
26namespace renderscript {
Jason Sams326e0dd2009-05-22 14:03:28 -070027
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080028Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080029 RsAllocationMipmapControl mc, void * ptr)
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080030 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080031
Jason Samseb4fe182011-05-26 16:33:01 -070032 memset(&mHal, 0, sizeof(mHal));
33 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsbad80742011-03-16 16:29:28 -070034 mHal.state.usageFlags = usages;
35 mHal.state.mipmapControl = mc;
Tim Murray2e1a94d2012-11-29 13:12:25 -080036 mHal.state.userProvidedPtr = ptr;
Jason Sams366c9c82010-12-08 16:14:36 -080037
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070038 setType(type);
Jason Samsbad80742011-03-16 16:29:28 -070039 updateCache();
40}
Jason Samsfa84da22010-03-01 15:31:04 -080041
Jason Samscfea6c12015-02-09 12:50:22 -080042Allocation::Allocation(Context *rsc, const Allocation *alloc, const Type *type)
43 : ObjectBase(rsc) {
44
45 memset(&mHal, 0, sizeof(mHal));
Jason Samscfea6c12015-02-09 12:50:22 -080046 mHal.state.baseAlloc = alloc;
Jason Samscfea6c12015-02-09 12:50:22 -080047 mHal.state.usageFlags = alloc->mHal.state.usageFlags;
48 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
49
50 setType(type);
51 updateCache();
Jason Samscfea6c12015-02-09 12:50:22 -080052}
53
Tim Murray34689382013-03-11 12:12:03 -070054void Allocation::operator delete(void* ptr) {
55 if (ptr) {
56 Allocation *a = (Allocation*) ptr;
57 a->getContext()->mHal.funcs.freeRuntimeMem(ptr);
58 }
59}
60
Miao Wang47a58812015-07-23 21:59:16 -070061Allocation * Allocation::createAllocationStrided(Context *rsc, const Type *type, uint32_t usages,
62 RsAllocationMipmapControl mc, void * ptr,
63 size_t requiredAlignment) {
Tim Murray34689382013-03-11 12:12:03 -070064 // Allocation objects must use allocator specified by the driver
65 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Allocation), 0);
66
67 if (!allocMem) {
68 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation");
Chris Wailes44bef6f2014-08-12 13:51:10 -070069 return nullptr;
Tim Murray34689382013-03-11 12:12:03 -070070 }
71
Jason Samsf82b6262015-05-11 15:02:50 -070072 bool success = false;
73 Allocation *a = nullptr;
74 if (usages & RS_ALLOCATION_USAGE_OEM) {
75 if (rsc->mHal.funcs.allocation.initOem != nullptr) {
76 a = new (allocMem) Allocation(rsc, type, usages, mc, nullptr);
77 success = rsc->mHal.funcs.allocation.initOem(rsc, a, type->getElement()->getHasReferences(), ptr);
78 } else {
79 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation Init called with USAGE_OEM but driver does not support it");
80 return nullptr;
81 }
Miao Wang47a58812015-07-23 21:59:16 -070082#ifdef RS_COMPATIBILITY_LIB
83 } else if (usages & RS_ALLOCATION_USAGE_INCREMENTAL_SUPPORT){
84 a = new (allocMem) Allocation(rsc, type, usages, mc, ptr);
85 success = rsc->mHal.funcs.allocation.initStrided(rsc, a, type->getElement()->getHasReferences(), requiredAlignment);
86#endif
Jason Samsf82b6262015-05-11 15:02:50 -070087 } else {
88 a = new (allocMem) Allocation(rsc, type, usages, mc, ptr);
89 success = rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences());
90 }
Jason Samseb4fe182011-05-26 16:33:01 -070091
Jason Samsf82b6262015-05-11 15:02:50 -070092 if (!success) {
Jason Samseb4fe182011-05-26 16:33:01 -070093 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
94 delete a;
Chris Wailes44bef6f2014-08-12 13:51:10 -070095 return nullptr;
Jason Samseb4fe182011-05-26 16:33:01 -070096 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -080097
Jason Samseb4fe182011-05-26 16:33:01 -070098 return a;
99}
100
Miao Wang47a58812015-07-23 21:59:16 -0700101Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
102 RsAllocationMipmapControl mc, void * ptr) {
103 return Allocation::createAllocationStrided(rsc, type, usages, mc, ptr, kMinimumRSAlignment);
104}
105
Jason Samscfea6c12015-02-09 12:50:22 -0800106Allocation * Allocation::createAdapter(Context *rsc, const Allocation *alloc, const Type *type) {
107 // Allocation objects must use allocator specified by the driver
108 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Allocation), 0);
109
110 if (!allocMem) {
111 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation");
112 return nullptr;
113 }
114
115 Allocation *a = new (allocMem) Allocation(rsc, alloc, type);
116
117 if (!rsc->mHal.funcs.allocation.initAdapter(rsc, a)) {
118 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
119 delete a;
120 return nullptr;
121 }
122
123 return a;
124}
125
Jason Sams442b7ff2015-03-06 16:50:11 -0800126void Allocation::adapterOffset(Context *rsc, const uint32_t *offsets, size_t len) {
127 if (len >= sizeof(uint32_t) * 9) {
128 mHal.state.originX = offsets[0];
129 mHal.state.originY = offsets[1];
130 mHal.state.originZ = offsets[2];
131 mHal.state.originLOD = offsets[3];
132 mHal.state.originFace = offsets[4];
133 mHal.state.originArray[0] = offsets[5];
134 mHal.state.originArray[1] = offsets[6];
135 mHal.state.originArray[2] = offsets[7];
136 mHal.state.originArray[3] = offsets[8];
137 }
138
139 rsc->mHal.funcs.allocation.adapterOffset(rsc, this);
140}
141
142
Jason Samscfea6c12015-02-09 12:50:22 -0800143
Jason Samsbad80742011-03-16 16:29:28 -0700144void Allocation::updateCache() {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700145 const Type *type = mHal.state.type;
Jason Samsa572aca2013-01-09 11:52:26 -0800146 mHal.state.yuv = type->getDimYuv();
Jason Samsbad80742011-03-16 16:29:28 -0700147 mHal.state.hasFaces = type->getDimFaces();
148 mHal.state.hasMipmaps = type->getDimLOD();
149 mHal.state.elementSizeBytes = type->getElementSizeBytes();
150 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -0700151}
152
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800153Allocation::~Allocation() {
Miao Wang62237212017-02-27 15:19:36 -0800154#ifndef RS_COMPATIBILITY_LIB
Miao Wangf750c532017-03-03 16:03:44 -0800155 if (mGrallocConsumer) {
Miao Wang75474682015-10-26 16:50:13 -0700156 mGrallocConsumer->releaseIdx(mCurrentIdx);
Miao Wangf750c532017-03-03 16:03:44 -0800157 if (!mGrallocConsumer->isActive()) {
158 delete mGrallocConsumer;
159 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700160 mGrallocConsumer = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700161 }
162#endif
163
Jason Samsc7cec1e2011-08-18 18:01:33 -0700164 freeChildrenUnlocked();
Jason Samseb4fe182011-05-26 16:33:01 -0700165 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700166}
167
Jason Sams366c9c82010-12-08 16:14:36 -0800168void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -0700169 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -0800170}
171
Jason Samsb8a94e22014-02-24 17:52:32 -0800172void * Allocation::getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face,
173 uint32_t z, uint32_t array, size_t *stride) {
174
175 if ((lod >= mHal.drvState.lodCount) ||
176 (z && (z >= mHal.drvState.lod[lod].dimZ)) ||
177 ((face != RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X) && !mHal.state.hasFaces) ||
178 (array != 0)) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700179 return nullptr;
Jason Samsb8a94e22014-02-24 17:52:32 -0800180 }
181
Jason Sams8ce12812015-05-18 17:28:59 -0700182 if (mRSC->mHal.funcs.allocation.getPointer != nullptr) {
183 // Notify the driver, if present that the user is mapping the buffer
184 mRSC->mHal.funcs.allocation.getPointer(rsc, this, lod, face, z, array);
185 }
186
Jason Samsb8a94e22014-02-24 17:52:32 -0800187 size_t s = 0;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700188 if ((stride != nullptr) && mHal.drvState.lod[0].dimY) {
Jason Samsb8a94e22014-02-24 17:52:32 -0800189 *stride = mHal.drvState.lod[lod].stride;
190 }
191 return mHal.drvState.lod[lod].mallocPtr;
192}
193
Jason Sams4b45b892010-12-29 14:31:29 -0800194void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800195 uint32_t count, const void *data, size_t sizeBytes) {
196 const size_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -0700197
Jason Samseb4fe182011-05-26 16:33:01 -0700198 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800199 char buf[1024];
George Burgess IV35fb3212016-03-02 13:55:48 -0800200 snprintf(buf, sizeof(buf),
201 "Allocation::subData called with mismatched size expected %zu, got %zu",
202 (count * eSize), sizeBytes);
Jason Samsa2737932014-01-14 12:28:33 -0800203 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Samsbad80742011-03-16 16:29:28 -0700204 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700205 return;
206 }
Jason Samse3929c92010-08-09 18:13:33 -0700207
Jason Samseb4fe182011-05-26 16:33:01 -0700208 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
209 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700210}
211
Jason Sams4b45b892010-12-29 14:31:29 -0800212void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800213 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Tim Murray358747a2012-11-26 13:52:04 -0800214 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Samseb4fe182011-05-26 16:33:01 -0700215 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700216}
217
Jason Sams236385b2011-01-12 14:53:25 -0800218void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700219 uint32_t lod,
220 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
221 rsc->mHal.funcs.allocation.data3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
222 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700223}
224
Jason Sams807fdc42012-07-25 17:55:39 -0700225void Allocation::read(Context *rsc, uint32_t xoff, uint32_t lod,
Tim Murray358747a2012-11-26 13:52:04 -0800226 uint32_t count, void *data, size_t sizeBytes) {
Jason Sams807fdc42012-07-25 17:55:39 -0700227 const size_t eSize = mHal.state.type->getElementSizeBytes();
228
229 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800230 char buf[1024];
George Burgess IV35fb3212016-03-02 13:55:48 -0800231 snprintf(buf, sizeof(buf),
232 "Allocation::read called with mismatched size expected %zu, got %zu",
233 (count * eSize), sizeBytes);
Jason Samsa2737932014-01-14 12:28:33 -0800234 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams807fdc42012-07-25 17:55:39 -0700235 mHal.state.type->dumpLOGV("type info");
236 return;
237 }
238
239 rsc->mHal.funcs.allocation.read1D(rsc, this, xoff, lod, count, data, sizeBytes);
240}
241
Tim Murray358747a2012-11-26 13:52:04 -0800242void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
243 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
244 const size_t eSize = mHal.state.elementSizeBytes;
245 const size_t lineSize = eSize * w;
246 if (!stride) {
247 stride = lineSize;
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700248 } else {
249 if ((lineSize * h) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800250 char buf[1024];
George Burgess IV35fb3212016-03-02 13:55:48 -0800251 snprintf(buf, sizeof(buf), "Allocation size mismatch, expected %zu, got %zu",
252 (lineSize * h), sizeBytes);
Jason Samsa2737932014-01-14 12:28:33 -0800253 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700254 return;
255 }
Tim Murray358747a2012-11-26 13:52:04 -0800256 }
257
258 rsc->mHal.funcs.allocation.read2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams807fdc42012-07-25 17:55:39 -0700259}
260
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700261void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
262 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride) {
263 const size_t eSize = mHal.state.elementSizeBytes;
264 const size_t lineSize = eSize * w;
265 if (!stride) {
266 stride = lineSize;
267 }
268
269 rsc->mHal.funcs.allocation.read3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
270
Jason Sams807fdc42012-07-25 17:55:39 -0700271}
272
Miao Wangcc8cea72015-02-19 18:14:46 -0800273void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
274 const void *data, uint32_t cIdx, size_t sizeBytes) {
Jason Samsa572aca2013-01-09 11:52:26 -0800275 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700276 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
277 return;
278 }
279
Miao Wangcc8cea72015-02-19 18:14:46 -0800280 if (y > 0 && y >= mHal.drvState.lod[0].dimY) {
281 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Y offset out of range.");
282 return;
283 }
284
285 if (z > 0 && z >= mHal.drvState.lod[0].dimZ) {
286 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Z offset out of range.");
287 return;
288 }
289
290 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
291 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
292 return;
293 }
294
Jason Samsbad80742011-03-16 16:29:28 -0700295 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800296 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
297 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700298 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
299 return;
300 }
301
Miao Wangcc8cea72015-02-19 18:14:46 -0800302 rsc->mHal.funcs.allocation.elementData(rsc, this, x, y, z, data, cIdx, sizeBytes);
Jason Samseb4fe182011-05-26 16:33:01 -0700303 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700304}
305
Miao Wangcc8cea72015-02-19 18:14:46 -0800306void Allocation::elementRead(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
307 void *data, uint32_t cIdx, size_t sizeBytes) {
Jason Samsa572aca2013-01-09 11:52:26 -0800308 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700309 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
310 return;
311 }
312
Miao Wangcc8cea72015-02-19 18:14:46 -0800313 if (y > 0 && y >= mHal.drvState.lod[0].dimY) {
314 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Y offset out of range.");
315 return;
316 }
317
318 if (z > 0 && z >= mHal.drvState.lod[0].dimZ) {
319 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Z offset out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700320 return;
321 }
322
Jason Samsbad80742011-03-16 16:29:28 -0700323 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Yang Nib8353c52015-02-14 18:00:59 -0800324 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700325 return;
326 }
327
Jason Samsbad80742011-03-16 16:29:28 -0700328 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Yang Nib8353c52015-02-14 18:00:59 -0800329 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800330 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700331 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
332 return;
333 }
334
Miao Wangcc8cea72015-02-19 18:14:46 -0800335 rsc->mHal.funcs.allocation.elementRead(rsc, this, x, y, z, data, cIdx, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700336}
337
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800338void Allocation::addProgramToDirty(const Program *p) {
Miao Wang82e135c2017-02-27 23:35:35 -0800339 mToDirtyList.push_back(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700340}
Jason Sams326e0dd2009-05-22 14:03:28 -0700341
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800342void Allocation::removeProgramToDirty(const Program *p) {
Yang Nib8353c52015-02-14 18:00:59 -0800343 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
344 if (mToDirtyList[ct] == p) {
Miao Wang82e135c2017-02-27 23:35:35 -0800345 mToDirtyList.erase(mToDirtyList.begin() + ct);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700346 return;
347 }
348 }
349 rsAssert(0);
350}
351
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800352void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800353 ObjectBase::dumpLOGV(prefix);
Jason Sams48ecf6a2013-07-09 15:35:29 -0700354 char buf[1024];
Jason Samsc21cf402009-11-17 17:26:46 -0800355
Jason Sams48ecf6a2013-07-09 15:35:29 -0700356 if ((strlen(prefix) + 10) < sizeof(buf)) {
George Burgess IV35fb3212016-03-02 13:55:48 -0800357 snprintf(buf, sizeof(buf), "%s type ", prefix);
Jason Sams48ecf6a2013-07-09 15:35:29 -0700358 if (mHal.state.type) {
359 mHal.state.type->dumpLOGV(buf);
360 }
Jason Samsc21cf402009-11-17 17:26:46 -0800361 }
Steve Block65982012011-10-20 11:56:00 +0100362 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Yang Nib8353c52015-02-14 18:00:59 -0800363 prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800364}
Jason Sams326e0dd2009-05-22 14:03:28 -0700365
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800366uint32_t Allocation::getPackedSize() const {
Jason Sams61656a72013-09-03 16:21:18 -0700367 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800368 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
369}
370
Jason Samse3150cf2012-07-24 18:10:20 -0700371void Allocation::writePackedData(Context *rsc, const Type *type,
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800372 uint8_t *dst, const uint8_t *src, bool dstPadded) {
373 const Element *elem = type->getElement();
374 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
375 uint32_t paddedBytes = elem->getSizeBytes();
Jason Sams61656a72013-09-03 16:21:18 -0700376 uint32_t numItems = type->getPackedSizeBytes() / paddedBytes;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800377
378 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
379 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
380
381 // no sub-elements
382 uint32_t fieldCount = elem->getFieldCount();
383 if (fieldCount == 0) {
384 for (uint32_t i = 0; i < numItems; i ++) {
385 memcpy(dst, src, unpaddedBytes);
386 src += srcInc;
387 dst += dstInc;
388 }
389 return;
390 }
391
392 // Cache offsets
393 uint32_t *offsetsPadded = new uint32_t[fieldCount];
394 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
395 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
396
397 for (uint32_t i = 0; i < fieldCount; i++) {
398 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
399 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
400 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
401 }
402
403 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
404 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
405
406 // complex elements, need to copy subelem after subelem
407 for (uint32_t i = 0; i < numItems; i ++) {
408 for (uint32_t fI = 0; fI < fieldCount; fI++) {
409 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
410 }
411 src += srcInc;
412 dst += dstInc;
413 }
414
415 delete[] offsetsPadded;
416 delete[] offsetsUnpadded;
417 delete[] sizeUnpadded;
418}
419
Jason Samse3150cf2012-07-24 18:10:20 -0700420void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800421 const uint8_t *src = (const uint8_t*)data;
Jason Sams61a4bb72012-07-25 19:33:43 -0700422 uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800423
Jason Samse3150cf2012-07-24 18:10:20 -0700424 writePackedData(rsc, getType(), dst, src, true);
Jason Sams61a4bb72012-07-25 19:33:43 -0700425 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800426}
427
Jason Samse3150cf2012-07-24 18:10:20 -0700428void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800429 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
Jason Sams61656a72013-09-03 16:21:18 -0700430 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800431
Jason Sams61a4bb72012-07-25 19:33:43 -0700432 const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800433 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
434
Jason Samse3150cf2012-07-24 18:10:20 -0700435 writePackedData(rsc, getType(), dst, src, false);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800436 stream->addByteArray(dst, getPackedSize());
437
438 delete[] dst;
Jason Sams61a4bb72012-07-25 19:33:43 -0700439 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800440}
441
Jason Samse3150cf2012-07-24 18:10:20 -0700442void Allocation::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700443 // Need to identify ourselves
444 stream->addU32((uint32_t)getClassId());
Jason Sams48ecf6a2013-07-09 15:35:29 -0700445 stream->addString(getName());
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700446
447 // First thing we need to serialize is the type object since it will be needed
448 // to initialize the class
Jason Samse3150cf2012-07-24 18:10:20 -0700449 mHal.state.type->serialize(rsc, stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700450
Jason Sams61656a72013-09-03 16:21:18 -0700451 uint32_t dataSize = mHal.state.type->getPackedSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800452 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
453 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700454 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800455 stream->addU32(packedSize);
456 if (dataSize == packedSize) {
457 // Now write the data
Jason Sams61a4bb72012-07-25 19:33:43 -0700458 stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize);
459 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800460 } else {
461 // Now write the data
Jason Samse3150cf2012-07-24 18:10:20 -0700462 packVec3Allocation(rsc, stream);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800463 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700464}
465
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800466Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700467 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700468 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800469 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Jason Samsa2737932014-01-14 12:28:33 -0800470 rsc->setError(RS_ERROR_FATAL_DRIVER,
471 "allocation loading failed due to corrupt file. (invalid id)\n");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700472 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700473 }
474
Jason Sams48ecf6a2013-07-09 15:35:29 -0700475 const char *name = stream->loadString();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700476
477 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800478 if (!type) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700479 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700480 }
481 type->compute();
482
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800483 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
484 type->decUserRef();
485
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700486 // Number of bytes we wrote out for this allocation
487 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800488 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
489 uint32_t packedSize = alloc->getPackedSize();
Jason Sams61656a72013-09-03 16:21:18 -0700490 if (dataSize != type->getPackedSizeBytes() &&
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800491 dataSize != packedSize) {
Jason Samsa2737932014-01-14 12:28:33 -0800492 rsc->setError(RS_ERROR_FATAL_DRIVER,
493 "allocation loading failed due to corrupt file. (invalid size)\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800494 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700495 ObjectBase::checkDelete(type);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700496 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700497 }
498
Jason Sams48ecf6a2013-07-09 15:35:29 -0700499 alloc->assignName(name);
Jason Sams61656a72013-09-03 16:21:18 -0700500 if (dataSize == type->getPackedSizeBytes()) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800501 uint32_t count = dataSize / type->getElementSizeBytes();
502 // Read in all of our allocation data
503 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
504 } else {
Jason Samse3150cf2012-07-24 18:10:20 -0700505 alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800506 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700507 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700508
509 return alloc;
510}
511
Jason Samseb4fe182011-05-26 16:33:01 -0700512void Allocation::sendDirty(const Context *rsc) const {
Jason Sams93eacc72012-12-18 14:26:57 -0800513#ifndef RS_COMPATIBILITY_LIB
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700514 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
515 mToDirtyList[ct]->forceDirty();
516 }
Jason Sams93eacc72012-12-18 14:26:57 -0800517#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700518 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700519}
Jason Sams326e0dd2009-05-22 14:03:28 -0700520
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800521void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700522 mHal.state.type->incRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700523}
524
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800525void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700526 if (!mHal.state.hasReferences || !getIsScript()) {
527 return;
528 }
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700529 mHal.state.type->decRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700530}
531
Jason Samsa36c50a2014-06-17 12:06:06 -0700532void Allocation::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700533 if (rsc->mHal.funcs.allocation.updateCachedObject != nullptr) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700534 rsc->mHal.funcs.allocation.updateCachedObject(rsc, this, (rs_allocation *)dstObj);
535 } else {
536 *((const void **)dstObj) = this;
537 }
538}
539
540
Jason Samsc7cec1e2011-08-18 18:01:33 -0700541void Allocation::freeChildrenUnlocked () {
Jason Sams61a4bb72012-07-25 19:33:43 -0700542 void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this);
Jason Sams61656a72013-09-03 16:21:18 -0700543 decRefs(ptr, mHal.state.type->getCellCount(), 0);
Jason Sams61a4bb72012-07-25 19:33:43 -0700544 mRSC->mHal.funcs.allocation.unlock1D(mRSC, this);
Jason Samsc7cec1e2011-08-18 18:01:33 -0700545}
546
547bool Allocation::freeChildren() {
548 if (mHal.state.hasReferences) {
549 incSysRef();
550 freeChildrenUnlocked();
551 return decSysRef();
552 }
553 return false;
554}
555
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800556void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700557}
558
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800559void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsa572aca2013-01-09 11:52:26 -0800560 uint32_t oldDimX = mHal.drvState.lod[0].dimX;
Jason Sams96abf812010-10-05 13:32:49 -0700561 if (dimX == oldDimX) {
562 return;
563 }
564
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700565 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700566 if (dimX < oldDimX) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700567 decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX);
568 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Jason Sams96abf812010-10-05 13:32:49 -0700569 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700570 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700571 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700572 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700573}
574
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800575void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Samsa2737932014-01-14 12:28:33 -0800576 rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700577}
578
Miao Wang75474682015-10-26 16:50:13 -0700579void Allocation::setupGrallocConsumer(const Context *rsc, uint32_t numAlloc) {
580#ifndef RS_COMPATIBILITY_LIB
581 // Configure GrallocConsumer to be in asynchronous mode
582 if (numAlloc > MAX_NUM_ALLOC || numAlloc <= 0) {
583 rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented");
584 return;
585 }
Miao Wangf750c532017-03-03 16:03:44 -0800586 mGrallocConsumer = new GrallocConsumer(rsc, this, numAlloc);
Miao Wang75474682015-10-26 16:50:13 -0700587 mCurrentIdx = 0;
588 mBufferQueueInited = true;
Miao Wang75474682015-10-26 16:50:13 -0700589#endif
590}
591
Jason Sams733396b2013-02-22 12:46:18 -0800592void * Allocation::getSurface(const Context *rsc) {
Jason Samsddceab92013-08-07 13:02:32 -0700593#ifndef RS_COMPATIBILITY_LIB
594 // Configure GrallocConsumer to be in asynchronous mode
Miao Wang75474682015-10-26 16:50:13 -0700595 if (!mBufferQueueInited) {
596 // This case is only used for single frame processing,
597 // since we will always call setupGrallocConsumer first in
598 // multi-frame case.
599 setupGrallocConsumer(rsc, 1);
600 }
Miao Wangf750c532017-03-03 16:03:44 -0800601 return mGrallocConsumer->getNativeWindow();
Jason Samsddceab92013-08-07 13:02:32 -0700602#else
Chris Wailes44bef6f2014-08-12 13:51:10 -0700603 return nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700604#endif
Jason Sams41e373d2012-01-13 14:01:20 -0800605}
606
Miao Wang75474682015-10-26 16:50:13 -0700607void Allocation::shareBufferQueue(const Context *rsc, const Allocation *alloc) {
608#ifndef RS_COMPATIBILITY_LIB
609 mGrallocConsumer = alloc->mGrallocConsumer;
610 mCurrentIdx = mGrallocConsumer->getNextAvailableIdx(this);
611 if (mCurrentIdx >= mGrallocConsumer->mNumAlloc) {
612 rsc->setError(RS_ERROR_DRIVER, "Maximum allocations attached to a BufferQueue");
613 return;
614 }
Miao Wang75474682015-10-26 16:50:13 -0700615 mBufferQueueInited = true;
616#endif
617}
618
619
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800620void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
621 ANativeWindow *nw = (ANativeWindow *)sur;
Jason Sams733396b2013-02-22 12:46:18 -0800622 rsc->mHal.funcs.allocation.setSurface(rsc, this, nw);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800623}
624
625void Allocation::ioSend(const Context *rsc) {
626 rsc->mHal.funcs.allocation.ioSend(rsc, this);
627}
628
629void Allocation::ioReceive(const Context *rsc) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700630 void *ptr = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700631 size_t stride = 0;
632#ifndef RS_COMPATIBILITY_LIB
633 if (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
Miao Wangf750c532017-03-03 16:03:44 -0800634 media_status_t ret = mGrallocConsumer->lockNextBuffer(mCurrentIdx);
Jason Samsddceab92013-08-07 13:02:32 -0700635
Miao Wangf750c532017-03-03 16:03:44 -0800636 if (ret == AMEDIA_OK) {
Jason Samsddceab92013-08-07 13:02:32 -0700637 rsc->mHal.funcs.allocation.ioReceive(rsc, this);
Miao Wangf750c532017-03-03 16:03:44 -0800638 } else if (ret == AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE) {
Jason Samsddceab92013-08-07 13:02:32 -0700639 // No new frame, don't do anything
640 } else {
641 rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer.");
642 }
643
644 }
645#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800646}
647
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700648bool Allocation::hasSameDims(const Allocation *other) const {
649 const Type *type0 = this->getType(),
650 *type1 = other->getType();
651
652 return (type0->getCellCount() == type1->getCellCount()) &&
653 (type0->getDimLOD() == type1->getDimLOD()) &&
654 (type0->getDimFaces() == type1->getDimFaces()) &&
655 (type0->getDimYuv() == type1->getDimYuv()) &&
656 (type0->getDimX() == type1->getDimX()) &&
657 (type0->getDimY() == type1->getDimY()) &&
658 (type0->getDimZ() == type1->getDimZ());
659}
660
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800661
Jason Sams326e0dd2009-05-22 14:03:28 -0700662/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700663//
Stephen Hines6a121812011-03-01 17:34:59 -0800664
Jason Sams366c9c82010-12-08 16:14:36 -0800665void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
666 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700667 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800668 a->syncAll(rsc, src);
669}
670
Jason Samsa2371512011-01-12 13:28:37 -0800671void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700672 Allocation *alloc = static_cast<Allocation *>(va);
673 rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc);
Jason Sams837e3882010-12-10 16:03:15 -0800674}
675
Jason Sams807fdc42012-07-25 17:55:39 -0700676void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
677 Allocation *a = static_cast<Allocation *>(va);
678 const Type * t = a->getType();
679 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700680 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700681}
682
Jason Sams4b45b892010-12-29 14:31:29 -0800683void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700684 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700685 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800686 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700687}
688
Miao Wangcc8cea72015-02-19 18:14:46 -0800689void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x,
690 uint32_t lod, const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700691 Allocation *a = static_cast<Allocation *>(va);
Miao Wangcc8cea72015-02-19 18:14:46 -0800692 a->elementData(rsc, x, 0, 0, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700693}
694
Miao Wangcc8cea72015-02-19 18:14:46 -0800695void rsi_AllocationElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
696 uint32_t lod, const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700697 Allocation *a = static_cast<Allocation *>(va);
Miao Wangcc8cea72015-02-19 18:14:46 -0800698 a->elementData(rsc, x, y, z, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700699}
700
Jason Sams4b45b892010-12-29 14:31:29 -0800701void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800702 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700703 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800704 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams326e0dd2009-05-22 14:03:28 -0700705}
706
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700707void rsi_Allocation3DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
708 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
709 Allocation *a = static_cast<Allocation *>(va);
710 a->data(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
711}
712
713
Jason Sams807fdc42012-07-25 17:55:39 -0700714void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
Jason Samse579df42009-08-10 14:55:26 -0700715 Allocation *a = static_cast<Allocation *>(va);
Jason Sams807fdc42012-07-25 17:55:39 -0700716 const Type * t = a->getType();
Miao Wangcc8cea72015-02-19 18:14:46 -0800717 if(t->getDimZ()) {
718 a->read(rsc, 0, 0, 0, 0, t->getDimX(), t->getDimY(), t->getDimZ(),
719 data, sizeBytes, 0);
720 } else if(t->getDimY()) {
Jason Sams807fdc42012-07-25 17:55:39 -0700721 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700722 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Jason Sams807fdc42012-07-25 17:55:39 -0700723 } else {
724 a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes);
725 }
726
Jason Samse579df42009-08-10 14:55:26 -0700727}
728
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800729void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700730 Allocation *a = static_cast<Allocation *>(va);
731 a->resize1D(rsc, dimX);
732}
733
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800734void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700735 Allocation *a = static_cast<Allocation *>(va);
736 a->resize2D(rsc, dimX, dimY);
737}
738
Jason Samsc975cf42011-04-28 18:26:48 -0700739RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800740 RsAllocationMipmapControl mipmaps,
Yang Ni79c51de2016-03-08 21:01:45 +0000741 uint32_t usages, uintptr_t ptr) {
Stephen Hines8f615d62013-12-20 12:23:32 -0800742 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mipmaps, (void*)ptr);
Yang Ni79c51de2016-03-08 21:01:45 +0000743 if (!alloc) {
744 return nullptr;
Jason Samseb4fe182011-05-26 16:33:01 -0700745 }
Yang Ni79c51de2016-03-08 21:01:45 +0000746 alloc->incUserRef();
Jason Samsf0c1df42010-10-26 13:09:17 -0700747 return alloc;
748}
749
Miao Wang47a58812015-07-23 21:59:16 -0700750RsAllocation rsi_AllocationCreateStrided(Context *rsc, RsType vtype,
751 RsAllocationMipmapControl mipmaps,
752 uint32_t usages, uintptr_t ptr,
753 size_t requiredAlignment) {
754 Allocation * alloc = Allocation::createAllocationStrided(rsc, static_cast<Type *>(vtype), usages, mipmaps,
755 (void*)ptr, requiredAlignment);
756 if (!alloc) {
757 return nullptr;
758 }
759 alloc->incUserRef();
760 return alloc;
761}
762
Jason Samsc975cf42011-04-28 18:26:48 -0700763RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800764 RsAllocationMipmapControl mipmaps,
Yang Ni79c51de2016-03-08 21:01:45 +0000765 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800766 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700767
Yang Ni79c51de2016-03-08 21:01:45 +0000768 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700769 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700770 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000771 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700772 return nullptr;
Jason Samsf0c1df42010-10-26 13:09:17 -0700773 }
774
Jason Sams807fdc42012-07-25 17:55:39 -0700775 texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray60c27962013-02-07 16:15:23 -0800776 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Stephen Hines8f615d62013-12-20 12:23:32 -0800777 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700778 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700779 }
780
Jason Samseb4fe182011-05-26 16:33:01 -0700781 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700782 return texAlloc;
783}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800784
Jason Samsc975cf42011-04-28 18:26:48 -0700785RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800786 RsAllocationMipmapControl mipmaps,
Yang Ni79c51de2016-03-08 21:01:45 +0000787 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800788 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800789
790 // Cubemap allocation's faces should be Width by Width each.
791 // Source data should have 6 * Width by Width pixels
792 // Error checking is done in the java layer
Yang Ni79c51de2016-03-08 21:01:45 +0000793 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800794 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700795 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000796 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700797 return nullptr;
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800798 }
799
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800800 uint32_t faceSize = t->getDimX();
801 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
802 uint32_t copySize = faceSize * t->getElementSizeBytes();
803
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800804 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800805 for (uint32_t face = 0; face < 6; face ++) {
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800806 for (uint32_t dI = 0; dI < faceSize; dI ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700807 texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face,
Tim Murray60c27962013-02-07 16:15:23 -0800808 t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize, 0);
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800809 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800810
Jason Sams366c9c82010-12-08 16:14:36 -0800811 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800812 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800813 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800814
Stephen Hines8f615d62013-12-20 12:23:32 -0800815 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700816 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800817 }
818
Jason Samseb4fe182011-05-26 16:33:01 -0700819 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800820 return texAlloc;
821}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800822
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700823void rsi_AllocationCopy2DRange(Context *rsc,
824 RsAllocation dstAlloc,
825 uint32_t dstXoff, uint32_t dstYoff,
826 uint32_t dstMip, uint32_t dstFace,
827 uint32_t width, uint32_t height,
828 RsAllocation srcAlloc,
829 uint32_t srcXoff, uint32_t srcYoff,
830 uint32_t srcMip, uint32_t srcFace) {
831 Allocation *dst = static_cast<Allocation *>(dstAlloc);
832 Allocation *src= static_cast<Allocation *>(srcAlloc);
833 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
834 (RsAllocationCubemapFace)dstFace,
835 width, height,
836 src, srcXoff, srcYoff,srcMip,
837 (RsAllocationCubemapFace)srcFace);
838}
839
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700840void rsi_AllocationCopy3DRange(Context *rsc,
841 RsAllocation dstAlloc,
842 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
843 uint32_t dstMip,
844 uint32_t width, uint32_t height, uint32_t depth,
845 RsAllocation srcAlloc,
846 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
847 uint32_t srcMip) {
848 Allocation *dst = static_cast<Allocation *>(dstAlloc);
849 Allocation *src= static_cast<Allocation *>(srcAlloc);
850 rsc->mHal.funcs.allocation.allocData3D(rsc, dst, dstXoff, dstYoff, dstZoff, dstMip,
851 width, height, depth,
852 src, srcXoff, srcYoff, srcZoff, srcMip);
853}
854
Miao Wang75474682015-10-26 16:50:13 -0700855void rsi_AllocationSetupBufferQueue(Context *rsc, RsAllocation valloc, uint32_t numAlloc) {
856 Allocation *alloc = static_cast<Allocation *>(valloc);
857 alloc->setupGrallocConsumer(rsc, numAlloc);
858}
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700859
Jason Sams733396b2013-02-22 12:46:18 -0800860void * rsi_AllocationGetSurface(Context *rsc, RsAllocation valloc) {
Jason Sams41e373d2012-01-13 14:01:20 -0800861 Allocation *alloc = static_cast<Allocation *>(valloc);
Jason Sams733396b2013-02-22 12:46:18 -0800862 void *s = alloc->getSurface(rsc);
863 return s;
Jason Sams3522f402012-03-23 11:47:26 -0700864}
865
Miao Wang75474682015-10-26 16:50:13 -0700866void rsi_AllocationShareBufferQueue(Context *rsc, RsAllocation valloc1, RsAllocation valloc2) {
867 Allocation *alloc1 = static_cast<Allocation *>(valloc1);
868 Allocation *alloc2 = static_cast<Allocation *>(valloc2);
869 alloc1->shareBufferQueue(rsc, alloc2);
870}
871
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800872void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
873 Allocation *alloc = static_cast<Allocation *>(valloc);
874 alloc->setSurface(rsc, sur);
875}
876
877void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
878 Allocation *alloc = static_cast<Allocation *>(valloc);
879 alloc->ioSend(rsc);
880}
881
Miao Wang75474682015-10-26 16:50:13 -0700882int64_t rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800883 Allocation *alloc = static_cast<Allocation *>(valloc);
884 alloc->ioReceive(rsc);
Miao Wang75474682015-10-26 16:50:13 -0700885 return alloc->getTimeStamp();
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800886}
887
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700888void *rsi_AllocationGetPointer(Context *rsc, RsAllocation valloc,
Jason Samsb8a94e22014-02-24 17:52:32 -0800889 uint32_t lod, RsAllocationCubemapFace face,
890 uint32_t z, uint32_t array, size_t *stride, size_t strideLen) {
891 Allocation *alloc = static_cast<Allocation *>(valloc);
Tim Murray0e61af92014-02-26 13:28:52 -0800892 rsAssert(strideLen == sizeof(size_t));
Jason Samsb8a94e22014-02-24 17:52:32 -0800893
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700894 return alloc->getPointer(rsc, lod, face, z, array, stride);
Jason Samsb8a94e22014-02-24 17:52:32 -0800895}
896
Tim Murray509ea5c2012-11-13 11:56:40 -0800897void rsi_Allocation1DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
898 uint32_t count, void *data, size_t sizeBytes) {
899 Allocation *a = static_cast<Allocation *>(va);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700900 rsc->mHal.funcs.allocation.read1D(rsc, a, xoff, lod, count, data, sizeBytes);
Tim Murray509ea5c2012-11-13 11:56:40 -0800901}
902
Miao Wangcc8cea72015-02-19 18:14:46 -0800903void rsi_AllocationElementRead(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
904 uint32_t lod, void *data, size_t sizeBytes, size_t eoff) {
905 Allocation *a = static_cast<Allocation *>(va);
906 a->elementRead(rsc, x, y, z, data, eoff, sizeBytes);
907}
908
Tim Murray7b3e3092012-11-16 13:32:24 -0800909void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff,
910 uint32_t lod, RsAllocationCubemapFace face, uint32_t w,
Tim Murray358747a2012-11-26 13:52:04 -0800911 uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Tim Murray7b3e3092012-11-16 13:32:24 -0800912 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800913 a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Tim Murray7b3e3092012-11-16 13:32:24 -0800914}
915
Miao Wangcc8cea72015-02-19 18:14:46 -0800916void rsi_Allocation3DRead(Context *rsc, RsAllocation va,
917 uint32_t xoff, uint32_t yoff, uint32_t zoff,
918 uint32_t lod, uint32_t w, uint32_t h, uint32_t d,
919 void *data, size_t sizeBytes, size_t stride) {
920 Allocation *a = static_cast<Allocation *>(va);
921 a->read(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
922}
923
Jason Samscfea6c12015-02-09 12:50:22 -0800924RsAllocation rsi_AllocationAdapterCreate(Context *rsc, RsType vwindow, RsAllocation vbase) {
925
926
927 Allocation * alloc = Allocation::createAdapter(rsc,
928 static_cast<Allocation *>(vbase), static_cast<Type *>(vwindow));
929 if (!alloc) {
930 return nullptr;
931 }
932 alloc->incUserRef();
933 return alloc;
934}
935
936void rsi_AllocationAdapterOffset(Context *rsc, RsAllocation va, const uint32_t *offsets, size_t len) {
Jason Sams442b7ff2015-03-06 16:50:11 -0800937 Allocation *a = static_cast<Allocation *>(va);
938 a->adapterOffset(rsc, offsets, len);
Jason Samscfea6c12015-02-09 12:50:22 -0800939}
940
941
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -0800942} // namespace renderscript
943} // namespace android