blob: a404c49cb1aae0b70b856a21b6938a1a70c52482 [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"
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070023
Jason Sams326e0dd2009-05-22 14:03:28 -070024using namespace android;
25using namespace android::renderscript;
26
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080027Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080028 RsAllocationMipmapControl mc, void * ptr)
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080029 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080030
Jason Samseb4fe182011-05-26 16:33:01 -070031 memset(&mHal, 0, sizeof(mHal));
32 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsbad80742011-03-16 16:29:28 -070033 mHal.state.usageFlags = usages;
34 mHal.state.mipmapControl = mc;
Jason Sams179e9a42011-11-23 15:02:15 -080035 mHal.state.usrPtr = ptr;
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 Samseb4fe182011-05-26 16:33:01 -070067 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070068}
69
Jason Sams366c9c82010-12-08 16:14:36 -080070void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -070071 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -080072}
73
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080074void Allocation::read(void *data) {
Jason Samseb4fe182011-05-26 16:33:01 -070075 memcpy(data, getPtr(), mHal.state.type->getSizeBytes());
Jason Samse579df42009-08-10 14:55:26 -070076}
77
Jason Sams4b45b892010-12-29 14:31:29 -080078void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -080079 uint32_t count, const void *data, size_t sizeBytes) {
80 const size_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -070081
Jason Samseb4fe182011-05-26 16:33:01 -070082 if ((count * eSize) != sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -080083 ALOGE("Allocation::subData called with mismatched size expected %zu, got %zu",
Jason Samseb4fe182011-05-26 16:33:01 -070084 (count * eSize), sizeBytes);
Jason Samsbad80742011-03-16 16:29:28 -070085 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -070086 return;
87 }
Jason Samse3929c92010-08-09 18:13:33 -070088
Jason Samseb4fe182011-05-26 16:33:01 -070089 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
90 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070091}
92
Jason Sams4b45b892010-12-29 14:31:29 -080093void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -080094 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
95 const size_t eSize = mHal.state.elementSizeBytes;
96 const size_t lineSize = eSize * w;
Jason Sams326e0dd2009-05-22 14:03:28 -070097
Steve Blockaf12ac62012-01-06 19:20:56 +000098 //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 -070099
Jason Samsa2371512011-01-12 13:28:37 -0800100 if ((lineSize * h) != sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800101 ALOGE("Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -0700102 rsAssert(!"Allocation::subData called with mismatched size");
103 return;
104 }
105
Jason Samseb4fe182011-05-26 16:33:01 -0700106 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes);
107 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700108}
109
Jason Sams236385b2011-01-12 14:53:25 -0800110void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
111 uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800112 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700113}
114
Jason Sams4b45b892010-12-29 14:31:29 -0800115void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800116 uint32_t cIdx, size_t sizeBytes) {
117 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700118
Jason Samsbad80742011-03-16 16:29:28 -0700119 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000120 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700121 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
122 return;
123 }
124
Jason Samsbad80742011-03-16 16:29:28 -0700125 if (x >= mHal.state.dimensionX) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000126 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700127 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
128 return;
129 }
130
Jason Samsbad80742011-03-16 16:29:28 -0700131 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800132 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
133 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800134 ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700135 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
136 return;
137 }
138
Jason Samseb4fe182011-05-26 16:33:01 -0700139 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
140 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700141}
142
Jason Sams4b45b892010-12-29 14:31:29 -0800143void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800144 const void *data, uint32_t cIdx, size_t sizeBytes) {
145 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700146
Jason Samsbad80742011-03-16 16:29:28 -0700147 if (x >= mHal.state.dimensionX) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000148 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700149 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
150 return;
151 }
152
Jason Samsbad80742011-03-16 16:29:28 -0700153 if (y >= mHal.state.dimensionY) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000154 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700155 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
156 return;
157 }
158
Jason Samsbad80742011-03-16 16:29:28 -0700159 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000160 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700161 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
162 return;
163 }
164
Jason Samsbad80742011-03-16 16:29:28 -0700165 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800166 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
167 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800168 ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700169 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
170 return;
171 }
172
Jason Samseb4fe182011-05-26 16:33:01 -0700173 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
174 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700175}
176
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800177void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700178 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700179}
Jason Sams326e0dd2009-05-22 14:03:28 -0700180
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800181void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700182 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
183 if (mToDirtyList[ct] == p) {
184 mToDirtyList.removeAt(ct);
185 return;
186 }
187 }
188 rsAssert(0);
189}
190
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800191void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800192 ObjectBase::dumpLOGV(prefix);
193
194 String8 s(prefix);
195 s.append(" type ");
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700196 if (mHal.state.type) {
Jason Samsbad80742011-03-16 16:29:28 -0700197 mHal.state.type->dumpLOGV(s.string());
Jason Samsc21cf402009-11-17 17:26:46 -0800198 }
199
Steve Block65982012011-10-20 11:56:00 +0100200 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Jason Samseb4fe182011-05-26 16:33:01 -0700201 prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800202}
Jason Sams326e0dd2009-05-22 14:03:28 -0700203
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800204uint32_t Allocation::getPackedSize() const {
205 uint32_t numItems = mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes();
206 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
207}
208
209void Allocation::writePackedData(const Type *type,
210 uint8_t *dst, const uint8_t *src, bool dstPadded) {
211 const Element *elem = type->getElement();
212 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
213 uint32_t paddedBytes = elem->getSizeBytes();
214 uint32_t numItems = type->getSizeBytes() / paddedBytes;
215
216 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
217 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
218
219 // no sub-elements
220 uint32_t fieldCount = elem->getFieldCount();
221 if (fieldCount == 0) {
222 for (uint32_t i = 0; i < numItems; i ++) {
223 memcpy(dst, src, unpaddedBytes);
224 src += srcInc;
225 dst += dstInc;
226 }
227 return;
228 }
229
230 // Cache offsets
231 uint32_t *offsetsPadded = new uint32_t[fieldCount];
232 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
233 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
234
235 for (uint32_t i = 0; i < fieldCount; i++) {
236 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
237 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
238 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
239 }
240
241 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
242 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
243
244 // complex elements, need to copy subelem after subelem
245 for (uint32_t i = 0; i < numItems; i ++) {
246 for (uint32_t fI = 0; fI < fieldCount; fI++) {
247 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
248 }
249 src += srcInc;
250 dst += dstInc;
251 }
252
253 delete[] offsetsPadded;
254 delete[] offsetsUnpadded;
255 delete[] sizeUnpadded;
256}
257
Stephen Hines6ae039b2012-01-18 18:46:27 -0800258void Allocation::unpackVec3Allocation(const void *data, size_t dataSize) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800259 const uint8_t *src = (const uint8_t*)data;
260 uint8_t *dst = (uint8_t*)getPtr();
261
262 writePackedData(getType(), dst, src, true);
263}
264
265void Allocation::packVec3Allocation(OStream *stream) const {
266 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
267 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
268 uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes;
269
270 const uint8_t *src = (const uint8_t*)getPtr();
271 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
272
273 writePackedData(getType(), dst, src, false);
274 stream->addByteArray(dst, getPackedSize());
275
276 delete[] dst;
277}
278
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800279void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700280 // Need to identify ourselves
281 stream->addU32((uint32_t)getClassId());
282
283 String8 name(getName());
284 stream->addString(&name);
285
286 // First thing we need to serialize is the type object since it will be needed
287 // to initialize the class
Jason Samsbad80742011-03-16 16:29:28 -0700288 mHal.state.type->serialize(stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700289
Jason Samsbad80742011-03-16 16:29:28 -0700290 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800291 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
292 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700293 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800294 stream->addU32(packedSize);
295 if (dataSize == packedSize) {
296 // Now write the data
297 stream->addByteArray(getPtr(), dataSize);
298 } else {
299 // Now write the data
300 packVec3Allocation(stream);
301 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700302}
303
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800304Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700305 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700306 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800307 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000308 ALOGE("allocation loading skipped due to invalid class id\n");
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700309 return NULL;
310 }
311
312 String8 name;
313 stream->loadString(&name);
314
315 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800316 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700317 return NULL;
318 }
319 type->compute();
320
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800321 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
322 type->decUserRef();
323
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700324 // Number of bytes we wrote out for this allocation
325 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800326 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
327 uint32_t packedSize = alloc->getPackedSize();
328 if (dataSize != type->getSizeBytes() &&
329 dataSize != packedSize) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000330 ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800331 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700332 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700333 return NULL;
334 }
335
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700336 alloc->setName(name.string(), name.size());
337
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800338 if (dataSize == type->getSizeBytes()) {
339 uint32_t count = dataSize / type->getElementSizeBytes();
340 // Read in all of our allocation data
341 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
342 } else {
343 alloc->unpackVec3Allocation(stream->getPtr() + stream->getPos(), dataSize);
344 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700345 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700346
347 return alloc;
348}
349
Jason Samseb4fe182011-05-26 16:33:01 -0700350void Allocation::sendDirty(const Context *rsc) const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700351 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
352 mToDirtyList[ct]->forceDirty();
353 }
Jason Samseb4fe182011-05-26 16:33:01 -0700354 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700355}
Jason Sams326e0dd2009-05-22 14:03:28 -0700356
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800357void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700358 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700359 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700360 uint32_t stride = e->getSizeBytes();
361
Jason Sams96abf812010-10-05 13:32:49 -0700362 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700363 while (ct > 0) {
364 e->incRefs(p);
365 ct --;
366 p += stride;
367 }
368}
369
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800370void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700371 if (!mHal.state.hasReferences || !getIsScript()) {
372 return;
373 }
Jason Samse3929c92010-08-09 18:13:33 -0700374 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700375 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700376 uint32_t stride = e->getSizeBytes();
377
Jason Sams96abf812010-10-05 13:32:49 -0700378 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700379 while (ct > 0) {
380 e->decRefs(p);
381 ct --;
382 p += stride;
383 }
384}
385
Jason Samsc7cec1e2011-08-18 18:01:33 -0700386void Allocation::freeChildrenUnlocked () {
387 decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
388}
389
390bool Allocation::freeChildren() {
391 if (mHal.state.hasReferences) {
392 incSysRef();
393 freeChildrenUnlocked();
394 return decSysRef();
395 }
396 return false;
397}
398
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800399void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700400}
401
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800402void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsbad80742011-03-16 16:29:28 -0700403 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams96abf812010-10-05 13:32:49 -0700404 if (dimX == oldDimX) {
405 return;
406 }
407
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700408 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700409 if (dimX < oldDimX) {
Jason Samseb4fe182011-05-26 16:33:01 -0700410 decRefs(getPtr(), oldDimX - dimX, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700411 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700412 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700413 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700414 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700415}
416
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800417void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000418 ALOGE("not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700419}
420
Jason Sams41e373d2012-01-13 14:01:20 -0800421int32_t Allocation::getSurfaceTextureID(const Context *rsc) {
422 int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this);
423 mHal.state.surfaceTextureID = id;
424 return id;
425}
426
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800427void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
428 ANativeWindow *nw = (ANativeWindow *)sur;
429 ANativeWindow *old = mHal.state.wndSurface;
430 if (nw) {
431 nw->incStrong(NULL);
432 }
433 rsc->mHal.funcs.allocation.setSurfaceTexture(rsc, this, nw);
434 mHal.state.wndSurface = nw;
435 if (old) {
436 old->decStrong(NULL);
437 }
438}
439
440void Allocation::ioSend(const Context *rsc) {
441 rsc->mHal.funcs.allocation.ioSend(rsc, this);
442}
443
444void Allocation::ioReceive(const Context *rsc) {
445 rsc->mHal.funcs.allocation.ioReceive(rsc, this);
446}
447
448
Jason Sams326e0dd2009-05-22 14:03:28 -0700449/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700450//
Stephen Hines6a121812011-03-01 17:34:59 -0800451
Jason Sams326e0dd2009-05-22 14:03:28 -0700452namespace android {
453namespace renderscript {
454
Jason Samsc975cf42011-04-28 18:26:48 -0700455static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
456
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800457static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700458 uint32_t w = out.getDimX();
459 uint32_t h = out.getDimY();
460
Jason Samse9f5c532009-07-28 17:20:11 -0700461 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700462 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
463 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
464 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
465
Jason Samse9f5c532009-07-28 17:20:11 -0700466 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700467 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
468 oPtr ++;
469 i1 += 2;
470 i2 += 2;
471 }
472 }
473}
474
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800475static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700476 uint32_t w = out.getDimX();
477 uint32_t h = out.getDimY();
478
Jason Samse9f5c532009-07-28 17:20:11 -0700479 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700480 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
481 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
482 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
483
Jason Samse9f5c532009-07-28 17:20:11 -0700484 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700485 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700486 oPtr ++;
487 i1 += 2;
488 i2 += 2;
489 }
490 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700491}
492
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800493static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800494 uint32_t w = out.getDimX();
495 uint32_t h = out.getDimY();
496
497 for (uint32_t y=0; y < h; y++) {
498 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
499 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
500 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
501
502 for (uint32_t x=0; x < w; x++) {
503 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
504 oPtr ++;
505 i1 += 2;
506 i2 += 2;
507 }
508 }
509}
510
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800511static void mip(const Adapter2D &out, const Adapter2D &in) {
512 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700513 case 32:
514 mip8888(out, in);
515 break;
516 case 16:
517 mip565(out, in);
518 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800519 case 8:
520 mip8(out, in);
521 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700522 }
Jason Samse9f5c532009-07-28 17:20:11 -0700523}
Jason Sams326e0dd2009-05-22 14:03:28 -0700524
Jason Sams366c9c82010-12-08 16:14:36 -0800525void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
526 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700527 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800528 a->syncAll(rsc, src);
529}
530
Jason Samsa2371512011-01-12 13:28:37 -0800531void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700532 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsc975cf42011-04-28 18:26:48 -0700533 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800534}
535
536void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
537 Allocation *texAlloc = static_cast<Allocation *>(va);
538 const Type * t = texAlloc->getType();
539
540 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
541 if (s != dataLen) {
542 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
543 return;
544 }
545
546 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700547}
548
Jason Sams4b45b892010-12-29 14:31:29 -0800549void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700550 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700551 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800552 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700553}
554
Jason Sams4b45b892010-12-29 14:31:29 -0800555void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800556 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700557 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800558 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700559}
560
Jason Sams4b45b892010-12-29 14:31:29 -0800561void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800562 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700563 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800564 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700565}
566
Jason Sams4b45b892010-12-29 14:31:29 -0800567void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700568 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700569 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800570 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700571}
572
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700573void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
Jason Samse579df42009-08-10 14:55:26 -0700574 Allocation *a = static_cast<Allocation *>(va);
575 a->read(data);
576}
577
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800578void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700579 Allocation *a = static_cast<Allocation *>(va);
580 a->resize1D(rsc, dimX);
581}
582
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800583void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700584 Allocation *a = static_cast<Allocation *>(va);
585 a->resize2D(rsc, dimX, dimY);
586}
587
Jason Samsc975cf42011-04-28 18:26:48 -0700588static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800589 Context *rsc = static_cast<Context *>(con);
590 Allocation *texAlloc = static_cast<Allocation *>(va);
591 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
592 for (uint32_t face = 0; face < numFaces; face ++) {
593 Adapter2D adapt(rsc, texAlloc);
594 Adapter2D adapt2(rsc, texAlloc);
595 adapt.setFace(face);
596 adapt2.setFace(face);
597 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
598 adapt.setLOD(lod);
599 adapt2.setLOD(lod + 1);
600 mip(adapt2, adapt);
601 }
602 }
603}
604
Jason Samsc975cf42011-04-28 18:26:48 -0700605RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
606 RsAllocationMipmapControl mips,
Jason Sams179e9a42011-11-23 15:02:15 -0800607 uint32_t usages, uint32_t ptr) {
608 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700609 if (!alloc) {
610 return NULL;
611 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700612 alloc->incUserRef();
613 return alloc;
614}
615
Jason Samsc975cf42011-04-28 18:26:48 -0700616RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
617 RsAllocationMipmapControl mips,
618 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800619 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700620
Jason Sams179e9a42011-11-23 15:02:15 -0800621 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700622 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
623 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000624 ALOGE("Memory allocation failure");
Jason Samsf0c1df42010-10-26 13:09:17 -0700625 return NULL;
626 }
627
Jason Sams366c9c82010-12-08 16:14:36 -0800628 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800629 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700630 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700631 }
632
Jason Samseb4fe182011-05-26 16:33:01 -0700633 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700634 return texAlloc;
635}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800636
Jason Samsc975cf42011-04-28 18:26:48 -0700637RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
638 RsAllocationMipmapControl mips,
639 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800640 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800641
642 // Cubemap allocation's faces should be Width by Width each.
643 // Source data should have 6 * Width by Width pixels
644 // Error checking is done in the java layer
Jason Sams179e9a42011-11-23 15:02:15 -0800645 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800646 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
647 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000648 ALOGE("Memory allocation failure");
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800649 return NULL;
650 }
651
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800652 uint32_t faceSize = t->getDimX();
653 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
654 uint32_t copySize = faceSize * t->getElementSizeBytes();
655
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800656 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800657 for (uint32_t face = 0; face < 6; face ++) {
658 Adapter2D faceAdapter(rsc, texAlloc);
659 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800660
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800661 for (uint32_t dI = 0; dI < faceSize; dI ++) {
662 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
663 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800664
Jason Sams366c9c82010-12-08 16:14:36 -0800665 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800666 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800667 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800668
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800669 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700670 AllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800671 }
672
Jason Samseb4fe182011-05-26 16:33:01 -0700673 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800674 return texAlloc;
675}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800676
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700677void rsi_AllocationCopy2DRange(Context *rsc,
678 RsAllocation dstAlloc,
679 uint32_t dstXoff, uint32_t dstYoff,
680 uint32_t dstMip, uint32_t dstFace,
681 uint32_t width, uint32_t height,
682 RsAllocation srcAlloc,
683 uint32_t srcXoff, uint32_t srcYoff,
684 uint32_t srcMip, uint32_t srcFace) {
685 Allocation *dst = static_cast<Allocation *>(dstAlloc);
686 Allocation *src= static_cast<Allocation *>(srcAlloc);
687 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
688 (RsAllocationCubemapFace)dstFace,
689 width, height,
690 src, srcXoff, srcYoff,srcMip,
691 (RsAllocationCubemapFace)srcFace);
692}
693
Jason Sams41e373d2012-01-13 14:01:20 -0800694int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) {
695 Allocation *alloc = static_cast<Allocation *>(valloc);
696 return alloc->getSurfaceTextureID(rsc);
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}