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 | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame^] | 33 | sp<ScriptIntrinsic3DLUT> ScriptIntrinsic3DLUT::create(sp<RS> rs, sp<const Element> e) { |
| 34 | return new ScriptIntrinsic3DLUT(rs, e); |
| 35 | } |
| 36 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 37 | ScriptIntrinsic3DLUT::ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e) |
| 38 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_3DLUT, e) { |
| 39 | |
| 40 | } |
| 41 | void ScriptIntrinsic3DLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) { |
| 42 | Script::forEach(0, ain, aout, NULL, 0); |
| 43 | } |
| 44 | void ScriptIntrinsic3DLUT::setLUT(sp<Allocation> lut) { |
| 45 | Script::setVar(0, lut); |
| 46 | } |
| 47 | |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame^] | 48 | sp<ScriptIntrinsicBlend> ScriptIntrinsicBlend::create(sp<RS> rs, sp<const Element> e) { |
| 49 | return new ScriptIntrinsicBlend(rs, e); |
| 50 | } |
| 51 | |
Tim Murray | 3cd44af | 2012-11-14 11:25:27 -0800 | [diff] [blame] | 52 | ScriptIntrinsicBlend::ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e) |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 53 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLEND, e) { |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void ScriptIntrinsicBlend::blendClear(sp<Allocation> in, sp<Allocation> out) { |
| 57 | Script::forEach(0, in, out, NULL, 0); |
| 58 | } |
| 59 | |
| 60 | void ScriptIntrinsicBlend::blendSrc(sp<Allocation> in, sp<Allocation> out) { |
| 61 | Script::forEach(1, in, out, NULL, 0); |
| 62 | } |
| 63 | |
| 64 | void ScriptIntrinsicBlend::blendDst(sp<Allocation> in, sp<Allocation> out) { |
| 65 | Script::forEach(2, in, out, NULL, 0); |
| 66 | } |
| 67 | |
| 68 | void ScriptIntrinsicBlend::blendSrcOver(sp<Allocation> in, sp<Allocation> out) { |
| 69 | Script::forEach(3, in, out, NULL, 0); |
| 70 | } |
| 71 | |
| 72 | void ScriptIntrinsicBlend::blendDstOver(sp<Allocation> in, sp<Allocation> out) { |
| 73 | Script::forEach(4, in, out, NULL, 0); |
| 74 | } |
| 75 | |
| 76 | void ScriptIntrinsicBlend::blendSrcIn(sp<Allocation> in, sp<Allocation> out) { |
| 77 | Script::forEach(5, in, out, NULL, 0); |
| 78 | } |
| 79 | |
| 80 | void ScriptIntrinsicBlend::blendDstIn(sp<Allocation> in, sp<Allocation> out) { |
| 81 | Script::forEach(6, in, out, NULL, 0); |
| 82 | } |
| 83 | |
| 84 | void ScriptIntrinsicBlend::blendSrcOut(sp<Allocation> in, sp<Allocation> out) { |
| 85 | Script::forEach(7, in, out, NULL, 0); |
| 86 | } |
| 87 | |
| 88 | void ScriptIntrinsicBlend::blendDstOut(sp<Allocation> in, sp<Allocation> out) { |
| 89 | Script::forEach(8, in, out, NULL, 0); |
| 90 | } |
| 91 | |
| 92 | void ScriptIntrinsicBlend::blendSrcAtop(sp<Allocation> in, sp<Allocation> out) { |
| 93 | Script::forEach(9, in, out, NULL, 0); |
| 94 | } |
| 95 | |
| 96 | void ScriptIntrinsicBlend::blendDstAtop(sp<Allocation> in, sp<Allocation> out) { |
| 97 | Script::forEach(10, in, out, NULL, 0); |
| 98 | } |
| 99 | |
| 100 | void ScriptIntrinsicBlend::blendXor(sp<Allocation> in, sp<Allocation> out) { |
| 101 | Script::forEach(11, in, out, NULL, 0); |
| 102 | } |
| 103 | |
| 104 | // Numbering jumps here |
| 105 | void ScriptIntrinsicBlend::blendMultiply(sp<Allocation> in, sp<Allocation> out) { |
| 106 | Script::forEach(14, in, out, NULL, 0); |
| 107 | } |
| 108 | |
| 109 | // Numbering jumps here |
| 110 | void ScriptIntrinsicBlend::blendAdd(sp<Allocation> in, sp<Allocation> out) { |
| 111 | Script::forEach(34, in, out, NULL, 0); |
| 112 | } |
| 113 | |
| 114 | void ScriptIntrinsicBlend::blendSubtract(sp<Allocation> in, sp<Allocation> out) { |
| 115 | Script::forEach(35, in, out, NULL, 0); |
| 116 | } |
| 117 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 118 | |
| 119 | |
| 120 | |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame^] | 121 | sp<ScriptIntrinsicBlur> ScriptIntrinsicBlur::create(sp<RS> rs, sp<const Element> e) { |
| 122 | return new ScriptIntrinsicBlur(rs, e); |
| 123 | } |
| 124 | |
Tim Murray | 3cd44af | 2012-11-14 11:25:27 -0800 | [diff] [blame] | 125 | ScriptIntrinsicBlur::ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e) |
Tim Murray | 8f1e60c | 2012-11-13 12:25:11 -0800 | [diff] [blame] | 126 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLUR, e) { |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 127 | |
Tim Murray | 8f1e60c | 2012-11-13 12:25:11 -0800 | [diff] [blame] | 128 | } |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 129 | |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame^] | 130 | void ScriptIntrinsicBlur::setInput(sp<Allocation> in) { |
Tim Murray | 8f1e60c | 2012-11-13 12:25:11 -0800 | [diff] [blame] | 131 | Script::setVar(1, in); |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame^] | 132 | } |
| 133 | |
| 134 | void ScriptIntrinsicBlur::forEach(sp<Allocation> out) { |
Tim Murray | 8f1e60c | 2012-11-13 12:25:11 -0800 | [diff] [blame] | 135 | Script::forEach(0, NULL, out, NULL, 0); |
| 136 | } |
Tim Murray | 7f0d568 | 2012-11-08 16:35:24 -0800 | [diff] [blame] | 137 | |
Tim Murray | 8f1e60c | 2012-11-13 12:25:11 -0800 | [diff] [blame] | 138 | void ScriptIntrinsicBlur::setRadius(float radius) { |
| 139 | Script::setVar(0, &radius, sizeof(float)); |
| 140 | } |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 141 | |
| 142 | |
| 143 | |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame^] | 144 | sp<ScriptIntrinsicColorMatrix> ScriptIntrinsicColorMatrix::create(sp<RS> rs, sp<const Element> e) { |
| 145 | return new ScriptIntrinsicColorMatrix(rs, e); |
| 146 | } |
| 147 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 148 | ScriptIntrinsicColorMatrix::ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e) |
| 149 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX, e) { |
| 150 | |
| 151 | } |
| 152 | |
| 153 | void ScriptIntrinsicColorMatrix::forEach(sp<Allocation> in, sp<Allocation> out) { |
| 154 | Script::forEach(0, in, out, NULL, 0); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | void ScriptIntrinsicColorMatrix::setColorMatrix3(float* m) { |
| 159 | Script::setVar(0, (void*)m, sizeof(float) * 9); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | void ScriptIntrinsicColorMatrix::setColorMatrix4(float* m) { |
| 164 | Script::setVar(0, (void*)m, sizeof(float) * 16); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | void 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 | |
| 174 | void 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 | |
| 180 | void 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 Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame^] | 185 | |
| 186 | |
| 187 | sp<ScriptIntrinsicConvolve3x3> ScriptIntrinsicConvolve3x3::create(sp<RS> rs, sp<const Element> e) { |
| 188 | return new ScriptIntrinsicConvolve3x3(rs, e); |
| 189 | } |
| 190 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 191 | ScriptIntrinsicConvolve3x3::ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e) |
| 192 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3, e) { |
| 193 | |
| 194 | } |
| 195 | |
| 196 | void ScriptIntrinsicConvolve3x3::setInput(sp<Allocation> in) { |
| 197 | Script::setVar(1, in); |
| 198 | } |
| 199 | |
| 200 | void ScriptIntrinsicConvolve3x3::forEach(sp<Allocation> out) { |
| 201 | Script::forEach(0, NULL, out, NULL, 0); |
| 202 | } |
| 203 | |
| 204 | void ScriptIntrinsicConvolve3x3::setCoefficients(float* v) { |
| 205 | Script::setVar(0, (void*)v, sizeof(float) * 9); |
| 206 | } |
| 207 | |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame^] | 208 | sp<ScriptIntrinsicConvolve5x5> ScriptIntrinsicConvolve5x5::create(sp<RS> rs, sp<const Element> e) { |
| 209 | return new ScriptIntrinsicConvolve5x5(rs, e); |
| 210 | } |
| 211 | |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 212 | ScriptIntrinsicConvolve5x5::ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e) |
| 213 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5, e) { |
| 214 | |
| 215 | } |
| 216 | |
| 217 | void ScriptIntrinsicConvolve5x5::setInput(sp<Allocation> in) { |
| 218 | Script::setVar(1, in); |
| 219 | } |
| 220 | |
| 221 | void ScriptIntrinsicConvolve5x5::forEach(sp<Allocation> out) { |
| 222 | Script::forEach(0, NULL, out, NULL, 0); |
| 223 | } |
| 224 | |
| 225 | void ScriptIntrinsicConvolve5x5::setCoefficients(float* v) { |
| 226 | Script::setVar(0, (void*)v, sizeof(float) * 25); |
| 227 | } |
| 228 | |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame^] | 229 | sp<ScriptIntrinsicHistogram> ScriptIntrinsicHistogram::create(sp<RS> rs, sp<const Element> e) { |
| 230 | return new ScriptIntrinsicHistogram(rs, e); |
| 231 | } |
| 232 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame] | 233 | ScriptIntrinsicHistogram::ScriptIntrinsicHistogram(sp<RS> rs, sp<const Element> e) |
| 234 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_HISTOGRAM, e) { |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 235 | |
| 236 | } |
| 237 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame] | 238 | void ScriptIntrinsicHistogram::setOutput(sp<Allocation> aout) { |
| 239 | Script::setVar(1, aout); |
| 240 | } |
| 241 | |
| 242 | void 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 | |
| 259 | void ScriptIntrinsicHistogram::forEach(sp<Allocation> ain) { |
| 260 | Script::forEach(0, ain, NULL, NULL, 0); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | void ScriptIntrinsicHistogram::forEach_dot(sp<Allocation> ain) { |
| 265 | Script::forEach(1, ain, NULL, NULL, 0); |
| 266 | } |
| 267 | |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame^] | 268 | sp<ScriptIntrinsicLUT> ScriptIntrinsicLUT::create(sp<RS> rs, sp<const Element> e) { |
| 269 | return new ScriptIntrinsicLUT(rs, e); |
| 270 | } |
| 271 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame] | 272 | ScriptIntrinsicLUT::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 Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 283 | void ScriptIntrinsicLUT::forEach(sp<Allocation> ain, sp<Allocation> aout) { |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame] | 284 | if (mDirty) { |
| 285 | LUT->copy1DFrom((void*)mCache); |
| 286 | mDirty = false; |
| 287 | } |
| 288 | Script::forEach(0, ain, aout, NULL, 0); |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 289 | |
| 290 | } |
| 291 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame] | 292 | void 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 Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 301 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame] | 302 | void ScriptIntrinsicLUT::setRed(unsigned char base, unsigned char length, unsigned char* lutValues) { |
| 303 | setTable(0, base, length, lutValues); |
| 304 | } |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame] | 305 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame] | 306 | void ScriptIntrinsicLUT::setGreen(unsigned char base, unsigned char length, unsigned char* lutValues) { |
| 307 | setTable(256, base, length, lutValues); |
| 308 | } |
| 309 | |
| 310 | void ScriptIntrinsicLUT::setBlue(unsigned char base, unsigned char length, unsigned char* lutValues) { |
| 311 | setTable(512, base, length, lutValues); |
| 312 | } |
| 313 | |
| 314 | void ScriptIntrinsicLUT::setAlpha(unsigned char base, unsigned char length, unsigned char* lutValues) { |
| 315 | setTable(768, base, length, lutValues); |
| 316 | } |
| 317 | |
| 318 | ScriptIntrinsicLUT::~ScriptIntrinsicLUT() { |
| 319 | |
| 320 | } |
| 321 | |
Tim Murray | 21fa7a0 | 2013-08-15 16:25:03 -0700 | [diff] [blame^] | 322 | sp<ScriptIntrinsicYuvToRGB> ScriptIntrinsicYuvToRGB::create(sp<RS> rs, sp<const Element> e) { |
| 323 | return new ScriptIntrinsicYuvToRGB(rs, e); |
| 324 | } |
| 325 | |
Tim Murray | b27b181 | 2013-08-05 14:00:40 -0700 | [diff] [blame] | 326 | ScriptIntrinsicYuvToRGB::ScriptIntrinsicYuvToRGB(sp<RS> rs, sp<const Element> e) |
| 327 | : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB, e) { |
| 328 | |
| 329 | } |
| 330 | |
| 331 | void ScriptIntrinsicYuvToRGB::setInput(sp<Allocation> in) { |
| 332 | Script::setVar(0, in); |
| 333 | } |
| 334 | |
| 335 | void ScriptIntrinsicYuvToRGB::forEach(sp<Allocation> out) { |
| 336 | Script::forEach(0, NULL, out, NULL, 0); |
| 337 | } |