blob: a5ebfacb2ff5871f1d24b5b18dd3e932242300a2 [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"
Adlai Holler872a32c2020-07-10 14:33:22 -040020#include "include/gpu/GrDirectContext.h"
Kevin Lubick5c93acf2023-05-09 12:11:43 -040021#include "include/gpu/ganesh/SkSurfaceGanesh.h"
Kevin Lubick9b028372023-10-05 15:04:54 -040022#include "tools/GpuToolUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/ToolUtils.h"
fmalitace2fc6a2016-08-26 10:13:39 -070024
Kevin Lubick5c93acf2023-05-09 12:11:43 -040025#if defined(SK_GRAPHITE)
26#include "include/gpu/graphite/Surface.h"
27#endif
28
fmalitace2fc6a2016-08-26 10:13:39 -070029namespace {
30
31const SkISize kSize = SkISize::Make(100, 100);
32const SkIRect kSubset = SkIRect::MakeLTRB(25, 25, 75, 75);
33const SkRect kDest = SkRect::MakeXYWH(10, 10, 100, 100);
34
35sk_sp<SkImage> make_mask(const sk_sp<SkSurface>& surface) {
Mike Kleinea3f0142019-03-20 11:12:10 -050036 ToolUtils::draw_checkerboard(surface->getCanvas(), 0x80808080, 0x00000000, 5);
fmalitace2fc6a2016-08-26 10:13:39 -070037 return surface->makeImageSnapshot();
38}
39
40class MaskGenerator final : public SkImageGenerator {
41public:
42 MaskGenerator(const SkImageInfo& info) : INHERITED(info) {}
43
Robert Phillips6f8ecbe2023-01-13 15:43:40 -050044 bool onGetPixels(const SkImageInfo& info, void* pixels,
45 size_t rowBytes, const Options&) override {
Mike Klein919cc452017-03-18 15:36:52 +000046 SkImageInfo surfaceInfo = info;
47 if (kAlpha_8_SkColorType == info.colorType()) {
48 surfaceInfo = surfaceInfo.makeColorSpace(nullptr);
49 }
50
Kevin Lubick5c93acf2023-05-09 12:11:43 -040051 make_mask(SkSurfaces::WrapPixels(surfaceInfo, pixels, rowBytes));
fmalitace2fc6a2016-08-26 10:13:39 -070052 return true;
53 }
54
55private:
John Stiles7571f9e2020-09-02 22:42:33 -040056 using INHERITED = SkImageGenerator;
fmalitace2fc6a2016-08-26 10:13:39 -070057};
58
59using MakerT = sk_sp<SkImage>(*)(SkCanvas*, const SkImageInfo&);
60const MakerT makers[] = {
Kevin Lubick77472bf2023-03-24 07:11:17 -040061 // SkImage_Raster
62 [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040063 return make_mask(SkSurfaces::Raster(info));
Kevin Lubick77472bf2023-03-24 07:11:17 -040064 },
fmalitace2fc6a2016-08-26 10:13:39 -070065
Kevin Lubickbf174bc2023-03-27 11:24:20 -040066 // SkImage_Ganesh
Kevin Lubick77472bf2023-03-24 07:11:17 -040067 [](SkCanvas* c, const SkImageInfo& info) -> sk_sp<SkImage> {
68 sk_sp<SkSurface> surface;
69 if (c->recordingContext()) {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040070 surface =
71 SkSurfaces::RenderTarget(c->recordingContext(), skgpu::Budgeted::kNo, info);
Kevin Lubick77472bf2023-03-24 07:11:17 -040072 } else {
Kevin Lubick0f7b44e2023-02-28 09:13:11 -050073#if defined(SK_GRAPHITE)
Kevin Lubick5c93acf2023-05-09 12:11:43 -040074 surface = SkSurfaces::RenderTarget(c->recorder(), info);
Robert Phillips6f8ecbe2023-01-13 15:43:40 -050075#endif
Kevin Lubick77472bf2023-03-24 07:11:17 -040076 }
Kevin Lubick5c93acf2023-05-09 12:11:43 -040077 return make_mask(surface ? surface : SkSurfaces::Raster(info));
Kevin Lubick77472bf2023-03-24 07:11:17 -040078 },
fmalitace2fc6a2016-08-26 10:13:39 -070079
Kevin Lubick77472bf2023-03-24 07:11:17 -040080 // SkImage_Lazy
81 [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
82 return SkImages::DeferredFromGenerator(std::make_unique<MaskGenerator>(info));
83 },
fmalitace2fc6a2016-08-26 10:13:39 -070084};
85
John Stilesa6841be2020-08-06 14:11:56 -040086} // namespace
fmalitace2fc6a2016-08-26 10:13:39 -070087
88// Checks whether subset SkImages preserve the original color type (A8 in this case).
89DEF_SIMPLE_GM(imagemasksubset, canvas, 480, 480) {
90 SkPaint paint;
91 paint.setColor(0xff00ff00);
92
93 const SkImageInfo info = SkImageInfo::MakeA8(kSize.width(), kSize.height());
94
Herb Derbyc37b3862022-06-21 09:49:17 -040095 for (size_t i = 0; i < std::size(makers); ++i) {
Robert Phillips0fb10ab2022-04-20 14:57:03 -040096 sk_sp<SkImage> image = ToolUtils::MakeTextureImage(canvas, makers[i](canvas, info));
fmalitace2fc6a2016-08-26 10:13:39 -070097 if (image) {
Mike Reedd396cd52021-01-23 21:14:47 -050098 canvas->drawImageRect(image, SkRect::Make(kSubset), kDest, SkSamplingOptions(),
99 &paint, SkCanvas::kStrict_SrcRectConstraint);
Robert Phillips6f8ecbe2023-01-13 15:43:40 -0500100 sk_sp<SkImage> subset;
101
102 if (auto direct = GrAsDirectContext(canvas->recordingContext())) {
Kevin Lubick14d319b2023-04-27 15:43:07 -0400103 subset = image->makeSubset(direct, kSubset);
Robert Phillips6f8ecbe2023-01-13 15:43:40 -0500104 } else {
Kevin Lubick0f7b44e2023-02-28 09:13:11 -0500105#if defined(SK_GRAPHITE)
Kevin Lubick14d319b2023-04-27 15:43:07 -0400106 subset = image->makeSubset(canvas->recorder(), kSubset, {});
Robert Phillips6f8ecbe2023-01-13 15:43:40 -0500107#endif
108 }
109
Mike Reedd396cd52021-01-23 21:14:47 -0500110 canvas->drawImageRect(subset, kDest.makeOffset(kSize.width() * 1.5f, 0),
111 SkSamplingOptions(), &paint);
fmalitace2fc6a2016-08-26 10:13:39 -0700112 }
113 canvas->translate(0, kSize.height() * 1.5f);
114 }
115}