ScriptIntrinsics.cpp revision b27b18130d0772203799ba0f2d27783b640dc891
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
33ScriptIntrinsic3DLUT::ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e)
34    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_3DLUT, e) {
35
36}
37void ScriptIntrinsic3DLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) {
38    Script::forEach(0, ain, aout, NULL, 0);
39}
40void ScriptIntrinsic3DLUT::setLUT(sp<Allocation> lut) {
41    Script::setVar(0, lut);
42}
43
44ScriptIntrinsicBlend::ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e)
45    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLEND, e) {
46}
47
48void ScriptIntrinsicBlend::blendClear(sp<Allocation> in, sp<Allocation> out) {
49    Script::forEach(0, in, out, NULL, 0);
50}
51
52void ScriptIntrinsicBlend::blendSrc(sp<Allocation> in, sp<Allocation> out) {
53    Script::forEach(1, in, out, NULL, 0);
54}
55
56void ScriptIntrinsicBlend::blendDst(sp<Allocation> in, sp<Allocation> out) {
57    Script::forEach(2, in, out, NULL, 0);
58}
59
60void ScriptIntrinsicBlend::blendSrcOver(sp<Allocation> in, sp<Allocation> out) {
61    Script::forEach(3, in, out, NULL, 0);
62}
63
64void ScriptIntrinsicBlend::blendDstOver(sp<Allocation> in, sp<Allocation> out) {
65    Script::forEach(4, in, out, NULL, 0);
66}
67
68void ScriptIntrinsicBlend::blendSrcIn(sp<Allocation> in, sp<Allocation> out) {
69    Script::forEach(5, in, out, NULL, 0);
70}
71
72void ScriptIntrinsicBlend::blendDstIn(sp<Allocation> in, sp<Allocation> out) {
73    Script::forEach(6, in, out, NULL, 0);
74}
75
76void ScriptIntrinsicBlend::blendSrcOut(sp<Allocation> in, sp<Allocation> out) {
77    Script::forEach(7, in, out, NULL, 0);
78}
79
80void ScriptIntrinsicBlend::blendDstOut(sp<Allocation> in, sp<Allocation> out) {
81    Script::forEach(8, in, out, NULL, 0);
82}
83
84void ScriptIntrinsicBlend::blendSrcAtop(sp<Allocation> in, sp<Allocation> out) {
85    Script::forEach(9, in, out, NULL, 0);
86}
87
88void ScriptIntrinsicBlend::blendDstAtop(sp<Allocation> in, sp<Allocation> out) {
89    Script::forEach(10, in, out, NULL, 0);
90}
91
92void ScriptIntrinsicBlend::blendXor(sp<Allocation> in, sp<Allocation> out) {
93    Script::forEach(11, in, out, NULL, 0);
94}
95
96// Numbering jumps here
97void ScriptIntrinsicBlend::blendMultiply(sp<Allocation> in, sp<Allocation> out) {
98    Script::forEach(14, in, out, NULL, 0);
99}
100
101// Numbering jumps here
102void ScriptIntrinsicBlend::blendAdd(sp<Allocation> in, sp<Allocation> out) {
103    Script::forEach(34, in, out, NULL, 0);
104}
105
106void ScriptIntrinsicBlend::blendSubtract(sp<Allocation> in, sp<Allocation> out) {
107    Script::forEach(35, in, out, NULL, 0);
108}
109
110
111
112
113ScriptIntrinsicBlur::ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e)
114    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLUR, e) {
115
116}
117
118void ScriptIntrinsicBlur::blur(sp<Allocation> in, sp<Allocation> out) {
119    Script::setVar(1, in);
120    Script::forEach(0, NULL, out, NULL, 0);
121}
122
123void ScriptIntrinsicBlur::setRadius(float radius) {
124    Script::setVar(0, &radius, sizeof(float));
125}
126
127
128
129ScriptIntrinsicColorMatrix::ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e)
130    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX, e) {
131
132}
133
134void ScriptIntrinsicColorMatrix::forEach(sp<Allocation> in, sp<Allocation> out) {
135    Script::forEach(0, in, out, NULL, 0);
136}
137
138
139void ScriptIntrinsicColorMatrix::setColorMatrix3(float* m) {
140    Script::setVar(0, (void*)m, sizeof(float) * 9);
141}
142
143
144void ScriptIntrinsicColorMatrix::setColorMatrix4(float* m) {
145    Script::setVar(0, (void*)m, sizeof(float) * 16);
146}
147
148
149void ScriptIntrinsicColorMatrix::setGreyscale() {
150    float matrix[] = {0.299f, 0.587f,  0.114f, 0.299f, 0.587f, 0.114f, 0.299f, 0.587f, 0.114f};
151    setColorMatrix3(matrix);
152}
153
154
155void ScriptIntrinsicColorMatrix::setRGBtoYUV() {
156    float matrix[] = {0.299f,0.587f,0.114f,-0.14713f,-0.28886f,0.436f,0.615f,-0.51499f,-0.10001f};
157    setColorMatrix3(matrix);
158}
159
160
161void ScriptIntrinsicColorMatrix::setYUVtoRGB() {
162    float matrix[] = {1.f,0.f,1.13983f,1.f,-0.39465f,-0.5806f,1.f,2.03211f,0.f};
163    setColorMatrix3(matrix);
164}
165
166ScriptIntrinsicConvolve3x3::ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e)
167    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3, e) {
168
169}
170
171void ScriptIntrinsicConvolve3x3::setInput(sp<Allocation> in) {
172    Script::setVar(1, in);
173}
174
175void ScriptIntrinsicConvolve3x3::forEach(sp<Allocation> out) {
176    Script::forEach(0, NULL, out, NULL, 0);
177}
178
179void ScriptIntrinsicConvolve3x3::setCoefficients(float* v) {
180    Script::setVar(0, (void*)v, sizeof(float) * 9);
181}
182
183ScriptIntrinsicConvolve5x5::ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e)
184    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5, e) {
185
186}
187
188void ScriptIntrinsicConvolve5x5::setInput(sp<Allocation> in) {
189    Script::setVar(1, in);
190}
191
192void ScriptIntrinsicConvolve5x5::forEach(sp<Allocation> out) {
193    Script::forEach(0, NULL, out, NULL, 0);
194}
195
196void ScriptIntrinsicConvolve5x5::setCoefficients(float* v) {
197    Script::setVar(0, (void*)v, sizeof(float) * 25);
198}
199
200ScriptIntrinsicHistogram::ScriptIntrinsicHistogram(sp<RS> rs, sp<const Element> e)
201    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_HISTOGRAM, e) {
202
203}
204
205void ScriptIntrinsicHistogram::setOutput(sp<Allocation> aout) {
206    Script::setVar(1, aout);
207}
208
209void ScriptIntrinsicHistogram::setDotCoefficients(float r, float g, float b, float a) {
210    if ((r < 0.f) || (g < 0.f) || (b < 0.f) || (a < 0.f)) {
211        return;
212    }
213    if ((r + g + b + a) > 1.f) {
214        return;
215    }
216
217    FieldPacker fp(16);
218    fp.add(r);
219    fp.add(g);
220    fp.add(b);
221    fp.add(a);
222    Script::setVar(0, fp.getData(), fp.getLength());
223
224}
225
226void ScriptIntrinsicHistogram::forEach(sp<Allocation> ain) {
227    Script::forEach(0, ain, NULL, NULL, 0);
228}
229
230
231void ScriptIntrinsicHistogram::forEach_dot(sp<Allocation> ain) {
232    Script::forEach(1, ain, NULL, NULL, 0);
233}
234
235ScriptIntrinsicLUT::ScriptIntrinsicLUT(sp<RS> rs, sp<const Element> e)
236    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_LUT, e), mDirty(true) {
237    LUT = Allocation::createSized(rs, e, 1024);
238    for (int i = 0; i < 256; i++) {
239        mCache[i] = i;
240        mCache[i+256] = i;
241        mCache[i+512] = i;
242        mCache[i+768] = i;
243    }
244}
245
246void ScriptIntrinsicLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) {
247    if (mDirty) {
248        LUT->copy1DFrom((void*)mCache);
249        mDirty = false;
250    }
251    Script::forEach(0, ain, aout, NULL, 0);
252
253}
254
255void ScriptIntrinsicLUT::setTable(unsigned int offset, unsigned char base, unsigned char length, unsigned char* lutValues) {
256    if ((base + length) >= 256 || length == 0) {
257        return;
258    }
259    mDirty = true;
260    for (int i = 0; i < length; i++) {
261        mCache[offset + base + i] = lutValues[i];
262    }
263}
264
265void ScriptIntrinsicLUT::setRed(unsigned char base, unsigned char length, unsigned char* lutValues) {
266    setTable(0, base, length, lutValues);
267}
268
269void ScriptIntrinsicLUT::setGreen(unsigned char base, unsigned char length, unsigned char* lutValues) {
270    setTable(256, base, length, lutValues);
271}
272
273void ScriptIntrinsicLUT::setBlue(unsigned char base, unsigned char length, unsigned char* lutValues) {
274    setTable(512, base, length, lutValues);
275}
276
277void ScriptIntrinsicLUT::setAlpha(unsigned char base, unsigned char length, unsigned char* lutValues) {
278    setTable(768, base, length, lutValues);
279}
280
281ScriptIntrinsicLUT::~ScriptIntrinsicLUT() {
282
283}
284
285ScriptIntrinsicYuvToRGB::ScriptIntrinsicYuvToRGB(sp<RS> rs, sp<const Element> e)
286    : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB, e) {
287
288}
289
290void ScriptIntrinsicYuvToRGB::setInput(sp<Allocation> in) {
291    Script::setVar(0, in);
292}
293
294void ScriptIntrinsicYuvToRGB::forEach(sp<Allocation> out) {
295    Script::forEach(0, NULL, out, NULL, 0);
296}
297