blob: b59ade8d9fe98a17b9a9251bea873c90e2addaca [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jason Sams326e0dd2009-05-22 14:03:28 -070016
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080017#include "rsContext.h"
Jason Samseb4fe182011-05-26 16:33:01 -070018#include "rs_hal.h"
19
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070020
Jason Sams326e0dd2009-05-22 14:03:28 -070021using namespace android;
22using namespace android::renderscript;
23
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080024Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
25 RsAllocationMipmapControl mc)
26 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080027
Jason Samseb4fe182011-05-26 16:33:01 -070028 memset(&mHal, 0, sizeof(mHal));
29 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsbad80742011-03-16 16:29:28 -070030 mHal.state.usageFlags = usages;
31 mHal.state.mipmapControl = mc;
Jason Sams366c9c82010-12-08 16:14:36 -080032
Jason Samsbad80742011-03-16 16:29:28 -070033 mHal.state.type.set(type);
34 updateCache();
35}
Jason Samsfa84da22010-03-01 15:31:04 -080036
Jason Samseb4fe182011-05-26 16:33:01 -070037Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
38 RsAllocationMipmapControl mc) {
39 Allocation *a = new Allocation(rsc, type, usages, mc);
40
41 if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
42 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
43 delete a;
44 return NULL;
45 }
46 return a;
47}
48
Jason Samsbad80742011-03-16 16:29:28 -070049void Allocation::updateCache() {
50 const Type *type = mHal.state.type.get();
51 mHal.state.dimensionX = type->getDimX();
52 mHal.state.dimensionY = type->getDimY();
53 mHal.state.dimensionZ = type->getDimZ();
54 mHal.state.hasFaces = type->getDimFaces();
55 mHal.state.hasMipmaps = type->getDimLOD();
56 mHal.state.elementSizeBytes = type->getElementSizeBytes();
57 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Sams326e0dd2009-05-22 14:03:28 -070058}
59
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080060Allocation::~Allocation() {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -070061 if (mHal.state.hasReferences &&
62 (mHal.state.hasFaces || mHal.state.hasMipmaps)) {
63 LOGE("Cube/mip allocation with references unsupported, memory not cleaned up!");
64 }
65
66 uint32_t elemCount = mHal.state.dimensionX;
67 if (mHal.state.dimensionY > 1) {
68 elemCount *= mHal.state.dimensionY;
69 }
70 if (mHal.state.dimensionZ > 1) {
71 elemCount *= mHal.state.dimensionZ;
72 }
73 decRefs(getPtr(), elemCount, 0);
Jason Samseb4fe182011-05-26 16:33:01 -070074 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070075}
76
Jason Sams366c9c82010-12-08 16:14:36 -080077void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -070078 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -080079}
80
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080081void Allocation::read(void *data) {
Jason Samseb4fe182011-05-26 16:33:01 -070082 memcpy(data, getPtr(), mHal.state.type->getSizeBytes());
Jason Samse579df42009-08-10 14:55:26 -070083}
84
Jason Sams4b45b892010-12-29 14:31:29 -080085void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
86 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -070087 const uint32_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -070088
Jason Samseb4fe182011-05-26 16:33:01 -070089 if ((count * eSize) != sizeBytes) {
90 LOGE("Allocation::subData called with mismatched size expected %i, got %i",
91 (count * eSize), sizeBytes);
Jason Samsbad80742011-03-16 16:29:28 -070092 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -070093 return;
94 }
Jason Samse3929c92010-08-09 18:13:33 -070095
Jason Samseb4fe182011-05-26 16:33:01 -070096 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
97 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070098}
99
Jason Sams4b45b892010-12-29 14:31:29 -0800100void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800101 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -0700102 const uint32_t eSize = mHal.state.elementSizeBytes;
103 const uint32_t lineSize = eSize * w;
Jason Sams326e0dd2009-05-22 14:03:28 -0700104
Jason Samsa2371512011-01-12 13:28:37 -0800105 //LOGE("data2d %p, %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -0700106
Jason Samsa2371512011-01-12 13:28:37 -0800107 if ((lineSize * h) != sizeBytes) {
108 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -0700109 rsAssert(!"Allocation::subData called with mismatched size");
110 return;
111 }
112
Jason Samseb4fe182011-05-26 16:33:01 -0700113 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes);
114 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700115}
116
Jason Sams236385b2011-01-12 14:53:25 -0800117void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
118 uint32_t lod, RsAllocationCubemapFace face,
119 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700120}
121
Jason Sams4b45b892010-12-29 14:31:29 -0800122void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800123 uint32_t cIdx, uint32_t sizeBytes) {
Jason Samsbad80742011-03-16 16:29:28 -0700124 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700125
Jason Samsbad80742011-03-16 16:29:28 -0700126 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700127 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
128 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
129 return;
130 }
131
Jason Samsbad80742011-03-16 16:29:28 -0700132 if (x >= mHal.state.dimensionX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700133 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
134 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
135 return;
136 }
137
Jason Samsbad80742011-03-16 16:29:28 -0700138 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700139 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800140 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700141 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
142 return;
143 }
144
Jason Samseb4fe182011-05-26 16:33:01 -0700145 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
146 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700147}
148
Jason Sams4b45b892010-12-29 14:31:29 -0800149void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800150 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Samsbad80742011-03-16 16:29:28 -0700151 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700152
Jason Samsbad80742011-03-16 16:29:28 -0700153 if (x >= mHal.state.dimensionX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700154 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
155 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
156 return;
157 }
158
Jason Samsbad80742011-03-16 16:29:28 -0700159 if (y >= mHal.state.dimensionY) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700160 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
161 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
162 return;
163 }
164
Jason Samsbad80742011-03-16 16:29:28 -0700165 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700166 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
167 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
168 return;
169 }
170
Jason Samsbad80742011-03-16 16:29:28 -0700171 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700172
173 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800174 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700175 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
176 return;
177 }
178
Jason Samseb4fe182011-05-26 16:33:01 -0700179 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
180 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700181}
182
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800183void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700184 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700185}
Jason Sams326e0dd2009-05-22 14:03:28 -0700186
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800187void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700188 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
189 if (mToDirtyList[ct] == p) {
190 mToDirtyList.removeAt(ct);
191 return;
192 }
193 }
194 rsAssert(0);
195}
196
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800197void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800198 ObjectBase::dumpLOGV(prefix);
199
200 String8 s(prefix);
201 s.append(" type ");
Jason Samsbad80742011-03-16 16:29:28 -0700202 if (mHal.state.type.get()) {
203 mHal.state.type->dumpLOGV(s.string());
Jason Samsc21cf402009-11-17 17:26:46 -0800204 }
205
Jason Samseb4fe182011-05-26 16:33:01 -0700206 LOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
207 prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800208}
Jason Sams326e0dd2009-05-22 14:03:28 -0700209
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800210void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700211 // Need to identify ourselves
212 stream->addU32((uint32_t)getClassId());
213
214 String8 name(getName());
215 stream->addString(&name);
216
217 // First thing we need to serialize is the type object since it will be needed
218 // to initialize the class
Jason Samsbad80742011-03-16 16:29:28 -0700219 mHal.state.type->serialize(stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700220
Jason Samsbad80742011-03-16 16:29:28 -0700221 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700222 // Write how much data we are storing
223 stream->addU32(dataSize);
224 // Now write the data
Jason Samseb4fe182011-05-26 16:33:01 -0700225 stream->addByteArray(getPtr(), dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700226}
227
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800228Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700229 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700230 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800231 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700232 LOGE("allocation loading skipped due to invalid class id\n");
233 return NULL;
234 }
235
236 String8 name;
237 stream->loadString(&name);
238
239 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800240 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700241 return NULL;
242 }
243 type->compute();
244
245 // Number of bytes we wrote out for this allocation
246 uint32_t dataSize = stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800247 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700248 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Sams225afd32010-10-21 14:06:55 -0700249 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700250 return NULL;
251 }
252
Jason Samseb4fe182011-05-26 16:33:01 -0700253 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700254 alloc->setName(name.string(), name.size());
255
Jason Sams4b45b892010-12-29 14:31:29 -0800256 uint32_t count = dataSize / type->getElementSizeBytes();
257
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700258 // Read in all of our allocation data
Jason Sams4b45b892010-12-29 14:31:29 -0800259 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700260 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700261
262 return alloc;
263}
264
Jason Samseb4fe182011-05-26 16:33:01 -0700265void Allocation::sendDirty(const Context *rsc) const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700266 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
267 mToDirtyList[ct]->forceDirty();
268 }
Jason Samseb4fe182011-05-26 16:33:01 -0700269 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700270}
Jason Sams326e0dd2009-05-22 14:03:28 -0700271
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800272void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700273 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700274 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700275 uint32_t stride = e->getSizeBytes();
276
Jason Sams96abf812010-10-05 13:32:49 -0700277 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700278 while (ct > 0) {
279 e->incRefs(p);
280 ct --;
281 p += stride;
282 }
283}
284
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800285void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700286 if (!mHal.state.hasReferences || !getIsScript()) {
287 return;
288 }
Jason Samse3929c92010-08-09 18:13:33 -0700289 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700290 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700291 uint32_t stride = e->getSizeBytes();
292
Jason Sams96abf812010-10-05 13:32:49 -0700293 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700294 while (ct > 0) {
295 e->decRefs(p);
296 ct --;
297 p += stride;
298 }
299}
300
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800301void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700302}
303
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800304void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsbad80742011-03-16 16:29:28 -0700305 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams96abf812010-10-05 13:32:49 -0700306 if (dimX == oldDimX) {
307 return;
308 }
309
Jason Samseb4fe182011-05-26 16:33:01 -0700310 Type *t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700311 if (dimX < oldDimX) {
Jason Samseb4fe182011-05-26 16:33:01 -0700312 decRefs(getPtr(), oldDimX - dimX, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700313 }
Jason Samseb4fe182011-05-26 16:33:01 -0700314 rsc->mHal.funcs.allocation.resize(rsc, this, t, mHal.state.hasReferences);
Jason Samsbad80742011-03-16 16:29:28 -0700315 mHal.state.type.set(t);
316 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700317}
318
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800319void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700320 LOGE("not implemented");
321}
322
Jason Sams326e0dd2009-05-22 14:03:28 -0700323/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700324//
Stephen Hines6a121812011-03-01 17:34:59 -0800325
Jason Sams326e0dd2009-05-22 14:03:28 -0700326namespace android {
327namespace renderscript {
328
Jason Samsc975cf42011-04-28 18:26:48 -0700329static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
330
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800331static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700332 uint32_t w = out.getDimX();
333 uint32_t h = out.getDimY();
334
Jason Samse9f5c532009-07-28 17:20:11 -0700335 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700336 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
337 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
338 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
339
Jason Samse9f5c532009-07-28 17:20:11 -0700340 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700341 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
342 oPtr ++;
343 i1 += 2;
344 i2 += 2;
345 }
346 }
347}
348
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800349static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700350 uint32_t w = out.getDimX();
351 uint32_t h = out.getDimY();
352
Jason Samse9f5c532009-07-28 17:20:11 -0700353 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700354 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
355 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
356 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
357
Jason Samse9f5c532009-07-28 17:20:11 -0700358 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700359 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700360 oPtr ++;
361 i1 += 2;
362 i2 += 2;
363 }
364 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700365}
366
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800367static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800368 uint32_t w = out.getDimX();
369 uint32_t h = out.getDimY();
370
371 for (uint32_t y=0; y < h; y++) {
372 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
373 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
374 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
375
376 for (uint32_t x=0; x < w; x++) {
377 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
378 oPtr ++;
379 i1 += 2;
380 i2 += 2;
381 }
382 }
383}
384
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800385static void mip(const Adapter2D &out, const Adapter2D &in) {
386 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700387 case 32:
388 mip8888(out, in);
389 break;
390 case 16:
391 mip565(out, in);
392 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800393 case 8:
394 mip8(out, in);
395 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700396 }
Jason Samse9f5c532009-07-28 17:20:11 -0700397}
Jason Sams326e0dd2009-05-22 14:03:28 -0700398
Jason Sams366c9c82010-12-08 16:14:36 -0800399void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
400 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700401 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800402 a->syncAll(rsc, src);
403}
404
Jason Samsa2371512011-01-12 13:28:37 -0800405void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700406 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsc975cf42011-04-28 18:26:48 -0700407 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800408}
409
410void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
411 Allocation *texAlloc = static_cast<Allocation *>(va);
412 const Type * t = texAlloc->getType();
413
414 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
415 if (s != dataLen) {
416 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
417 return;
418 }
419
420 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700421}
422
Jason Sams4b45b892010-12-29 14:31:29 -0800423void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700424 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700425 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800426 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700427}
428
Jason Sams4b45b892010-12-29 14:31:29 -0800429void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700430 const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped
Jason Sams326e0dd2009-05-22 14:03:28 -0700431 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800432 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700433}
434
Jason Sams4b45b892010-12-29 14:31:29 -0800435void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700436 const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped
Jason Sams5f0c84c2010-08-31 13:50:42 -0700437 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800438 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700439}
440
Jason Sams4b45b892010-12-29 14:31:29 -0800441void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700442 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700443 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800444 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700445}
446
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700447void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
Jason Samse579df42009-08-10 14:55:26 -0700448 Allocation *a = static_cast<Allocation *>(va);
449 a->read(data);
450}
451
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800452void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700453 Allocation *a = static_cast<Allocation *>(va);
454 a->resize1D(rsc, dimX);
455}
456
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800457void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700458 Allocation *a = static_cast<Allocation *>(va);
459 a->resize2D(rsc, dimX, dimY);
460}
461
Jason Samsc975cf42011-04-28 18:26:48 -0700462static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800463 Context *rsc = static_cast<Context *>(con);
464 Allocation *texAlloc = static_cast<Allocation *>(va);
465 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
466 for (uint32_t face = 0; face < numFaces; face ++) {
467 Adapter2D adapt(rsc, texAlloc);
468 Adapter2D adapt2(rsc, texAlloc);
469 adapt.setFace(face);
470 adapt2.setFace(face);
471 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
472 adapt.setLOD(lod);
473 adapt2.setLOD(lod + 1);
474 mip(adapt2, adapt);
475 }
476 }
477}
478
Jason Samsc975cf42011-04-28 18:26:48 -0700479RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
480 RsAllocationMipmapControl mips,
481 uint32_t usages) {
Jason Samseb4fe182011-05-26 16:33:01 -0700482 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips);
483 if (!alloc) {
484 return NULL;
485 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700486 alloc->incUserRef();
487 return alloc;
488}
489
Jason Samsc975cf42011-04-28 18:26:48 -0700490RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
491 RsAllocationMipmapControl mips,
492 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800493 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700494
Jason Samsc975cf42011-04-28 18:26:48 -0700495 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages);
Jason Samsf0c1df42010-10-26 13:09:17 -0700496 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
497 if (texAlloc == NULL) {
498 LOGE("Memory allocation failure");
499 return NULL;
500 }
501
Jason Sams366c9c82010-12-08 16:14:36 -0800502 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800503 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700504 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700505 }
506
Jason Samseb4fe182011-05-26 16:33:01 -0700507 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700508 return texAlloc;
509}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800510
Jason Samsc975cf42011-04-28 18:26:48 -0700511RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
512 RsAllocationMipmapControl mips,
513 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800514 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800515
516 // Cubemap allocation's faces should be Width by Width each.
517 // Source data should have 6 * Width by Width pixels
518 // Error checking is done in the java layer
Jason Samsc975cf42011-04-28 18:26:48 -0700519 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800520 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
521 if (texAlloc == NULL) {
522 LOGE("Memory allocation failure");
523 return NULL;
524 }
525
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800526 uint32_t faceSize = t->getDimX();
527 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
528 uint32_t copySize = faceSize * t->getElementSizeBytes();
529
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800530 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800531 for (uint32_t face = 0; face < 6; face ++) {
532 Adapter2D faceAdapter(rsc, texAlloc);
533 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800534
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800535 for (uint32_t dI = 0; dI < faceSize; dI ++) {
536 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
537 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800538
Jason Sams366c9c82010-12-08 16:14:36 -0800539 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800540 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800541 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800542
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800543 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700544 AllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800545 }
546
Jason Samseb4fe182011-05-26 16:33:01 -0700547 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800548 return texAlloc;
549}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800550
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700551void rsi_AllocationCopy2DRange(Context *rsc,
552 RsAllocation dstAlloc,
553 uint32_t dstXoff, uint32_t dstYoff,
554 uint32_t dstMip, uint32_t dstFace,
555 uint32_t width, uint32_t height,
556 RsAllocation srcAlloc,
557 uint32_t srcXoff, uint32_t srcYoff,
558 uint32_t srcMip, uint32_t srcFace) {
559 Allocation *dst = static_cast<Allocation *>(dstAlloc);
560 Allocation *src= static_cast<Allocation *>(srcAlloc);
561 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
562 (RsAllocationCubemapFace)dstFace,
563 width, height,
564 src, srcXoff, srcYoff,srcMip,
565 (RsAllocationCubemapFace)srcFace);
566}
567
Jason Samsc975cf42011-04-28 18:26:48 -0700568}
569}
570
571const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
572 Allocation *a = static_cast<Allocation *>(va);
573 a->getType()->incUserRef();
574
575 return a->getType();
576}