blob: 3d989bd077ca84ffaa368e93ead9b0d9a92ae7cf [file] [log] [blame]
Jason Sams709a0972012-11-15 18:18:04 -08001/*
Jason Samsbc0ca6b2013-02-15 18:13:43 -08002 * Copyright (C) 2013 The Android Open Source Project
Jason Sams709a0972012-11-15 18:18:04 -08003 *
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
Jason Sams6b589092013-04-19 14:32:31 -070021#ifndef RS_COMPATIBILITY_LIB
22#include "hardware/gralloc.h"
23#endif
24
Jason Sams709a0972012-11-15 18:18:04 -080025using namespace android;
26using namespace android::renderscript;
27
28namespace android {
29namespace renderscript {
30
31
32class RsdCpuScriptIntrinsicYuvToRGB : public RsdCpuScriptIntrinsic {
33public:
34 virtual void populateScript(Script *);
35 virtual void invokeFreeChildren();
36
37 virtual void setGlobalObj(uint32_t slot, ObjectBase *data);
38
39 virtual ~RsdCpuScriptIntrinsicYuvToRGB();
Jason Samsc905efd2012-11-26 15:20:18 -080040 RsdCpuScriptIntrinsicYuvToRGB(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e);
Jason Sams709a0972012-11-15 18:18:04 -080041
42protected:
43 ObjectBaseRef<Allocation> alloc;
44
45 static void kernel(const RsForEachStubParamStruct *p,
46 uint32_t xstart, uint32_t xend,
47 uint32_t instep, uint32_t outstep);
48};
49
50}
51}
52
53
54void RsdCpuScriptIntrinsicYuvToRGB::setGlobalObj(uint32_t slot, ObjectBase *data) {
55 rsAssert(slot == 0);
56 alloc.set(static_cast<Allocation *>(data));
57}
58
59
60
61
62static uchar4 rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v) {
63 short Y = ((short)y) - 16;
64 short U = ((short)u) - 128;
65 short V = ((short)v) - 128;
66
67 short4 p;
Tim Murray0b575de2013-03-15 15:56:43 -070068 p.x = (Y * 298 + V * 409 + 128) >> 8;
69 p.y = (Y * 298 - U * 100 - V * 208 + 128) >> 8;
70 p.z = (Y * 298 + U * 516 + 128) >> 8;
71 p.w = 255;
72 if(p.x < 0) {
73 p.x = 0;
Jason Sams709a0972012-11-15 18:18:04 -080074 }
Tim Murray0b575de2013-03-15 15:56:43 -070075 if(p.x > 255) {
76 p.x = 255;
Jason Sams709a0972012-11-15 18:18:04 -080077 }
Tim Murray0b575de2013-03-15 15:56:43 -070078 if(p.y < 0) {
79 p.y = 0;
Jason Sams709a0972012-11-15 18:18:04 -080080 }
Tim Murray0b575de2013-03-15 15:56:43 -070081 if(p.y > 255) {
82 p.y = 255;
Jason Sams709a0972012-11-15 18:18:04 -080083 }
Tim Murray0b575de2013-03-15 15:56:43 -070084 if(p.z < 0) {
85 p.z = 0;
Jason Sams709a0972012-11-15 18:18:04 -080086 }
Tim Murray0b575de2013-03-15 15:56:43 -070087 if(p.z > 255) {
88 p.z = 255;
Jason Sams709a0972012-11-15 18:18:04 -080089 }
90
Tim Murray0b575de2013-03-15 15:56:43 -070091 return (uchar4){p.x, p.y, p.z, p.w};
Jason Sams709a0972012-11-15 18:18:04 -080092}
93
94
95static short YuvCoeff[] = {
96 298, 409, -100, 516, -208, 255, 0, 0,
97 16, 16, 16, 16, 16, 16, 16, 16,
98 128, 128, 128, 128, 128, 128, 128, 128,
99 298, 298, 298, 298, 298, 298, 298, 298,
100 255, 255, 255, 255, 255, 255, 255, 255
101
102
103};
104
105extern "C" void rsdIntrinsicYuv_K(void *dst, const uchar *Y, const uchar *uv, uint32_t count, const short *param);
Jason Sams6b589092013-04-19 14:32:31 -0700106extern "C" void rsdIntrinsicYuv2_K(void *dst, const uchar *Y, const uchar *u, const uchar *v, uint32_t count, const short *param);
Jason Sams709a0972012-11-15 18:18:04 -0800107
108void RsdCpuScriptIntrinsicYuvToRGB::kernel(const RsForEachStubParamStruct *p,
109 uint32_t xstart, uint32_t xend,
110 uint32_t instep, uint32_t outstep) {
111 RsdCpuScriptIntrinsicYuvToRGB *cp = (RsdCpuScriptIntrinsicYuvToRGB *)p->usr;
112 if (!cp->alloc.get()) {
113 ALOGE("YuvToRGB executed without input, skipping");
114 return;
115 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800116 const uchar *pinY = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800117 const size_t strideY = cp->alloc->mHal.drvState.lod[0].stride;
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800118 const uchar *Y = pinY + (p->y * strideY);
Jason Sams709a0972012-11-15 18:18:04 -0800119
120 uchar4 *out = (uchar4 *)p->out;
121 uint32_t x1 = xstart;
122 uint32_t x2 = xend;
123
Jason Sams6b589092013-04-19 14:32:31 -0700124 switch (cp->alloc->mHal.state.yuv) {
125 // In API 17 there was no yuv format and the intrinsic treated everything as NV21
126 case 0:
127#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
128 case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21
Jason Sams709a0972012-11-15 18:18:04 -0800129#endif
Jason Sams6b589092013-04-19 14:32:31 -0700130 {
131 const uchar *pinUV = (const uchar *)cp->alloc->mHal.drvState.lod[1].mallocPtr;
132 const size_t strideUV = cp->alloc->mHal.drvState.lod[1].stride;
133 const uchar *uv = pinUV + ((p->y >> 1) * strideUV);
Jason Sams709a0972012-11-15 18:18:04 -0800134
Jason Sams6b589092013-04-19 14:32:31 -0700135 if(x2 > x1) {
136 #if defined(ARCH_ARM_HAVE_NEON)
137 int32_t len = (x2 - x1 - 1) >> 3;
138 if(len > 0) {
139 rsdIntrinsicYuv_K(out, Y, uv, len, YuvCoeff);
140 x1 += len << 3;
141 out += len << 3;
142 }
143 #endif
144
145 // ALOGE("y %i %i %i", p->y, x1, x2);
146 while(x1 < x2) {
147 uchar u = uv[(x1 & 0xffffe) + 1];
148 uchar v = uv[(x1 & 0xffffe) + 0];
149 *out = rsYuvToRGBA_uchar4(Y[x1], u, v);
150 out++;
151 x1++;
152 *out = rsYuvToRGBA_uchar4(Y[x1], u, v);
153 out++;
154 x1++;
155 }
156 }
Jason Sams709a0972012-11-15 18:18:04 -0800157 }
Jason Sams6b589092013-04-19 14:32:31 -0700158 break;
159
160#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
161 case HAL_PIXEL_FORMAT_YV12:
162 {
163 const uchar *pinU = (const uchar *)cp->alloc->mHal.drvState.lod[1].mallocPtr;
164 const size_t strideU = cp->alloc->mHal.drvState.lod[1].stride;
165 const uchar *u = pinU + ((p->y >> 1) * strideU);
166
167 const uchar *pinV = (const uchar *)cp->alloc->mHal.drvState.lod[2].mallocPtr;
168 const size_t strideV = cp->alloc->mHal.drvState.lod[2].stride;
169 const uchar *v = pinV + ((p->y >> 1) * strideV);
170
171 if(x2 > x1) {
172 #if defined(ARCH_ARM_HAVE_NEON)
173 int32_t len = (x2 - x1 - 1) >> 3;
174 if(len > 0) {
175 rsdIntrinsicYuv2_K(out, Y, u, v, len, YuvCoeff);
176 x1 += len << 3;
177 out += len << 3;
178 }
179 #endif
180
181 // ALOGE("y %i %i %i", p->y, x1, x2);
182 while(x1 < x2) {
183 uchar ut = u[x1];
184 uchar vt = v[x1];
185 *out = rsYuvToRGBA_uchar4(Y[x1], ut, vt);
186 out++;
187 x1++;
188 *out = rsYuvToRGBA_uchar4(Y[x1], ut, vt);
189 out++;
190 x1++;
191 }
192 }
193 }
194 break;
195#endif
Jason Sams709a0972012-11-15 18:18:04 -0800196 }
Jason Sams6b589092013-04-19 14:32:31 -0700197
Jason Sams709a0972012-11-15 18:18:04 -0800198}
199
200RsdCpuScriptIntrinsicYuvToRGB::RsdCpuScriptIntrinsicYuvToRGB(
Jason Samsc905efd2012-11-26 15:20:18 -0800201 RsdCpuReferenceImpl *ctx, const Script *s, const Element *e)
202 : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB) {
Jason Sams709a0972012-11-15 18:18:04 -0800203
204 mRootPtr = &kernel;
205}
206
207RsdCpuScriptIntrinsicYuvToRGB::~RsdCpuScriptIntrinsicYuvToRGB() {
208}
209
210void RsdCpuScriptIntrinsicYuvToRGB::populateScript(Script *s) {
211 s->mHal.info.exportedVariableCount = 1;
212}
213
214void RsdCpuScriptIntrinsicYuvToRGB::invokeFreeChildren() {
215 alloc.clear();
216}
217
218
Jason Samsc905efd2012-11-26 15:20:18 -0800219RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx,
220 const Script *s, const Element *e) {
221 return new RsdCpuScriptIntrinsicYuvToRGB(ctx, s, e);
Jason Sams709a0972012-11-15 18:18:04 -0800222}
223
224