blob: d7e89333a2b3dfc738d2e04a6a7ac88f271784cd [file] [log] [blame]
Jason Samsd19f10d2009-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 Samsd19f10d2009-05-22 14:03:28 -070016
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080017#include "rsContext.h"
Jason Sams7e8aae72011-05-26 16:33:01 -070018#include "rs_hal.h"
19
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -070020
Jason Samsd19f10d2009-05-22 14:03:28 -070021using namespace android;
22using namespace android::renderscript;
23
Alex Sakhartchouk08571962010-12-15 09:59:58 -080024Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams857d0c72011-11-23 15:02:15 -080025 RsAllocationMipmapControl mc, void * ptr)
Alex Sakhartchouk08571962010-12-15 09:59:58 -080026 : ObjectBase(rsc) {
Jason Sams8a64743f2010-03-01 15:31:04 -080027
Jason Sams7e8aae72011-05-26 16:33:01 -070028 memset(&mHal, 0, sizeof(mHal));
29 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samse4a06c52011-03-16 16:29:28 -070030 mHal.state.usageFlags = usages;
31 mHal.state.mipmapControl = mc;
Jason Sams857d0c72011-11-23 15:02:15 -080032 mHal.state.usrPtr = ptr;
Jason Sams5476b452010-12-08 16:14:36 -080033
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070034 setType(type);
Jason Samse4a06c52011-03-16 16:29:28 -070035 updateCache();
36}
Jason Sams8a64743f2010-03-01 15:31:04 -080037
Jason Sams7e8aae72011-05-26 16:33:01 -070038Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams857d0c72011-11-23 15:02:15 -080039 RsAllocationMipmapControl mc, void * ptr) {
40 Allocation *a = new Allocation(rsc, type, usages, mc, ptr);
Jason Sams7e8aae72011-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 Samse4a06c52011-03-16 16:29:28 -070050void Allocation::updateCache() {
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070051 const Type *type = mHal.state.type;
Jason Samse4a06c52011-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 Samsd19f10d2009-05-22 14:03:28 -070059}
60
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080061Allocation::~Allocation() {
Jason Sams777ec262011-08-18 18:01:33 -070062 freeChildrenUnlocked();
Jason Sams7e8aae72011-05-26 16:33:01 -070063 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Samsd19f10d2009-05-22 14:03:28 -070064}
65
Jason Sams5476b452010-12-08 16:14:36 -080066void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Sams7e8aae72011-05-26 16:33:01 -070067 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Sams3b7d39b2009-12-14 12:57:40 -080068}
69
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080070void Allocation::read(void *data) {
Jason Sams7e8aae72011-05-26 16:33:01 -070071 memcpy(data, getPtr(), mHal.state.type->getSizeBytes());
Jason Sams40a29e82009-08-10 14:55:26 -070072}
73
Jason Sams49a05d72010-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 Sams7e8aae72011-05-26 16:33:01 -070076 const uint32_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams07ae4062009-08-27 20:23:34 -070077
Jason Sams7e8aae72011-05-26 16:33:01 -070078 if ((count * eSize) != sizeBytes) {
Steve Block3762c312012-01-06 19:20:56 +000079 ALOGE("Allocation::subData called with mismatched size expected %i, got %i",
Jason Sams7e8aae72011-05-26 16:33:01 -070080 (count * eSize), sizeBytes);
Jason Samse4a06c52011-03-16 16:29:28 -070081 mHal.state.type->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -070082 return;
83 }
Jason Samsb28ca96f2010-08-09 18:13:33 -070084
Jason Sams7e8aae72011-05-26 16:33:01 -070085 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
86 sendDirty(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -070087}
88
Jason Sams49a05d72010-12-29 14:31:29 -080089void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080090 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams7e8aae72011-05-26 16:33:01 -070091 const uint32_t eSize = mHal.state.elementSizeBytes;
92 const uint32_t lineSize = eSize * w;
Jason Samsd19f10d2009-05-22 14:03:28 -070093
Steve Block3762c312012-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 Sams07ae4062009-08-27 20:23:34 -070095
Jason Samsf7086092011-01-12 13:28:37 -080096 if ((lineSize * h) != sizeBytes) {
Steve Block3762c312012-01-06 19:20:56 +000097 ALOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams07ae4062009-08-27 20:23:34 -070098 rsAssert(!"Allocation::subData called with mismatched size");
99 return;
100 }
101
Jason Sams7e8aae72011-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 Samsd19f10d2009-05-22 14:03:28 -0700104}
105
Jason Samsfb9f82c2011-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 Samsd19f10d2009-05-22 14:03:28 -0700109}
110
Jason Sams49a05d72010-12-29 14:31:29 -0800111void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800112 uint32_t cIdx, uint32_t sizeBytes) {
Jason Samse4a06c52011-03-16 16:29:28 -0700113 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams49bdaf02010-08-31 13:50:42 -0700114
Jason Samse4a06c52011-03-16 16:29:28 -0700115 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Block3762c312012-01-06 19:20:56 +0000116 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -0700117 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
118 return;
119 }
120
Jason Samse4a06c52011-03-16 16:29:28 -0700121 if (x >= mHal.state.dimensionX) {
Steve Block3762c312012-01-06 19:20:56 +0000122 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams49bdaf02010-08-31 13:50:42 -0700123 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
124 return;
125 }
126
Jason Samse4a06c52011-03-16 16:29:28 -0700127 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -0700128 if (sizeBytes != e->getSizeBytes()) {
Steve Block3762c312012-01-06 19:20:56 +0000129 ALOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams49bdaf02010-08-31 13:50:42 -0700130 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
131 return;
132 }
133
Jason Sams7e8aae72011-05-26 16:33:01 -0700134 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
135 sendDirty(rsc);
Jason Sams49bdaf02010-08-31 13:50:42 -0700136}
137
Jason Sams49a05d72010-12-29 14:31:29 -0800138void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800139 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Samse4a06c52011-03-16 16:29:28 -0700140 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams49bdaf02010-08-31 13:50:42 -0700141
Jason Samse4a06c52011-03-16 16:29:28 -0700142 if (x >= mHal.state.dimensionX) {
Steve Block3762c312012-01-06 19:20:56 +0000143 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams49bdaf02010-08-31 13:50:42 -0700144 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
145 return;
146 }
147
Jason Samse4a06c52011-03-16 16:29:28 -0700148 if (y >= mHal.state.dimensionY) {
Steve Block3762c312012-01-06 19:20:56 +0000149 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams49bdaf02010-08-31 13:50:42 -0700150 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
151 return;
152 }
153
Jason Samse4a06c52011-03-16 16:29:28 -0700154 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Block3762c312012-01-06 19:20:56 +0000155 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -0700156 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
157 return;
158 }
159
Jason Samse4a06c52011-03-16 16:29:28 -0700160 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -0700161
162 if (sizeBytes != e->getSizeBytes()) {
Steve Block3762c312012-01-06 19:20:56 +0000163 ALOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams49bdaf02010-08-31 13:50:42 -0700164 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
165 return;
166 }
167
Jason Sams7e8aae72011-05-26 16:33:01 -0700168 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
169 sendDirty(rsc);
Jason Sams49bdaf02010-08-31 13:50:42 -0700170}
171
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800172void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700173 mToDirtyList.push(p);
Jason Sams83f1c632009-10-26 15:19:28 -0700174}
Jason Samsd19f10d2009-05-22 14:03:28 -0700175
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800176void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams83f1c632009-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 Sakhartchouked9f2102010-11-09 17:00:54 -0800186void Allocation::dumpLOGV(const char *prefix) const {
Jason Sams715333b2009-11-17 17:26:46 -0800187 ObjectBase::dumpLOGV(prefix);
188
189 String8 s(prefix);
190 s.append(" type ");
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -0700191 if (mHal.state.type) {
Jason Samse4a06c52011-03-16 16:29:28 -0700192 mHal.state.type->dumpLOGV(s.string());
Jason Sams715333b2009-11-17 17:26:46 -0800193 }
194
Steve Block71f2cf12011-10-20 11:56:00 +0100195 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Jason Sams7e8aae72011-05-26 16:33:01 -0700196 prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Sams715333b2009-11-17 17:26:46 -0800197}
Jason Samsd19f10d2009-05-22 14:03:28 -0700198
Alex Sakhartchouke60149d2011-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 Sakhartchouked9f2102010-11-09 17:00:54 -0800274void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-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 Samse4a06c52011-03-16 16:29:28 -0700283 mHal.state.type->serialize(stream);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700284
Jason Samse4a06c52011-03-16 16:29:28 -0700285 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchouke60149d2011-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 Sakhartchoukaa7d2882010-05-21 12:53:13 -0700288 // Write how much data we are storing
Alex Sakhartchouke60149d2011-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 Sakhartchoukaa7d2882010-05-21 12:53:13 -0700297}
298
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800299Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700300 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700301 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800302 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Steve Block3762c312012-01-06 19:20:56 +0000303 ALOGE("allocation loading skipped due to invalid class id\n");
Alex Sakhartchoukaa7d2882010-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 Sakhartchouked9f2102010-11-09 17:00:54 -0800311 if (!type) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700312 return NULL;
313 }
314 type->compute();
315
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800316 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
317 type->decUserRef();
318
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700319 // Number of bytes we wrote out for this allocation
320 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouke60149d2011-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 Block3762c312012-01-06 19:20:56 +0000325 ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800326 ObjectBase::checkDelete(alloc);
Jason Samsb38d5342010-10-21 14:06:55 -0700327 ObjectBase::checkDelete(type);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700328 return NULL;
329 }
330
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700331 alloc->setName(name.string(), name.size());
332
Alex Sakhartchouke60149d2011-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 Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700340 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700341
342 return alloc;
343}
344
Jason Sams7e8aae72011-05-26 16:33:01 -0700345void Allocation::sendDirty(const Context *rsc) const {
Jason Sams83f1c632009-10-26 15:19:28 -0700346 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
347 mToDirtyList[ct]->forceDirty();
348 }
Jason Sams7e8aae72011-05-26 16:33:01 -0700349 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams83f1c632009-10-26 15:19:28 -0700350}
Jason Samsd19f10d2009-05-22 14:03:28 -0700351
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800352void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700353 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samse4a06c52011-03-16 16:29:28 -0700354 const Element *e = mHal.state.type->getElement();
Jason Samsb28ca96f2010-08-09 18:13:33 -0700355 uint32_t stride = e->getSizeBytes();
356
Jason Sams5edc6082010-10-05 13:32:49 -0700357 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700358 while (ct > 0) {
359 e->incRefs(p);
360 ct --;
361 p += stride;
362 }
363}
364
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800365void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk065fa8d2011-08-12 11:30:30 -0700366 if (!mHal.state.hasReferences || !getIsScript()) {
367 return;
368 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700369 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samse4a06c52011-03-16 16:29:28 -0700370 const Element *e = mHal.state.type->getElement();
Jason Samsb28ca96f2010-08-09 18:13:33 -0700371 uint32_t stride = e->getSizeBytes();
372
Jason Sams5edc6082010-10-05 13:32:49 -0700373 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700374 while (ct > 0) {
375 e->decRefs(p);
376 ct --;
377 p += stride;
378 }
379}
380
Jason Sams777ec262011-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 Sakhartchouked9f2102010-11-09 17:00:54 -0800394void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams5edc6082010-10-05 13:32:49 -0700395}
396
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800397void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samse4a06c52011-03-16 16:29:28 -0700398 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams5edc6082010-10-05 13:32:49 -0700399 if (dimX == oldDimX) {
400 return;
401 }
402
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700403 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -0700404 if (dimX < oldDimX) {
Jason Sams7e8aae72011-05-26 16:33:01 -0700405 decRefs(getPtr(), oldDimX - dimX, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -0700406 }
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700407 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -0700408 setType(t.get());
Jason Samse4a06c52011-03-16 16:29:28 -0700409 updateCache();
Jason Sams5edc6082010-10-05 13:32:49 -0700410}
411
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800412void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Steve Block3762c312012-01-06 19:20:56 +0000413 ALOGE("not implemented");
Jason Sams5edc6082010-10-05 13:32:49 -0700414}
415
Jason Samsd19f10d2009-05-22 14:03:28 -0700416/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700417//
Stephen Hines60f9a622011-03-01 17:34:59 -0800418
Jason Samsd19f10d2009-05-22 14:03:28 -0700419namespace android {
420namespace renderscript {
421
Jason Samsc5765372011-04-28 18:26:48 -0700422static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
423
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800424static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700425 uint32_t w = out.getDimX();
426 uint32_t h = out.getDimY();
427
Jason Sams6f5c61c2009-07-28 17:20:11 -0700428 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700429 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
430 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
431 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
432
Jason Sams6f5c61c2009-07-28 17:20:11 -0700433 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700434 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
435 oPtr ++;
436 i1 += 2;
437 i2 += 2;
438 }
439 }
440}
441
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800442static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700443 uint32_t w = out.getDimX();
444 uint32_t h = out.getDimY();
445
Jason Sams6f5c61c2009-07-28 17:20:11 -0700446 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700447 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
448 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
449 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
450
Jason Sams6f5c61c2009-07-28 17:20:11 -0700451 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700452 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700453 oPtr ++;
454 i1 += 2;
455 i2 += 2;
456 }
457 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700458}
459
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800460static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Samse20e3b42010-01-19 17:53:54 -0800461 uint32_t w = out.getDimX();
462 uint32_t h = out.getDimY();
463
464 for (uint32_t y=0; y < h; y++) {
465 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
466 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
467 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
468
469 for (uint32_t x=0; x < w; x++) {
470 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
471 oPtr ++;
472 i1 += 2;
473 i2 += 2;
474 }
475 }
476}
477
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800478static void mip(const Adapter2D &out, const Adapter2D &in) {
479 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Sams6f5c61c2009-07-28 17:20:11 -0700480 case 32:
481 mip8888(out, in);
482 break;
483 case 16:
484 mip565(out, in);
485 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800486 case 8:
487 mip8(out, in);
488 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700489 }
Jason Sams6f5c61c2009-07-28 17:20:11 -0700490}
Jason Samsd19f10d2009-05-22 14:03:28 -0700491
Jason Sams5476b452010-12-08 16:14:36 -0800492void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
493 Allocation *a = static_cast<Allocation *>(va);
Jason Sams7e8aae72011-05-26 16:33:01 -0700494 a->sendDirty(rsc);
Jason Sams5476b452010-12-08 16:14:36 -0800495 a->syncAll(rsc, src);
496}
497
Jason Samsf7086092011-01-12 13:28:37 -0800498void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700499 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsc5765372011-04-28 18:26:48 -0700500 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams4ef66502010-12-10 16:03:15 -0800501}
502
503void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
504 Allocation *texAlloc = static_cast<Allocation *>(va);
505 const Type * t = texAlloc->getType();
506
507 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
508 if (s != dataLen) {
509 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
510 return;
511 }
512
513 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700514}
515
Jason Sams49a05d72010-12-29 14:31:29 -0800516void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700517 uint32_t count, const void *data, size_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700518 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800519 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700520}
521
Jason Sams49a05d72010-12-29 14:31:29 -0800522void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700523 const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped
Jason Samsd19f10d2009-05-22 14:03:28 -0700524 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800525 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700526}
527
Jason Sams49a05d72010-12-29 14:31:29 -0800528void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Alex Sakhartchouk17a8a192011-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 Sams49bdaf02010-08-31 13:50:42 -0700530 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800531 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700532}
533
Jason Sams49a05d72010-12-29 14:31:29 -0800534void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700535 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700536 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800537 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700538}
539
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700540void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
Jason Sams40a29e82009-08-10 14:55:26 -0700541 Allocation *a = static_cast<Allocation *>(va);
542 a->read(data);
543}
544
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800545void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700546 Allocation *a = static_cast<Allocation *>(va);
547 a->resize1D(rsc, dimX);
548}
549
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800550void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700551 Allocation *a = static_cast<Allocation *>(va);
552 a->resize2D(rsc, dimX, dimY);
553}
554
Jason Samsc5765372011-04-28 18:26:48 -0700555static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800556 Context *rsc = static_cast<Context *>(con);
557 Allocation *texAlloc = static_cast<Allocation *>(va);
558 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
559 for (uint32_t face = 0; face < numFaces; face ++) {
560 Adapter2D adapt(rsc, texAlloc);
561 Adapter2D adapt2(rsc, texAlloc);
562 adapt.setFace(face);
563 adapt2.setFace(face);
564 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
565 adapt.setLOD(lod);
566 adapt2.setLOD(lod + 1);
567 mip(adapt2, adapt);
568 }
569 }
570}
571
Jason Samsc5765372011-04-28 18:26:48 -0700572RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
573 RsAllocationMipmapControl mips,
Jason Sams857d0c72011-11-23 15:02:15 -0800574 uint32_t usages, uint32_t ptr) {
575 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
Jason Sams7e8aae72011-05-26 16:33:01 -0700576 if (!alloc) {
577 return NULL;
578 }
Jason Sams31a7e422010-10-26 13:09:17 -0700579 alloc->incUserRef();
580 return alloc;
581}
582
Jason Samsc5765372011-04-28 18:26:48 -0700583RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
584 RsAllocationMipmapControl mips,
585 const void *data, size_t data_length, uint32_t usages) {
Jason Sams5476b452010-12-08 16:14:36 -0800586 Type *t = static_cast<Type *>(vtype);
Jason Sams31a7e422010-10-26 13:09:17 -0700587
Jason Sams857d0c72011-11-23 15:02:15 -0800588 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Jason Sams31a7e422010-10-26 13:09:17 -0700589 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
590 if (texAlloc == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000591 ALOGE("Memory allocation failure");
Jason Sams31a7e422010-10-26 13:09:17 -0700592 return NULL;
593 }
594
Jason Sams5476b452010-12-08 16:14:36 -0800595 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsd4b23b52010-12-13 15:32:35 -0800596 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc5765372011-04-28 18:26:48 -0700597 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams31a7e422010-10-26 13:09:17 -0700598 }
599
Jason Sams7e8aae72011-05-26 16:33:01 -0700600 texAlloc->sendDirty(rsc);
Jason Sams31a7e422010-10-26 13:09:17 -0700601 return texAlloc;
602}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800603
Jason Samsc5765372011-04-28 18:26:48 -0700604RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
605 RsAllocationMipmapControl mips,
606 const void *data, size_t data_length, uint32_t usages) {
Jason Sams5476b452010-12-08 16:14:36 -0800607 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800608
609 // Cubemap allocation's faces should be Width by Width each.
610 // Source data should have 6 * Width by Width pixels
611 // Error checking is done in the java layer
Jason Sams857d0c72011-11-23 15:02:15 -0800612 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800613 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
614 if (texAlloc == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000615 ALOGE("Memory allocation failure");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800616 return NULL;
617 }
618
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800619 uint32_t faceSize = t->getDimX();
620 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
621 uint32_t copySize = faceSize * t->getElementSizeBytes();
622
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800623 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams5476b452010-12-08 16:14:36 -0800624 for (uint32_t face = 0; face < 6; face ++) {
625 Adapter2D faceAdapter(rsc, texAlloc);
626 faceAdapter.setFace(face);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800627
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800628 for (uint32_t dI = 0; dI < faceSize; dI ++) {
629 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
630 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800631
Jason Sams5476b452010-12-08 16:14:36 -0800632 // Move the data pointer to the next cube face
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800633 sourcePtr += copySize;
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800634 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800635
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800636 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc5765372011-04-28 18:26:48 -0700637 AllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800638 }
639
Jason Sams7e8aae72011-05-26 16:33:01 -0700640 texAlloc->sendDirty(rsc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800641 return texAlloc;
642}
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800643
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700644void rsi_AllocationCopy2DRange(Context *rsc,
645 RsAllocation dstAlloc,
646 uint32_t dstXoff, uint32_t dstYoff,
647 uint32_t dstMip, uint32_t dstFace,
648 uint32_t width, uint32_t height,
649 RsAllocation srcAlloc,
650 uint32_t srcXoff, uint32_t srcYoff,
651 uint32_t srcMip, uint32_t srcFace) {
652 Allocation *dst = static_cast<Allocation *>(dstAlloc);
653 Allocation *src= static_cast<Allocation *>(srcAlloc);
654 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
655 (RsAllocationCubemapFace)dstFace,
656 width, height,
657 src, srcXoff, srcYoff,srcMip,
658 (RsAllocationCubemapFace)srcFace);
659}
660
Jason Samsc5765372011-04-28 18:26:48 -0700661}
662}
663
664const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
665 Allocation *a = static_cast<Allocation *>(va);
666 a->getType()->incUserRef();
667
668 return a->getType();
669}