blob: a907749c3ea830d72a6acdd542c4cd5f1c7c618b [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
Stephen Hinesb0934b62013-07-03 17:27:38 -070021#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
Jason Sams7ac2a4d2012-02-15 12:04:24 -080022#include "system/window.h"
Andy McFadden58fd6a52012-12-18 09:50:03 -080023#include "gui/GLConsumer.h"
Tim Murray0b575de2013-03-15 15:56:43 -070024#endif
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070025
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -080026namespace android {
27namespace renderscript {
Jason Sams326e0dd2009-05-22 14:03:28 -070028
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080029Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080030 RsAllocationMipmapControl mc, void * ptr)
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080031 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080032
Jason Samseb4fe182011-05-26 16:33:01 -070033 memset(&mHal, 0, sizeof(mHal));
34 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsbad80742011-03-16 16:29:28 -070035 mHal.state.usageFlags = usages;
36 mHal.state.mipmapControl = mc;
Tim Murray2e1a94d2012-11-29 13:12:25 -080037 mHal.state.userProvidedPtr = ptr;
Jason Sams366c9c82010-12-08 16:14:36 -080038
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070039 setType(type);
Jason Samsbad80742011-03-16 16:29:28 -070040 updateCache();
41}
Jason Samsfa84da22010-03-01 15:31:04 -080042
Jason Samscfea6c12015-02-09 12:50:22 -080043Allocation::Allocation(Context *rsc, const Allocation *alloc, const Type *type)
44 : ObjectBase(rsc) {
45
46 memset(&mHal, 0, sizeof(mHal));
Jason Samscfea6c12015-02-09 12:50:22 -080047 mHal.state.baseAlloc = alloc;
Jason Samscfea6c12015-02-09 12:50:22 -080048 mHal.state.usageFlags = alloc->mHal.state.usageFlags;
49 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
50
51 setType(type);
52 updateCache();
Jason Samscfea6c12015-02-09 12:50:22 -080053}
54
Tim Murray34689382013-03-11 12:12:03 -070055void Allocation::operator delete(void* ptr) {
56 if (ptr) {
57 Allocation *a = (Allocation*) ptr;
58 a->getContext()->mHal.funcs.freeRuntimeMem(ptr);
59 }
60}
61
Miao Wang47a58812015-07-23 21:59:16 -070062Allocation * Allocation::createAllocationStrided(Context *rsc, const Type *type, uint32_t usages,
63 RsAllocationMipmapControl mc, void * ptr,
64 size_t requiredAlignment) {
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
Jason Samsf82b6262015-05-11 15:02:50 -070073 bool success = false;
74 Allocation *a = nullptr;
75 if (usages & RS_ALLOCATION_USAGE_OEM) {
76 if (rsc->mHal.funcs.allocation.initOem != nullptr) {
77 a = new (allocMem) Allocation(rsc, type, usages, mc, nullptr);
78 success = rsc->mHal.funcs.allocation.initOem(rsc, a, type->getElement()->getHasReferences(), ptr);
79 } else {
80 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation Init called with USAGE_OEM but driver does not support it");
81 return nullptr;
82 }
Miao Wang47a58812015-07-23 21:59:16 -070083#ifdef RS_COMPATIBILITY_LIB
84 } else if (usages & RS_ALLOCATION_USAGE_INCREMENTAL_SUPPORT){
85 a = new (allocMem) Allocation(rsc, type, usages, mc, ptr);
86 success = rsc->mHal.funcs.allocation.initStrided(rsc, a, type->getElement()->getHasReferences(), requiredAlignment);
87#endif
Jason Samsf82b6262015-05-11 15:02:50 -070088 } else {
89 a = new (allocMem) Allocation(rsc, type, usages, mc, ptr);
90 success = rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences());
91 }
Jason Samseb4fe182011-05-26 16:33:01 -070092
Jason Samsf82b6262015-05-11 15:02:50 -070093 if (!success) {
Jason Samseb4fe182011-05-26 16:33:01 -070094 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
95 delete a;
Chris Wailes44bef6f2014-08-12 13:51:10 -070096 return nullptr;
Jason Samseb4fe182011-05-26 16:33:01 -070097 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -080098
Jason Samseb4fe182011-05-26 16:33:01 -070099 return a;
100}
101
Miao Wang47a58812015-07-23 21:59:16 -0700102Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
103 RsAllocationMipmapControl mc, void * ptr) {
104 return Allocation::createAllocationStrided(rsc, type, usages, mc, ptr, kMinimumRSAlignment);
105}
106
Jason Samscfea6c12015-02-09 12:50:22 -0800107Allocation * Allocation::createAdapter(Context *rsc, const Allocation *alloc, const Type *type) {
108 // Allocation objects must use allocator specified by the driver
109 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Allocation), 0);
110
111 if (!allocMem) {
112 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation");
113 return nullptr;
114 }
115
116 Allocation *a = new (allocMem) Allocation(rsc, alloc, type);
117
118 if (!rsc->mHal.funcs.allocation.initAdapter(rsc, a)) {
119 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
120 delete a;
121 return nullptr;
122 }
123
124 return a;
125}
126
Jason Sams442b7ff2015-03-06 16:50:11 -0800127void Allocation::adapterOffset(Context *rsc, const uint32_t *offsets, size_t len) {
128 if (len >= sizeof(uint32_t) * 9) {
129 mHal.state.originX = offsets[0];
130 mHal.state.originY = offsets[1];
131 mHal.state.originZ = offsets[2];
132 mHal.state.originLOD = offsets[3];
133 mHal.state.originFace = offsets[4];
134 mHal.state.originArray[0] = offsets[5];
135 mHal.state.originArray[1] = offsets[6];
136 mHal.state.originArray[2] = offsets[7];
137 mHal.state.originArray[3] = offsets[8];
138 }
139
140 rsc->mHal.funcs.allocation.adapterOffset(rsc, this);
141}
142
143
Jason Samscfea6c12015-02-09 12:50:22 -0800144
Jason Samsbad80742011-03-16 16:29:28 -0700145void Allocation::updateCache() {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700146 const Type *type = mHal.state.type;
Jason Samsa572aca2013-01-09 11:52:26 -0800147 mHal.state.yuv = type->getDimYuv();
Jason Samsbad80742011-03-16 16:29:28 -0700148 mHal.state.hasFaces = type->getDimFaces();
149 mHal.state.hasMipmaps = type->getDimLOD();
150 mHal.state.elementSizeBytes = type->getElementSizeBytes();
151 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -0700152}
153
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800154Allocation::~Allocation() {
Jason Samsddceab92013-08-07 13:02:32 -0700155#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
156 if (mGrallocConsumer.get()) {
Miao Wang75474682015-10-26 16:50:13 -0700157 mGrallocConsumer->releaseIdx(mCurrentIdx);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700158 mGrallocConsumer = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700159 }
160#endif
161
Jason Samsc7cec1e2011-08-18 18:01:33 -0700162 freeChildrenUnlocked();
Jason Samseb4fe182011-05-26 16:33:01 -0700163 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700164}
165
Jason Sams366c9c82010-12-08 16:14:36 -0800166void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -0700167 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -0800168}
169
Jason Samsb8a94e22014-02-24 17:52:32 -0800170void * Allocation::getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face,
171 uint32_t z, uint32_t array, size_t *stride) {
172
173 if ((lod >= mHal.drvState.lodCount) ||
174 (z && (z >= mHal.drvState.lod[lod].dimZ)) ||
175 ((face != RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X) && !mHal.state.hasFaces) ||
176 (array != 0)) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700177 return nullptr;
Jason Samsb8a94e22014-02-24 17:52:32 -0800178 }
179
Jason Sams8ce12812015-05-18 17:28:59 -0700180 if (mRSC->mHal.funcs.allocation.getPointer != nullptr) {
181 // Notify the driver, if present that the user is mapping the buffer
182 mRSC->mHal.funcs.allocation.getPointer(rsc, this, lod, face, z, array);
183 }
184
Jason Samsb8a94e22014-02-24 17:52:32 -0800185 size_t s = 0;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700186 if ((stride != nullptr) && mHal.drvState.lod[0].dimY) {
Jason Samsb8a94e22014-02-24 17:52:32 -0800187 *stride = mHal.drvState.lod[lod].stride;
188 }
189 return mHal.drvState.lod[lod].mallocPtr;
190}
191
Jason Sams4b45b892010-12-29 14:31:29 -0800192void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800193 uint32_t count, const void *data, size_t sizeBytes) {
194 const size_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -0700195
Jason Samseb4fe182011-05-26 16:33:01 -0700196 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800197 char buf[1024];
George Burgess IV35fb3212016-03-02 13:55:48 -0800198 snprintf(buf, sizeof(buf),
199 "Allocation::subData called with mismatched size expected %zu, got %zu",
200 (count * eSize), sizeBytes);
Jason Samsa2737932014-01-14 12:28:33 -0800201 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Samsbad80742011-03-16 16:29:28 -0700202 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700203 return;
204 }
Jason Samse3929c92010-08-09 18:13:33 -0700205
Jason Samseb4fe182011-05-26 16:33:01 -0700206 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
207 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700208}
209
Jason Sams4b45b892010-12-29 14:31:29 -0800210void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800211 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Tim Murray358747a2012-11-26 13:52:04 -0800212 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Samseb4fe182011-05-26 16:33:01 -0700213 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700214}
215
Jason Sams236385b2011-01-12 14:53:25 -0800216void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700217 uint32_t lod,
218 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
219 rsc->mHal.funcs.allocation.data3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
220 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700221}
222
Jason Sams807fdc42012-07-25 17:55:39 -0700223void Allocation::read(Context *rsc, uint32_t xoff, uint32_t lod,
Tim Murray358747a2012-11-26 13:52:04 -0800224 uint32_t count, void *data, size_t sizeBytes) {
Jason Sams807fdc42012-07-25 17:55:39 -0700225 const size_t eSize = mHal.state.type->getElementSizeBytes();
226
227 if ((count * eSize) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800228 char buf[1024];
George Burgess IV35fb3212016-03-02 13:55:48 -0800229 snprintf(buf, sizeof(buf),
230 "Allocation::read called with mismatched size expected %zu, got %zu",
231 (count * eSize), sizeBytes);
Jason Samsa2737932014-01-14 12:28:33 -0800232 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams807fdc42012-07-25 17:55:39 -0700233 mHal.state.type->dumpLOGV("type info");
234 return;
235 }
236
237 rsc->mHal.funcs.allocation.read1D(rsc, this, xoff, lod, count, data, sizeBytes);
238}
239
Tim Murray358747a2012-11-26 13:52:04 -0800240void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
241 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
242 const size_t eSize = mHal.state.elementSizeBytes;
243 const size_t lineSize = eSize * w;
244 if (!stride) {
245 stride = lineSize;
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700246 } else {
247 if ((lineSize * h) != sizeBytes) {
Jason Samsa2737932014-01-14 12:28:33 -0800248 char buf[1024];
George Burgess IV35fb3212016-03-02 13:55:48 -0800249 snprintf(buf, sizeof(buf), "Allocation size mismatch, expected %zu, got %zu",
250 (lineSize * h), sizeBytes);
Jason Samsa2737932014-01-14 12:28:33 -0800251 rsc->setError(RS_ERROR_BAD_VALUE, buf);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700252 return;
253 }
Tim Murray358747a2012-11-26 13:52:04 -0800254 }
255
256 rsc->mHal.funcs.allocation.read2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams807fdc42012-07-25 17:55:39 -0700257}
258
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700259void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
260 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride) {
261 const size_t eSize = mHal.state.elementSizeBytes;
262 const size_t lineSize = eSize * w;
263 if (!stride) {
264 stride = lineSize;
265 }
266
267 rsc->mHal.funcs.allocation.read3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
268
Jason Sams807fdc42012-07-25 17:55:39 -0700269}
270
Miao Wangcc8cea72015-02-19 18:14:46 -0800271void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
272 const void *data, uint32_t cIdx, size_t sizeBytes) {
Jason Samsa572aca2013-01-09 11:52:26 -0800273 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700274 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
275 return;
276 }
277
Miao Wangcc8cea72015-02-19 18:14:46 -0800278 if (y > 0 && y >= mHal.drvState.lod[0].dimY) {
279 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Y offset out of range.");
280 return;
281 }
282
283 if (z > 0 && z >= mHal.drvState.lod[0].dimZ) {
284 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Z offset out of range.");
285 return;
286 }
287
288 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
289 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
290 return;
291 }
292
Jason Samsbad80742011-03-16 16:29:28 -0700293 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800294 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
295 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700296 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
297 return;
298 }
299
Miao Wangcc8cea72015-02-19 18:14:46 -0800300 rsc->mHal.funcs.allocation.elementData(rsc, this, x, y, z, data, cIdx, sizeBytes);
Jason Samseb4fe182011-05-26 16:33:01 -0700301 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700302}
303
Miao Wangcc8cea72015-02-19 18:14:46 -0800304void Allocation::elementRead(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
305 void *data, uint32_t cIdx, size_t sizeBytes) {
Jason Samsa572aca2013-01-09 11:52:26 -0800306 if (x >= mHal.drvState.lod[0].dimX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700307 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
308 return;
309 }
310
Miao Wangcc8cea72015-02-19 18:14:46 -0800311 if (y > 0 && y >= mHal.drvState.lod[0].dimY) {
312 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Y offset out of range.");
313 return;
314 }
315
316 if (z > 0 && z >= mHal.drvState.lod[0].dimZ) {
317 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData Z offset out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700318 return;
319 }
320
Jason Samsbad80742011-03-16 16:29:28 -0700321 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Yang Nib8353c52015-02-14 18:00:59 -0800322 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
Jason Sams5f0c84c2010-08-31 13:50:42 -0700323 return;
324 }
325
Jason Samsbad80742011-03-16 16:29:28 -0700326 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Yang Nib8353c52015-02-14 18:00:59 -0800327 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800328 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700329 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
330 return;
331 }
332
Miao Wangcc8cea72015-02-19 18:14:46 -0800333 rsc->mHal.funcs.allocation.elementRead(rsc, this, x, y, z, data, cIdx, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700334}
335
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800336void Allocation::addProgramToDirty(const Program *p) {
Yang Nib8353c52015-02-14 18:00:59 -0800337 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700338}
Jason Sams326e0dd2009-05-22 14:03:28 -0700339
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800340void Allocation::removeProgramToDirty(const Program *p) {
Yang Nib8353c52015-02-14 18:00:59 -0800341 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
342 if (mToDirtyList[ct] == p) {
343 mToDirtyList.removeAt(ct);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700344 return;
345 }
346 }
347 rsAssert(0);
348}
349
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800350void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800351 ObjectBase::dumpLOGV(prefix);
Jason Sams48ecf6a2013-07-09 15:35:29 -0700352 char buf[1024];
Jason Samsc21cf402009-11-17 17:26:46 -0800353
Jason Sams48ecf6a2013-07-09 15:35:29 -0700354 if ((strlen(prefix) + 10) < sizeof(buf)) {
George Burgess IV35fb3212016-03-02 13:55:48 -0800355 snprintf(buf, sizeof(buf), "%s type ", prefix);
Jason Sams48ecf6a2013-07-09 15:35:29 -0700356 if (mHal.state.type) {
357 mHal.state.type->dumpLOGV(buf);
358 }
Jason Samsc21cf402009-11-17 17:26:46 -0800359 }
Steve Block65982012011-10-20 11:56:00 +0100360 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Yang Nib8353c52015-02-14 18:00:59 -0800361 prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800362}
Jason Sams326e0dd2009-05-22 14:03:28 -0700363
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800364uint32_t Allocation::getPackedSize() const {
Jason Sams61656a72013-09-03 16:21:18 -0700365 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800366 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
367}
368
Jason Samse3150cf2012-07-24 18:10:20 -0700369void Allocation::writePackedData(Context *rsc, const Type *type,
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800370 uint8_t *dst, const uint8_t *src, bool dstPadded) {
371 const Element *elem = type->getElement();
372 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
373 uint32_t paddedBytes = elem->getSizeBytes();
Jason Sams61656a72013-09-03 16:21:18 -0700374 uint32_t numItems = type->getPackedSizeBytes() / paddedBytes;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800375
376 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
377 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
378
379 // no sub-elements
380 uint32_t fieldCount = elem->getFieldCount();
381 if (fieldCount == 0) {
382 for (uint32_t i = 0; i < numItems; i ++) {
383 memcpy(dst, src, unpaddedBytes);
384 src += srcInc;
385 dst += dstInc;
386 }
387 return;
388 }
389
390 // Cache offsets
391 uint32_t *offsetsPadded = new uint32_t[fieldCount];
392 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
393 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
394
395 for (uint32_t i = 0; i < fieldCount; i++) {
396 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
397 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
398 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
399 }
400
401 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
402 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
403
404 // complex elements, need to copy subelem after subelem
405 for (uint32_t i = 0; i < numItems; i ++) {
406 for (uint32_t fI = 0; fI < fieldCount; fI++) {
407 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
408 }
409 src += srcInc;
410 dst += dstInc;
411 }
412
413 delete[] offsetsPadded;
414 delete[] offsetsUnpadded;
415 delete[] sizeUnpadded;
416}
417
Jason Samse3150cf2012-07-24 18:10:20 -0700418void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800419 const uint8_t *src = (const uint8_t*)data;
Jason Sams61a4bb72012-07-25 19:33:43 -0700420 uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800421
Jason Samse3150cf2012-07-24 18:10:20 -0700422 writePackedData(rsc, getType(), dst, src, true);
Jason Sams61a4bb72012-07-25 19:33:43 -0700423 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800424}
425
Jason Samse3150cf2012-07-24 18:10:20 -0700426void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800427 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
Jason Sams61656a72013-09-03 16:21:18 -0700428 uint32_t numItems = mHal.state.type->getCellCount();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800429
Jason Sams61a4bb72012-07-25 19:33:43 -0700430 const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800431 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
432
Jason Samse3150cf2012-07-24 18:10:20 -0700433 writePackedData(rsc, getType(), dst, src, false);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800434 stream->addByteArray(dst, getPackedSize());
435
436 delete[] dst;
Jason Sams61a4bb72012-07-25 19:33:43 -0700437 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800438}
439
Jason Samse3150cf2012-07-24 18:10:20 -0700440void Allocation::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700441 // Need to identify ourselves
442 stream->addU32((uint32_t)getClassId());
Jason Sams48ecf6a2013-07-09 15:35:29 -0700443 stream->addString(getName());
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700444
445 // First thing we need to serialize is the type object since it will be needed
446 // to initialize the class
Jason Samse3150cf2012-07-24 18:10:20 -0700447 mHal.state.type->serialize(rsc, stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700448
Jason Sams61656a72013-09-03 16:21:18 -0700449 uint32_t dataSize = mHal.state.type->getPackedSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800450 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
451 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700452 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800453 stream->addU32(packedSize);
454 if (dataSize == packedSize) {
455 // Now write the data
Jason Sams61a4bb72012-07-25 19:33:43 -0700456 stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize);
457 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800458 } else {
459 // Now write the data
Jason Samse3150cf2012-07-24 18:10:20 -0700460 packVec3Allocation(rsc, stream);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800461 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700462}
463
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800464Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700465 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700466 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800467 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Jason Samsa2737932014-01-14 12:28:33 -0800468 rsc->setError(RS_ERROR_FATAL_DRIVER,
469 "allocation loading failed due to corrupt file. (invalid id)\n");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700470 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700471 }
472
Jason Sams48ecf6a2013-07-09 15:35:29 -0700473 const char *name = stream->loadString();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700474
475 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800476 if (!type) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700477 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700478 }
479 type->compute();
480
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800481 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
482 type->decUserRef();
483
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700484 // Number of bytes we wrote out for this allocation
485 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800486 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
487 uint32_t packedSize = alloc->getPackedSize();
Jason Sams61656a72013-09-03 16:21:18 -0700488 if (dataSize != type->getPackedSizeBytes() &&
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800489 dataSize != packedSize) {
Jason Samsa2737932014-01-14 12:28:33 -0800490 rsc->setError(RS_ERROR_FATAL_DRIVER,
491 "allocation loading failed due to corrupt file. (invalid size)\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800492 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700493 ObjectBase::checkDelete(type);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700494 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700495 }
496
Jason Sams48ecf6a2013-07-09 15:35:29 -0700497 alloc->assignName(name);
Jason Sams61656a72013-09-03 16:21:18 -0700498 if (dataSize == type->getPackedSizeBytes()) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800499 uint32_t count = dataSize / type->getElementSizeBytes();
500 // Read in all of our allocation data
501 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
502 } else {
Jason Samse3150cf2012-07-24 18:10:20 -0700503 alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800504 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700505 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700506
507 return alloc;
508}
509
Jason Samseb4fe182011-05-26 16:33:01 -0700510void Allocation::sendDirty(const Context *rsc) const {
Jason Sams93eacc72012-12-18 14:26:57 -0800511#ifndef RS_COMPATIBILITY_LIB
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700512 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
513 mToDirtyList[ct]->forceDirty();
514 }
Jason Sams93eacc72012-12-18 14:26:57 -0800515#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700516 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700517}
Jason Sams326e0dd2009-05-22 14:03:28 -0700518
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800519void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700520 mHal.state.type->incRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700521}
522
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800523void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700524 if (!mHal.state.hasReferences || !getIsScript()) {
525 return;
526 }
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700527 mHal.state.type->decRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700528}
529
Jason Samsa36c50a2014-06-17 12:06:06 -0700530void Allocation::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700531 if (rsc->mHal.funcs.allocation.updateCachedObject != nullptr) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700532 rsc->mHal.funcs.allocation.updateCachedObject(rsc, this, (rs_allocation *)dstObj);
533 } else {
534 *((const void **)dstObj) = this;
535 }
536}
537
538
Jason Samsc7cec1e2011-08-18 18:01:33 -0700539void Allocation::freeChildrenUnlocked () {
Jason Sams61a4bb72012-07-25 19:33:43 -0700540 void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this);
Jason Sams61656a72013-09-03 16:21:18 -0700541 decRefs(ptr, mHal.state.type->getCellCount(), 0);
Jason Sams61a4bb72012-07-25 19:33:43 -0700542 mRSC->mHal.funcs.allocation.unlock1D(mRSC, this);
Jason Samsc7cec1e2011-08-18 18:01:33 -0700543}
544
545bool Allocation::freeChildren() {
546 if (mHal.state.hasReferences) {
547 incSysRef();
548 freeChildrenUnlocked();
549 return decSysRef();
550 }
551 return false;
552}
553
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800554void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700555}
556
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800557void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsa572aca2013-01-09 11:52:26 -0800558 uint32_t oldDimX = mHal.drvState.lod[0].dimX;
Jason Sams96abf812010-10-05 13:32:49 -0700559 if (dimX == oldDimX) {
560 return;
561 }
562
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700563 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700564 if (dimX < oldDimX) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700565 decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX);
566 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Jason Sams96abf812010-10-05 13:32:49 -0700567 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700568 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700569 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700570 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700571}
572
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800573void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Samsa2737932014-01-14 12:28:33 -0800574 rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700575}
576
Jason Samsddceab92013-08-07 13:02:32 -0700577#ifndef RS_COMPATIBILITY_LIB
Miao Wang75474682015-10-26 16:50:13 -0700578Allocation::NewBufferListener::NewBufferListener(uint32_t numAlloc) {
579 alloc = new const Allocation *[numAlloc];
580 mNumAlloc = numAlloc;
581 for (uint32_t i = 0; i < numAlloc; i++) {
582 alloc[i] = nullptr;
583 }
584}
585
586Allocation::NewBufferListener::~NewBufferListener() {
587 delete[] alloc;
588}
589
Dan Stozaf0d7aa22014-11-04 11:38:47 -0800590void Allocation::NewBufferListener::onFrameAvailable(const BufferItem& /* item */) {
Miao Wang75474682015-10-26 16:50:13 -0700591 for (uint32_t i = 0; i < mNumAlloc; i++) {
592 if (alloc[i] != nullptr) {
593 intptr_t ip = (intptr_t)alloc[i];
594 rsc->sendMessageToClient(&ip, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, 0, sizeof(ip), true);
595 }
596 }
Jason Samsddceab92013-08-07 13:02:32 -0700597}
598#endif
599
Miao Wang75474682015-10-26 16:50:13 -0700600void Allocation::setupGrallocConsumer(const Context *rsc, uint32_t numAlloc) {
601#ifndef RS_COMPATIBILITY_LIB
602 // Configure GrallocConsumer to be in asynchronous mode
603 if (numAlloc > MAX_NUM_ALLOC || numAlloc <= 0) {
604 rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented");
605 return;
606 }
607 sp<IGraphicBufferConsumer> bc;
608 BufferQueue::createBufferQueue(&mGraphicBufferProducer, &bc);
609 mGrallocConsumer = new GrallocConsumer(this, bc, mHal.drvState.grallocFlags, numAlloc);
610
611 mBufferListener = new NewBufferListener(numAlloc);
612 mBufferListener->rsc = rsc;
613 mBufferListener->alloc[0] = this;
614 mCurrentIdx = 0;
615 mBufferQueueInited = true;
616
617 mGrallocConsumer->setFrameAvailableListener(mBufferListener);
618#endif
619}
620
Jason Sams733396b2013-02-22 12:46:18 -0800621void * Allocation::getSurface(const Context *rsc) {
Jason Samsddceab92013-08-07 13:02:32 -0700622#ifndef RS_COMPATIBILITY_LIB
623 // Configure GrallocConsumer to be in asynchronous mode
Miao Wang75474682015-10-26 16:50:13 -0700624 if (!mBufferQueueInited) {
625 // This case is only used for single frame processing,
626 // since we will always call setupGrallocConsumer first in
627 // multi-frame case.
628 setupGrallocConsumer(rsc, 1);
629 }
630 mGraphicBufferProducer->incStrong(nullptr);
631 return mGraphicBufferProducer.get();
Jason Samsddceab92013-08-07 13:02:32 -0700632#else
Chris Wailes44bef6f2014-08-12 13:51:10 -0700633 return nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700634#endif
635 //return rsc->mHal.funcs.allocation.getSurface(rsc, this);
Jason Sams41e373d2012-01-13 14:01:20 -0800636}
637
Miao Wang75474682015-10-26 16:50:13 -0700638void Allocation::shareBufferQueue(const Context *rsc, const Allocation *alloc) {
639#ifndef RS_COMPATIBILITY_LIB
640 mGrallocConsumer = alloc->mGrallocConsumer;
641 mCurrentIdx = mGrallocConsumer->getNextAvailableIdx(this);
642 if (mCurrentIdx >= mGrallocConsumer->mNumAlloc) {
643 rsc->setError(RS_ERROR_DRIVER, "Maximum allocations attached to a BufferQueue");
644 return;
645 }
646
647 mGraphicBufferProducer = alloc->mGraphicBufferProducer;
648 mBufferListener = alloc->mBufferListener;
649 mBufferListener->alloc[mCurrentIdx] = this;
650 mBufferQueueInited = true;
651#endif
652}
653
654
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800655void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
656 ANativeWindow *nw = (ANativeWindow *)sur;
Jason Sams733396b2013-02-22 12:46:18 -0800657 rsc->mHal.funcs.allocation.setSurface(rsc, this, nw);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800658}
659
660void Allocation::ioSend(const Context *rsc) {
661 rsc->mHal.funcs.allocation.ioSend(rsc, this);
662}
663
664void Allocation::ioReceive(const Context *rsc) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700665 void *ptr = nullptr;
Jason Samsddceab92013-08-07 13:02:32 -0700666 size_t stride = 0;
667#ifndef RS_COMPATIBILITY_LIB
668 if (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
Miao Wang75474682015-10-26 16:50:13 -0700669 status_t ret = mGrallocConsumer->lockNextBuffer(mCurrentIdx);
Jason Samsddceab92013-08-07 13:02:32 -0700670
671 if (ret == OK) {
672 rsc->mHal.funcs.allocation.ioReceive(rsc, this);
673 } else if (ret == BAD_VALUE) {
674 // No new frame, don't do anything
675 } else {
676 rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer.");
677 }
678
679 }
680#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800681}
682
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700683bool Allocation::hasSameDims(const Allocation *other) const {
684 const Type *type0 = this->getType(),
685 *type1 = other->getType();
686
687 return (type0->getCellCount() == type1->getCellCount()) &&
688 (type0->getDimLOD() == type1->getDimLOD()) &&
689 (type0->getDimFaces() == type1->getDimFaces()) &&
690 (type0->getDimYuv() == type1->getDimYuv()) &&
691 (type0->getDimX() == type1->getDimX()) &&
692 (type0->getDimY() == type1->getDimY()) &&
693 (type0->getDimZ() == type1->getDimZ());
694}
695
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800696
Jason Sams326e0dd2009-05-22 14:03:28 -0700697/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700698//
Stephen Hines6a121812011-03-01 17:34:59 -0800699
Jason Sams366c9c82010-12-08 16:14:36 -0800700void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
701 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700702 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800703 a->syncAll(rsc, src);
704}
705
Jason Samsa2371512011-01-12 13:28:37 -0800706void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700707 Allocation *alloc = static_cast<Allocation *>(va);
708 rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc);
Jason Sams837e3882010-12-10 16:03:15 -0800709}
710
Jason Sams807fdc42012-07-25 17:55:39 -0700711void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
712 Allocation *a = static_cast<Allocation *>(va);
713 const Type * t = a->getType();
714 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700715 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700716}
717
Jason Sams4b45b892010-12-29 14:31:29 -0800718void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700719 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700720 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800721 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700722}
723
Miao Wangcc8cea72015-02-19 18:14:46 -0800724void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x,
725 uint32_t lod, const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700726 Allocation *a = static_cast<Allocation *>(va);
Miao Wangcc8cea72015-02-19 18:14:46 -0800727 a->elementData(rsc, x, 0, 0, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700728}
729
Miao Wangcc8cea72015-02-19 18:14:46 -0800730void rsi_AllocationElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
731 uint32_t lod, const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700732 Allocation *a = static_cast<Allocation *>(va);
Miao Wangcc8cea72015-02-19 18:14:46 -0800733 a->elementData(rsc, x, y, z, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700734}
735
Jason Sams4b45b892010-12-29 14:31:29 -0800736void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800737 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700738 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800739 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Jason Sams326e0dd2009-05-22 14:03:28 -0700740}
741
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700742void rsi_Allocation3DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
743 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) {
744 Allocation *a = static_cast<Allocation *>(va);
745 a->data(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
746}
747
748
Jason Sams807fdc42012-07-25 17:55:39 -0700749void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
Jason Samse579df42009-08-10 14:55:26 -0700750 Allocation *a = static_cast<Allocation *>(va);
Jason Sams807fdc42012-07-25 17:55:39 -0700751 const Type * t = a->getType();
Miao Wangcc8cea72015-02-19 18:14:46 -0800752 if(t->getDimZ()) {
753 a->read(rsc, 0, 0, 0, 0, t->getDimX(), t->getDimY(), t->getDimZ(),
754 data, sizeBytes, 0);
755 } else if(t->getDimY()) {
Jason Sams807fdc42012-07-25 17:55:39 -0700756 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700757 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Jason Sams807fdc42012-07-25 17:55:39 -0700758 } else {
759 a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes);
760 }
761
Jason Samse579df42009-08-10 14:55:26 -0700762}
763
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800764void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700765 Allocation *a = static_cast<Allocation *>(va);
766 a->resize1D(rsc, dimX);
767}
768
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800769void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700770 Allocation *a = static_cast<Allocation *>(va);
771 a->resize2D(rsc, dimX, dimY);
772}
773
Jason Samsc975cf42011-04-28 18:26:48 -0700774RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800775 RsAllocationMipmapControl mipmaps,
Yang Ni79c51de2016-03-08 21:01:45 +0000776 uint32_t usages, uintptr_t ptr) {
Stephen Hines8f615d62013-12-20 12:23:32 -0800777 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mipmaps, (void*)ptr);
Yang Ni79c51de2016-03-08 21:01:45 +0000778 if (!alloc) {
779 return nullptr;
Jason Samseb4fe182011-05-26 16:33:01 -0700780 }
Yang Ni79c51de2016-03-08 21:01:45 +0000781 alloc->incUserRef();
Jason Samsf0c1df42010-10-26 13:09:17 -0700782 return alloc;
783}
784
Miao Wang47a58812015-07-23 21:59:16 -0700785RsAllocation rsi_AllocationCreateStrided(Context *rsc, RsType vtype,
786 RsAllocationMipmapControl mipmaps,
787 uint32_t usages, uintptr_t ptr,
788 size_t requiredAlignment) {
789 Allocation * alloc = Allocation::createAllocationStrided(rsc, static_cast<Type *>(vtype), usages, mipmaps,
790 (void*)ptr, requiredAlignment);
791 if (!alloc) {
792 return nullptr;
793 }
794 alloc->incUserRef();
795 return alloc;
796}
797
Jason Samsc975cf42011-04-28 18:26:48 -0700798RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800799 RsAllocationMipmapControl mipmaps,
Yang Ni79c51de2016-03-08 21:01:45 +0000800 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800801 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700802
Yang Ni79c51de2016-03-08 21:01:45 +0000803 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700804 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700805 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000806 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700807 return nullptr;
Jason Samsf0c1df42010-10-26 13:09:17 -0700808 }
809
Jason Sams807fdc42012-07-25 17:55:39 -0700810 texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray60c27962013-02-07 16:15:23 -0800811 t->getDimX(), t->getDimY(), data, sizeBytes, 0);
Stephen Hines8f615d62013-12-20 12:23:32 -0800812 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700813 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700814 }
815
Jason Samseb4fe182011-05-26 16:33:01 -0700816 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700817 return texAlloc;
818}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800819
Jason Samsc975cf42011-04-28 18:26:48 -0700820RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
Stephen Hines8f615d62013-12-20 12:23:32 -0800821 RsAllocationMipmapControl mipmaps,
Yang Ni79c51de2016-03-08 21:01:45 +0000822 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800823 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800824
825 // Cubemap allocation's faces should be Width by Width each.
826 // Source data should have 6 * Width by Width pixels
827 // Error checking is done in the java layer
Yang Ni79c51de2016-03-08 21:01:45 +0000828 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800829 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700830 if (texAlloc == nullptr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000831 ALOGE("Memory allocation failure");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700832 return nullptr;
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800833 }
834
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800835 uint32_t faceSize = t->getDimX();
836 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
837 uint32_t copySize = faceSize * t->getElementSizeBytes();
838
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800839 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800840 for (uint32_t face = 0; face < 6; face ++) {
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800841 for (uint32_t dI = 0; dI < faceSize; dI ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700842 texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face,
Tim Murray60c27962013-02-07 16:15:23 -0800843 t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize, 0);
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800844 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800845
Jason Sams366c9c82010-12-08 16:14:36 -0800846 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800847 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800848 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800849
Stephen Hines8f615d62013-12-20 12:23:32 -0800850 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700851 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800852 }
853
Jason Samseb4fe182011-05-26 16:33:01 -0700854 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800855 return texAlloc;
856}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800857
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700858void rsi_AllocationCopy2DRange(Context *rsc,
859 RsAllocation dstAlloc,
860 uint32_t dstXoff, uint32_t dstYoff,
861 uint32_t dstMip, uint32_t dstFace,
862 uint32_t width, uint32_t height,
863 RsAllocation srcAlloc,
864 uint32_t srcXoff, uint32_t srcYoff,
865 uint32_t srcMip, uint32_t srcFace) {
866 Allocation *dst = static_cast<Allocation *>(dstAlloc);
867 Allocation *src= static_cast<Allocation *>(srcAlloc);
868 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
869 (RsAllocationCubemapFace)dstFace,
870 width, height,
871 src, srcXoff, srcYoff,srcMip,
872 (RsAllocationCubemapFace)srcFace);
873}
874
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700875void rsi_AllocationCopy3DRange(Context *rsc,
876 RsAllocation dstAlloc,
877 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
878 uint32_t dstMip,
879 uint32_t width, uint32_t height, uint32_t depth,
880 RsAllocation srcAlloc,
881 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
882 uint32_t srcMip) {
883 Allocation *dst = static_cast<Allocation *>(dstAlloc);
884 Allocation *src= static_cast<Allocation *>(srcAlloc);
885 rsc->mHal.funcs.allocation.allocData3D(rsc, dst, dstXoff, dstYoff, dstZoff, dstMip,
886 width, height, depth,
887 src, srcXoff, srcYoff, srcZoff, srcMip);
888}
889
Miao Wang75474682015-10-26 16:50:13 -0700890void rsi_AllocationSetupBufferQueue(Context *rsc, RsAllocation valloc, uint32_t numAlloc) {
891 Allocation *alloc = static_cast<Allocation *>(valloc);
892 alloc->setupGrallocConsumer(rsc, numAlloc);
893}
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700894
Jason Sams733396b2013-02-22 12:46:18 -0800895void * rsi_AllocationGetSurface(Context *rsc, RsAllocation valloc) {
Jason Sams41e373d2012-01-13 14:01:20 -0800896 Allocation *alloc = static_cast<Allocation *>(valloc);
Jason Sams733396b2013-02-22 12:46:18 -0800897 void *s = alloc->getSurface(rsc);
898 return s;
Jason Sams3522f402012-03-23 11:47:26 -0700899}
900
Miao Wang75474682015-10-26 16:50:13 -0700901void rsi_AllocationShareBufferQueue(Context *rsc, RsAllocation valloc1, RsAllocation valloc2) {
902 Allocation *alloc1 = static_cast<Allocation *>(valloc1);
903 Allocation *alloc2 = static_cast<Allocation *>(valloc2);
904 alloc1->shareBufferQueue(rsc, alloc2);
905}
906
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800907void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
908 Allocation *alloc = static_cast<Allocation *>(valloc);
909 alloc->setSurface(rsc, sur);
910}
911
912void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
913 Allocation *alloc = static_cast<Allocation *>(valloc);
914 alloc->ioSend(rsc);
915}
916
Miao Wang75474682015-10-26 16:50:13 -0700917int64_t rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800918 Allocation *alloc = static_cast<Allocation *>(valloc);
919 alloc->ioReceive(rsc);
Miao Wang75474682015-10-26 16:50:13 -0700920 return alloc->getTimeStamp();
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800921}
922
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700923void *rsi_AllocationGetPointer(Context *rsc, RsAllocation valloc,
Jason Samsb8a94e22014-02-24 17:52:32 -0800924 uint32_t lod, RsAllocationCubemapFace face,
925 uint32_t z, uint32_t array, size_t *stride, size_t strideLen) {
926 Allocation *alloc = static_cast<Allocation *>(valloc);
Tim Murray0e61af92014-02-26 13:28:52 -0800927 rsAssert(strideLen == sizeof(size_t));
Jason Samsb8a94e22014-02-24 17:52:32 -0800928
Stephen Hinesaf7373f2014-09-11 00:58:18 -0700929 return alloc->getPointer(rsc, lod, face, z, array, stride);
Jason Samsb8a94e22014-02-24 17:52:32 -0800930}
931
Tim Murray509ea5c2012-11-13 11:56:40 -0800932void rsi_Allocation1DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
933 uint32_t count, void *data, size_t sizeBytes) {
934 Allocation *a = static_cast<Allocation *>(va);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700935 rsc->mHal.funcs.allocation.read1D(rsc, a, xoff, lod, count, data, sizeBytes);
Tim Murray509ea5c2012-11-13 11:56:40 -0800936}
937
Miao Wangcc8cea72015-02-19 18:14:46 -0800938void rsi_AllocationElementRead(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
939 uint32_t lod, void *data, size_t sizeBytes, size_t eoff) {
940 Allocation *a = static_cast<Allocation *>(va);
941 a->elementRead(rsc, x, y, z, data, eoff, sizeBytes);
942}
943
Tim Murray7b3e3092012-11-16 13:32:24 -0800944void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff,
945 uint32_t lod, RsAllocationCubemapFace face, uint32_t w,
Tim Murray358747a2012-11-26 13:52:04 -0800946 uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Tim Murray7b3e3092012-11-16 13:32:24 -0800947 Allocation *a = static_cast<Allocation *>(va);
Tim Murray358747a2012-11-26 13:52:04 -0800948 a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
Tim Murray7b3e3092012-11-16 13:32:24 -0800949}
950
Miao Wangcc8cea72015-02-19 18:14:46 -0800951void rsi_Allocation3DRead(Context *rsc, RsAllocation va,
952 uint32_t xoff, uint32_t yoff, uint32_t zoff,
953 uint32_t lod, uint32_t w, uint32_t h, uint32_t d,
954 void *data, size_t sizeBytes, size_t stride) {
955 Allocation *a = static_cast<Allocation *>(va);
956 a->read(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride);
957}
958
Jason Samscfea6c12015-02-09 12:50:22 -0800959RsAllocation rsi_AllocationAdapterCreate(Context *rsc, RsType vwindow, RsAllocation vbase) {
960
961
962 Allocation * alloc = Allocation::createAdapter(rsc,
963 static_cast<Allocation *>(vbase), static_cast<Type *>(vwindow));
964 if (!alloc) {
965 return nullptr;
966 }
967 alloc->incUserRef();
968 return alloc;
969}
970
971void rsi_AllocationAdapterOffset(Context *rsc, RsAllocation va, const uint32_t *offsets, size_t len) {
Jason Sams442b7ff2015-03-06 16:50:11 -0800972 Allocation *a = static_cast<Allocation *>(va);
973 a->adapterOffset(rsc, offsets, len);
Jason Samscfea6c12015-02-09 12:50:22 -0800974}
975
976
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -0800977} // namespace renderscript
978} // namespace android