blob: 8c6fedc86975a5bbe7b419cfee6d729d29389f02 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Stephen Hines6ae039b2012-01-18 18:46:27 -08002 * Copyright (C) 2009-2012 The Android Open Source Project
Jason Sams326e0dd2009-05-22 14:03:28 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jason Sams326e0dd2009-05-22 14:03:28 -070016
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080017#include "rsContext.h"
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080018#include "rsAllocation.h"
19#include "rsAdapter.h"
Jason Samseb4fe182011-05-26 16:33:01 -070020#include "rs_hal.h"
21
Jason Sams7ac2a4d2012-02-15 12:04:24 -080022#include "system/window.h"
Jason Sams3522f402012-03-23 11:47:26 -070023#include "gui/SurfaceTexture.h"
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070024
Jason Sams326e0dd2009-05-22 14:03:28 -070025using namespace android;
26using namespace android::renderscript;
27
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080028Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080029 RsAllocationMipmapControl mc, void * ptr)
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080030 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080031
Jason Samseb4fe182011-05-26 16:33:01 -070032 memset(&mHal, 0, sizeof(mHal));
33 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsbad80742011-03-16 16:29:28 -070034 mHal.state.usageFlags = usages;
35 mHal.state.mipmapControl = mc;
Jason Sams366c9c82010-12-08 16:14:36 -080036
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070037 setType(type);
Jason Samsbad80742011-03-16 16:29:28 -070038 updateCache();
39}
Jason Samsfa84da22010-03-01 15:31:04 -080040
Jason Samseb4fe182011-05-26 16:33:01 -070041Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
Jason Sams179e9a42011-11-23 15:02:15 -080042 RsAllocationMipmapControl mc, void * ptr) {
43 Allocation *a = new Allocation(rsc, type, usages, mc, ptr);
Jason Samseb4fe182011-05-26 16:33:01 -070044
45 if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
46 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
47 delete a;
48 return NULL;
49 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -080050
Jason Samseb4fe182011-05-26 16:33:01 -070051 return a;
52}
53
Jason Samsbad80742011-03-16 16:29:28 -070054void Allocation::updateCache() {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070055 const Type *type = mHal.state.type;
Jason Samsbad80742011-03-16 16:29:28 -070056 mHal.state.dimensionX = type->getDimX();
57 mHal.state.dimensionY = type->getDimY();
58 mHal.state.dimensionZ = type->getDimZ();
59 mHal.state.hasFaces = type->getDimFaces();
60 mHal.state.hasMipmaps = type->getDimLOD();
61 mHal.state.elementSizeBytes = type->getElementSizeBytes();
62 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -070063}
64
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080065Allocation::~Allocation() {
Jason Samsc7cec1e2011-08-18 18:01:33 -070066 freeChildrenUnlocked();
Jason Sams3522f402012-03-23 11:47:26 -070067 setSurfaceTexture(mRSC, NULL);
Jason Samseb4fe182011-05-26 16:33:01 -070068 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070069}
70
Jason Sams366c9c82010-12-08 16:14:36 -080071void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -070072 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -080073}
74
Jason Sams4b45b892010-12-29 14:31:29 -080075void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -080076 uint32_t count, const void *data, size_t sizeBytes) {
77 const size_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -070078
Jason Samseb4fe182011-05-26 16:33:01 -070079 if ((count * eSize) != sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -080080 ALOGE("Allocation::subData called with mismatched size expected %zu, got %zu",
Jason Samseb4fe182011-05-26 16:33:01 -070081 (count * eSize), sizeBytes);
Jason Samsbad80742011-03-16 16:29:28 -070082 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -070083 return;
84 }
Jason Samse3929c92010-08-09 18:13:33 -070085
Jason Samseb4fe182011-05-26 16:33:01 -070086 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
87 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070088}
89
Jason Sams4b45b892010-12-29 14:31:29 -080090void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -080091 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
92 const size_t eSize = mHal.state.elementSizeBytes;
93 const size_t lineSize = eSize * w;
Jason Sams326e0dd2009-05-22 14:03:28 -070094
Steve Blockaf12ac62012-01-06 19:20:56 +000095 //ALOGE("data2d %p, %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -070096
Jason Samsa2371512011-01-12 13:28:37 -080097 if ((lineSize * h) != sizeBytes) {
Stephen Hines6ae039b2012-01-18 18:46:27 -080098 ALOGE("Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -070099 rsAssert(!"Allocation::subData called with mismatched size");
100 return;
101 }
102
Jason Samseb4fe182011-05-26 16:33:01 -0700103 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes);
104 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700105}
106
Jason Sams236385b2011-01-12 14:53:25 -0800107void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
108 uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800109 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700110}
111
Jason Sams807fdc42012-07-25 17:55:39 -0700112void Allocation::read(Context *rsc, uint32_t xoff, uint32_t lod,
113 uint32_t count, void *data, size_t sizeBytes) {
114 const size_t eSize = mHal.state.type->getElementSizeBytes();
115
116 if ((count * eSize) != sizeBytes) {
117 ALOGE("Allocation::read called with mismatched size expected %zu, got %zu",
118 (count * eSize), sizeBytes);
119 mHal.state.type->dumpLOGV("type info");
120 return;
121 }
122
123 rsc->mHal.funcs.allocation.read1D(rsc, this, xoff, lod, count, data, sizeBytes);
124}
125
126void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
127 uint32_t w, uint32_t h, void *data, size_t sizeBytes) {
128 const size_t eSize = mHal.state.elementSizeBytes;
129 const size_t lineSize = eSize * w;
130
131 if ((lineSize * h) != sizeBytes) {
132 ALOGE("Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
133 rsAssert(!"Allocation::read called with mismatched size");
134 return;
135 }
136
137 rsc->mHal.funcs.allocation.read2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes);
138}
139
140void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
141 uint32_t lod, RsAllocationCubemapFace face,
142 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes) {
143}
144
Jason Sams4b45b892010-12-29 14:31:29 -0800145void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800146 uint32_t cIdx, size_t sizeBytes) {
147 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700148
Jason Samsbad80742011-03-16 16:29:28 -0700149 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000150 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700151 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
152 return;
153 }
154
Jason Samsbad80742011-03-16 16:29:28 -0700155 if (x >= mHal.state.dimensionX) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000156 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700157 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
158 return;
159 }
160
Jason Samsbad80742011-03-16 16:29:28 -0700161 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800162 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
163 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800164 ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700165 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
166 return;
167 }
168
Jason Samseb4fe182011-05-26 16:33:01 -0700169 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
170 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700171}
172
Jason Sams4b45b892010-12-29 14:31:29 -0800173void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800174 const void *data, uint32_t cIdx, size_t sizeBytes) {
175 size_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700176
Jason Samsbad80742011-03-16 16:29:28 -0700177 if (x >= mHal.state.dimensionX) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000178 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700179 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
180 return;
181 }
182
Jason Samsbad80742011-03-16 16:29:28 -0700183 if (y >= mHal.state.dimensionY) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000184 ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700185 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
186 return;
187 }
188
Jason Samsbad80742011-03-16 16:29:28 -0700189 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000190 ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700191 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
192 return;
193 }
194
Jason Samsbad80742011-03-16 16:29:28 -0700195 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Alex Sakhartchouk76946322012-02-02 09:47:26 -0800196 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
197 if (sizeBytes != e->getSizeBytes() * elemArraySize) {
Stephen Hines6ae039b2012-01-18 18:46:27 -0800198 ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700199 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
200 return;
201 }
202
Jason Samseb4fe182011-05-26 16:33:01 -0700203 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
204 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700205}
206
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800207void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700208 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700209}
Jason Sams326e0dd2009-05-22 14:03:28 -0700210
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800211void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700212 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
213 if (mToDirtyList[ct] == p) {
214 mToDirtyList.removeAt(ct);
215 return;
216 }
217 }
218 rsAssert(0);
219}
220
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800221void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800222 ObjectBase::dumpLOGV(prefix);
223
224 String8 s(prefix);
225 s.append(" type ");
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700226 if (mHal.state.type) {
Jason Samsbad80742011-03-16 16:29:28 -0700227 mHal.state.type->dumpLOGV(s.string());
Jason Samsc21cf402009-11-17 17:26:46 -0800228 }
229
Steve Block65982012011-10-20 11:56:00 +0100230 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
Jason Sams61a4bb72012-07-25 19:33:43 -0700231 prefix, mHal.drvState.mallocPtrLOD0, mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800232}
Jason Sams326e0dd2009-05-22 14:03:28 -0700233
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800234uint32_t Allocation::getPackedSize() const {
235 uint32_t numItems = mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes();
236 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
237}
238
Jason Samse3150cf2012-07-24 18:10:20 -0700239void Allocation::writePackedData(Context *rsc, const Type *type,
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800240 uint8_t *dst, const uint8_t *src, bool dstPadded) {
241 const Element *elem = type->getElement();
242 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
243 uint32_t paddedBytes = elem->getSizeBytes();
244 uint32_t numItems = type->getSizeBytes() / paddedBytes;
245
246 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
247 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes;
248
249 // no sub-elements
250 uint32_t fieldCount = elem->getFieldCount();
251 if (fieldCount == 0) {
252 for (uint32_t i = 0; i < numItems; i ++) {
253 memcpy(dst, src, unpaddedBytes);
254 src += srcInc;
255 dst += dstInc;
256 }
257 return;
258 }
259
260 // Cache offsets
261 uint32_t *offsetsPadded = new uint32_t[fieldCount];
262 uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
263 uint32_t *sizeUnpadded = new uint32_t[fieldCount];
264
265 for (uint32_t i = 0; i < fieldCount; i++) {
266 offsetsPadded[i] = elem->getFieldOffsetBytes(i);
267 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
268 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
269 }
270
271 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
272 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded;
273
274 // complex elements, need to copy subelem after subelem
275 for (uint32_t i = 0; i < numItems; i ++) {
276 for (uint32_t fI = 0; fI < fieldCount; fI++) {
277 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
278 }
279 src += srcInc;
280 dst += dstInc;
281 }
282
283 delete[] offsetsPadded;
284 delete[] offsetsUnpadded;
285 delete[] sizeUnpadded;
286}
287
Jason Samse3150cf2012-07-24 18:10:20 -0700288void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800289 const uint8_t *src = (const uint8_t*)data;
Jason Sams61a4bb72012-07-25 19:33:43 -0700290 uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800291
Jason Samse3150cf2012-07-24 18:10:20 -0700292 writePackedData(rsc, getType(), dst, src, true);
Jason Sams61a4bb72012-07-25 19:33:43 -0700293 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800294}
295
Jason Samse3150cf2012-07-24 18:10:20 -0700296void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const {
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800297 uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
298 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
299 uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes;
300
Jason Sams61a4bb72012-07-25 19:33:43 -0700301 const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800302 uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
303
Jason Samse3150cf2012-07-24 18:10:20 -0700304 writePackedData(rsc, getType(), dst, src, false);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800305 stream->addByteArray(dst, getPackedSize());
306
307 delete[] dst;
Jason Sams61a4bb72012-07-25 19:33:43 -0700308 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800309}
310
Jason Samse3150cf2012-07-24 18:10:20 -0700311void Allocation::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700312 // Need to identify ourselves
313 stream->addU32((uint32_t)getClassId());
314
315 String8 name(getName());
316 stream->addString(&name);
317
318 // First thing we need to serialize is the type object since it will be needed
319 // to initialize the class
Jason Samse3150cf2012-07-24 18:10:20 -0700320 mHal.state.type->serialize(rsc, stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700321
Jason Samsbad80742011-03-16 16:29:28 -0700322 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800323 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
324 uint32_t packedSize = getPackedSize();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700325 // Write how much data we are storing
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800326 stream->addU32(packedSize);
327 if (dataSize == packedSize) {
328 // Now write the data
Jason Sams61a4bb72012-07-25 19:33:43 -0700329 stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize);
330 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800331 } else {
332 // Now write the data
Jason Samse3150cf2012-07-24 18:10:20 -0700333 packVec3Allocation(rsc, stream);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800334 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700335}
336
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800337Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700338 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700339 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800340 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000341 ALOGE("allocation loading skipped due to invalid class id\n");
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700342 return NULL;
343 }
344
345 String8 name;
346 stream->loadString(&name);
347
348 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800349 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700350 return NULL;
351 }
352 type->compute();
353
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800354 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
355 type->decUserRef();
356
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700357 // Number of bytes we wrote out for this allocation
358 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800359 // 3 element vectors are padded to 4 in memory, but padding isn't serialized
360 uint32_t packedSize = alloc->getPackedSize();
361 if (dataSize != type->getSizeBytes() &&
362 dataSize != packedSize) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000363 ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800364 ObjectBase::checkDelete(alloc);
Jason Sams225afd32010-10-21 14:06:55 -0700365 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700366 return NULL;
367 }
368
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700369 alloc->setName(name.string(), name.size());
370
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800371 if (dataSize == type->getSizeBytes()) {
372 uint32_t count = dataSize / type->getElementSizeBytes();
373 // Read in all of our allocation data
374 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
375 } else {
Jason Samse3150cf2012-07-24 18:10:20 -0700376 alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800377 }
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700378 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700379
380 return alloc;
381}
382
Jason Samseb4fe182011-05-26 16:33:01 -0700383void Allocation::sendDirty(const Context *rsc) const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700384 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
385 mToDirtyList[ct]->forceDirty();
386 }
Jason Samseb4fe182011-05-26 16:33:01 -0700387 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700388}
Jason Sams326e0dd2009-05-22 14:03:28 -0700389
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800390void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700391 mHal.state.type->incRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700392}
393
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800394void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700395 if (!mHal.state.hasReferences || !getIsScript()) {
396 return;
397 }
Stephen Hines9f70a4e2012-04-02 20:18:48 -0700398 mHal.state.type->decRefs(ptr, ct, startOff);
Jason Samse3929c92010-08-09 18:13:33 -0700399}
400
Jason Samsc7cec1e2011-08-18 18:01:33 -0700401void Allocation::freeChildrenUnlocked () {
Jason Sams61a4bb72012-07-25 19:33:43 -0700402 void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this);
403 decRefs(ptr, mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
404 mRSC->mHal.funcs.allocation.unlock1D(mRSC, this);
Jason Samsc7cec1e2011-08-18 18:01:33 -0700405}
406
407bool Allocation::freeChildren() {
408 if (mHal.state.hasReferences) {
409 incSysRef();
410 freeChildrenUnlocked();
411 return decSysRef();
412 }
413 return false;
414}
415
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800416void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700417}
418
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800419void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsbad80742011-03-16 16:29:28 -0700420 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams96abf812010-10-05 13:32:49 -0700421 if (dimX == oldDimX) {
422 return;
423 }
424
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700425 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700426 if (dimX < oldDimX) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700427 decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX);
428 rsc->mHal.funcs.allocation.unlock1D(rsc, this);
Jason Sams96abf812010-10-05 13:32:49 -0700429 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700430 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700431 setType(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700432 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700433}
434
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800435void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000436 ALOGE("not implemented");
Jason Sams96abf812010-10-05 13:32:49 -0700437}
438
Jason Sams41e373d2012-01-13 14:01:20 -0800439int32_t Allocation::getSurfaceTextureID(const Context *rsc) {
440 int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this);
441 mHal.state.surfaceTextureID = id;
442 return id;
443}
444
Jason Sams3522f402012-03-23 11:47:26 -0700445void Allocation::setSurfaceTexture(const Context *rsc, SurfaceTexture *st) {
446 if(st != mHal.state.surfaceTexture) {
447 if(mHal.state.surfaceTexture != NULL) {
448 mHal.state.surfaceTexture->decStrong(NULL);
449 }
450 mHal.state.surfaceTexture = st;
451 if(mHal.state.surfaceTexture != NULL) {
452 mHal.state.surfaceTexture->incStrong(NULL);
453 }
454 }
455}
456
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800457void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
458 ANativeWindow *nw = (ANativeWindow *)sur;
459 ANativeWindow *old = mHal.state.wndSurface;
460 if (nw) {
461 nw->incStrong(NULL);
462 }
463 rsc->mHal.funcs.allocation.setSurfaceTexture(rsc, this, nw);
464 mHal.state.wndSurface = nw;
465 if (old) {
466 old->decStrong(NULL);
467 }
468}
469
470void Allocation::ioSend(const Context *rsc) {
471 rsc->mHal.funcs.allocation.ioSend(rsc, this);
472}
473
474void Allocation::ioReceive(const Context *rsc) {
475 rsc->mHal.funcs.allocation.ioReceive(rsc, this);
476}
477
478
Jason Sams326e0dd2009-05-22 14:03:28 -0700479/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700480//
Stephen Hines6a121812011-03-01 17:34:59 -0800481
Jason Sams326e0dd2009-05-22 14:03:28 -0700482namespace android {
483namespace renderscript {
484
Jason Sams366c9c82010-12-08 16:14:36 -0800485void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
486 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700487 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800488 a->syncAll(rsc, src);
489}
490
Jason Samsa2371512011-01-12 13:28:37 -0800491void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700492 Allocation *alloc = static_cast<Allocation *>(va);
493 rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc);
Jason Sams837e3882010-12-10 16:03:15 -0800494}
495
Jason Sams807fdc42012-07-25 17:55:39 -0700496void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
497 Allocation *a = static_cast<Allocation *>(va);
498 const Type * t = a->getType();
499 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
500 t->getDimX(), t->getDimY(), data, sizeBytes);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700501}
502
Jason Sams4b45b892010-12-29 14:31:29 -0800503void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700504 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700505 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800506 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700507}
508
Jason Sams4b45b892010-12-29 14:31:29 -0800509void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800510 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700511 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800512 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700513}
514
Jason Sams4b45b892010-12-29 14:31:29 -0800515void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Stephen Hines6ae039b2012-01-18 18:46:27 -0800516 const void *data, size_t sizeBytes, size_t eoff) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700517 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800518 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700519}
520
Jason Sams4b45b892010-12-29 14:31:29 -0800521void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700522 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700523 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800524 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700525}
526
Jason Sams807fdc42012-07-25 17:55:39 -0700527void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
Jason Samse579df42009-08-10 14:55:26 -0700528 Allocation *a = static_cast<Allocation *>(va);
Jason Sams807fdc42012-07-25 17:55:39 -0700529 const Type * t = a->getType();
530 if(t->getDimY()) {
531 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
532 t->getDimX(), t->getDimY(), data, sizeBytes);
533 } else {
534 a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes);
535 }
536
Jason Samse579df42009-08-10 14:55:26 -0700537}
538
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800539void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700540 Allocation *a = static_cast<Allocation *>(va);
541 a->resize1D(rsc, dimX);
542}
543
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800544void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700545 Allocation *a = static_cast<Allocation *>(va);
546 a->resize2D(rsc, dimX, dimY);
547}
548
Jason Samsc975cf42011-04-28 18:26:48 -0700549RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
550 RsAllocationMipmapControl mips,
Jason Sams179e9a42011-11-23 15:02:15 -0800551 uint32_t usages, uint32_t ptr) {
552 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700553 if (!alloc) {
554 return NULL;
555 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700556 alloc->incUserRef();
557 return alloc;
558}
559
Jason Samsc975cf42011-04-28 18:26:48 -0700560RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
561 RsAllocationMipmapControl mips,
Jason Sams807fdc42012-07-25 17:55:39 -0700562 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800563 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700564
Jason Sams179e9a42011-11-23 15:02:15 -0800565 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700566 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
567 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000568 ALOGE("Memory allocation failure");
Jason Samsf0c1df42010-10-26 13:09:17 -0700569 return NULL;
570 }
571
Jason Sams807fdc42012-07-25 17:55:39 -0700572 texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
573 t->getDimX(), t->getDimY(), data, sizeBytes);
Jason Samsebc50192010-12-13 15:32:35 -0800574 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700575 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700576 }
577
Jason Samseb4fe182011-05-26 16:33:01 -0700578 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700579 return texAlloc;
580}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800581
Jason Samsc975cf42011-04-28 18:26:48 -0700582RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
583 RsAllocationMipmapControl mips,
Jason Sams807fdc42012-07-25 17:55:39 -0700584 const void *data, size_t sizeBytes, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800585 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800586
587 // Cubemap allocation's faces should be Width by Width each.
588 // Source data should have 6 * Width by Width pixels
589 // Error checking is done in the java layer
Jason Sams179e9a42011-11-23 15:02:15 -0800590 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800591 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
592 if (texAlloc == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000593 ALOGE("Memory allocation failure");
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800594 return NULL;
595 }
596
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800597 uint32_t faceSize = t->getDimX();
598 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
599 uint32_t copySize = faceSize * t->getElementSizeBytes();
600
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800601 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800602 for (uint32_t face = 0; face < 6; face ++) {
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800603 for (uint32_t dI = 0; dI < faceSize; dI ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700604 texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face,
605 t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize);
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800606 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800607
Jason Sams366c9c82010-12-08 16:14:36 -0800608 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800609 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800610 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800611
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800612 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700613 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800614 }
615
Jason Samseb4fe182011-05-26 16:33:01 -0700616 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800617 return texAlloc;
618}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800619
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700620void rsi_AllocationCopy2DRange(Context *rsc,
621 RsAllocation dstAlloc,
622 uint32_t dstXoff, uint32_t dstYoff,
623 uint32_t dstMip, uint32_t dstFace,
624 uint32_t width, uint32_t height,
625 RsAllocation srcAlloc,
626 uint32_t srcXoff, uint32_t srcYoff,
627 uint32_t srcMip, uint32_t srcFace) {
628 Allocation *dst = static_cast<Allocation *>(dstAlloc);
629 Allocation *src= static_cast<Allocation *>(srcAlloc);
630 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
631 (RsAllocationCubemapFace)dstFace,
632 width, height,
633 src, srcXoff, srcYoff,srcMip,
634 (RsAllocationCubemapFace)srcFace);
635}
636
Jason Sams41e373d2012-01-13 14:01:20 -0800637int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) {
638 Allocation *alloc = static_cast<Allocation *>(valloc);
639 return alloc->getSurfaceTextureID(rsc);
640}
641
Jason Sams3522f402012-03-23 11:47:26 -0700642void rsi_AllocationGetSurfaceTextureID2(Context *rsc, RsAllocation valloc, void *vst, size_t len) {
643 Allocation *alloc = static_cast<Allocation *>(valloc);
644 alloc->setSurfaceTexture(rsc, static_cast<SurfaceTexture *>(vst));
645}
646
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800647void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
648 Allocation *alloc = static_cast<Allocation *>(valloc);
649 alloc->setSurface(rsc, sur);
650}
651
652void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
653 Allocation *alloc = static_cast<Allocation *>(valloc);
654 alloc->ioSend(rsc);
655}
656
657void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
658 Allocation *alloc = static_cast<Allocation *>(valloc);
659 alloc->ioReceive(rsc);
660}
661
Jason Samsc975cf42011-04-28 18:26:48 -0700662}
663}
664
665const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
666 Allocation *a = static_cast<Allocation *>(va);
667 a->getType()->incUserRef();
668
669 return a->getType();
670}