blob: ba1e5f44918aaa8fe183b05a90e5c92e500b80a4 [file] [log] [blame]
Tim Murray7f0d5682012-11-08 16:35:24 -08001/*
2 * Copyright (C) 2008-2012 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 */
16
Tim Murray7f0d5682012-11-08 16:35:24 -080017#include <malloc.h>
18
19#include "RenderScript.h"
Tim Murrayfa77db82013-08-29 11:07:17 -070020#include "rsCppInternal.h"
Tim Murray7f0d5682012-11-08 16:35:24 -080021
22using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080023using namespace RSC;
Tim Murray7f0d5682012-11-08 16:35:24 -080024
Tim Murray3cd44af2012-11-14 11:25:27 -080025ScriptIntrinsic::ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e)
Tim Murray7f0d5682012-11-08 16:35:24 -080026 : Script(NULL, rs) {
Tim Murrayfa77db82013-08-29 11:07:17 -070027 mID = createDispatch(rs, RS::dispatch->ScriptIntrinsicCreate(rs->getContext(), id, e->getID()));
Tim Murray10913a52013-08-20 17:19:47 -070028 mElement = e;
Tim Murray7f0d5682012-11-08 16:35:24 -080029}
30
Tim Murrayb27b1812013-08-05 14:00:40 -070031ScriptIntrinsic::~ScriptIntrinsic() {
32
33}
34
Tim Murray21fa7a02013-08-15 16:25:03 -070035sp<ScriptIntrinsic3DLUT> ScriptIntrinsic3DLUT::create(sp<RS> rs, sp<const Element> e) {
Tim Murray10913a52013-08-20 17:19:47 -070036 if (e->isCompatible(Element::U8_4(rs)) == false) {
37 rs->throwError(RS_ERROR_INVALID_ELEMENT, "Element not supported for intrinsic");
38 return NULL;
39 }
Tim Murray21fa7a02013-08-15 16:25:03 -070040 return new ScriptIntrinsic3DLUT(rs, e);
41}
42
Tim Murray89daad62013-07-29 14:30:02 -070043ScriptIntrinsic3DLUT::ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e)
44 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_3DLUT, e) {
45
46}
47void ScriptIntrinsic3DLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) {
Tim Murray10913a52013-08-20 17:19:47 -070048 if (ain->getType()->getElement()->isCompatible(mElement) == false ||
49 aout->getType()->getElement()->isCompatible(mElement) == false) {
50 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "3DLUT forEach element mismatch");
51 return;
52 }
Tim Murray89daad62013-07-29 14:30:02 -070053 Script::forEach(0, ain, aout, NULL, 0);
54}
55void ScriptIntrinsic3DLUT::setLUT(sp<Allocation> lut) {
Tim Murray10913a52013-08-20 17:19:47 -070056 sp<const Type> t = lut->getType();
57 if (!t->getElement()->isCompatible(mElement)) {
58 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "setLUT element does not match");
59 return;
60 }
61 if (t->getZ() == 0) {
62 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "setLUT Allocation must be 3D");
63 return;
64 }
65
Tim Murray89daad62013-07-29 14:30:02 -070066 Script::setVar(0, lut);
67}
68
Tim Murray21fa7a02013-08-15 16:25:03 -070069sp<ScriptIntrinsicBlend> ScriptIntrinsicBlend::create(sp<RS> rs, sp<const Element> e) {
Tim Murray10913a52013-08-20 17:19:47 -070070 if (e->isCompatible(Element::U8_4(rs)) == false) {
71 rs->throwError(RS_ERROR_INVALID_ELEMENT, "Element not supported for intrinsic");
72 return NULL;
73 }
Tim Murray21fa7a02013-08-15 16:25:03 -070074 return new ScriptIntrinsicBlend(rs, e);
75}
76
Tim Murray3cd44af2012-11-14 11:25:27 -080077ScriptIntrinsicBlend::ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e)
Tim Murray7f0d5682012-11-08 16:35:24 -080078 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLEND, e) {
Tim Murray7f0d5682012-11-08 16:35:24 -080079}
80
Tim Murray75e877d2013-09-11 14:45:20 -070081void ScriptIntrinsicBlend::forEachClear(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -070082 if (in->getType()->getElement()->isCompatible(mElement) == false ||
83 out->getType()->getElement()->isCompatible(mElement) == false) {
84 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
85 }
Tim Murray7f0d5682012-11-08 16:35:24 -080086 Script::forEach(0, in, out, NULL, 0);
87}
88
Tim Murray75e877d2013-09-11 14:45:20 -070089void ScriptIntrinsicBlend::forEachSrc(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -070090 if (in->getType()->getElement()->isCompatible(mElement) == false ||
91 out->getType()->getElement()->isCompatible(mElement) == false) {
92 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
93 }
Tim Murray7f0d5682012-11-08 16:35:24 -080094 Script::forEach(1, in, out, NULL, 0);
95}
96
Tim Murray75e877d2013-09-11 14:45:20 -070097void ScriptIntrinsicBlend::forEachDst(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -070098 if (in->getType()->getElement()->isCompatible(mElement) == false ||
99 out->getType()->getElement()->isCompatible(mElement) == false) {
100 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
101 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800102 Script::forEach(2, in, out, NULL, 0);
103}
104
Tim Murray75e877d2013-09-11 14:45:20 -0700105void ScriptIntrinsicBlend::forEachSrcOver(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700106 if (in->getType()->getElement()->isCompatible(mElement) == false ||
107 out->getType()->getElement()->isCompatible(mElement) == false) {
108 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
109 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800110 Script::forEach(3, in, out, NULL, 0);
111}
112
Tim Murray75e877d2013-09-11 14:45:20 -0700113void ScriptIntrinsicBlend::forEachDstOver(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700114 if (in->getType()->getElement()->isCompatible(mElement) == false ||
115 out->getType()->getElement()->isCompatible(mElement) == false) {
116 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
117 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800118 Script::forEach(4, in, out, NULL, 0);
119}
120
Tim Murray75e877d2013-09-11 14:45:20 -0700121void ScriptIntrinsicBlend::forEachSrcIn(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700122 if (in->getType()->getElement()->isCompatible(mElement) == false ||
123 out->getType()->getElement()->isCompatible(mElement) == false) {
124 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
125 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800126 Script::forEach(5, in, out, NULL, 0);
127}
128
Tim Murray75e877d2013-09-11 14:45:20 -0700129void ScriptIntrinsicBlend::forEachDstIn(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700130 if (in->getType()->getElement()->isCompatible(mElement) == false ||
131 out->getType()->getElement()->isCompatible(mElement) == false) {
132 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
133 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800134 Script::forEach(6, in, out, NULL, 0);
135}
136
Tim Murray75e877d2013-09-11 14:45:20 -0700137void ScriptIntrinsicBlend::forEachSrcOut(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700138 if (in->getType()->getElement()->isCompatible(mElement) == false ||
139 out->getType()->getElement()->isCompatible(mElement) == false) {
140 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
141 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800142 Script::forEach(7, in, out, NULL, 0);
143}
144
Tim Murray75e877d2013-09-11 14:45:20 -0700145void ScriptIntrinsicBlend::forEachDstOut(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700146 if (in->getType()->getElement()->isCompatible(mElement) == false ||
147 out->getType()->getElement()->isCompatible(mElement) == false) {
148 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
149 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800150 Script::forEach(8, in, out, NULL, 0);
151}
152
Tim Murray75e877d2013-09-11 14:45:20 -0700153void ScriptIntrinsicBlend::forEachSrcAtop(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700154 if (in->getType()->getElement()->isCompatible(mElement) == false ||
155 out->getType()->getElement()->isCompatible(mElement) == false) {
156 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
157 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800158 Script::forEach(9, in, out, NULL, 0);
159}
160
Tim Murray75e877d2013-09-11 14:45:20 -0700161void ScriptIntrinsicBlend::forEachDstAtop(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700162 if (in->getType()->getElement()->isCompatible(mElement) == false ||
163 out->getType()->getElement()->isCompatible(mElement) == false) {
164 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
165 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800166 Script::forEach(10, in, out, NULL, 0);
167}
168
Tim Murray75e877d2013-09-11 14:45:20 -0700169void ScriptIntrinsicBlend::forEachXor(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700170 if (in->getType()->getElement()->isCompatible(mElement) == false ||
171 out->getType()->getElement()->isCompatible(mElement) == false) {
172 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
173 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800174 Script::forEach(11, in, out, NULL, 0);
175}
176
177// Numbering jumps here
Tim Murray75e877d2013-09-11 14:45:20 -0700178void ScriptIntrinsicBlend::forEachMultiply(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700179 if (in->getType()->getElement()->isCompatible(mElement) == false ||
180 out->getType()->getElement()->isCompatible(mElement) == false) {
181 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
182 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800183 Script::forEach(14, in, out, NULL, 0);
184}
185
186// Numbering jumps here
Tim Murray75e877d2013-09-11 14:45:20 -0700187void ScriptIntrinsicBlend::forEachAdd(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700188 if (in->getType()->getElement()->isCompatible(mElement) == false ||
189 out->getType()->getElement()->isCompatible(mElement) == false) {
190 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
191 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800192 Script::forEach(34, in, out, NULL, 0);
193}
194
Tim Murray75e877d2013-09-11 14:45:20 -0700195void ScriptIntrinsicBlend::forEachSubtract(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700196 if (in->getType()->getElement()->isCompatible(mElement) == false ||
197 out->getType()->getElement()->isCompatible(mElement) == false) {
198 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blend");
199 }
Tim Murray7f0d5682012-11-08 16:35:24 -0800200 Script::forEach(35, in, out, NULL, 0);
201}
202
Tim Murray89daad62013-07-29 14:30:02 -0700203
204
205
Tim Murray21fa7a02013-08-15 16:25:03 -0700206sp<ScriptIntrinsicBlur> ScriptIntrinsicBlur::create(sp<RS> rs, sp<const Element> e) {
Tim Murray10913a52013-08-20 17:19:47 -0700207 if ((e->isCompatible(Element::U8_4(rs)) == false) &&
208 (e->isCompatible(Element::U8(rs)) == false)) {
209 rs->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blur");
210 return NULL;
211 }
Tim Murray21fa7a02013-08-15 16:25:03 -0700212 return new ScriptIntrinsicBlur(rs, e);
213}
214
Tim Murray3cd44af2012-11-14 11:25:27 -0800215ScriptIntrinsicBlur::ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e)
Tim Murray8f1e60c2012-11-13 12:25:11 -0800216 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLUR, e) {
Tim Murray7f0d5682012-11-08 16:35:24 -0800217
Tim Murray8f1e60c2012-11-13 12:25:11 -0800218}
Tim Murray7f0d5682012-11-08 16:35:24 -0800219
Tim Murray21fa7a02013-08-15 16:25:03 -0700220void ScriptIntrinsicBlur::setInput(sp<Allocation> in) {
Tim Murray10913a52013-08-20 17:19:47 -0700221 if (in->getType()->getElement()->isCompatible(mElement) == false) {
222 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blur input");
223 return;
224 }
Tim Murray8f1e60c2012-11-13 12:25:11 -0800225 Script::setVar(1, in);
Tim Murray21fa7a02013-08-15 16:25:03 -0700226}
227
228void ScriptIntrinsicBlur::forEach(sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700229 if (out->getType()->getElement()->isCompatible(mElement) == false) {
230 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element in blur output");
231 return;
232 }
Tim Murray8f1e60c2012-11-13 12:25:11 -0800233 Script::forEach(0, NULL, out, NULL, 0);
234}
Tim Murray7f0d5682012-11-08 16:35:24 -0800235
Tim Murray8f1e60c2012-11-13 12:25:11 -0800236void ScriptIntrinsicBlur::setRadius(float radius) {
Tim Murray10913a52013-08-20 17:19:47 -0700237 if (radius > 0.f && radius <= 25.f) {
238 Script::setVar(0, &radius, sizeof(float));
239 } else {
240 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Blur radius out of 0-25 pixel bound");
241 }
Tim Murray8f1e60c2012-11-13 12:25:11 -0800242}
Tim Murray89daad62013-07-29 14:30:02 -0700243
244
245
Tim Murrayaae73c92013-09-03 17:05:46 -0700246sp<ScriptIntrinsicColorMatrix> ScriptIntrinsicColorMatrix::create(sp<RS> rs) {
247 return new ScriptIntrinsicColorMatrix(rs, Element::RGBA_8888(rs));
Tim Murray21fa7a02013-08-15 16:25:03 -0700248}
249
Tim Murray89daad62013-07-29 14:30:02 -0700250ScriptIntrinsicColorMatrix::ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e)
251 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX, e) {
Tim Murrayaae73c92013-09-03 17:05:46 -0700252 float add[4] = {0.f, 0.f, 0.f, 0.f};
253 setAdd(add);
Tim Murray89daad62013-07-29 14:30:02 -0700254
255}
256
257void ScriptIntrinsicColorMatrix::forEach(sp<Allocation> in, sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700258 if (!(in->getType()->getElement()->isCompatible(Element::U8(mRS))) &&
259 !(in->getType()->getElement()->isCompatible(Element::U8_2(mRS))) &&
260 !(in->getType()->getElement()->isCompatible(Element::U8_3(mRS))) &&
261 !(in->getType()->getElement()->isCompatible(Element::U8_4(mRS))) &&
262 !(in->getType()->getElement()->isCompatible(Element::F32(mRS))) &&
263 !(in->getType()->getElement()->isCompatible(Element::F32_2(mRS))) &&
264 !(in->getType()->getElement()->isCompatible(Element::F32_3(mRS))) &&
265 !(in->getType()->getElement()->isCompatible(Element::F32_4(mRS)))) {
266 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for ColorMatrix");
267 return;
268 }
269
270 if (!(out->getType()->getElement()->isCompatible(Element::U8(mRS))) &&
271 !(out->getType()->getElement()->isCompatible(Element::U8_2(mRS))) &&
272 !(out->getType()->getElement()->isCompatible(Element::U8_3(mRS))) &&
273 !(out->getType()->getElement()->isCompatible(Element::U8_4(mRS))) &&
274 !(out->getType()->getElement()->isCompatible(Element::F32(mRS))) &&
275 !(out->getType()->getElement()->isCompatible(Element::F32_2(mRS))) &&
276 !(out->getType()->getElement()->isCompatible(Element::F32_3(mRS))) &&
277 !(out->getType()->getElement()->isCompatible(Element::F32_4(mRS)))) {
278 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for ColorMatrix");
279 return;
280 }
281
Tim Murray89daad62013-07-29 14:30:02 -0700282 Script::forEach(0, in, out, NULL, 0);
283}
284
Tim Murray10913a52013-08-20 17:19:47 -0700285void ScriptIntrinsicColorMatrix::setAdd(float* add) {
286 Script::setVar(1, (void*)add, sizeof(float) * 4);
287}
Tim Murray89daad62013-07-29 14:30:02 -0700288
289void ScriptIntrinsicColorMatrix::setColorMatrix3(float* m) {
Tim Murrayaae73c92013-09-03 17:05:46 -0700290 float temp[16];
291 temp[0] = m[0];
292 temp[1] = m[1];
293 temp[2] = m[2];
294 temp[3] = 0.f;
295
296 temp[4] = m[3];
297 temp[5] = m[4];
298 temp[6] = m[5];
299 temp[7] = 0.f;
300
301 temp[8] = m[6];
302 temp[9] = m[7];
303 temp[10] = m[8];
304 temp[11] = 0.f;
305
306 temp[12] = 0.f;
307 temp[13] = 0.f;
308 temp[14] = 0.f;
309 temp[15] = 1.f;
310
311 setColorMatrix4(temp);
Tim Murray89daad62013-07-29 14:30:02 -0700312}
313
314
315void ScriptIntrinsicColorMatrix::setColorMatrix4(float* m) {
316 Script::setVar(0, (void*)m, sizeof(float) * 16);
317}
318
319
320void ScriptIntrinsicColorMatrix::setGreyscale() {
Tim Murrayaae73c92013-09-03 17:05:46 -0700321 float matrix[] = {0.299f, 0.299f, 0.299f,0.587f,0.587f,0.587f,0.114f,0.114f, 0.114f};
Tim Murray89daad62013-07-29 14:30:02 -0700322 setColorMatrix3(matrix);
323}
324
325
326void ScriptIntrinsicColorMatrix::setRGBtoYUV() {
Tim Murrayaae73c92013-09-03 17:05:46 -0700327 float matrix[] = { 0.299f, -0.14713f, 0.615f, 0.587f, -0.28886f, -0.51499f, 0.114f, 0.436f, -0.10001f};
Tim Murray89daad62013-07-29 14:30:02 -0700328 setColorMatrix3(matrix);
329}
330
331
332void ScriptIntrinsicColorMatrix::setYUVtoRGB() {
Tim Murrayaae73c92013-09-03 17:05:46 -0700333 float matrix[] = {1.f, 1.f, 1.f, 0.f, -0.39465f, 2.03211f, 1.13983f, -0.5806f, 0.f};
Tim Murray89daad62013-07-29 14:30:02 -0700334 setColorMatrix3(matrix);
335}
336
Tim Murray21fa7a02013-08-15 16:25:03 -0700337
338
339sp<ScriptIntrinsicConvolve3x3> ScriptIntrinsicConvolve3x3::create(sp<RS> rs, sp<const Element> e) {
Tim Murray10913a52013-08-20 17:19:47 -0700340 if (!(e->isCompatible(Element::U8(rs))) &&
341 !(e->isCompatible(Element::U8_2(rs))) &&
342 !(e->isCompatible(Element::U8_3(rs))) &&
343 !(e->isCompatible(Element::U8_4(rs))) &&
344 !(e->isCompatible(Element::F32(rs))) &&
345 !(e->isCompatible(Element::F32_2(rs))) &&
346 !(e->isCompatible(Element::F32_3(rs))) &&
347 !(e->isCompatible(Element::F32_4(rs)))) {
348 rs->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for Convolve3x3");
349 return NULL;
350 }
351
Tim Murray21fa7a02013-08-15 16:25:03 -0700352 return new ScriptIntrinsicConvolve3x3(rs, e);
353}
354
Tim Murray89daad62013-07-29 14:30:02 -0700355ScriptIntrinsicConvolve3x3::ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e)
356 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3, e) {
357
358}
359
360void ScriptIntrinsicConvolve3x3::setInput(sp<Allocation> in) {
Tim Murray10913a52013-08-20 17:19:47 -0700361 if (!(in->getType()->getElement()->isCompatible(mElement))) {
362 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Element mismatch in Convolve3x3");
363 return;
364 }
Tim Murray89daad62013-07-29 14:30:02 -0700365 Script::setVar(1, in);
366}
367
368void ScriptIntrinsicConvolve3x3::forEach(sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700369 if (!(out->getType()->getElement()->isCompatible(mElement))) {
370 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Element mismatch in Convolve3x3");
371 return;
372 }
Tim Murray89daad62013-07-29 14:30:02 -0700373 Script::forEach(0, NULL, out, NULL, 0);
374}
375
376void ScriptIntrinsicConvolve3x3::setCoefficients(float* v) {
377 Script::setVar(0, (void*)v, sizeof(float) * 9);
378}
379
Tim Murray21fa7a02013-08-15 16:25:03 -0700380sp<ScriptIntrinsicConvolve5x5> ScriptIntrinsicConvolve5x5::create(sp<RS> rs, sp<const Element> e) {
Tim Murray10913a52013-08-20 17:19:47 -0700381 if (!(e->isCompatible(Element::U8(rs))) &&
382 !(e->isCompatible(Element::U8_2(rs))) &&
383 !(e->isCompatible(Element::U8_3(rs))) &&
384 !(e->isCompatible(Element::U8_4(rs))) &&
385 !(e->isCompatible(Element::F32(rs))) &&
386 !(e->isCompatible(Element::F32_2(rs))) &&
387 !(e->isCompatible(Element::F32_3(rs))) &&
388 !(e->isCompatible(Element::F32_4(rs)))) {
389 rs->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for Convolve5x5");
390 return NULL;
391 }
392
Tim Murray21fa7a02013-08-15 16:25:03 -0700393 return new ScriptIntrinsicConvolve5x5(rs, e);
394}
395
Tim Murray89daad62013-07-29 14:30:02 -0700396ScriptIntrinsicConvolve5x5::ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e)
397 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5, e) {
398
399}
400
401void ScriptIntrinsicConvolve5x5::setInput(sp<Allocation> in) {
Tim Murray10913a52013-08-20 17:19:47 -0700402 if (!(in->getType()->getElement()->isCompatible(mElement))) {
403 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Element mismatch in Convolve5x5 input");
404 return;
405 }
Tim Murray89daad62013-07-29 14:30:02 -0700406 Script::setVar(1, in);
407}
408
409void ScriptIntrinsicConvolve5x5::forEach(sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700410 if (!(out->getType()->getElement()->isCompatible(mElement))) {
411 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Element mismatch in Convolve5x5 output");
412 return;
413 }
414
Tim Murray89daad62013-07-29 14:30:02 -0700415 Script::forEach(0, NULL, out, NULL, 0);
416}
417
418void ScriptIntrinsicConvolve5x5::setCoefficients(float* v) {
419 Script::setVar(0, (void*)v, sizeof(float) * 25);
420}
421
Tim Murray10913a52013-08-20 17:19:47 -0700422sp<ScriptIntrinsicHistogram> ScriptIntrinsicHistogram::create(sp<RS> rs) {
423 return new ScriptIntrinsicHistogram(rs, NULL);
Tim Murray21fa7a02013-08-15 16:25:03 -0700424}
425
Tim Murrayb27b1812013-08-05 14:00:40 -0700426ScriptIntrinsicHistogram::ScriptIntrinsicHistogram(sp<RS> rs, sp<const Element> e)
427 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_HISTOGRAM, e) {
Tim Murray89daad62013-07-29 14:30:02 -0700428
429}
430
Tim Murray10913a52013-08-20 17:19:47 -0700431void ScriptIntrinsicHistogram::setOutput(sp<Allocation> out) {
432 if (!(out->getType()->getElement()->isCompatible(Element::U32(mRS))) &&
433 !(out->getType()->getElement()->isCompatible(Element::U32_2(mRS))) &&
434 !(out->getType()->getElement()->isCompatible(Element::U32_3(mRS))) &&
435 !(out->getType()->getElement()->isCompatible(Element::U32_4(mRS))) &&
436 !(out->getType()->getElement()->isCompatible(Element::I32(mRS))) &&
437 !(out->getType()->getElement()->isCompatible(Element::I32_2(mRS))) &&
438 !(out->getType()->getElement()->isCompatible(Element::I32_3(mRS))) &&
439 !(out->getType()->getElement()->isCompatible(Element::I32_4(mRS)))) {
440 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for Histogram output");
441 return;
442 }
443
444 if (out->getType()->getX() != 256 ||
445 out->getType()->getY() != 0 ||
446 out->getType()->hasMipmaps()) {
447 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Invalid Allocation type for Histogram output");
448 return;
449 }
450 mOut = out;
451 Script::setVar(1, out);
Tim Murrayb27b1812013-08-05 14:00:40 -0700452}
453
454void ScriptIntrinsicHistogram::setDotCoefficients(float r, float g, float b, float a) {
455 if ((r < 0.f) || (g < 0.f) || (b < 0.f) || (a < 0.f)) {
456 return;
457 }
458 if ((r + g + b + a) > 1.f) {
459 return;
460 }
461
462 FieldPacker fp(16);
463 fp.add(r);
464 fp.add(g);
465 fp.add(b);
466 fp.add(a);
467 Script::setVar(0, fp.getData(), fp.getLength());
468
469}
470
471void ScriptIntrinsicHistogram::forEach(sp<Allocation> ain) {
Tim Murray10913a52013-08-20 17:19:47 -0700472 if (ain->getType()->getElement()->getVectorSize() <
473 mOut->getType()->getElement()->getVectorSize()) {
474 mRS->throwError(RS_ERROR_INVALID_PARAMETER,
475 "Input vector size must be >= output vector size");
476 return;
477 }
478
479 if (!(ain->getType()->getElement()->isCompatible(Element::U8(mRS))) ||
480 !(ain->getType()->getElement()->isCompatible(Element::U8_4(mRS)))) {
481 mRS->throwError(RS_ERROR_INVALID_ELEMENT,
482 "Input allocation to Histogram must be U8 or U8_4");
483 return;
484 }
485
Tim Murrayb27b1812013-08-05 14:00:40 -0700486 Script::forEach(0, ain, NULL, NULL, 0);
487}
488
489
490void ScriptIntrinsicHistogram::forEach_dot(sp<Allocation> ain) {
Tim Murray10913a52013-08-20 17:19:47 -0700491 if (mOut->getType()->getElement()->getVectorSize() != 1) {
492 mRS->throwError(RS_ERROR_INVALID_PARAMETER,
493 "Output Histogram allocation must have vector size of 1 " \
494 "when used with forEach_dot");
495 return;
496 }
497 if (!(ain->getType()->getElement()->isCompatible(Element::U8(mRS))) ||
498 !(ain->getType()->getElement()->isCompatible(Element::U8_4(mRS)))) {
499 mRS->throwError(RS_ERROR_INVALID_ELEMENT,
500 "Input allocation to Histogram must be U8 or U8_4");
501 return;
502 }
503
Tim Murrayb27b1812013-08-05 14:00:40 -0700504 Script::forEach(1, ain, NULL, NULL, 0);
505}
506
Tim Murray21fa7a02013-08-15 16:25:03 -0700507sp<ScriptIntrinsicLUT> ScriptIntrinsicLUT::create(sp<RS> rs, sp<const Element> e) {
Tim Murray10913a52013-08-20 17:19:47 -0700508 if (!(e->isCompatible(Element::U8_4(rs)))) {
509 rs->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for LUT");
510 return NULL;
511 }
Tim Murray21fa7a02013-08-15 16:25:03 -0700512 return new ScriptIntrinsicLUT(rs, e);
513}
514
Tim Murrayb27b1812013-08-05 14:00:40 -0700515ScriptIntrinsicLUT::ScriptIntrinsicLUT(sp<RS> rs, sp<const Element> e)
516 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_LUT, e), mDirty(true) {
Tim Murray2acce992013-08-28 14:23:31 -0700517 LUT = Allocation::createSized(rs, Element::U8(rs), 1024);
Tim Murrayb27b1812013-08-05 14:00:40 -0700518 for (int i = 0; i < 256; i++) {
519 mCache[i] = i;
520 mCache[i+256] = i;
521 mCache[i+512] = i;
522 mCache[i+768] = i;
523 }
Tim Murray2acce992013-08-28 14:23:31 -0700524 setVar(0, LUT);
Tim Murrayb27b1812013-08-05 14:00:40 -0700525}
526
Tim Murray89daad62013-07-29 14:30:02 -0700527void ScriptIntrinsicLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) {
Tim Murrayb27b1812013-08-05 14:00:40 -0700528 if (mDirty) {
529 LUT->copy1DFrom((void*)mCache);
530 mDirty = false;
531 }
Tim Murray10913a52013-08-20 17:19:47 -0700532 if (!(ain->getType()->getElement()->isCompatible(Element::U8_4(mRS))) ||
533 !(aout->getType()->getElement()->isCompatible(Element::U8_4(mRS)))) {
534 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for LUT");
535 return;
536 }
Tim Murrayb27b1812013-08-05 14:00:40 -0700537 Script::forEach(0, ain, aout, NULL, 0);
Tim Murray89daad62013-07-29 14:30:02 -0700538
539}
540
Tim Murray2acce992013-08-28 14:23:31 -0700541void ScriptIntrinsicLUT::setTable(unsigned int offset, unsigned char base, unsigned int length, unsigned char* lutValues) {
542 if ((base + length) > 256 || length == 0) {
543 mRS->throwError(RS_ERROR_INVALID_PARAMETER, "LUT out of range");
Tim Murrayb27b1812013-08-05 14:00:40 -0700544 return;
545 }
546 mDirty = true;
Tim Murray2acce992013-08-28 14:23:31 -0700547 for (unsigned int i = 0; i < length; i++) {
Tim Murrayb27b1812013-08-05 14:00:40 -0700548 mCache[offset + base + i] = lutValues[i];
549 }
550}
Tim Murray89daad62013-07-29 14:30:02 -0700551
Tim Murray2acce992013-08-28 14:23:31 -0700552void ScriptIntrinsicLUT::setRed(unsigned char base, unsigned int length, unsigned char* lutValues) {
Tim Murrayb27b1812013-08-05 14:00:40 -0700553 setTable(0, base, length, lutValues);
554}
Tim Murray89daad62013-07-29 14:30:02 -0700555
Tim Murray2acce992013-08-28 14:23:31 -0700556void ScriptIntrinsicLUT::setGreen(unsigned char base, unsigned int length, unsigned char* lutValues) {
Tim Murrayb27b1812013-08-05 14:00:40 -0700557 setTable(256, base, length, lutValues);
558}
559
Tim Murray2acce992013-08-28 14:23:31 -0700560void ScriptIntrinsicLUT::setBlue(unsigned char base, unsigned int length, unsigned char* lutValues) {
Tim Murrayb27b1812013-08-05 14:00:40 -0700561 setTable(512, base, length, lutValues);
562}
563
Tim Murray2acce992013-08-28 14:23:31 -0700564void ScriptIntrinsicLUT::setAlpha(unsigned char base, unsigned int length, unsigned char* lutValues) {
Tim Murrayb27b1812013-08-05 14:00:40 -0700565 setTable(768, base, length, lutValues);
566}
567
568ScriptIntrinsicLUT::~ScriptIntrinsicLUT() {
569
570}
571
Tim Murray21fa7a02013-08-15 16:25:03 -0700572sp<ScriptIntrinsicYuvToRGB> ScriptIntrinsicYuvToRGB::create(sp<RS> rs, sp<const Element> e) {
Tim Murray10913a52013-08-20 17:19:47 -0700573 if (!(e->isCompatible(Element::U8_4(rs)))) {
574 rs->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for YuvToRGB");
575 return NULL;
576 }
Tim Murray21fa7a02013-08-15 16:25:03 -0700577 return new ScriptIntrinsicYuvToRGB(rs, e);
578}
579
Tim Murrayb27b1812013-08-05 14:00:40 -0700580ScriptIntrinsicYuvToRGB::ScriptIntrinsicYuvToRGB(sp<RS> rs, sp<const Element> e)
581 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB, e) {
582
583}
584
585void ScriptIntrinsicYuvToRGB::setInput(sp<Allocation> in) {
Tim Murrayeb4426d2013-08-27 15:30:16 -0700586 if (!(in->getType()->getElement()->isCompatible(Element::YUV(mRS)))) {
Tim Murray10913a52013-08-20 17:19:47 -0700587 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for input in YuvToRGB");
588 return;
589 }
Tim Murrayb27b1812013-08-05 14:00:40 -0700590 Script::setVar(0, in);
591}
592
593void ScriptIntrinsicYuvToRGB::forEach(sp<Allocation> out) {
Tim Murray10913a52013-08-20 17:19:47 -0700594 if (!(out->getType()->getElement()->isCompatible(mElement))) {
595 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for output in YuvToRGB");
596 return;
597 }
598
Tim Murrayb27b1812013-08-05 14:00:40 -0700599 Script::forEach(0, NULL, out, NULL, 0);
600}
Matthieu Delahaye6fc3e122014-03-04 11:05:49 -0600601
602sp<ScriptIntrinsicVP9LoopFilter> ScriptIntrinsicVP9LoopFilter::create(sp<RS> rs, sp<const Element> e) {
603 if (!(e->isCompatible(Element::U8(rs)))) {
604 rs->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for Vp9LoopFilter");
605 return NULL;
606 }
607 return new ScriptIntrinsicVP9LoopFilter(rs, e);
608}
609
610ScriptIntrinsicVP9LoopFilter::ScriptIntrinsicVP9LoopFilter(sp<RS> rs, sp<const Element> e)
611 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_LOOP_FILTER, e) {
612 sp<const Type> t_pad = Type::create(rs, e, 1, 0, 0);
613 mPadAlloc = Allocation::createTyped(rs, t_pad, RS_ALLOCATION_MIPMAP_NONE, RS_ALLOCATION_USAGE_SCRIPT, NULL);
614}
615
616void ScriptIntrinsicVP9LoopFilter::setLoopFilterDomain(int start, int stop, int numPlanes, int miRows, int miCols) {
617 FieldPacker fp(20);
618 fp.add(start);
619 fp.add(stop);
620 fp.add(numPlanes);
621 fp.add(miRows);
622 fp.add(miCols);
623 Script::setVar(0, fp.getData(), fp.getLength());
624}
625
626void ScriptIntrinsicVP9LoopFilter::setBufferInfo(const BufferInfo *bufInfo) {
627 Script::setVar(1, bufInfo, sizeof(BufferInfo));
628}
629
630void ScriptIntrinsicVP9LoopFilter::setLoopFilterInfo(sp<Allocation> lfInfo) {
631 Script::setVar(2, lfInfo);
632}
633
634void ScriptIntrinsicVP9LoopFilter::setLoopFilterMasks(sp<Allocation> lfMasks) {
635 Script::setVar(3, lfMasks);
636}
637
638void ScriptIntrinsicVP9LoopFilter::forEach(sp<Allocation> frameBuffer) {
639 if (!(frameBuffer->getType()->getElement()->isCompatible(mElement))) {
640 mRS->throwError(RS_ERROR_INVALID_ELEMENT, "Invalid element for input/output in Vp9LoopFilter");
641 return;
642 }
643 Script::setVar(4, frameBuffer);
644 Script::forEach(0, mPadAlloc, NULL, NULL, 0);
645}