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
|
|
|
|
2015-05-27 22:08:33 +00:00
|
|
|
#include "gm.h"
|
2016-11-15 21:44:34 +00:00
|
|
|
#include "SkBlendModePriv.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2015-05-27 22:08:33 +00:00
|
|
|
#include "SkShader.h"
|
|
|
|
|
|
|
|
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 {
|
|
|
|
fLabelPaint.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&fLabelPaint);
|
2015-05-27 22:08:33 +00:00
|
|
|
fLabelPaint.setTextSize(5 * kShapeSize/8);
|
|
|
|
fLabelPaint.setSubpixelText(true);
|
|
|
|
|
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) {
|
|
|
|
fLabelPaint.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
canvas->drawText("Src Unknown", sizeof("Src Unknown") - 1,
|
2015-12-17 18:58:28 +00:00
|
|
|
kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2,
|
2015-06-10 16:31:09 +00:00
|
|
|
kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint);
|
|
|
|
canvas->drawText("Src Opaque", sizeof("Src Opaque") - 1,
|
2016-03-29 16:03:52 +00:00
|
|
|
kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 +
|
|
|
|
kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3,
|
2015-12-17 18:58:28 +00:00
|
|
|
fLabelPaint);
|
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) {
|
|
|
|
sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6,
|
|
|
|
10);
|
|
|
|
} 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);
|
|
|
|
|
|
|
|
SkPaint titlePaint(fLabelPaint);
|
|
|
|
titlePaint.setTextSize(9 * titlePaint.getTextSize() / 8);
|
|
|
|
titlePaint.setFakeBoldText(true);
|
|
|
|
titlePaint.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
canvas->drawText("Porter Duff", sizeof("Porter Duff") - 1,
|
2015-12-17 18:58:28 +00:00
|
|
|
kLabelSpacing + 4 * kShapeTypeSpacing,
|
2015-06-10 16:31:09 +00:00
|
|
|
kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
|
|
|
|
canvas->drawText("Advanced", sizeof("Advanced") - 1,
|
2015-12-17 18:58:28 +00:00
|
|
|
kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSpacing,
|
2015-06-10 16:31:09 +00:00
|
|
|
kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
|
|
|
|
|
|
|
|
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);
|
2015-05-27 22:08:33 +00:00
|
|
|
fLabelPaint.setTextAlign(SkPaint::kRight_Align);
|
|
|
|
canvas->drawText(modeName, strlen(modeName), kLabelSpacing - kShapeSize / 4,
|
2015-12-17 18:58:28 +00:00
|
|
|
fLabelPaint.getTextSize() / 4, fLabelPaint);
|
2015-05-27 22:08:33 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
void setupShapePaint(SkCanvas* canvas, GrColor 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:
|
|
|
|
SkFAIL("Invalid shape.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkPaint fLabelPaint;
|
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; )
|