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