2013-06-03 17:13:25 +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/SkBlendMode.h"
|
|
|
|
#include "include/core/SkBlurTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorFilter.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkDrawLooper.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkMaskFilter.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.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"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkLayerDrawLooper.h"
|
|
|
|
#include "src/core/SkBlurMask.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/core/SkClipOpPriv.h"
|
2013-06-03 17:13:25 +00:00
|
|
|
|
2019-07-22 18:51:10 +00:00
|
|
|
#ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-06-03 17:13:25 +00:00
|
|
|
// This GM tests 3 different ways of drawing four shadows around a square:
|
|
|
|
// just using 4 blurred rects
|
|
|
|
// using 4 1-level draw loopers
|
|
|
|
// using 1 4-level draw looper
|
|
|
|
// They all produce exactly the same pixels
|
|
|
|
class MegaLooperGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
// The types define "<# of loopers> x <# of stages per looper>"
|
|
|
|
enum Type {
|
|
|
|
k0x0_Type, // draw without loopers at all
|
|
|
|
k4x1_Type, // a looper for each shadow
|
|
|
|
k1x4_Type, // all four shadows in one looper
|
|
|
|
};
|
|
|
|
|
|
|
|
MegaLooperGM(Type type) : fType(type) {}
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
private:
|
|
|
|
SkString onShortName() override {
|
2013-06-03 17:13:25 +00:00
|
|
|
switch (fType) {
|
|
|
|
case k0x0_Type:
|
|
|
|
return SkString("megalooper_0x0");
|
|
|
|
break;
|
|
|
|
case k4x1_Type:
|
|
|
|
return SkString("megalooper_4x1");
|
|
|
|
break;
|
|
|
|
case k1x4_Type: // fall through
|
|
|
|
default:
|
|
|
|
return SkString("megalooper_1x4");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
SkISize onISize() override {
|
|
|
|
return {kWidth, kHeight};
|
2013-06-03 17:13:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-06-03 17:13:25 +00:00
|
|
|
for (int y = 100; y < kHeight; y += 200) {
|
|
|
|
for (int x = 100; x < kWidth; x += 200) {
|
|
|
|
switch (fType) {
|
|
|
|
case k0x0_Type:
|
|
|
|
draw0x0(canvas, SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
break;
|
|
|
|
case k4x1_Type:
|
|
|
|
draw4x1(canvas, SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
break;
|
|
|
|
case k1x4_Type: // fall through
|
|
|
|
default:
|
|
|
|
draw1x4(canvas, SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kWidth = 800;
|
|
|
|
static constexpr int kHeight = 800;
|
|
|
|
static constexpr int kHalfOuterClipSize = 100;
|
|
|
|
static constexpr int kHalfSquareSize = 50;
|
|
|
|
static constexpr int kOffsetToOutsideClip = kHalfSquareSize + kHalfOuterClipSize + 1;
|
2013-06-03 17:13:25 +00:00
|
|
|
|
|
|
|
static const SkPoint gBlurOffsets[4];
|
|
|
|
static const SkColor gColors[4];
|
|
|
|
Type fType;
|
|
|
|
|
|
|
|
// Just draw a blurred rect at each of the four corners of a square (centered at x,y).
|
|
|
|
// Use two clips to define a rectori where we want pixels to appear.
|
|
|
|
void draw0x0(SkCanvas* canvas, SkScalar x, SkScalar y) {
|
|
|
|
SkRect innerClip = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize };
|
|
|
|
innerClip.offset(x, y);
|
|
|
|
|
2013-06-04 07:00:53 +00:00
|
|
|
SkRect outerClip = {
|
|
|
|
-kHalfOuterClipSize-kHalfSquareSize, -kHalfOuterClipSize-kHalfSquareSize,
|
2013-06-03 17:13:25 +00:00
|
|
|
kHalfOuterClipSize+kHalfSquareSize, kHalfOuterClipSize+kHalfSquareSize
|
|
|
|
};
|
|
|
|
outerClip.offset(x, y);
|
|
|
|
|
|
|
|
canvas->save();
|
2016-12-09 14:00:50 +00:00
|
|
|
canvas->clipRect(outerClip, kIntersect_SkClipOp);
|
|
|
|
canvas->clipRect(innerClip, kDifference_SkClipOp);
|
2013-06-03 17:13:25 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2016-04-04 17:02:58 +00:00
|
|
|
paint.setMaskFilter(MakeBlur());
|
2013-06-03 17:13:25 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
paint.setColor(gColors[i]);
|
|
|
|
|
|
|
|
SkRect rect = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize };
|
|
|
|
rect.offset(gBlurOffsets[i]);
|
|
|
|
rect.offset(x, y);
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2016-04-04 17:02:58 +00:00
|
|
|
static sk_sp<SkMaskFilter> MakeBlur() {
|
2016-09-01 18:24:54 +00:00
|
|
|
const SkScalar kBlurSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(25));
|
2013-06-03 17:13:25 +00:00
|
|
|
|
2018-03-14 17:01:17 +00:00
|
|
|
return SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kBlurSigma);
|
2013-06-03 17:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This draws 4 blurred shadows around a single square (centered at x, y).
|
|
|
|
// Each blur is offset +/- half the square's side in x & y from the original
|
|
|
|
// (so each blurred rect is centered at one of the corners of the original).
|
|
|
|
// For each blur a large outer clip is centered around the blurred rect
|
|
|
|
// while a difference clip stays at the location of the original rect.
|
|
|
|
// Each blurred rect is drawn with a draw looper where the original (non-
|
|
|
|
// blurred rect) is offset to reside outside of the large outer clip (so
|
2013-06-04 07:00:53 +00:00
|
|
|
// it never appears) but the offset in the draw looper is used to translate
|
2013-06-03 17:13:25 +00:00
|
|
|
// the blurred version back into the clip.
|
|
|
|
void draw4x1(SkCanvas* canvas, SkScalar x, SkScalar y) {
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
SkPaint loopPaint;
|
|
|
|
|
2016-03-21 20:25:16 +00:00
|
|
|
loopPaint.setLooper(create1Looper(-kOffsetToOutsideClip, 0, gColors[i]));
|
2013-06-03 17:13:25 +00:00
|
|
|
loopPaint.setAntiAlias(true);
|
|
|
|
|
2013-06-04 07:00:53 +00:00
|
|
|
SkRect outerClip = {
|
|
|
|
-kHalfOuterClipSize, -kHalfOuterClipSize,
|
|
|
|
kHalfOuterClipSize, kHalfOuterClipSize
|
2013-06-03 17:13:25 +00:00
|
|
|
};
|
|
|
|
outerClip.offset(x, y);
|
|
|
|
// center it on the blurred rect
|
|
|
|
outerClip.offset(gBlurOffsets[i]);
|
|
|
|
|
|
|
|
SkRect rect = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize };
|
|
|
|
rect.offset(x, y);
|
|
|
|
|
|
|
|
canvas->save();
|
2016-12-09 14:00:50 +00:00
|
|
|
canvas->clipRect(outerClip, kIntersect_SkClipOp);
|
|
|
|
canvas->clipRect(rect, kDifference_SkClipOp);
|
2013-06-03 17:13:25 +00:00
|
|
|
|
|
|
|
// move the rect to where we want the blur to appear
|
|
|
|
rect.offset(gBlurOffsets[i]);
|
|
|
|
// then move it outside the clip (the blur stage of the draw
|
|
|
|
// looper will undo this translation)
|
|
|
|
rect.offset(SkIntToScalar(kOffsetToOutsideClip), 0);
|
|
|
|
|
|
|
|
canvas->drawRect(rect, loopPaint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-04 07:00:53 +00:00
|
|
|
// Create a 1-tier drawlooper
|
2016-03-21 20:25:16 +00:00
|
|
|
sk_sp<SkDrawLooper> create1Looper(SkScalar xOff, SkScalar yOff, SkColor color) {
|
2014-04-15 15:48:36 +00:00
|
|
|
SkLayerDrawLooper::Builder looperBuilder;
|
2013-06-03 17:13:25 +00:00
|
|
|
SkLayerDrawLooper::LayerInfo info;
|
|
|
|
|
|
|
|
info.fPaintBits = SkLayerDrawLooper::kColorFilter_Bit |
|
|
|
|
SkLayerDrawLooper::kMaskFilter_Bit;
|
2016-11-03 18:45:31 +00:00
|
|
|
info.fColorMode = SkBlendMode::kSrc;
|
2013-06-03 17:13:25 +00:00
|
|
|
info.fOffset.set(xOff, yOff);
|
|
|
|
info.fPostTranslate = false;
|
|
|
|
|
2014-04-15 15:48:36 +00:00
|
|
|
SkPaint* paint = looperBuilder.addLayer(info);
|
2013-06-03 17:13:25 +00:00
|
|
|
|
2016-04-04 17:02:58 +00:00
|
|
|
paint->setMaskFilter(MakeBlur());
|
2013-06-03 17:13:25 +00:00
|
|
|
|
2019-04-08 20:23:20 +00:00
|
|
|
paint->setColorFilter(SkColorFilters::Blend(color, SkBlendMode::kSrcIn));
|
2013-06-03 17:13:25 +00:00
|
|
|
|
2016-03-21 20:25:16 +00:00
|
|
|
return looperBuilder.detach();
|
2013-06-03 17:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void draw1x4(SkCanvas* canvas, SkScalar x, SkScalar y) {
|
|
|
|
SkRect rect = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize };
|
|
|
|
rect.offset(x, y);
|
|
|
|
|
2013-06-04 07:00:53 +00:00
|
|
|
SkRect outerClip = {
|
|
|
|
-kHalfOuterClipSize-kHalfSquareSize, -kHalfOuterClipSize-kHalfSquareSize,
|
2013-06-03 17:13:25 +00:00
|
|
|
kHalfOuterClipSize+kHalfSquareSize, kHalfOuterClipSize+kHalfSquareSize
|
|
|
|
};
|
|
|
|
outerClip.offset(x, y);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2016-03-21 20:25:16 +00:00
|
|
|
paint.setLooper(create4Looper(-kOffsetToOutsideClip-kHalfSquareSize, 0));
|
2013-06-03 17:13:25 +00:00
|
|
|
|
|
|
|
canvas->save();
|
2016-12-09 14:00:50 +00:00
|
|
|
canvas->clipRect(outerClip, kIntersect_SkClipOp);
|
|
|
|
canvas->clipRect(rect, kDifference_SkClipOp);
|
2013-06-03 17:13:25 +00:00
|
|
|
|
|
|
|
rect.offset(SkIntToScalar(kOffsetToOutsideClip+kHalfSquareSize), 0);
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a 4-tier draw looper
|
2016-03-21 20:25:16 +00:00
|
|
|
sk_sp<SkDrawLooper> create4Looper(SkScalar xOff, SkScalar yOff) {
|
2014-04-15 15:48:36 +00:00
|
|
|
SkLayerDrawLooper::Builder looperBuilder;
|
2013-06-03 17:13:25 +00:00
|
|
|
SkLayerDrawLooper::LayerInfo info;
|
|
|
|
|
|
|
|
info.fPaintBits = SkLayerDrawLooper::kColorFilter_Bit |
|
|
|
|
SkLayerDrawLooper::kMaskFilter_Bit;
|
2016-11-03 18:45:31 +00:00
|
|
|
info.fColorMode = SkBlendMode::kSrc;
|
2013-06-03 17:13:25 +00:00
|
|
|
info.fPostTranslate = false;
|
|
|
|
|
|
|
|
SkPaint* paint;
|
|
|
|
|
|
|
|
for (int i = 3; i >= 0; --i) {
|
|
|
|
info.fOffset.set(xOff+gBlurOffsets[i].fX, yOff+gBlurOffsets[i].fY);
|
2014-04-15 15:48:36 +00:00
|
|
|
paint = looperBuilder.addLayer(info);
|
2013-06-03 17:13:25 +00:00
|
|
|
|
2016-04-04 17:02:58 +00:00
|
|
|
paint->setMaskFilter(MakeBlur());
|
2013-06-03 17:13:25 +00:00
|
|
|
|
2019-04-08 20:23:20 +00:00
|
|
|
paint->setColorFilter(SkColorFilters::Blend(gColors[i], SkBlendMode::kSrcIn));
|
2013-06-03 17:13:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 20:25:16 +00:00
|
|
|
return looperBuilder.detach();
|
2013-06-03 17:13:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkPoint MegaLooperGM::gBlurOffsets[4] = {
|
2013-06-04 07:00:53 +00:00
|
|
|
{ kHalfSquareSize, kHalfSquareSize },
|
|
|
|
{ -kHalfSquareSize, kHalfSquareSize },
|
|
|
|
{ kHalfSquareSize, -kHalfSquareSize },
|
2013-06-03 17:13:25 +00:00
|
|
|
{ -kHalfSquareSize, -kHalfSquareSize }
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkColor MegaLooperGM::gColors[4] = {
|
|
|
|
SK_ColorGREEN, SK_ColorYELLOW, SK_ColorBLUE, SK_ColorRED
|
|
|
|
};
|
2019-07-18 15:51:19 +00:00
|
|
|
} // namespace
|
2013-06-03 17:13:25 +00:00
|
|
|
|
|
|
|
DEF_GM( return new MegaLooperGM(MegaLooperGM::k0x0_Type); )
|
|
|
|
DEF_GM( return new MegaLooperGM(MegaLooperGM::k4x1_Type); )
|
|
|
|
DEF_GM( return new MegaLooperGM(MegaLooperGM::k1x4_Type); )
|
2019-07-22 18:51:10 +00:00
|
|
|
|
|
|
|
#endif
|