Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 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 | |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 17 | #include <malloc.h> |
| 18 | |
| 19 | #include "RenderScript.h" |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 20 | |
| 21 | using namespace android; |
Tim Murray | 9eb7f4b | 2012-11-16 14:02:18 -0800 | [diff] [blame] | 22 | using namespace RSC; |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 23 | |
Tim Murray | 3cd44af | 2012-11-14 11:25:27 -0800 | [diff] [blame] | 24 | ScriptIntrinsic::ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e) |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 25 | : Script(NULL, rs) { |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 26 | mID = RS::dispatch->ScriptIntrinsicCreate(rs->getContext(), id, e->getID()); |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 27 | } |
| 28 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame^] | 29 | ScriptIntrinsic::~ScriptIntrinsic() { |
| 30 | |
| 31 | } |
| 32 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 33 | ScriptIntrinsic3DLUT::ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e) |
| 34 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_3DLUT, e) { |
| 35 | |
| 36 | } |
| 37 | void ScriptIntrinsic3DLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) { |
| 38 | Script::forEach(0, ain, aout, NULL, 0); |
| 39 | } |
| 40 | void ScriptIntrinsic3DLUT::setLUT(sp<Allocation> lut) { |
| 41 | Script::setVar(0, lut); |
| 42 | } |
| 43 | |
Tim Murray | 3cd44af | 2012-11-14 11:25:27 -0800 | [diff] [blame] | 44 | ScriptIntrinsicBlend::ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e) |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 45 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLEND, e) { |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void ScriptIntrinsicBlend::blendClear(sp<Allocation> in, sp<Allocation> out) { |
| 49 | Script::forEach(0, in, out, NULL, 0); |
| 50 | } |
| 51 | |
| 52 | void ScriptIntrinsicBlend::blendSrc(sp<Allocation> in, sp<Allocation> out) { |
| 53 | Script::forEach(1, in, out, NULL, 0); |
| 54 | } |
| 55 | |
| 56 | void ScriptIntrinsicBlend::blendDst(sp<Allocation> in, sp<Allocation> out) { |
| 57 | Script::forEach(2, in, out, NULL, 0); |
| 58 | } |
| 59 | |
| 60 | void ScriptIntrinsicBlend::blendSrcOver(sp<Allocation> in, sp<Allocation> out) { |
| 61 | Script::forEach(3, in, out, NULL, 0); |
| 62 | } |
| 63 | |
| 64 | void ScriptIntrinsicBlend::blendDstOver(sp<Allocation> in, sp<Allocation> out) { |
| 65 | Script::forEach(4, in, out, NULL, 0); |
| 66 | } |
| 67 | |
| 68 | void ScriptIntrinsicBlend::blendSrcIn(sp<Allocation> in, sp<Allocation> out) { |
| 69 | Script::forEach(5, in, out, NULL, 0); |
| 70 | } |
| 71 | |
| 72 | void ScriptIntrinsicBlend::blendDstIn(sp<Allocation> in, sp<Allocation> out) { |
| 73 | Script::forEach(6, in, out, NULL, 0); |
| 74 | } |
| 75 | |
| 76 | void ScriptIntrinsicBlend::blendSrcOut(sp<Allocation> in, sp<Allocation> out) { |
| 77 | Script::forEach(7, in, out, NULL, 0); |
| 78 | } |
| 79 | |
| 80 | void ScriptIntrinsicBlend::blendDstOut(sp<Allocation> in, sp<Allocation> out) { |
| 81 | Script::forEach(8, in, out, NULL, 0); |
| 82 | } |
| 83 | |
| 84 | void ScriptIntrinsicBlend::blendSrcAtop(sp<Allocation> in, sp<Allocation> out) { |
| 85 | Script::forEach(9, in, out, NULL, 0); |
| 86 | } |
| 87 | |
| 88 | void ScriptIntrinsicBlend::blendDstAtop(sp<Allocation> in, sp<Allocation> out) { |
| 89 | Script::forEach(10, in, out, NULL, 0); |
| 90 | } |
| 91 | |
| 92 | void ScriptIntrinsicBlend::blendXor(sp<Allocation> in, sp<Allocation> out) { |
| 93 | Script::forEach(11, in, out, NULL, 0); |
| 94 | } |
| 95 | |
| 96 | // Numbering jumps here |
| 97 | void ScriptIntrinsicBlend::blendMultiply(sp<Allocation> in, sp<Allocation> out) { |
| 98 | Script::forEach(14, in, out, NULL, 0); |
| 99 | } |
| 100 | |
| 101 | // Numbering jumps here |
| 102 | void ScriptIntrinsicBlend::blendAdd(sp<Allocation> in, sp<Allocation> out) { |
| 103 | Script::forEach(34, in, out, NULL, 0); |
| 104 | } |
| 105 | |
| 106 | void ScriptIntrinsicBlend::blendSubtract(sp<Allocation> in, sp<Allocation> out) { |
| 107 | Script::forEach(35, in, out, NULL, 0); |
| 108 | } |
| 109 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 110 | |
| 111 | |
| 112 | |
Tim Murray | 3cd44af | 2012-11-14 11:25:27 -0800 | [diff] [blame] | 113 | ScriptIntrinsicBlur::ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e) |
Tim Murray | 8f1e60c | 2012-11-13 12:25:11 -0800 | [diff] [blame] | 114 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLUR, e) { |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 115 | |
Tim Murray | 8f1e60c | 2012-11-13 12:25:11 -0800 | [diff] [blame] | 116 | } |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 117 | |
Tim Murray | 8f1e60c | 2012-11-13 12:25:11 -0800 | [diff] [blame] | 118 | void ScriptIntrinsicBlur::blur(sp<Allocation> in, sp<Allocation> out) { |
| 119 | Script::setVar(1, in); |
| 120 | Script::forEach(0, NULL, out, NULL, 0); |
| 121 | } |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 122 | |
Tim Murray | 8f1e60c | 2012-11-13 12:25:11 -0800 | [diff] [blame] | 123 | void ScriptIntrinsicBlur::setRadius(float radius) { |
| 124 | Script::setVar(0, &radius, sizeof(float)); |
| 125 | } |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 126 | |
| 127 | |
| 128 | |
| 129 | ScriptIntrinsicColorMatrix::ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e) |
| 130 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX, e) { |
| 131 | |
| 132 | } |
| 133 | |
| 134 | void ScriptIntrinsicColorMatrix::forEach(sp<Allocation> in, sp<Allocation> out) { |
| 135 | Script::forEach(0, in, out, NULL, 0); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void ScriptIntrinsicColorMatrix::setColorMatrix3(float* m) { |
| 140 | Script::setVar(0, (void*)m, sizeof(float) * 9); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | void ScriptIntrinsicColorMatrix::setColorMatrix4(float* m) { |
| 145 | Script::setVar(0, (void*)m, sizeof(float) * 16); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void 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 | |
| 155 | void 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 | |
| 161 | void 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 | |
| 166 | ScriptIntrinsicConvolve3x3::ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e) |
| 167 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3, e) { |
| 168 | |
| 169 | } |
| 170 | |
| 171 | void ScriptIntrinsicConvolve3x3::setInput(sp<Allocation> in) { |
| 172 | Script::setVar(1, in); |
| 173 | } |
| 174 | |
| 175 | void ScriptIntrinsicConvolve3x3::forEach(sp<Allocation> out) { |
| 176 | Script::forEach(0, NULL, out, NULL, 0); |
| 177 | } |
| 178 | |
| 179 | void ScriptIntrinsicConvolve3x3::setCoefficients(float* v) { |
| 180 | Script::setVar(0, (void*)v, sizeof(float) * 9); |
| 181 | } |
| 182 | |
| 183 | ScriptIntrinsicConvolve5x5::ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e) |
| 184 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5, e) { |
| 185 | |
| 186 | } |
| 187 | |
| 188 | void ScriptIntrinsicConvolve5x5::setInput(sp<Allocation> in) { |
| 189 | Script::setVar(1, in); |
| 190 | } |
| 191 | |
| 192 | void ScriptIntrinsicConvolve5x5::forEach(sp<Allocation> out) { |
| 193 | Script::forEach(0, NULL, out, NULL, 0); |
| 194 | } |
| 195 | |
| 196 | void ScriptIntrinsicConvolve5x5::setCoefficients(float* v) { |
| 197 | Script::setVar(0, (void*)v, sizeof(float) * 25); |
| 198 | } |
| 199 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame^] | 200 | ScriptIntrinsicHistogram::ScriptIntrinsicHistogram(sp<RS> rs, sp<const Element> e) |
| 201 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_HISTOGRAM, e) { |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 202 | |
| 203 | } |
| 204 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame^] | 205 | void ScriptIntrinsicHistogram::setOutput(sp<Allocation> aout) { |
| 206 | Script::setVar(1, aout); |
| 207 | } |
| 208 | |
| 209 | void 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 | |
| 226 | void ScriptIntrinsicHistogram::forEach(sp<Allocation> ain) { |
| 227 | Script::forEach(0, ain, NULL, NULL, 0); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | void ScriptIntrinsicHistogram::forEach_dot(sp<Allocation> ain) { |
| 232 | Script::forEach(1, ain, NULL, NULL, 0); |
| 233 | } |
| 234 | |
| 235 | ScriptIntrinsicLUT::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 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 246 | void ScriptIntrinsicLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) { |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame^] | 247 | if (mDirty) { |
| 248 | LUT->copy1DFrom((void*)mCache); |
| 249 | mDirty = false; |
| 250 | } |
| 251 | Script::forEach(0, ain, aout, NULL, 0); |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 252 | |
| 253 | } |
| 254 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame^] | 255 | void 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 | } |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 264 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame^] | 265 | void ScriptIntrinsicLUT::setRed(unsigned char base, unsigned char length, unsigned char* lutValues) { |
| 266 | setTable(0, base, length, lutValues); |
| 267 | } |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 268 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame^] | 269 | void ScriptIntrinsicLUT::setGreen(unsigned char base, unsigned char length, unsigned char* lutValues) { |
| 270 | setTable(256, base, length, lutValues); |
| 271 | } |
| 272 | |
| 273 | void ScriptIntrinsicLUT::setBlue(unsigned char base, unsigned char length, unsigned char* lutValues) { |
| 274 | setTable(512, base, length, lutValues); |
| 275 | } |
| 276 | |
| 277 | void ScriptIntrinsicLUT::setAlpha(unsigned char base, unsigned char length, unsigned char* lutValues) { |
| 278 | setTable(768, base, length, lutValues); |
| 279 | } |
| 280 | |
| 281 | ScriptIntrinsicLUT::~ScriptIntrinsicLUT() { |
| 282 | |
| 283 | } |
| 284 | |
| 285 | ScriptIntrinsicYuvToRGB::ScriptIntrinsicYuvToRGB(sp<RS> rs, sp<const Element> e) |
| 286 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB, e) { |
| 287 | |
| 288 | } |
| 289 | |
| 290 | void ScriptIntrinsicYuvToRGB::setInput(sp<Allocation> in) { |
| 291 | Script::setVar(0, in); |
| 292 | } |
| 293 | |
| 294 | void ScriptIntrinsicYuvToRGB::forEach(sp<Allocation> out) { |
| 295 | Script::forEach(0, NULL, out, NULL, 0); |
| 296 | } |