blob: c1192fefff7ab42e9592108d170d94520e27d7ed [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,
25 RsAllocationMipmapControl mc)
26 : 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 Sams366c9c82010-12-08 16:14:36 -080032
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070033 setType(type);
Jason Samsbad80742011-03-16 16:29:28 -070034 updateCache();
35}
Jason Samsfa84da22010-03-01 15:31:04 -080036
Jason Samseb4fe182011-05-26 16:33:01 -070037Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
38 RsAllocationMipmapControl mc) {
39 Allocation *a = new Allocation(rsc, type, usages, mc);
40
41 if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
42 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
43 delete a;
44 return NULL;
45 }
46 return a;
47}
48
Jason Samsbad80742011-03-16 16:29:28 -070049void Allocation::updateCache() {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070050 const Type *type = mHal.state.type;
Jason Samsbad80742011-03-16 16:29:28 -070051 mHal.state.dimensionX = type->getDimX();
52 mHal.state.dimensionY = type->getDimY();
53 mHal.state.dimensionZ = type->getDimZ();
54 mHal.state.hasFaces = type->getDimFaces();
55 mHal.state.hasMipmaps = type->getDimLOD();
56 mHal.state.elementSizeBytes = type->getElementSizeBytes();
57 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -070058}
59
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080060Allocation::~Allocation() {
Jason Samsc7cec1e2011-08-18 18:01:33 -070061 freeChildrenUnlocked();
Jason Samseb4fe182011-05-26 16:33:01 -070062 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070063}
64
Jason Sams366c9c82010-12-08 16:14:36 -080065void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -070066 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -080067}
68
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080069void Allocation::read(void *data) {
Jason Samseb4fe182011-05-26 16:33:01 -070070 memcpy(data, getPtr(), mHal.state.type->getSizeBytes());
Jason Samse579df42009-08-10 14:55:26 -070071}
72
Jason Sams4b45b892010-12-29 14:31:29 -080073void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
74 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -070075 const uint32_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -070076
Jason Samseb4fe182011-05-26 16:33:01 -070077 if ((count * eSize) != sizeBytes) {
78 LOGE("Allocation::subData called with mismatched size expected %i, got %i",
79 (count * eSize), sizeBytes);
Jason Samsbad80742011-03-16 16:29:28 -070080 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -070081 return;
82 }
Jason Samse3929c92010-08-09 18:13:33 -070083
Jason Samseb4fe182011-05-26 16:33:01 -070084 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
85 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070086}
87
Jason Sams4b45b892010-12-29 14:31:29 -080088void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080089 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -070090 const uint32_t eSize = mHal.state.elementSizeBytes;
91 const uint32_t lineSize = eSize * w;
Jason Sams326e0dd2009-05-22 14:03:28 -070092
Jason Samsa2371512011-01-12 13:28:37 -080093 //LOGE("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 -070094
Jason Samsa2371512011-01-12 13:28:37 -080095 if ((lineSize * h) != sizeBytes) {
96 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -070097 rsAssert(!"Allocation::subData called with mismatched size");
98 return;
99 }
100
Jason Samseb4fe182011-05-26 16:33:01 -0700101 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes);
102 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700103}
104
Jason Sams236385b2011-01-12 14:53:25 -0800105void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
106 uint32_t lod, RsAllocationCubemapFace face,
107 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700108}
109
Jason Sams4b45b892010-12-29 14:31:29 -0800110void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800111 uint32_t cIdx, uint32_t sizeBytes) {
Jason Samsbad80742011-03-16 16:29:28 -0700112 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700113
Jason Samsbad80742011-03-16 16:29:28 -0700114 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700115 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
116 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
117 return;
118 }
119
Jason Samsbad80742011-03-16 16:29:28 -0700120 if (x >= mHal.state.dimensionX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700121 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
122 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
123 return;
124 }
125
Jason Samsbad80742011-03-16 16:29:28 -0700126 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700127 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800128 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700129 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
130 return;
131 }
132
Jason Samseb4fe182011-05-26 16:33:01 -0700133 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
134 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700135}
136
Jason Sams4b45b892010-12-29 14:31:29 -0800137void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Samsbad80742011-03-16 16:29:28 -0700139 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700140
Jason Samsbad80742011-03-16 16:29:28 -0700141 if (x >= mHal.state.dimensionX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700142 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
143 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
144 return;
145 }
146
Jason Samsbad80742011-03-16 16:29:28 -0700147 if (y >= mHal.state.dimensionY) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700148 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
149 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 (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700154 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
155 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
156 return;
157 }
158
Jason Samsbad80742011-03-16 16:29:28 -0700159 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700160
161 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800162 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700163 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
164 return;
165 }
166
Jason Samseb4fe182011-05-26 16:33:01 -0700167 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
168 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700169}
170
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800171void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700172 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700173}
Jason Sams326e0dd2009-05-22 14:03:28 -0700174
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800175void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700176 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
177 if (mToDirtyList[ct] == p) {
178 mToDirtyList.removeAt(ct);
179 return;
180 }
181 }
182 rsAssert(0);
183}
184
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800185void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800186 ObjectBase::dumpLOGV(prefix);
187
188 String8 s(prefix);
189 s.append(" type ");
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700190 if (mHal.state.type) {
Jason Samsbad80742011-03-16 16:29:28 -0700191 mHal.state.type->dumpLOGV(s.string());
Jason Samsc21cf402009-11-17 17:26:46 -0800192 }
193
Steve Block65982012011-10-20 11:56:00 +0100194 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Jason Samseb4fe182011-05-26 16:33:01 -0700195 prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800196}
Jason Sams326e0dd2009-05-22 14:03:28 -0700197
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800198uint32_t Allocation::getPackedSize() const {
199 uint32_t numItems = mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes();
200 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
201}
202
203void Allocation::writePackedData(const Type *type,
204 uint8_t *dst, const uint8_t *src, bool dstPadded) {
205 const Element *elem = type->getElement();
206 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
207 uint32_t paddedBytes = elem->getSizeBytes();
208 uint32_t numItems = type->getSizeBytes() / paddedBytes;
209
210 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
211 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
212
213 // no sub-elements
214 uint32_t fieldCount = elem->getFieldCount();
215 if (fieldCount == 0) {
216 for (uint32_t i = 0; i < numItems; i ++) {
217 memcpy(dst, src, unpaddedBytes);
218 src += srcInc;
219 dst += dstInc;
220 }
221 return;
222 }
223
224 // Cache offsets
225 uint32_t *offsetsPadded = new uint32_t[fieldCount];
226 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
227 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
228
229 for (uint32_t i = 0; i < fieldCount; i++) {
230 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
231 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
232 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
233 }
234
235 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
236 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
237
238 // complex elements, need to copy subelem after subelem
239 for (uint32_t i = 0; i < numItems; i ++) {
240 for (uint32_t fI = 0; fI < fieldCount; fI++) {
241 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
242 }
243 src += srcInc;
244 dst += dstInc;
245 }
246
247 delete[] offsetsPadded;
248 delete[] offsetsUnpadded;
249 delete[] sizeUnpadded;
250}
251
252void Allocation::unpackVec3Allocation(const void *data, uint32_t dataSize) {
253 const uint8_t *src = (const uint8_t*)data;
254 uint8_t *dst = (uint8_t*)getPtr();
255
256 writePackedData(getType(), dst, src, true);
257}
258
259void Allocation::packVec3Allocation(OStream *stream) const {
260 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
261 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
262 uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes;
263
264 const uint8_t *src = (const uint8_t*)getPtr();
265 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
266
267 writePackedData(getType(), dst, src, false);
268 stream->addByteArray(dst, getPackedSize());
269
270 delete[] dst;
271}
272
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800273void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700274 // Need to identify ourselves
275 stream->addU32((uint32_t)getClassId());
276
277 String8 name(getName());
278 stream->addString(&name);
279
280 // First thing we need to serialize is the type object since it will be needed
281 // to initialize the class
Jason Samsbad80742011-03-16 16:29:28 -0700282 mHal.state.type->serialize(stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700283
Jason Samsbad80742011-03-16 16:29:28 -0700284 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800285 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
286 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700287 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800288 stream->addU32(packedSize);
289 if (dataSize == packedSize) {
290 // Now write the data
291 stream->addByteArray(getPtr(), dataSize);
292 } else {
293 // Now write the data
294 packVec3Allocation(stream);
295 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700296}
297
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800298Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700299 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700300 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800301 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700302 LOGE("allocation loading skipped due to invalid class id\n");
303 return NULL;
304 }
305
306 String8 name;
307 stream->loadString(&name);
308
309 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800310 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700311 return NULL;
312 }
313 type->compute();
314
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800315 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
316 type->decUserRef();
317
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700318 // Number of bytes we wrote out for this allocation
319 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800320 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
321 uint32_t packedSize = alloc->getPackedSize();
322 if (dataSize != type->getSizeBytes() &&
323 dataSize != packedSize) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700324 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800325 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700326 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700327 return NULL;
328 }
329
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700330 alloc->setName(name.string(), name.size());
331
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800332 if (dataSize == type->getSizeBytes()) {
333 uint32_t count = dataSize / type->getElementSizeBytes();
334 // Read in all of our allocation data
335 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
336 } else {
337 alloc->unpackVec3Allocation(stream->getPtr() + stream->getPos(), dataSize);
338 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700339 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700340
341 return alloc;
342}
343
Jason Samseb4fe182011-05-26 16:33:01 -0700344void Allocation::sendDirty(const Context *rsc) const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700345 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
346 mToDirtyList[ct]->forceDirty();
347 }
Jason Samseb4fe182011-05-26 16:33:01 -0700348 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700349}
Jason Sams326e0dd2009-05-22 14:03:28 -0700350
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800351void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700352 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700353 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700354 uint32_t stride = e->getSizeBytes();
355
Jason Sams96abf812010-10-05 13:32:49 -0700356 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700357 while (ct > 0) {
358 e->incRefs(p);
359 ct --;
360 p += stride;
361 }
362}
363
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800364void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700365 if (!mHal.state.hasReferences || !getIsScript()) {
366 return;
367 }
Jason Samse3929c92010-08-09 18:13:33 -0700368 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700369 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700370 uint32_t stride = e->getSizeBytes();
371
Jason Sams96abf812010-10-05 13:32:49 -0700372 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700373 while (ct > 0) {
374 e->decRefs(p);
375 ct --;
376 p += stride;
377 }
378}
379
Jason Samsc7cec1e2011-08-18 18:01:33 -0700380void Allocation::freeChildrenUnlocked () {
381 decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
382}
383
384bool Allocation::freeChildren() {
385 if (mHal.state.hasReferences) {
386 incSysRef();
387 freeChildrenUnlocked();
388 return decSysRef();
389 }
390 return false;
391}
392
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800393void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700394}
395
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800396void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsbad80742011-03-16 16:29:28 -0700397 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams96abf812010-10-05 13:32:49 -0700398 if (dimX == oldDimX) {
399 return;
400 }
401
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700402 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700403 if (dimX < oldDimX) {
Jason Samseb4fe182011-05-26 16:33:01 -0700404 decRefs(getPtr(), oldDimX - dimX, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700405 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700406 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700407 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700408 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700409}
410
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800411void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700412 LOGE("not implemented");
413}
414
Jason Sams326e0dd2009-05-22 14:03:28 -0700415/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700416//
Stephen Hines6a121812011-03-01 17:34:59 -0800417
Jason Sams326e0dd2009-05-22 14:03:28 -0700418namespace android {
419namespace renderscript {
420
Jason Samsc975cf42011-04-28 18:26:48 -0700421static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
422
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800423static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700424 uint32_t w = out.getDimX();
425 uint32_t h = out.getDimY();
426
Jason Samse9f5c532009-07-28 17:20:11 -0700427 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700428 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
429 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
430 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
431
Jason Samse9f5c532009-07-28 17:20:11 -0700432 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700433 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
434 oPtr ++;
435 i1 += 2;
436 i2 += 2;
437 }
438 }
439}
440
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800441static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700442 uint32_t w = out.getDimX();
443 uint32_t h = out.getDimY();
444
Jason Samse9f5c532009-07-28 17:20:11 -0700445 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700446 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
447 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
448 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
449
Jason Samse9f5c532009-07-28 17:20:11 -0700450 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700451 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700452 oPtr ++;
453 i1 += 2;
454 i2 += 2;
455 }
456 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700457}
458
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800459static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800460 uint32_t w = out.getDimX();
461 uint32_t h = out.getDimY();
462
463 for (uint32_t y=0; y < h; y++) {
464 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
465 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
466 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
467
468 for (uint32_t x=0; x < w; x++) {
469 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
470 oPtr ++;
471 i1 += 2;
472 i2 += 2;
473 }
474 }
475}
476
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800477static void mip(const Adapter2D &out, const Adapter2D &in) {
478 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700479 case 32:
480 mip8888(out, in);
481 break;
482 case 16:
483 mip565(out, in);
484 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800485 case 8:
486 mip8(out, in);
487 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700488 }
Jason Samse9f5c532009-07-28 17:20:11 -0700489}
Jason Sams326e0dd2009-05-22 14:03:28 -0700490
Jason Sams366c9c82010-12-08 16:14:36 -0800491void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
492 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700493 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800494 a->syncAll(rsc, src);
495}
496
Jason Samsa2371512011-01-12 13:28:37 -0800497void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700498 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsc975cf42011-04-28 18:26:48 -0700499 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800500}
501
502void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
503 Allocation *texAlloc = static_cast<Allocation *>(va);
504 const Type * t = texAlloc->getType();
505
506 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
507 if (s != dataLen) {
508 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
509 return;
510 }
511
512 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700513}
514
Jason Sams4b45b892010-12-29 14:31:29 -0800515void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700516 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700517 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800518 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700519}
520
Jason Sams4b45b892010-12-29 14:31:29 -0800521void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700522 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 -0700523 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800524 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700525}
526
Jason Sams4b45b892010-12-29 14:31:29 -0800527void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700528 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 -0700529 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800530 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700531}
532
Jason Sams4b45b892010-12-29 14:31:29 -0800533void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700534 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700535 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800536 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700537}
538
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700539void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
Jason Samse579df42009-08-10 14:55:26 -0700540 Allocation *a = static_cast<Allocation *>(va);
541 a->read(data);
542}
543
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800544void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700545 Allocation *a = static_cast<Allocation *>(va);
546 a->resize1D(rsc, dimX);
547}
548
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800549void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700550 Allocation *a = static_cast<Allocation *>(va);
551 a->resize2D(rsc, dimX, dimY);
552}
553
Jason Samsc975cf42011-04-28 18:26:48 -0700554static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800555 Context *rsc = static_cast<Context *>(con);
556 Allocation *texAlloc = static_cast<Allocation *>(va);
557 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
558 for (uint32_t face = 0; face < numFaces; face ++) {
559 Adapter2D adapt(rsc, texAlloc);
560 Adapter2D adapt2(rsc, texAlloc);
561 adapt.setFace(face);
562 adapt2.setFace(face);
563 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
564 adapt.setLOD(lod);
565 adapt2.setLOD(lod + 1);
566 mip(adapt2, adapt);
567 }
568 }
569}
570
Jason Samsc975cf42011-04-28 18:26:48 -0700571RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
572 RsAllocationMipmapControl mips,
573 uint32_t usages) {
Jason Samseb4fe182011-05-26 16:33:01 -0700574 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips);
575 if (!alloc) {
576 return NULL;
577 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700578 alloc->incUserRef();
579 return alloc;
580}
581
Jason Samsc975cf42011-04-28 18:26:48 -0700582RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
583 RsAllocationMipmapControl mips,
584 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800585 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700586
Jason Samsc975cf42011-04-28 18:26:48 -0700587 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages);
Jason Samsf0c1df42010-10-26 13:09:17 -0700588 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
589 if (texAlloc == NULL) {
590 LOGE("Memory allocation failure");
591 return NULL;
592 }
593
Jason Sams366c9c82010-12-08 16:14:36 -0800594 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800595 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700596 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700597 }
598
Jason Samseb4fe182011-05-26 16:33:01 -0700599 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700600 return texAlloc;
601}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800602
Jason Samsc975cf42011-04-28 18:26:48 -0700603RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
604 RsAllocationMipmapControl mips,
605 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800606 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800607
608 // Cubemap allocation's faces should be Width by Width each.
609 // Source data should have 6 * Width by Width pixels
610 // Error checking is done in the java layer
Jason Samsc975cf42011-04-28 18:26:48 -0700611 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800612 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
613 if (texAlloc == NULL) {
614 LOGE("Memory allocation failure");
615 return NULL;
616 }
617
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800618 uint32_t faceSize = t->getDimX();
619 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
620 uint32_t copySize = faceSize * t->getElementSizeBytes();
621
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800622 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800623 for (uint32_t face = 0; face < 6; face ++) {
624 Adapter2D faceAdapter(rsc, texAlloc);
625 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800626
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800627 for (uint32_t dI = 0; dI < faceSize; dI ++) {
628 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
629 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800630
Jason Sams366c9c82010-12-08 16:14:36 -0800631 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800632 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800633 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800634
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800635 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700636 AllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800637 }
638
Jason Samseb4fe182011-05-26 16:33:01 -0700639 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800640 return texAlloc;
641}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800642
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700643void rsi_AllocationCopy2DRange(Context *rsc,
644 RsAllocation dstAlloc,
645 uint32_t dstXoff, uint32_t dstYoff,
646 uint32_t dstMip, uint32_t dstFace,
647 uint32_t width, uint32_t height,
648 RsAllocation srcAlloc,
649 uint32_t srcXoff, uint32_t srcYoff,
650 uint32_t srcMip, uint32_t srcFace) {
651 Allocation *dst = static_cast<Allocation *>(dstAlloc);
652 Allocation *src= static_cast<Allocation *>(srcAlloc);
653 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
654 (RsAllocationCubemapFace)dstFace,
655 width, height,
656 src, srcXoff, srcYoff,srcMip,
657 (RsAllocationCubemapFace)srcFace);
658}
659
Jason Samsc975cf42011-04-28 18:26:48 -0700660}
661}
662
663const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
664 Allocation *a = static_cast<Allocation *>(va);
665 a->getType()->incUserRef();
666
667 return a->getType();
668}