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"
|
|
|
|
|
|
|
|
#include "SkPictureImageFilter.h"
|
2014-04-18 18:04:41 +00:00
|
|
|
#include "SkPictureRecorder.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);
|
|
|
|
canvas->clear(SK_ColorBLACK);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
|
|
|
paint.setColor(0xFFFFFFFF);
|
|
|
|
paint.setTextSize(SkIntToScalar(96));
|
|
|
|
const char* str = "e";
|
|
|
|
canvas->drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
|
|
|
|
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();
|
2013-12-12 23:28:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-04-08 19:36:08 +00:00
|
|
|
canvas->clear(SK_ColorBLACK);
|
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));
|
|
|
|
sk_sp<SkImageFilter> pictureSourceResampled(SkPictureImageFilter::MakeForLocalSpace(
|
|
|
|
fPicture,
|
|
|
|
fPicture->cullRect(),
|
|
|
|
kLow_SkFilterQuality));
|
|
|
|
sk_sp<SkImageFilter> pictureSourcePixelated(SkPictureImageFilter::MakeForLocalSpace(
|
|
|
|
fPicture,
|
|
|
|
fPicture->cullRect(),
|
|
|
|
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
|
|
|
|
|
|
|
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-03-28 20:13:42 +00:00
|
|
|
|
2013-12-12 23:28:52 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM( return new PictureImageFilterGM; )
|