Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1 | /* |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 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 | |
| 18 | #include "rsCpuIntrinsic.h" |
| 19 | #include "rsCpuIntrinsicInlines.h" |
| 20 | |
Tim Murray | aa78237 | 2013-08-29 18:42:54 -0700 | [diff] [blame] | 21 | #ifdef RS_COMPATIBILITY_LIB |
| 22 | #include "rsCompatibilityLib.h" |
| 23 | #endif |
| 24 | |
Jason Sams | 6b58909 | 2013-04-19 14:32:31 -0700 | [diff] [blame] | 25 | #ifndef RS_COMPATIBILITY_LIB |
| 26 | #include "hardware/gralloc.h" |
| 27 | #endif |
| 28 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 29 | using namespace android; |
| 30 | using namespace android::renderscript; |
| 31 | |
| 32 | namespace android { |
| 33 | namespace renderscript { |
| 34 | |
| 35 | |
| 36 | class RsdCpuScriptIntrinsicYuvToRGB : public RsdCpuScriptIntrinsic { |
| 37 | public: |
| 38 | virtual void populateScript(Script *); |
| 39 | virtual void invokeFreeChildren(); |
| 40 | |
| 41 | virtual void setGlobalObj(uint32_t slot, ObjectBase *data); |
| 42 | |
| 43 | virtual ~RsdCpuScriptIntrinsicYuvToRGB(); |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 44 | RsdCpuScriptIntrinsicYuvToRGB(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 45 | |
| 46 | protected: |
| 47 | ObjectBaseRef<Allocation> alloc; |
| 48 | |
Chris Wailes | 80ef693 | 2014-07-08 11:22:18 -0700 | [diff] [blame] | 49 | static void kernel(const RsExpandKernelParams *p, |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 50 | uint32_t xstart, uint32_t xend, |
Chris Wailes | 9ed7910 | 2014-07-25 15:53:28 -0700 | [diff] [blame] | 51 | uint32_t outstep); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
| 58 | void RsdCpuScriptIntrinsicYuvToRGB::setGlobalObj(uint32_t slot, ObjectBase *data) { |
| 59 | rsAssert(slot == 0); |
| 60 | alloc.set(static_cast<Allocation *>(data)); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | static uchar4 rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v) { |
| 67 | short Y = ((short)y) - 16; |
| 68 | short U = ((short)u) - 128; |
| 69 | short V = ((short)v) - 128; |
| 70 | |
| 71 | short4 p; |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 72 | p.x = (Y * 298 + V * 409 + 128) >> 8; |
| 73 | p.y = (Y * 298 - U * 100 - V * 208 + 128) >> 8; |
| 74 | p.z = (Y * 298 + U * 516 + 128) >> 8; |
| 75 | p.w = 255; |
| 76 | if(p.x < 0) { |
| 77 | p.x = 0; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 78 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 79 | if(p.x > 255) { |
| 80 | p.x = 255; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 81 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 82 | if(p.y < 0) { |
| 83 | p.y = 0; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 84 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 85 | if(p.y > 255) { |
| 86 | p.y = 255; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 87 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 88 | if(p.z < 0) { |
| 89 | p.z = 0; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 90 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 91 | if(p.z > 255) { |
| 92 | p.z = 255; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 93 | } |
| 94 | |
synergy dev | 8994abb | 2013-12-05 00:24:37 -0800 | [diff] [blame] | 95 | return (uchar4){static_cast<uchar>(p.x), static_cast<uchar>(p.y), |
| 96 | static_cast<uchar>(p.z), static_cast<uchar>(p.w)}; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
Simon Hosie | ccd7a46 | 2014-02-01 01:35:11 -0800 | [diff] [blame] | 100 | extern "C" void rsdIntrinsicYuv_K(void *dst, const uchar *Y, const uchar *uv, uint32_t xstart, size_t xend); |
| 101 | extern "C" void rsdIntrinsicYuvR_K(void *dst, const uchar *Y, const uchar *uv, uint32_t xstart, size_t xend); |
| 102 | extern "C" void rsdIntrinsicYuv2_K(void *dst, const uchar *Y, const uchar *u, const uchar *v, size_t xstart, size_t xend); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 103 | |
Chris Wailes | 80ef693 | 2014-07-08 11:22:18 -0700 | [diff] [blame] | 104 | void RsdCpuScriptIntrinsicYuvToRGB::kernel(const RsExpandKernelParams *p, |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 105 | uint32_t xstart, uint32_t xend, |
Chris Wailes | 9ed7910 | 2014-07-25 15:53:28 -0700 | [diff] [blame] | 106 | uint32_t outstep) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 107 | RsdCpuScriptIntrinsicYuvToRGB *cp = (RsdCpuScriptIntrinsicYuvToRGB *)p->usr; |
| 108 | if (!cp->alloc.get()) { |
| 109 | ALOGE("YuvToRGB executed without input, skipping"); |
| 110 | return; |
| 111 | } |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 112 | const uchar *pinY = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr; |
Jason Sams | e99f3e2 | 2013-09-11 18:18:54 -0700 | [diff] [blame] | 113 | if (pinY == NULL) { |
| 114 | ALOGE("YuvToRGB executed without data, skipping"); |
| 115 | return; |
| 116 | } |
Tim Murray | 606e500 | 2013-06-13 12:33:49 -0700 | [diff] [blame] | 117 | |
| 118 | size_t strideY = cp->alloc->mHal.drvState.lod[0].stride; |
| 119 | |
| 120 | // calculate correct stride in legacy case |
| 121 | if (cp->alloc->mHal.drvState.lod[0].dimY == 0) { |
| 122 | strideY = p->dimX; |
| 123 | } |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 124 | const uchar *Y = pinY + (p->y * strideY); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 125 | |
Simon Hosie | 1d9c887 | 2014-05-01 23:28:45 -0700 | [diff] [blame] | 126 | uchar4 *out = (uchar4 *)p->out + xstart; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 127 | uint32_t x1 = xstart; |
| 128 | uint32_t x2 = xend; |
| 129 | |
Jason Sams | 0052f8d | 2013-09-19 17:27:29 -0700 | [diff] [blame] | 130 | size_t cstep = cp->alloc->mHal.drvState.yuv.step; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 131 | |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 132 | const uchar *pinU = (const uchar *)cp->alloc->mHal.drvState.lod[1].mallocPtr; |
| 133 | const size_t strideU = cp->alloc->mHal.drvState.lod[1].stride; |
| 134 | const uchar *u = pinU + ((p->y >> 1) * strideU); |
Jason Sams | 06bd91e | 2013-06-11 18:38:11 -0700 | [diff] [blame] | 135 | |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 136 | const uchar *pinV = (const uchar *)cp->alloc->mHal.drvState.lod[2].mallocPtr; |
| 137 | const size_t strideV = cp->alloc->mHal.drvState.lod[2].stride; |
| 138 | const uchar *v = pinV + ((p->y >> 1) * strideV); |
| 139 | |
Jason Sams | 0052f8d | 2013-09-19 17:27:29 -0700 | [diff] [blame] | 140 | //ALOGE("pinY, %p, Y, %p, p->y, %d, strideY, %d", pinY, Y, p->y, strideY); |
| 141 | //ALOGE("pinU, %p, U, %p, p->y, %d, strideU, %d", pinU, u, p->y, strideU); |
| 142 | //ALOGE("pinV, %p, V, %p, p->y, %d, strideV, %d", pinV, v, p->y, strideV); |
| 143 | //ALOGE("dimX, %d, dimY, %d", cp->alloc->mHal.drvState.lod[0].dimX, cp->alloc->mHal.drvState.lod[0].dimY); |
| 144 | //ALOGE("p->dimX, %d, p->dimY, %d", p->dimX, p->dimY); |
| 145 | |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 146 | if (pinU == NULL) { |
| 147 | // Legacy yuv support didn't fill in uv |
| 148 | v = ((uint8_t *)cp->alloc->mHal.drvState.lod[0].mallocPtr) + |
| 149 | (strideY * p->dimY) + |
| 150 | ((p->y >> 1) * strideY); |
| 151 | u = v + 1; |
Jason Sams | 0052f8d | 2013-09-19 17:27:29 -0700 | [diff] [blame] | 152 | cstep = 2; |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Simon Hosie | 1d9c887 | 2014-05-01 23:28:45 -0700 | [diff] [blame] | 155 | /* If we start on an odd pixel then deal with it here and bump things along |
| 156 | * so that subsequent code can carry on with even-odd pairing assumptions. |
| 157 | */ |
| 158 | if((x1 & 1) && (x2 > x1)) { |
| 159 | int cx = (x1 >> 1) * cstep; |
| 160 | *out = rsYuvToRGBA_uchar4(Y[x1], u[cx], v[cx]); |
| 161 | out++; |
| 162 | x1++; |
| 163 | } |
Tim Murray | 6a45ddb | 2014-08-06 11:49:02 -0700 | [diff] [blame] | 164 | // reenable for ARM64 when intrinsic is fixed |
| 165 | #if defined(ARCH_ARM_USE_INTRINSICS) && !defined(ARCH_ARM64_USE_INTRINSICS) |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 166 | if((x2 > x1) && gArchUseSIMD) { |
Simon Hosie | ccd7a46 | 2014-02-01 01:35:11 -0800 | [diff] [blame] | 167 | int32_t len = x2 - x1; |
| 168 | if (cstep == 1) { |
Simon Hosie | 1d9c887 | 2014-05-01 23:28:45 -0700 | [diff] [blame] | 169 | rsdIntrinsicYuv2_K(p->out, Y, u, v, x1, x2); |
Simon Hosie | ccd7a46 | 2014-02-01 01:35:11 -0800 | [diff] [blame] | 170 | x1 += len; |
| 171 | out += len; |
| 172 | } else if (cstep == 2) { |
| 173 | // Check for proper interleave |
| 174 | intptr_t ipu = (intptr_t)u; |
| 175 | intptr_t ipv = (intptr_t)v; |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 176 | |
Simon Hosie | ccd7a46 | 2014-02-01 01:35:11 -0800 | [diff] [blame] | 177 | if (ipu == (ipv + 1)) { |
Simon Hosie | 1d9c887 | 2014-05-01 23:28:45 -0700 | [diff] [blame] | 178 | rsdIntrinsicYuv_K(p->out, Y, v, x1, x2); |
Simon Hosie | ccd7a46 | 2014-02-01 01:35:11 -0800 | [diff] [blame] | 179 | x1 += len; |
| 180 | out += len; |
| 181 | } else if (ipu == (ipv - 1)) { |
Simon Hosie | 1d9c887 | 2014-05-01 23:28:45 -0700 | [diff] [blame] | 182 | rsdIntrinsicYuvR_K(p->out, Y, u, x1, x2); |
Simon Hosie | ccd7a46 | 2014-02-01 01:35:11 -0800 | [diff] [blame] | 183 | x1 += len; |
| 184 | out += len; |
Jason Sams | 6b58909 | 2013-04-19 14:32:31 -0700 | [diff] [blame] | 185 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 186 | } |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 187 | } |
Jason Sams | 6b58909 | 2013-04-19 14:32:31 -0700 | [diff] [blame] | 188 | #endif |
Jason Sams | 61656a7 | 2013-09-03 16:21:18 -0700 | [diff] [blame] | 189 | |
| 190 | if(x2 > x1) { |
| 191 | // ALOGE("y %i %i %i", p->y, x1, x2); |
| 192 | while(x1 < x2) { |
| 193 | int cx = (x1 >> 1) * cstep; |
| 194 | *out = rsYuvToRGBA_uchar4(Y[x1], u[cx], v[cx]); |
| 195 | out++; |
| 196 | x1++; |
| 197 | *out = rsYuvToRGBA_uchar4(Y[x1], u[cx], v[cx]); |
| 198 | out++; |
| 199 | x1++; |
| 200 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 201 | } |
Jason Sams | 6b58909 | 2013-04-19 14:32:31 -0700 | [diff] [blame] | 202 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | RsdCpuScriptIntrinsicYuvToRGB::RsdCpuScriptIntrinsicYuvToRGB( |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 206 | RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) |
| 207 | : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 208 | |
| 209 | mRootPtr = &kernel; |
| 210 | } |
| 211 | |
| 212 | RsdCpuScriptIntrinsicYuvToRGB::~RsdCpuScriptIntrinsicYuvToRGB() { |
| 213 | } |
| 214 | |
| 215 | void RsdCpuScriptIntrinsicYuvToRGB::populateScript(Script *s) { |
| 216 | s->mHal.info.exportedVariableCount = 1; |
| 217 | } |
| 218 | |
| 219 | void RsdCpuScriptIntrinsicYuvToRGB::invokeFreeChildren() { |
| 220 | alloc.clear(); |
| 221 | } |
| 222 | |
| 223 | |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 224 | RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx, |
| 225 | const Script *s, const Element *e) { |
| 226 | return new RsdCpuScriptIntrinsicYuvToRGB(ctx, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 227 | } |