ScriptIntrinsics.cpp revision 21fa7a0a23eddab88ff261017f6d7a2548b4d89a
1/*
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
17#include <malloc.h>
18
19#include "RenderScript.h"
20
21using namespace android;
22using namespace RSC;
23
24ScriptIntrinsic::ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e)
25    : Script(NULL, rs) {
26    mID = RS::dispatch->ScriptIntrinsicCreate(rs->getContext(), id, e->getID());
27}
28
29ScriptIntrinsic::~ScriptIntrinsic() {
30
31}
32
33sp<ScriptIntrinsic3DLUT> ScriptIntrinsic3DLUT::create(sp<RS> rs, sp<const Element> e) {
34    return new ScriptIntrinsic3DLUT(rs, e);
35}
36
37ScriptIntrinsic3DLUT::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
48sp<ScriptIntrinsicBlend> ScriptIntrinsicBlend::create(sp<RS> rs, sp<const Element> e) {
49    return new ScriptIntrinsicBlend(rs, e);
50}
51
52ScriptIntrinsicBlend::ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e)
53    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLEND, e) {
54}
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
118
119
120
121sp<ScriptIntrinsicBlur> ScriptIntrinsicBlur::create(sp<RS> rs, sp<const Element> e) {
122    return new ScriptIntrinsicBlur(rs, e);
123}
124
125ScriptIntrinsicBlur::ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e)
126    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLUR, e) {
127
128}
129
130void ScriptIntrinsicBlur::setInput(sp<Allocation> in) {
131    Script::setVar(1, in);
132}
133
134void ScriptIntrinsicBlur::forEach(sp<Allocation> out) {
135    Script::forEach(0, NULL, out, NULL, 0);
136}
137
138void ScriptIntrinsicBlur::setRadius(float radius) {
139    Script::setVar(0, &radius, sizeof(float));
140}
141
142
143
144sp<ScriptIntrinsicColorMatrix> ScriptIntrinsicColorMatrix::create(sp<RS> rs, sp<const Element> e) {
145    return new ScriptIntrinsicColorMatrix(rs, e);
146}
147
148ScriptIntrinsicColorMatrix::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
185
186
187sp<ScriptIntrinsicConvolve3x3> ScriptIntrinsicConvolve3x3::create(sp<RS> rs, sp<const Element> e) {
188    return new ScriptIntrinsicConvolve3x3(rs, e);
189}
190
191ScriptIntrinsicConvolve3x3::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
208sp<ScriptIntrinsicConvolve5x5> ScriptIntrinsicConvolve5x5::create(sp<RS> rs, sp<const Element> e) {
209    return new ScriptIntrinsicConvolve5x5(rs, e);
210}
211
212ScriptIntrinsicConvolve5x5::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
229sp<ScriptIntrinsicHistogram> ScriptIntrinsicHistogram::create(sp<RS> rs, sp<const Element> e) {
230    return new ScriptIntrinsicHistogram(rs, e);
231}
232
233ScriptIntrinsicHistogram::ScriptIntrinsicHistogram(sp<RS> rs, sp<const Element> e)
234    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_HISTOGRAM, e) {
235
236}
237
238void 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
268sp<ScriptIntrinsicLUT> ScriptIntrinsicLUT::create(sp<RS> rs, sp<const Element> e) {
269    return new ScriptIntrinsicLUT(rs, e);
270}
271
272ScriptIntrinsicLUT::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
283void ScriptIntrinsicLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) {
284    if (mDirty) {
285        LUT->copy1DFrom((void*)mCache);
286        mDirty = false;
287    }
288    Script::forEach(0, ain, aout, NULL, 0);
289
290}
291
292void 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}
301
302void ScriptIntrinsicLUT::setRed(unsigned char base, unsigned char length, unsigned char* lutValues) {
303    setTable(0, base, length, lutValues);
304}
305
306void 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
322sp<ScriptIntrinsicYuvToRGB> ScriptIntrinsicYuvToRGB::create(sp<RS> rs, sp<const Element> e) {
323    return new ScriptIntrinsicYuvToRGB(rs, e);
324}
325
326ScriptIntrinsicYuvToRGB::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}
338