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