Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 3 | * |
| 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 Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 16 | |
Alex Sakhartchouk | 77d9f4b | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 17 | #include "rsContext.h" |
Alex Sakhartchouk | 4edf030 | 2012-03-09 10:47:27 -0800 | [diff] [blame] | 18 | #include "rsAllocation.h" |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 19 | #include "rs_hal.h" |
| 20 | |
Miao Wang | f750c53 | 2017-03-03 16:03:44 -0800 | [diff] [blame] | 21 | #ifndef RS_COMPATIBILITY_LIB |
| 22 | #include "rsGrallocConsumer.h" |
| 23 | #endif |
| 24 | |
Chih-Hung Hsieh | 11496ac | 2016-11-15 15:14:05 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace renderscript { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | |
Alex Sakhartchouk | a2aab8b | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 28 | Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages, |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 29 | RsAllocationMipmapControl mc, void * ptr) |
Alex Sakhartchouk | a2aab8b | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 30 | : ObjectBase(rsc) { |
Jason Sams | fa84da2 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 31 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 32 | memset(&mHal, 0, sizeof(mHal)); |
| 33 | mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE; |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 34 | mHal.state.usageFlags = usages; |
| 35 | mHal.state.mipmapControl = mc; |
Tim Murray | 2e1a94d | 2012-11-29 13:12:25 -0800 | [diff] [blame] | 36 | mHal.state.userProvidedPtr = ptr; |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 37 | |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 38 | setType(type); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 39 | updateCache(); |
| 40 | } |
Jason Sams | fa84da2 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 41 | |
Jason Sams | cfea6c1 | 2015-02-09 12:50:22 -0800 | [diff] [blame] | 42 | Allocation::Allocation(Context *rsc, const Allocation *alloc, const Type *type) |
| 43 | : ObjectBase(rsc) { |
| 44 | |
| 45 | memset(&mHal, 0, sizeof(mHal)); |
Jason Sams | cfea6c1 | 2015-02-09 12:50:22 -0800 | [diff] [blame] | 46 | mHal.state.baseAlloc = alloc; |
Jason Sams | cfea6c1 | 2015-02-09 12:50:22 -0800 | [diff] [blame] | 47 | mHal.state.usageFlags = alloc->mHal.state.usageFlags; |
| 48 | mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE; |
| 49 | |
| 50 | setType(type); |
| 51 | updateCache(); |
Jason Sams | cfea6c1 | 2015-02-09 12:50:22 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Tim Murray | 3468938 | 2013-03-11 12:12:03 -0700 | [diff] [blame] | 54 | void Allocation::operator delete(void* ptr) { |
| 55 | if (ptr) { |
| 56 | Allocation *a = (Allocation*) ptr; |
| 57 | a->getContext()->mHal.funcs.freeRuntimeMem(ptr); |
| 58 | } |
| 59 | } |
| 60 | |
Miao Wang | 47a5881 | 2015-07-23 21:59:16 -0700 | [diff] [blame] | 61 | Allocation * Allocation::createAllocationStrided(Context *rsc, const Type *type, uint32_t usages, |
| 62 | RsAllocationMipmapControl mc, void * ptr, |
| 63 | size_t requiredAlignment) { |
Tim Murray | 3468938 | 2013-03-11 12:12:03 -0700 | [diff] [blame] | 64 | // 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 Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 69 | return nullptr; |
Tim Murray | 3468938 | 2013-03-11 12:12:03 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Jason Sams | f82b626 | 2015-05-11 15:02:50 -0700 | [diff] [blame] | 72 | 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 Wang | 47a5881 | 2015-07-23 21:59:16 -0700 | [diff] [blame] | 82 | #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 Sams | f82b626 | 2015-05-11 15:02:50 -0700 | [diff] [blame] | 87 | } 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 Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 91 | |
Jason Sams | f82b626 | 2015-05-11 15:02:50 -0700 | [diff] [blame] | 92 | if (!success) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 93 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure"); |
| 94 | delete a; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 95 | return nullptr; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 96 | } |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 97 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 98 | return a; |
| 99 | } |
| 100 | |
Miao Wang | 47a5881 | 2015-07-23 21:59:16 -0700 | [diff] [blame] | 101 | Allocation * 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 Sams | cfea6c1 | 2015-02-09 12:50:22 -0800 | [diff] [blame] | 106 | Allocation * 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 Sams | 442b7ff | 2015-03-06 16:50:11 -0800 | [diff] [blame] | 126 | void 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 Sams | cfea6c1 | 2015-02-09 12:50:22 -0800 | [diff] [blame] | 143 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 144 | void Allocation::updateCache() { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 145 | const Type *type = mHal.state.type; |
Jason Sams | a572aca | 2013-01-09 11:52:26 -0800 | [diff] [blame] | 146 | mHal.state.yuv = type->getDimYuv(); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 147 | 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 Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 153 | Allocation::~Allocation() { |
Miao Wang | 6223721 | 2017-02-27 15:19:36 -0800 | [diff] [blame] | 154 | #ifndef RS_COMPATIBILITY_LIB |
Miao Wang | f750c53 | 2017-03-03 16:03:44 -0800 | [diff] [blame] | 155 | if (mGrallocConsumer) { |
Miao Wang | 7547468 | 2015-10-26 16:50:13 -0700 | [diff] [blame] | 156 | mGrallocConsumer->releaseIdx(mCurrentIdx); |
Miao Wang | f750c53 | 2017-03-03 16:03:44 -0800 | [diff] [blame] | 157 | if (!mGrallocConsumer->isActive()) { |
| 158 | delete mGrallocConsumer; |
| 159 | } |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 160 | mGrallocConsumer = nullptr; |
Jason Sams | ddceab9 | 2013-08-07 13:02:32 -0700 | [diff] [blame] | 161 | } |
| 162 | #endif |
| 163 | |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 164 | freeChildrenUnlocked(); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 165 | mRSC->mHal.funcs.allocation.destroy(mRSC, this); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 168 | void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 169 | rsc->mHal.funcs.allocation.syncAll(rsc, this, src); |
Jason Sams | cf4c7c9 | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Jason Sams | b8a94e2 | 2014-02-24 17:52:32 -0800 | [diff] [blame] | 172 | void * 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 Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 179 | return nullptr; |
Jason Sams | b8a94e2 | 2014-02-24 17:52:32 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Jason Sams | 8ce1281 | 2015-05-18 17:28:59 -0700 | [diff] [blame] | 182 | 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 Sams | b8a94e2 | 2014-02-24 17:52:32 -0800 | [diff] [blame] | 187 | size_t s = 0; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 188 | if ((stride != nullptr) && mHal.drvState.lod[0].dimY) { |
Jason Sams | b8a94e2 | 2014-02-24 17:52:32 -0800 | [diff] [blame] | 189 | *stride = mHal.drvState.lod[lod].stride; |
| 190 | } |
| 191 | return mHal.drvState.lod[lod].mallocPtr; |
| 192 | } |
| 193 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 194 | void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod, |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 195 | uint32_t count, const void *data, size_t sizeBytes) { |
| 196 | const size_t eSize = mHal.state.type->getElementSizeBytes(); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 197 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 198 | if ((count * eSize) != sizeBytes) { |
Jason Sams | a273793 | 2014-01-14 12:28:33 -0800 | [diff] [blame] | 199 | char buf[1024]; |
George Burgess IV | 35fb321 | 2016-03-02 13:55:48 -0800 | [diff] [blame] | 200 | snprintf(buf, sizeof(buf), |
| 201 | "Allocation::subData called with mismatched size expected %zu, got %zu", |
| 202 | (count * eSize), sizeBytes); |
Jason Sams | a273793 | 2014-01-14 12:28:33 -0800 | [diff] [blame] | 203 | rsc->setError(RS_ERROR_BAD_VALUE, buf); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 204 | mHal.state.type->dumpLOGV("type info"); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 205 | return; |
| 206 | } |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 207 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 208 | rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes); |
| 209 | sendDirty(rsc); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 212 | void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 213 | uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) { |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 214 | rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 215 | sendDirty(rsc); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Jason Sams | 236385b | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 218 | void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 219 | 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 Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 225 | void Allocation::read(Context *rsc, uint32_t xoff, uint32_t lod, |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 226 | uint32_t count, void *data, size_t sizeBytes) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 227 | const size_t eSize = mHal.state.type->getElementSizeBytes(); |
| 228 | |
| 229 | if ((count * eSize) != sizeBytes) { |
Jason Sams | a273793 | 2014-01-14 12:28:33 -0800 | [diff] [blame] | 230 | char buf[1024]; |
George Burgess IV | 35fb321 | 2016-03-02 13:55:48 -0800 | [diff] [blame] | 231 | snprintf(buf, sizeof(buf), |
| 232 | "Allocation::read called with mismatched size expected %zu, got %zu", |
| 233 | (count * eSize), sizeBytes); |
Jason Sams | a273793 | 2014-01-14 12:28:33 -0800 | [diff] [blame] | 234 | rsc->setError(RS_ERROR_BAD_VALUE, buf); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 235 | 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 Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 242 | void 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 Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 248 | } else { |
| 249 | if ((lineSize * h) != sizeBytes) { |
Jason Sams | a273793 | 2014-01-14 12:28:33 -0800 | [diff] [blame] | 250 | char buf[1024]; |
George Burgess IV | 35fb321 | 2016-03-02 13:55:48 -0800 | [diff] [blame] | 251 | snprintf(buf, sizeof(buf), "Allocation size mismatch, expected %zu, got %zu", |
| 252 | (lineSize * h), sizeBytes); |
Jason Sams | a273793 | 2014-01-14 12:28:33 -0800 | [diff] [blame] | 253 | rsc->setError(RS_ERROR_BAD_VALUE, buf); |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 254 | return; |
| 255 | } |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | rsc->mHal.funcs.allocation.read2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 261 | void 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 Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 273 | void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, uint32_t z, |
| 274 | const void *data, uint32_t cIdx, size_t sizeBytes) { |
Jason Sams | a572aca | 2013-01-09 11:52:26 -0800 | [diff] [blame] | 275 | if (x >= mHal.drvState.lod[0].dimX) { |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 276 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); |
| 277 | return; |
| 278 | } |
| 279 | |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 280 | 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 Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 295 | const Element * e = mHal.state.type->getElement()->getField(cIdx); |
Alex Sakhartchouk | 7694632 | 2012-02-02 09:47:26 -0800 | [diff] [blame] | 296 | uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx); |
| 297 | if (sizeBytes != e->getSizeBytes() * elemArraySize) { |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 298 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); |
| 299 | return; |
| 300 | } |
| 301 | |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 302 | rsc->mHal.funcs.allocation.elementData(rsc, this, x, y, z, data, cIdx, sizeBytes); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 303 | sendDirty(rsc); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 306 | void Allocation::elementRead(Context *rsc, uint32_t x, uint32_t y, uint32_t z, |
| 307 | void *data, uint32_t cIdx, size_t sizeBytes) { |
Jason Sams | a572aca | 2013-01-09 11:52:26 -0800 | [diff] [blame] | 308 | if (x >= mHal.drvState.lod[0].dimX) { |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 309 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); |
| 310 | return; |
| 311 | } |
| 312 | |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 313 | 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 Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 320 | return; |
| 321 | } |
| 322 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 323 | if (cIdx >= mHal.state.type->getElement()->getFieldCount()) { |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 324 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 325 | return; |
| 326 | } |
| 327 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 328 | const Element * e = mHal.state.type->getElement()->getField(cIdx); |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 329 | uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx); |
Alex Sakhartchouk | 7694632 | 2012-02-02 09:47:26 -0800 | [diff] [blame] | 330 | if (sizeBytes != e->getSizeBytes() * elemArraySize) { |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 331 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); |
| 332 | return; |
| 333 | } |
| 334 | |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 335 | rsc->mHal.funcs.allocation.elementRead(rsc, this, x, y, z, data, cIdx, sizeBytes); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 338 | void Allocation::addProgramToDirty(const Program *p) { |
Miao Wang | 82e135c | 2017-02-27 23:35:35 -0800 | [diff] [blame] | 339 | mToDirtyList.push_back(p); |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 340 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 341 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 342 | void Allocation::removeProgramToDirty(const Program *p) { |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 343 | for (size_t ct=0; ct < mToDirtyList.size(); ct++) { |
| 344 | if (mToDirtyList[ct] == p) { |
Miao Wang | 82e135c | 2017-02-27 23:35:35 -0800 | [diff] [blame] | 345 | mToDirtyList.erase(mToDirtyList.begin() + ct); |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 346 | return; |
| 347 | } |
| 348 | } |
| 349 | rsAssert(0); |
| 350 | } |
| 351 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 352 | void Allocation::dumpLOGV(const char *prefix) const { |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 353 | ObjectBase::dumpLOGV(prefix); |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 354 | char buf[1024]; |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 355 | |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 356 | if ((strlen(prefix) + 10) < sizeof(buf)) { |
George Burgess IV | 35fb321 | 2016-03-02 13:55:48 -0800 | [diff] [blame] | 357 | snprintf(buf, sizeof(buf), "%s type ", prefix); |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 358 | if (mHal.state.type) { |
| 359 | mHal.state.type->dumpLOGV(buf); |
| 360 | } |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 361 | } |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 362 | ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x", |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 363 | prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags, mHal.state.mipmapControl); |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 364 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 365 | |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 366 | uint32_t Allocation::getPackedSize() const { |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 367 | uint32_t numItems = mHal.state.type->getCellCount(); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 368 | return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded(); |
| 369 | } |
| 370 | |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 371 | void Allocation::writePackedData(Context *rsc, const Type *type, |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 372 | 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 Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 376 | uint32_t numItems = type->getPackedSizeBytes() / paddedBytes; |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 377 | |
| 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 Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 420 | void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) { |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 421 | const uint8_t *src = (const uint8_t*)data; |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 422 | uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 423 | |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 424 | writePackedData(rsc, getType(), dst, src, true); |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 425 | rsc->mHal.funcs.allocation.unlock1D(rsc, this); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 428 | void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const { |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 429 | uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded(); |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 430 | uint32_t numItems = mHal.state.type->getCellCount(); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 431 | |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 432 | const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 433 | uint8_t *dst = new uint8_t[numItems * unpaddedBytes]; |
| 434 | |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 435 | writePackedData(rsc, getType(), dst, src, false); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 436 | stream->addByteArray(dst, getPackedSize()); |
| 437 | |
| 438 | delete[] dst; |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 439 | rsc->mHal.funcs.allocation.unlock1D(rsc, this); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 442 | void Allocation::serialize(Context *rsc, OStream *stream) const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 443 | // Need to identify ourselves |
| 444 | stream->addU32((uint32_t)getClassId()); |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 445 | stream->addString(getName()); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 446 | |
| 447 | // First thing we need to serialize is the type object since it will be needed |
| 448 | // to initialize the class |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 449 | mHal.state.type->serialize(rsc, stream); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 450 | |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 451 | uint32_t dataSize = mHal.state.type->getPackedSizeBytes(); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 452 | // 3 element vectors are padded to 4 in memory, but padding isn't serialized |
| 453 | uint32_t packedSize = getPackedSize(); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 454 | // Write how much data we are storing |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 455 | stream->addU32(packedSize); |
| 456 | if (dataSize == packedSize) { |
| 457 | // Now write the data |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 458 | stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize); |
| 459 | rsc->mHal.funcs.allocation.unlock1D(rsc, this); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 460 | } else { |
| 461 | // Now write the data |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 462 | packVec3Allocation(rsc, stream); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 463 | } |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 466 | Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 467 | // First make sure we are reading the correct object |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 468 | RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 469 | if (classID != RS_A3D_CLASS_ID_ALLOCATION) { |
Jason Sams | a273793 | 2014-01-14 12:28:33 -0800 | [diff] [blame] | 470 | rsc->setError(RS_ERROR_FATAL_DRIVER, |
| 471 | "allocation loading failed due to corrupt file. (invalid id)\n"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 472 | return nullptr; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 475 | const char *name = stream->loadString(); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 476 | |
| 477 | Type *type = Type::createFromStream(rsc, stream); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 478 | if (!type) { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 479 | return nullptr; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 480 | } |
| 481 | type->compute(); |
| 482 | |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 483 | Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT); |
| 484 | type->decUserRef(); |
| 485 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 486 | // Number of bytes we wrote out for this allocation |
| 487 | uint32_t dataSize = stream->loadU32(); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 488 | // 3 element vectors are padded to 4 in memory, but padding isn't serialized |
| 489 | uint32_t packedSize = alloc->getPackedSize(); |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 490 | if (dataSize != type->getPackedSizeBytes() && |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 491 | dataSize != packedSize) { |
Jason Sams | a273793 | 2014-01-14 12:28:33 -0800 | [diff] [blame] | 492 | rsc->setError(RS_ERROR_FATAL_DRIVER, |
| 493 | "allocation loading failed due to corrupt file. (invalid size)\n"); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 494 | ObjectBase::checkDelete(alloc); |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 495 | ObjectBase::checkDelete(type); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 496 | return nullptr; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 499 | alloc->assignName(name); |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 500 | if (dataSize == type->getPackedSizeBytes()) { |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 501 | 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 Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 505 | alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 506 | } |
Alex Sakhartchouk | e6d9fbc | 2010-08-11 10:30:44 -0700 | [diff] [blame] | 507 | stream->reset(stream->getPos() + dataSize); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 508 | |
| 509 | return alloc; |
| 510 | } |
| 511 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 512 | void Allocation::sendDirty(const Context *rsc) const { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 513 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 514 | for (size_t ct=0; ct < mToDirtyList.size(); ct++) { |
| 515 | mToDirtyList[ct]->forceDirty(); |
| 516 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 517 | #endif |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 518 | mRSC->mHal.funcs.allocation.markDirty(rsc, this); |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 519 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 520 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 521 | void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const { |
Stephen Hines | 9f70a4e | 2012-04-02 20:18:48 -0700 | [diff] [blame] | 522 | mHal.state.type->incRefs(ptr, ct, startOff); |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 525 | void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const { |
Alex Sakhartchouk | 5c4369a | 2011-08-12 11:30:30 -0700 | [diff] [blame] | 526 | if (!mHal.state.hasReferences || !getIsScript()) { |
| 527 | return; |
| 528 | } |
Stephen Hines | 9f70a4e | 2012-04-02 20:18:48 -0700 | [diff] [blame] | 529 | mHal.state.type->decRefs(ptr, ct, startOff); |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Jason Sams | a36c50a | 2014-06-17 12:06:06 -0700 | [diff] [blame] | 532 | void Allocation::callUpdateCacheObject(const Context *rsc, void *dstObj) const { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 533 | if (rsc->mHal.funcs.allocation.updateCachedObject != nullptr) { |
Jason Sams | a36c50a | 2014-06-17 12:06:06 -0700 | [diff] [blame] | 534 | rsc->mHal.funcs.allocation.updateCachedObject(rsc, this, (rs_allocation *)dstObj); |
| 535 | } else { |
| 536 | *((const void **)dstObj) = this; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 541 | void Allocation::freeChildrenUnlocked () { |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 542 | void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this); |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 543 | decRefs(ptr, mHal.state.type->getCellCount(), 0); |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 544 | mRSC->mHal.funcs.allocation.unlock1D(mRSC, this); |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | bool Allocation::freeChildren() { |
| 548 | if (mHal.state.hasReferences) { |
| 549 | incSysRef(); |
| 550 | freeChildrenUnlocked(); |
| 551 | return decSysRef(); |
| 552 | } |
| 553 | return false; |
| 554 | } |
| 555 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 556 | void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) { |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 559 | void Allocation::resize1D(Context *rsc, uint32_t dimX) { |
Jason Sams | a572aca | 2013-01-09 11:52:26 -0800 | [diff] [blame] | 560 | uint32_t oldDimX = mHal.drvState.lod[0].dimX; |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 561 | if (dimX == oldDimX) { |
| 562 | return; |
| 563 | } |
| 564 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 565 | ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX); |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 566 | if (dimX < oldDimX) { |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 567 | decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX); |
| 568 | rsc->mHal.funcs.allocation.unlock1D(rsc, this); |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 569 | } |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 570 | rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences); |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 571 | setType(t.get()); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 572 | updateCache(); |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 575 | void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) { |
Jason Sams | a273793 | 2014-01-14 12:28:33 -0800 | [diff] [blame] | 576 | rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented"); |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Miao Wang | 7547468 | 2015-10-26 16:50:13 -0700 | [diff] [blame] | 579 | void 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 Wang | f750c53 | 2017-03-03 16:03:44 -0800 | [diff] [blame] | 586 | mGrallocConsumer = new GrallocConsumer(rsc, this, numAlloc); |
Miao Wang | 7547468 | 2015-10-26 16:50:13 -0700 | [diff] [blame] | 587 | mCurrentIdx = 0; |
| 588 | mBufferQueueInited = true; |
Miao Wang | 7547468 | 2015-10-26 16:50:13 -0700 | [diff] [blame] | 589 | #endif |
| 590 | } |
| 591 | |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 592 | void * Allocation::getSurface(const Context *rsc) { |
Jason Sams | ddceab9 | 2013-08-07 13:02:32 -0700 | [diff] [blame] | 593 | #ifndef RS_COMPATIBILITY_LIB |
| 594 | // Configure GrallocConsumer to be in asynchronous mode |
Miao Wang | 7547468 | 2015-10-26 16:50:13 -0700 | [diff] [blame] | 595 | 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 Wang | f750c53 | 2017-03-03 16:03:44 -0800 | [diff] [blame] | 601 | return mGrallocConsumer->getNativeWindow(); |
Jason Sams | ddceab9 | 2013-08-07 13:02:32 -0700 | [diff] [blame] | 602 | #else |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 603 | return nullptr; |
Jason Sams | ddceab9 | 2013-08-07 13:02:32 -0700 | [diff] [blame] | 604 | #endif |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Miao Wang | 7547468 | 2015-10-26 16:50:13 -0700 | [diff] [blame] | 607 | void 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 Wang | 7547468 | 2015-10-26 16:50:13 -0700 | [diff] [blame] | 615 | mBufferQueueInited = true; |
| 616 | #endif |
| 617 | } |
| 618 | |
| 619 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 620 | void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) { |
| 621 | ANativeWindow *nw = (ANativeWindow *)sur; |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 622 | rsc->mHal.funcs.allocation.setSurface(rsc, this, nw); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | void Allocation::ioSend(const Context *rsc) { |
| 626 | rsc->mHal.funcs.allocation.ioSend(rsc, this); |
| 627 | } |
| 628 | |
| 629 | void Allocation::ioReceive(const Context *rsc) { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 630 | void *ptr = nullptr; |
Jason Sams | ddceab9 | 2013-08-07 13:02:32 -0700 | [diff] [blame] | 631 | size_t stride = 0; |
| 632 | #ifndef RS_COMPATIBILITY_LIB |
| 633 | if (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { |
Miao Wang | f750c53 | 2017-03-03 16:03:44 -0800 | [diff] [blame] | 634 | media_status_t ret = mGrallocConsumer->lockNextBuffer(mCurrentIdx); |
Jason Sams | ddceab9 | 2013-08-07 13:02:32 -0700 | [diff] [blame] | 635 | |
Miao Wang | f750c53 | 2017-03-03 16:03:44 -0800 | [diff] [blame] | 636 | if (ret == AMEDIA_OK) { |
Jason Sams | ddceab9 | 2013-08-07 13:02:32 -0700 | [diff] [blame] | 637 | rsc->mHal.funcs.allocation.ioReceive(rsc, this); |
Miao Wang | f750c53 | 2017-03-03 16:03:44 -0800 | [diff] [blame] | 638 | } else if (ret == AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE) { |
Jason Sams | ddceab9 | 2013-08-07 13:02:32 -0700 | [diff] [blame] | 639 | // 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 Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 648 | bool 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 Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 661 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 662 | ///////////////// |
Jason Sams | 565ac36 | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 663 | // |
Stephen Hines | 6a12181 | 2011-03-01 17:34:59 -0800 | [diff] [blame] | 664 | |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 665 | void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) { |
| 666 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 667 | a->sendDirty(rsc); |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 668 | a->syncAll(rsc, src); |
| 669 | } |
| 670 | |
Jason Sams | a237151 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 671 | void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) { |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 672 | Allocation *alloc = static_cast<Allocation *>(va); |
| 673 | rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc); |
Jason Sams | 837e388 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 676 | void 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 Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 680 | t->getDimX(), t->getDimY(), data, sizeBytes, 0); |
Alex Sakhartchouk | 39f2ef6 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 683 | void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod, |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 684 | uint32_t count, const void *data, size_t sizeBytes) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 685 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 686 | a->data(rsc, xoff, lod, count, data, sizeBytes); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 689 | void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, |
| 690 | uint32_t lod, const void *data, size_t sizeBytes, size_t eoff) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 691 | Allocation *a = static_cast<Allocation *>(va); |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 692 | a->elementData(rsc, x, 0, 0, data, eoff, sizeBytes); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 695 | void 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 Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 697 | Allocation *a = static_cast<Allocation *>(va); |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 698 | a->elementData(rsc, x, y, z, data, eoff, sizeBytes); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 701 | void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 702 | uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) { |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 703 | Allocation *a = static_cast<Allocation *>(va); |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 704 | a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 707 | void 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 Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 714 | void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) { |
Jason Sams | e579df4 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 715 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 716 | const Type * t = a->getType(); |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 717 | 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 Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 721 | a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 722 | t->getDimX(), t->getDimY(), data, sizeBytes, 0); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 723 | } else { |
| 724 | a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes); |
| 725 | } |
| 726 | |
Jason Sams | e579df4 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 729 | void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) { |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 730 | Allocation *a = static_cast<Allocation *>(va); |
| 731 | a->resize1D(rsc, dimX); |
| 732 | } |
| 733 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 734 | void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) { |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 735 | Allocation *a = static_cast<Allocation *>(va); |
| 736 | a->resize2D(rsc, dimX, dimY); |
| 737 | } |
| 738 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 739 | RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype, |
Stephen Hines | 8f615d6 | 2013-12-20 12:23:32 -0800 | [diff] [blame] | 740 | RsAllocationMipmapControl mipmaps, |
Yang Ni | 79c51de | 2016-03-08 21:01:45 +0000 | [diff] [blame] | 741 | uint32_t usages, uintptr_t ptr) { |
Stephen Hines | 8f615d6 | 2013-12-20 12:23:32 -0800 | [diff] [blame] | 742 | Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mipmaps, (void*)ptr); |
Yang Ni | 79c51de | 2016-03-08 21:01:45 +0000 | [diff] [blame] | 743 | if (!alloc) { |
| 744 | return nullptr; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 745 | } |
Yang Ni | 79c51de | 2016-03-08 21:01:45 +0000 | [diff] [blame] | 746 | alloc->incUserRef(); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 747 | return alloc; |
| 748 | } |
| 749 | |
Miao Wang | 47a5881 | 2015-07-23 21:59:16 -0700 | [diff] [blame] | 750 | RsAllocation 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 Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 763 | RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype, |
Stephen Hines | 8f615d6 | 2013-12-20 12:23:32 -0800 | [diff] [blame] | 764 | RsAllocationMipmapControl mipmaps, |
Yang Ni | 79c51de | 2016-03-08 21:01:45 +0000 | [diff] [blame] | 765 | const void *data, size_t sizeBytes, uint32_t usages) { |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 766 | Type *t = static_cast<Type *>(vtype); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 767 | |
Yang Ni | 79c51de | 2016-03-08 21:01:45 +0000 | [diff] [blame] | 768 | RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 769 | Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 770 | if (texAlloc == nullptr) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 771 | ALOGE("Memory allocation failure"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 772 | return nullptr; |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 775 | texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, |
Tim Murray | 60c2796 | 2013-02-07 16:15:23 -0800 | [diff] [blame] | 776 | t->getDimX(), t->getDimY(), data, sizeBytes, 0); |
Stephen Hines | 8f615d6 | 2013-12-20 12:23:32 -0800 | [diff] [blame] | 777 | if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) { |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 778 | rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 781 | texAlloc->sendDirty(rsc); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 782 | return texAlloc; |
| 783 | } |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 784 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 785 | RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype, |
Stephen Hines | 8f615d6 | 2013-12-20 12:23:32 -0800 | [diff] [blame] | 786 | RsAllocationMipmapControl mipmaps, |
Yang Ni | 79c51de | 2016-03-08 21:01:45 +0000 | [diff] [blame] | 787 | const void *data, size_t sizeBytes, uint32_t usages) { |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 788 | Type *t = static_cast<Type *>(vtype); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 789 | |
| 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 Ni | 79c51de | 2016-03-08 21:01:45 +0000 | [diff] [blame] | 793 | RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 794 | Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 795 | if (texAlloc == nullptr) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 796 | ALOGE("Memory allocation failure"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 797 | return nullptr; |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Alex Sakhartchouk | 9f8bc4f | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 800 | uint32_t faceSize = t->getDimX(); |
| 801 | uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes(); |
| 802 | uint32_t copySize = faceSize * t->getElementSizeBytes(); |
| 803 | |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 804 | uint8_t *sourcePtr = (uint8_t*)data; |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 805 | for (uint32_t face = 0; face < 6; face ++) { |
Alex Sakhartchouk | 9f8bc4f | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 806 | for (uint32_t dI = 0; dI < faceSize; dI ++) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 807 | texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face, |
Tim Murray | 60c2796 | 2013-02-07 16:15:23 -0800 | [diff] [blame] | 808 | t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize, 0); |
Alex Sakhartchouk | 9f8bc4f | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 809 | } |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 810 | |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 811 | // Move the data pointer to the next cube face |
Alex Sakhartchouk | 9f8bc4f | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 812 | sourcePtr += copySize; |
Alex Sakhartchouk | f8aafcf | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 813 | } |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 814 | |
Stephen Hines | 8f615d6 | 2013-12-20 12:23:32 -0800 | [diff] [blame] | 815 | if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) { |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 816 | rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 817 | } |
| 818 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 819 | texAlloc->sendDirty(rsc); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 820 | return texAlloc; |
| 821 | } |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 822 | |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 823 | void 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 Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 840 | void 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 Wang | 7547468 | 2015-10-26 16:50:13 -0700 | [diff] [blame] | 855 | void rsi_AllocationSetupBufferQueue(Context *rsc, RsAllocation valloc, uint32_t numAlloc) { |
| 856 | Allocation *alloc = static_cast<Allocation *>(valloc); |
| 857 | alloc->setupGrallocConsumer(rsc, numAlloc); |
| 858 | } |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 859 | |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 860 | void * rsi_AllocationGetSurface(Context *rsc, RsAllocation valloc) { |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 861 | Allocation *alloc = static_cast<Allocation *>(valloc); |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 862 | void *s = alloc->getSurface(rsc); |
| 863 | return s; |
Jason Sams | 3522f40 | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Miao Wang | 7547468 | 2015-10-26 16:50:13 -0700 | [diff] [blame] | 866 | void 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 Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 872 | void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) { |
| 873 | Allocation *alloc = static_cast<Allocation *>(valloc); |
| 874 | alloc->setSurface(rsc, sur); |
| 875 | } |
| 876 | |
| 877 | void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) { |
| 878 | Allocation *alloc = static_cast<Allocation *>(valloc); |
| 879 | alloc->ioSend(rsc); |
| 880 | } |
| 881 | |
Miao Wang | 7547468 | 2015-10-26 16:50:13 -0700 | [diff] [blame] | 882 | int64_t rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) { |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 883 | Allocation *alloc = static_cast<Allocation *>(valloc); |
| 884 | alloc->ioReceive(rsc); |
Miao Wang | 7547468 | 2015-10-26 16:50:13 -0700 | [diff] [blame] | 885 | return alloc->getTimeStamp(); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 886 | } |
| 887 | |
Stephen Hines | af7373f | 2014-09-11 00:58:18 -0700 | [diff] [blame] | 888 | void *rsi_AllocationGetPointer(Context *rsc, RsAllocation valloc, |
Jason Sams | b8a94e2 | 2014-02-24 17:52:32 -0800 | [diff] [blame] | 889 | 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 Murray | 0e61af9 | 2014-02-26 13:28:52 -0800 | [diff] [blame] | 892 | rsAssert(strideLen == sizeof(size_t)); |
Jason Sams | b8a94e2 | 2014-02-24 17:52:32 -0800 | [diff] [blame] | 893 | |
Stephen Hines | af7373f | 2014-09-11 00:58:18 -0700 | [diff] [blame] | 894 | return alloc->getPointer(rsc, lod, face, z, array, stride); |
Jason Sams | b8a94e2 | 2014-02-24 17:52:32 -0800 | [diff] [blame] | 895 | } |
| 896 | |
Tim Murray | 509ea5c | 2012-11-13 11:56:40 -0800 | [diff] [blame] | 897 | void 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 Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 900 | rsc->mHal.funcs.allocation.read1D(rsc, a, xoff, lod, count, data, sizeBytes); |
Tim Murray | 509ea5c | 2012-11-13 11:56:40 -0800 | [diff] [blame] | 901 | } |
| 902 | |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 903 | void 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 Murray | 7b3e309 | 2012-11-16 13:32:24 -0800 | [diff] [blame] | 909 | void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, |
| 910 | uint32_t lod, RsAllocationCubemapFace face, uint32_t w, |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 911 | uint32_t h, void *data, size_t sizeBytes, size_t stride) { |
Tim Murray | 7b3e309 | 2012-11-16 13:32:24 -0800 | [diff] [blame] | 912 | Allocation *a = static_cast<Allocation *>(va); |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 913 | a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride); |
Tim Murray | 7b3e309 | 2012-11-16 13:32:24 -0800 | [diff] [blame] | 914 | } |
| 915 | |
Miao Wang | cc8cea7 | 2015-02-19 18:14:46 -0800 | [diff] [blame] | 916 | void 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 Sams | cfea6c1 | 2015-02-09 12:50:22 -0800 | [diff] [blame] | 924 | RsAllocation 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 | |
| 936 | void rsi_AllocationAdapterOffset(Context *rsc, RsAllocation va, const uint32_t *offsets, size_t len) { |
Jason Sams | 442b7ff | 2015-03-06 16:50:11 -0800 | [diff] [blame] | 937 | Allocation *a = static_cast<Allocation *>(va); |
| 938 | a->adapterOffset(rsc, offsets, len); |
Jason Sams | cfea6c1 | 2015-02-09 12:50:22 -0800 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | |
Chih-Hung Hsieh | 11496ac | 2016-11-15 15:14:05 -0800 | [diff] [blame] | 942 | } // namespace renderscript |
| 943 | } // namespace android |