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" |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 21 | #include "include/gpu/ganesh/SkSurfaceGanesh.h" |
Kevin Lubick | 9b02837 | 2023-10-05 15:04:54 -0400 | [diff] [blame] | 22 | #include "tools/GpuToolUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tools/ToolUtils.h" |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 24 | |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 25 | #if defined(SK_GRAPHITE) |
| 26 | #include "include/gpu/graphite/Surface.h" |
| 27 | #endif |
| 28 | |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 29 | namespace { |
| 30 | |
| 31 | const SkISize kSize = SkISize::Make(100, 100); |
| 32 | const SkIRect kSubset = SkIRect::MakeLTRB(25, 25, 75, 75); |
| 33 | const SkRect kDest = SkRect::MakeXYWH(10, 10, 100, 100); |
| 34 | |
| 35 | sk_sp<SkImage> make_mask(const sk_sp<SkSurface>& surface) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 36 | ToolUtils::draw_checkerboard(surface->getCanvas(), 0x80808080, 0x00000000, 5); |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 37 | return surface->makeImageSnapshot(); |
| 38 | } |
| 39 | |
| 40 | class MaskGenerator final : public SkImageGenerator { |
| 41 | public: |
| 42 | MaskGenerator(const SkImageInfo& info) : INHERITED(info) {} |
| 43 | |
Robert Phillips | 6f8ecbe | 2023-01-13 15:43:40 -0500 | [diff] [blame] | 44 | bool onGetPixels(const SkImageInfo& info, void* pixels, |
| 45 | size_t rowBytes, const Options&) override { |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 46 | SkImageInfo surfaceInfo = info; |
| 47 | if (kAlpha_8_SkColorType == info.colorType()) { |
| 48 | surfaceInfo = surfaceInfo.makeColorSpace(nullptr); |
| 49 | } |
| 50 | |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 51 | make_mask(SkSurfaces::WrapPixels(surfaceInfo, pixels, rowBytes)); |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | |
| 55 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 56 | using INHERITED = SkImageGenerator; |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | using MakerT = sk_sp<SkImage>(*)(SkCanvas*, const SkImageInfo&); |
| 60 | const MakerT makers[] = { |
Kevin Lubick | 77472bf | 2023-03-24 07:11:17 -0400 | [diff] [blame] | 61 | // SkImage_Raster |
| 62 | [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> { |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 63 | return make_mask(SkSurfaces::Raster(info)); |
Kevin Lubick | 77472bf | 2023-03-24 07:11:17 -0400 | [diff] [blame] | 64 | }, |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 65 | |
Kevin Lubick | bf174bc | 2023-03-27 11:24:20 -0400 | [diff] [blame] | 66 | // SkImage_Ganesh |
Kevin Lubick | 77472bf | 2023-03-24 07:11:17 -0400 | [diff] [blame] | 67 | [](SkCanvas* c, const SkImageInfo& info) -> sk_sp<SkImage> { |
| 68 | sk_sp<SkSurface> surface; |
| 69 | if (c->recordingContext()) { |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 70 | surface = |
| 71 | SkSurfaces::RenderTarget(c->recordingContext(), skgpu::Budgeted::kNo, info); |
Kevin Lubick | 77472bf | 2023-03-24 07:11:17 -0400 | [diff] [blame] | 72 | } else { |
Kevin Lubick | 0f7b44e | 2023-02-28 09:13:11 -0500 | [diff] [blame] | 73 | #if defined(SK_GRAPHITE) |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 74 | surface = SkSurfaces::RenderTarget(c->recorder(), info); |
Robert Phillips | 6f8ecbe | 2023-01-13 15:43:40 -0500 | [diff] [blame] | 75 | #endif |
Kevin Lubick | 77472bf | 2023-03-24 07:11:17 -0400 | [diff] [blame] | 76 | } |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 77 | return make_mask(surface ? surface : SkSurfaces::Raster(info)); |
Kevin Lubick | 77472bf | 2023-03-24 07:11:17 -0400 | [diff] [blame] | 78 | }, |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 79 | |
Kevin Lubick | 77472bf | 2023-03-24 07:11:17 -0400 | [diff] [blame] | 80 | // SkImage_Lazy |
| 81 | [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> { |
| 82 | return SkImages::DeferredFromGenerator(std::make_unique<MaskGenerator>(info)); |
| 83 | }, |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 86 | } // namespace |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 87 | |
| 88 | // Checks whether subset SkImages preserve the original color type (A8 in this case). |
| 89 | DEF_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 Derby | c37b386 | 2022-06-21 09:49:17 -0400 | [diff] [blame] | 95 | for (size_t i = 0; i < std::size(makers); ++i) { |
Robert Phillips | 0fb10ab | 2022-04-20 14:57:03 -0400 | [diff] [blame] | 96 | sk_sp<SkImage> image = ToolUtils::MakeTextureImage(canvas, makers[i](canvas, info)); |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 97 | if (image) { |
Mike Reed | d396cd5 | 2021-01-23 21:14:47 -0500 | [diff] [blame] | 98 | canvas->drawImageRect(image, SkRect::Make(kSubset), kDest, SkSamplingOptions(), |
| 99 | &paint, SkCanvas::kStrict_SrcRectConstraint); |
Robert Phillips | 6f8ecbe | 2023-01-13 15:43:40 -0500 | [diff] [blame] | 100 | sk_sp<SkImage> subset; |
| 101 | |
| 102 | if (auto direct = GrAsDirectContext(canvas->recordingContext())) { |
Kevin Lubick | 14d319b | 2023-04-27 15:43:07 -0400 | [diff] [blame] | 103 | subset = image->makeSubset(direct, kSubset); |
Robert Phillips | 6f8ecbe | 2023-01-13 15:43:40 -0500 | [diff] [blame] | 104 | } else { |
Kevin Lubick | 0f7b44e | 2023-02-28 09:13:11 -0500 | [diff] [blame] | 105 | #if defined(SK_GRAPHITE) |
Kevin Lubick | 14d319b | 2023-04-27 15:43:07 -0400 | [diff] [blame] | 106 | subset = image->makeSubset(canvas->recorder(), kSubset, {}); |
Robert Phillips | 6f8ecbe | 2023-01-13 15:43:40 -0500 | [diff] [blame] | 107 | #endif |
| 108 | } |
| 109 | |
Mike Reed | d396cd5 | 2021-01-23 21:14:47 -0500 | [diff] [blame] | 110 | canvas->drawImageRect(subset, kDest.makeOffset(kSize.width() * 1.5f, 0), |
| 111 | SkSamplingOptions(), &paint); |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 112 | } |
| 113 | canvas->translate(0, kSize.height() * 1.5f); |
| 114 | } |
| 115 | } |