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