blob: 5d099361cb678c4e02554ff80372c49074a3a559 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Stephen Hines6ae039b2012-01-18 18:46:27 -08002 * Copyright (C) 2009-2012 The Android Open Source Project
Jason Sams326e0dd2009-05-22 14:03:28 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jason Sams326e0dd2009-05-22 14:03:28 -070016
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080017#include "rsContext.h"
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080018#include "rsAllocation.h"
19#include "rsAdapter.h"
Jason Samseb4fe182011-05-26 16:33:01 -070020#include "rs_hal.h"
21
Jason Sams7ac2a4d2012-02-15 12:04:24 -080022#include "system/window.h"
Jason Sams3522f402012-03-23 11:47:26 -070023#include "gui/SurfaceTexture.h"
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070024
Jason Sams326e0dd2009-05-22 14:03:28 -070025using namespace android;
26using namespace android::renderscript;
27
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080028Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080029 RsAllocationMipmapControl mc, void * ptr)
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080030 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080031
Jason Samseb4fe182011-05-26 16:33:01 -070032 memset(&mHal, 0, sizeof(mHal));
33 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsbad80742011-03-16 16:29:28 -070034 mHal.state.usageFlags = usages;
35 mHal.state.mipmapControl = mc;
Jason Sams366c9c82010-12-08 16:14:36 -080036
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070037 setType(type);
Jason Samsbad80742011-03-16 16:29:28 -070038 updateCache();
39}
Jason Samsfa84da22010-03-01 15:31:04 -080040
Jason Samseb4fe182011-05-26 16:33:01 -070041Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080042 RsAllocationMipmapControl mc, void * ptr) {
43 Allocation *a = new Allocation(rsc, type, usages, mc, ptr);
Jason Samseb4fe182011-05-26 16:33:01 -070044
45 if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
46 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
47 delete a;
48 return NULL;
49 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -080050
Jason Samseb4fe182011-05-26 16:33:01 -070051 return a;
52}
53
Jason Samsbad80742011-03-16 16:29:28 -070054void Allocation::updateCache() {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070055 const Type *type = mHal.state.type;
Jason Samsbad80742011-03-16 16:29:28 -070056 mHal.state.dimensionX = type->getDimX();
57 mHal.state.dimensionY = type->getDimY();
58 mHal.state.dimensionZ = type->getDimZ();
59 mHal.state.hasFaces = type->getDimFaces();
60 mHal.state.hasMipmaps = type->getDimLOD();
61 mHal.state.elementSizeBytes = type->getElementSizeBytes();
62 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -070063}
64
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080065Allocation::~Allocation() {
Jason Samsc7cec1e2011-08-18 18:01:33 -070066 freeChildrenUnlocked();
Jason Sams3522f402012-03-23 11:47:26 -070067 setSurfaceTexture(mRSC, NULL);
Jason Samseb4fe182011-05-26 16:33:01 -070068 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070069}
70
Jason Sams366c9c82010-12-08 16:14:36 -080071void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -070072 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -080073}
74
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080075void Allocation::read(void *data) {
Jason Samseb4fe182011-05-26 16:33:01 -070076 memcpy(data, getPtr(), mHal.state.type->getSizeBytes());
Jason Samse579df42009-08-10 14:55:26 -070077}
78
Jason Sams4b45b892010-12-29 14:31:29 -080079void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -080080 uint32_t count, const void *data, size_t sizeBytes) {
81 const size_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -070082
Jason Samseb4fe182011-05-26 16:33:01 -070083 if ((count * eSize) != sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -080084 ALOGE("Allocation::subData called with mismatched size expected %zu, got %zu",
Jason Samseb4fe182011-05-26 16:33:01 -070085 (count * eSize), sizeBytes);
Jason Samsbad80742011-03-16 16:29:28 -070086 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -070087 return;
88 }
Jason Samse3929c92010-08-09 18:13:33 -070089
Jason Samseb4fe182011-05-26 16:33:01 -070090 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
91 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070092}
93
Jason Sams4b45b892010-12-29 14:31:29 -080094void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -080095 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
96 const size_t eSize = mHal.state.elementSizeBytes;
97 const size_t lineSize = eSize * w;
Jason Sams326e0dd2009-05-22 14:03:28 -070098
Steve Blockaf12ac62012-01-06 19:20:56 +000099 //ALOGE("data2d %p, %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -0700100
Jason Samsa2371512011-01-12 13:28:37 -0800101 if ((lineSize * h) != sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800102 ALOGE("Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -0700103 rsAssert(!"Allocation::subData called with mismatched size");
104 return;
105 }
106
Jason Samseb4fe182011-05-26 16:33:01 -0700107 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes);
108 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700109}
110
Jason Sams236385b2011-01-12 14:53:25 -0800111void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
112 uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800113 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700114}
115
Jason Sams4b45b892010-12-29 14:31:29 -0800116void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800117 uint32_t cIdx, size_t sizeBytes) {
118 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700119
Jason Samsbad80742011-03-16 16:29:28 -0700120 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000121 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700122 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
123 return;
124 }
125
Jason Samsbad80742011-03-16 16:29:28 -0700126 if (x >= mHal.state.dimensionX) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000127 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700128 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
129 return;
130 }
131
Jason Samsbad80742011-03-16 16:29:28 -0700132 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800133 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
134 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800135 ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700136 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
137 return;
138 }
139
Jason Samseb4fe182011-05-26 16:33:01 -0700140 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
141 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700142}
143
Jason Sams4b45b892010-12-29 14:31:29 -0800144void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800145 const void *data, uint32_t cIdx, size_t sizeBytes) {
146 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700147
Jason Samsbad80742011-03-16 16:29:28 -0700148 if (x >= mHal.state.dimensionX) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000149 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700150 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
151 return;
152 }
153
Jason Samsbad80742011-03-16 16:29:28 -0700154 if (y >= mHal.state.dimensionY) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000155 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700156 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
157 return;
158 }
159
Jason Samsbad80742011-03-16 16:29:28 -0700160 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000161 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700162 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
163 return;
164 }
165
Jason Samsbad80742011-03-16 16:29:28 -0700166 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800167 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
168 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800169 ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700170 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
171 return;
172 }
173
Jason Samseb4fe182011-05-26 16:33:01 -0700174 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
175 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700176}
177
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800178void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700179 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700180}
Jason Sams326e0dd2009-05-22 14:03:28 -0700181
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800182void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700183 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
184 if (mToDirtyList[ct] == p) {
185 mToDirtyList.removeAt(ct);
186 return;
187 }
188 }
189 rsAssert(0);
190}
191
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800192void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800193 ObjectBase::dumpLOGV(prefix);
194
195 String8 s(prefix);
196 s.append(" type ");
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700197 if (mHal.state.type) {
Jason Samsbad80742011-03-16 16:29:28 -0700198 mHal.state.type->dumpLOGV(s.string());
Jason Samsc21cf402009-11-17 17:26:46 -0800199 }
200
Steve Block65982012011-10-20 11:56:00 +0100201 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Jason Samseb4fe182011-05-26 16:33:01 -0700202 prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800203}
Jason Sams326e0dd2009-05-22 14:03:28 -0700204
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800205uint32_t Allocation::getPackedSize() const {
206 uint32_t numItems = mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes();
207 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
208}
209
210void Allocation::writePackedData(const Type *type,
211 uint8_t *dst, const uint8_t *src, bool dstPadded) {
212 const Element *elem = type->getElement();
213 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
214 uint32_t paddedBytes = elem->getSizeBytes();
215 uint32_t numItems = type->getSizeBytes() / paddedBytes;
216
217 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
218 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
219
220 // no sub-elements
221 uint32_t fieldCount = elem->getFieldCount();
222 if (fieldCount == 0) {
223 for (uint32_t i = 0; i < numItems; i ++) {
224 memcpy(dst, src, unpaddedBytes);
225 src += srcInc;
226 dst += dstInc;
227 }
228 return;
229 }
230
231 // Cache offsets
232 uint32_t *offsetsPadded = new uint32_t[fieldCount];
233 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
234 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
235
236 for (uint32_t i = 0; i < fieldCount; i++) {
237 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
238 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
239 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
240 }
241
242 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
243 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
244
245 // complex elements, need to copy subelem after subelem
246 for (uint32_t i = 0; i < numItems; i ++) {
247 for (uint32_t fI = 0; fI < fieldCount; fI++) {
248 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
249 }
250 src += srcInc;
251 dst += dstInc;
252 }
253
254 delete[] offsetsPadded;
255 delete[] offsetsUnpadded;
256 delete[] sizeUnpadded;
257}
258
Stephen Hines6ae039b2012-01-18 18:46:27 -0800259void Allocation::unpackVec3Allocation(const void *data, size_t dataSize) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800260 const uint8_t *src = (const uint8_t*)data;
261 uint8_t *dst = (uint8_t*)getPtr();
262
263 writePackedData(getType(), dst, src, true);
264}
265
266void Allocation::packVec3Allocation(OStream *stream) const {
267 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
268 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
269 uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes;
270
271 const uint8_t *src = (const uint8_t*)getPtr();
272 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
273
274 writePackedData(getType(), dst, src, false);
275 stream->addByteArray(dst, getPackedSize());
276
277 delete[] dst;
278}
279
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800280void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700281 // Need to identify ourselves
282 stream->addU32((uint32_t)getClassId());
283
284 String8 name(getName());
285 stream->addString(&name);
286
287 // First thing we need to serialize is the type object since it will be needed
288 // to initialize the class
Jason Samsbad80742011-03-16 16:29:28 -0700289 mHal.state.type->serialize(stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700290
Jason Samsbad80742011-03-16 16:29:28 -0700291 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800292 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
293 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700294 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800295 stream->addU32(packedSize);
296 if (dataSize == packedSize) {
297 // Now write the data
298 stream->addByteArray(getPtr(), dataSize);
299 } else {
300 // Now write the data
301 packVec3Allocation(stream);
302 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700303}
304
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800305Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700306 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700307 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800308 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000309 ALOGE("allocation loading skipped due to invalid class id\n");
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700310 return NULL;
311 }
312
313 String8 name;
314 stream->loadString(&name);
315
316 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800317 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700318 return NULL;
319 }
320 type->compute();
321
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800322 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
323 type->decUserRef();
324
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700325 // Number of bytes we wrote out for this allocation
326 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800327 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
328 uint32_t packedSize = alloc->getPackedSize();
329 if (dataSize != type->getSizeBytes() &&
330 dataSize != packedSize) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000331 ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800332 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700333 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700334 return NULL;
335 }
336
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700337 alloc->setName(name.string(), name.size());
338
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800339 if (dataSize == type->getSizeBytes()) {
340 uint32_t count = dataSize / type->getElementSizeBytes();
341 // Read in all of our allocation data
342 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
343 } else {
344 alloc->unpackVec3Allocation(stream->getPtr() + stream->getPos(), dataSize);
345 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700346 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700347
348 return alloc;
349}
350
Jason Samseb4fe182011-05-26 16:33:01 -0700351void Allocation::sendDirty(const Context *rsc) const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700352 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
353 mToDirtyList[ct]->forceDirty();
354 }
Jason Samseb4fe182011-05-26 16:33:01 -0700355 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700356}
Jason Sams326e0dd2009-05-22 14:03:28 -0700357
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800358void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700359 mHal.state.type->incRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700360}
361
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800362void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700363 if (!mHal.state.hasReferences || !getIsScript()) {
364 return;
365 }
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700366 mHal.state.type->decRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700367}
368
Jason Samsc7cec1e2011-08-18 18:01:33 -0700369void Allocation::freeChildrenUnlocked () {
370 decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
371}
372
373bool Allocation::freeChildren() {
374 if (mHal.state.hasReferences) {
375 incSysRef();
376 freeChildrenUnlocked();
377 return decSysRef();
378 }
379 return false;
380}
381
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800382void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700383}
384
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800385void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsbad80742011-03-16 16:29:28 -0700386 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams96abf812010-10-05 13:32:49 -0700387 if (dimX == oldDimX) {
388 return;
389 }
390
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700391 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700392 if (dimX < oldDimX) {
Jason Samseb4fe182011-05-26 16:33:01 -0700393 decRefs(getPtr(), oldDimX - dimX, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700394 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700395 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700396 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700397 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700398}
399
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800400void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000401 ALOGE("not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700402}
403
Jason Sams41e373d2012-01-13 14:01:20 -0800404int32_t Allocation::getSurfaceTextureID(const Context *rsc) {
405 int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this);
406 mHal.state.surfaceTextureID = id;
407 return id;
408}
409
Jason Sams3522f402012-03-23 11:47:26 -0700410void Allocation::setSurfaceTexture(const Context *rsc, SurfaceTexture *st) {
411 if(st != mHal.state.surfaceTexture) {
412 if(mHal.state.surfaceTexture != NULL) {
413 mHal.state.surfaceTexture->decStrong(NULL);
414 }
415 mHal.state.surfaceTexture = st;
416 if(mHal.state.surfaceTexture != NULL) {
417 mHal.state.surfaceTexture->incStrong(NULL);
418 }
419 }
420}
421
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800422void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
423 ANativeWindow *nw = (ANativeWindow *)sur;
424 ANativeWindow *old = mHal.state.wndSurface;
425 if (nw) {
426 nw->incStrong(NULL);
427 }
428 rsc->mHal.funcs.allocation.setSurfaceTexture(rsc, this, nw);
429 mHal.state.wndSurface = nw;
430 if (old) {
431 old->decStrong(NULL);
432 }
433}
434
435void Allocation::ioSend(const Context *rsc) {
436 rsc->mHal.funcs.allocation.ioSend(rsc, this);
437}
438
439void Allocation::ioReceive(const Context *rsc) {
440 rsc->mHal.funcs.allocation.ioReceive(rsc, this);
441}
442
443
Jason Sams326e0dd2009-05-22 14:03:28 -0700444/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700445//
Stephen Hines6a121812011-03-01 17:34:59 -0800446
Jason Sams326e0dd2009-05-22 14:03:28 -0700447namespace android {
448namespace renderscript {
449
Jason Samsc975cf42011-04-28 18:26:48 -0700450static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
451
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800452static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700453 uint32_t w = out.getDimX();
454 uint32_t h = out.getDimY();
455
Jason Samse9f5c532009-07-28 17:20:11 -0700456 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700457 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
458 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
459 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
460
Jason Samse9f5c532009-07-28 17:20:11 -0700461 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700462 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
463 oPtr ++;
464 i1 += 2;
465 i2 += 2;
466 }
467 }
468}
469
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800470static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700471 uint32_t w = out.getDimX();
472 uint32_t h = out.getDimY();
473
Jason Samse9f5c532009-07-28 17:20:11 -0700474 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700475 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
476 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
477 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
478
Jason Samse9f5c532009-07-28 17:20:11 -0700479 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700480 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700481 oPtr ++;
482 i1 += 2;
483 i2 += 2;
484 }
485 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700486}
487
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800488static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800489 uint32_t w = out.getDimX();
490 uint32_t h = out.getDimY();
491
492 for (uint32_t y=0; y < h; y++) {
493 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
494 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
495 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
496
497 for (uint32_t x=0; x < w; x++) {
498 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
499 oPtr ++;
500 i1 += 2;
501 i2 += 2;
502 }
503 }
504}
505
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800506static void mip(const Adapter2D &out, const Adapter2D &in) {
507 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700508 case 32:
509 mip8888(out, in);
510 break;
511 case 16:
512 mip565(out, in);
513 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800514 case 8:
515 mip8(out, in);
516 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700517 }
Jason Samse9f5c532009-07-28 17:20:11 -0700518}
Jason Sams326e0dd2009-05-22 14:03:28 -0700519
Jason Sams366c9c82010-12-08 16:14:36 -0800520void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
521 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700522 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800523 a->syncAll(rsc, src);
524}
525
Jason Samsa2371512011-01-12 13:28:37 -0800526void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700527 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsc975cf42011-04-28 18:26:48 -0700528 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800529}
530
531void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
532 Allocation *texAlloc = static_cast<Allocation *>(va);
533 const Type * t = texAlloc->getType();
534
535 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
536 if (s != dataLen) {
537 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
538 return;
539 }
540
541 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700542}
543
Jason Sams4b45b892010-12-29 14:31:29 -0800544void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700545 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700546 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800547 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700548}
549
Jason Sams4b45b892010-12-29 14:31:29 -0800550void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800551 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700552 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800553 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700554}
555
Jason Sams4b45b892010-12-29 14:31:29 -0800556void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800557 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700558 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800559 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700560}
561
Jason Sams4b45b892010-12-29 14:31:29 -0800562void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700563 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700564 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800565 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700566}
567
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700568void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
Jason Samse579df42009-08-10 14:55:26 -0700569 Allocation *a = static_cast<Allocation *>(va);
570 a->read(data);
571}
572
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800573void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700574 Allocation *a = static_cast<Allocation *>(va);
575 a->resize1D(rsc, dimX);
576}
577
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800578void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700579 Allocation *a = static_cast<Allocation *>(va);
580 a->resize2D(rsc, dimX, dimY);
581}
582
Jason Samsc975cf42011-04-28 18:26:48 -0700583static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800584 Context *rsc = static_cast<Context *>(con);
585 Allocation *texAlloc = static_cast<Allocation *>(va);
586 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
587 for (uint32_t face = 0; face < numFaces; face ++) {
588 Adapter2D adapt(rsc, texAlloc);
589 Adapter2D adapt2(rsc, texAlloc);
590 adapt.setFace(face);
591 adapt2.setFace(face);
592 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
593 adapt.setLOD(lod);
594 adapt2.setLOD(lod + 1);
595 mip(adapt2, adapt);
596 }
597 }
598}
599
Jason Samsc975cf42011-04-28 18:26:48 -0700600RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
601 RsAllocationMipmapControl mips,
Jason Sams179e9a42011-11-23 15:02:15 -0800602 uint32_t usages, uint32_t ptr) {
603 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700604 if (!alloc) {
605 return NULL;
606 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700607 alloc->incUserRef();
608 return alloc;
609}
610
Jason Samsc975cf42011-04-28 18:26:48 -0700611RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
612 RsAllocationMipmapControl mips,
613 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800614 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700615
Jason Sams179e9a42011-11-23 15:02:15 -0800616 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700617 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
618 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000619 ALOGE("Memory allocation failure");
Jason Samsf0c1df42010-10-26 13:09:17 -0700620 return NULL;
621 }
622
Jason Sams366c9c82010-12-08 16:14:36 -0800623 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800624 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700625 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700626 }
627
Jason Samseb4fe182011-05-26 16:33:01 -0700628 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700629 return texAlloc;
630}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800631
Jason Samsc975cf42011-04-28 18:26:48 -0700632RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
633 RsAllocationMipmapControl mips,
634 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800635 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800636
637 // Cubemap allocation's faces should be Width by Width each.
638 // Source data should have 6 * Width by Width pixels
639 // Error checking is done in the java layer
Jason Sams179e9a42011-11-23 15:02:15 -0800640 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800641 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
642 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000643 ALOGE("Memory allocation failure");
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800644 return NULL;
645 }
646
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800647 uint32_t faceSize = t->getDimX();
648 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
649 uint32_t copySize = faceSize * t->getElementSizeBytes();
650
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800651 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800652 for (uint32_t face = 0; face < 6; face ++) {
653 Adapter2D faceAdapter(rsc, texAlloc);
654 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800655
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800656 for (uint32_t dI = 0; dI < faceSize; dI ++) {
657 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
658 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800659
Jason Sams366c9c82010-12-08 16:14:36 -0800660 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800661 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800662 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800663
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800664 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700665 AllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800666 }
667
Jason Samseb4fe182011-05-26 16:33:01 -0700668 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800669 return texAlloc;
670}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800671
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700672void rsi_AllocationCopy2DRange(Context *rsc,
673 RsAllocation dstAlloc,
674 uint32_t dstXoff, uint32_t dstYoff,
675 uint32_t dstMip, uint32_t dstFace,
676 uint32_t width, uint32_t height,
677 RsAllocation srcAlloc,
678 uint32_t srcXoff, uint32_t srcYoff,
679 uint32_t srcMip, uint32_t srcFace) {
680 Allocation *dst = static_cast<Allocation *>(dstAlloc);
681 Allocation *src= static_cast<Allocation *>(srcAlloc);
682 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
683 (RsAllocationCubemapFace)dstFace,
684 width, height,
685 src, srcXoff, srcYoff,srcMip,
686 (RsAllocationCubemapFace)srcFace);
687}
688
Jason Sams41e373d2012-01-13 14:01:20 -0800689int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) {
690 Allocation *alloc = static_cast<Allocation *>(valloc);
691 return alloc->getSurfaceTextureID(rsc);
692}
693
Jason Sams3522f402012-03-23 11:47:26 -0700694void rsi_AllocationGetSurfaceTextureID2(Context *rsc, RsAllocation valloc, void *vst, size_t len) {
695 Allocation *alloc = static_cast<Allocation *>(valloc);
696 alloc->setSurfaceTexture(rsc, static_cast<SurfaceTexture *>(vst));
697}
698
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800699void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
700 Allocation *alloc = static_cast<Allocation *>(valloc);
701 alloc->setSurface(rsc, sur);
702}
703
704void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
705 Allocation *alloc = static_cast<Allocation *>(valloc);
706 alloc->ioSend(rsc);
707}
708
709void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
710 Allocation *alloc = static_cast<Allocation *>(valloc);
711 alloc->ioReceive(rsc);
712}
713
Jason Samsc975cf42011-04-28 18:26:48 -0700714}
715}
716
717const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
718 Allocation *a = static_cast<Allocation *>(va);
719 a->getType()->incUserRef();
720
721 return a->getType();
722}