blob: 3c0a63605000c5e41a3212ba414a9805e1743f2b [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkColorPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkImage.h"
14#include "include/core/SkImageFilter.h"
15#include "include/core/SkImageInfo.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkPoint.h"
18#include "include/core/SkRect.h"
19#include "include/core/SkRefCnt.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/effects/SkImageSource.h"
25#include "include/effects/SkMagnifierImageFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "include/gpu/GrTypes.h"
27
28#include <utility>
29
30class GrContext;
Robert Phillips8bb3b212017-02-06 12:32:55 -050031
32static sk_sp<SkImage> make_image(GrContext* context, int size, GrSurfaceOrigin origin) {
33 if (context) {
34 SkImageInfo ii = SkImageInfo::Make(size, size, kN32_SkColorType, kPremul_SkAlphaType);
35 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, 0,
36 origin, nullptr));
Mike Klein290690c2019-04-02 18:11:57 -040037 if (surf) {
38 SkCanvas* canvas = surf->getCanvas();
39
40 canvas->clear(SK_ColorRED);
41 const struct {
42 SkPoint fPt;
43 SkColor fColor;
44 } rec[] = {
45 { { 1.5f, 1.5f }, SK_ColorGREEN },
46 { { 2.5f, 1.5f }, SK_ColorBLUE },
47 { { 1.5f, 2.5f }, SK_ColorCYAN },
48 { { 2.5f, 2.5f }, SK_ColorGRAY },
49 };
50 SkPaint paint;
51 for (const auto& r : rec) {
52 paint.setColor(r.fColor);
53 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, &r.fPt, paint);
54 }
55 return surf->makeImageSnapshot();
Robert Phillips92d20a02017-02-09 12:22:21 -050056 }
Robert Phillips8bb3b212017-02-06 12:32:55 -050057 }
Mike Klein290690c2019-04-02 18:11:57 -040058
59 SkBitmap bm;
60 bm.allocN32Pixels(size, size);
61 bm.eraseColor(SK_ColorRED);
62 *bm.getAddr32(1, 1) = SkPackARGB32(0xFF, 0x00, 0xFF, 0x00);
63 *bm.getAddr32(2, 1) = SkPackARGB32(0xFF, 0x00, 0x00, 0xFF);
64 *bm.getAddr32(1, 2) = SkPackARGB32(0xFF, 0x00, 0xFF, 0xFF);
65 *bm.getAddr32(2, 2) = SkPackARGB32(0xFF, 0x88, 0x88, 0x88);
66 return SkImage::MakeFromBitmap(bm);
Robert Phillips8bb3b212017-02-06 12:32:55 -050067}
68
69/*
70 * This GM creates an image with a 2x2:
71 * Green | Blue
72 * ------------
73 * Cyan | Gray
74 * block of pixels in one corner of a 33x33 field. The 'srcRect' feature of the
75 * SkMagnifierImageFilter is then used to blow it up with different inset border widths.
76 *
77 * In GPU-mode we wind up drawing 4 rects:
78 *
79 * BottomLeft origin + 1-wide inset | TopLeft origin + 1-wide inset
80 * ----------------------------------------------------------------
81 * BottomLeft origin + 7-wide inset | TopLeft origin + 7-wide inset
82 *
83 * In Raster-mode the source origin isn't used.
84 */
85class SimpleMagnificationGM : public skiagm::GM {
86public:
87 SimpleMagnificationGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040088 this->setBGColor(0xFFCCCCCC);
Robert Phillips8bb3b212017-02-06 12:32:55 -050089 }
90
91protected:
92 SkString onShortName() override {
93 return SkString("simple-magnification");
94 }
95
96 SkISize onISize() override {
97 return SkISize::Make(3*kPad+2*kImgSize, 3*kPad+2*kImgSize);
98 }
99
100 void draw(SkCanvas* canvas, sk_sp<SkImage> image, const SkIPoint& offset, int inset) {
101 sk_sp<SkImageFilter> imgSrc(SkImageSource::Make(std::move(image)));
102
103 SkRect srcRect = SkRect::MakeXYWH(1.0f, 1.0f, 2.0f, 2.0f);
104 sk_sp<SkImageFilter> magFilter(SkMagnifierImageFilter::Make(srcRect, inset, imgSrc));
105
106 SkPaint paint;
107 paint.setImageFilter(std::move(magFilter));
108
109 canvas->save();
110 canvas->translate(offset.fX, offset.fY);
111 SkRect rect = SkRect::MakeWH(kImgSize, kImgSize);
112 canvas->drawRect(rect, paint);
113
114 canvas->restore();
115 }
116
Chris Dalton50e24d72019-02-07 16:20:09 -0700117 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
Robert Phillips8bb3b212017-02-06 12:32:55 -0500118 GrContext* context = canvas->getGrContext();
119
120 sk_sp<SkImage> bottomLImg = make_image(context, kImgSize, kBottomLeft_GrSurfaceOrigin);
121 sk_sp<SkImage> topLImg = make_image(context, kImgSize, kTopLeft_GrSurfaceOrigin);
Robert Phillips92d20a02017-02-09 12:22:21 -0500122 if (!bottomLImg || !topLImg) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700123 *errorMsg = "Could not load images. Did you forget to set the resourcePath?";
124 return DrawResult::kFail;
Robert Phillips92d20a02017-02-09 12:22:21 -0500125 }
Robert Phillips8bb3b212017-02-06 12:32:55 -0500126
127 int bigOffset = 2 * kPad + kImgSize;
128
129 this->draw(canvas, bottomLImg, SkIPoint::Make(kPad, kPad), 1);
130 this->draw(canvas, topLImg, SkIPoint::Make(bigOffset, kPad), 1);
131 this->draw(canvas, bottomLImg, SkIPoint::Make(kPad, bigOffset), 7);
132 this->draw(canvas, topLImg, SkIPoint::Make(bigOffset, bigOffset), 7);
Chris Dalton50e24d72019-02-07 16:20:09 -0700133 return DrawResult::kOk;
Robert Phillips8bb3b212017-02-06 12:32:55 -0500134 }
135
136private:
137 static const int kImgSize = 33;
138 static const int kPad = 2;
139
140 typedef skiagm::GM INHERITED;
141};
142
143//////////////////////////////////////////////////////////////////////////////
144
145DEF_GM(return new SimpleMagnificationGM;)