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 | |
| 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkImage.h" |
| 12 | #include "SkImageGenerator.h" |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 13 | #include "SkMakeUnique.h" |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 14 | #include "SkSurface.h" |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 15 | |
| 16 | namespace { |
| 17 | |
| 18 | const SkISize kSize = SkISize::Make(100, 100); |
| 19 | const SkIRect kSubset = SkIRect::MakeLTRB(25, 25, 75, 75); |
| 20 | const SkRect kDest = SkRect::MakeXYWH(10, 10, 100, 100); |
| 21 | |
| 22 | sk_sp<SkImage> make_mask(const sk_sp<SkSurface>& surface) { |
| 23 | sk_tool_utils::draw_checkerboard(surface->getCanvas(), 0x80808080, 0x00000000, 5); |
| 24 | return surface->makeImageSnapshot(); |
| 25 | } |
| 26 | |
| 27 | class MaskGenerator final : public SkImageGenerator { |
| 28 | public: |
| 29 | MaskGenerator(const SkImageInfo& info) : INHERITED(info) {} |
| 30 | |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 31 | bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options&) |
| 32 | override { |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 33 | SkImageInfo surfaceInfo = info; |
| 34 | if (kAlpha_8_SkColorType == info.colorType()) { |
| 35 | surfaceInfo = surfaceInfo.makeColorSpace(nullptr); |
| 36 | } |
| 37 | |
| 38 | make_mask(SkSurface::MakeRasterDirect(surfaceInfo, pixels, rowBytes)); |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 39 | return true; |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | typedef SkImageGenerator INHERITED; |
| 44 | }; |
| 45 | |
| 46 | using MakerT = sk_sp<SkImage>(*)(SkCanvas*, const SkImageInfo&); |
| 47 | const MakerT makers[] = { |
| 48 | // SkImage_Raster |
| 49 | [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> { |
| 50 | return make_mask(SkSurface::MakeRaster(info)); |
| 51 | }, |
| 52 | |
| 53 | // SkImage_Gpu |
| 54 | [](SkCanvas* c, const SkImageInfo& info) -> sk_sp<SkImage> { |
| 55 | sk_sp<SkSurface> surface; |
| 56 | #if SK_SUPPORT_GPU |
| 57 | surface = SkSurface::MakeRenderTarget(c->getGrContext(), SkBudgeted::kNo, info); |
| 58 | #endif |
| 59 | return make_mask(surface ? surface : SkSurface::MakeRaster(info)); |
| 60 | }, |
| 61 | |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 62 | // SkImage_Lazy |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 63 | [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> { |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 64 | return SkImage::MakeFromGenerator(skstd::make_unique<MaskGenerator>(info)); |
fmalita | ce2fc6a | 2016-08-26 10:13:39 -0700 | [diff] [blame] | 65 | }, |
| 66 | }; |
| 67 | |
| 68 | } // anonymous ns |
| 69 | |
| 70 | // Checks whether subset SkImages preserve the original color type (A8 in this case). |
| 71 | DEF_SIMPLE_GM(imagemasksubset, canvas, 480, 480) { |
| 72 | SkPaint paint; |
| 73 | paint.setColor(0xff00ff00); |
| 74 | |
| 75 | const SkImageInfo info = SkImageInfo::MakeA8(kSize.width(), kSize.height()); |
| 76 | |
| 77 | for (size_t i = 0; i < SK_ARRAY_COUNT(makers); ++i) { |
| 78 | sk_sp<SkImage> image = makers[i](canvas, info); |
| 79 | if (image) { |
| 80 | canvas->drawImageRect(image, SkRect::Make(kSubset), kDest, &paint); |
| 81 | sk_sp<SkImage> subset = image->makeSubset(kSubset); |
| 82 | canvas->drawImageRect(subset, kDest.makeOffset(kSize.width() * 1.5f, 0), &paint); |
| 83 | } |
| 84 | canvas->translate(0, kSize.height() * 1.5f); |
| 85 | } |
| 86 | } |