blob: c8e71828d8b44ab52c68b83141c0661304ac2cd4 [file] [log] [blame]
Jason Sams221a4b12012-02-22 15:22:41 -08001/*
Tim Murray10913a52013-08-20 17:19:47 -07002 * Copyright (C) 2013 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"
Tim Murray10913a52013-08-20 17:19:47 -070018#include "rsCppInternal.h"
Jason Sams221a4b12012-02-22 15:22:41 -080019
Jason Sams69cccdf2012-04-02 19:11:49 -070020using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080021using namespace RSC;
Jason Sams221a4b12012-02-22 15:22:41 -080022
23void * Allocation::getIDSafe() const {
Jason Sams221a4b12012-02-22 15:22:41 -080024 return getID();
25}
26
Jason Sams69cccdf2012-04-02 19:11:49 -070027void Allocation::updateCacheInfo(sp<const Type> t) {
Jason Sams221a4b12012-02-22 15:22:41 -080028 mCurrentDimX = t->getX();
29 mCurrentDimY = t->getY();
30 mCurrentDimZ = t->getZ();
31 mCurrentCount = mCurrentDimX;
32 if (mCurrentDimY > 1) {
33 mCurrentCount *= mCurrentDimY;
34 }
35 if (mCurrentDimZ > 1) {
36 mCurrentCount *= mCurrentDimZ;
37 }
38}
39
Tim Murray84bf2b82012-10-31 16:03:16 -070040Allocation::Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage) :
Tim Murraybaca6c32012-11-14 16:51:46 -080041 BaseObj(id, rs), mSelectedY(0), mSelectedZ(0), mSelectedLOD(0),
42 mSelectedFace(RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X) {
Jason Sams69cccdf2012-04-02 19:11:49 -070043
Jason Sams221a4b12012-02-22 15:22:41 -080044 if ((usage & ~(RS_ALLOCATION_USAGE_SCRIPT |
45 RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE |
46 RS_ALLOCATION_USAGE_GRAPHICS_VERTEX |
47 RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS |
48 RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams221a4b12012-02-22 15:22:41 -080049 RS_ALLOCATION_USAGE_IO_INPUT |
Tim Murray96267c22013-02-12 11:25:12 -080050 RS_ALLOCATION_USAGE_IO_OUTPUT |
51 RS_ALLOCATION_USAGE_SHARED)) != 0) {
Jason Sams221a4b12012-02-22 15:22:41 -080052 ALOGE("Unknown usage specified.");
53 }
54
Jason Sams3522f402012-03-23 11:47:26 -070055 if ((usage & RS_ALLOCATION_USAGE_IO_INPUT) != 0) {
Jason Sams221a4b12012-02-22 15:22:41 -080056 mWriteAllowed = false;
Jason Sams3522f402012-03-23 11:47:26 -070057 if ((usage & ~(RS_ALLOCATION_USAGE_IO_INPUT |
Jason Sams221a4b12012-02-22 15:22:41 -080058 RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE |
59 RS_ALLOCATION_USAGE_SCRIPT)) != 0) {
60 ALOGE("Invalid usage combination.");
61 }
62 }
63
64 mType = t;
65 mUsage = usage;
66
Tim Murray89daad62013-07-29 14:30:02 -070067 if (t != NULL) {
Jason Sams221a4b12012-02-22 15:22:41 -080068 updateCacheInfo(t);
69 }
Tim Murray89daad62013-07-29 14:30:02 -070070
Jason Sams221a4b12012-02-22 15:22:41 -080071}
72
Tim Murray89daad62013-07-29 14:30:02 -070073
74
Jason Sams221a4b12012-02-22 15:22:41 -080075void Allocation::validateIsInt32() {
76 RsDataType dt = mType->getElement()->getDataType();
77 if ((dt == RS_TYPE_SIGNED_32) || (dt == RS_TYPE_UNSIGNED_32)) {
78 return;
79 }
80 ALOGE("32 bit integer source does not match allocation type %i", dt);
81}
82
83void Allocation::validateIsInt16() {
84 RsDataType dt = mType->getElement()->getDataType();
85 if ((dt == RS_TYPE_SIGNED_16) || (dt == RS_TYPE_UNSIGNED_16)) {
86 return;
87 }
88 ALOGE("16 bit integer source does not match allocation type %i", dt);
89}
90
91void Allocation::validateIsInt8() {
92 RsDataType dt = mType->getElement()->getDataType();
93 if ((dt == RS_TYPE_SIGNED_8) || (dt == RS_TYPE_UNSIGNED_8)) {
94 return;
95 }
96 ALOGE("8 bit integer source does not match allocation type %i", dt);
97}
98
99void Allocation::validateIsFloat32() {
100 RsDataType dt = mType->getElement()->getDataType();
101 if (dt == RS_TYPE_FLOAT_32) {
102 return;
103 }
104 ALOGE("32 bit float source does not match allocation type %i", dt);
105}
106
107void Allocation::validateIsObject() {
108 RsDataType dt = mType->getElement()->getDataType();
109 if ((dt == RS_TYPE_ELEMENT) ||
110 (dt == RS_TYPE_TYPE) ||
111 (dt == RS_TYPE_ALLOCATION) ||
112 (dt == RS_TYPE_SAMPLER) ||
113 (dt == RS_TYPE_SCRIPT) ||
114 (dt == RS_TYPE_MESH) ||
115 (dt == RS_TYPE_PROGRAM_FRAGMENT) ||
116 (dt == RS_TYPE_PROGRAM_VERTEX) ||
117 (dt == RS_TYPE_PROGRAM_RASTER) ||
118 (dt == RS_TYPE_PROGRAM_STORE)) {
119 return;
120 }
121 ALOGE("Object source does not match allocation type %i", dt);
122}
123
124void Allocation::updateFromNative() {
125 BaseObj::updateFromNative();
126
Tim Murraya4230962013-07-17 16:50:10 -0700127 const void *typeID = RS::dispatch->AllocationGetType(mRS->getContext(), getID());
Jason Sams221a4b12012-02-22 15:22:41 -0800128 if(typeID != NULL) {
Jason Sams69cccdf2012-04-02 19:11:49 -0700129 sp<const Type> old = mType;
130 sp<Type> t = new Type((void *)typeID, mRS);
Jason Sams221a4b12012-02-22 15:22:41 -0800131 t->updateFromNative();
132 updateCacheInfo(t);
133 mType = t;
Jason Sams221a4b12012-02-22 15:22:41 -0800134 }
135}
136
137void Allocation::syncAll(RsAllocationUsageType srcLocation) {
138 switch (srcLocation) {
139 case RS_ALLOCATION_USAGE_SCRIPT:
140 case RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS:
141 case RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE:
142 case RS_ALLOCATION_USAGE_GRAPHICS_VERTEX:
Tim Murray10913a52013-08-20 17:19:47 -0700143 case RS_ALLOCATION_USAGE_SHARED:
Jason Sams221a4b12012-02-22 15:22:41 -0800144 break;
145 default:
Tim Murray10913a52013-08-20 17:19:47 -0700146 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Source must be exactly one usage type.");
147 return;
Jason Sams221a4b12012-02-22 15:22:41 -0800148 }
Tim Murray10913a52013-08-20 17:19:47 -0700149 tryDispatch(mRS, RS::dispatch->AllocationSyncAll(mRS->getContext(), getIDSafe(), srcLocation));
Jason Sams221a4b12012-02-22 15:22:41 -0800150}
151
152void Allocation::ioSendOutput() {
Tim Murray0b575de2013-03-15 15:56:43 -0700153#ifndef RS_COMPATIBILITY_LIB
Jason Sams221a4b12012-02-22 15:22:41 -0800154 if ((mUsage & RS_ALLOCATION_USAGE_IO_OUTPUT) == 0) {
Tim Murray10913a52013-08-20 17:19:47 -0700155 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Can only send buffer if IO_OUTPUT usage specified.");
156 return;
Jason Sams221a4b12012-02-22 15:22:41 -0800157 }
Tim Murray10913a52013-08-20 17:19:47 -0700158 tryDispatch(mRS, RS::dispatch->AllocationIoSend(mRS->getContext(), getID()));
Tim Murray0b575de2013-03-15 15:56:43 -0700159#endif
Jason Sams221a4b12012-02-22 15:22:41 -0800160}
161
162void Allocation::ioGetInput() {
Tim Murray0b575de2013-03-15 15:56:43 -0700163#ifndef RS_COMPATIBILITY_LIB
Jason Sams221a4b12012-02-22 15:22:41 -0800164 if ((mUsage & RS_ALLOCATION_USAGE_IO_INPUT) == 0) {
Tim Murray10913a52013-08-20 17:19:47 -0700165 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Can only send buffer if IO_OUTPUT usage specified.");
166 return;
Jason Sams221a4b12012-02-22 15:22:41 -0800167 }
Tim Murray10913a52013-08-20 17:19:47 -0700168 tryDispatch(mRS, RS::dispatch->AllocationIoReceive(mRS->getContext(), getID()));
Tim Murray0b575de2013-03-15 15:56:43 -0700169#endif
Jason Sams221a4b12012-02-22 15:22:41 -0800170}
171
Jason Sams221a4b12012-02-22 15:22:41 -0800172void Allocation::generateMipmaps() {
Tim Murray10913a52013-08-20 17:19:47 -0700173 tryDispatch(mRS, RS::dispatch->AllocationGenerateMipmaps(mRS->getContext(), getID()));
Jason Sams221a4b12012-02-22 15:22:41 -0800174}
175
Tim Murray0b93e302012-11-15 14:56:54 -0800176void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const void *data) {
Jason Sams69cccdf2012-04-02 19:11:49 -0700177
Jason Sams221a4b12012-02-22 15:22:41 -0800178 if(count < 1) {
Tim Murray10913a52013-08-20 17:19:47 -0700179 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Count must be >= 1.");
Jason Sams221a4b12012-02-22 15:22:41 -0800180 return;
181 }
182 if((off + count) > mCurrentCount) {
183 ALOGE("Overflow, Available count %zu, got %zu at offset %zu.", mCurrentCount, count, off);
Tim Murray10913a52013-08-20 17:19:47 -0700184 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Invalid copy specified");
Jason Sams221a4b12012-02-22 15:22:41 -0800185 return;
186 }
Jason Sams221a4b12012-02-22 15:22:41 -0800187
Tim Murray10913a52013-08-20 17:19:47 -0700188 tryDispatch(mRS, RS::dispatch->Allocation1DData(mRS->getContext(), getIDSafe(), off, mSelectedLOD,
189 count, data, count * mType->getElement()->getSizeBytes()));
Jason Sams221a4b12012-02-22 15:22:41 -0800190}
191
Tim Murray0b93e302012-11-15 14:56:54 -0800192void Allocation::copy1DRangeTo(uint32_t off, size_t count, void *data) {
Tim Murray509ea5c2012-11-13 11:56:40 -0800193 if(count < 1) {
Tim Murray10913a52013-08-20 17:19:47 -0700194 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Count must be >= 1.");
Tim Murray509ea5c2012-11-13 11:56:40 -0800195 return;
196 }
197 if((off + count) > mCurrentCount) {
198 ALOGE("Overflow, Available count %zu, got %zu at offset %zu.", mCurrentCount, count, off);
Tim Murray10913a52013-08-20 17:19:47 -0700199 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Invalid copy specified");
Tim Murray509ea5c2012-11-13 11:56:40 -0800200 return;
201 }
Tim Murray0b93e302012-11-15 14:56:54 -0800202
Tim Murray10913a52013-08-20 17:19:47 -0700203 tryDispatch(mRS, RS::dispatch->Allocation1DRead(mRS->getContext(), getIDSafe(), off, mSelectedLOD,
204 count, data, count * mType->getElement()->getSizeBytes()));
Tim Murray509ea5c2012-11-13 11:56:40 -0800205}
206
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800207void Allocation::copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data,
208 uint32_t dataOff) {
Jason Sams69cccdf2012-04-02 19:11:49 -0700209
Tim Murray10913a52013-08-20 17:19:47 -0700210 tryDispatch(mRS, RS::dispatch->AllocationCopy2DRange(mRS->getContext(), getIDSafe(), off, 0,
211 mSelectedLOD, mSelectedFace,
212 count, 1, data->getIDSafe(), dataOff, 0,
213 data->mSelectedLOD, data->mSelectedFace));
Jason Sams221a4b12012-02-22 15:22:41 -0800214}
215
Tim Murray0b93e302012-11-15 14:56:54 -0800216void Allocation::copy1DFrom(const void* data) {
217 copy1DRangeFrom(0, mCurrentCount, data);
218}
219
220void Allocation::copy1DTo(void* data) {
221 copy1DRangeTo(0, mCurrentCount, data);
222}
223
224
Jason Sams221a4b12012-02-22 15:22:41 -0800225void Allocation::validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h) {
226 if (mAdaptedAllocation != NULL) {
227
228 } else {
229 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
Tim Murray10913a52013-08-20 17:19:47 -0700230 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Updated region larger than allocation.");
Jason Sams221a4b12012-02-22 15:22:41 -0800231 }
232 }
233}
234
235void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800236 const void *data) {
Jason Sams221a4b12012-02-22 15:22:41 -0800237 validate2DRange(xoff, yoff, w, h);
Tim Murray10913a52013-08-20 17:19:47 -0700238 tryDispatch(mRS, RS::dispatch->Allocation2DData(mRS->getContext(), getIDSafe(), xoff,
239 yoff, mSelectedLOD, mSelectedFace,
240 w, h, data, w * h * mType->getElement()->getSizeBytes(),
241 w * mType->getElement()->getSizeBytes()));
Jason Sams221a4b12012-02-22 15:22:41 -0800242}
243
244void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800245 sp<const Allocation> data, uint32_t dataXoff, uint32_t dataYoff) {
Jason Sams221a4b12012-02-22 15:22:41 -0800246 validate2DRange(xoff, yoff, w, h);
Tim Murray10913a52013-08-20 17:19:47 -0700247 tryDispatch(mRS, RS::dispatch->AllocationCopy2DRange(mRS->getContext(), getIDSafe(), xoff, yoff,
248 mSelectedLOD, mSelectedFace,
249 w, h, data->getIDSafe(), dataXoff, dataYoff,
250 data->mSelectedLOD, data->mSelectedFace));
Jason Sams221a4b12012-02-22 15:22:41 -0800251}
252
Tim Murray7b3e3092012-11-16 13:32:24 -0800253void Allocation::copy2DRangeTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
254 void* data) {
255 validate2DRange(xoff, yoff, w, h);
Tim Murray10913a52013-08-20 17:19:47 -0700256 tryDispatch(mRS, RS::dispatch->Allocation2DRead(mRS->getContext(), getIDSafe(), xoff, yoff,
257 mSelectedLOD, mSelectedFace, w, h, data,
258 w * h * mType->getElement()->getSizeBytes(),
259 w * mType->getElement()->getSizeBytes()));
Tim Murray7b3e3092012-11-16 13:32:24 -0800260}
261
Tim Murray358747a2012-11-26 13:52:04 -0800262void Allocation::copy2DStridedFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
263 const void *data, size_t stride) {
264 validate2DRange(xoff, yoff, w, h);
Tim Murray10913a52013-08-20 17:19:47 -0700265 tryDispatch(mRS, RS::dispatch->Allocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff,
266 mSelectedLOD, mSelectedFace, w, h, data,
267 w * h * mType->getElement()->getSizeBytes(), stride));
Tim Murray358747a2012-11-26 13:52:04 -0800268}
269
270void Allocation::copy2DStridedFrom(const void* data, size_t stride) {
271 copy2DStridedFrom(0, 0, mCurrentDimX, mCurrentDimY, data, stride);
272}
273
274void Allocation::copy2DStridedTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
275 void *data, size_t stride) {
276 validate2DRange(xoff, yoff, w, h);
Tim Murray10913a52013-08-20 17:19:47 -0700277 tryDispatch(mRS, RS::dispatch->Allocation2DRead(mRS->getContext(), getIDSafe(), xoff, yoff,
278 mSelectedLOD, mSelectedFace, w, h, data,
279 w * h * mType->getElement()->getSizeBytes(), stride));
Tim Murray358747a2012-11-26 13:52:04 -0800280}
281
282void Allocation::copy2DStridedTo(void* data, size_t stride) {
283 copy2DStridedTo(0, 0, mCurrentDimX, mCurrentDimY, data, stride);
284}
285
Tim Murray9d24ae62013-08-30 12:17:14 -0700286void Allocation::validate3DRange(uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t w,
287 uint32_t h, uint32_t d) {
288 if (mAdaptedAllocation != NULL) {
289
290 } else {
291 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
292 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Updated region larger than allocation.");
293 }
294 }
295}
296
297void Allocation::copy3DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t w,
298 uint32_t h, uint32_t d, const void* data) {
299 validate3DRange(xoff, yoff, zoff, w, h, d);
300 tryDispatch(mRS, RS::dispatch->Allocation3DData(mRS->getContext(), getIDSafe(), xoff, yoff, zoff,
301 mSelectedLOD, w, h, d, data,
302 w * h * d * mType->getElement()->getSizeBytes(),
303 w * mType->getElement()->getSizeBytes()));
304}
305
306void Allocation::copy3DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t w, uint32_t h, uint32_t d,
307 sp<const Allocation> data, uint32_t dataXoff, uint32_t dataYoff, uint32_t dataZoff) {
308 validate3DRange(xoff, yoff, zoff, dataXoff, dataYoff, dataZoff);
309 tryDispatch(mRS, RS::dispatch->AllocationCopy3DRange(mRS->getContext(), getIDSafe(), xoff, yoff, zoff,
310 mSelectedLOD, w, h, d, data->getIDSafe(),
311 dataXoff, dataYoff, dataZoff, data->mSelectedLOD));
312}
313
314
Tim Murray89daad62013-07-29 14:30:02 -0700315sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
316 RsAllocationMipmapControl mips, uint32_t usage) {
Tim Murray10913a52013-08-20 17:19:47 -0700317 void *id = 0;
318 if (rs->getError() == RS_SUCCESS) {
319 id = RS::dispatch->AllocationCreateTyped(rs->getContext(), type->getID(), mips, usage, 0);
320 }
Jason Sams221a4b12012-02-22 15:22:41 -0800321 if (id == 0) {
Tim Murray10913a52013-08-20 17:19:47 -0700322 rs->throwError(RS_ERROR_RUNTIME_ERROR, "Allocation creation failed");
Jason Sams221a4b12012-02-22 15:22:41 -0800323 return NULL;
324 }
325 return new Allocation(id, rs, type, usage);
326}
327
Tim Murray89daad62013-07-29 14:30:02 -0700328sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
329 RsAllocationMipmapControl mips, uint32_t usage,
330 void *pointer) {
Tim Murray10913a52013-08-20 17:19:47 -0700331 void *id = 0;
332 if (rs->getError() == RS_SUCCESS) {
333 id = RS::dispatch->AllocationCreateTyped(rs->getContext(), type->getID(), mips, usage,
334 (uintptr_t)pointer);
335 }
Jason Sams221a4b12012-02-22 15:22:41 -0800336 if (id == 0) {
Tim Murray10913a52013-08-20 17:19:47 -0700337 rs->throwError(RS_ERROR_RUNTIME_ERROR, "Allocation creation failed");
338 return NULL;
Jason Sams221a4b12012-02-22 15:22:41 -0800339 }
340 return new Allocation(id, rs, type, usage);
341}
342
Tim Murray89daad62013-07-29 14:30:02 -0700343sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
344 uint32_t usage) {
Jason Sams221a4b12012-02-22 15:22:41 -0800345 return createTyped(rs, type, RS_ALLOCATION_MIPMAP_NONE, usage);
346}
347
Tim Murray89daad62013-07-29 14:30:02 -0700348sp<Allocation> Allocation::createSized(sp<RS> rs, sp<const Element> e,
349 size_t count, uint32_t usage) {
Jason Sams221a4b12012-02-22 15:22:41 -0800350 Type::Builder b(rs, e);
351 b.setX(count);
Jason Sams69cccdf2012-04-02 19:11:49 -0700352 sp<const Type> t = b.create();
Jason Sams221a4b12012-02-22 15:22:41 -0800353
Tim Murray684726c2012-11-14 11:57:42 -0800354 return createTyped(rs, t, usage);
355}
356
Tim Murray89daad62013-07-29 14:30:02 -0700357sp<Allocation> Allocation::createSized2D(sp<RS> rs, sp<const Element> e,
358 size_t x, size_t y, uint32_t usage) {
Tim Murray684726c2012-11-14 11:57:42 -0800359 Type::Builder b(rs, e);
360 b.setX(x);
361 b.setY(y);
362 sp<const Type> t = b.create();
363
364 return createTyped(rs, t, usage);
Jason Sams221a4b12012-02-22 15:22:41 -0800365}