2014-12-08 17:18:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 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"
|
2014-12-08 17:18:58 +00:00
|
|
|
#include "SkBlurImageFilter.h"
|
|
|
|
#include "SkDropShadowImageFilter.h"
|
2015-09-25 16:15:55 +00:00
|
|
|
#include "SkImageSource.h"
|
2014-12-08 17:18:58 +00:00
|
|
|
#include "SkOffsetImageFilter.h"
|
2017-05-16 14:39:06 +00:00
|
|
|
#include "SkPath.h"
|
2014-12-08 17:18:58 +00:00
|
|
|
#include "SkPictureImageFilter.h"
|
|
|
|
#include "SkPictureRecorder.h"
|
|
|
|
#include "SkRandom.h"
|
2015-09-25 16:15:55 +00:00
|
|
|
#include "SkSurface.h"
|
2015-12-07 18:36:30 +00:00
|
|
|
#include "SkTileImageFilter.h"
|
2014-12-08 17:18:58 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
// Each method of this type must draw its geometry inside 'r' using 'p'
|
|
|
|
typedef void(*drawMth)(SkCanvas* canvas, const SkRect& r, const SkPaint& p);
|
|
|
|
|
|
|
|
static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_oval(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
canvas->drawOval(r, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_rrect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
SkScalar xRad = r.width() / 4.0f;
|
|
|
|
SkScalar yRad = r.height() / 4.0f;
|
|
|
|
|
|
|
|
SkRRect rr;
|
|
|
|
rr.setRectXY(r, xRad, yRad);
|
|
|
|
canvas->drawRRect(rr, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_drrect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
SkScalar xRad = r.width() / 4.0f;
|
|
|
|
SkScalar yRad = r.height() / 4.0f;
|
|
|
|
|
|
|
|
SkRRect outer;
|
|
|
|
outer.setRectXY(r, xRad, yRad);
|
|
|
|
SkRRect inner = outer;
|
|
|
|
inner.inset(xRad, yRad);
|
|
|
|
canvas->drawDRRect(outer, inner, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_path(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
SkPath path;
|
|
|
|
|
|
|
|
path.moveTo(r.fLeft, r.fTop);
|
|
|
|
path.lineTo(r.fLeft, r.fBottom);
|
|
|
|
path.lineTo(r.fRight, r.fBottom);
|
|
|
|
path.close();
|
|
|
|
|
|
|
|
canvas->drawPath(path, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_points(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
SkPoint pts0[2] = { { r.fLeft, r.fTop }, { r.fRight, r.fBottom } };
|
|
|
|
SkPoint pts1[2] = { { r.fLeft, r.fBottom }, { r.fRight, r.fTop } };
|
|
|
|
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts0, p);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts1, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_bitmap(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
SkBitmap bm;
|
|
|
|
|
|
|
|
bm.allocN32Pixels(64, 64);
|
|
|
|
SkCanvas temp(bm);
|
|
|
|
temp.clear(SK_ColorMAGENTA);
|
|
|
|
|
|
|
|
canvas->drawBitmapRect(bm, r, &p);
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr drawMth gDrawMthds[] = {
|
2014-12-08 17:18:58 +00:00
|
|
|
draw_rect, draw_oval, draw_rrect, draw_drrect, draw_path, draw_points, draw_bitmap
|
|
|
|
};
|
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
static void add_paint(SkTArray<SkPaint>* paints, sk_sp<SkImageFilter> filter) {
|
2014-12-08 17:18:58 +00:00
|
|
|
SkPaint& p = paints->push_back();
|
2016-04-04 11:31:25 +00:00
|
|
|
p.setImageFilter(std::move(filter));
|
2014-12-08 17:18:58 +00:00
|
|
|
SkASSERT(p.canComputeFastBounds());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a selection of imagefilter-based paints to test
|
2016-04-04 11:31:25 +00:00
|
|
|
static void create_paints(SkTArray<SkPaint>* paints, sk_sp<SkImageFilter> source) {
|
2014-12-08 17:18:58 +00:00
|
|
|
{
|
|
|
|
SkMatrix scale;
|
|
|
|
scale.setScale(2.0f, 2.0f);
|
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
sk_sp<SkImageFilter> scaleMIF(
|
2016-04-05 22:09:00 +00:00
|
|
|
SkImageFilter::MakeMatrixFilter(scale, kLow_SkFilterQuality, source));
|
2014-12-08 17:18:58 +00:00
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
add_paint(paints, std::move(scaleMIF));
|
2014-12-08 17:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SkMatrix rot;
|
|
|
|
rot.setRotate(-33.3f);
|
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
sk_sp<SkImageFilter> rotMIF(
|
2016-04-05 22:09:00 +00:00
|
|
|
SkImageFilter::MakeMatrixFilter(rot, kLow_SkFilterQuality, source));
|
2014-12-08 17:18:58 +00:00
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
add_paint(paints, std::move(rotMIF));
|
2014-12-08 17:18:58 +00:00
|
|
|
}
|
|
|
|
|
2015-12-07 18:36:30 +00:00
|
|
|
{
|
|
|
|
SkRect src = SkRect::MakeXYWH(20, 20, 10, 10);
|
|
|
|
SkRect dst = SkRect::MakeXYWH(30, 30, 30, 30);
|
2016-04-15 14:57:40 +00:00
|
|
|
sk_sp<SkImageFilter> tileIF(SkTileImageFilter::Make(src, dst, nullptr));
|
2015-12-07 18:36:30 +00:00
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
add_paint(paints, std::move(tileIF));
|
2015-12-07 18:36:30 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 17:18:58 +00:00
|
|
|
{
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkDropShadowImageFilter::ShadowMode kBoth =
|
2014-12-08 17:18:58 +00:00
|
|
|
SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode;
|
|
|
|
|
2016-04-06 15:40:59 +00:00
|
|
|
sk_sp<SkImageFilter> dsif(SkDropShadowImageFilter::Make(10.0f, 10.0f,
|
|
|
|
3.0f, 3.0f,
|
|
|
|
SK_ColorRED, kBoth,
|
|
|
|
source));
|
2014-12-08 17:18:58 +00:00
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
add_paint(paints, std::move(dsif));
|
2014-12-08 17:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-04 11:31:25 +00:00
|
|
|
sk_sp<SkImageFilter> dsif(
|
2016-04-06 15:40:59 +00:00
|
|
|
SkDropShadowImageFilter::Make(27.0f, 27.0f,
|
2014-12-08 17:18:58 +00:00
|
|
|
3.0f, 3.0f,
|
|
|
|
SK_ColorRED,
|
|
|
|
SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode,
|
2016-04-06 15:40:59 +00:00
|
|
|
source));
|
2014-12-08 17:18:58 +00:00
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
add_paint(paints, std::move(dsif));
|
2014-12-08 17:18:58 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
add_paint(paints, SkBlurImageFilter::Make(3, 3, source));
|
|
|
|
add_paint(paints, SkOffsetImageFilter::Make(15, 15, source));
|
2014-12-08 17:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This GM visualizes the fast bounds for various combinations of geometry
|
|
|
|
// and image filter
|
|
|
|
class ImageFilterFastBoundGM : public GM {
|
|
|
|
public:
|
|
|
|
ImageFilterFastBoundGM() {
|
2015-06-15 13:51:08 +00:00
|
|
|
this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
|
2014-12-08 17:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kTileWidth = 100;
|
|
|
|
static constexpr int kTileHeight = 100;
|
|
|
|
static constexpr int kNumVertTiles = 7;
|
|
|
|
static constexpr int kNumXtraCols = 2;
|
2014-12-08 17:18:58 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override{ return SkString("filterfastbounds"); }
|
2014-12-08 17:18:58 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override{
|
2014-12-08 17:18:58 +00:00
|
|
|
return SkISize::Make((SK_ARRAY_COUNT(gDrawMthds) + kNumXtraCols) * kTileWidth,
|
|
|
|
kNumVertTiles * kTileHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_geom_with_paint(drawMth draw, const SkIPoint& off,
|
|
|
|
SkCanvas* canvas, const SkPaint& p) {
|
|
|
|
SkPaint redStroked;
|
|
|
|
redStroked.setColor(SK_ColorRED);
|
|
|
|
redStroked.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
SkPaint blueStroked;
|
|
|
|
blueStroked.setColor(SK_ColorBLUE);
|
|
|
|
blueStroked.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
const SkRect r = SkRect::MakeLTRB(20, 20, 30, 30);
|
|
|
|
SkRect storage;
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(off.fX), SkIntToScalar(off.fY));
|
|
|
|
canvas->scale(1.5f, 1.5f);
|
|
|
|
|
|
|
|
const SkRect& fastBound = p.computeFastBounds(r, &storage);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(fastBound);
|
|
|
|
(*draw)(canvas, r, p);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->drawRect(r, redStroked);
|
|
|
|
canvas->drawRect(fastBound, blueStroked);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_savelayer_with_paint(const SkIPoint& off,
|
|
|
|
SkCanvas* canvas,
|
|
|
|
const SkPaint& p) {
|
|
|
|
SkPaint redStroked;
|
|
|
|
redStroked.setColor(SK_ColorRED);
|
|
|
|
redStroked.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
SkPaint blueStroked;
|
|
|
|
blueStroked.setColor(SK_ColorBLUE);
|
|
|
|
blueStroked.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
const SkRect bounds = SkRect::MakeWH(10, 10);
|
|
|
|
SkRect storage;
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(30, 30);
|
|
|
|
canvas->translate(SkIntToScalar(off.fX), SkIntToScalar(off.fY));
|
|
|
|
canvas->scale(1.5f, 1.5f);
|
|
|
|
|
|
|
|
const SkRect& fastBound = p.computeFastBounds(bounds, &storage);
|
|
|
|
|
|
|
|
canvas->saveLayer(&fastBound, &p);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->drawRect(bounds, redStroked);
|
|
|
|
canvas->drawRect(fastBound, blueStroked);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override{
|
2014-12-08 17:18:58 +00:00
|
|
|
|
|
|
|
SkPaint blackFill;
|
|
|
|
|
|
|
|
//-----------
|
|
|
|
// Normal paints (no source)
|
|
|
|
SkTArray<SkPaint> paints;
|
2016-04-04 11:31:25 +00:00
|
|
|
create_paints(&paints, nullptr);
|
2014-12-08 17:18:58 +00:00
|
|
|
|
|
|
|
//-----------
|
|
|
|
// Paints with a PictureImageFilter as a source
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> pic;
|
2014-12-08 17:18:58 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
SkPictureRecorder rec;
|
|
|
|
|
|
|
|
SkCanvas* c = rec.beginRecording(10, 10);
|
|
|
|
c->drawRect(SkRect::MakeWH(10, 10), blackFill);
|
2016-03-18 14:25:55 +00:00
|
|
|
pic = rec.finishRecordingAsPicture();
|
2014-12-08 17:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkTArray<SkPaint> pifPaints;
|
2016-04-04 11:31:25 +00:00
|
|
|
create_paints(&pifPaints, SkPictureImageFilter::Make(pic));
|
2014-12-08 17:18:58 +00:00
|
|
|
|
|
|
|
//-----------
|
2015-09-25 16:15:55 +00:00
|
|
|
// Paints with a SkImageSource as a source
|
2014-12-08 17:18:58 +00:00
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRasterN32Premul(10, 10));
|
2014-12-08 17:18:58 +00:00
|
|
|
{
|
|
|
|
SkPaint p;
|
2015-09-25 16:15:55 +00:00
|
|
|
SkCanvas* temp = surface->getCanvas();
|
|
|
|
temp->clear(SK_ColorYELLOW);
|
2014-12-08 17:18:58 +00:00
|
|
|
p.setColor(SK_ColorBLUE);
|
2015-09-25 16:15:55 +00:00
|
|
|
temp->drawRect(SkRect::MakeLTRB(5, 5, 10, 10), p);
|
2014-12-08 17:18:58 +00:00
|
|
|
p.setColor(SK_ColorGREEN);
|
2015-09-25 16:15:55 +00:00
|
|
|
temp->drawRect(SkRect::MakeLTRB(5, 0, 10, 5), p);
|
2014-12-08 17:18:58 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(surface->makeImageSnapshot());
|
2016-04-01 16:28:51 +00:00
|
|
|
sk_sp<SkImageFilter> imageSource(SkImageSource::Make(std::move(image)));
|
2014-12-08 17:18:58 +00:00
|
|
|
SkTArray<SkPaint> bmsPaints;
|
2016-04-04 11:31:25 +00:00
|
|
|
create_paints(&bmsPaints, std::move(imageSource));
|
2014-12-08 17:18:58 +00:00
|
|
|
|
|
|
|
//-----------
|
|
|
|
SkASSERT(paints.count() == kNumVertTiles);
|
|
|
|
SkASSERT(paints.count() == pifPaints.count());
|
|
|
|
SkASSERT(paints.count() == bmsPaints.count());
|
|
|
|
|
|
|
|
// horizontal separators
|
|
|
|
for (int i = 1; i < paints.count(); ++i) {
|
|
|
|
canvas->drawLine(0,
|
|
|
|
i*SkIntToScalar(kTileHeight),
|
|
|
|
SkIntToScalar((SK_ARRAY_COUNT(gDrawMthds) + kNumXtraCols)*kTileWidth),
|
|
|
|
i*SkIntToScalar(kTileHeight),
|
|
|
|
blackFill);
|
|
|
|
}
|
|
|
|
// vertical separators
|
|
|
|
for (int i = 0; i < (int)SK_ARRAY_COUNT(gDrawMthds) + kNumXtraCols; ++i) {
|
|
|
|
canvas->drawLine(SkIntToScalar(i * kTileWidth),
|
|
|
|
0,
|
2016-03-29 16:03:52 +00:00
|
|
|
SkIntToScalar(i * kTileWidth),
|
2014-12-08 17:18:58 +00:00
|
|
|
SkIntToScalar(paints.count() * kTileWidth),
|
|
|
|
blackFill);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A column of saveLayers with PictureImageFilters
|
|
|
|
for (int i = 0; i < pifPaints.count(); ++i) {
|
2016-03-29 16:03:52 +00:00
|
|
|
draw_savelayer_with_paint(SkIPoint::Make(0, i*kTileHeight),
|
2014-12-08 17:18:58 +00:00
|
|
|
canvas, pifPaints[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A column of saveLayers with BitmapSources
|
|
|
|
for (int i = 0; i < pifPaints.count(); ++i) {
|
|
|
|
draw_savelayer_with_paint(SkIPoint::Make(kTileWidth, i*kTileHeight),
|
|
|
|
canvas, bmsPaints[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multiple columns with different geometry
|
|
|
|
for (int i = 0; i < (int)SK_ARRAY_COUNT(gDrawMthds); ++i) {
|
|
|
|
for (int j = 0; j < paints.count(); ++j) {
|
|
|
|
draw_geom_with_paint(*gDrawMthds[i],
|
|
|
|
SkIPoint::Make((i+kNumXtraCols) * kTileWidth, j*kTileHeight),
|
|
|
|
canvas, paints[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new ImageFilterFastBoundGM;)
|
2014-12-08 17:18:58 +00:00
|
|
|
}
|