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