imagefromyuvtextures.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. // This test only works with the GPU backend.
  8. #include "gm/gm.h"
  9. #include "include/core/SkBitmap.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.h"
  12. #include "include/core/SkColorFilter.h"
  13. #include "include/core/SkColorPriv.h"
  14. #include "include/core/SkImage.h"
  15. #include "include/core/SkImageInfo.h"
  16. #include "include/core/SkPaint.h"
  17. #include "include/core/SkPixmap.h"
  18. #include "include/core/SkPoint.h"
  19. #include "include/core/SkRefCnt.h"
  20. #include "include/core/SkScalar.h"
  21. #include "include/core/SkShader.h"
  22. #include "include/core/SkSize.h"
  23. #include "include/core/SkString.h"
  24. #include "include/core/SkTileMode.h"
  25. #include "include/core/SkTypes.h"
  26. #include "include/effects/SkGradientShader.h"
  27. #include "include/gpu/GrBackendSurface.h"
  28. #include "include/gpu/GrContext.h"
  29. #include "include/gpu/GrTypes.h"
  30. #include "include/private/GrTypesPriv.h"
  31. #include "include/private/SkTo.h"
  32. #include "src/gpu/GrContextPriv.h"
  33. #include "src/gpu/GrGpu.h"
  34. class GrRenderTargetContext;
  35. static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
  36. static const float kJPEGConversionMatrix[20] = {
  37. 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
  38. 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
  39. 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
  40. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f
  41. };
  42. return SkColorFilters::Matrix(kJPEGConversionMatrix);
  43. }
  44. namespace skiagm {
  45. class ImageFromYUVTextures : public GpuGM {
  46. public:
  47. ImageFromYUVTextures() {
  48. this->setBGColor(0xFFFFFFFF);
  49. }
  50. protected:
  51. SkString onShortName() override {
  52. return SkString("image_from_yuv_textures");
  53. }
  54. SkISize onISize() override {
  55. return SkISize::Make(kBmpSize + 2 * kPad, 390);
  56. }
  57. void onOnceBeforeDraw() override {
  58. // We create an RGB bitmap and then extract YUV bmps where the U and V bitmaps are
  59. // subsampled by 2 in both dimensions.
  60. SkPaint paint;
  61. constexpr SkColor kColors[] =
  62. { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE };
  63. paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(0,0), kBmpSize / 2.f, kColors,
  64. nullptr, SK_ARRAY_COUNT(kColors),
  65. SkTileMode::kMirror));
  66. SkBitmap rgbBmp;
  67. rgbBmp.allocN32Pixels(kBmpSize, kBmpSize, true);
  68. SkCanvas canvas(rgbBmp);
  69. canvas.drawPaint(paint);
  70. SkPMColor* rgbColors = static_cast<SkPMColor*>(rgbBmp.getPixels());
  71. SkImageInfo yinfo = SkImageInfo::MakeA8(kBmpSize, kBmpSize);
  72. fYUVBmps[0].allocPixels(yinfo);
  73. SkImageInfo uinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2);
  74. fYUVBmps[1].allocPixels(uinfo);
  75. SkImageInfo vinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2);
  76. fYUVBmps[2].allocPixels(vinfo);
  77. unsigned char* yPixels;
  78. signed char* uvPixels[2];
  79. yPixels = static_cast<unsigned char*>(fYUVBmps[0].getPixels());
  80. uvPixels[0] = static_cast<signed char*>(fYUVBmps[1].getPixels());
  81. uvPixels[1] = static_cast<signed char*>(fYUVBmps[2].getPixels());
  82. // Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though
  83. // we will draw it with all the supported yuv color spaces when converted back to RGB
  84. for (int i = 0; i < kBmpSize * kBmpSize; ++i) {
  85. yPixels[i] = static_cast<unsigned char>(0.299f * SkGetPackedR32(rgbColors[i]) +
  86. 0.587f * SkGetPackedG32(rgbColors[i]) +
  87. 0.114f * SkGetPackedB32(rgbColors[i]));
  88. }
  89. for (int j = 0; j < kBmpSize / 2; ++j) {
  90. for (int i = 0; i < kBmpSize / 2; ++i) {
  91. // Average together 4 pixels of RGB.
  92. int rgb[] = { 0, 0, 0 };
  93. for (int y = 0; y < 2; ++y) {
  94. for (int x = 0; x < 2; ++x) {
  95. int rgbIndex = (2 * j + y) * kBmpSize + 2 * i + x;
  96. rgb[0] += SkGetPackedR32(rgbColors[rgbIndex]);
  97. rgb[1] += SkGetPackedG32(rgbColors[rgbIndex]);
  98. rgb[2] += SkGetPackedB32(rgbColors[rgbIndex]);
  99. }
  100. }
  101. for (int c = 0; c < 3; ++c) {
  102. rgb[c] /= 4;
  103. }
  104. int uvIndex = j * kBmpSize / 2 + i;
  105. uvPixels[0][uvIndex] = static_cast<signed char>(
  106. ((-38 * rgb[0] - 74 * rgb[1] + 112 * rgb[2] + 128) >> 8) + 128);
  107. uvPixels[1][uvIndex] = static_cast<signed char>(
  108. ((112 * rgb[0] - 94 * rgb[1] - 18 * rgb[2] + 128) >> 8) + 128);
  109. }
  110. }
  111. fRGBImage = SkImage::MakeRasterCopy(SkPixmap(rgbBmp.info(), rgbColors, rgbBmp.rowBytes()));
  112. }
  113. void createYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) {
  114. for (int i = 0; i < 3; ++i) {
  115. SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes()));
  116. yuvTextures[i] = context->priv().createBackendTexture(&fYUVBmps[i].pixmap(), 1,
  117. GrRenderable::kNo,
  118. GrProtected::kNo);
  119. }
  120. }
  121. void createResultTexture(GrContext* context, int width, int height,
  122. GrBackendTexture* resultTexture) {
  123. *resultTexture = context->createBackendTexture(
  124. width, height, kRGBA_8888_SkColorType, SkColors::kTransparent,
  125. GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
  126. }
  127. void deleteBackendTextures(GrContext* context, GrBackendTexture textures[], int n) {
  128. if (context->abandoned()) {
  129. return;
  130. }
  131. GrFlushInfo flushInfo;
  132. flushInfo.fFlags = kSyncCpu_GrFlushFlag;
  133. context->flush(flushInfo);
  134. for (int i = 0; i < n; ++i) {
  135. context->deleteBackendTexture(textures[i]);
  136. }
  137. }
  138. void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
  139. // draw the original
  140. SkScalar yOffset = kPad;
  141. canvas->drawImage(fRGBImage.get(), kPad, yOffset);
  142. yOffset += kBmpSize + kPad;
  143. for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
  144. GrBackendTexture yuvTextures[3];
  145. this->createYUVTextures(context, yuvTextures);
  146. auto image = SkImage::MakeFromYUVTexturesCopy(context,
  147. static_cast<SkYUVColorSpace>(space),
  148. yuvTextures,
  149. kTopLeft_GrSurfaceOrigin);
  150. this->deleteBackendTextures(context, yuvTextures, 3);
  151. SkPaint paint;
  152. if (kIdentity_SkYUVColorSpace == space) {
  153. // The identity color space needs post-processing to appear correct
  154. paint.setColorFilter(yuv_to_rgb_colorfilter());
  155. }
  156. canvas->drawImage(image.get(), kPad, yOffset, &paint);
  157. yOffset += kBmpSize + kPad;
  158. }
  159. for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
  160. GrBackendTexture yuvTextures[3];
  161. GrBackendTexture resultTexture;
  162. this->createYUVTextures(context, yuvTextures);
  163. this->createResultTexture(
  164. context, yuvTextures[0].width(), yuvTextures[0].height(), &resultTexture);
  165. auto image = SkImage::MakeFromYUVTexturesCopyWithExternalBackend(
  166. context,
  167. static_cast<SkYUVColorSpace>(space),
  168. yuvTextures,
  169. kTopLeft_GrSurfaceOrigin,
  170. resultTexture);
  171. SkPaint paint;
  172. if (kIdentity_SkYUVColorSpace == space) {
  173. // The identity color space needs post-processing to appear correct
  174. paint.setColorFilter(yuv_to_rgb_colorfilter());
  175. }
  176. canvas->drawImage(image.get(), kPad, yOffset, &paint);
  177. yOffset += kBmpSize + kPad;
  178. GrBackendTexture texturesToDelete[4]{
  179. yuvTextures[0],
  180. yuvTextures[1],
  181. yuvTextures[2],
  182. resultTexture,
  183. };
  184. this->deleteBackendTextures(context, texturesToDelete, 4);
  185. }
  186. }
  187. private:
  188. sk_sp<SkImage> fRGBImage;
  189. SkBitmap fYUVBmps[3];
  190. static constexpr SkScalar kPad = 10.0f;
  191. static constexpr int kBmpSize = 32;
  192. typedef GM INHERITED;
  193. };
  194. DEF_GM(return new ImageFromYUVTextures;)
  195. }