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 #if SK_SUPPORT_GPU
     13 
     14 #include "GrContext.h"
     15 #include "GrContextPriv.h"
     16 #include "GrGpu.h"
     17 #include "GrTest.h"
     18 #include "SkBitmap.h"
     19 #include "SkGradientShader.h"
     20 #include "SkImage.h"
     21 
     22 namespace skiagm {
     23 class ImageFromYUVTextures : public GM {
     24 public:
     25     ImageFromYUVTextures() {
     26         this->setBGColor(0xFFFFFFFF);
     27     }
     28 
     29 protected:
     30     SkString onShortName() override {
     31         return SkString("image_from_yuv_textures");
     32     }
     33 
     34     SkISize onISize() override {
     35         return SkISize::Make(50, 175);
     36     }
     37 
     38     void onOnceBeforeDraw() override {
     39         // We create an RGB bitmap and then extract YUV bmps where the U and V bitmaps are
     40         // subsampled by 2 in both dimensions.
     41         SkPaint paint;
     42         constexpr SkColor kColors[] =
     43             { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE };
     44         paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(0,0), kBmpSize / 2.f, kColors,
     45                                                      nullptr, SK_ARRAY_COUNT(kColors),
     46                                                      SkShader::kMirror_TileMode));
     47         SkBitmap rgbBmp;
     48         rgbBmp.allocN32Pixels(kBmpSize, kBmpSize, true);
     49         SkCanvas canvas(rgbBmp);
     50         canvas.drawPaint(paint);
     51         SkPMColor* rgbColors = static_cast<SkPMColor*>(rgbBmp.getPixels());
     52 
     53         SkImageInfo yinfo = SkImageInfo::MakeA8(kBmpSize, kBmpSize);
     54         fYUVBmps[0].allocPixels(yinfo);
     55         SkImageInfo uinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2);
     56         fYUVBmps[1].allocPixels(uinfo);
     57         SkImageInfo vinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2);
     58         fYUVBmps[2].allocPixels(vinfo);
     59         unsigned char* yPixels;
     60         signed char* uvPixels[2];
     61         yPixels = static_cast<unsigned char*>(fYUVBmps[0].getPixels());
     62         uvPixels[0] = static_cast<signed char*>(fYUVBmps[1].getPixels());
     63         uvPixels[1] = static_cast<signed char*>(fYUVBmps[2].getPixels());
     64 
     65         // Here we encode using the NTC encoding (even though we will draw it with all the supported
     66         // yuv color spaces when converted back to RGB)
     67         for (int i = 0; i < kBmpSize * kBmpSize; ++i) {
     68             yPixels[i] = static_cast<unsigned char>(0.299f * SkGetPackedR32(rgbColors[i]) +
     69                                                     0.587f * SkGetPackedG32(rgbColors[i]) +
     70                                                     0.114f * SkGetPackedB32(rgbColors[i]));
     71         }
     72         for (int j = 0; j < kBmpSize / 2; ++j) {
     73             for (int i = 0; i < kBmpSize / 2; ++i) {
     74                 // Average together 4 pixels of RGB.
     75                 int rgb[] = { 0, 0, 0 };
     76                 for (int y = 0; y < 2; ++y) {
     77                     for (int x = 0; x < 2; ++x) {
     78                         int rgbIndex = (2 * j + y) * kBmpSize + 2 * i + x;
     79                         rgb[0] += SkGetPackedR32(rgbColors[rgbIndex]);
     80                         rgb[1] += SkGetPackedG32(rgbColors[rgbIndex]);
     81                         rgb[2] += SkGetPackedB32(rgbColors[rgbIndex]);
     82                     }
     83                 }
     84                 for (int c = 0; c < 3; ++c) {
     85                     rgb[c] /= 4;
     86                 }
     87                 int uvIndex = j * kBmpSize / 2 + i;
     88                 uvPixels[0][uvIndex] = static_cast<signed char>(
     89                     ((-38 * rgb[0] -  74 * rgb[1] + 112 * rgb[2] + 128) >> 8) + 128);
     90                 uvPixels[1][uvIndex] = static_cast<signed char>(
     91                     ((112 * rgb[0] -  94 * rgb[1] -  18 * rgb[2] + 128) >> 8) + 128);
     92             }
     93         }
     94         fRGBImage = SkImage::MakeRasterCopy(SkPixmap(rgbBmp.info(), rgbColors, rgbBmp.rowBytes()));
     95     }
     96 
     97     void createYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) {
     98         GrGpu* gpu = context->contextPriv().getGpu();
     99         if (!gpu) {
    100             return;
    101         }
    102 
    103         for (int i = 0; i < 3; ++i) {
    104             SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes()));
    105             yuvTextures[i] = gpu->createTestingOnlyBackendTexture(fYUVBmps[i].getPixels(),
    106                                                                   fYUVBmps[i].width(),
    107                                                                   fYUVBmps[i].height(),
    108                                                                   kAlpha_8_GrPixelConfig,
    109                                                                   false, GrMipMapped::kNo);
    110         }
    111         context->resetContext();
    112     }
    113 
    114     void deleteYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) {
    115 
    116         GrGpu* gpu = context->contextPriv().getGpu();
    117         if (!gpu) {
    118             return;
    119         }
    120 
    121         for (int i = 0; i < 3; ++i) {
    122             if (yuvTextures[i].isValid()) {
    123                 gpu->deleteTestingOnlyBackendTexture(&yuvTextures[i]);
    124             }
    125         }
    126 
    127         context->resetContext();
    128     }
    129 
    130     void onDraw(SkCanvas* canvas) override {
    131         GrContext* context = canvas->getGrContext();
    132         if (!context) {
    133             skiagm::GM::DrawGpuOnlyMessage(canvas);
    134             return;
    135         }
    136 
    137 
    138         constexpr SkScalar kPad = 10.f;
    139 
    140         SkISize sizes[] = {
    141             { fYUVBmps[0].width(), fYUVBmps[0].height()},
    142             { fYUVBmps[1].width(), fYUVBmps[1].height()},
    143             { fYUVBmps[2].width(), fYUVBmps[2].height()},
    144         };
    145         SkTArray<sk_sp<SkImage>> images;
    146         images.push_back(fRGBImage);
    147         for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
    148             GrBackendTexture yuvTextures[3];
    149             this->createYUVTextures(context, yuvTextures);
    150             images.push_back(SkImage::MakeFromYUVTexturesCopy(context,
    151                                                               static_cast<SkYUVColorSpace>(space),
    152                                                               yuvTextures, sizes,
    153                                                               kTopLeft_GrSurfaceOrigin));
    154             this->deleteYUVTextures(context, yuvTextures);
    155         }
    156         for (int i = 0; i < images.count(); ++ i) {
    157             SkScalar y = (i + 1) * kPad + i * fYUVBmps[0].height();
    158             SkScalar x = kPad;
    159 
    160             canvas->drawImage(images[i].get(), x, y);
    161         }
    162      }
    163 
    164 private:
    165     sk_sp<SkImage>  fRGBImage;
    166     SkBitmap        fYUVBmps[3];
    167 
    168     static constexpr int kBmpSize = 32;
    169 
    170     typedef GM INHERITED;
    171 };
    172 
    173 DEF_GM(return new ImageFromYUVTextures;)
    174 }
    175 
    176 #endif
    177