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     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) 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             *errorMsg = "Failed to load images. Did you forget to set the resourcePath?";
     61             return DrawResult::kFail;
     62         }
     63         canvas->drawImage(opaqueImage, 0.0f, 0.0f);
     64         canvas->drawImage(make_color_space(opaqueImage, wideGamut), 128.0f, 0.0f);
     65         canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear), 256.0f, 0.0f);
     66         canvas->drawImage(premulImage, 0.0f, 128.0f);
     67         canvas->drawImage(make_color_space(premulImage, wideGamut), 128.0f, 128.0f);
     68         canvas->drawImage(make_color_space(premulImage, wideGamutLinear), 256.0f, 128.0f);
     69         canvas->translate(0.0f, 256.0f);
     70 
     71         // Raster images
     72         opaqueImage = make_raster_image("images/mandrill_128.png");
     73         premulImage = make_raster_image("images/color_wheel.png");
     74         canvas->drawImage(opaqueImage, 0.0f, 0.0f);
     75         canvas->drawImage(make_color_space(opaqueImage, wideGamut), 128.0f, 0.0f);
     76         canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear), 256.0f, 0.0f);
     77         canvas->drawImage(premulImage, 0.0f, 128.0f);
     78         canvas->drawImage(make_color_space(premulImage, wideGamut), 128.0f, 128.0f);
     79         canvas->drawImage(make_color_space(premulImage, wideGamutLinear), 256.0f, 128.0f);
     80         return DrawResult::kOk;
     81     }
     82 
     83 private:
     84     typedef skiagm::GM INHERITED;
     85 };
     86 
     87 DEF_GM(return new MakeCSGM;)
     88 
     89 DEF_SIMPLE_GM_BG(makecolortypeandspace, canvas, 128 * 3, 128 * 4, SK_ColorWHITE) {
     90     sk_sp<SkImage> images[] = {
     91         GetResourceAsImage("images/mandrill_128.png"),
     92         GetResourceAsImage("images/color_wheel.png"),
     93     };
     94     auto rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kRec2020);
     95 
     96     // Use the lazy images on the first iteration, and concrete (raster/GPU) images on the second
     97     for (bool lazy : {true, false}) {
     98         for (int j = 0; j < 2; ++j) {
     99             const SkImage* image = images[j].get();
    100             if (!image) {
    101                 // Can happen on bots that abandon the GPU context
    102                 continue;
    103             }
    104 
    105             // Unmodified
    106             canvas->drawImage(image, 0, 0);
    107 
    108             // Change the color type/space of the image in a couple ways. In both cases, codec
    109             // may fail, because we refude to decode transparent sources to opaque color types.
    110             // Guard against that, to avoid cascading failures in DDL.
    111 
    112             // 565 in a wide color space (should be visibly quantized). Fails with the color_wheel,
    113             // because of the codec issues mentioned above.
    114             auto image565 = image->makeColorTypeAndColorSpace(kRGB_565_SkColorType, rec2020);
    115             if (!lazy || image565->makeRasterImage()) {
    116                 canvas->drawImage(image565, 128, 0);
    117             }
    118 
    119             // Grayscale in the original color space. This fails in even more cases, due to the
    120             // above opaque issue, and because Ganesh doesn't support drawing to gray, at all.
    121             auto imageGray = image->makeColorTypeAndColorSpace(kGray_8_SkColorType,
    122                                                                image->refColorSpace());
    123             if (!lazy || imageGray->makeRasterImage()) {
    124                 canvas->drawImage(imageGray, 256, 0);
    125             }
    126 
    127             images[j] = canvas->getGrContext()
    128                     ? image->makeTextureImage(canvas->getGrContext(), nullptr)
    129                     : image->makeRasterImage();
    130 
    131             canvas->translate(0, 128);
    132         }
    133     }
    134 }
    135