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