blob: f3e0c0a75291ab78f00a85f6569a4a94faccc23c [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() {
Jason Samseb4fe182011-05-26 16:33:01 -070061 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070062}
63
Jason Sams366c9c82010-12-08 16:14:36 -080064void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Samseb4fe182011-05-26 16:33:01 -070065 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Samscf4c7c92009-12-14 12:57:40 -080066}
67
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080068void Allocation::read(void *data) {
Jason Samseb4fe182011-05-26 16:33:01 -070069 memcpy(data, getPtr(), mHal.state.type->getSizeBytes());
Jason Samse579df42009-08-10 14:55:26 -070070}
71
Jason Sams4b45b892010-12-29 14:31:29 -080072void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
73 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -070074 const uint32_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams9397e302009-08-27 20:23:34 -070075
Jason Samseb4fe182011-05-26 16:33:01 -070076 if ((count * eSize) != sizeBytes) {
77 LOGE("Allocation::subData called with mismatched size expected %i, got %i",
78 (count * eSize), sizeBytes);
Jason Samsbad80742011-03-16 16:29:28 -070079 mHal.state.type->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -070080 return;
81 }
Jason Samse3929c92010-08-09 18:13:33 -070082
Jason Samseb4fe182011-05-26 16:33:01 -070083 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
84 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070085}
86
Jason Sams4b45b892010-12-29 14:31:29 -080087void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080088 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -070089 const uint32_t eSize = mHal.state.elementSizeBytes;
90 const uint32_t lineSize = eSize * w;
Jason Sams326e0dd2009-05-22 14:03:28 -070091
Jason Samsa2371512011-01-12 13:28:37 -080092 //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 -070093
Jason Samsa2371512011-01-12 13:28:37 -080094 if ((lineSize * h) != sizeBytes) {
95 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -070096 rsAssert(!"Allocation::subData called with mismatched size");
97 return;
98 }
99
Jason Samseb4fe182011-05-26 16:33:01 -0700100 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes);
101 sendDirty(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700102}
103
Jason Sams236385b2011-01-12 14:53:25 -0800104void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
105 uint32_t lod, RsAllocationCubemapFace face,
106 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700107}
108
Jason Sams4b45b892010-12-29 14:31:29 -0800109void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800110 uint32_t cIdx, uint32_t sizeBytes) {
Jason Samsbad80742011-03-16 16:29:28 -0700111 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700112
Jason Samsbad80742011-03-16 16:29:28 -0700113 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700114 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
115 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
116 return;
117 }
118
Jason Samsbad80742011-03-16 16:29:28 -0700119 if (x >= mHal.state.dimensionX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700120 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
121 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
122 return;
123 }
124
Jason Samsbad80742011-03-16 16:29:28 -0700125 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700126 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800127 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700128 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
129 return;
130 }
131
Jason Samseb4fe182011-05-26 16:33:01 -0700132 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
133 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700134}
135
Jason Sams4b45b892010-12-29 14:31:29 -0800136void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800137 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Samsbad80742011-03-16 16:29:28 -0700138 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams5f0c84c2010-08-31 13:50:42 -0700139
Jason Samsbad80742011-03-16 16:29:28 -0700140 if (x >= mHal.state.dimensionX) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700141 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
142 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
143 return;
144 }
145
Jason Samsbad80742011-03-16 16:29:28 -0700146 if (y >= mHal.state.dimensionY) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700147 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
148 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
149 return;
150 }
151
Jason Samsbad80742011-03-16 16:29:28 -0700152 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700153 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
154 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
155 return;
156 }
157
Jason Samsbad80742011-03-16 16:29:28 -0700158 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700159
160 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800161 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700162 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
163 return;
164 }
165
Jason Samseb4fe182011-05-26 16:33:01 -0700166 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
167 sendDirty(rsc);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700168}
169
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800170void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700171 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700172}
Jason Sams326e0dd2009-05-22 14:03:28 -0700173
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800174void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700175 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
176 if (mToDirtyList[ct] == p) {
177 mToDirtyList.removeAt(ct);
178 return;
179 }
180 }
181 rsAssert(0);
182}
183
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800184void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800185 ObjectBase::dumpLOGV(prefix);
186
187 String8 s(prefix);
188 s.append(" type ");
Jason Samsbad80742011-03-16 16:29:28 -0700189 if (mHal.state.type.get()) {
190 mHal.state.type->dumpLOGV(s.string());
Jason Samsc21cf402009-11-17 17:26:46 -0800191 }
192
Jason Samseb4fe182011-05-26 16:33:01 -0700193 LOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
194 prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Samsc21cf402009-11-17 17:26:46 -0800195}
Jason Sams326e0dd2009-05-22 14:03:28 -0700196
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800197void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700198 // Need to identify ourselves
199 stream->addU32((uint32_t)getClassId());
200
201 String8 name(getName());
202 stream->addString(&name);
203
204 // First thing we need to serialize is the type object since it will be needed
205 // to initialize the class
Jason Samsbad80742011-03-16 16:29:28 -0700206 mHal.state.type->serialize(stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700207
Jason Samsbad80742011-03-16 16:29:28 -0700208 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700209 // Write how much data we are storing
210 stream->addU32(dataSize);
211 // Now write the data
Jason Samseb4fe182011-05-26 16:33:01 -0700212 stream->addByteArray(getPtr(), dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700213}
214
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800215Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700216 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700217 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800218 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700219 LOGE("allocation loading skipped due to invalid class id\n");
220 return NULL;
221 }
222
223 String8 name;
224 stream->loadString(&name);
225
226 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800227 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700228 return NULL;
229 }
230 type->compute();
231
232 // Number of bytes we wrote out for this allocation
233 uint32_t dataSize = stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800234 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700235 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Sams225afd32010-10-21 14:06:55 -0700236 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700237 return NULL;
238 }
239
Jason Samseb4fe182011-05-26 16:33:01 -0700240 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700241 alloc->setName(name.string(), name.size());
242
Jason Sams4b45b892010-12-29 14:31:29 -0800243 uint32_t count = dataSize / type->getElementSizeBytes();
244
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700245 // Read in all of our allocation data
Jason Sams4b45b892010-12-29 14:31:29 -0800246 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700247 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700248
249 return alloc;
250}
251
Jason Samseb4fe182011-05-26 16:33:01 -0700252void Allocation::sendDirty(const Context *rsc) const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700253 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
254 mToDirtyList[ct]->forceDirty();
255 }
Jason Samseb4fe182011-05-26 16:33:01 -0700256 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700257}
Jason Sams326e0dd2009-05-22 14:03:28 -0700258
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800259void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700260 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samsbad80742011-03-16 16:29:28 -0700261 const Element *e = mHal.state.type->getElement();
Jason Samse3929c92010-08-09 18:13:33 -0700262 uint32_t stride = e->getSizeBytes();
263
Jason Sams96abf812010-10-05 13:32:49 -0700264 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700265 while (ct > 0) {
266 e->incRefs(p);
267 ct --;
268 p += stride;
269 }
270}
271
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800272void Allocation::decRefs(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->decRefs(p);
280 ct --;
281 p += stride;
282 }
283}
284
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800285void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700286}
287
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800288void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samsbad80742011-03-16 16:29:28 -0700289 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams96abf812010-10-05 13:32:49 -0700290 if (dimX == oldDimX) {
291 return;
292 }
293
Jason Samseb4fe182011-05-26 16:33:01 -0700294 Type *t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700295 if (dimX < oldDimX) {
Jason Samseb4fe182011-05-26 16:33:01 -0700296 decRefs(getPtr(), oldDimX - dimX, dimX);
Jason Sams96abf812010-10-05 13:32:49 -0700297 }
Jason Samseb4fe182011-05-26 16:33:01 -0700298 rsc->mHal.funcs.allocation.resize(rsc, this, t, mHal.state.hasReferences);
Jason Samsbad80742011-03-16 16:29:28 -0700299 mHal.state.type.set(t);
300 updateCache();
Jason Sams96abf812010-10-05 13:32:49 -0700301}
302
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800303void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700304 LOGE("not implemented");
305}
306
Jason Sams326e0dd2009-05-22 14:03:28 -0700307/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700308//
Stephen Hines6a121812011-03-01 17:34:59 -0800309
Jason Sams326e0dd2009-05-22 14:03:28 -0700310namespace android {
311namespace renderscript {
312
Jason Samsc975cf42011-04-28 18:26:48 -0700313static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
314
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800315static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700316 uint32_t w = out.getDimX();
317 uint32_t h = out.getDimY();
318
Jason Samse9f5c532009-07-28 17:20:11 -0700319 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700320 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
321 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
322 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
323
Jason Samse9f5c532009-07-28 17:20:11 -0700324 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700325 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
326 oPtr ++;
327 i1 += 2;
328 i2 += 2;
329 }
330 }
331}
332
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800333static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700334 uint32_t w = out.getDimX();
335 uint32_t h = out.getDimY();
336
Jason Samse9f5c532009-07-28 17:20:11 -0700337 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700338 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
339 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
340 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
341
Jason Samse9f5c532009-07-28 17:20:11 -0700342 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700343 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700344 oPtr ++;
345 i1 += 2;
346 i2 += 2;
347 }
348 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700349}
350
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800351static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800352 uint32_t w = out.getDimX();
353 uint32_t h = out.getDimY();
354
355 for (uint32_t y=0; y < h; y++) {
356 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
357 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
358 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
359
360 for (uint32_t x=0; x < w; x++) {
361 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
362 oPtr ++;
363 i1 += 2;
364 i2 += 2;
365 }
366 }
367}
368
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800369static void mip(const Adapter2D &out, const Adapter2D &in) {
370 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700371 case 32:
372 mip8888(out, in);
373 break;
374 case 16:
375 mip565(out, in);
376 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800377 case 8:
378 mip8(out, in);
379 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700380 }
Jason Samse9f5c532009-07-28 17:20:11 -0700381}
Jason Sams326e0dd2009-05-22 14:03:28 -0700382
Jason Sams366c9c82010-12-08 16:14:36 -0800383void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
384 Allocation *a = static_cast<Allocation *>(va);
Jason Samseb4fe182011-05-26 16:33:01 -0700385 a->sendDirty(rsc);
Jason Sams366c9c82010-12-08 16:14:36 -0800386 a->syncAll(rsc, src);
387}
388
Jason Samsa2371512011-01-12 13:28:37 -0800389void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700390 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsc975cf42011-04-28 18:26:48 -0700391 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800392}
393
394void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
395 Allocation *texAlloc = static_cast<Allocation *>(va);
396 const Type * t = texAlloc->getType();
397
398 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
399 if (s != dataLen) {
400 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
401 return;
402 }
403
404 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700405}
406
Jason Sams4b45b892010-12-29 14:31:29 -0800407void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700408 uint32_t count, const void *data, size_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700409 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800410 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700411}
412
Jason Sams4b45b892010-12-29 14:31:29 -0800413void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700414 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 -0700415 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800416 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700417}
418
Jason Sams4b45b892010-12-29 14:31:29 -0800419void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700420 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 -0700421 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800422 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700423}
424
Jason Sams4b45b892010-12-29 14:31:29 -0800425void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700426 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700427 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800428 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700429}
430
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700431void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
Jason Samse579df42009-08-10 14:55:26 -0700432 Allocation *a = static_cast<Allocation *>(va);
433 a->read(data);
434}
435
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800436void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700437 Allocation *a = static_cast<Allocation *>(va);
438 a->resize1D(rsc, dimX);
439}
440
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800441void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700442 Allocation *a = static_cast<Allocation *>(va);
443 a->resize2D(rsc, dimX, dimY);
444}
445
Jason Samsc975cf42011-04-28 18:26:48 -0700446static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800447 Context *rsc = static_cast<Context *>(con);
448 Allocation *texAlloc = static_cast<Allocation *>(va);
449 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
450 for (uint32_t face = 0; face < numFaces; face ++) {
451 Adapter2D adapt(rsc, texAlloc);
452 Adapter2D adapt2(rsc, texAlloc);
453 adapt.setFace(face);
454 adapt2.setFace(face);
455 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
456 adapt.setLOD(lod);
457 adapt2.setLOD(lod + 1);
458 mip(adapt2, adapt);
459 }
460 }
461}
462
Jason Samsc975cf42011-04-28 18:26:48 -0700463RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
464 RsAllocationMipmapControl mips,
465 uint32_t usages) {
Jason Samseb4fe182011-05-26 16:33:01 -0700466 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips);
467 if (!alloc) {
468 return NULL;
469 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700470 alloc->incUserRef();
471 return alloc;
472}
473
Jason Samsc975cf42011-04-28 18:26:48 -0700474RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
475 RsAllocationMipmapControl mips,
476 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800477 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700478
Jason Samsc975cf42011-04-28 18:26:48 -0700479 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages);
Jason Samsf0c1df42010-10-26 13:09:17 -0700480 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
481 if (texAlloc == NULL) {
482 LOGE("Memory allocation failure");
483 return NULL;
484 }
485
Jason Sams366c9c82010-12-08 16:14:36 -0800486 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800487 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700488 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700489 }
490
Jason Samseb4fe182011-05-26 16:33:01 -0700491 texAlloc->sendDirty(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700492 return texAlloc;
493}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800494
Jason Samsc975cf42011-04-28 18:26:48 -0700495RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
496 RsAllocationMipmapControl mips,
497 const void *data, size_t data_length, uint32_t usages) {
Jason Sams366c9c82010-12-08 16:14:36 -0800498 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800499
500 // Cubemap allocation's faces should be Width by Width each.
501 // Source data should have 6 * Width by Width pixels
502 // Error checking is done in the java layer
Jason Samsc975cf42011-04-28 18:26:48 -0700503 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800504 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
505 if (texAlloc == NULL) {
506 LOGE("Memory allocation failure");
507 return NULL;
508 }
509
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800510 uint32_t faceSize = t->getDimX();
511 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
512 uint32_t copySize = faceSize * t->getElementSizeBytes();
513
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800514 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800515 for (uint32_t face = 0; face < 6; face ++) {
516 Adapter2D faceAdapter(rsc, texAlloc);
517 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800518
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800519 for (uint32_t dI = 0; dI < faceSize; dI ++) {
520 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
521 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800522
Jason Sams366c9c82010-12-08 16:14:36 -0800523 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800524 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800525 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800526
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800527 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc975cf42011-04-28 18:26:48 -0700528 AllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800529 }
530
Jason Samseb4fe182011-05-26 16:33:01 -0700531 texAlloc->sendDirty(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800532 return texAlloc;
533}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800534
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700535void rsi_AllocationCopy2DRange(Context *rsc,
536 RsAllocation dstAlloc,
537 uint32_t dstXoff, uint32_t dstYoff,
538 uint32_t dstMip, uint32_t dstFace,
539 uint32_t width, uint32_t height,
540 RsAllocation srcAlloc,
541 uint32_t srcXoff, uint32_t srcYoff,
542 uint32_t srcMip, uint32_t srcFace) {
543 Allocation *dst = static_cast<Allocation *>(dstAlloc);
544 Allocation *src= static_cast<Allocation *>(srcAlloc);
545 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
546 (RsAllocationCubemapFace)dstFace,
547 width, height,
548 src, srcXoff, srcYoff,srcMip,
549 (RsAllocationCubemapFace)srcFace);
550}
551
Jason Samsc975cf42011-04-28 18:26:48 -0700552}
553}
554
555const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
556 Allocation *a = static_cast<Allocation *>(va);
557 a->getType()->incUserRef();
558
559 return a->getType();
560}