blob: 02c680939de38e6749f3776364cc50abc4a6c651 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
Stephen Hines4cbe25a2012-01-18 18:46:27 -08002 * Copyright (C) 2009-2012 The Android Open Source Project
Jason Samsd19f10d2009-05-22 14:03:28 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jason 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,
Stephen Hines4cbe25a2012-01-18 18:46:27 -080075 uint32_t count, const void *data, size_t sizeBytes) {
76 const size_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) {
Stephen Hines4cbe25a2012-01-18 18:46:27 -080079 ALOGE("Allocation::subData called with mismatched size expected %zu, got %zu",
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,
Stephen Hines4cbe25a2012-01-18 18:46:27 -080090 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
91 const size_t eSize = mHal.state.elementSizeBytes;
92 const size_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) {
Stephen Hines4cbe25a2012-01-18 18:46:27 -080097 ALOGE("Allocation size mismatch, expected %zu, got %zu", (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,
Stephen Hines4cbe25a2012-01-18 18:46:27 -0800108 uint32_t w, uint32_t h, uint32_t d, const void *data, size_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,
Stephen Hines4cbe25a2012-01-18 18:46:27 -0800112 uint32_t cIdx, size_t sizeBytes) {
113 size_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);
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800128 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
129 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Stephen Hines4cbe25a2012-01-18 18:46:27 -0800130 ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams49bdaf02010-08-31 13:50:42 -0700131 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
132 return;
133 }
134
Jason Sams7e8aae72011-05-26 16:33:01 -0700135 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
136 sendDirty(rsc);
Jason Sams49bdaf02010-08-31 13:50:42 -0700137}
138
Jason Sams49a05d72010-12-29 14:31:29 -0800139void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Stephen Hines4cbe25a2012-01-18 18:46:27 -0800140 const void *data, uint32_t cIdx, size_t sizeBytes) {
141 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams49bdaf02010-08-31 13:50:42 -0700142
Jason Samse4a06c52011-03-16 16:29:28 -0700143 if (x >= mHal.state.dimensionX) {
Steve Block3762c312012-01-06 19:20:56 +0000144 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams49bdaf02010-08-31 13:50:42 -0700145 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
146 return;
147 }
148
Jason Samse4a06c52011-03-16 16:29:28 -0700149 if (y >= mHal.state.dimensionY) {
Steve Block3762c312012-01-06 19:20:56 +0000150 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams49bdaf02010-08-31 13:50:42 -0700151 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
152 return;
153 }
154
Jason Samse4a06c52011-03-16 16:29:28 -0700155 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Block3762c312012-01-06 19:20:56 +0000156 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -0700157 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
158 return;
159 }
160
Jason Samse4a06c52011-03-16 16:29:28 -0700161 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800162 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
163 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Stephen Hines4cbe25a2012-01-18 18:46:27 -0800164 ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams49bdaf02010-08-31 13:50:42 -0700165 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
166 return;
167 }
168
Jason Sams7e8aae72011-05-26 16:33:01 -0700169 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
170 sendDirty(rsc);
Jason Sams49bdaf02010-08-31 13:50:42 -0700171}
172
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800173void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700174 mToDirtyList.push(p);
Jason Sams83f1c632009-10-26 15:19:28 -0700175}
Jason Samsd19f10d2009-05-22 14:03:28 -0700176
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800177void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams83f1c632009-10-26 15:19:28 -0700178 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
179 if (mToDirtyList[ct] == p) {
180 mToDirtyList.removeAt(ct);
181 return;
182 }
183 }
184 rsAssert(0);
185}
186
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800187void Allocation::dumpLOGV(const char *prefix) const {
Jason Sams715333b2009-11-17 17:26:46 -0800188 ObjectBase::dumpLOGV(prefix);
189
190 String8 s(prefix);
191 s.append(" type ");
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -0700192 if (mHal.state.type) {
Jason Samse4a06c52011-03-16 16:29:28 -0700193 mHal.state.type->dumpLOGV(s.string());
Jason Sams715333b2009-11-17 17:26:46 -0800194 }
195
Steve Block71f2cf12011-10-20 11:56:00 +0100196 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Jason Sams7e8aae72011-05-26 16:33:01 -0700197 prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Sams715333b2009-11-17 17:26:46 -0800198}
Jason Samsd19f10d2009-05-22 14:03:28 -0700199
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800200uint32_t Allocation::getPackedSize() const {
201 uint32_t numItems = mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes();
202 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
203}
204
205void Allocation::writePackedData(const Type *type,
206 uint8_t *dst, const uint8_t *src, bool dstPadded) {
207 const Element *elem = type->getElement();
208 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
209 uint32_t paddedBytes = elem->getSizeBytes();
210 uint32_t numItems = type->getSizeBytes() / paddedBytes;
211
212 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
213 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
214
215 // no sub-elements
216 uint32_t fieldCount = elem->getFieldCount();
217 if (fieldCount == 0) {
218 for (uint32_t i = 0; i < numItems; i ++) {
219 memcpy(dst, src, unpaddedBytes);
220 src += srcInc;
221 dst += dstInc;
222 }
223 return;
224 }
225
226 // Cache offsets
227 uint32_t *offsetsPadded = new uint32_t[fieldCount];
228 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
229 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
230
231 for (uint32_t i = 0; i < fieldCount; i++) {
232 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
233 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
234 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
235 }
236
237 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
238 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
239
240 // complex elements, need to copy subelem after subelem
241 for (uint32_t i = 0; i < numItems; i ++) {
242 for (uint32_t fI = 0; fI < fieldCount; fI++) {
243 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
244 }
245 src += srcInc;
246 dst += dstInc;
247 }
248
249 delete[] offsetsPadded;
250 delete[] offsetsUnpadded;
251 delete[] sizeUnpadded;
252}
253
Stephen Hines4cbe25a2012-01-18 18:46:27 -0800254void Allocation::unpackVec3Allocation(const void *data, size_t dataSize) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800255 const uint8_t *src = (const uint8_t*)data;
256 uint8_t *dst = (uint8_t*)getPtr();
257
258 writePackedData(getType(), dst, src, true);
259}
260
261void Allocation::packVec3Allocation(OStream *stream) const {
262 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
263 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
264 uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes;
265
266 const uint8_t *src = (const uint8_t*)getPtr();
267 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
268
269 writePackedData(getType(), dst, src, false);
270 stream->addByteArray(dst, getPackedSize());
271
272 delete[] dst;
273}
274
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800275void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700276 // Need to identify ourselves
277 stream->addU32((uint32_t)getClassId());
278
279 String8 name(getName());
280 stream->addString(&name);
281
282 // First thing we need to serialize is the type object since it will be needed
283 // to initialize the class
Jason Samse4a06c52011-03-16 16:29:28 -0700284 mHal.state.type->serialize(stream);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700285
Jason Samse4a06c52011-03-16 16:29:28 -0700286 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800287 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
288 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700289 // Write how much data we are storing
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800290 stream->addU32(packedSize);
291 if (dataSize == packedSize) {
292 // Now write the data
293 stream->addByteArray(getPtr(), dataSize);
294 } else {
295 // Now write the data
296 packVec3Allocation(stream);
297 }
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700298}
299
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800300Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700301 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700302 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800303 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Steve Block3762c312012-01-06 19:20:56 +0000304 ALOGE("allocation loading skipped due to invalid class id\n");
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700305 return NULL;
306 }
307
308 String8 name;
309 stream->loadString(&name);
310
311 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800312 if (!type) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700313 return NULL;
314 }
315 type->compute();
316
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800317 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
318 type->decUserRef();
319
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700320 // Number of bytes we wrote out for this allocation
321 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800322 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
323 uint32_t packedSize = alloc->getPackedSize();
324 if (dataSize != type->getSizeBytes() &&
325 dataSize != packedSize) {
Steve Block3762c312012-01-06 19:20:56 +0000326 ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800327 ObjectBase::checkDelete(alloc);
Jason Samsb38d5342010-10-21 14:06:55 -0700328 ObjectBase::checkDelete(type);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700329 return NULL;
330 }
331
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700332 alloc->setName(name.string(), name.size());
333
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800334 if (dataSize == type->getSizeBytes()) {
335 uint32_t count = dataSize / type->getElementSizeBytes();
336 // Read in all of our allocation data
337 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
338 } else {
339 alloc->unpackVec3Allocation(stream->getPtr() + stream->getPos(), dataSize);
340 }
Alex Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700341 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700342
343 return alloc;
344}
345
Jason Sams7e8aae72011-05-26 16:33:01 -0700346void Allocation::sendDirty(const Context *rsc) const {
Jason Sams83f1c632009-10-26 15:19:28 -0700347 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
348 mToDirtyList[ct]->forceDirty();
349 }
Jason Sams7e8aae72011-05-26 16:33:01 -0700350 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams83f1c632009-10-26 15:19:28 -0700351}
Jason Samsd19f10d2009-05-22 14:03:28 -0700352
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800353void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700354 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samse4a06c52011-03-16 16:29:28 -0700355 const Element *e = mHal.state.type->getElement();
Jason Samsb28ca96f2010-08-09 18:13:33 -0700356 uint32_t stride = e->getSizeBytes();
357
Jason Sams5edc6082010-10-05 13:32:49 -0700358 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700359 while (ct > 0) {
360 e->incRefs(p);
361 ct --;
362 p += stride;
363 }
364}
365
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800366void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk065fa8d2011-08-12 11:30:30 -0700367 if (!mHal.state.hasReferences || !getIsScript()) {
368 return;
369 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700370 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samse4a06c52011-03-16 16:29:28 -0700371 const Element *e = mHal.state.type->getElement();
Jason Samsb28ca96f2010-08-09 18:13:33 -0700372 uint32_t stride = e->getSizeBytes();
373
Jason Sams5edc6082010-10-05 13:32:49 -0700374 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700375 while (ct > 0) {
376 e->decRefs(p);
377 ct --;
378 p += stride;
379 }
380}
381
Jason Sams777ec262011-08-18 18:01:33 -0700382void Allocation::freeChildrenUnlocked () {
383 decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
384}
385
386bool Allocation::freeChildren() {
387 if (mHal.state.hasReferences) {
388 incSysRef();
389 freeChildrenUnlocked();
390 return decSysRef();
391 }
392 return false;
393}
394
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800395void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams5edc6082010-10-05 13:32:49 -0700396}
397
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800398void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samse4a06c52011-03-16 16:29:28 -0700399 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams5edc6082010-10-05 13:32:49 -0700400 if (dimX == oldDimX) {
401 return;
402 }
403
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700404 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -0700405 if (dimX < oldDimX) {
Jason Sams7e8aae72011-05-26 16:33:01 -0700406 decRefs(getPtr(), oldDimX - dimX, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -0700407 }
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700408 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -0700409 setType(t.get());
Jason Samse4a06c52011-03-16 16:29:28 -0700410 updateCache();
Jason Sams5edc6082010-10-05 13:32:49 -0700411}
412
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800413void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Steve Block3762c312012-01-06 19:20:56 +0000414 ALOGE("not implemented");
Jason Sams5edc6082010-10-05 13:32:49 -0700415}
416
Jason Sams615e7ce2012-01-13 14:01:20 -0800417int32_t Allocation::getSurfaceTextureID(const Context *rsc) {
418 int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this);
419 mHal.state.surfaceTextureID = id;
420 return id;
421}
422
Jason Samsd19f10d2009-05-22 14:03:28 -0700423/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700424//
Stephen Hines60f9a622011-03-01 17:34:59 -0800425
Jason Samsd19f10d2009-05-22 14:03:28 -0700426namespace android {
427namespace renderscript {
428
Jason Samsc5765372011-04-28 18:26:48 -0700429static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
430
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800431static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700432 uint32_t w = out.getDimX();
433 uint32_t h = out.getDimY();
434
Jason Sams6f5c61c2009-07-28 17:20:11 -0700435 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700436 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
437 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
438 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
439
Jason Sams6f5c61c2009-07-28 17:20:11 -0700440 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700441 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
442 oPtr ++;
443 i1 += 2;
444 i2 += 2;
445 }
446 }
447}
448
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800449static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700450 uint32_t w = out.getDimX();
451 uint32_t h = out.getDimY();
452
Jason Sams6f5c61c2009-07-28 17:20:11 -0700453 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700454 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
455 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
456 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
457
Jason Sams6f5c61c2009-07-28 17:20:11 -0700458 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700459 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700460 oPtr ++;
461 i1 += 2;
462 i2 += 2;
463 }
464 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700465}
466
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800467static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Samse20e3b42010-01-19 17:53:54 -0800468 uint32_t w = out.getDimX();
469 uint32_t h = out.getDimY();
470
471 for (uint32_t y=0; y < h; y++) {
472 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
473 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
474 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
475
476 for (uint32_t x=0; x < w; x++) {
477 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
478 oPtr ++;
479 i1 += 2;
480 i2 += 2;
481 }
482 }
483}
484
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800485static void mip(const Adapter2D &out, const Adapter2D &in) {
486 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Sams6f5c61c2009-07-28 17:20:11 -0700487 case 32:
488 mip8888(out, in);
489 break;
490 case 16:
491 mip565(out, in);
492 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800493 case 8:
494 mip8(out, in);
495 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700496 }
Jason Sams6f5c61c2009-07-28 17:20:11 -0700497}
Jason Samsd19f10d2009-05-22 14:03:28 -0700498
Jason Sams5476b452010-12-08 16:14:36 -0800499void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
500 Allocation *a = static_cast<Allocation *>(va);
Jason Sams7e8aae72011-05-26 16:33:01 -0700501 a->sendDirty(rsc);
Jason Sams5476b452010-12-08 16:14:36 -0800502 a->syncAll(rsc, src);
503}
504
Jason Samsf7086092011-01-12 13:28:37 -0800505void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700506 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsc5765372011-04-28 18:26:48 -0700507 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams4ef66502010-12-10 16:03:15 -0800508}
509
510void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
511 Allocation *texAlloc = static_cast<Allocation *>(va);
512 const Type * t = texAlloc->getType();
513
514 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
515 if (s != dataLen) {
516 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
517 return;
518 }
519
520 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700521}
522
Jason Sams49a05d72010-12-29 14:31:29 -0800523void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700524 uint32_t count, const void *data, size_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700525 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800526 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700527}
528
Jason Sams49a05d72010-12-29 14:31:29 -0800529void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines4cbe25a2012-01-18 18:46:27 -0800530 const void *data, size_t sizeBytes, size_t eoff) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700531 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800532 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700533}
534
Jason Sams49a05d72010-12-29 14:31:29 -0800535void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Stephen Hines4cbe25a2012-01-18 18:46:27 -0800536 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700537 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800538 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700539}
540
Jason Sams49a05d72010-12-29 14:31:29 -0800541void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700542 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700543 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800544 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700545}
546
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700547void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
Jason Sams40a29e82009-08-10 14:55:26 -0700548 Allocation *a = static_cast<Allocation *>(va);
549 a->read(data);
550}
551
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800552void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700553 Allocation *a = static_cast<Allocation *>(va);
554 a->resize1D(rsc, dimX);
555}
556
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800557void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700558 Allocation *a = static_cast<Allocation *>(va);
559 a->resize2D(rsc, dimX, dimY);
560}
561
Jason Samsc5765372011-04-28 18:26:48 -0700562static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800563 Context *rsc = static_cast<Context *>(con);
564 Allocation *texAlloc = static_cast<Allocation *>(va);
565 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
566 for (uint32_t face = 0; face < numFaces; face ++) {
567 Adapter2D adapt(rsc, texAlloc);
568 Adapter2D adapt2(rsc, texAlloc);
569 adapt.setFace(face);
570 adapt2.setFace(face);
571 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
572 adapt.setLOD(lod);
573 adapt2.setLOD(lod + 1);
574 mip(adapt2, adapt);
575 }
576 }
577}
578
Jason Samsc5765372011-04-28 18:26:48 -0700579RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
580 RsAllocationMipmapControl mips,
Jason Sams857d0c72011-11-23 15:02:15 -0800581 uint32_t usages, uint32_t ptr) {
582 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
Jason Sams7e8aae72011-05-26 16:33:01 -0700583 if (!alloc) {
584 return NULL;
585 }
Jason Sams31a7e422010-10-26 13:09:17 -0700586 alloc->incUserRef();
587 return alloc;
588}
589
Jason Samsc5765372011-04-28 18:26:48 -0700590RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
591 RsAllocationMipmapControl mips,
592 const void *data, size_t data_length, uint32_t usages) {
Jason Sams5476b452010-12-08 16:14:36 -0800593 Type *t = static_cast<Type *>(vtype);
Jason Sams31a7e422010-10-26 13:09:17 -0700594
Jason Sams857d0c72011-11-23 15:02:15 -0800595 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Jason Sams31a7e422010-10-26 13:09:17 -0700596 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
597 if (texAlloc == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000598 ALOGE("Memory allocation failure");
Jason Sams31a7e422010-10-26 13:09:17 -0700599 return NULL;
600 }
601
Jason Sams5476b452010-12-08 16:14:36 -0800602 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsd4b23b52010-12-13 15:32:35 -0800603 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc5765372011-04-28 18:26:48 -0700604 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams31a7e422010-10-26 13:09:17 -0700605 }
606
Jason Sams7e8aae72011-05-26 16:33:01 -0700607 texAlloc->sendDirty(rsc);
Jason Sams31a7e422010-10-26 13:09:17 -0700608 return texAlloc;
609}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800610
Jason Samsc5765372011-04-28 18:26:48 -0700611RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
612 RsAllocationMipmapControl mips,
613 const void *data, size_t data_length, uint32_t usages) {
Jason Sams5476b452010-12-08 16:14:36 -0800614 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800615
616 // Cubemap allocation's faces should be Width by Width each.
617 // Source data should have 6 * Width by Width pixels
618 // Error checking is done in the java layer
Jason Sams857d0c72011-11-23 15:02:15 -0800619 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800620 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
621 if (texAlloc == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000622 ALOGE("Memory allocation failure");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800623 return NULL;
624 }
625
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800626 uint32_t faceSize = t->getDimX();
627 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
628 uint32_t copySize = faceSize * t->getElementSizeBytes();
629
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800630 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams5476b452010-12-08 16:14:36 -0800631 for (uint32_t face = 0; face < 6; face ++) {
632 Adapter2D faceAdapter(rsc, texAlloc);
633 faceAdapter.setFace(face);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800634
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800635 for (uint32_t dI = 0; dI < faceSize; dI ++) {
636 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
637 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800638
Jason Sams5476b452010-12-08 16:14:36 -0800639 // Move the data pointer to the next cube face
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800640 sourcePtr += copySize;
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800641 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800642
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800643 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc5765372011-04-28 18:26:48 -0700644 AllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800645 }
646
Jason Sams7e8aae72011-05-26 16:33:01 -0700647 texAlloc->sendDirty(rsc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800648 return texAlloc;
649}
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800650
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700651void rsi_AllocationCopy2DRange(Context *rsc,
652 RsAllocation dstAlloc,
653 uint32_t dstXoff, uint32_t dstYoff,
654 uint32_t dstMip, uint32_t dstFace,
655 uint32_t width, uint32_t height,
656 RsAllocation srcAlloc,
657 uint32_t srcXoff, uint32_t srcYoff,
658 uint32_t srcMip, uint32_t srcFace) {
659 Allocation *dst = static_cast<Allocation *>(dstAlloc);
660 Allocation *src= static_cast<Allocation *>(srcAlloc);
661 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
662 (RsAllocationCubemapFace)dstFace,
663 width, height,
664 src, srcXoff, srcYoff,srcMip,
665 (RsAllocationCubemapFace)srcFace);
666}
667
Jason Sams615e7ce2012-01-13 14:01:20 -0800668int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) {
669 Allocation *alloc = static_cast<Allocation *>(valloc);
670 return alloc->getSurfaceTextureID(rsc);
671}
672
Jason Samsc5765372011-04-28 18:26:48 -0700673}
674}
675
676const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
677 Allocation *a = static_cast<Allocation *>(va);
678 a->getType()->incUserRef();
679
680 return a->getType();
681}