fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColorSpace.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkImage.h" |
| 12 | #include "include/core/SkImageGenerator.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 19 | #include "include/core/SkTypes.h" |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 20 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "tools/ToolUtils.h" |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
| 25 | const SkISize kSize = SkISize::Make(100, 100); |
| 26 | const SkIRect kSubset = SkIRect::MakeLTRB(25, 25, 75, 75); |
| 27 | const SkRect kDest = SkRect::MakeXYWH(10, 10, 100, 100); |
| 28 | |
| 29 | sk_sp<SkImage> make_mask(const sk_sp<SkSurface>& surface) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 30 | ToolUtils::draw_checkerboard(surface->getCanvas(), 0x80808080, 0x00000000, 5); |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 31 | return surface->makeImageSnapshot(); |
| 32 | } |
| 33 | |
| 34 | class MaskGenerator final : public SkImageGenerator { |
| 35 | public: |
| 36 | MaskGenerator(const SkImageInfo& info) : INHERITED(info) {} |
| 37 | |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 38 | bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options&) |
| 39 | override { |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 40 | SkImageInfo surfaceInfo = info; |
| 41 | if (kAlpha_8_SkColorType == info.colorType()) { |
| 42 | surfaceInfo = surfaceInfo.makeColorSpace(nullptr); |
| 43 | } |
| 44 | |
| 45 | make_mask(SkSurface::MakeRasterDirect(surfaceInfo, pixels, rowBytes)); |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 46 | return true; |
| 47 | } |
| 48 | |
| 49 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 50 | using INHERITED = SkImageGenerator; |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | using MakerT = sk_sp<SkImage>(*)(SkCanvas*, const SkImageInfo&); |
| 54 | const MakerT makers[] = { |
| 55 | // SkImage_Raster |
| 56 | [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> { |
| 57 | return make_mask(SkSurface::MakeRaster(info)); |
| 58 | }, |
| 59 | |
| 60 | // SkImage_Gpu |
| 61 | [](SkCanvas* c, const SkImageInfo& info) -> sk_sp<SkImage> { |
| 62 | sk_sp<SkSurface> surface; |
Robert Phillips | 16bf7d3 | 2020-07-07 10:20:27 -0400 | [diff] [blame] | 63 | surface = SkSurface::MakeRenderTarget(c->recordingContext(), SkBudgeted::kNo, info); |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 64 | return make_mask(surface ? surface : SkSurface::MakeRaster(info)); |
| 65 | }, |
| 66 | |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 67 | // SkImage_Lazy |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 68 | [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> { |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 69 | return SkImage::MakeFromGenerator(std::make_unique<MaskGenerator>(info)); |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 70 | }, |
| 71 | }; |
| 72 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 73 | } // namespace |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 74 | |
| 75 | // Checks whether subset SkImages preserve the original color type (A8 in this case). |
| 76 | DEF_SIMPLE_GM(imagemasksubset, canvas, 480, 480) { |
| 77 | SkPaint paint; |
| 78 | paint.setColor(0xff00ff00); |
| 79 | |
| 80 | const SkImageInfo info = SkImageInfo::MakeA8(kSize.width(), kSize.height()); |
| 81 | |
| 82 | for (size_t i = 0; i < SK_ARRAY_COUNT(makers); ++i) { |
| 83 | sk_sp<SkImage> image = makers[i](canvas, info); |
| 84 | if (image) { |
Mike Reed | d396cd5 | 2021-01-23 21:14:47 -0500 | [diff] [blame] | 85 | canvas->drawImageRect(image, SkRect::Make(kSubset), kDest, SkSamplingOptions(), |
| 86 | &paint, SkCanvas::kStrict_SrcRectConstraint); |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 87 | auto direct = GrAsDirectContext(canvas->recordingContext()); |
| 88 | sk_sp<SkImage> subset = image->makeSubset(kSubset, direct); |
Mike Reed | d396cd5 | 2021-01-23 21:14:47 -0500 | [diff] [blame] | 89 | canvas->drawImageRect(subset, kDest.makeOffset(kSize.width() * 1.5f, 0), |
| 90 | SkSamplingOptions(), &paint); |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 91 | } |
| 92 | canvas->translate(0, kSize.height() * 1.5f); |
| 93 | } |
| 94 | } |