1 /*
2 * Copyright 2013 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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageFilter.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPicture.h"
17 #include "include/core/SkPictureRecorder.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkTypeface.h"
24 #include "include/effects/SkImageFilters.h"
25 #include "tools/ToolUtils.h"
26
27 // This GM exercises the SkPictureImageFilter ImageFilter class.
28
fill_rect_filtered(SkCanvas * canvas,const SkRect & clipRect,sk_sp<SkImageFilter> filter)29 static void fill_rect_filtered(SkCanvas* canvas,
30 const SkRect& clipRect,
31 sk_sp<SkImageFilter> filter) {
32 SkPaint paint;
33 paint.setImageFilter(filter);
34 canvas->save();
35 canvas->clipRect(clipRect);
36 canvas->drawPaint(paint);
37 canvas->restore();
38 }
39
make_picture()40 static sk_sp<SkPicture> make_picture() {
41 SkPictureRecorder recorder;
42 SkCanvas* canvas = recorder.beginRecording(100, 100);
43 SkPaint paint;
44 paint.setColor(0xFFFFFFFF);
45 SkFont font(ToolUtils::create_portable_typeface(), 96.0f);
46 canvas->drawString("e", 20.0f, 70.0f, font, paint);
47 return recorder.finishRecordingAsPicture();
48 }
49
50 // Create a picture that will draw LCD text
make_LCD_picture()51 static sk_sp<SkPicture> make_LCD_picture() {
52 SkPictureRecorder recorder;
53 SkCanvas* canvas = recorder.beginRecording(100, 100);
54 canvas->clear(SK_ColorTRANSPARENT);
55 SkPaint paint;
56 paint.setColor(0xFFFFFFFF);
57 // this has to be small enough that it doesn't become a path
58 SkFont font(ToolUtils::create_portable_typeface(), 36.0f);
59 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
60 canvas->drawString("e", 20.0f, 70.0f, font, paint);
61 return recorder.finishRecordingAsPicture();
62 }
63
64 class PictureImageFilterGM : public skiagm::GM {
65 public:
PictureImageFilterGM()66 PictureImageFilterGM() { }
67
68 protected:
onShortName()69 SkString onShortName() override {
70 return SkString("pictureimagefilter");
71 }
72
onISize()73 SkISize onISize() override { return SkISize::Make(600, 300); }
74
onOnceBeforeDraw()75 void onOnceBeforeDraw() override {
76 fPicture = make_picture();
77 fLCDPicture = make_LCD_picture();
78 }
79
make(sk_sp<SkPicture> pic,SkRect r,const SkSamplingOptions & sampling)80 sk_sp<SkImageFilter> make(sk_sp<SkPicture> pic, SkRect r, const SkSamplingOptions& sampling) {
81 SkISize dim = { SkScalarRoundToInt(r.width()), SkScalarRoundToInt(r.height()) };
82 auto img = SkImage::MakeFromPicture(pic, dim, nullptr, nullptr,
83 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
84 return SkImageFilters::Image(img, r, r, sampling);
85 }
make(const SkSamplingOptions & sampling)86 sk_sp<SkImageFilter> make(const SkSamplingOptions& sampling) {
87 return make(fPicture, fPicture->cullRect(), sampling);
88 }
89
onDraw(SkCanvas * canvas)90 void onDraw(SkCanvas* canvas) override {
91 canvas->clear(SK_ColorGRAY);
92 {
93 SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
94 SkRect emptyRect = SkRect::MakeXYWH(20, 20, 0, 0);
95 SkRect bounds = SkRect::MakeXYWH(0, 0, 100, 100);
96 sk_sp<SkImageFilter> pictureSource(SkImageFilters::Picture(fPicture));
97 sk_sp<SkImageFilter> pictureSourceSrcRect(SkImageFilters::Picture(fPicture, srcRect));
98 sk_sp<SkImageFilter> pictureSourceEmptyRect(SkImageFilters::Picture(fPicture,
99 emptyRect));
100 sk_sp<SkImageFilter> pictureSourceResampled = make(SkSamplingOptions(SkFilterMode::kLinear));
101 sk_sp<SkImageFilter> pictureSourcePixelated = make(SkSamplingOptions());
102
103 canvas->save();
104 // Draw the picture unscaled.
105 fill_rect_filtered(canvas, bounds, pictureSource);
106 canvas->translate(SkIntToScalar(100), 0);
107
108 // Draw an unscaled subset of the source picture.
109 fill_rect_filtered(canvas, bounds, pictureSourceSrcRect);
110 canvas->translate(SkIntToScalar(100), 0);
111
112 // Draw the picture to an empty rect (should draw nothing).
113 fill_rect_filtered(canvas, bounds, pictureSourceEmptyRect);
114 canvas->translate(SkIntToScalar(100), 0);
115
116 // Draw the LCD picture to a layer
117 {
118 SkPaint stroke;
119 stroke.setStyle(SkPaint::kStroke_Style);
120
121 canvas->drawRect(bounds, stroke);
122
123 SkPaint paint;
124 paint.setImageFilter(make(fLCDPicture, fPicture->cullRect(), SkSamplingOptions()));
125
126 canvas->scale(4, 4);
127 canvas->translate(-0.9f*srcRect.fLeft, -2.45f*srcRect.fTop);
128
129 canvas->saveLayer(&bounds, &paint);
130 canvas->restore();
131 }
132
133 canvas->restore();
134
135 // Draw the picture scaled
136 canvas->translate(0, SkIntToScalar(100));
137 canvas->scale(200 / srcRect.width(), 200 / srcRect.height());
138 canvas->translate(-srcRect.fLeft, -srcRect.fTop);
139 fill_rect_filtered(canvas, srcRect, pictureSource);
140
141 // Draw the picture scaled, but rasterized at original resolution
142 canvas->translate(srcRect.width(), 0);
143 fill_rect_filtered(canvas, srcRect, pictureSourceResampled);
144
145 // Draw the picture scaled, pixelated
146 canvas->translate(srcRect.width(), 0);
147 fill_rect_filtered(canvas, srcRect, pictureSourcePixelated);
148 }
149 }
150
151 private:
152 sk_sp<SkPicture> fPicture;
153 sk_sp<SkPicture> fLCDPicture;
154
155 using INHERITED = GM;
156 };
157
158 ///////////////////////////////////////////////////////////////////////////////
159
160 DEF_GM( return new PictureImageFilterGM; )
161