Debian Codegraph @

gm/imagemasksubset.cpp

raw
1 /*
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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkImageGenerator.h"
13 #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"
18 #include "include/core/SkSurface.h"
19 #include "include/core/SkTypes.h"
20 #include "tools/GpuToolUtils.h"
21 #include "tools/ToolUtils.h"
22
23 #if defined(SK_GANESH)
24 #include "include/gpu/ganesh/GrDirectContext.h"
25 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
26 #endif
27
28 #if defined(SK_GRAPHITE)
29 #include "include/gpu/graphite/Surface.h"
30 #endif
31
32 namespace {
33
34 const SkISize kSize = SkISize::Make(100, 100);
35 const SkIRect kSubset = SkIRect::MakeLTRB(25, 25, 75, 75);
36 const SkRect kDest = SkRect::MakeXYWH(10, 10, 100, 100);
37
38 sk_sp<SkImage> make_mask(const sk_sp<SkSurface>& surface) {
39 ToolUtils::draw_checkerboard(surface->getCanvas(), 0x80808080, 0x00000000, 5);
40 return surface->makeImageSnapshot();
41 }
42
43 class MaskGenerator final : public SkImageGenerator {
44 public:
45 MaskGenerator(const SkImageInfo& info) : INHERITED(info) {}
46
47 bool onGetPixels(const SkImageInfo& info, void* pixels,
48 size_t rowBytes, const Options&) override {
49 SkImageInfo surfaceInfo = info;
50 if (kAlpha_8_SkColorType == info.colorType()) {
51 surfaceInfo = surfaceInfo.makeColorSpace(nullptr);
52 }
53
54 make_mask(SkSurfaces::WrapPixels(surfaceInfo, pixels, rowBytes));
55 return true;
56 }
57
58 private:
59 using INHERITED = SkImageGenerator;
60 };
61
62 using MakerT = sk_sp<SkImage>(*)(SkCanvas*, const SkImageInfo&);
63 const MakerT makers[] = {
64 // SkImage_Raster
65 [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
66 return make_mask(SkSurfaces::Raster(info));
67 },
68
69 // SkImage_Ganesh
70 [](SkCanvas* c, const SkImageInfo& info) -> sk_sp<SkImage> {
71 sk_sp<SkSurface> surface;
72 #if defined(SK_GANESH)
73 if (auto rc = c->recordingContext()) {
74 surface = SkSurfaces::RenderTarget(rc, skgpu::Budgeted::kNo, info);
75 }
76 #endif
77 #if defined(SK_GRAPHITE)
78 if (auto r = c->recorder()) {
79 surface = SkSurfaces::RenderTarget(r, info);
80 }
81 #endif
82 return make_mask(surface ? surface : SkSurfaces::Raster(info));
83 },
84
85 // SkImage_Lazy
86 [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
87 return SkImages::DeferredFromGenerator(std::make_unique<MaskGenerator>(info));
88 },
89 };
90
91 } // namespace
92
93 // Checks whether subset SkImages preserve the original color type (A8 in this case).
94 DEF_SIMPLE_GM(imagemasksubset, canvas, 480, 480) {
95 SkPaint paint;
96 paint.setColor(0xff00ff00);
97
98 const SkImageInfo info = SkImageInfo::MakeA8(kSize.width(), kSize.height());
99
100 for (size_t i = 0; i < std::size(makers); ++i) {
101 sk_sp<SkImage> image = ToolUtils::MakeTextureImage(canvas, makers[i](canvas, info));
102 if (image) {
103 canvas->drawImageRect(image, SkRect::Make(kSubset), kDest, SkSamplingOptions(),
104 &paint, SkCanvas::kStrict_SrcRectConstraint);
105 sk_sp<SkImage> subset = image->makeSubset(canvas->baseRecorder(), kSubset, {});
106
107 canvas->drawImageRect(subset, kDest.makeOffset(kSize.width() * 1.5f, 0),
108 SkSamplingOptions(), &paint);
109 }
110 canvas->translate(0, kSize.height() * 1.5f);
111 }
112 }