Home | History | Annotate | Download | only in gm
      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.h"
     11 
     12 #include "GrBackendSurface.h"
     13 #include "GrContext.h"
     14 #include "GrContextPriv.h"
     15 #include "GrGpu.h"
     16 #include "SkBitmap.h"
     17 #include "SkGradientShader.h"
     18 #include "SkImage.h"
     19 #include "SkTo.h"
     20 
     21 static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
     22     static const float kJPEGConversionMatrix[20] = {
     23         1.0f,  0.0f,       1.402f,    0.0f, -180.0f,
     24         1.0f, -0.344136f, -0.714136f, 0.0f,  136.0f,
     25         1.0f,  1.772f,     0.0f,      0.0f, -227.6f,
     26         0.0f,  0.0f,       0.0f,      1.0f,    0.0f
     27     };
     28 
     29     return SkColorFilter::MakeMatrixFilterRowMajor255(kJPEGConversionMatrix);
     30 }
     31 
     32 namespace skiagm {
     33 class ImageFromYUVTextures : public GpuGM {
     34 public:
     35     ImageFromYUVTextures() {
     36         this->setBGColor(0xFFFFFFFF);
     37     }
     38 
     39 protected:
     40     SkString onShortName() override {
     41         return SkString("image_from_yuv_textures");
     42     }
     43 
     44     SkISize onISize() override {
     45         return SkISize::Make(kBmpSize + 2 * kPad, 390);
     46     }
     47 
     48     void onOnceBeforeDraw() override {
     49         // We create an RGB bitmap and then extract YUV bmps where the U and V bitmaps are
     50         // subsampled by 2 in both dimensions.
     51         SkPaint paint;
     52         constexpr SkColor kColors[] =
     53             { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE };
     54         paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(0,0), kBmpSize / 2.f, kColors,
     55                                                      nullptr, SK_ARRAY_COUNT(kColors),
     56                                                      SkShader::kMirror_TileMode));
     57         SkBitmap rgbBmp;
     58         rgbBmp.allocN32Pixels(kBmpSize, kBmpSize, true);
     59         SkCanvas canvas(rgbBmp);
     60         canvas.drawPaint(paint);
     61         SkPMColor* rgbColors = static_cast<SkPMColor*>(rgbBmp.getPixels());
     62 
     63         SkImageInfo yinfo = SkImageInfo::MakeA8(kBmpSize, kBmpSize);
     64         fYUVBmps[0].allocPixels(yinfo);
     65         SkImageInfo uinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2);
     66         fYUVBmps[1].allocPixels(uinfo);
     67         SkImageInfo vinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2);
     68         fYUVBmps[2].allocPixels(vinfo);
     69         unsigned char* yPixels;
     70         signed char* uvPixels[2];
     71         yPixels = static_cast<unsigned char*>(fYUVBmps[0].getPixels());
     72         uvPixels[0] = static_cast<signed char*>(fYUVBmps[1].getPixels());
     73         uvPixels[1] = static_cast<signed char*>(fYUVBmps[2].getPixels());
     74 
     75         // Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though
     76         // we will draw it with all the supported yuv color spaces when converted back to RGB
     77         for (int i = 0; i < kBmpSize * kBmpSize; ++i) {
     78             yPixels[i] = static_cast<unsigned char>(0.299f * SkGetPackedR32(rgbColors[i]) +
     79                                                     0.587f * SkGetPackedG32(rgbColors[i]) +
     80                                                     0.114f * SkGetPackedB32(rgbColors[i]));
     81         }
     82         for (int j = 0; j < kBmpSize / 2; ++j) {
     83             for (int i = 0; i < kBmpSize / 2; ++i) {
     84                 // Average together 4 pixels of RGB.
     85                 int rgb[] = { 0, 0, 0 };
     86                 for (int y = 0; y < 2; ++y) {
     87                     for (int x = 0; x < 2; ++x) {
     88                         int rgbIndex = (2 * j + y) * kBmpSize + 2 * i + x;
     89                         rgb[0] += SkGetPackedR32(rgbColors[rgbIndex]);
     90                         rgb[1] += SkGetPackedG32(rgbColors[rgbIndex]);
     91                         rgb[2] += SkGetPackedB32(rgbColors[rgbIndex]);
     92                     }
     93                 }
     94                 for (int c = 0; c < 3; ++c) {
     95                     rgb[c] /= 4;
     96                 }
     97                 int uvIndex = j * kBmpSize / 2 + i;
     98                 uvPixels[0][uvIndex] = static_cast<signed char>(
     99                     ((-38 * rgb[0] -  74 * rgb[1] + 112 * rgb[2] + 128) >> 8) + 128);
    100                 uvPixels[1][uvIndex] = static_cast<signed char>(
    101                     ((112 * rgb[0] -  94 * rgb[1] -  18 * rgb[2] + 128) >> 8) + 128);
    102             }
    103         }
    104         fRGBImage = SkImage::MakeRasterCopy(SkPixmap(rgbBmp.info(), rgbColors, rgbBmp.rowBytes()));
    105     }
    106 
    107     void createYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) {
    108         GrGpu* gpu = context->priv().getGpu();
    109         if (!gpu) {
    110             return;
    111         }
    112 
    113         for (int i = 0; i < 3; ++i) {
    114             SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes()));
    115             yuvTextures[i] = gpu->createTestingOnlyBackendTexture(fYUVBmps[i].getPixels(),
    116                                                                   fYUVBmps[i].width(),
    117                                                                   fYUVBmps[i].height(),
    118                                                                   GrColorType::kAlpha_8,
    119                                                                   false, GrMipMapped::kNo);
    120         }
    121         context->resetContext();
    122     }
    123 
    124     void createResultTexture(GrContext* context, int width, int height,
    125                              GrBackendTexture* resultTexture) {
    126         GrGpu* gpu = context->priv().getGpu();
    127         if (!gpu) {
    128             return;
    129         }
    130 
    131         *resultTexture = gpu->createTestingOnlyBackendTexture(
    132                 nullptr, width, height, GrColorType::kRGBA_8888, true, GrMipMapped::kNo);
    133 
    134         context->resetContext();
    135     }
    136 
    137     void deleteBackendTextures(GrContext* context, GrBackendTexture textures[], int n) {
    138         if (context->abandoned()) {
    139             return;
    140         }
    141 
    142         GrGpu* gpu = context->priv().getGpu();
    143         if (!gpu) {
    144             return;
    145         }
    146 
    147         context->flush();
    148         gpu->testingOnly_flushGpuAndSync();
    149         for (int i = 0; i < n; ++i) {
    150             if (textures[i].isValid()) {
    151                 gpu->deleteTestingOnlyBackendTexture(textures[i]);
    152             }
    153         }
    154 
    155         context->resetContext();
    156     }
    157 
    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