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