blob: 9f3ce2095089ed9279bddbc11ef86d66182cd153 [file] [log] [blame]
Jason Sams221a4b12012-02-22 15:22:41 -08001/*
Jason Sams3522f402012-03-23 11:47:26 -07002 * Copyright (C) 2012 The Android Open Source Project
Jason Sams221a4b12012-02-22 15:22:41 -08003 *
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 */
16
Jason Sams221a4b12012-02-22 15:22:41 -080017#include "RenderScript.h"
Jason Sams221a4b12012-02-22 15:22:41 -080018
Jason Sams69cccdf2012-04-02 19:11:49 -070019using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080020using namespace RSC;
Jason Sams221a4b12012-02-22 15:22:41 -080021
22void * Allocation::getIDSafe() const {
Jason Sams221a4b12012-02-22 15:22:41 -080023 return getID();
24}
25
Jason Sams69cccdf2012-04-02 19:11:49 -070026void Allocation::updateCacheInfo(sp<const Type> t) {
Jason Sams221a4b12012-02-22 15:22:41 -080027 mCurrentDimX = t->getX();
28 mCurrentDimY = t->getY();
29 mCurrentDimZ = t->getZ();
30 mCurrentCount = mCurrentDimX;
31 if (mCurrentDimY > 1) {
32 mCurrentCount *= mCurrentDimY;
33 }
34 if (mCurrentDimZ > 1) {
35 mCurrentCount *= mCurrentDimZ;
36 }
37}
38
Tim Murray84bf2b82012-10-31 16:03:16 -070039Allocation::Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage) :
Tim Murraybaca6c32012-11-14 16:51:46 -080040 BaseObj(id, rs), mSelectedY(0), mSelectedZ(0), mSelectedLOD(0),
41 mSelectedFace(RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X) {
Jason Sams69cccdf2012-04-02 19:11:49 -070042
Jason Sams221a4b12012-02-22 15:22:41 -080043 if ((usage & ~(RS_ALLOCATION_USAGE_SCRIPT |
44 RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE |
45 RS_ALLOCATION_USAGE_GRAPHICS_VERTEX |
46 RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS |
47 RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams221a4b12012-02-22 15:22:41 -080048 RS_ALLOCATION_USAGE_IO_INPUT |
Tim Murray96267c22013-02-12 11:25:12 -080049 RS_ALLOCATION_USAGE_IO_OUTPUT |
50 RS_ALLOCATION_USAGE_SHARED)) != 0) {
Jason Sams221a4b12012-02-22 15:22:41 -080051 ALOGE("Unknown usage specified.");
52 }
53
Jason Sams3522f402012-03-23 11:47:26 -070054 if ((usage & RS_ALLOCATION_USAGE_IO_INPUT) != 0) {
Jason Sams221a4b12012-02-22 15:22:41 -080055 mWriteAllowed = false;
Jason Sams3522f402012-03-23 11:47:26 -070056 if ((usage & ~(RS_ALLOCATION_USAGE_IO_INPUT |
Jason Sams221a4b12012-02-22 15:22:41 -080057 RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE |
58 RS_ALLOCATION_USAGE_SCRIPT)) != 0) {
59 ALOGE("Invalid usage combination.");
60 }
61 }
62
63 mType = t;
64 mUsage = usage;
65
Jason Sams69cccdf2012-04-02 19:11:49 -070066 if (t.get() != NULL) {
Jason Sams221a4b12012-02-22 15:22:41 -080067 updateCacheInfo(t);
68 }
69}
70
71void Allocation::validateIsInt32() {
72 RsDataType dt = mType->getElement()->getDataType();
73 if ((dt == RS_TYPE_SIGNED_32) || (dt == RS_TYPE_UNSIGNED_32)) {
74 return;
75 }
76 ALOGE("32 bit integer source does not match allocation type %i", dt);
77}
78
79void Allocation::validateIsInt16() {
80 RsDataType dt = mType->getElement()->getDataType();
81 if ((dt == RS_TYPE_SIGNED_16) || (dt == RS_TYPE_UNSIGNED_16)) {
82 return;
83 }
84 ALOGE("16 bit integer source does not match allocation type %i", dt);
85}
86
87void Allocation::validateIsInt8() {
88 RsDataType dt = mType->getElement()->getDataType();
89 if ((dt == RS_TYPE_SIGNED_8) || (dt == RS_TYPE_UNSIGNED_8)) {
90 return;
91 }
92 ALOGE("8 bit integer source does not match allocation type %i", dt);
93}
94
95void Allocation::validateIsFloat32() {
96 RsDataType dt = mType->getElement()->getDataType();
97 if (dt == RS_TYPE_FLOAT_32) {
98 return;
99 }
100 ALOGE("32 bit float source does not match allocation type %i", dt);
101}
102
103void Allocation::validateIsObject() {
104 RsDataType dt = mType->getElement()->getDataType();
105 if ((dt == RS_TYPE_ELEMENT) ||
106 (dt == RS_TYPE_TYPE) ||
107 (dt == RS_TYPE_ALLOCATION) ||
108 (dt == RS_TYPE_SAMPLER) ||
109 (dt == RS_TYPE_SCRIPT) ||
110 (dt == RS_TYPE_MESH) ||
111 (dt == RS_TYPE_PROGRAM_FRAGMENT) ||
112 (dt == RS_TYPE_PROGRAM_VERTEX) ||
113 (dt == RS_TYPE_PROGRAM_RASTER) ||
114 (dt == RS_TYPE_PROGRAM_STORE)) {
115 return;
116 }
117 ALOGE("Object source does not match allocation type %i", dt);
118}
119
120void Allocation::updateFromNative() {
121 BaseObj::updateFromNative();
122
Tim Murray84bf2b82012-10-31 16:03:16 -0700123 const void *typeID = rsaAllocationGetType(mRS->getContext(), getID());
Jason Sams221a4b12012-02-22 15:22:41 -0800124 if(typeID != NULL) {
Jason Sams69cccdf2012-04-02 19:11:49 -0700125 sp<const Type> old = mType;
126 sp<Type> t = new Type((void *)typeID, mRS);
Jason Sams221a4b12012-02-22 15:22:41 -0800127 t->updateFromNative();
128 updateCacheInfo(t);
129 mType = t;
Jason Sams221a4b12012-02-22 15:22:41 -0800130 }
131}
132
133void Allocation::syncAll(RsAllocationUsageType srcLocation) {
134 switch (srcLocation) {
135 case RS_ALLOCATION_USAGE_SCRIPT:
136 case RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS:
137 case RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE:
138 case RS_ALLOCATION_USAGE_GRAPHICS_VERTEX:
139 break;
140 default:
141 ALOGE("Source must be exactly one usage type.");
142 }
Tim Murray84bf2b82012-10-31 16:03:16 -0700143 rsAllocationSyncAll(mRS->getContext(), getIDSafe(), srcLocation);
Jason Sams221a4b12012-02-22 15:22:41 -0800144}
145
146void Allocation::ioSendOutput() {
147 if ((mUsage & RS_ALLOCATION_USAGE_IO_OUTPUT) == 0) {
148 ALOGE("Can only send buffer if IO_OUTPUT usage specified.");
149 }
Tim Murray84bf2b82012-10-31 16:03:16 -0700150 rsAllocationIoSend(mRS->getContext(), getID());
Jason Sams221a4b12012-02-22 15:22:41 -0800151}
152
153void Allocation::ioGetInput() {
154 if ((mUsage & RS_ALLOCATION_USAGE_IO_INPUT) == 0) {
155 ALOGE("Can only send buffer if IO_OUTPUT usage specified.");
156 }
Tim Murray84bf2b82012-10-31 16:03:16 -0700157 rsAllocationIoReceive(mRS->getContext(), getID());
Jason Sams221a4b12012-02-22 15:22:41 -0800158}
159
Jason Sams221a4b12012-02-22 15:22:41 -0800160void Allocation::generateMipmaps() {
Tim Murray84bf2b82012-10-31 16:03:16 -0700161 rsAllocationGenerateMipmaps(mRS->getContext(), getID());
Jason Sams221a4b12012-02-22 15:22:41 -0800162}
163
Tim Murray0b93e302012-11-15 14:56:54 -0800164void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const void *data) {
Jason Sams69cccdf2012-04-02 19:11:49 -0700165
Jason Sams221a4b12012-02-22 15:22:41 -0800166 if(count < 1) {
167 ALOGE("Count must be >= 1.");
168 return;
169 }
170 if((off + count) > mCurrentCount) {
171 ALOGE("Overflow, Available count %zu, got %zu at offset %zu.", mCurrentCount, count, off);
172 return;
173 }
Jason Sams221a4b12012-02-22 15:22:41 -0800174
Tim Murray0b93e302012-11-15 14:56:54 -0800175 rsAllocation1DData(mRS->getContext(), getIDSafe(), off, mSelectedLOD, count, data,
176 count * mType->getElement()->getSizeBytes());
Jason Sams221a4b12012-02-22 15:22:41 -0800177}
178
Tim Murray0b93e302012-11-15 14:56:54 -0800179void Allocation::copy1DRangeTo(uint32_t off, size_t count, void *data) {
Tim Murray509ea5c2012-11-13 11:56:40 -0800180 if(count < 1) {
181 ALOGE("Count must be >= 1.");
182 return;
183 }
184 if((off + count) > mCurrentCount) {
185 ALOGE("Overflow, Available count %zu, got %zu at offset %zu.", mCurrentCount, count, off);
186 return;
187 }
Tim Murray0b93e302012-11-15 14:56:54 -0800188
189 rsAllocation1DRead(mRS->getContext(), getIDSafe(), off, mSelectedLOD, count, data,
190 count * mType->getElement()->getSizeBytes());
Tim Murray509ea5c2012-11-13 11:56:40 -0800191}
192
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800193void Allocation::copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data,
194 uint32_t dataOff) {
Jason Sams69cccdf2012-04-02 19:11:49 -0700195
Tim Murray84bf2b82012-10-31 16:03:16 -0700196 rsAllocationCopy2DRange(mRS->getContext(), getIDSafe(), off, 0,
Jason Sams221a4b12012-02-22 15:22:41 -0800197 mSelectedLOD, mSelectedFace,
198 count, 1, data->getIDSafe(), dataOff, 0,
199 data->mSelectedLOD, data->mSelectedFace);
200}
201
Tim Murray0b93e302012-11-15 14:56:54 -0800202void Allocation::copy1DFrom(const void* data) {
203 copy1DRangeFrom(0, mCurrentCount, data);
204}
205
206void Allocation::copy1DTo(void* data) {
207 copy1DRangeTo(0, mCurrentCount, data);
208}
209
210
Jason Sams221a4b12012-02-22 15:22:41 -0800211void Allocation::validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h) {
212 if (mAdaptedAllocation != NULL) {
213
214 } else {
215 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
216 ALOGE("Updated region larger than allocation.");
217 }
218 }
219}
220
221void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800222 const void *data) {
Jason Sams221a4b12012-02-22 15:22:41 -0800223 validate2DRange(xoff, yoff, w, h);
Tim Murray84bf2b82012-10-31 16:03:16 -0700224 rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
Tim Murray358747a2012-11-26 13:52:04 -0800225 w, h, data, w * h * mType->getElement()->getSizeBytes(), w * mType->getElement()->getSizeBytes());
Jason Sams221a4b12012-02-22 15:22:41 -0800226}
227
228void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800229 sp<const Allocation> data, uint32_t dataXoff, uint32_t dataYoff) {
Jason Sams221a4b12012-02-22 15:22:41 -0800230 validate2DRange(xoff, yoff, w, h);
Tim Murray84bf2b82012-10-31 16:03:16 -0700231 rsAllocationCopy2DRange(mRS->getContext(), getIDSafe(), xoff, yoff,
Jason Sams221a4b12012-02-22 15:22:41 -0800232 mSelectedLOD, mSelectedFace,
233 w, h, data->getIDSafe(), dataXoff, dataYoff,
234 data->mSelectedLOD, data->mSelectedFace);
235}
236
Tim Murray7b3e3092012-11-16 13:32:24 -0800237void Allocation::copy2DRangeTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
238 void* data) {
239 validate2DRange(xoff, yoff, w, h);
240 rsAllocation2DRead(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
Tim Murray358747a2012-11-26 13:52:04 -0800241 w, h, data, w * h * mType->getElement()->getSizeBytes(), w * mType->getElement()->getSizeBytes());
Tim Murray7b3e3092012-11-16 13:32:24 -0800242}
243
Tim Murray358747a2012-11-26 13:52:04 -0800244void Allocation::copy2DStridedFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
245 const void *data, size_t stride) {
246 validate2DRange(xoff, yoff, w, h);
247 rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
248 w, h, data, w * h * mType->getElement()->getSizeBytes(), stride);
249}
250
251void Allocation::copy2DStridedFrom(const void* data, size_t stride) {
252 copy2DStridedFrom(0, 0, mCurrentDimX, mCurrentDimY, data, stride);
253}
254
255void Allocation::copy2DStridedTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
256 void *data, size_t stride) {
257 validate2DRange(xoff, yoff, w, h);
258 rsAllocation2DRead(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
259 w, h, data, w * h * mType->getElement()->getSizeBytes(), stride);
260}
261
262void Allocation::copy2DStridedTo(void* data, size_t stride) {
263 copy2DStridedTo(0, 0, mCurrentDimX, mCurrentDimY, data, stride);
264}
265
266
Jason Sams221a4b12012-02-22 15:22:41 -0800267/*
Jason Sams221a4b12012-02-22 15:22:41 -0800268void resize(int dimX) {
269 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
270 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
271 }
272 mRS.nAllocationResize1D(getID(), dimX);
273 mRS.finish(); // Necessary because resize is fifoed and update is async.
274
275 int typeID = mRS.nAllocationGetType(getID());
276 mType = new Type(typeID, mRS);
277 mType.updateFromNative();
278 updateCacheInfo(mType);
279}
280
281void resize(int dimX, int dimY) {
282 if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
283 throw new RSInvalidStateException(
284 "Resize only support for 2D allocations at this time.");
285 }
286 if (mType.getY() == 0) {
287 throw new RSInvalidStateException(
288 "Resize only support for 2D allocations at this time.");
289 }
290 mRS.nAllocationResize2D(getID(), dimX, dimY);
291 mRS.finish(); // Necessary because resize is fifoed and update is async.
292
293 int typeID = mRS.nAllocationGetType(getID());
294 mType = new Type(typeID, mRS);
295 mType.updateFromNative();
296 updateCacheInfo(mType);
297}
298*/
299
300
Tim Murray84bf2b82012-10-31 16:03:16 -0700301android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
Tim Murray684726c2012-11-14 11:57:42 -0800302 RsAllocationMipmapControl mips, uint32_t usage) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700303 void *id = rsAllocationCreateTyped(rs->getContext(), type->getID(), mips, usage, 0);
Jason Sams221a4b12012-02-22 15:22:41 -0800304 if (id == 0) {
305 ALOGE("Allocation creation failed.");
306 return NULL;
307 }
308 return new Allocation(id, rs, type, usage);
309}
310
Tim Murray84bf2b82012-10-31 16:03:16 -0700311android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
Tim Murray684726c2012-11-14 11:57:42 -0800312 RsAllocationMipmapControl mips, uint32_t usage,
313 void *pointer) {
314 void *id = rsAllocationCreateTyped(rs->getContext(), type->getID(), mips, usage,
315 (uint32_t)pointer);
Jason Sams221a4b12012-02-22 15:22:41 -0800316 if (id == 0) {
317 ALOGE("Allocation creation failed.");
318 }
319 return new Allocation(id, rs, type, usage);
320}
321
Tim Murray84bf2b82012-10-31 16:03:16 -0700322android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
Tim Murray684726c2012-11-14 11:57:42 -0800323 uint32_t usage) {
Jason Sams221a4b12012-02-22 15:22:41 -0800324 return createTyped(rs, type, RS_ALLOCATION_MIPMAP_NONE, usage);
325}
326
Tim Murray84bf2b82012-10-31 16:03:16 -0700327android::sp<Allocation> Allocation::createSized(sp<RS> rs, sp<const Element> e,
Tim Murray684726c2012-11-14 11:57:42 -0800328 size_t count, uint32_t usage) {
Jason Sams221a4b12012-02-22 15:22:41 -0800329 Type::Builder b(rs, e);
330 b.setX(count);
Jason Sams69cccdf2012-04-02 19:11:49 -0700331 sp<const Type> t = b.create();
Jason Sams221a4b12012-02-22 15:22:41 -0800332
Tim Murray684726c2012-11-14 11:57:42 -0800333 return createTyped(rs, t, usage);
334}
335
336android::sp<Allocation> Allocation::createSized2D(sp<RS> rs, sp<const Element> e,
337 size_t x, size_t y, uint32_t usage) {
338 Type::Builder b(rs, e);
339 b.setX(x);
340 b.setY(y);
341 sp<const Type> t = b.create();
342
343 return createTyped(rs, t, usage);
Jason Sams221a4b12012-02-22 15:22:41 -0800344}