blob: 1cf386ea16131c871f317d48c33bea4ad07fc343 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jason 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
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070020
Jason Sams326e0dd2009-05-22 14:03:28 -070021using namespace android;
22using namespace android::renderscript;
23
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080024Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080025 RsAllocationMipmapControl mc, void * ptr)
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080026 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080027
Jason Samseb4fe182011-05-26 16:33:01 -070028 memset(&mHal, 0, sizeof(mHal));
29 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsbad80742011-03-16 16:29:28 -070030 mHal.state.usageFlags = usages;
31 mHal.state.mipmapControl = mc;
Jason Sams179e9a42011-11-23 15:02:15 -080032 mHal.state.usrPtr = ptr;
Jason Sams366c9c82010-12-08 16:14:36 -080033
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070034 setType(type);
Jason Samsbad80742011-03-16 16:29:28 -070035 updateCache();
36}
Jason Samsfa84da22010-03-01 15:31:04 -080037
Jason Samseb4fe182011-05-26 16:33:01 -070038Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080039 RsAllocationMipmapControl mc, void * ptr) {
40 Allocation *a = new Allocation(rsc, type, usages, mc, ptr);
Jason Samseb4fe182011-05-26 16:33:01 -070041
42 if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
43 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
44 delete a;
45 return NULL;
46 }
47 return a;
48}
49
Jason Samsbad80742011-03-16 16:29:28 -070050void Allocation::updateCache() {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070051 const Type *type = mHal.state.type;
Jason Samsbad80742011-03-16 16:29:28 -070052 mHal.state.dimensionX = type->getDimX();
53 mHal.state.dimensionY = type->getDimY();
54 mHal.state.dimensionZ = type->getDimZ();
55 mHal.state.hasFaces = type->getDimFaces();
56 mHal.state.hasMipmaps = type->getDimLOD();
57 mHal.state.elementSizeBytes = type->getElementSizeBytes();
58 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -070059}
60
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080061Allocation::~Allocation() {
Jason Samsc7cec1e2011-08-18 18:01:33 -070062 freeChildrenUnlocked();
Jason Samseb4fe182011-05-26 16:33:01 -070063 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070064}
65
Jason Sams366c9c82010-12-08 16:14:36 -080066void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -070067 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -080068}
69
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080070void Allocation::read(void *data) {
Jason Samseb4fe182011-05-26 16:33:01 -070071 memcpy(data, getPtr(), mHal.state.type->getSizeBytes());
Jason Samse579df42009-08-10 14:55:26 -070072}
73
Jason Sams4b45b892010-12-29 14:31:29 -080074void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
75 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -070076 const uint32_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -070077
Jason Samseb4fe182011-05-26 16:33:01 -070078 if ((count * eSize) != sizeBytes) {
Steve Blockaf12ac62012-01-06 19:20:56 +000079 ALOGE("Allocation::subData called with mismatched size expected %i, got %i",
Jason Samseb4fe182011-05-26 16:33:01 -070080 (count * eSize), sizeBytes);
Jason Samsbad80742011-03-16 16:29:28 -070081 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -070082 return;
83 }
Jason Samse3929c92010-08-09 18:13:33 -070084
Jason Samseb4fe182011-05-26 16:33:01 -070085 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
86 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070087}
88
Jason Sams4b45b892010-12-29 14:31:29 -080089void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080090 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -070091 const uint32_t eSize = mHal.state.elementSizeBytes;
92 const uint32_t lineSize = eSize * w;
Jason Sams326e0dd2009-05-22 14:03:28 -070093
Steve Blockaf12ac62012-01-06 19:20:56 +000094 //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 -070095
Jason Samsa2371512011-01-12 13:28:37 -080096 if ((lineSize * h) != sizeBytes) {
Steve Blockaf12ac62012-01-06 19:20:56 +000097 ALOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -070098 rsAssert(!"Allocation::subData called with mismatched size");
99 return;
100 }
101
Jason Samseb4fe182011-05-26 16:33:01 -0700102 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes);
103 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700104}
105
Jason Sams236385b2011-01-12 14:53:25 -0800106void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
107 uint32_t lod, RsAllocationCubemapFace face,
108 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700109}
110
Jason Sams4b45b892010-12-29 14:31:29 -0800111void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800112 uint32_t cIdx, uint32_t sizeBytes) {
Jason Samsbad80742011-03-16 16:29:28 -0700113 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700114
Jason Samsbad80742011-03-16 16:29:28 -0700115 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000116 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700117 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
118 return;
119 }
120
Jason Samsbad80742011-03-16 16:29:28 -0700121 if (x >= mHal.state.dimensionX) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000122 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700123 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
124 return;
125 }
126
Jason Samsbad80742011-03-16 16:29:28 -0700127 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700128 if (sizeBytes != e->getSizeBytes()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000129 ALOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700130 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
131 return;
132 }
133
Jason Samseb4fe182011-05-26 16:33:01 -0700134 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
135 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700136}
137
Jason Sams4b45b892010-12-29 14:31:29 -0800138void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800139 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Samsbad80742011-03-16 16:29:28 -0700140 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700141
Jason Samsbad80742011-03-16 16:29:28 -0700142 if (x >= mHal.state.dimensionX) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000143 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700144 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
145 return;
146 }
147
Jason Samsbad80742011-03-16 16:29:28 -0700148 if (y >= mHal.state.dimensionY) {
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 (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000155 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700156 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
157 return;
158 }
159
Jason Samsbad80742011-03-16 16:29:28 -0700160 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700161
162 if (sizeBytes != e->getSizeBytes()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000163 ALOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700164 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
165 return;
166 }
167
Jason Samseb4fe182011-05-26 16:33:01 -0700168 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
169 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700170}
171
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800172void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700173 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700174}
Jason Sams326e0dd2009-05-22 14:03:28 -0700175
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800176void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700177 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
178 if (mToDirtyList[ct] == p) {
179 mToDirtyList.removeAt(ct);
180 return;
181 }
182 }
183 rsAssert(0);
184}
185
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800186void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800187 ObjectBase::dumpLOGV(prefix);
188
189 String8 s(prefix);
190 s.append(" type ");
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700191 if (mHal.state.type) {
Jason Samsbad80742011-03-16 16:29:28 -0700192 mHal.state.type->dumpLOGV(s.string());
Jason Samsc21cf402009-11-17 17:26:46 -0800193 }
194
Steve Block65982012011-10-20 11:56:00 +0100195 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Jason Samseb4fe182011-05-26 16:33:01 -0700196 prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800197}
Jason Sams326e0dd2009-05-22 14:03:28 -0700198
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800199uint32_t Allocation::getPackedSize() const {
200 uint32_t numItems = mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes();
201 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
202}
203
204void Allocation::writePackedData(const Type *type,
205 uint8_t *dst, const uint8_t *src, bool dstPadded) {
206 const Element *elem = type->getElement();
207 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
208 uint32_t paddedBytes = elem->getSizeBytes();
209 uint32_t numItems = type->getSizeBytes() / paddedBytes;
210
211 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
212 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
213
214 // no sub-elements
215 uint32_t fieldCount = elem->getFieldCount();
216 if (fieldCount == 0) {
217 for (uint32_t i = 0; i < numItems; i ++) {
218 memcpy(dst, src, unpaddedBytes);
219 src += srcInc;
220 dst += dstInc;
221 }
222 return;
223 }
224
225 // Cache offsets
226 uint32_t *offsetsPadded = new uint32_t[fieldCount];
227 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
228 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
229
230 for (uint32_t i = 0; i < fieldCount; i++) {
231 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
232 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
233 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
234 }
235
236 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
237 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
238
239 // complex elements, need to copy subelem after subelem
240 for (uint32_t i = 0; i < numItems; i ++) {
241 for (uint32_t fI = 0; fI < fieldCount; fI++) {
242 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
243 }
244 src += srcInc;
245 dst += dstInc;
246 }
247
248 delete[] offsetsPadded;
249 delete[] offsetsUnpadded;
250 delete[] sizeUnpadded;
251}
252
253void Allocation::unpackVec3Allocation(const void *data, uint32_t dataSize) {
254 const uint8_t *src = (const uint8_t*)data;
255 uint8_t *dst = (uint8_t*)getPtr();
256
257 writePackedData(getType(), dst, src, true);
258}
259
260void Allocation::packVec3Allocation(OStream *stream) const {
261 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
262 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
263 uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes;
264
265 const uint8_t *src = (const uint8_t*)getPtr();
266 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
267
268 writePackedData(getType(), dst, src, false);
269 stream->addByteArray(dst, getPackedSize());
270
271 delete[] dst;
272}
273
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800274void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700275 // Need to identify ourselves
276 stream->addU32((uint32_t)getClassId());
277
278 String8 name(getName());
279 stream->addString(&name);
280
281 // First thing we need to serialize is the type object since it will be needed
282 // to initialize the class
Jason Samsbad80742011-03-16 16:29:28 -0700283 mHal.state.type->serialize(stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700284
Jason Samsbad80742011-03-16 16:29:28 -0700285 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800286 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
287 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700288 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800289 stream->addU32(packedSize);
290 if (dataSize == packedSize) {
291 // Now write the data
292 stream->addByteArray(getPtr(), dataSize);
293 } else {
294 // Now write the data
295 packVec3Allocation(stream);
296 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700297}
298
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800299Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700300 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700301 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800302 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000303 ALOGE("allocation loading skipped due to invalid class id\n");
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700304 return NULL;
305 }
306
307 String8 name;
308 stream->loadString(&name);
309
310 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800311 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700312 return NULL;
313 }
314 type->compute();
315
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800316 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
317 type->decUserRef();
318
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700319 // Number of bytes we wrote out for this allocation
320 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800321 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
322 uint32_t packedSize = alloc->getPackedSize();
323 if (dataSize != type->getSizeBytes() &&
324 dataSize != packedSize) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000325 ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800326 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700327 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700328 return NULL;
329 }
330
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700331 alloc->setName(name.string(), name.size());
332
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800333 if (dataSize == type->getSizeBytes()) {
334 uint32_t count = dataSize / type->getElementSizeBytes();
335 // Read in all of our allocation data
336 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
337 } else {
338 alloc->unpackVec3Allocation(stream->getPtr() + stream->getPos(), dataSize);
339 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700340 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700341
342 return alloc;
343}
344
Jason Samseb4fe182011-05-26 16:33:01 -0700345void Allocation::sendDirty(const Context *rsc) const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700346 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
347 mToDirtyList[ct]->forceDirty();
348 }
Jason Samseb4fe182011-05-26 16:33:01 -0700349 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700350}
Jason Sams326e0dd2009-05-22 14:03:28 -0700351
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800352void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700353 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700354 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700355 uint32_t stride = e->getSizeBytes();
356
Jason Sams96abf812010-10-05 13:32:49 -0700357 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700358 while (ct > 0) {
359 e->incRefs(p);
360 ct --;
361 p += stride;
362 }
363}
364
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800365void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700366 if (!mHal.state.hasReferences || !getIsScript()) {
367 return;
368 }
Jason Samse3929c92010-08-09 18:13:33 -0700369 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700370 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700371 uint32_t stride = e->getSizeBytes();
372
Jason Sams96abf812010-10-05 13:32:49 -0700373 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700374 while (ct > 0) {
375 e->decRefs(p);
376 ct --;
377 p += stride;
378 }
379}
380
Jason Samsc7cec1e2011-08-18 18:01:33 -0700381void Allocation::freeChildrenUnlocked () {
382 decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
383}
384
385bool Allocation::freeChildren() {
386 if (mHal.state.hasReferences) {
387 incSysRef();
388 freeChildrenUnlocked();
389 return decSysRef();
390 }
391 return false;
392}
393
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800394void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700395}
396
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800397void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsbad80742011-03-16 16:29:28 -0700398 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams96abf812010-10-05 13:32:49 -0700399 if (dimX == oldDimX) {
400 return;
401 }
402
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700403 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700404 if (dimX < oldDimX) {
Jason Samseb4fe182011-05-26 16:33:01 -0700405 decRefs(getPtr(), oldDimX - dimX, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700406 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700407 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700408 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700409 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700410}
411
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800412void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000413 ALOGE("not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700414}
415
Jason Sams41e373d2012-01-13 14:01:20 -0800416int32_t Allocation::getSurfaceTextureID(const Context *rsc) {
417 int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this);
418 mHal.state.surfaceTextureID = id;
419 return id;
420}
421
Jason Sams326e0dd2009-05-22 14:03:28 -0700422/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700423//
Stephen Hines6a121812011-03-01 17:34:59 -0800424
Jason Sams326e0dd2009-05-22 14:03:28 -0700425namespace android {
426namespace renderscript {
427
Jason Samsc975cf42011-04-28 18:26:48 -0700428static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
429
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800430static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700431 uint32_t w = out.getDimX();
432 uint32_t h = out.getDimY();
433
Jason Samse9f5c532009-07-28 17:20:11 -0700434 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700435 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
436 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
437 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
438
Jason Samse9f5c532009-07-28 17:20:11 -0700439 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700440 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
441 oPtr ++;
442 i1 += 2;
443 i2 += 2;
444 }
445 }
446}
447
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800448static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700449 uint32_t w = out.getDimX();
450 uint32_t h = out.getDimY();
451
Jason Samse9f5c532009-07-28 17:20:11 -0700452 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700453 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
454 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
455 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
456
Jason Samse9f5c532009-07-28 17:20:11 -0700457 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700458 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700459 oPtr ++;
460 i1 += 2;
461 i2 += 2;
462 }
463 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700464}
465
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800466static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800467 uint32_t w = out.getDimX();
468 uint32_t h = out.getDimY();
469
470 for (uint32_t y=0; y < h; y++) {
471 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
472 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
473 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
474
475 for (uint32_t x=0; x < w; x++) {
476 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
477 oPtr ++;
478 i1 += 2;
479 i2 += 2;
480 }
481 }
482}
483
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800484static void mip(const Adapter2D &out, const Adapter2D &in) {
485 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700486 case 32:
487 mip8888(out, in);
488 break;
489 case 16:
490 mip565(out, in);
491 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800492 case 8:
493 mip8(out, in);
494 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700495 }
Jason Samse9f5c532009-07-28 17:20:11 -0700496}
Jason Sams326e0dd2009-05-22 14:03:28 -0700497
Jason Sams366c9c82010-12-08 16:14:36 -0800498void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
499 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700500 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800501 a->syncAll(rsc, src);
502}
503
Jason Samsa2371512011-01-12 13:28:37 -0800504void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700505 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsc975cf42011-04-28 18:26:48 -0700506 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800507}
508
509void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
510 Allocation *texAlloc = static_cast<Allocation *>(va);
511 const Type * t = texAlloc->getType();
512
513 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
514 if (s != dataLen) {
515 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
516 return;
517 }
518
519 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700520}
521
Jason Sams4b45b892010-12-29 14:31:29 -0800522void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700523 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700524 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800525 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700526}
527
Jason Sams4b45b892010-12-29 14:31:29 -0800528void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700529 const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped
Jason Sams326e0dd2009-05-22 14:03:28 -0700530 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800531 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700532}
533
Jason Sams4b45b892010-12-29 14:31:29 -0800534void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700535 const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped
Jason Sams5f0c84c2010-08-31 13:50:42 -0700536 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800537 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700538}
539
Jason Sams4b45b892010-12-29 14:31:29 -0800540void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700541 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700542 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800543 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700544}
545
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700546void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
Jason Samse579df42009-08-10 14:55:26 -0700547 Allocation *a = static_cast<Allocation *>(va);
548 a->read(data);
549}
550
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800551void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700552 Allocation *a = static_cast<Allocation *>(va);
553 a->resize1D(rsc, dimX);
554}
555
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800556void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700557 Allocation *a = static_cast<Allocation *>(va);
558 a->resize2D(rsc, dimX, dimY);
559}
560
Jason Samsc975cf42011-04-28 18:26:48 -0700561static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800562 Context *rsc = static_cast<Context *>(con);
563 Allocation *texAlloc = static_cast<Allocation *>(va);
564 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
565 for (uint32_t face = 0; face < numFaces; face ++) {
566 Adapter2D adapt(rsc, texAlloc);
567 Adapter2D adapt2(rsc, texAlloc);
568 adapt.setFace(face);
569 adapt2.setFace(face);
570 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
571 adapt.setLOD(lod);
572 adapt2.setLOD(lod + 1);
573 mip(adapt2, adapt);
574 }
575 }
576}
577
Jason Samsc975cf42011-04-28 18:26:48 -0700578RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
579 RsAllocationMipmapControl mips,
Jason Sams179e9a42011-11-23 15:02:15 -0800580 uint32_t usages, uint32_t ptr) {
581 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700582 if (!alloc) {
583 return NULL;
584 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700585 alloc->incUserRef();
586 return alloc;
587}
588
Jason Samsc975cf42011-04-28 18:26:48 -0700589RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
590 RsAllocationMipmapControl mips,
591 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800592 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700593
Jason Sams179e9a42011-11-23 15:02:15 -0800594 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700595 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
596 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000597 ALOGE("Memory allocation failure");
Jason Samsf0c1df42010-10-26 13:09:17 -0700598 return NULL;
599 }
600
Jason Sams366c9c82010-12-08 16:14:36 -0800601 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800602 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700603 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700604 }
605
Jason Samseb4fe182011-05-26 16:33:01 -0700606 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700607 return texAlloc;
608}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800609
Jason Samsc975cf42011-04-28 18:26:48 -0700610RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
611 RsAllocationMipmapControl mips,
612 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800613 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800614
615 // Cubemap allocation's faces should be Width by Width each.
616 // Source data should have 6 * Width by Width pixels
617 // Error checking is done in the java layer
Jason Sams179e9a42011-11-23 15:02:15 -0800618 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800619 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
620 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000621 ALOGE("Memory allocation failure");
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800622 return NULL;
623 }
624
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800625 uint32_t faceSize = t->getDimX();
626 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
627 uint32_t copySize = faceSize * t->getElementSizeBytes();
628
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800629 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800630 for (uint32_t face = 0; face < 6; face ++) {
631 Adapter2D faceAdapter(rsc, texAlloc);
632 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800633
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800634 for (uint32_t dI = 0; dI < faceSize; dI ++) {
635 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
636 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800637
Jason Sams366c9c82010-12-08 16:14:36 -0800638 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800639 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800640 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800641
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800642 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700643 AllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800644 }
645
Jason Samseb4fe182011-05-26 16:33:01 -0700646 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800647 return texAlloc;
648}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800649
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700650void rsi_AllocationCopy2DRange(Context *rsc,
651 RsAllocation dstAlloc,
652 uint32_t dstXoff, uint32_t dstYoff,
653 uint32_t dstMip, uint32_t dstFace,
654 uint32_t width, uint32_t height,
655 RsAllocation srcAlloc,
656 uint32_t srcXoff, uint32_t srcYoff,
657 uint32_t srcMip, uint32_t srcFace) {
658 Allocation *dst = static_cast<Allocation *>(dstAlloc);
659 Allocation *src= static_cast<Allocation *>(srcAlloc);
660 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
661 (RsAllocationCubemapFace)dstFace,
662 width, height,
663 src, srcXoff, srcYoff,srcMip,
664 (RsAllocationCubemapFace)srcFace);
665}
666
Jason Sams41e373d2012-01-13 14:01:20 -0800667int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) {
668 Allocation *alloc = static_cast<Allocation *>(valloc);
669 return alloc->getSurfaceTextureID(rsc);
670}
671
Jason Samsc975cf42011-04-28 18:26:48 -0700672}
673}
674
675const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
676 Allocation *a = static_cast<Allocation *>(va);
677 a->getType()->incUserRef();
678
679 return a->getType();
680}