2015-05-27 22:08:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2016-11-15 21:44:34 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkTextUtils.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2015-05-27 22:08:33 +00:00
|
|
|
|
|
|
|
enum {
|
2016-11-15 21:44:34 +00:00
|
|
|
kXfermodeCount = (int)SkBlendMode::kLastMode + 1 + 1, // extra for arith
|
2015-05-27 22:08:33 +00:00
|
|
|
kShapeSize = 22,
|
|
|
|
kShapeSpacing = 36,
|
|
|
|
kShapeTypeSpacing = 4 * kShapeSpacing / 3,
|
2015-12-17 18:58:28 +00:00
|
|
|
kPaintSpacing = 4 * kShapeTypeSpacing,
|
2015-05-27 22:08:33 +00:00
|
|
|
kLabelSpacing = 3 * kShapeSize,
|
|
|
|
kMargin = kShapeSpacing / 2,
|
|
|
|
kXfermodeTypeSpacing = kLabelSpacing + 2 * kPaintSpacing + kShapeTypeSpacing,
|
|
|
|
kTitleSpacing = 3 * kShapeSpacing / 4,
|
|
|
|
kSubtitleSpacing = 5 * kShapeSpacing / 8
|
|
|
|
};
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColor kBGColor = 0xc8d2b887;
|
2015-05-27 22:08:33 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColor kShapeColors[2] = {
|
|
|
|
0x82ff0080, // input color unknown
|
|
|
|
0xff00ffff, // input color opaque
|
2015-05-27 22:08:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum Shape {
|
|
|
|
kSquare_Shape,
|
|
|
|
kDiamond_Shape,
|
|
|
|
kOval_Shape,
|
2015-12-17 18:58:28 +00:00
|
|
|
kConcave_Shape,
|
2015-05-27 22:08:33 +00:00
|
|
|
|
2015-12-17 18:58:28 +00:00
|
|
|
kLast_Shape = kConcave_Shape
|
2015-05-27 22:08:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies AA works properly on all Xfermodes, including arithmetic, with both opaque and unknown
|
|
|
|
* src colors.
|
|
|
|
*/
|
2016-09-26 22:51:24 +00:00
|
|
|
class AAXfermodesGM : public skiagm::GM {
|
2015-05-27 22:08:33 +00:00
|
|
|
public:
|
|
|
|
AAXfermodesGM() {}
|
|
|
|
|
|
|
|
protected:
|
2015-06-10 16:31:09 +00:00
|
|
|
enum DrawingPass {
|
|
|
|
kCheckerboard_Pass,
|
|
|
|
kBackground_Pass,
|
|
|
|
kShape_Pass
|
|
|
|
};
|
|
|
|
|
2015-05-27 22:08:33 +00:00
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("aaxfermodes");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(2 * kMargin + 2 * kXfermodeTypeSpacing -
|
|
|
|
(kXfermodeTypeSpacing - (kLabelSpacing + 2 * kPaintSpacing)),
|
|
|
|
2 * kMargin + kTitleSpacing + kSubtitleSpacing +
|
2016-11-15 21:44:34 +00:00
|
|
|
(1 + (int)SkBlendMode::kLastCoeffMode) * kShapeSpacing);
|
2015-05-27 22:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
fLabelFont.setTypeface(ToolUtils::create_portable_typeface());
|
2019-01-03 20:45:53 +00:00
|
|
|
fLabelFont.setSize(5 * kShapeSize/8);
|
|
|
|
fLabelFont.setSubpixel(true);
|
2015-05-27 22:08:33 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar radius = -1.4f * kShapeSize/2;
|
2015-05-27 22:08:33 +00:00
|
|
|
SkPoint pts[4] = {
|
|
|
|
{-radius, 0},
|
|
|
|
{0, -1.33f * radius},
|
|
|
|
{radius, 0},
|
|
|
|
{0, 1.33f * radius}
|
|
|
|
};
|
2015-12-17 18:58:28 +00:00
|
|
|
fOval.moveTo(pts[0]);
|
|
|
|
fOval.quadTo(pts[1], pts[2]);
|
|
|
|
fOval.quadTo(pts[3], pts[0]);
|
|
|
|
|
|
|
|
fConcave.moveTo(-radius, 0);
|
|
|
|
fConcave.quadTo(0, 0, 0, -radius);
|
|
|
|
fConcave.quadTo(0, 0, radius, 0);
|
|
|
|
fConcave.quadTo(0, 0, 0, radius);
|
|
|
|
fConcave.quadTo(0, 0, -radius, 0);
|
|
|
|
fConcave.close();
|
2015-05-27 22:08:33 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 16:31:09 +00:00
|
|
|
void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) {
|
|
|
|
SkRect clipRect =
|
|
|
|
{ -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 };
|
2015-05-27 22:08:33 +00:00
|
|
|
|
2015-06-10 16:31:09 +00:00
|
|
|
canvas->save();
|
|
|
|
if (kCheckerboard_Pass == drawingPass) {
|
|
|
|
canvas->translate(kMargin, kMargin);
|
|
|
|
}
|
2015-05-27 22:08:33 +00:00
|
|
|
canvas->translate(0, kTitleSpacing);
|
|
|
|
|
|
|
|
for (size_t xfermodeSet = 0; xfermodeSet < 2; xfermodeSet++) {
|
2016-11-15 21:44:34 +00:00
|
|
|
size_t firstMode = ((size_t)SkBlendMode::kLastCoeffMode + 1) * xfermodeSet;
|
2015-05-27 22:08:33 +00:00
|
|
|
canvas->save();
|
|
|
|
|
2015-06-10 16:31:09 +00:00
|
|
|
if (kShape_Pass == drawingPass) {
|
2018-10-25 16:36:06 +00:00
|
|
|
SkTextUtils::DrawString(canvas, "Src Unknown",
|
2015-12-17 18:58:28 +00:00
|
|
|
kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2,
|
2019-01-03 20:45:53 +00:00
|
|
|
kSubtitleSpacing / 2 + fLabelFont.getSize() / 3, fLabelFont, SkPaint(),
|
2018-10-30 16:53:21 +00:00
|
|
|
SkTextUtils::kCenter_Align);
|
2018-10-25 16:36:06 +00:00
|
|
|
SkTextUtils::DrawString(canvas, "Src Opaque",
|
2016-03-29 16:03:52 +00:00
|
|
|
kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 +
|
2019-01-03 20:45:53 +00:00
|
|
|
kPaintSpacing, kSubtitleSpacing / 2 + fLabelFont.getSize() / 3,
|
|
|
|
fLabelFont, SkPaint(), SkTextUtils::kCenter_Align);
|
2015-06-10 16:31:09 +00:00
|
|
|
}
|
2015-05-27 22:08:33 +00:00
|
|
|
|
|
|
|
canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2);
|
|
|
|
|
2016-11-15 21:44:34 +00:00
|
|
|
for (size_t m = 0; m <= (size_t)SkBlendMode::kLastCoeffMode; m++) {
|
|
|
|
if (firstMode + m > (size_t)SkBlendMode::kLastMode) {
|
2016-09-26 22:51:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-10-06 00:33:02 +00:00
|
|
|
SkBlendMode mode = static_cast<SkBlendMode>(firstMode + m);
|
2015-05-27 22:08:33 +00:00
|
|
|
canvas->save();
|
|
|
|
|
2015-06-10 16:31:09 +00:00
|
|
|
if (kShape_Pass == drawingPass) {
|
|
|
|
this->drawModeName(canvas, mode);
|
|
|
|
}
|
2015-05-27 22:08:33 +00:00
|
|
|
canvas->translate(kLabelSpacing + kShapeSpacing/2, 0);
|
|
|
|
|
|
|
|
for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors); colorIdx++) {
|
|
|
|
SkPaint paint;
|
|
|
|
this->setupShapePaint(canvas, kShapeColors[colorIdx], mode, &paint);
|
|
|
|
SkASSERT(colorIdx == 0 || 255 == paint.getAlpha());
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx++) {
|
2015-06-10 16:31:09 +00:00
|
|
|
if (kShape_Pass != drawingPass) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(clipRect);
|
|
|
|
if (kCheckerboard_Pass == drawingPass) {
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 10);
|
2015-06-10 16:31:09 +00:00
|
|
|
} else {
|
|
|
|
SkASSERT(kBackground_Pass == drawingPass);
|
2016-10-06 00:33:02 +00:00
|
|
|
canvas->drawColor(kBGColor, SkBlendMode::kSrc);
|
2015-06-10 16:31:09 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
} else {
|
|
|
|
this->drawShape(canvas, static_cast<Shape>(shapeIdx), paint, mode);
|
|
|
|
}
|
2015-05-27 22:08:33 +00:00
|
|
|
canvas->translate(kShapeTypeSpacing, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(kPaintSpacing, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, kShapeSpacing);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(kXfermodeTypeSpacing, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2015-06-10 16:31:09 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
draw_pass(canvas, kCheckerboard_Pass);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(nullptr, nullptr);
|
2015-06-10 16:31:09 +00:00
|
|
|
|
|
|
|
canvas->translate(kMargin, kMargin);
|
|
|
|
draw_pass(canvas, kBackground_Pass);
|
|
|
|
|
2019-01-03 20:45:53 +00:00
|
|
|
SkFont titleFont(fLabelFont);
|
|
|
|
titleFont.setSize(9 * titleFont.getSize() / 8);
|
|
|
|
titleFont.setEmbolden(true);
|
2018-10-25 16:36:06 +00:00
|
|
|
SkTextUtils::DrawString(canvas, "Porter Duff",
|
|
|
|
kLabelSpacing + 4 * kShapeTypeSpacing,
|
2019-01-03 20:45:53 +00:00
|
|
|
kTitleSpacing / 2 + titleFont.getSize() / 3, titleFont, SkPaint(),
|
2018-10-30 16:53:21 +00:00
|
|
|
SkTextUtils::kCenter_Align);
|
2018-10-25 16:36:06 +00:00
|
|
|
SkTextUtils::DrawString(canvas, "Advanced",
|
|
|
|
kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSpacing,
|
2019-01-03 20:45:53 +00:00
|
|
|
kTitleSpacing / 2 + titleFont.getSize() / 3, titleFont, SkPaint(),
|
2018-10-30 16:53:21 +00:00
|
|
|
SkTextUtils::kCenter_Align);
|
2015-06-10 16:31:09 +00:00
|
|
|
|
|
|
|
draw_pass(canvas, kShape_Pass);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
void drawModeName(SkCanvas* canvas, SkBlendMode mode) {
|
2016-11-15 21:44:34 +00:00
|
|
|
const char* modeName = SkBlendMode_Name(mode);
|
2018-10-25 16:36:06 +00:00
|
|
|
SkTextUtils::DrawString(canvas, modeName, kLabelSpacing - kShapeSize / 4,
|
2019-01-03 20:45:53 +00:00
|
|
|
fLabelFont.getSize() / 4, fLabelFont, SkPaint(),
|
2018-10-30 16:53:21 +00:00
|
|
|
SkTextUtils::kRight_Align);
|
2015-05-27 22:08:33 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:45:23 +00:00
|
|
|
void setupShapePaint(SkCanvas* canvas, SkColor color, SkBlendMode mode, SkPaint* paint) {
|
2015-05-27 22:08:33 +00:00
|
|
|
paint->setColor(color);
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
if (mode == SkBlendMode::kPlus) {
|
2015-05-27 22:08:33 +00:00
|
|
|
// Check for overflow, otherwise we might get confusing AA artifacts.
|
|
|
|
int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color),
|
|
|
|
SkColorGetR(kBGColor) + SkColorGetR(color)),
|
|
|
|
SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color),
|
|
|
|
SkColorGetB(kBGColor) + SkColorGetB(color)));
|
|
|
|
|
|
|
|
if (maxSum > 255) {
|
|
|
|
SkPaint dimPaint;
|
|
|
|
dimPaint.setAntiAlias(false);
|
2016-10-06 00:33:02 +00:00
|
|
|
dimPaint.setBlendMode(SkBlendMode::kDstIn);
|
2015-05-27 22:08:33 +00:00
|
|
|
if (255 != paint->getAlpha()) {
|
|
|
|
// Dim the src and dst colors.
|
|
|
|
dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0);
|
|
|
|
paint->setAlpha(255 * paint->getAlpha() / maxSum);
|
|
|
|
} else {
|
|
|
|
// Just clear the dst, we need to preserve the paint's opacity.
|
|
|
|
dimPaint.setARGB(0, 0, 0, 0);
|
|
|
|
}
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawRect({ -kShapeSpacing/2, -kShapeSpacing/2,
|
|
|
|
kShapeSpacing/2 + 3 * kShapeTypeSpacing, kShapeSpacing/2 },
|
|
|
|
dimPaint);
|
2015-05-27 22:08:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkBlendMode mode) {
|
|
|
|
SkASSERT(mode <= SkBlendMode::kLastMode);
|
2015-05-27 22:08:33 +00:00
|
|
|
SkPaint shapePaint(paint);
|
|
|
|
shapePaint.setAntiAlias(kSquare_Shape != shape);
|
2016-10-06 00:33:02 +00:00
|
|
|
shapePaint.setBlendMode(mode);
|
2015-05-27 22:08:33 +00:00
|
|
|
|
|
|
|
switch (shape) {
|
|
|
|
case kSquare_Shape:
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawRect({ -kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2 },
|
|
|
|
shapePaint);
|
2015-05-27 22:08:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kDiamond_Shape:
|
|
|
|
canvas->save();
|
|
|
|
canvas->rotate(45);
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawRect({ -kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2 },
|
|
|
|
shapePaint);
|
2015-05-27 22:08:33 +00:00
|
|
|
canvas->restore();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kOval_Shape:
|
|
|
|
canvas->save();
|
2016-10-06 00:33:02 +00:00
|
|
|
canvas->rotate(static_cast<SkScalar>((511 * (int)mode + 257) % 360));
|
2015-12-17 18:58:28 +00:00
|
|
|
canvas->drawPath(fOval, shapePaint);
|
2015-05-27 22:08:33 +00:00
|
|
|
canvas->restore();
|
|
|
|
break;
|
|
|
|
|
2015-12-17 18:58:28 +00:00
|
|
|
case kConcave_Shape:
|
|
|
|
canvas->drawPath(fConcave, shapePaint);
|
|
|
|
break;
|
|
|
|
|
2015-05-27 22:08:33 +00:00
|
|
|
default:
|
2017-08-16 14:53:04 +00:00
|
|
|
SK_ABORT("Invalid shape.");
|
2015-05-27 22:08:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-01-03 20:45:53 +00:00
|
|
|
SkFont fLabelFont;
|
2015-12-17 18:58:28 +00:00
|
|
|
SkPath fOval;
|
|
|
|
SkPath fConcave;
|
2015-05-27 22:08:33 +00:00
|
|
|
|
2016-09-26 22:51:24 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
2015-05-27 22:08:33 +00:00
|
|
|
};
|
2016-09-26 22:51:24 +00:00
|
|
|
DEF_GM( return new AAXfermodesGM; )
|