blob: 83c88fdffacf6c85d5c8f078a633f08c153277a7 [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"
Jason Samseb4fe182011-05-26 16:33:01 -070018#include "rs_hal.h"
19
Jason Sams7ac2a4d2012-02-15 12:04:24 -080020#include "system/window.h"
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070021
Jason Sams326e0dd2009-05-22 14:03:28 -070022using namespace android;
23using namespace android::renderscript;
24
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080025Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080026 RsAllocationMipmapControl mc, void * ptr)
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080027 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080028
Jason Samseb4fe182011-05-26 16:33:01 -070029 memset(&mHal, 0, sizeof(mHal));
30 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsbad80742011-03-16 16:29:28 -070031 mHal.state.usageFlags = usages;
32 mHal.state.mipmapControl = mc;
Jason Sams179e9a42011-11-23 15:02:15 -080033 mHal.state.usrPtr = ptr;
Jason Sams366c9c82010-12-08 16:14:36 -080034
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070035 setType(type);
Jason Samsbad80742011-03-16 16:29:28 -070036 updateCache();
37}
Jason Samsfa84da22010-03-01 15:31:04 -080038
Jason Samseb4fe182011-05-26 16:33:01 -070039Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080040 RsAllocationMipmapControl mc, void * ptr) {
41 Allocation *a = new Allocation(rsc, type, usages, mc, ptr);
Jason Samseb4fe182011-05-26 16:33:01 -070042
43 if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
44 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
45 delete a;
46 return NULL;
47 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -080048
Jason Samseb4fe182011-05-26 16:33:01 -070049 return a;
50}
51
Jason Samsbad80742011-03-16 16:29:28 -070052void Allocation::updateCache() {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070053 const Type *type = mHal.state.type;
Jason Samsbad80742011-03-16 16:29:28 -070054 mHal.state.dimensionX = type->getDimX();
55 mHal.state.dimensionY = type->getDimY();
56 mHal.state.dimensionZ = type->getDimZ();
57 mHal.state.hasFaces = type->getDimFaces();
58 mHal.state.hasMipmaps = type->getDimLOD();
59 mHal.state.elementSizeBytes = type->getElementSizeBytes();
60 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -070061}
62
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080063Allocation::~Allocation() {
Jason Samsc7cec1e2011-08-18 18:01:33 -070064 freeChildrenUnlocked();
Jason Samseb4fe182011-05-26 16:33:01 -070065 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070066}
67
Jason Sams366c9c82010-12-08 16:14:36 -080068void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -070069 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -080070}
71
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080072void Allocation::read(void *data) {
Jason Samseb4fe182011-05-26 16:33:01 -070073 memcpy(data, getPtr(), mHal.state.type->getSizeBytes());
Jason Samse579df42009-08-10 14:55:26 -070074}
75
Jason Sams4b45b892010-12-29 14:31:29 -080076void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -080077 uint32_t count, const void *data, size_t sizeBytes) {
78 const size_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -070079
Jason Samseb4fe182011-05-26 16:33:01 -070080 if ((count * eSize) != sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -080081 ALOGE("Allocation::subData called with mismatched size expected %zu, got %zu",
Jason Samseb4fe182011-05-26 16:33:01 -070082 (count * eSize), sizeBytes);
Jason Samsbad80742011-03-16 16:29:28 -070083 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -070084 return;
85 }
Jason Samse3929c92010-08-09 18:13:33 -070086
Jason Samseb4fe182011-05-26 16:33:01 -070087 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
88 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070089}
90
Jason Sams4b45b892010-12-29 14:31:29 -080091void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -080092 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
93 const size_t eSize = mHal.state.elementSizeBytes;
94 const size_t lineSize = eSize * w;
Jason Sams326e0dd2009-05-22 14:03:28 -070095
Steve Blockaf12ac62012-01-06 19:20:56 +000096 //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 -070097
Jason Samsa2371512011-01-12 13:28:37 -080098 if ((lineSize * h) != sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -080099 ALOGE("Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -0700100 rsAssert(!"Allocation::subData called with mismatched size");
101 return;
102 }
103
Jason Samseb4fe182011-05-26 16:33:01 -0700104 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes);
105 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700106}
107
Jason Sams236385b2011-01-12 14:53:25 -0800108void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
109 uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800110 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700111}
112
Jason Sams4b45b892010-12-29 14:31:29 -0800113void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800114 uint32_t cIdx, size_t sizeBytes) {
115 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700116
Jason Samsbad80742011-03-16 16:29:28 -0700117 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000118 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700119 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
120 return;
121 }
122
Jason Samsbad80742011-03-16 16:29:28 -0700123 if (x >= mHal.state.dimensionX) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000124 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700125 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
126 return;
127 }
128
Jason Samsbad80742011-03-16 16:29:28 -0700129 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800130 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
131 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800132 ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700133 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
134 return;
135 }
136
Jason Samseb4fe182011-05-26 16:33:01 -0700137 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
138 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700139}
140
Jason Sams4b45b892010-12-29 14:31:29 -0800141void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800142 const void *data, uint32_t cIdx, size_t sizeBytes) {
143 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700144
Jason Samsbad80742011-03-16 16:29:28 -0700145 if (x >= mHal.state.dimensionX) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000146 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700147 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
148 return;
149 }
150
Jason Samsbad80742011-03-16 16:29:28 -0700151 if (y >= mHal.state.dimensionY) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000152 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700153 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
154 return;
155 }
156
Jason Samsbad80742011-03-16 16:29:28 -0700157 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000158 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700159 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
160 return;
161 }
162
Jason Samsbad80742011-03-16 16:29:28 -0700163 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800164 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
165 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800166 ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700167 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
168 return;
169 }
170
Jason Samseb4fe182011-05-26 16:33:01 -0700171 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
172 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700173}
174
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800175void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700176 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700177}
Jason Sams326e0dd2009-05-22 14:03:28 -0700178
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800179void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700180 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
181 if (mToDirtyList[ct] == p) {
182 mToDirtyList.removeAt(ct);
183 return;
184 }
185 }
186 rsAssert(0);
187}
188
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800189void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800190 ObjectBase::dumpLOGV(prefix);
191
192 String8 s(prefix);
193 s.append(" type ");
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700194 if (mHal.state.type) {
Jason Samsbad80742011-03-16 16:29:28 -0700195 mHal.state.type->dumpLOGV(s.string());
Jason Samsc21cf402009-11-17 17:26:46 -0800196 }
197
Steve Block65982012011-10-20 11:56:00 +0100198 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Jason Samseb4fe182011-05-26 16:33:01 -0700199 prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800200}
Jason Sams326e0dd2009-05-22 14:03:28 -0700201
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800202uint32_t Allocation::getPackedSize() const {
203 uint32_t numItems = mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes();
204 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
205}
206
207void Allocation::writePackedData(const Type *type,
208 uint8_t *dst, const uint8_t *src, bool dstPadded) {
209 const Element *elem = type->getElement();
210 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
211 uint32_t paddedBytes = elem->getSizeBytes();
212 uint32_t numItems = type->getSizeBytes() / paddedBytes;
213
214 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
215 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
216
217 // no sub-elements
218 uint32_t fieldCount = elem->getFieldCount();
219 if (fieldCount == 0) {
220 for (uint32_t i = 0; i < numItems; i ++) {
221 memcpy(dst, src, unpaddedBytes);
222 src += srcInc;
223 dst += dstInc;
224 }
225 return;
226 }
227
228 // Cache offsets
229 uint32_t *offsetsPadded = new uint32_t[fieldCount];
230 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
231 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
232
233 for (uint32_t i = 0; i < fieldCount; i++) {
234 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
235 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
236 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
237 }
238
239 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
240 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
241
242 // complex elements, need to copy subelem after subelem
243 for (uint32_t i = 0; i < numItems; i ++) {
244 for (uint32_t fI = 0; fI < fieldCount; fI++) {
245 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
246 }
247 src += srcInc;
248 dst += dstInc;
249 }
250
251 delete[] offsetsPadded;
252 delete[] offsetsUnpadded;
253 delete[] sizeUnpadded;
254}
255
Stephen Hines6ae039b2012-01-18 18:46:27 -0800256void Allocation::unpackVec3Allocation(const void *data, size_t dataSize) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800257 const uint8_t *src = (const uint8_t*)data;
258 uint8_t *dst = (uint8_t*)getPtr();
259
260 writePackedData(getType(), dst, src, true);
261}
262
263void Allocation::packVec3Allocation(OStream *stream) const {
264 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
265 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
266 uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes;
267
268 const uint8_t *src = (const uint8_t*)getPtr();
269 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
270
271 writePackedData(getType(), dst, src, false);
272 stream->addByteArray(dst, getPackedSize());
273
274 delete[] dst;
275}
276
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800277void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700278 // Need to identify ourselves
279 stream->addU32((uint32_t)getClassId());
280
281 String8 name(getName());
282 stream->addString(&name);
283
284 // First thing we need to serialize is the type object since it will be needed
285 // to initialize the class
Jason Samsbad80742011-03-16 16:29:28 -0700286 mHal.state.type->serialize(stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700287
Jason Samsbad80742011-03-16 16:29:28 -0700288 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800289 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
290 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700291 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800292 stream->addU32(packedSize);
293 if (dataSize == packedSize) {
294 // Now write the data
295 stream->addByteArray(getPtr(), dataSize);
296 } else {
297 // Now write the data
298 packVec3Allocation(stream);
299 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700300}
301
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800302Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700303 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700304 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800305 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000306 ALOGE("allocation loading skipped due to invalid class id\n");
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700307 return NULL;
308 }
309
310 String8 name;
311 stream->loadString(&name);
312
313 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800314 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700315 return NULL;
316 }
317 type->compute();
318
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800319 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
320 type->decUserRef();
321
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700322 // Number of bytes we wrote out for this allocation
323 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800324 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
325 uint32_t packedSize = alloc->getPackedSize();
326 if (dataSize != type->getSizeBytes() &&
327 dataSize != packedSize) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000328 ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800329 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700330 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700331 return NULL;
332 }
333
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700334 alloc->setName(name.string(), name.size());
335
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800336 if (dataSize == type->getSizeBytes()) {
337 uint32_t count = dataSize / type->getElementSizeBytes();
338 // Read in all of our allocation data
339 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
340 } else {
341 alloc->unpackVec3Allocation(stream->getPtr() + stream->getPos(), dataSize);
342 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700343 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700344
345 return alloc;
346}
347
Jason Samseb4fe182011-05-26 16:33:01 -0700348void Allocation::sendDirty(const Context *rsc) const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700349 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
350 mToDirtyList[ct]->forceDirty();
351 }
Jason Samseb4fe182011-05-26 16:33:01 -0700352 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700353}
Jason Sams326e0dd2009-05-22 14:03:28 -0700354
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800355void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700356 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700357 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700358 uint32_t stride = e->getSizeBytes();
359
Jason Sams96abf812010-10-05 13:32:49 -0700360 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700361 while (ct > 0) {
362 e->incRefs(p);
363 ct --;
364 p += stride;
365 }
366}
367
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800368void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700369 if (!mHal.state.hasReferences || !getIsScript()) {
370 return;
371 }
Jason Samse3929c92010-08-09 18:13:33 -0700372 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700373 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700374 uint32_t stride = e->getSizeBytes();
375
Jason Sams96abf812010-10-05 13:32:49 -0700376 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700377 while (ct > 0) {
378 e->decRefs(p);
379 ct --;
380 p += stride;
381 }
382}
383
Jason Samsc7cec1e2011-08-18 18:01:33 -0700384void Allocation::freeChildrenUnlocked () {
385 decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
386}
387
388bool Allocation::freeChildren() {
389 if (mHal.state.hasReferences) {
390 incSysRef();
391 freeChildrenUnlocked();
392 return decSysRef();
393 }
394 return false;
395}
396
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800397void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700398}
399
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800400void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsbad80742011-03-16 16:29:28 -0700401 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams96abf812010-10-05 13:32:49 -0700402 if (dimX == oldDimX) {
403 return;
404 }
405
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700406 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700407 if (dimX < oldDimX) {
Jason Samseb4fe182011-05-26 16:33:01 -0700408 decRefs(getPtr(), oldDimX - dimX, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700409 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700410 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700411 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700412 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700413}
414
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800415void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000416 ALOGE("not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700417}
418
Jason Sams41e373d2012-01-13 14:01:20 -0800419int32_t Allocation::getSurfaceTextureID(const Context *rsc) {
420 int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this);
421 mHal.state.surfaceTextureID = id;
422 return id;
423}
424
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800425void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
426 ANativeWindow *nw = (ANativeWindow *)sur;
427 ANativeWindow *old = mHal.state.wndSurface;
428 if (nw) {
429 nw->incStrong(NULL);
430 }
431 rsc->mHal.funcs.allocation.setSurfaceTexture(rsc, this, nw);
432 mHal.state.wndSurface = nw;
433 if (old) {
434 old->decStrong(NULL);
435 }
436}
437
438void Allocation::ioSend(const Context *rsc) {
439 rsc->mHal.funcs.allocation.ioSend(rsc, this);
440}
441
442void Allocation::ioReceive(const Context *rsc) {
443 rsc->mHal.funcs.allocation.ioReceive(rsc, this);
444}
445
446
Jason Sams326e0dd2009-05-22 14:03:28 -0700447/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700448//
Stephen Hines6a121812011-03-01 17:34:59 -0800449
Jason Sams326e0dd2009-05-22 14:03:28 -0700450namespace android {
451namespace renderscript {
452
Jason Samsc975cf42011-04-28 18:26:48 -0700453static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
454
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800455static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700456 uint32_t w = out.getDimX();
457 uint32_t h = out.getDimY();
458
Jason Samse9f5c532009-07-28 17:20:11 -0700459 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700460 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
461 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
462 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
463
Jason Samse9f5c532009-07-28 17:20:11 -0700464 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700465 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
466 oPtr ++;
467 i1 += 2;
468 i2 += 2;
469 }
470 }
471}
472
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800473static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700474 uint32_t w = out.getDimX();
475 uint32_t h = out.getDimY();
476
Jason Samse9f5c532009-07-28 17:20:11 -0700477 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700478 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
479 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
480 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
481
Jason Samse9f5c532009-07-28 17:20:11 -0700482 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700483 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700484 oPtr ++;
485 i1 += 2;
486 i2 += 2;
487 }
488 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700489}
490
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800491static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800492 uint32_t w = out.getDimX();
493 uint32_t h = out.getDimY();
494
495 for (uint32_t y=0; y < h; y++) {
496 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
497 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
498 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
499
500 for (uint32_t x=0; x < w; x++) {
501 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
502 oPtr ++;
503 i1 += 2;
504 i2 += 2;
505 }
506 }
507}
508
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800509static void mip(const Adapter2D &out, const Adapter2D &in) {
510 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700511 case 32:
512 mip8888(out, in);
513 break;
514 case 16:
515 mip565(out, in);
516 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800517 case 8:
518 mip8(out, in);
519 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700520 }
Jason Samse9f5c532009-07-28 17:20:11 -0700521}
Jason Sams326e0dd2009-05-22 14:03:28 -0700522
Jason Sams366c9c82010-12-08 16:14:36 -0800523void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
524 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700525 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800526 a->syncAll(rsc, src);
527}
528
Jason Samsa2371512011-01-12 13:28:37 -0800529void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700530 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsc975cf42011-04-28 18:26:48 -0700531 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800532}
533
534void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
535 Allocation *texAlloc = static_cast<Allocation *>(va);
536 const Type * t = texAlloc->getType();
537
538 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
539 if (s != dataLen) {
540 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
541 return;
542 }
543
544 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700545}
546
Jason Sams4b45b892010-12-29 14:31:29 -0800547void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700548 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700549 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800550 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700551}
552
Jason Sams4b45b892010-12-29 14:31:29 -0800553void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800554 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700555 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800556 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700557}
558
Jason Sams4b45b892010-12-29 14:31:29 -0800559void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800560 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700561 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800562 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700563}
564
Jason Sams4b45b892010-12-29 14:31:29 -0800565void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700566 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700567 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800568 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700569}
570
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700571void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
Jason Samse579df42009-08-10 14:55:26 -0700572 Allocation *a = static_cast<Allocation *>(va);
573 a->read(data);
574}
575
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800576void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700577 Allocation *a = static_cast<Allocation *>(va);
578 a->resize1D(rsc, dimX);
579}
580
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800581void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700582 Allocation *a = static_cast<Allocation *>(va);
583 a->resize2D(rsc, dimX, dimY);
584}
585
Jason Samsc975cf42011-04-28 18:26:48 -0700586static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800587 Context *rsc = static_cast<Context *>(con);
588 Allocation *texAlloc = static_cast<Allocation *>(va);
589 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
590 for (uint32_t face = 0; face < numFaces; face ++) {
591 Adapter2D adapt(rsc, texAlloc);
592 Adapter2D adapt2(rsc, texAlloc);
593 adapt.setFace(face);
594 adapt2.setFace(face);
595 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
596 adapt.setLOD(lod);
597 adapt2.setLOD(lod + 1);
598 mip(adapt2, adapt);
599 }
600 }
601}
602
Jason Samsc975cf42011-04-28 18:26:48 -0700603RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
604 RsAllocationMipmapControl mips,
Jason Sams179e9a42011-11-23 15:02:15 -0800605 uint32_t usages, uint32_t ptr) {
606 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700607 if (!alloc) {
608 return NULL;
609 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700610 alloc->incUserRef();
611 return alloc;
612}
613
Jason Samsc975cf42011-04-28 18:26:48 -0700614RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
615 RsAllocationMipmapControl mips,
616 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800617 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700618
Jason Sams179e9a42011-11-23 15:02:15 -0800619 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700620 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
621 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000622 ALOGE("Memory allocation failure");
Jason Samsf0c1df42010-10-26 13:09:17 -0700623 return NULL;
624 }
625
Jason Sams366c9c82010-12-08 16:14:36 -0800626 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800627 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700628 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700629 }
630
Jason Samseb4fe182011-05-26 16:33:01 -0700631 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700632 return texAlloc;
633}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800634
Jason Samsc975cf42011-04-28 18:26:48 -0700635RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
636 RsAllocationMipmapControl mips,
637 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800638 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800639
640 // Cubemap allocation's faces should be Width by Width each.
641 // Source data should have 6 * Width by Width pixels
642 // Error checking is done in the java layer
Jason Sams179e9a42011-11-23 15:02:15 -0800643 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800644 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
645 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000646 ALOGE("Memory allocation failure");
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800647 return NULL;
648 }
649
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800650 uint32_t faceSize = t->getDimX();
651 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
652 uint32_t copySize = faceSize * t->getElementSizeBytes();
653
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800654 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800655 for (uint32_t face = 0; face < 6; face ++) {
656 Adapter2D faceAdapter(rsc, texAlloc);
657 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800658
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800659 for (uint32_t dI = 0; dI < faceSize; dI ++) {
660 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
661 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800662
Jason Sams366c9c82010-12-08 16:14:36 -0800663 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800664 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800665 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800666
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800667 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700668 AllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800669 }
670
Jason Samseb4fe182011-05-26 16:33:01 -0700671 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800672 return texAlloc;
673}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800674
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700675void rsi_AllocationCopy2DRange(Context *rsc,
676 RsAllocation dstAlloc,
677 uint32_t dstXoff, uint32_t dstYoff,
678 uint32_t dstMip, uint32_t dstFace,
679 uint32_t width, uint32_t height,
680 RsAllocation srcAlloc,
681 uint32_t srcXoff, uint32_t srcYoff,
682 uint32_t srcMip, uint32_t srcFace) {
683 Allocation *dst = static_cast<Allocation *>(dstAlloc);
684 Allocation *src= static_cast<Allocation *>(srcAlloc);
685 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
686 (RsAllocationCubemapFace)dstFace,
687 width, height,
688 src, srcXoff, srcYoff,srcMip,
689 (RsAllocationCubemapFace)srcFace);
690}
691
Jason Sams41e373d2012-01-13 14:01:20 -0800692int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) {
693 Allocation *alloc = static_cast<Allocation *>(valloc);
694 return alloc->getSurfaceTextureID(rsc);
695}
696
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800697void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
698 Allocation *alloc = static_cast<Allocation *>(valloc);
699 alloc->setSurface(rsc, sur);
700}
701
702void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
703 Allocation *alloc = static_cast<Allocation *>(valloc);
704 alloc->ioSend(rsc);
705}
706
707void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
708 Allocation *alloc = static_cast<Allocation *>(valloc);
709 alloc->ioReceive(rsc);
710}
711
Jason Samsc975cf42011-04-28 18:26:48 -0700712}
713}
714
715const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
716 Allocation *a = static_cast<Allocation *>(va);
717 a->getType()->incUserRef();
718
719 return a->getType();
720}