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-03-20 16:12:10 +00:00
|
|
|
#include "ToolUtils.h"
|
2014-01-31 14:48:58 +00:00
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
2018-12-15 18:45:33 +00:00
|
|
|
#include "SkFont.h"
|
2014-01-31 14:48:58 +00:00
|
|
|
#include "SkGradientShader.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2014-01-31 14:48:58 +00:00
|
|
|
#include "SkTLList.h"
|
|
|
|
|
|
|
|
static SkBitmap make_bmp(int w, int h) {
|
|
|
|
SkBitmap bmp;
|
|
|
|
bmp.allocN32Pixels(w, h, true);
|
|
|
|
|
|
|
|
SkCanvas canvas(bmp);
|
|
|
|
SkScalar wScalar = SkIntToScalar(w);
|
|
|
|
SkScalar hScalar = SkIntToScalar(h);
|
|
|
|
|
|
|
|
SkPoint pt = { wScalar / 2, hScalar / 2 };
|
|
|
|
|
|
|
|
SkScalar radius = 3 * SkMaxScalar(wScalar, hScalar);
|
|
|
|
|
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));
|
2014-01-31 14:48:58 +00:00
|
|
|
canvas.drawRect(rect, paint);
|
|
|
|
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 };
|
|
|
|
canvas.drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding,
|
|
|
|
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);
|
2019-01-05 21:35:13 +00:00
|
|
|
canvas.drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding,
|
|
|
|
texPos.fX, texPos.fY, font, paint);
|
2014-01-31 14:48:58 +00:00
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2014-01-31 14:48:58 +00:00
|
|
|
SkPath tri;
|
|
|
|
tri.moveTo(5.f, 5.f);
|
|
|
|
tri.lineTo(100.f, 20.f);
|
|
|
|
tri.lineTo(15.f, 100.f);
|
|
|
|
|
2014-02-04 15:09:16 +00:00
|
|
|
fClips.addToTail()->setPath(tri);
|
2014-01-31 14:48:58 +00:00
|
|
|
|
|
|
|
SkPath 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);
|
|
|
|
}
|
|
|
|
}
|
2014-02-04 15:09:16 +00:00
|
|
|
fClips.addToTail()->setPath(hexagon);
|
2014-01-31 14:48:58 +00:00
|
|
|
|
|
|
|
SkMatrix scaleM;
|
|
|
|
scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
|
|
|
|
hexagon.transform(scaleM);
|
2014-02-04 15:09:16 +00:00
|
|
|
fClips.addToTail()->setPath(hexagon);
|
|
|
|
|
|
|
|
fClips.addToTail()->setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f));
|
2014-01-31 14:48:58 +00:00
|
|
|
|
|
|
|
SkPath rotRect;
|
|
|
|
SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
|
|
|
|
rotRect.addRect(rect);
|
|
|
|
SkMatrix rotM;
|
|
|
|
rotM.setRotate(23.f, rect.centerX(), rect.centerY());
|
|
|
|
rotRect.transform(rotM);
|
2014-02-04 15:09:16 +00:00
|
|
|
fClips.addToTail()->setPath(rotRect);
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2014-01-31 14:48:58 +00:00
|
|
|
fBmp = make_bmp(100, 100);
|
|
|
|
}
|
|
|
|
|
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();
|
2015-07-14 17:54:12 +00:00
|
|
|
canvas->drawBitmapRect(fBmp, SkRect::MakeIWH(size.fWidth, size.fHeight), &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);
|
2018-12-15 18:45:33 +00:00
|
|
|
SkScalar textW = font.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding);
|
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) {
|
2015-11-19 03:01:12 +00:00
|
|
|
for (ClipList::Iter iter(fClips, ClipList::Iter::kHead_IterStart);
|
2014-09-05 20:34:00 +00:00
|
|
|
iter.get();
|
2014-02-07 21:13:11 +00:00
|
|
|
iter.next()) {
|
|
|
|
const Clip* clip = iter.get();
|
|
|
|
SkScalar x = startX;
|
|
|
|
for (int aa = 0; aa < 2; ++aa) {
|
|
|
|
if (doLayer) {
|
|
|
|
SkRect bounds;
|
|
|
|
clip->getBounds(&bounds);
|
|
|
|
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);
|
2016-12-09 14:00:50 +00:00
|
|
|
clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa));
|
2014-02-07 21:13:11 +00:00
|
|
|
canvas->drawBitmap(fBmp, 0, 0);
|
|
|
|
canvas->restore();
|
|
|
|
x += fBmp.width() + kMargin;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
clip->getBounds(&bounds);
|
|
|
|
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);
|
|
|
|
SkPath closedClipPath;
|
|
|
|
clip->asClosedPath(&closedClipPath);
|
|
|
|
canvas->drawPath(closedClipPath, clipOutlinePaint);
|
2016-12-09 14:00:50 +00:00
|
|
|
clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa));
|
2014-02-07 21:13:11 +00:00
|
|
|
canvas->scale(1.f, 1.8f);
|
2018-12-15 18:45:33 +00:00
|
|
|
canvas->drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding,
|
|
|
|
0, 1.5f * font.getSize(), font, txtPaint);
|
2014-02-07 21:13:11 +00:00
|
|
|
canvas->restore();
|
|
|
|
x += textW + 2 * kMargin;
|
|
|
|
}
|
|
|
|
y += fBmp.height() + kMargin;
|
2014-01-31 14:48:58 +00:00
|
|
|
}
|
2014-02-07 21:13:11 +00:00
|
|
|
y = 0;
|
|
|
|
startX += 2 * fBmp.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:
|
|
|
|
canvas->clipPath(fPath, op, aa);
|
|
|
|
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
|
|
|
|
2014-02-04 15:09:16 +00:00
|
|
|
void asClosedPath(SkPath* path) const {
|
|
|
|
switch (fClipType) {
|
|
|
|
case kPath_ClipType:
|
|
|
|
*path = fPath;
|
|
|
|
path->close();
|
|
|
|
break;
|
|
|
|
case kRect_ClipType:
|
|
|
|
path->reset();
|
|
|
|
path->addRect(fRect);
|
|
|
|
break;
|
|
|
|
case kNone_ClipType:
|
|
|
|
SkDEBUGFAIL("Uninitialized Clip.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2014-02-04 15:09:16 +00:00
|
|
|
void setPath(const SkPath& path) {
|
|
|
|
fClipType = kPath_ClipType;
|
|
|
|
fPath = path;
|
|
|
|
}
|
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;
|
|
|
|
fPath.reset();
|
|
|
|
}
|
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:
|
|
|
|
*bounds = fPath.getBounds();
|
|
|
|
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;
|
|
|
|
SkPath fPath;
|
|
|
|
SkRect fRect;
|
|
|
|
};
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2015-11-19 03:01:12 +00:00
|
|
|
typedef SkTLList<Clip, 1> ClipList;
|
|
|
|
ClipList fClips;
|
2014-01-31 14:48:58 +00:00
|
|
|
SkBitmap fBmp;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new ConvexPolyClip;)
|
2014-01-31 14:48:58 +00:00
|
|
|
}
|