blob: f05b005d86e14e9b106c6bc8d8ed5f5600659281 [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
Tim Murray89daad62013-07-29 14:30:02 -070066 if (t != NULL) {
Jason Sams221a4b12012-02-22 15:22:41 -080067 updateCacheInfo(t);
68 }
Tim Murray89daad62013-07-29 14:30:02 -070069
Jason Sams221a4b12012-02-22 15:22:41 -080070}
71
Tim Murray89daad62013-07-29 14:30:02 -070072
73
Jason Sams221a4b12012-02-22 15:22:41 -080074void Allocation::validateIsInt32() {
75 RsDataType dt = mType->getElement()->getDataType();
76 if ((dt == RS_TYPE_SIGNED_32) || (dt == RS_TYPE_UNSIGNED_32)) {
77 return;
78 }
79 ALOGE("32 bit integer source does not match allocation type %i", dt);
80}
81
82void Allocation::validateIsInt16() {
83 RsDataType dt = mType->getElement()->getDataType();
84 if ((dt == RS_TYPE_SIGNED_16) || (dt == RS_TYPE_UNSIGNED_16)) {
85 return;
86 }
87 ALOGE("16 bit integer source does not match allocation type %i", dt);
88}
89
90void Allocation::validateIsInt8() {
91 RsDataType dt = mType->getElement()->getDataType();
92 if ((dt == RS_TYPE_SIGNED_8) || (dt == RS_TYPE_UNSIGNED_8)) {
93 return;
94 }
95 ALOGE("8 bit integer source does not match allocation type %i", dt);
96}
97
98void Allocation::validateIsFloat32() {
99 RsDataType dt = mType->getElement()->getDataType();
100 if (dt == RS_TYPE_FLOAT_32) {
101 return;
102 }
103 ALOGE("32 bit float source does not match allocation type %i", dt);
104}
105
106void Allocation::validateIsObject() {
107 RsDataType dt = mType->getElement()->getDataType();
108 if ((dt == RS_TYPE_ELEMENT) ||
109 (dt == RS_TYPE_TYPE) ||
110 (dt == RS_TYPE_ALLOCATION) ||
111 (dt == RS_TYPE_SAMPLER) ||
112 (dt == RS_TYPE_SCRIPT) ||
113 (dt == RS_TYPE_MESH) ||
114 (dt == RS_TYPE_PROGRAM_FRAGMENT) ||
115 (dt == RS_TYPE_PROGRAM_VERTEX) ||
116 (dt == RS_TYPE_PROGRAM_RASTER) ||
117 (dt == RS_TYPE_PROGRAM_STORE)) {
118 return;
119 }
120 ALOGE("Object source does not match allocation type %i", dt);
121}
122
123void Allocation::updateFromNative() {
124 BaseObj::updateFromNative();
125
Tim Murraya4230962013-07-17 16:50:10 -0700126 const void *typeID = RS::dispatch->AllocationGetType(mRS->getContext(), getID());
Jason Sams221a4b12012-02-22 15:22:41 -0800127 if(typeID != NULL) {
Jason Sams69cccdf2012-04-02 19:11:49 -0700128 sp<const Type> old = mType;
129 sp<Type> t = new Type((void *)typeID, mRS);
Jason Sams221a4b12012-02-22 15:22:41 -0800130 t->updateFromNative();
131 updateCacheInfo(t);
132 mType = t;
Jason Sams221a4b12012-02-22 15:22:41 -0800133 }
134}
135
136void Allocation::syncAll(RsAllocationUsageType srcLocation) {
137 switch (srcLocation) {
138 case RS_ALLOCATION_USAGE_SCRIPT:
139 case RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS:
140 case RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE:
141 case RS_ALLOCATION_USAGE_GRAPHICS_VERTEX:
142 break;
143 default:
144 ALOGE("Source must be exactly one usage type.");
145 }
Tim Murraya4230962013-07-17 16:50:10 -0700146 RS::dispatch->AllocationSyncAll(mRS->getContext(), getIDSafe(), srcLocation);
Jason Sams221a4b12012-02-22 15:22:41 -0800147}
148
149void Allocation::ioSendOutput() {
Tim Murray0b575de2013-03-15 15:56:43 -0700150#ifndef RS_COMPATIBILITY_LIB
Jason Sams221a4b12012-02-22 15:22:41 -0800151 if ((mUsage & RS_ALLOCATION_USAGE_IO_OUTPUT) == 0) {
152 ALOGE("Can only send buffer if IO_OUTPUT usage specified.");
153 }
Tim Murraya4230962013-07-17 16:50:10 -0700154 RS::dispatch->AllocationIoSend(mRS->getContext(), getID());
Tim Murray0b575de2013-03-15 15:56:43 -0700155#endif
Jason Sams221a4b12012-02-22 15:22:41 -0800156}
157
158void Allocation::ioGetInput() {
Tim Murray0b575de2013-03-15 15:56:43 -0700159#ifndef RS_COMPATIBILITY_LIB
Jason Sams221a4b12012-02-22 15:22:41 -0800160 if ((mUsage & RS_ALLOCATION_USAGE_IO_INPUT) == 0) {
161 ALOGE("Can only send buffer if IO_OUTPUT usage specified.");
162 }
Tim Murraya4230962013-07-17 16:50:10 -0700163 RS::dispatch->AllocationIoReceive(mRS->getContext(), getID());
Tim Murray0b575de2013-03-15 15:56:43 -0700164#endif
Jason Sams221a4b12012-02-22 15:22:41 -0800165}
166
Jason Sams221a4b12012-02-22 15:22:41 -0800167void Allocation::generateMipmaps() {
Tim Murraya4230962013-07-17 16:50:10 -0700168 RS::dispatch->AllocationGenerateMipmaps(mRS->getContext(), getID());
Jason Sams221a4b12012-02-22 15:22:41 -0800169}
170
Tim Murray0b93e302012-11-15 14:56:54 -0800171void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const void *data) {
Jason Sams69cccdf2012-04-02 19:11:49 -0700172
Jason Sams221a4b12012-02-22 15:22:41 -0800173 if(count < 1) {
174 ALOGE("Count must be >= 1.");
175 return;
176 }
177 if((off + count) > mCurrentCount) {
178 ALOGE("Overflow, Available count %zu, got %zu at offset %zu.", mCurrentCount, count, off);
179 return;
180 }
Jason Sams221a4b12012-02-22 15:22:41 -0800181
Tim Murraya4230962013-07-17 16:50:10 -0700182 RS::dispatch->Allocation1DData(mRS->getContext(), getIDSafe(), off, mSelectedLOD, count, data,
Tim Murray89daad62013-07-29 14:30:02 -0700183 count * mType->getElement()->getSizeBytes());
Jason Sams221a4b12012-02-22 15:22:41 -0800184}
185
Tim Murray0b93e302012-11-15 14:56:54 -0800186void Allocation::copy1DRangeTo(uint32_t off, size_t count, void *data) {
Tim Murray509ea5c2012-11-13 11:56:40 -0800187 if(count < 1) {
188 ALOGE("Count must be >= 1.");
189 return;
190 }
191 if((off + count) > mCurrentCount) {
192 ALOGE("Overflow, Available count %zu, got %zu at offset %zu.", mCurrentCount, count, off);
193 return;
194 }
Tim Murray0b93e302012-11-15 14:56:54 -0800195
Tim Murraya4230962013-07-17 16:50:10 -0700196 RS::dispatch->Allocation1DRead(mRS->getContext(), getIDSafe(), off, mSelectedLOD, count, data,
Tim Murray89daad62013-07-29 14:30:02 -0700197 count * mType->getElement()->getSizeBytes());
Tim Murray509ea5c2012-11-13 11:56:40 -0800198}
199
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800200void Allocation::copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data,
201 uint32_t dataOff) {
Jason Sams69cccdf2012-04-02 19:11:49 -0700202
Tim Murraya4230962013-07-17 16:50:10 -0700203 RS::dispatch->AllocationCopy2DRange(mRS->getContext(), getIDSafe(), off, 0,
Tim Murray89daad62013-07-29 14:30:02 -0700204 mSelectedLOD, mSelectedFace,
205 count, 1, data->getIDSafe(), dataOff, 0,
206 data->mSelectedLOD, data->mSelectedFace);
Jason Sams221a4b12012-02-22 15:22:41 -0800207}
208
Tim Murray0b93e302012-11-15 14:56:54 -0800209void Allocation::copy1DFrom(const void* data) {
210 copy1DRangeFrom(0, mCurrentCount, data);
211}
212
213void Allocation::copy1DTo(void* data) {
214 copy1DRangeTo(0, mCurrentCount, data);
215}
216
217
Jason Sams221a4b12012-02-22 15:22:41 -0800218void Allocation::validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h) {
219 if (mAdaptedAllocation != NULL) {
220
221 } else {
222 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
223 ALOGE("Updated region larger than allocation.");
224 }
225 }
226}
227
228void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800229 const void *data) {
Jason Sams221a4b12012-02-22 15:22:41 -0800230 validate2DRange(xoff, yoff, w, h);
Tim Murraya4230962013-07-17 16:50:10 -0700231 RS::dispatch->Allocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
Tim Murray89daad62013-07-29 14:30:02 -0700232 w, h, data, w * h * mType->getElement()->getSizeBytes(), w * mType->getElement()->getSizeBytes());
Jason Sams221a4b12012-02-22 15:22:41 -0800233}
234
235void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800236 sp<const Allocation> data, uint32_t dataXoff, uint32_t dataYoff) {
Jason Sams221a4b12012-02-22 15:22:41 -0800237 validate2DRange(xoff, yoff, w, h);
Tim Murraya4230962013-07-17 16:50:10 -0700238 RS::dispatch->AllocationCopy2DRange(mRS->getContext(), getIDSafe(), xoff, yoff,
Tim Murray89daad62013-07-29 14:30:02 -0700239 mSelectedLOD, mSelectedFace,
240 w, h, data->getIDSafe(), dataXoff, dataYoff,
241 data->mSelectedLOD, data->mSelectedFace);
Jason Sams221a4b12012-02-22 15:22:41 -0800242}
243
Tim Murray7b3e3092012-11-16 13:32:24 -0800244void Allocation::copy2DRangeTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
245 void* data) {
246 validate2DRange(xoff, yoff, w, h);
Tim Murraya4230962013-07-17 16:50:10 -0700247 RS::dispatch->Allocation2DRead(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
Tim Murray89daad62013-07-29 14:30:02 -0700248 w, h, data, w * h * mType->getElement()->getSizeBytes(), w * mType->getElement()->getSizeBytes());
Tim Murray7b3e3092012-11-16 13:32:24 -0800249}
250
Tim Murray358747a2012-11-26 13:52:04 -0800251void Allocation::copy2DStridedFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
252 const void *data, size_t stride) {
253 validate2DRange(xoff, yoff, w, h);
Tim Murraya4230962013-07-17 16:50:10 -0700254 RS::dispatch->Allocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
Tim Murray89daad62013-07-29 14:30:02 -0700255 w, h, data, w * h * mType->getElement()->getSizeBytes(), stride);
Tim Murray358747a2012-11-26 13:52:04 -0800256}
257
258void Allocation::copy2DStridedFrom(const void* data, size_t stride) {
259 copy2DStridedFrom(0, 0, mCurrentDimX, mCurrentDimY, data, stride);
260}
261
262void Allocation::copy2DStridedTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
263 void *data, size_t stride) {
264 validate2DRange(xoff, yoff, w, h);
Tim Murraya4230962013-07-17 16:50:10 -0700265 RS::dispatch->Allocation2DRead(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
Tim Murray89daad62013-07-29 14:30:02 -0700266 w, h, data, w * h * mType->getElement()->getSizeBytes(), stride);
Tim Murray358747a2012-11-26 13:52:04 -0800267}
268
269void Allocation::copy2DStridedTo(void* data, size_t stride) {
270 copy2DStridedTo(0, 0, mCurrentDimX, mCurrentDimY, data, stride);
271}
272
Tim Murray89daad62013-07-29 14:30:02 -0700273sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
274 RsAllocationMipmapControl mips, uint32_t usage) {
Tim Murraya4230962013-07-17 16:50:10 -0700275 void *id = RS::dispatch->AllocationCreateTyped(rs->getContext(), type->getID(), mips, usage, 0);
Jason Sams221a4b12012-02-22 15:22:41 -0800276 if (id == 0) {
277 ALOGE("Allocation creation failed.");
278 return NULL;
279 }
280 return new Allocation(id, rs, type, usage);
281}
282
Tim Murray89daad62013-07-29 14:30:02 -0700283sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
284 RsAllocationMipmapControl mips, uint32_t usage,
285 void *pointer) {
Tim Murraya4230962013-07-17 16:50:10 -0700286 void *id = RS::dispatch->AllocationCreateTyped(rs->getContext(), type->getID(), mips, usage,
Tim Murray89daad62013-07-29 14:30:02 -0700287 (uintptr_t)pointer);
Jason Sams221a4b12012-02-22 15:22:41 -0800288 if (id == 0) {
289 ALOGE("Allocation creation failed.");
290 }
291 return new Allocation(id, rs, type, usage);
292}
293
Tim Murray89daad62013-07-29 14:30:02 -0700294sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
295 uint32_t usage) {
Jason Sams221a4b12012-02-22 15:22:41 -0800296 return createTyped(rs, type, RS_ALLOCATION_MIPMAP_NONE, usage);
297}
298
Tim Murray89daad62013-07-29 14:30:02 -0700299sp<Allocation> Allocation::createSized(sp<RS> rs, sp<const Element> e,
300 size_t count, uint32_t usage) {
Jason Sams221a4b12012-02-22 15:22:41 -0800301 Type::Builder b(rs, e);
302 b.setX(count);
Jason Sams69cccdf2012-04-02 19:11:49 -0700303 sp<const Type> t = b.create();
Jason Sams221a4b12012-02-22 15:22:41 -0800304
Tim Murray684726c2012-11-14 11:57:42 -0800305 return createTyped(rs, t, usage);
306}
307
Tim Murray89daad62013-07-29 14:30:02 -0700308sp<Allocation> Allocation::createSized2D(sp<RS> rs, sp<const Element> e,
309 size_t x, size_t y, uint32_t usage) {
Tim Murray684726c2012-11-14 11:57:42 -0800310 Type::Builder b(rs, e);
311 b.setX(x);
312 b.setY(y);
313 sp<const Type> t = b.create();
314
315 return createTyped(rs, t, usage);
Jason Sams221a4b12012-02-22 15:22:41 -0800316}