blob: 05412c7b0b9254c0e6c37e0cf6dc9a1f14146421 [file] [log] [blame]
Jason Samsd19f10d2009-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 Samsd19f10d2009-05-22 14:03:28 -070016
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080017#include "rsContext.h"
Jason Sams7e8aae72011-05-26 16:33:01 -070018#include "rs_hal.h"
19
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -070020
Jason Samsd19f10d2009-05-22 14:03:28 -070021using namespace android;
22using namespace android::renderscript;
23
Alex Sakhartchouk08571962010-12-15 09:59:58 -080024Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
25 RsAllocationMipmapControl mc)
26 : ObjectBase(rsc) {
Jason Sams8a647432010-03-01 15:31:04 -080027
Jason Sams7e8aae72011-05-26 16:33:01 -070028 memset(&mHal, 0, sizeof(mHal));
29 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samse4a06c52011-03-16 16:29:28 -070030 mHal.state.usageFlags = usages;
31 mHal.state.mipmapControl = mc;
Jason Sams5476b452010-12-08 16:14:36 -080032
Jason Samse4a06c52011-03-16 16:29:28 -070033 mHal.state.type.set(type);
34 updateCache();
35}
Jason Sams8a647432010-03-01 15:31:04 -080036
Jason Sams7e8aae72011-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 Samse4a06c52011-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 Samsd19f10d2009-05-22 14:03:28 -070058}
59
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080060Allocation::~Allocation() {
Jason Sams7e8aae72011-05-26 16:33:01 -070061 mRSC->mHal.funcs.allocation.destroy(mRSC, this);
Jason Samsd19f10d2009-05-22 14:03:28 -070062}
63
Jason Sams5476b452010-12-08 16:14:36 -080064void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
Jason Sams7e8aae72011-05-26 16:33:01 -070065 rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
Jason Sams3b7d39b2009-12-14 12:57:40 -080066}
67
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080068void Allocation::read(void *data) {
Jason Sams7e8aae72011-05-26 16:33:01 -070069 memcpy(data, getPtr(), mHal.state.type->getSizeBytes());
Jason Sams40a29e82009-08-10 14:55:26 -070070}
71
Jason Sams49a05d72010-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 Sams7e8aae72011-05-26 16:33:01 -070074 const uint32_t eSize = mHal.state.type->getElementSizeBytes();
Jason Sams07ae4062009-08-27 20:23:34 -070075
Jason Sams7e8aae72011-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 Samse4a06c52011-03-16 16:29:28 -070079 mHal.state.type->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -070080 return;
81 }
Jason Samsb28ca96f2010-08-09 18:13:33 -070082
Jason Sams7e8aae72011-05-26 16:33:01 -070083 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
84 sendDirty(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -070085}
86
Jason Sams49a05d72010-12-29 14:31:29 -080087void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080088 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams7e8aae72011-05-26 16:33:01 -070089 const uint32_t eSize = mHal.state.elementSizeBytes;
90 const uint32_t lineSize = eSize * w;
Jason Samsd19f10d2009-05-22 14:03:28 -070091
Jason Samsf7086092011-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 Sams07ae4062009-08-27 20:23:34 -070093
Jason Samsf7086092011-01-12 13:28:37 -080094 if ((lineSize * h) != sizeBytes) {
95 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams07ae4062009-08-27 20:23:34 -070096 rsAssert(!"Allocation::subData called with mismatched size");
97 return;
98 }
99
Jason Sams7e8aae72011-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 Samsd19f10d2009-05-22 14:03:28 -0700102}
103
Jason Samsfb9f82c2011-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 Samsd19f10d2009-05-22 14:03:28 -0700107}
108
Jason Sams49a05d72010-12-29 14:31:29 -0800109void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800110 uint32_t cIdx, uint32_t sizeBytes) {
Jason Samse4a06c52011-03-16 16:29:28 -0700111 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams49bdaf02010-08-31 13:50:42 -0700112
Jason Samse4a06c52011-03-16 16:29:28 -0700113 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams49bdaf02010-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 Samse4a06c52011-03-16 16:29:28 -0700119 if (x >= mHal.state.dimensionX) {
Jason Sams49bdaf02010-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 Samse4a06c52011-03-16 16:29:28 -0700125 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -0700126 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800127 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams49bdaf02010-08-31 13:50:42 -0700128 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
129 return;
130 }
131
Jason Sams7e8aae72011-05-26 16:33:01 -0700132 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
133 sendDirty(rsc);
Jason Sams49bdaf02010-08-31 13:50:42 -0700134}
135
Jason Sams49a05d72010-12-29 14:31:29 -0800136void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800137 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Samse4a06c52011-03-16 16:29:28 -0700138 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Sams49bdaf02010-08-31 13:50:42 -0700139
Jason Samse4a06c52011-03-16 16:29:28 -0700140 if (x >= mHal.state.dimensionX) {
Jason Sams49bdaf02010-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 Samse4a06c52011-03-16 16:29:28 -0700146 if (y >= mHal.state.dimensionY) {
Jason Sams49bdaf02010-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 Samse4a06c52011-03-16 16:29:28 -0700152 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams49bdaf02010-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 Samse4a06c52011-03-16 16:29:28 -0700158 const Element * e = mHal.state.type->getElement()->getField(cIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -0700159
160 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800161 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams49bdaf02010-08-31 13:50:42 -0700162 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
163 return;
164 }
165
Jason Sams7e8aae72011-05-26 16:33:01 -0700166 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
167 sendDirty(rsc);
Jason Sams49bdaf02010-08-31 13:50:42 -0700168}
169
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800170void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800171#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700172 mToDirtyList.push(p);
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800173#endif //ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700174}
Jason Samsd19f10d2009-05-22 14:03:28 -0700175
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800176void Allocation::removeProgramToDirty(const Program *p) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800177#ifndef ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700178 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
179 if (mToDirtyList[ct] == p) {
180 mToDirtyList.removeAt(ct);
181 return;
182 }
183 }
184 rsAssert(0);
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800185#endif //ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700186}
187
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800188void Allocation::dumpLOGV(const char *prefix) const {
Jason Sams715333b2009-11-17 17:26:46 -0800189 ObjectBase::dumpLOGV(prefix);
190
191 String8 s(prefix);
192 s.append(" type ");
Jason Samse4a06c52011-03-16 16:29:28 -0700193 if (mHal.state.type.get()) {
194 mHal.state.type->dumpLOGV(s.string());
Jason Sams715333b2009-11-17 17:26:46 -0800195 }
196
Jason Sams7e8aae72011-05-26 16:33:01 -0700197 LOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x",
198 prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
Jason Sams715333b2009-11-17 17:26:46 -0800199}
Jason Samsd19f10d2009-05-22 14:03:28 -0700200
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800201void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700202 // Need to identify ourselves
203 stream->addU32((uint32_t)getClassId());
204
205 String8 name(getName());
206 stream->addString(&name);
207
208 // First thing we need to serialize is the type object since it will be needed
209 // to initialize the class
Jason Samse4a06c52011-03-16 16:29:28 -0700210 mHal.state.type->serialize(stream);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700211
Jason Samse4a06c52011-03-16 16:29:28 -0700212 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700213 // Write how much data we are storing
214 stream->addU32(dataSize);
215 // Now write the data
Jason Sams7e8aae72011-05-26 16:33:01 -0700216 stream->addByteArray(getPtr(), dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700217}
218
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800219Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700220 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700221 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800222 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700223 LOGE("allocation loading skipped due to invalid class id\n");
224 return NULL;
225 }
226
227 String8 name;
228 stream->loadString(&name);
229
230 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800231 if (!type) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700232 return NULL;
233 }
234 type->compute();
235
236 // Number of bytes we wrote out for this allocation
237 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800238 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700239 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Samsb38d5342010-10-21 14:06:55 -0700240 ObjectBase::checkDelete(type);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700241 return NULL;
242 }
243
Jason Sams7e8aae72011-05-26 16:33:01 -0700244 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700245 alloc->setName(name.string(), name.size());
246
Jason Sams49a05d72010-12-29 14:31:29 -0800247 uint32_t count = dataSize / type->getElementSizeBytes();
248
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700249 // Read in all of our allocation data
Jason Sams49a05d72010-12-29 14:31:29 -0800250 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700251 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700252
253 return alloc;
254}
255
Jason Sams7e8aae72011-05-26 16:33:01 -0700256void Allocation::sendDirty(const Context *rsc) const {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800257#ifndef ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700258 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
259 mToDirtyList[ct]->forceDirty();
260 }
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800261#endif //ANDROID_RS_SERIALIZE
Jason Sams7e8aae72011-05-26 16:33:01 -0700262 mRSC->mHal.funcs.allocation.markDirty(rsc, this);
Jason Sams83f1c632009-10-26 15:19:28 -0700263}
Jason Samsd19f10d2009-05-22 14:03:28 -0700264
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800265void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700266 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samse4a06c52011-03-16 16:29:28 -0700267 const Element *e = mHal.state.type->getElement();
Jason Samsb28ca96f2010-08-09 18:13:33 -0700268 uint32_t stride = e->getSizeBytes();
269
Jason Sams5edc6082010-10-05 13:32:49 -0700270 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700271 while (ct > 0) {
272 e->incRefs(p);
273 ct --;
274 p += stride;
275 }
276}
277
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800278void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700279 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samse4a06c52011-03-16 16:29:28 -0700280 const Element *e = mHal.state.type->getElement();
Jason Samsb28ca96f2010-08-09 18:13:33 -0700281 uint32_t stride = e->getSizeBytes();
282
Jason Sams5edc6082010-10-05 13:32:49 -0700283 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700284 while (ct > 0) {
285 e->decRefs(p);
286 ct --;
287 p += stride;
288 }
289}
290
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800291void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams5edc6082010-10-05 13:32:49 -0700292}
293
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800294void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samse4a06c52011-03-16 16:29:28 -0700295 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams5edc6082010-10-05 13:32:49 -0700296 if (dimX == oldDimX) {
297 return;
298 }
299
Jason Sams7e8aae72011-05-26 16:33:01 -0700300 Type *t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -0700301 if (dimX < oldDimX) {
Jason Sams7e8aae72011-05-26 16:33:01 -0700302 decRefs(getPtr(), oldDimX - dimX, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -0700303 }
Jason Sams7e8aae72011-05-26 16:33:01 -0700304 rsc->mHal.funcs.allocation.resize(rsc, this, t, mHal.state.hasReferences);
Jason Samse4a06c52011-03-16 16:29:28 -0700305 mHal.state.type.set(t);
306 updateCache();
Jason Sams5edc6082010-10-05 13:32:49 -0700307}
308
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800309void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700310 LOGE("not implemented");
311}
312
Jason Samsd19f10d2009-05-22 14:03:28 -0700313/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700314//
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800315#ifndef ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -0700316
Stephen Hines60f9a622011-03-01 17:34:59 -0800317
Jason Samsd19f10d2009-05-22 14:03:28 -0700318namespace android {
319namespace renderscript {
320
Jason Samsc5765372011-04-28 18:26:48 -0700321static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
322
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800323static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700324 uint32_t w = out.getDimX();
325 uint32_t h = out.getDimY();
326
Jason Sams6f5c61c2009-07-28 17:20:11 -0700327 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700328 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
329 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
330 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
331
Jason Sams6f5c61c2009-07-28 17:20:11 -0700332 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700333 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
334 oPtr ++;
335 i1 += 2;
336 i2 += 2;
337 }
338 }
339}
340
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800341static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700342 uint32_t w = out.getDimX();
343 uint32_t h = out.getDimY();
344
Jason Sams6f5c61c2009-07-28 17:20:11 -0700345 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700346 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
347 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
348 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
349
Jason Sams6f5c61c2009-07-28 17:20:11 -0700350 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700351 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700352 oPtr ++;
353 i1 += 2;
354 i2 += 2;
355 }
356 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700357}
358
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800359static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Samse20e3b42010-01-19 17:53:54 -0800360 uint32_t w = out.getDimX();
361 uint32_t h = out.getDimY();
362
363 for (uint32_t y=0; y < h; y++) {
364 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
365 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
366 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
367
368 for (uint32_t x=0; x < w; x++) {
369 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
370 oPtr ++;
371 i1 += 2;
372 i2 += 2;
373 }
374 }
375}
376
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800377static void mip(const Adapter2D &out, const Adapter2D &in) {
378 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Sams6f5c61c2009-07-28 17:20:11 -0700379 case 32:
380 mip8888(out, in);
381 break;
382 case 16:
383 mip565(out, in);
384 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800385 case 8:
386 mip8(out, in);
387 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700388 }
Jason Sams6f5c61c2009-07-28 17:20:11 -0700389}
Jason Samsd19f10d2009-05-22 14:03:28 -0700390
Jason Sams5476b452010-12-08 16:14:36 -0800391void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
392 Allocation *a = static_cast<Allocation *>(va);
Jason Sams7e8aae72011-05-26 16:33:01 -0700393 a->sendDirty(rsc);
Jason Sams5476b452010-12-08 16:14:36 -0800394 a->syncAll(rsc, src);
395}
396
Jason Samsf7086092011-01-12 13:28:37 -0800397void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700398 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsc5765372011-04-28 18:26:48 -0700399 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams4ef66502010-12-10 16:03:15 -0800400}
401
402void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
403 Allocation *texAlloc = static_cast<Allocation *>(va);
404 const Type * t = texAlloc->getType();
405
406 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
407 if (s != dataLen) {
408 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
409 return;
410 }
411
412 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700413}
414
Jason Sams49a05d72010-12-29 14:31:29 -0800415void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
416 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700417 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800418 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700419}
420
Jason Sams49a05d72010-12-29 14:31:29 -0800421void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
422 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700423 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800424 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700425}
426
Jason Sams49a05d72010-12-29 14:31:29 -0800427void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
428 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700429 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800430 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700431}
432
Jason Sams49a05d72010-12-29 14:31:29 -0800433void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
434 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700435 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800436 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700437}
438
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700439void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
Jason Sams40a29e82009-08-10 14:55:26 -0700440 Allocation *a = static_cast<Allocation *>(va);
441 a->read(data);
442}
443
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800444void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700445 Allocation *a = static_cast<Allocation *>(va);
446 a->resize1D(rsc, dimX);
447}
448
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800449void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700450 Allocation *a = static_cast<Allocation *>(va);
451 a->resize2D(rsc, dimX, dimY);
452}
453
Jason Samsc5765372011-04-28 18:26:48 -0700454static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800455 Context *rsc = static_cast<Context *>(con);
456 Allocation *texAlloc = static_cast<Allocation *>(va);
457 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
458 for (uint32_t face = 0; face < numFaces; face ++) {
459 Adapter2D adapt(rsc, texAlloc);
460 Adapter2D adapt2(rsc, texAlloc);
461 adapt.setFace(face);
462 adapt2.setFace(face);
463 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
464 adapt.setLOD(lod);
465 adapt2.setLOD(lod + 1);
466 mip(adapt2, adapt);
467 }
468 }
469}
470
Jason Samsc5765372011-04-28 18:26:48 -0700471RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
472 RsAllocationMipmapControl mips,
473 uint32_t usages) {
Jason Sams7e8aae72011-05-26 16:33:01 -0700474 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips);
475 if (!alloc) {
476 return NULL;
477 }
Jason Sams31a7e422010-10-26 13:09:17 -0700478 alloc->incUserRef();
479 return alloc;
480}
481
Jason Samsc5765372011-04-28 18:26:48 -0700482RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
483 RsAllocationMipmapControl mips,
484 const void *data, size_t data_length, uint32_t usages) {
Jason Sams5476b452010-12-08 16:14:36 -0800485 Type *t = static_cast<Type *>(vtype);
Jason Sams31a7e422010-10-26 13:09:17 -0700486
Jason Samsc5765372011-04-28 18:26:48 -0700487 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages);
Jason Sams31a7e422010-10-26 13:09:17 -0700488 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
489 if (texAlloc == NULL) {
490 LOGE("Memory allocation failure");
491 return NULL;
492 }
493
Jason Sams5476b452010-12-08 16:14:36 -0800494 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsd4b23b52010-12-13 15:32:35 -0800495 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc5765372011-04-28 18:26:48 -0700496 AllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams31a7e422010-10-26 13:09:17 -0700497 }
498
Jason Sams7e8aae72011-05-26 16:33:01 -0700499 texAlloc->sendDirty(rsc);
Jason Sams31a7e422010-10-26 13:09:17 -0700500 return texAlloc;
501}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800502
Jason Samsc5765372011-04-28 18:26:48 -0700503RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
504 RsAllocationMipmapControl mips,
505 const void *data, size_t data_length, uint32_t usages) {
Jason Sams5476b452010-12-08 16:14:36 -0800506 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800507
508 // Cubemap allocation's faces should be Width by Width each.
509 // Source data should have 6 * Width by Width pixels
510 // Error checking is done in the java layer
Jason Samsc5765372011-04-28 18:26:48 -0700511 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800512 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
513 if (texAlloc == NULL) {
514 LOGE("Memory allocation failure");
515 return NULL;
516 }
517
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800518 uint32_t faceSize = t->getDimX();
519 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
520 uint32_t copySize = faceSize * t->getElementSizeBytes();
521
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800522 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams5476b452010-12-08 16:14:36 -0800523 for (uint32_t face = 0; face < 6; face ++) {
524 Adapter2D faceAdapter(rsc, texAlloc);
525 faceAdapter.setFace(face);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800526
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800527 for (uint32_t dI = 0; dI < faceSize; dI ++) {
528 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
529 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800530
Jason Sams5476b452010-12-08 16:14:36 -0800531 // Move the data pointer to the next cube face
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800532 sourcePtr += copySize;
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800533 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800534
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800535 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Samsc5765372011-04-28 18:26:48 -0700536 AllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800537 }
538
Jason Sams7e8aae72011-05-26 16:33:01 -0700539 texAlloc->sendDirty(rsc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800540 return texAlloc;
541}
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800542
Jason Samsc5765372011-04-28 18:26:48 -0700543}
544}
545
546const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
547 Allocation *a = static_cast<Allocation *>(va);
548 a->getType()->incUserRef();
549
550 return a->getType();
551}
552
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800553#endif //ANDROID_RS_SERIALIZE