rsCpuIntrinsicYuvToRGB.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2013 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. #include "rsCpuIntrinsic.h"
  17. #include "rsCpuIntrinsicInlines.h"
  18. #ifdef RS_COMPATIBILITY_LIB
  19. #include "rsCompatibilityLib.h"
  20. #endif
  21. #ifndef RS_COMPATIBILITY_LIB
  22. #include "hardware/gralloc.h"
  23. #endif
  24. namespace android {
  25. namespace renderscript {
  26. class RsdCpuScriptIntrinsicYuvToRGB : public RsdCpuScriptIntrinsic {
  27. public:
  28. void populateScript(Script *) override;
  29. void invokeFreeChildren() override;
  30. void setGlobalObj(uint32_t slot, ObjectBase *data) override;
  31. ~RsdCpuScriptIntrinsicYuvToRGB() override;
  32. RsdCpuScriptIntrinsicYuvToRGB(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e);
  33. protected:
  34. ObjectBaseRef<Allocation> alloc;
  35. static void kernel(const RsExpandKernelDriverInfo *info,
  36. uint32_t xstart, uint32_t xend,
  37. uint32_t outstep);
  38. };
  39. void RsdCpuScriptIntrinsicYuvToRGB::setGlobalObj(uint32_t slot, ObjectBase *data) {
  40. rsAssert(slot == 0);
  41. alloc.set(static_cast<Allocation *>(data));
  42. }
  43. static uchar4 rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v) {
  44. int16_t Y = ((int16_t)y) - 16;
  45. int16_t U = ((int16_t)u) - 128;
  46. int16_t V = ((int16_t)v) - 128;
  47. short4 p;
  48. p.x = (Y * 298 + V * 409 + 128) >> 8;
  49. p.y = (Y * 298 - U * 100 - V * 208 + 128) >> 8;
  50. p.z = (Y * 298 + U * 516 + 128) >> 8;
  51. p.w = 255;
  52. if(p.x < 0) {
  53. p.x = 0;
  54. }
  55. if(p.x > 255) {
  56. p.x = 255;
  57. }
  58. if(p.y < 0) {
  59. p.y = 0;
  60. }
  61. if(p.y > 255) {
  62. p.y = 255;
  63. }
  64. if(p.z < 0) {
  65. p.z = 0;
  66. }
  67. if(p.z > 255) {
  68. p.z = 255;
  69. }
  70. return (uchar4){static_cast<uchar>(p.x), static_cast<uchar>(p.y),
  71. static_cast<uchar>(p.z), static_cast<uchar>(p.w)};
  72. }
  73. extern "C" void rsdIntrinsicYuv_K(void *dst, const uchar *Y, const uchar *uv, uint32_t xstart, size_t xend);
  74. extern "C" void rsdIntrinsicYuvR_K(void *dst, const uchar *Y, const uchar *uv, uint32_t xstart, size_t xend);
  75. extern "C" void rsdIntrinsicYuv2_K(void *dst, const uchar *Y, const uchar *u, const uchar *v, size_t xstart, size_t xend);
  76. void RsdCpuScriptIntrinsicYuvToRGB::kernel(const RsExpandKernelDriverInfo *info,
  77. uint32_t xstart, uint32_t xend,
  78. uint32_t outstep) {
  79. RsdCpuScriptIntrinsicYuvToRGB *cp = (RsdCpuScriptIntrinsicYuvToRGB *)info->usr;
  80. if (!cp->alloc.get()) {
  81. ALOGE("YuvToRGB executed without input, skipping");
  82. return;
  83. }
  84. const uchar *pinY = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
  85. if (pinY == nullptr) {
  86. ALOGE("YuvToRGB executed without data, skipping");
  87. return;
  88. }
  89. size_t strideY = cp->alloc->mHal.drvState.lod[0].stride;
  90. // calculate correct stride in legacy case
  91. if (cp->alloc->mHal.drvState.lod[0].dimY == 0) {
  92. strideY = info->dim.x;
  93. }
  94. const uchar *Y = pinY + (info->current.y * strideY);
  95. uchar4 *out = (uchar4 *)info->outPtr[0] + xstart;
  96. uint32_t x1 = xstart;
  97. uint32_t x2 = xend;
  98. size_t cstep = cp->alloc->mHal.drvState.yuv.step;
  99. const uchar *pinU = (const uchar *)cp->alloc->mHal.drvState.lod[1].mallocPtr;
  100. const size_t strideU = cp->alloc->mHal.drvState.lod[1].stride;
  101. const uchar *u = pinU + ((info->current.y >> 1) * strideU);
  102. const uchar *pinV = (const uchar *)cp->alloc->mHal.drvState.lod[2].mallocPtr;
  103. const size_t strideV = cp->alloc->mHal.drvState.lod[2].stride;
  104. const uchar *v = pinV + ((info->current.y >> 1) * strideV);
  105. //ALOGE("pinY, %p, Y, %p, info->current.y, %d, strideY, %d", pinY, Y, info->current.y, strideY);
  106. //ALOGE("pinU, %p, U, %p, info->current.y, %d, strideU, %d", pinU, u, info->current.y, strideU);
  107. //ALOGE("pinV, %p, V, %p, info->current.y, %d, strideV, %d", pinV, v, info->current.y, strideV);
  108. //ALOGE("dimX, %d, dimY, %d", cp->alloc->mHal.drvState.lod[0].dimX, cp->alloc->mHal.drvState.lod[0].dimY);
  109. //ALOGE("info->dim.x, %d, info->dim.y, %d", info->dim.x, info->dim.y);
  110. if (pinU == nullptr) {
  111. // Legacy yuv support didn't fill in uv
  112. v = ((uint8_t *)cp->alloc->mHal.drvState.lod[0].mallocPtr) +
  113. (strideY * info->dim.y) +
  114. ((info->current.y >> 1) * strideY);
  115. u = v + 1;
  116. cstep = 2;
  117. }
  118. /* If we start on an odd pixel then deal with it here and bump things along
  119. * so that subsequent code can carry on with even-odd pairing assumptions.
  120. */
  121. if((x1 & 1) && (x2 > x1)) {
  122. int cx = (x1 >> 1) * cstep;
  123. *out = rsYuvToRGBA_uchar4(Y[x1], u[cx], v[cx]);
  124. out++;
  125. x1++;
  126. }
  127. #if defined(ARCH_ARM_USE_INTRINSICS)
  128. if((x2 > x1) && gArchUseSIMD) {
  129. int32_t len = x2 - x1;
  130. if (cstep == 1) {
  131. rsdIntrinsicYuv2_K(info->outPtr[0], Y, u, v, x1, x2);
  132. x1 += len;
  133. out += len;
  134. } else if (cstep == 2) {
  135. // Check for proper interleave
  136. intptr_t ipu = (intptr_t)u;
  137. intptr_t ipv = (intptr_t)v;
  138. if (ipu == (ipv + 1)) {
  139. rsdIntrinsicYuv_K(info->outPtr[0], Y, v, x1, x2);
  140. x1 += len;
  141. out += len;
  142. } else if (ipu == (ipv - 1)) {
  143. rsdIntrinsicYuvR_K(info->outPtr[0], Y, u, x1, x2);
  144. x1 += len;
  145. out += len;
  146. }
  147. }
  148. }
  149. #endif
  150. if(x2 > x1) {
  151. // ALOGE("y %i %i %i", info->current.y, x1, x2);
  152. while(x1 < x2) {
  153. int cx = (x1 >> 1) * cstep;
  154. *out = rsYuvToRGBA_uchar4(Y[x1], u[cx], v[cx]);
  155. out++;
  156. x1++;
  157. *out = rsYuvToRGBA_uchar4(Y[x1], u[cx], v[cx]);
  158. out++;
  159. x1++;
  160. }
  161. }
  162. }
  163. RsdCpuScriptIntrinsicYuvToRGB::RsdCpuScriptIntrinsicYuvToRGB(
  164. RsdCpuReferenceImpl *ctx, const Script *s, const Element *e)
  165. : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB) {
  166. mRootPtr = &kernel;
  167. }
  168. RsdCpuScriptIntrinsicYuvToRGB::~RsdCpuScriptIntrinsicYuvToRGB() {
  169. }
  170. void RsdCpuScriptIntrinsicYuvToRGB::populateScript(Script *s) {
  171. s->mHal.info.exportedVariableCount = 1;
  172. }
  173. void RsdCpuScriptIntrinsicYuvToRGB::invokeFreeChildren() {
  174. alloc.clear();
  175. }
  176. RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx,
  177. const Script *s, const Element *e) {
  178. return new RsdCpuScriptIntrinsicYuvToRGB(ctx, s, e);
  179. }
  180. } // namespace renderscript
  181. } // namespace android