2013-12-12 23:28:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2013-12-12 23:28:52 +00:00
|
|
|
|
|
|
|
#include "SkPictureImageFilter.h"
|
2014-04-18 18:04:41 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2013-12-12 23:28:52 +00:00
|
|
|
|
2017-11-09 21:50:20 +00:00
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkImageSource.h"
|
|
|
|
|
2013-12-12 23:28:52 +00:00
|
|
|
// This GM exercises the SkPictureImageFilter ImageFilter class.
|
|
|
|
|
2016-03-28 20:13:42 +00:00
|
|
|
static void fill_rect_filtered(SkCanvas* canvas,
|
|
|
|
const SkRect& clipRect,
|
|
|
|
sk_sp<SkImageFilter> filter) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(filter);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(clipRect);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkPicture> make_picture() {
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(100, 100, nullptr, 0);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xFFFFFFFF);
|
2019-01-08 14:38:02 +00:00
|
|
|
SkFont font(sk_tool_utils::create_portable_typeface(), 96.0f);
|
|
|
|
canvas->drawString("e", 20.0f, 70.0f, font, paint);
|
2016-03-28 20:13:42 +00:00
|
|
|
return recorder.finishRecordingAsPicture();
|
|
|
|
}
|
|
|
|
|
2016-04-13 17:55:07 +00:00
|
|
|
// Create a picture that will draw LCD text
|
|
|
|
static sk_sp<SkPicture> make_LCD_picture() {
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(100, 100, nullptr, 0);
|
|
|
|
canvas->clear(SK_ColorTRANSPARENT);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xFFFFFFFF);
|
|
|
|
// this has to be small enough that it doesn't become a path
|
2019-01-08 14:38:02 +00:00
|
|
|
SkFont font(sk_tool_utils::create_portable_typeface(), 36.0f);
|
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
|
|
|
canvas->drawString("e", 20.0f, 70.0f, font, paint);
|
2016-04-13 17:55:07 +00:00
|
|
|
return recorder.finishRecordingAsPicture();
|
|
|
|
}
|
|
|
|
|
2013-12-12 23:28:52 +00:00
|
|
|
class PictureImageFilterGM : public skiagm::GM {
|
|
|
|
public:
|
2016-03-28 20:13:42 +00:00
|
|
|
PictureImageFilterGM() { }
|
2013-12-12 23:28:52 +00:00
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-12-12 23:28:52 +00:00
|
|
|
return SkString("pictureimagefilter");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(600, 300); }
|
2013-12-12 23:28:52 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2016-03-28 20:13:42 +00:00
|
|
|
fPicture = make_picture();
|
2016-04-13 17:55:07 +00:00
|
|
|
fLCDPicture = make_LCD_picture();
|
2013-12-12 23:28:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 21:50:20 +00:00
|
|
|
sk_sp<SkImageFilter> make(sk_sp<SkPicture> pic, SkRect r, SkFilterQuality fq) {
|
|
|
|
SkISize dim = { SkScalarRoundToInt(r.width()), SkScalarRoundToInt(r.height()) };
|
|
|
|
auto img = SkImage::MakeFromPicture(pic, dim, nullptr, nullptr,
|
|
|
|
SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
|
|
|
|
return SkImageSource::Make(img, r, r, fq);
|
|
|
|
}
|
|
|
|
sk_sp<SkImageFilter> make(SkFilterQuality fq) {
|
|
|
|
return make(fPicture, fPicture->cullRect(), fq);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-04-13 17:55:07 +00:00
|
|
|
canvas->clear(SK_ColorGRAY);
|
2013-12-12 23:28:52 +00:00
|
|
|
{
|
|
|
|
SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
|
|
|
|
SkRect emptyRect = SkRect::MakeXYWH(20, 20, 0, 0);
|
|
|
|
SkRect bounds = SkRect::MakeXYWH(0, 0, 100, 100);
|
2016-03-28 20:13:42 +00:00
|
|
|
sk_sp<SkImageFilter> pictureSource(SkPictureImageFilter::Make(fPicture));
|
|
|
|
sk_sp<SkImageFilter> pictureSourceSrcRect(SkPictureImageFilter::Make(fPicture,
|
|
|
|
srcRect));
|
|
|
|
sk_sp<SkImageFilter> pictureSourceEmptyRect(SkPictureImageFilter::Make(fPicture,
|
|
|
|
emptyRect));
|
2017-11-09 21:50:20 +00:00
|
|
|
sk_sp<SkImageFilter> pictureSourceResampled = make(kLow_SkFilterQuality);
|
|
|
|
sk_sp<SkImageFilter> pictureSourcePixelated = make(kNone_SkFilterQuality);
|
2014-12-02 19:50:56 +00:00
|
|
|
|
|
|
|
canvas->save();
|
2013-12-12 23:28:52 +00:00
|
|
|
// Draw the picture unscaled.
|
2016-03-28 20:13:42 +00:00
|
|
|
fill_rect_filtered(canvas, bounds, pictureSource);
|
2013-12-12 23:28:52 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
|
|
|
|
|
|
|
// Draw an unscaled subset of the source picture.
|
2016-03-28 20:13:42 +00:00
|
|
|
fill_rect_filtered(canvas, bounds, pictureSourceSrcRect);
|
2013-12-12 23:28:52 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
|
|
|
|
|
|
|
// Draw the picture to an empty rect (should draw nothing).
|
2016-03-28 20:13:42 +00:00
|
|
|
fill_rect_filtered(canvas, bounds, pictureSourceEmptyRect);
|
2013-12-12 23:28:52 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
2014-12-02 19:50:56 +00:00
|
|
|
|
2016-04-13 17:55:07 +00:00
|
|
|
// Draw the LCD picture to a layer
|
|
|
|
{
|
|
|
|
SkPaint stroke;
|
|
|
|
stroke.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
canvas->drawRect(bounds, stroke);
|
|
|
|
|
|
|
|
SkPaint paint;
|
2017-11-09 21:50:20 +00:00
|
|
|
paint.setImageFilter(make(fLCDPicture, fPicture->cullRect(), kNone_SkFilterQuality));
|
2016-04-13 17:55:07 +00:00
|
|
|
|
|
|
|
canvas->scale(4, 4);
|
|
|
|
canvas->translate(-0.9f*srcRect.fLeft, -2.45f*srcRect.fTop);
|
|
|
|
|
2019-01-07 21:34:18 +00:00
|
|
|
canvas->saveLayer(&bounds, &paint);
|
2016-04-13 17:55:07 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2014-12-02 19:50:56 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
// Draw the picture scaled
|
|
|
|
canvas->translate(0, SkIntToScalar(100));
|
|
|
|
canvas->scale(200 / srcRect.width(), 200 / srcRect.height());
|
|
|
|
canvas->translate(-srcRect.fLeft, -srcRect.fTop);
|
2016-03-28 20:13:42 +00:00
|
|
|
fill_rect_filtered(canvas, srcRect, pictureSource);
|
2014-12-02 19:50:56 +00:00
|
|
|
|
|
|
|
// Draw the picture scaled, but rasterized at original resolution
|
|
|
|
canvas->translate(srcRect.width(), 0);
|
2016-03-28 20:13:42 +00:00
|
|
|
fill_rect_filtered(canvas, srcRect, pictureSourceResampled);
|
2014-12-09 21:07:22 +00:00
|
|
|
|
|
|
|
// Draw the picture scaled, pixelated
|
|
|
|
canvas->translate(srcRect.width(), 0);
|
2016-03-28 20:13:42 +00:00
|
|
|
fill_rect_filtered(canvas, srcRect, pictureSourcePixelated);
|
2013-12-12 23:28:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> fPicture;
|
2016-04-13 17:55:07 +00:00
|
|
|
sk_sp<SkPicture> fLCDPicture;
|
2016-03-28 20:13:42 +00:00
|
|
|
|
2013-12-12 23:28:52 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM( return new PictureImageFilterGM; )
|