blob: a1634034afbde8becf7c355e278b95fabe324723 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColorSpace.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkImage.h"
12#include "include/core/SkImageGenerator.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkImageInfo.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkSize.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "tools/ToolUtils.h"
fmalitace2fc6a2016-08-26 10:13:39 -070021
22namespace {
23
24const SkISize kSize = SkISize::Make(100, 100);
25const SkIRect kSubset = SkIRect::MakeLTRB(25, 25, 75, 75);
26const SkRect kDest = SkRect::MakeXYWH(10, 10, 100, 100);
27
28sk_sp<SkImage> make_mask(const sk_sp<SkSurface>& surface) {
Mike Kleinea3f0142019-03-20 11:12:10 -050029 ToolUtils::draw_checkerboard(surface->getCanvas(), 0x80808080, 0x00000000, 5);
fmalitace2fc6a2016-08-26 10:13:39 -070030 return surface->makeImageSnapshot();
31}
32
33class MaskGenerator final : public SkImageGenerator {
34public:
35 MaskGenerator(const SkImageInfo& info) : INHERITED(info) {}
36
Matt Sarettebb1b5c2017-05-12 11:41:27 -040037 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options&)
38 override {
Mike Klein919cc452017-03-18 15:36:52 +000039 SkImageInfo surfaceInfo = info;
40 if (kAlpha_8_SkColorType == info.colorType()) {
41 surfaceInfo = surfaceInfo.makeColorSpace(nullptr);
42 }
43
44 make_mask(SkSurface::MakeRasterDirect(surfaceInfo, pixels, rowBytes));
fmalitace2fc6a2016-08-26 10:13:39 -070045 return true;
46 }
47
48private:
49 typedef SkImageGenerator INHERITED;
50};
51
52using MakerT = sk_sp<SkImage>(*)(SkCanvas*, const SkImageInfo&);
53const MakerT makers[] = {
54 // SkImage_Raster
55 [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
56 return make_mask(SkSurface::MakeRaster(info));
57 },
58
59 // SkImage_Gpu
60 [](SkCanvas* c, const SkImageInfo& info) -> sk_sp<SkImage> {
61 sk_sp<SkSurface> surface;
fmalitace2fc6a2016-08-26 10:13:39 -070062 surface = SkSurface::MakeRenderTarget(c->getGrContext(), SkBudgeted::kNo, info);
fmalitace2fc6a2016-08-26 10:13:39 -070063 return make_mask(surface ? surface : SkSurface::MakeRaster(info));
64 },
65
Brian Osmanf1b43822017-04-20 13:43:23 -040066 // SkImage_Lazy
fmalitace2fc6a2016-08-26 10:13:39 -070067 [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050068 return SkImage::MakeFromGenerator(std::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}