Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2017 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 #include "gm.h"
      9 #include "Resources.h"
     10 #include "SkCodec.h"
     11 #include "SkColorSpace.h"
     12 #include "SkImage.h"
     13 #include "SkImagePriv.h"
     14 
     15 sk_sp<SkImage> make_raster_image(const char* path) {
     16     sk_sp<SkData> resourceData = GetResourceAsData(path);
     17     std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(resourceData);
     18 
     19     SkBitmap bitmap;
     20     bitmap.allocPixels(codec->getInfo());
     21 
     22     codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes());
     23     return SkImage::MakeFromBitmap(bitmap);
     24 }
     25 
     26 sk_sp<SkImage> make_color_space(sk_sp<SkImage> orig, sk_sp<SkColorSpace> colorSpace) {
     27     sk_sp<SkImage> xform = orig->makeColorSpace(colorSpace);
     28 
     29     // Assign an sRGB color space on the xformed image, so we can see the effects of the xform
     30     // when we draw.
     31     sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
     32     if (colorSpace->gammaIsLinear()) {
     33         srgb = SkColorSpace::MakeSRGBLinear();
     34     }
     35     return SkImageMakeRasterCopyAndAssignColorSpace(xform.get(), srgb.get());
     36 }
     37 
     38 class MakeCSGM : public skiagm::GM {
     39 public:
     40     MakeCSGM() {}
     41 
     42 protected:
     43     SkString onShortName() override {
     44         return SkString("makecolorspace");
     45     }
     46 
     47     SkISize onISize() override {
     48         return SkISize::Make(128*3, 128*4);
     49     }
     50 
     51     void onDraw(SkCanvas* canvas) override {
     52         sk_sp<SkColorSpace> wideGamut = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
     53                                                               SkNamedGamut::kAdobeRGB);
     54         sk_sp<SkColorSpace> wideGamutLinear = wideGamut->makeLinearGamma();
     55 
     56         // Lazy images
     57         sk_sp<SkImage> opaqueImage = GetResourceAsImage("images/mandrill_128.png");
     58         sk_sp<SkImage> premulImage = GetResourceAsImage("images/color_wheel.png");
     59         if (!opaqueImage || !premulImage) {
     60             return;
     61         }
     62         canvas->drawImage(opaqueImage, 0.0f, 0.0f);
     63         canvas->drawImage(make_color_space(opaqueImage, wideGamut), 128.0f, 0.0f);
     64         canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear), 256.0f, 0.0f);
     65         canvas->drawImage(premulImage, 0.0f, 128.0f);
     66         canvas->drawImage(make_color_space(premulImage, wideGamut), 128.0f, 128.0f);
     67         canvas->drawImage(make_color_space(premulImage, wideGamutLinear), 256.0f, 128.0f);
     68         canvas->translate(0.0f, 256.0f);
     69 
     70         // Raster images
     71         opaqueImage = make_raster_image("images/mandrill_128.png");
     72         premulImage = make_raster_image("images/color_wheel.png");
     73         canvas->drawImage(opaqueImage, 0.0f, 0.0f);
     74         canvas->drawImage(make_color_space(opaqueImage, wideGamut), 128.0f, 0.0f);
     75         canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear), 256.0f, 0.0f);
     76         canvas->drawImage(premulImage, 0.0f, 128.0f);
     77         canvas->drawImage(make_color_space(premulImage, wideGamut), 128.0f, 128.0f);
     78         canvas->drawImage(make_color_space(premulImage, wideGamutLinear), 256.0f, 128.0f);
     79     }
     80 
     81 private:
     82     typedef skiagm::GM INHERITED;
     83 };
     84 
     85 DEF_GM(return new MakeCSGM;)
     86 
     87 DEF_SIMPLE_GM_BG(makecolortypeandspace, canvas, 128 * 3, 128 * 4, SK_ColorWHITE) {
     88     sk_sp<SkImage> images[] = {
     89         GetResourceAsImage("images/mandrill_128.png"),
     90         GetResourceAsImage("images/color_wheel.png"),
     91     };
     92     auto rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kRec2020);
     93 
     94     // Use the lazy images on the first iteration, and concrete (raster/GPU) images on the second
     95     for (bool lazy : {true, false}) {
     96         for (int j = 0; j < 2; ++j) {
     97             const SkImage* image = images[j].get();
     98             if (!image) {
     99                 // Can happen on bots that abandon the GPU context
    100                 continue;
    101             }
    102 
    103             // Unmodified
    104             canvas->drawImage(image, 0, 0);
    105 
    106             // Change the color type/space of the image in a couple ways. In both cases, codec
    107             // may fail, because we refude to decode transparent sources to opaque color types.
    108             // Guard against that, to avoid cascading failures in DDL.
    109 
    110             // 565 in a wide color space (should be visibly quantized). Fails with the color_wheel,
    111             // because of the codec issues mentioned above.
    112             auto image565 = image->makeColorTypeAndColorSpace(kRGB_565_SkColorType, rec2020);
    113             if (!lazy || image565->makeRasterImage()) {
    114                 canvas->drawImage(image565, 128, 0);
    115             }
    116 
    117             // Grayscale in the original color space. This fails in even more cases, due to the
    118             // above opaque issue, and because Ganesh doesn't support drawing to gray, at all.
    119             auto imageGray = image->makeColorTypeAndColorSpace(kGray_8_SkColorType,
    120                                                                image->refColorSpace());
    121             if (!lazy || imageGray->makeRasterImage()) {
    122                 canvas->drawImage(imageGray, 256, 0);
    123             }
    124 
    125             images[j] = canvas->getGrContext()
    126                     ? image->makeTextureImage(canvas->getGrContext(), nullptr)
    127                     : image->makeRasterImage();
    128 
    129             canvas->translate(0, 128);
    130         }
    131     }
    132 }
    133