blob: ab24952ae3ab3b17d2066cc8cf1aae71fb6ef696 [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 Murray7f0d5682012-11-08 16:35:24 -080020
21using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080022using namespace RSC;
Tim Murray7f0d5682012-11-08 16:35:24 -080023
Tim Murray3cd44af2012-11-14 11:25:27 -080024ScriptIntrinsic::ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e)
Tim Murray7f0d5682012-11-08 16:35:24 -080025 : Script(NULL, rs) {
Tim Murraya4230962013-07-17 16:50:10 -070026 mID = RS::dispatch->ScriptIntrinsicCreate(rs->getContext(), id, e->getID());
Tim Murray7f0d5682012-11-08 16:35:24 -080027}
28
Tim Murrayb27b1812013-08-05 14:00:40 -070029ScriptIntrinsic::~ScriptIntrinsic() {
30
31}
32
Tim Murray21fa7a02013-08-15 16:25:03 -070033sp<ScriptIntrinsic3DLUT> ScriptIntrinsic3DLUT::create(sp<RS> rs, sp<const Element> e) {
34 return new ScriptIntrinsic3DLUT(rs, e);
35}
36
Tim Murray89daad62013-07-29 14:30:02 -070037ScriptIntrinsic3DLUT::ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e)
38 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_3DLUT, e) {
39
40}
41void ScriptIntrinsic3DLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) {
42 Script::forEach(0, ain, aout, NULL, 0);
43}
44void ScriptIntrinsic3DLUT::setLUT(sp<Allocation> lut) {
45 Script::setVar(0, lut);
46}
47
Tim Murray21fa7a02013-08-15 16:25:03 -070048sp<ScriptIntrinsicBlend> ScriptIntrinsicBlend::create(sp<RS> rs, sp<const Element> e) {
49 return new ScriptIntrinsicBlend(rs, e);
50}
51
Tim Murray3cd44af2012-11-14 11:25:27 -080052ScriptIntrinsicBlend::ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e)
Tim Murray7f0d5682012-11-08 16:35:24 -080053 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLEND, e) {
Tim Murray7f0d5682012-11-08 16:35:24 -080054}
55
56void ScriptIntrinsicBlend::blendClear(sp<Allocation> in, sp<Allocation> out) {
57 Script::forEach(0, in, out, NULL, 0);
58}
59
60void ScriptIntrinsicBlend::blendSrc(sp<Allocation> in, sp<Allocation> out) {
61 Script::forEach(1, in, out, NULL, 0);
62}
63
64void ScriptIntrinsicBlend::blendDst(sp<Allocation> in, sp<Allocation> out) {
65 Script::forEach(2, in, out, NULL, 0);
66}
67
68void ScriptIntrinsicBlend::blendSrcOver(sp<Allocation> in, sp<Allocation> out) {
69 Script::forEach(3, in, out, NULL, 0);
70}
71
72void ScriptIntrinsicBlend::blendDstOver(sp<Allocation> in, sp<Allocation> out) {
73 Script::forEach(4, in, out, NULL, 0);
74}
75
76void ScriptIntrinsicBlend::blendSrcIn(sp<Allocation> in, sp<Allocation> out) {
77 Script::forEach(5, in, out, NULL, 0);
78}
79
80void ScriptIntrinsicBlend::blendDstIn(sp<Allocation> in, sp<Allocation> out) {
81 Script::forEach(6, in, out, NULL, 0);
82}
83
84void ScriptIntrinsicBlend::blendSrcOut(sp<Allocation> in, sp<Allocation> out) {
85 Script::forEach(7, in, out, NULL, 0);
86}
87
88void ScriptIntrinsicBlend::blendDstOut(sp<Allocation> in, sp<Allocation> out) {
89 Script::forEach(8, in, out, NULL, 0);
90}
91
92void ScriptIntrinsicBlend::blendSrcAtop(sp<Allocation> in, sp<Allocation> out) {
93 Script::forEach(9, in, out, NULL, 0);
94}
95
96void ScriptIntrinsicBlend::blendDstAtop(sp<Allocation> in, sp<Allocation> out) {
97 Script::forEach(10, in, out, NULL, 0);
98}
99
100void ScriptIntrinsicBlend::blendXor(sp<Allocation> in, sp<Allocation> out) {
101 Script::forEach(11, in, out, NULL, 0);
102}
103
104// Numbering jumps here
105void ScriptIntrinsicBlend::blendMultiply(sp<Allocation> in, sp<Allocation> out) {
106 Script::forEach(14, in, out, NULL, 0);
107}
108
109// Numbering jumps here
110void ScriptIntrinsicBlend::blendAdd(sp<Allocation> in, sp<Allocation> out) {
111 Script::forEach(34, in, out, NULL, 0);
112}
113
114void ScriptIntrinsicBlend::blendSubtract(sp<Allocation> in, sp<Allocation> out) {
115 Script::forEach(35, in, out, NULL, 0);
116}
117
Tim Murray89daad62013-07-29 14:30:02 -0700118
119
120
Tim Murray21fa7a02013-08-15 16:25:03 -0700121sp<ScriptIntrinsicBlur> ScriptIntrinsicBlur::create(sp<RS> rs, sp<const Element> e) {
122 return new ScriptIntrinsicBlur(rs, e);
123}
124
Tim Murray3cd44af2012-11-14 11:25:27 -0800125ScriptIntrinsicBlur::ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e)
Tim Murray8f1e60c2012-11-13 12:25:11 -0800126 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLUR, e) {
Tim Murray7f0d5682012-11-08 16:35:24 -0800127
Tim Murray8f1e60c2012-11-13 12:25:11 -0800128}
Tim Murray7f0d5682012-11-08 16:35:24 -0800129
Tim Murray21fa7a02013-08-15 16:25:03 -0700130void ScriptIntrinsicBlur::setInput(sp<Allocation> in) {
Tim Murray8f1e60c2012-11-13 12:25:11 -0800131 Script::setVar(1, in);
Tim Murray21fa7a02013-08-15 16:25:03 -0700132}
133
134void ScriptIntrinsicBlur::forEach(sp<Allocation> out) {
Tim Murray8f1e60c2012-11-13 12:25:11 -0800135 Script::forEach(0, NULL, out, NULL, 0);
136}
Tim Murray7f0d5682012-11-08 16:35:24 -0800137
Tim Murray8f1e60c2012-11-13 12:25:11 -0800138void ScriptIntrinsicBlur::setRadius(float radius) {
139 Script::setVar(0, &radius, sizeof(float));
140}
Tim Murray89daad62013-07-29 14:30:02 -0700141
142
143
Tim Murray21fa7a02013-08-15 16:25:03 -0700144sp<ScriptIntrinsicColorMatrix> ScriptIntrinsicColorMatrix::create(sp<RS> rs, sp<const Element> e) {
145 return new ScriptIntrinsicColorMatrix(rs, e);
146}
147
Tim Murray89daad62013-07-29 14:30:02 -0700148ScriptIntrinsicColorMatrix::ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e)
149 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX, e) {
150
151}
152
153void ScriptIntrinsicColorMatrix::forEach(sp<Allocation> in, sp<Allocation> out) {
154 Script::forEach(0, in, out, NULL, 0);
155}
156
157
158void ScriptIntrinsicColorMatrix::setColorMatrix3(float* m) {
159 Script::setVar(0, (void*)m, sizeof(float) * 9);
160}
161
162
163void ScriptIntrinsicColorMatrix::setColorMatrix4(float* m) {
164 Script::setVar(0, (void*)m, sizeof(float) * 16);
165}
166
167
168void ScriptIntrinsicColorMatrix::setGreyscale() {
169 float matrix[] = {0.299f, 0.587f, 0.114f, 0.299f, 0.587f, 0.114f, 0.299f, 0.587f, 0.114f};
170 setColorMatrix3(matrix);
171}
172
173
174void ScriptIntrinsicColorMatrix::setRGBtoYUV() {
175 float matrix[] = {0.299f,0.587f,0.114f,-0.14713f,-0.28886f,0.436f,0.615f,-0.51499f,-0.10001f};
176 setColorMatrix3(matrix);
177}
178
179
180void ScriptIntrinsicColorMatrix::setYUVtoRGB() {
181 float matrix[] = {1.f,0.f,1.13983f,1.f,-0.39465f,-0.5806f,1.f,2.03211f,0.f};
182 setColorMatrix3(matrix);
183}
184
Tim Murray21fa7a02013-08-15 16:25:03 -0700185
186
187sp<ScriptIntrinsicConvolve3x3> ScriptIntrinsicConvolve3x3::create(sp<RS> rs, sp<const Element> e) {
188 return new ScriptIntrinsicConvolve3x3(rs, e);
189}
190
Tim Murray89daad62013-07-29 14:30:02 -0700191ScriptIntrinsicConvolve3x3::ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e)
192 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3, e) {
193
194}
195
196void ScriptIntrinsicConvolve3x3::setInput(sp<Allocation> in) {
197 Script::setVar(1, in);
198}
199
200void ScriptIntrinsicConvolve3x3::forEach(sp<Allocation> out) {
201 Script::forEach(0, NULL, out, NULL, 0);
202}
203
204void ScriptIntrinsicConvolve3x3::setCoefficients(float* v) {
205 Script::setVar(0, (void*)v, sizeof(float) * 9);
206}
207
Tim Murray21fa7a02013-08-15 16:25:03 -0700208sp<ScriptIntrinsicConvolve5x5> ScriptIntrinsicConvolve5x5::create(sp<RS> rs, sp<const Element> e) {
209 return new ScriptIntrinsicConvolve5x5(rs, e);
210}
211
Tim Murray89daad62013-07-29 14:30:02 -0700212ScriptIntrinsicConvolve5x5::ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e)
213 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5, e) {
214
215}
216
217void ScriptIntrinsicConvolve5x5::setInput(sp<Allocation> in) {
218 Script::setVar(1, in);
219}
220
221void ScriptIntrinsicConvolve5x5::forEach(sp<Allocation> out) {
222 Script::forEach(0, NULL, out, NULL, 0);
223}
224
225void ScriptIntrinsicConvolve5x5::setCoefficients(float* v) {
226 Script::setVar(0, (void*)v, sizeof(float) * 25);
227}
228
Tim Murray21fa7a02013-08-15 16:25:03 -0700229sp<ScriptIntrinsicHistogram> ScriptIntrinsicHistogram::create(sp<RS> rs, sp<const Element> e) {
230 return new ScriptIntrinsicHistogram(rs, e);
231}
232
Tim Murrayb27b1812013-08-05 14:00:40 -0700233ScriptIntrinsicHistogram::ScriptIntrinsicHistogram(sp<RS> rs, sp<const Element> e)
234 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_HISTOGRAM, e) {
Tim Murray89daad62013-07-29 14:30:02 -0700235
236}
237
Tim Murrayb27b1812013-08-05 14:00:40 -0700238void ScriptIntrinsicHistogram::setOutput(sp<Allocation> aout) {
239 Script::setVar(1, aout);
240}
241
242void ScriptIntrinsicHistogram::setDotCoefficients(float r, float g, float b, float a) {
243 if ((r < 0.f) || (g < 0.f) || (b < 0.f) || (a < 0.f)) {
244 return;
245 }
246 if ((r + g + b + a) > 1.f) {
247 return;
248 }
249
250 FieldPacker fp(16);
251 fp.add(r);
252 fp.add(g);
253 fp.add(b);
254 fp.add(a);
255 Script::setVar(0, fp.getData(), fp.getLength());
256
257}
258
259void ScriptIntrinsicHistogram::forEach(sp<Allocation> ain) {
260 Script::forEach(0, ain, NULL, NULL, 0);
261}
262
263
264void ScriptIntrinsicHistogram::forEach_dot(sp<Allocation> ain) {
265 Script::forEach(1, ain, NULL, NULL, 0);
266}
267
Tim Murray21fa7a02013-08-15 16:25:03 -0700268sp<ScriptIntrinsicLUT> ScriptIntrinsicLUT::create(sp<RS> rs, sp<const Element> e) {
269 return new ScriptIntrinsicLUT(rs, e);
270}
271
Tim Murrayb27b1812013-08-05 14:00:40 -0700272ScriptIntrinsicLUT::ScriptIntrinsicLUT(sp<RS> rs, sp<const Element> e)
273 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_LUT, e), mDirty(true) {
274 LUT = Allocation::createSized(rs, e, 1024);
275 for (int i = 0; i < 256; i++) {
276 mCache[i] = i;
277 mCache[i+256] = i;
278 mCache[i+512] = i;
279 mCache[i+768] = i;
280 }
281}
282
Tim Murray89daad62013-07-29 14:30:02 -0700283void ScriptIntrinsicLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) {
Tim Murrayb27b1812013-08-05 14:00:40 -0700284 if (mDirty) {
285 LUT->copy1DFrom((void*)mCache);
286 mDirty = false;
287 }
288 Script::forEach(0, ain, aout, NULL, 0);
Tim Murray89daad62013-07-29 14:30:02 -0700289
290}
291
Tim Murrayb27b1812013-08-05 14:00:40 -0700292void ScriptIntrinsicLUT::setTable(unsigned int offset, unsigned char base, unsigned char length, unsigned char* lutValues) {
293 if ((base + length) >= 256 || length == 0) {
294 return;
295 }
296 mDirty = true;
297 for (int i = 0; i < length; i++) {
298 mCache[offset + base + i] = lutValues[i];
299 }
300}
Tim Murray89daad62013-07-29 14:30:02 -0700301
Tim Murrayb27b1812013-08-05 14:00:40 -0700302void ScriptIntrinsicLUT::setRed(unsigned char base, unsigned char length, unsigned char* lutValues) {
303 setTable(0, base, length, lutValues);
304}
Tim Murray89daad62013-07-29 14:30:02 -0700305
Tim Murrayb27b1812013-08-05 14:00:40 -0700306void ScriptIntrinsicLUT::setGreen(unsigned char base, unsigned char length, unsigned char* lutValues) {
307 setTable(256, base, length, lutValues);
308}
309
310void ScriptIntrinsicLUT::setBlue(unsigned char base, unsigned char length, unsigned char* lutValues) {
311 setTable(512, base, length, lutValues);
312}
313
314void ScriptIntrinsicLUT::setAlpha(unsigned char base, unsigned char length, unsigned char* lutValues) {
315 setTable(768, base, length, lutValues);
316}
317
318ScriptIntrinsicLUT::~ScriptIntrinsicLUT() {
319
320}
321
Tim Murray21fa7a02013-08-15 16:25:03 -0700322sp<ScriptIntrinsicYuvToRGB> ScriptIntrinsicYuvToRGB::create(sp<RS> rs, sp<const Element> e) {
323 return new ScriptIntrinsicYuvToRGB(rs, e);
324}
325
Tim Murrayb27b1812013-08-05 14:00:40 -0700326ScriptIntrinsicYuvToRGB::ScriptIntrinsicYuvToRGB(sp<RS> rs, sp<const Element> e)
327 : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB, e) {
328
329}
330
331void ScriptIntrinsicYuvToRGB::setInput(sp<Allocation> in) {
332 Script::setVar(0, in);
333}
334
335void ScriptIntrinsicYuvToRGB::forEach(sp<Allocation> out) {
336 Script::forEach(0, NULL, out, NULL, 0);
337}