2014-01-31 14:48: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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkClipOp.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2020-08-25 15:48:41 +00:00
|
|
|
#include "include/core/SkPathBuilder.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2014-01-31 14:48:58 +00:00
|
|
|
|
2021-01-25 00:49:21 +00:00
|
|
|
static sk_sp<SkImage> make_img(int w, int h) {
|
|
|
|
auto surf = SkSurface::MakeRaster(SkImageInfo::MakeN32(w, h, kOpaque_SkAlphaType));
|
|
|
|
auto canvas = surf->getCanvas();
|
2014-01-31 14:48:58 +00:00
|
|
|
|
|
|
|
SkScalar wScalar = SkIntToScalar(w);
|
|
|
|
SkScalar hScalar = SkIntToScalar(h);
|
|
|
|
|
|
|
|
SkPoint pt = { wScalar / 2, hScalar / 2 };
|
|
|
|
|
2020-02-05 18:34:09 +00:00
|
|
|
SkScalar radius = 3 * std::max(wScalar, hScalar);
|
2014-01-31 14:48:58 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkColor colors[] = {SK_ColorDKGRAY,
|
|
|
|
ToolUtils::color_to_565(0xFF222255),
|
|
|
|
ToolUtils::color_to_565(0xFF331133),
|
|
|
|
ToolUtils::color_to_565(0xFF884422),
|
|
|
|
ToolUtils::color_to_565(0xFF000022),
|
|
|
|
SK_ColorWHITE,
|
|
|
|
ToolUtils::color_to_565(0xFFAABBCC)};
|
2014-01-31 14:48:58 +00:00
|
|
|
|
|
|
|
SkScalar pos[] = {0,
|
|
|
|
SK_Scalar1 / 6,
|
|
|
|
2 * SK_Scalar1 / 6,
|
|
|
|
3 * SK_Scalar1 / 6,
|
|
|
|
4 * SK_Scalar1 / 6,
|
|
|
|
5 * SK_Scalar1 / 6,
|
|
|
|
SK_Scalar1};
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
SkRect rect = SkRect::MakeWH(wScalar, hScalar);
|
|
|
|
SkMatrix mat = SkMatrix::I();
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
2016-03-09 17:50:50 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeRadial(
|
2014-04-28 14:55:39 +00:00
|
|
|
pt, radius,
|
|
|
|
colors, pos,
|
|
|
|
SK_ARRAY_COUNT(colors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kRepeat,
|
2016-03-09 17:50:50 +00:00
|
|
|
0, &mat));
|
2021-01-25 00:49:21 +00:00
|
|
|
canvas->drawRect(rect, paint);
|
2014-01-31 14:48:58 +00:00
|
|
|
rect.inset(wScalar / 8, hScalar / 8);
|
|
|
|
mat.preTranslate(6 * wScalar, 6 * hScalar);
|
|
|
|
mat.postScale(SK_Scalar1 / 3, SK_Scalar1 / 3);
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), wScalar / 2.2f);
|
2019-01-05 21:35:13 +00:00
|
|
|
|
2017-08-28 14:34:05 +00:00
|
|
|
paint.setShader(nullptr);
|
2018-08-16 14:17:03 +00:00
|
|
|
paint.setColor(SK_ColorLTGRAY);
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr char kTxt[] = "Skia";
|
2019-01-05 21:35:13 +00:00
|
|
|
SkPoint texPos = { wScalar / 17, hScalar / 2 + font.getSize() / 2.5f };
|
2021-01-25 00:49:21 +00:00
|
|
|
canvas->drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, SkTextEncoding::kUTF8,
|
|
|
|
texPos.fX, texPos.fY, font, paint);
|
2014-01-31 14:48:58 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(SK_Scalar1);
|
2021-01-25 00:49:21 +00:00
|
|
|
canvas->drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, SkTextEncoding::kUTF8,
|
|
|
|
texPos.fX, texPos.fY, font, paint);
|
|
|
|
return surf->makeImageSnapshot();
|
2014-01-31 14:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
/**
|
|
|
|
* This GM tests convex polygon clips.
|
|
|
|
*/
|
|
|
|
class ConvexPolyClip : public GM {
|
|
|
|
public:
|
|
|
|
ConvexPolyClip() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-01-31 14:48:58 +00:00
|
|
|
return SkString("convex_poly_clip");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-02-07 21:13:11 +00:00
|
|
|
// When benchmarking the saveLayer set of draws is skipped.
|
|
|
|
int w = 435;
|
|
|
|
if (kBench_Mode != this->getMode()) {
|
|
|
|
w *= 2;
|
|
|
|
}
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(w, 540);
|
2014-01-31 14:48:58 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2021-08-10 16:27:25 +00:00
|
|
|
// On < c++17, emplace_back() returns a void :(
|
|
|
|
auto emplace_back = [](std::vector<Clip>& clips) -> Clip& {
|
|
|
|
clips.emplace_back();
|
|
|
|
return clips.back();
|
|
|
|
};
|
|
|
|
|
|
|
|
emplace_back(fClips).setPath(SkPath::Polygon({
|
2020-08-25 15:48:41 +00:00
|
|
|
{ 5.f, 5.f},
|
|
|
|
{100.f, 20.f},
|
|
|
|
{ 15.f, 100.f},
|
|
|
|
}, false));
|
2014-01-31 14:48:58 +00:00
|
|
|
|
2020-08-25 15:48:41 +00:00
|
|
|
SkPathBuilder hexagon;
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kRadius = 45.f;
|
2014-01-31 14:48:58 +00:00
|
|
|
const SkPoint center = { kRadius, kRadius };
|
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
|
|
SkScalar angle = 2 * SK_ScalarPI * i / 6;
|
2019-04-02 14:59:28 +00:00
|
|
|
SkPoint point = { SkScalarCos(angle), SkScalarSin(angle) };
|
2014-01-31 14:48:58 +00:00
|
|
|
point.scale(kRadius);
|
|
|
|
point = center + point;
|
|
|
|
if (0 == i) {
|
|
|
|
hexagon.moveTo(point);
|
|
|
|
} else {
|
|
|
|
hexagon.lineTo(point);
|
|
|
|
}
|
|
|
|
}
|
2021-08-10 16:27:25 +00:00
|
|
|
emplace_back(fClips).setPath(hexagon.snapshot());
|
2014-01-31 14:48:58 +00:00
|
|
|
|
|
|
|
SkMatrix scaleM;
|
|
|
|
scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
|
2021-08-10 16:27:25 +00:00
|
|
|
emplace_back(fClips).setPath(hexagon.detach().makeTransform(scaleM));
|
2014-02-04 15:09:16 +00:00
|
|
|
|
2021-08-10 16:27:25 +00:00
|
|
|
emplace_back(fClips).setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f));
|
2014-01-31 14:48:58 +00:00
|
|
|
|
|
|
|
SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
|
|
|
|
SkMatrix rotM;
|
|
|
|
rotM.setRotate(23.f, rect.centerX(), rect.centerY());
|
2021-08-10 16:27:25 +00:00
|
|
|
emplace_back(fClips).setPath(SkPath::Rect(rect).makeTransform(rotM));
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2021-01-25 00:49:21 +00:00
|
|
|
fImg = make_img(100, 100);
|
2014-01-31 14:48:58 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-01-31 14:48:58 +00:00
|
|
|
SkScalar y = 0;
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kMargin = 10.f;
|
2014-01-31 14:48:58 +00:00
|
|
|
|
|
|
|
SkPaint bgPaint;
|
|
|
|
bgPaint.setAlpha(0x15);
|
2017-02-22 18:21:42 +00:00
|
|
|
SkISize size = canvas->getBaseLayerSize();
|
2021-01-25 00:49:21 +00:00
|
|
|
canvas->drawImageRect(fImg, SkRect::MakeIWH(size.fWidth, size.fHeight),
|
|
|
|
SkSamplingOptions(), &bgPaint);
|
2014-01-31 14:48:58 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr char kTxt[] = "Clip Me!";
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 23);
|
2019-05-07 19:38:46 +00:00
|
|
|
SkScalar textW = font.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1, SkTextEncoding::kUTF8);
|
2014-02-07 21:13:11 +00:00
|
|
|
SkPaint txtPaint;
|
2018-08-16 14:17:03 +00:00
|
|
|
txtPaint.setColor(SK_ColorDKGRAY);
|
2014-02-07 21:13:11 +00:00
|
|
|
|
|
|
|
SkScalar startX = 0;
|
|
|
|
int testLayers = kBench_Mode != this->getMode();
|
|
|
|
for (int doLayer = 0; doLayer <= testLayers; ++doLayer) {
|
2021-08-10 16:27:25 +00:00
|
|
|
for (const Clip& clip : fClips) {
|
2014-02-07 21:13:11 +00:00
|
|
|
SkScalar x = startX;
|
|
|
|
for (int aa = 0; aa < 2; ++aa) {
|
|
|
|
if (doLayer) {
|
|
|
|
SkRect bounds;
|
2021-08-10 16:27:25 +00:00
|
|
|
clip.getBounds(&bounds);
|
2014-02-07 21:13:11 +00:00
|
|
|
bounds.outset(2, 2);
|
|
|
|
bounds.offset(x, y);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&bounds, nullptr);
|
2014-02-07 21:13:11 +00:00
|
|
|
} else {
|
|
|
|
canvas->save();
|
|
|
|
}
|
|
|
|
canvas->translate(x, y);
|
2021-08-10 16:27:25 +00:00
|
|
|
clip.setOnCanvas(canvas, SkClipOp::kIntersect, SkToBool(aa));
|
2021-01-25 00:49:21 +00:00
|
|
|
canvas->drawImage(fImg, 0, 0);
|
2014-02-07 21:13:11 +00:00
|
|
|
canvas->restore();
|
2021-01-25 00:49:21 +00:00
|
|
|
x += fImg->width() + kMargin;
|
2014-02-07 21:13:11 +00:00
|
|
|
}
|
|
|
|
for (int aa = 0; aa < 2; ++aa) {
|
|
|
|
|
|
|
|
SkPaint clipOutlinePaint;
|
|
|
|
clipOutlinePaint.setAntiAlias(true);
|
|
|
|
clipOutlinePaint.setColor(0x50505050);
|
|
|
|
clipOutlinePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
clipOutlinePaint.setStrokeWidth(0);
|
|
|
|
|
|
|
|
if (doLayer) {
|
|
|
|
SkRect bounds;
|
2021-08-10 16:27:25 +00:00
|
|
|
clip.getBounds(&bounds);
|
2014-02-07 21:13:11 +00:00
|
|
|
bounds.outset(2, 2);
|
|
|
|
bounds.offset(x, y);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&bounds, nullptr);
|
2014-02-07 21:13:11 +00:00
|
|
|
} else {
|
|
|
|
canvas->save();
|
|
|
|
}
|
|
|
|
canvas->translate(x, y);
|
2021-08-10 16:27:25 +00:00
|
|
|
SkPath closedClipPath = clip.asClosedPath();
|
2014-02-07 21:13:11 +00:00
|
|
|
canvas->drawPath(closedClipPath, clipOutlinePaint);
|
2021-08-10 16:27:25 +00:00
|
|
|
clip.setOnCanvas(canvas, SkClipOp::kIntersect, SkToBool(aa));
|
2014-02-07 21:13:11 +00:00
|
|
|
canvas->scale(1.f, 1.8f);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, SkTextEncoding::kUTF8,
|
2018-12-15 18:45:33 +00:00
|
|
|
0, 1.5f * font.getSize(), font, txtPaint);
|
2014-02-07 21:13:11 +00:00
|
|
|
canvas->restore();
|
|
|
|
x += textW + 2 * kMargin;
|
|
|
|
}
|
2021-01-25 00:49:21 +00:00
|
|
|
y += fImg->height() + kMargin;
|
2014-01-31 14:48:58 +00:00
|
|
|
}
|
2014-02-07 21:13:11 +00:00
|
|
|
y = 0;
|
2021-01-25 00:49:21 +00:00
|
|
|
startX += 2 * fImg->width() + SkScalarCeilToInt(2 * textW) + 6 * kMargin;
|
2014-01-31 14:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool runAsBench() const override { return true; }
|
2015-01-23 18:31:45 +00:00
|
|
|
|
2014-01-31 14:48:58 +00:00
|
|
|
private:
|
2014-02-04 15:09:16 +00:00
|
|
|
class Clip {
|
|
|
|
public:
|
|
|
|
enum ClipType {
|
|
|
|
kNone_ClipType,
|
|
|
|
kPath_ClipType,
|
|
|
|
kRect_ClipType
|
|
|
|
};
|
|
|
|
|
|
|
|
Clip () : fClipType(kNone_ClipType) {}
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2016-12-09 14:00:50 +00:00
|
|
|
void setOnCanvas(SkCanvas* canvas, SkClipOp op, bool aa) const {
|
2014-02-04 15:09:16 +00:00
|
|
|
switch (fClipType) {
|
|
|
|
case kPath_ClipType:
|
2020-09-09 00:47:09 +00:00
|
|
|
canvas->clipPath(fPathBuilder.snapshot(), op, aa);
|
2014-02-04 15:09:16 +00:00
|
|
|
break;
|
|
|
|
case kRect_ClipType:
|
|
|
|
canvas->clipRect(fRect, op, aa);
|
|
|
|
break;
|
|
|
|
case kNone_ClipType:
|
|
|
|
SkDEBUGFAIL("Uninitialized Clip.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2020-09-09 00:47:09 +00:00
|
|
|
SkPath asClosedPath() const {
|
2014-02-04 15:09:16 +00:00
|
|
|
switch (fClipType) {
|
|
|
|
case kPath_ClipType:
|
2020-09-09 00:47:09 +00:00
|
|
|
return SkPathBuilder(fPathBuilder).close().detach();
|
2014-02-04 15:09:16 +00:00
|
|
|
break;
|
|
|
|
case kRect_ClipType:
|
2020-09-09 00:47:09 +00:00
|
|
|
return SkPath::Rect(fRect);
|
2014-02-04 15:09:16 +00:00
|
|
|
case kNone_ClipType:
|
|
|
|
SkDEBUGFAIL("Uninitialized Clip.");
|
|
|
|
break;
|
|
|
|
}
|
2020-09-09 00:47:09 +00:00
|
|
|
return SkPath();
|
2014-02-04 15:09:16 +00:00
|
|
|
}
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2014-02-04 15:09:16 +00:00
|
|
|
void setPath(const SkPath& path) {
|
|
|
|
fClipType = kPath_ClipType;
|
2020-09-09 00:47:09 +00:00
|
|
|
fPathBuilder = path;
|
2014-02-04 15:09:16 +00:00
|
|
|
}
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2014-02-04 15:09:16 +00:00
|
|
|
void setRect(const SkRect& rect) {
|
|
|
|
fClipType = kRect_ClipType;
|
|
|
|
fRect = rect;
|
2020-09-09 00:47:09 +00:00
|
|
|
fPathBuilder.reset();
|
2014-02-04 15:09:16 +00:00
|
|
|
}
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2014-02-04 15:09:16 +00:00
|
|
|
ClipType getType() const { return fClipType; }
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2014-02-07 21:13:11 +00:00
|
|
|
void getBounds(SkRect* bounds) const {
|
|
|
|
switch (fClipType) {
|
|
|
|
case kPath_ClipType:
|
2020-09-09 00:47:09 +00:00
|
|
|
*bounds = fPathBuilder.computeBounds();
|
2014-02-07 21:13:11 +00:00
|
|
|
break;
|
|
|
|
case kRect_ClipType:
|
|
|
|
*bounds = fRect;
|
|
|
|
break;
|
|
|
|
case kNone_ClipType:
|
|
|
|
SkDEBUGFAIL("Uninitialized Clip.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-04 15:09:16 +00:00
|
|
|
private:
|
|
|
|
ClipType fClipType;
|
2020-09-09 00:47:09 +00:00
|
|
|
SkPathBuilder fPathBuilder;
|
2014-02-04 15:09:16 +00:00
|
|
|
SkRect fRect;
|
|
|
|
};
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2021-08-10 16:27:25 +00:00
|
|
|
std::vector<Clip> fClips;
|
|
|
|
sk_sp<SkImage> fImg;;
|
2014-01-31 14:48:58 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2014-01-31 14:48:58 +00:00
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new ConvexPolyClip;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|