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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorSpace.h"
|
|
|
|
#include "include/core/SkFilterQuality.h"
|
|
|
|
#include "include/core/SkFont.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPicture.h"
|
|
|
|
#include "include/core/SkPictureRecorder.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2017-11-09 21:50:20 +00:00
|
|
|
|
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;
|
2020-08-21 17:42:01 +00:00
|
|
|
SkCanvas* canvas = recorder.beginRecording(100, 100);
|
2016-03-28 20:13:42 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xFFFFFFFF);
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 96.0f);
|
2019-01-08 14:38:02 +00:00
|
|
|
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;
|
2020-08-21 17:42:01 +00:00
|
|
|
SkCanvas* canvas = recorder.beginRecording(100, 100);
|
2016-04-13 17:55:07 +00:00
|
|
|
canvas->clear(SK_ColorTRANSPARENT);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xFFFFFFFF);
|
|
|
|
// this has to be small enough that it doesn't become a path
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 36.0f);
|
2019-01-08 14:38:02 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-14 02:04:58 +00:00
|
|
|
sk_sp<SkImageFilter> make(sk_sp<SkPicture> pic, SkRect r, const SkSamplingOptions& sampling) {
|
2017-11-09 21:50:20 +00:00
|
|
|
SkISize dim = { SkScalarRoundToInt(r.width()), SkScalarRoundToInt(r.height()) };
|
|
|
|
auto img = SkImage::MakeFromPicture(pic, dim, nullptr, nullptr,
|
|
|
|
SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
|
2021-02-14 02:04:58 +00:00
|
|
|
return SkImageFilters::Image(img, r, r, sampling);
|
2017-11-09 21:50:20 +00:00
|
|
|
}
|
2021-02-14 02:04:58 +00:00
|
|
|
sk_sp<SkImageFilter> make(const SkSamplingOptions& sampling) {
|
|
|
|
return make(fPicture, fPicture->cullRect(), sampling);
|
2017-11-09 21:50:20 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> pictureSource(SkImageFilters::Picture(fPicture));
|
|
|
|
sk_sp<SkImageFilter> pictureSourceSrcRect(SkImageFilters::Picture(fPicture, srcRect));
|
|
|
|
sk_sp<SkImageFilter> pictureSourceEmptyRect(SkImageFilters::Picture(fPicture,
|
|
|
|
emptyRect));
|
2021-02-14 02:04:58 +00:00
|
|
|
sk_sp<SkImageFilter> pictureSourceResampled = make(SkSamplingOptions(SkFilterMode::kLinear));
|
|
|
|
sk_sp<SkImageFilter> pictureSourcePixelated = make(SkSamplingOptions());
|
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;
|
2021-02-14 02:04:58 +00:00
|
|
|
paint.setImageFilter(make(fLCDPicture, fPicture->cullRect(), SkSamplingOptions()));
|
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
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2013-12-12 23:28:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM( return new PictureImageFilterGM; )
|