blob: a366d4985a48fca522578f2a6718dc524b5b8b7a [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());
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700255 type->decUserRef();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700256
Jason Sams4b45b892010-12-29 14:31:29 -0800257 uint32_t count = dataSize / type->getElementSizeBytes();
258
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700259 // Read in all of our allocation data
Jason Sams4b45b892010-12-29 14:31:29 -0800260 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700261 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700262
263 return alloc;
264}
265
Jason Samseb4fe182011-05-26 16:33:01 -0700266void Allocation::sendDirty(const Context *rsc) const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700267 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
268 mToDirtyList[ct]->forceDirty();
269 }
Jason Samseb4fe182011-05-26 16:33:01 -0700270 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700271}
Jason Sams326e0dd2009-05-22 14:03:28 -0700272
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800273void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700274 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700275 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700276 uint32_t stride = e->getSizeBytes();
277
Jason Sams96abf812010-10-05 13:32:49 -0700278 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700279 while (ct > 0) {
280 e->incRefs(p);
281 ct --;
282 p += stride;
283 }
284}
285
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800286void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Alex Sakhartchouk5c4369a2011-08-12 11:30:30 -0700287 if (!mHal.state.hasReferences || !getIsScript()) {
288 return;
289 }
Jason Samse3929c92010-08-09 18:13:33 -0700290 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700291 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700292 uint32_t stride = e->getSizeBytes();
293
Jason Sams96abf812010-10-05 13:32:49 -0700294 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700295 while (ct > 0) {
296 e->decRefs(p);
297 ct --;
298 p += stride;
299 }
300}
301
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800302void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700303}
304
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800305void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsbad80742011-03-16 16:29:28 -0700306 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams96abf812010-10-05 13:32:49 -0700307 if (dimX == oldDimX) {
308 return;
309 }
310
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700311 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700312 if (dimX < oldDimX) {
Jason Samseb4fe182011-05-26 16:33:01 -0700313 decRefs(getPtr(), oldDimX - dimX, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700314 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700315 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
316 mHal.state.type.set(t.get());
Jason Samsbad80742011-03-16 16:29:28 -0700317 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700318}
319
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800320void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700321 LOGE("not implemented");
322}
323
Jason Sams326e0dd2009-05-22 14:03:28 -0700324/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700325//
Stephen Hines6a121812011-03-01 17:34:59 -0800326
Jason Sams326e0dd2009-05-22 14:03:28 -0700327namespace android {
328namespace renderscript {
329
Jason Samsc975cf42011-04-28 18:26:48 -0700330static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
331
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800332static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700333 uint32_t w = out.getDimX();
334 uint32_t h = out.getDimY();
335
Jason Samse9f5c532009-07-28 17:20:11 -0700336 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700337 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
338 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
339 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
340
Jason Samse9f5c532009-07-28 17:20:11 -0700341 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700342 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
343 oPtr ++;
344 i1 += 2;
345 i2 += 2;
346 }
347 }
348}
349
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800350static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700351 uint32_t w = out.getDimX();
352 uint32_t h = out.getDimY();
353
Jason Samse9f5c532009-07-28 17:20:11 -0700354 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700355 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
356 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
357 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
358
Jason Samse9f5c532009-07-28 17:20:11 -0700359 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700360 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700361 oPtr ++;
362 i1 += 2;
363 i2 += 2;
364 }
365 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700366}
367
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800368static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800369 uint32_t w = out.getDimX();
370 uint32_t h = out.getDimY();
371
372 for (uint32_t y=0; y < h; y++) {
373 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
374 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
375 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
376
377 for (uint32_t x=0; x < w; x++) {
378 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
379 oPtr ++;
380 i1 += 2;
381 i2 += 2;
382 }
383 }
384}
385
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800386static void mip(const Adapter2D &out, const Adapter2D &in) {
387 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700388 case 32:
389 mip8888(out, in);
390 break;
391 case 16:
392 mip565(out, in);
393 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800394 case 8:
395 mip8(out, in);
396 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700397 }
Jason Samse9f5c532009-07-28 17:20:11 -0700398}
Jason Sams326e0dd2009-05-22 14:03:28 -0700399
Jason Sams366c9c82010-12-08 16:14:36 -0800400void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
401 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700402 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800403 a->syncAll(rsc, src);
404}
405
Jason Samsa2371512011-01-12 13:28:37 -0800406void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700407 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsc975cf42011-04-28 18:26:48 -0700408 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800409}
410
411void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
412 Allocation *texAlloc = static_cast<Allocation *>(va);
413 const Type * t = texAlloc->getType();
414
415 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
416 if (s != dataLen) {
417 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
418 return;
419 }
420
421 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700422}
423
Jason Sams4b45b892010-12-29 14:31:29 -0800424void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700425 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700426 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800427 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700428}
429
Jason Sams4b45b892010-12-29 14:31:29 -0800430void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700431 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 -0700432 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800433 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700434}
435
Jason Sams4b45b892010-12-29 14:31:29 -0800436void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700437 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 -0700438 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800439 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700440}
441
Jason Sams4b45b892010-12-29 14:31:29 -0800442void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700443 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700444 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800445 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700446}
447
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700448void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
Jason Samse579df42009-08-10 14:55:26 -0700449 Allocation *a = static_cast<Allocation *>(va);
450 a->read(data);
451}
452
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800453void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700454 Allocation *a = static_cast<Allocation *>(va);
455 a->resize1D(rsc, dimX);
456}
457
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800458void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700459 Allocation *a = static_cast<Allocation *>(va);
460 a->resize2D(rsc, dimX, dimY);
461}
462
Jason Samsc975cf42011-04-28 18:26:48 -0700463static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800464 Context *rsc = static_cast<Context *>(con);
465 Allocation *texAlloc = static_cast<Allocation *>(va);
466 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
467 for (uint32_t face = 0; face < numFaces; face ++) {
468 Adapter2D adapt(rsc, texAlloc);
469 Adapter2D adapt2(rsc, texAlloc);
470 adapt.setFace(face);
471 adapt2.setFace(face);
472 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
473 adapt.setLOD(lod);
474 adapt2.setLOD(lod + 1);
475 mip(adapt2, adapt);
476 }
477 }
478}
479
Jason Samsc975cf42011-04-28 18:26:48 -0700480RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
481 RsAllocationMipmapControl mips,
482 uint32_t usages) {
Jason Samseb4fe182011-05-26 16:33:01 -0700483 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips);
484 if (!alloc) {
485 return NULL;
486 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700487 alloc->incUserRef();
488 return alloc;
489}
490
Jason Samsc975cf42011-04-28 18:26:48 -0700491RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
492 RsAllocationMipmapControl mips,
493 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800494 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700495
Jason Samsc975cf42011-04-28 18:26:48 -0700496 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages);
Jason Samsf0c1df42010-10-26 13:09:17 -0700497 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
498 if (texAlloc == NULL) {
499 LOGE("Memory allocation failure");
500 return NULL;
501 }
502
Jason Sams366c9c82010-12-08 16:14:36 -0800503 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800504 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700505 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700506 }
507
Jason Samseb4fe182011-05-26 16:33:01 -0700508 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700509 return texAlloc;
510}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800511
Jason Samsc975cf42011-04-28 18:26:48 -0700512RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
513 RsAllocationMipmapControl mips,
514 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800515 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800516
517 // Cubemap allocation's faces should be Width by Width each.
518 // Source data should have 6 * Width by Width pixels
519 // Error checking is done in the java layer
Jason Samsc975cf42011-04-28 18:26:48 -0700520 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800521 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
522 if (texAlloc == NULL) {
523 LOGE("Memory allocation failure");
524 return NULL;
525 }
526
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800527 uint32_t faceSize = t->getDimX();
528 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
529 uint32_t copySize = faceSize * t->getElementSizeBytes();
530
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800531 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800532 for (uint32_t face = 0; face < 6; face ++) {
533 Adapter2D faceAdapter(rsc, texAlloc);
534 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800535
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800536 for (uint32_t dI = 0; dI < faceSize; dI ++) {
537 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
538 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800539
Jason Sams366c9c82010-12-08 16:14:36 -0800540 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800541 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800542 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800543
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800544 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700545 AllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800546 }
547
Jason Samseb4fe182011-05-26 16:33:01 -0700548 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800549 return texAlloc;
550}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800551
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700552void rsi_AllocationCopy2DRange(Context *rsc,
553 RsAllocation dstAlloc,
554 uint32_t dstXoff, uint32_t dstYoff,
555 uint32_t dstMip, uint32_t dstFace,
556 uint32_t width, uint32_t height,
557 RsAllocation srcAlloc,
558 uint32_t srcXoff, uint32_t srcYoff,
559 uint32_t srcMip, uint32_t srcFace) {
560 Allocation *dst = static_cast<Allocation *>(dstAlloc);
561 Allocation *src= static_cast<Allocation *>(srcAlloc);
562 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
563 (RsAllocationCubemapFace)dstFace,
564 width, height,
565 src, srcXoff, srcYoff,srcMip,
566 (RsAllocationCubemapFace)srcFace);
567}
568
Jason Samsc975cf42011-04-28 18:26:48 -0700569}
570}
571
572const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
573 Allocation *a = static_cast<Allocation *>(va);
574 a->getType()->incUserRef();
575
576 return a->getType();
577}