Robert Phillips | 8bb3b21 | 2017-02-06 12:32:55 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 "SkImageSource.h" |
| 10 | #include "SkMagnifierImageFilter.h" |
| 11 | #include "SkSurface.h" |
| 12 | |
| 13 | static sk_sp<SkImage> make_image(GrContext* context, int size, GrSurfaceOrigin origin) { |
| 14 | if (context) { |
| 15 | SkImageInfo ii = SkImageInfo::Make(size, size, kN32_SkColorType, kPremul_SkAlphaType); |
| 16 | sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, 0, |
| 17 | origin, nullptr)); |
Robert Phillips | 92d20a0 | 2017-02-09 12:22:21 -0500 | [diff] [blame^] | 18 | if (!surf) { |
| 19 | return nullptr; |
| 20 | } |
Robert Phillips | 8bb3b21 | 2017-02-06 12:32:55 -0500 | [diff] [blame] | 21 | |
| 22 | SkCanvas* canvas = surf->getCanvas(); |
| 23 | |
| 24 | canvas->clear(SK_ColorRED); |
| 25 | canvas->drawPoint(1.5f, 1.5f, SK_ColorGREEN); |
| 26 | canvas->drawPoint(2.5f, 1.5f, SK_ColorBLUE); |
| 27 | canvas->drawPoint(1.5f, 2.5f, SK_ColorCYAN); |
| 28 | canvas->drawPoint(2.5f, 2.5f, SK_ColorGRAY); |
| 29 | |
| 30 | return surf->makeImageSnapshot(); |
| 31 | } else { |
| 32 | SkBitmap bm; |
| 33 | bm.allocN32Pixels(size, size); |
| 34 | bm.eraseRGB(255, 0, 0); |
| 35 | *bm.getAddr32(1, 1) = SkPackARGB32(0xFF, 0x00, 0xFF, 0x00); |
| 36 | *bm.getAddr32(2, 1) = SkPackARGB32(0xFF, 0x00, 0x00, 0xFF); |
| 37 | *bm.getAddr32(1, 2) = SkPackARGB32(0xFF, 0x00, 0xFF, 0xFF); |
| 38 | *bm.getAddr32(2, 2) = SkPackARGB32(0xFF, 0x88, 0x88, 0x88); |
| 39 | |
| 40 | return SkImage::MakeFromBitmap(bm); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * This GM creates an image with a 2x2: |
| 46 | * Green | Blue |
| 47 | * ------------ |
| 48 | * Cyan | Gray |
| 49 | * block of pixels in one corner of a 33x33 field. The 'srcRect' feature of the |
| 50 | * SkMagnifierImageFilter is then used to blow it up with different inset border widths. |
| 51 | * |
| 52 | * In GPU-mode we wind up drawing 4 rects: |
| 53 | * |
| 54 | * BottomLeft origin + 1-wide inset | TopLeft origin + 1-wide inset |
| 55 | * ---------------------------------------------------------------- |
| 56 | * BottomLeft origin + 7-wide inset | TopLeft origin + 7-wide inset |
| 57 | * |
| 58 | * In Raster-mode the source origin isn't used. |
| 59 | */ |
| 60 | class SimpleMagnificationGM : public skiagm::GM { |
| 61 | public: |
| 62 | SimpleMagnificationGM() { |
| 63 | this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 64 | } |
| 65 | |
| 66 | protected: |
| 67 | SkString onShortName() override { |
| 68 | return SkString("simple-magnification"); |
| 69 | } |
| 70 | |
| 71 | SkISize onISize() override { |
| 72 | return SkISize::Make(3*kPad+2*kImgSize, 3*kPad+2*kImgSize); |
| 73 | } |
| 74 | |
| 75 | void draw(SkCanvas* canvas, sk_sp<SkImage> image, const SkIPoint& offset, int inset) { |
| 76 | sk_sp<SkImageFilter> imgSrc(SkImageSource::Make(std::move(image))); |
| 77 | |
| 78 | SkRect srcRect = SkRect::MakeXYWH(1.0f, 1.0f, 2.0f, 2.0f); |
| 79 | sk_sp<SkImageFilter> magFilter(SkMagnifierImageFilter::Make(srcRect, inset, imgSrc)); |
| 80 | |
| 81 | SkPaint paint; |
| 82 | paint.setImageFilter(std::move(magFilter)); |
| 83 | |
| 84 | canvas->save(); |
| 85 | canvas->translate(offset.fX, offset.fY); |
| 86 | SkRect rect = SkRect::MakeWH(kImgSize, kImgSize); |
| 87 | canvas->drawRect(rect, paint); |
| 88 | |
| 89 | canvas->restore(); |
| 90 | } |
| 91 | |
| 92 | void onDraw(SkCanvas* canvas) override { |
| 93 | GrContext* context = canvas->getGrContext(); |
| 94 | |
| 95 | sk_sp<SkImage> bottomLImg = make_image(context, kImgSize, kBottomLeft_GrSurfaceOrigin); |
| 96 | sk_sp<SkImage> topLImg = make_image(context, kImgSize, kTopLeft_GrSurfaceOrigin); |
Robert Phillips | 92d20a0 | 2017-02-09 12:22:21 -0500 | [diff] [blame^] | 97 | if (!bottomLImg || !topLImg) { |
| 98 | return; |
| 99 | } |
Robert Phillips | 8bb3b21 | 2017-02-06 12:32:55 -0500 | [diff] [blame] | 100 | |
| 101 | int bigOffset = 2 * kPad + kImgSize; |
| 102 | |
| 103 | this->draw(canvas, bottomLImg, SkIPoint::Make(kPad, kPad), 1); |
| 104 | this->draw(canvas, topLImg, SkIPoint::Make(bigOffset, kPad), 1); |
| 105 | this->draw(canvas, bottomLImg, SkIPoint::Make(kPad, bigOffset), 7); |
| 106 | this->draw(canvas, topLImg, SkIPoint::Make(bigOffset, bigOffset), 7); |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | static const int kImgSize = 33; |
| 111 | static const int kPad = 2; |
| 112 | |
| 113 | typedef skiagm::GM INHERITED; |
| 114 | }; |
| 115 | |
| 116 | ////////////////////////////////////////////////////////////////////////////// |
| 117 | |
| 118 | DEF_GM(return new SimpleMagnificationGM;) |