blob: 49ff3c540596276ad76b6433c66daebdb9f0cba5 [file] [log] [blame]
fmalitace2fc6a2016-08-26 10:13:39 -07001/*
2 * Copyright 2016 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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
fmalitace2fc6a2016-08-26 10:13:39 -070010#include "SkCanvas.h"
11#include "SkImage.h"
12#include "SkImageGenerator.h"
Mike Reed185130c2017-02-15 15:14:16 -050013#include "SkMakeUnique.h"
fmalitace2fc6a2016-08-26 10:13:39 -070014#include "SkSurface.h"
fmalitace2fc6a2016-08-26 10:13:39 -070015
16namespace {
17
18const SkISize kSize = SkISize::Make(100, 100);
19const SkIRect kSubset = SkIRect::MakeLTRB(25, 25, 75, 75);
20const SkRect kDest = SkRect::MakeXYWH(10, 10, 100, 100);
21
22sk_sp<SkImage> make_mask(const sk_sp<SkSurface>& surface) {
23 sk_tool_utils::draw_checkerboard(surface->getCanvas(), 0x80808080, 0x00000000, 5);
24 return surface->makeImageSnapshot();
25}
26
27class MaskGenerator final : public SkImageGenerator {
28public:
29 MaskGenerator(const SkImageInfo& info) : INHERITED(info) {}
30
31 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor*,
32 int*) override {
33 if (info.colorType() == kIndex_8_SkColorType) {
34 return false;
35 }
36
Mike Klein919cc452017-03-18 15:36:52 +000037 SkImageInfo surfaceInfo = info;
38 if (kAlpha_8_SkColorType == info.colorType()) {
39 surfaceInfo = surfaceInfo.makeColorSpace(nullptr);
40 }
41
42 make_mask(SkSurface::MakeRasterDirect(surfaceInfo, pixels, rowBytes));
fmalitace2fc6a2016-08-26 10:13:39 -070043 return true;
44 }
45
46private:
47 typedef SkImageGenerator INHERITED;
48};
49
50using MakerT = sk_sp<SkImage>(*)(SkCanvas*, const SkImageInfo&);
51const MakerT makers[] = {
52 // SkImage_Raster
53 [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
54 return make_mask(SkSurface::MakeRaster(info));
55 },
56
57 // SkImage_Gpu
58 [](SkCanvas* c, const SkImageInfo& info) -> sk_sp<SkImage> {
59 sk_sp<SkSurface> surface;
60#if SK_SUPPORT_GPU
61 surface = SkSurface::MakeRenderTarget(c->getGrContext(), SkBudgeted::kNo, info);
62#endif
63 return make_mask(surface ? surface : SkSurface::MakeRaster(info));
64 },
65
66 // SkImage_Generator
67 [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
Mike Reed185130c2017-02-15 15:14:16 -050068 return SkImage::MakeFromGenerator(skstd::make_unique<MaskGenerator>(info));
fmalitace2fc6a2016-08-26 10:13:39 -070069 },
70};
71
72} // anonymous ns
73
74// Checks whether subset SkImages preserve the original color type (A8 in this case).
75DEF_SIMPLE_GM(imagemasksubset, canvas, 480, 480) {
76 SkPaint paint;
77 paint.setColor(0xff00ff00);
78
79 const SkImageInfo info = SkImageInfo::MakeA8(kSize.width(), kSize.height());
80
81 for (size_t i = 0; i < SK_ARRAY_COUNT(makers); ++i) {
82 sk_sp<SkImage> image = makers[i](canvas, info);
83 if (image) {
84 canvas->drawImageRect(image, SkRect::Make(kSubset), kDest, &paint);
85 sk_sp<SkImage> subset = image->makeSubset(kSubset);
86 canvas->drawImageRect(subset, kDest.makeOffset(kSize.width() * 1.5f, 0), &paint);
87 }
88 canvas->translate(0, kSize.height() * 1.5f);
89 }
90}