| 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 |
#include "tools/fonts/FontToolUtils.h" |
| 27 |
|
| 28 |
// This GM exercises the SkPictureImageFilter ImageFilter class. |
| 29 |
|
| 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 |
|
| 41 |
static sk_sp<SkPicture> make_picture() { |
| 42 |
SkPictureRecorder recorder; |
| 43 |
SkCanvas* canvas = recorder.beginRecording(100, 100); |
| 44 |
SkPaint paint; |
| 45 |
paint.setColor(0xFFFFFFFF); |
| 46 |
SkFont font(ToolUtils::DefaultPortableTypeface(), 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 |
| 52 |
static sk_sp<SkPicture> make_LCD_picture() { |
| 53 |
SkPictureRecorder recorder; |
| 54 |
SkCanvas* canvas = recorder.beginRecording(100, 100); |
| 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::DefaultPortableTypeface(), 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: |
| 67 |
PictureImageFilterGM() { } |
| 68 |
|
| 69 |
protected: |
| 70 |
SkString getName() const override { return SkString("pictureimagefilter"); } |
| 71 |
|
| 72 |
SkISize getISize() override { return SkISize::Make(600, 300); } |
| 73 |
|
| 74 |
void onOnceBeforeDraw() override { |
| 75 |
fPicture = make_picture(); |
| 76 |
fLCDPicture = make_LCD_picture(); |
| 77 |
} |
| 78 |
|
| 79 |
sk_sp<SkImageFilter> make(sk_sp<SkPicture> pic, SkRect r, const SkSamplingOptions& sampling) { |
| 80 |
SkISize dim = { SkScalarRoundToInt(r.width()), SkScalarRoundToInt(r.height()) }; |
| 81 |
auto img = SkImages::DeferredFromPicture( |
| 82 |
pic, dim, nullptr, nullptr, SkImages::BitDepth::kU8, SkColorSpace::MakeSRGB()); |
| 83 |
return SkImageFilters::Image(img, r, r, sampling); |
| 84 |
} |
| 85 |
sk_sp<SkImageFilter> make(const SkSamplingOptions& sampling) { |
| 86 |
return make(fPicture, fPicture->cullRect(), sampling); |
| 87 |
} |
| 88 |
|
| 89 |
void onDraw(SkCanvas* canvas) override { |
| 90 |
canvas->clear(SK_ColorGRAY); |
| 91 |
{ |
| 92 |
SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30); |
| 93 |
SkRect emptyRect = SkRect::MakeXYWH(20, 20, 0, 0); |
| 94 |
SkRect bounds = SkRect::MakeXYWH(0, 0, 100, 100); |
| 95 |
sk_sp<SkImageFilter> pictureSource(SkImageFilters::Picture(fPicture)); |
| 96 |
sk_sp<SkImageFilter> pictureSourceSrcRect(SkImageFilters::Picture(fPicture, srcRect)); |
| 97 |
sk_sp<SkImageFilter> pictureSourceEmptyRect(SkImageFilters::Picture(fPicture, |
| 98 |
emptyRect)); |
| 99 |
sk_sp<SkImageFilter> pictureSourceResampled = make(SkSamplingOptions(SkFilterMode::kLinear)); |
| 100 |
sk_sp<SkImageFilter> pictureSourcePixelated = make(SkSamplingOptions()); |
| 101 |
|
| 102 |
canvas->save(); |
| 103 |
// Draw the picture unscaled. |
| 104 |
fill_rect_filtered(canvas, bounds, pictureSource); |
| 105 |
canvas->translate(SkIntToScalar(100), 0); |
| 106 |
|
| 107 |
// Draw an unscaled subset of the source picture. |
| 108 |
fill_rect_filtered(canvas, bounds, pictureSourceSrcRect); |
| 109 |
canvas->translate(SkIntToScalar(100), 0); |
| 110 |
|
| 111 |
// Draw the picture to an empty rect (should draw nothing). |
| 112 |
fill_rect_filtered(canvas, bounds, pictureSourceEmptyRect); |
| 113 |
canvas->translate(SkIntToScalar(100), 0); |
| 114 |
|
| 115 |
// Draw the LCD picture to a layer |
| 116 |
{ |
| 117 |
SkPaint stroke; |
| 118 |
stroke.setStyle(SkPaint::kStroke_Style); |
| 119 |
|
| 120 |
canvas->drawRect(bounds, stroke); |
| 121 |
|
| 122 |
SkPaint paint; |
| 123 |
paint.setImageFilter(make(fLCDPicture, fPicture->cullRect(), SkSamplingOptions())); |
| 124 |
|
| 125 |
canvas->scale(4, 4); |
| 126 |
canvas->translate(-0.9f*srcRect.fLeft, -2.45f*srcRect.fTop); |
| 127 |
|
| 128 |
canvas->saveLayer(&bounds, &paint); |
| 129 |
canvas->restore(); |
| 130 |
} |
| 131 |
|
| 132 |
canvas->restore(); |
| 133 |
|
| 134 |
// Draw the picture scaled |
| 135 |
canvas->translate(0, SkIntToScalar(100)); |
| 136 |
canvas->scale(200 / srcRect.width(), 200 / srcRect.height()); |
| 137 |
canvas->translate(-srcRect.fLeft, -srcRect.fTop); |
| 138 |
fill_rect_filtered(canvas, srcRect, pictureSource); |
| 139 |
|
| 140 |
// Draw the picture scaled, but rasterized at original resolution |
| 141 |
canvas->translate(srcRect.width(), 0); |
| 142 |
fill_rect_filtered(canvas, srcRect, pictureSourceResampled); |
| 143 |
|
| 144 |
// Draw the picture scaled, pixelated |
| 145 |
canvas->translate(srcRect.width(), 0); |
| 146 |
fill_rect_filtered(canvas, srcRect, pictureSourcePixelated); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
private: |
| 151 |
sk_sp<SkPicture> fPicture; |
| 152 |
sk_sp<SkPicture> fLCDPicture; |
| 153 |
|
| 154 |
using INHERITED = GM; |
| 155 |
}; |
| 156 |
|
| 157 |
/////////////////////////////////////////////////////////////////////////////// |
| 158 |
|
| 159 |
DEF_GM( return new PictureImageFilterGM; ) |