blob: 3e41798264d9548a4bebde23d7bab4db9979ced7 [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"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040024#include "include/effects/SkImageFilters.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "include/gpu/GrTypes.h"
26
27#include <utility>
28
Robert Phillips16bf7d32020-07-07 10:20:27 -040029static sk_sp<SkImage> make_image(GrRecordingContext* context, int size, GrSurfaceOrigin origin) {
Robert Phillips8bb3b212017-02-06 12:32:55 -050030 if (context) {
31 SkImageInfo ii = SkImageInfo::Make(size, size, kN32_SkColorType, kPremul_SkAlphaType);
32 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, 0,
33 origin, nullptr));
Mike Klein290690c2019-04-02 18:11:57 -040034 if (surf) {
35 SkCanvas* canvas = surf->getCanvas();
36
37 canvas->clear(SK_ColorRED);
38 const struct {
39 SkPoint fPt;
40 SkColor fColor;
41 } rec[] = {
42 { { 1.5f, 1.5f }, SK_ColorGREEN },
43 { { 2.5f, 1.5f }, SK_ColorBLUE },
44 { { 1.5f, 2.5f }, SK_ColorCYAN },
45 { { 2.5f, 2.5f }, SK_ColorGRAY },
46 };
47 SkPaint paint;
48 for (const auto& r : rec) {
49 paint.setColor(r.fColor);
50 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, &r.fPt, paint);
51 }
52 return surf->makeImageSnapshot();
Robert Phillips92d20a02017-02-09 12:22:21 -050053 }
Robert Phillips8bb3b212017-02-06 12:32:55 -050054 }
Mike Klein290690c2019-04-02 18:11:57 -040055
56 SkBitmap bm;
57 bm.allocN32Pixels(size, size);
58 bm.eraseColor(SK_ColorRED);
59 *bm.getAddr32(1, 1) = SkPackARGB32(0xFF, 0x00, 0xFF, 0x00);
60 *bm.getAddr32(2, 1) = SkPackARGB32(0xFF, 0x00, 0x00, 0xFF);
61 *bm.getAddr32(1, 2) = SkPackARGB32(0xFF, 0x00, 0xFF, 0xFF);
62 *bm.getAddr32(2, 2) = SkPackARGB32(0xFF, 0x88, 0x88, 0x88);
63 return SkImage::MakeFromBitmap(bm);
Robert Phillips8bb3b212017-02-06 12:32:55 -050064}
65
66/*
67 * This GM creates an image with a 2x2:
68 * Green | Blue
69 * ------------
70 * Cyan | Gray
71 * block of pixels in one corner of a 33x33 field. The 'srcRect' feature of the
72 * SkMagnifierImageFilter is then used to blow it up with different inset border widths.
73 *
74 * In GPU-mode we wind up drawing 4 rects:
75 *
76 * BottomLeft origin + 1-wide inset | TopLeft origin + 1-wide inset
77 * ----------------------------------------------------------------
78 * BottomLeft origin + 7-wide inset | TopLeft origin + 7-wide inset
79 *
80 * In Raster-mode the source origin isn't used.
81 */
82class SimpleMagnificationGM : public skiagm::GM {
83public:
84 SimpleMagnificationGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040085 this->setBGColor(0xFFCCCCCC);
Robert Phillips8bb3b212017-02-06 12:32:55 -050086 }
87
88protected:
89 SkString onShortName() override {
90 return SkString("simple-magnification");
91 }
92
93 SkISize onISize() override {
94 return SkISize::Make(3*kPad+2*kImgSize, 3*kPad+2*kImgSize);
95 }
96
97 void draw(SkCanvas* canvas, sk_sp<SkImage> image, const SkIPoint& offset, int inset) {
Michael Ludwig898bbfa2019-08-02 15:21:23 -040098 sk_sp<SkImageFilter> imgSrc(SkImageFilters::Image(std::move(image)));
Robert Phillips8bb3b212017-02-06 12:32:55 -050099
100 SkRect srcRect = SkRect::MakeXYWH(1.0f, 1.0f, 2.0f, 2.0f);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400101 sk_sp<SkImageFilter> magFilter(SkImageFilters::Magnifier(srcRect, inset, imgSrc));
Robert Phillips8bb3b212017-02-06 12:32:55 -0500102
103 SkPaint paint;
104 paint.setImageFilter(std::move(magFilter));
105
106 canvas->save();
107 canvas->translate(offset.fX, offset.fY);
108 SkRect rect = SkRect::MakeWH(kImgSize, kImgSize);
109 canvas->drawRect(rect, paint);
110
111 canvas->restore();
112 }
113
Chris Dalton50e24d72019-02-07 16:20:09 -0700114 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
Robert Phillips16bf7d32020-07-07 10:20:27 -0400115 auto context = canvas->recordingContext();
Robert Phillips8bb3b212017-02-06 12:32:55 -0500116
117 sk_sp<SkImage> bottomLImg = make_image(context, kImgSize, kBottomLeft_GrSurfaceOrigin);
118 sk_sp<SkImage> topLImg = make_image(context, kImgSize, kTopLeft_GrSurfaceOrigin);
Robert Phillips92d20a02017-02-09 12:22:21 -0500119 if (!bottomLImg || !topLImg) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700120 *errorMsg = "Could not load images. Did you forget to set the resourcePath?";
121 return DrawResult::kFail;
Robert Phillips92d20a02017-02-09 12:22:21 -0500122 }
Robert Phillips8bb3b212017-02-06 12:32:55 -0500123
124 int bigOffset = 2 * kPad + kImgSize;
125
126 this->draw(canvas, bottomLImg, SkIPoint::Make(kPad, kPad), 1);
127 this->draw(canvas, topLImg, SkIPoint::Make(bigOffset, kPad), 1);
128 this->draw(canvas, bottomLImg, SkIPoint::Make(kPad, bigOffset), 7);
129 this->draw(canvas, topLImg, SkIPoint::Make(bigOffset, bigOffset), 7);
Chris Dalton50e24d72019-02-07 16:20:09 -0700130 return DrawResult::kOk;
Robert Phillips8bb3b212017-02-06 12:32:55 -0500131 }
132
133private:
134 static const int kImgSize = 33;
135 static const int kPad = 2;
136
137 typedef skiagm::GM INHERITED;
138};
139
140//////////////////////////////////////////////////////////////////////////////
141
142DEF_GM(return new SimpleMagnificationGM;)