2015-04-22 15:51:38 +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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkShader.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2015-04-22 15:51:38 +00:00
|
|
|
|
|
|
|
// This class of GMs test how edges/verts snap near rounding boundaries in device space without
|
|
|
|
// anti-aliaing.
|
|
|
|
class PixelSnapGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
PixelSnapGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// kTrans should be even or checkboards wont agree in different test cases.
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kTrans = 14;
|
|
|
|
inline static constexpr int kLabelPad = 4;
|
2015-04-22 15:51:38 +00:00
|
|
|
// The inverse of this value should be a perfect SkScalar.
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kSubPixelSteps = 8;
|
|
|
|
inline static constexpr int kLabelTextSize = 9;
|
2015-04-22 15:51:38 +00:00
|
|
|
|
2015-08-20 14:57:51 +00:00
|
|
|
static_assert(kSubPixelSteps < 99, "label_offset_too_small");
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kLabelOffsetX = 2 * kLabelTextSize + kLabelPad;
|
|
|
|
inline static constexpr int kLabelOffsetY = kLabelTextSize + kLabelPad;
|
2015-04-22 18:34:46 +00:00
|
|
|
|
2015-04-22 15:51:38 +00:00
|
|
|
SkISize onISize() override {
|
2015-04-22 18:34:46 +00:00
|
|
|
return SkISize::Make((kSubPixelSteps + 1) * kTrans + kLabelOffsetX + kLabelPad,
|
|
|
|
(kSubPixelSteps + 1) * kTrans + kLabelOffsetY + kLabelPad);
|
2015-04-22 15:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
SkPaint bgPaint;
|
2019-03-20 16:12:10 +00:00
|
|
|
bgPaint.setShader(ToolUtils::create_checkerboard_shader(0xFFAAAAAA, 0xFF777777, 1));
|
2015-04-22 15:51:38 +00:00
|
|
|
canvas->drawPaint(bgPaint);
|
|
|
|
|
|
|
|
SkString offset;
|
|
|
|
SkPaint labelPaint;
|
|
|
|
labelPaint.setColor(SK_ColorWHITE);
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), SkIntToScalar(kLabelTextSize));
|
2015-04-22 15:51:38 +00:00
|
|
|
SkPaint linePaint;
|
|
|
|
linePaint.setColor(SK_ColorWHITE);
|
|
|
|
|
2015-07-28 17:37:53 +00:00
|
|
|
// Draw row labels
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(0, SkIntToScalar(kLabelOffsetY));
|
|
|
|
for (int i = 0; i <= kSubPixelSteps; ++i) {
|
|
|
|
offset.printf("%d", i);
|
2019-01-08 14:38:02 +00:00
|
|
|
canvas->drawString(offset, 0, i * kTrans + SkIntToScalar(kLabelTextSize),
|
|
|
|
font, labelPaint);
|
2015-07-28 17:37:53 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2015-04-22 15:51:38 +00:00
|
|
|
|
2015-07-28 17:37:53 +00:00
|
|
|
// Draw col labels
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(kLabelOffsetX), 0);
|
|
|
|
for (int i = 0; i <= kSubPixelSteps; ++i) {
|
|
|
|
offset.printf("%d", i);
|
2019-01-08 14:38:02 +00:00
|
|
|
canvas->drawString(offset, i * SkIntToScalar(kTrans), SkIntToScalar(kLabelTextSize),
|
|
|
|
font, labelPaint);
|
2015-07-28 17:37:53 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2015-04-22 15:51:38 +00:00
|
|
|
|
2015-04-22 18:34:46 +00:00
|
|
|
canvas->translate(SkIntToScalar(kLabelOffsetX), SkIntToScalar(kLabelOffsetY));
|
2015-04-22 15:51:38 +00:00
|
|
|
|
|
|
|
// Draw test case grid lines (Draw them all at pixel centers to hopefully avoid any
|
|
|
|
// snapping issues).
|
|
|
|
for (int i = 0; i <= kSubPixelSteps + 1; ++i) {
|
|
|
|
canvas->drawLine(0.5f,
|
|
|
|
i * SkIntToScalar(kTrans) + 0.5f,
|
|
|
|
SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f,
|
|
|
|
i * SkIntToScalar(kTrans) + 0.5f,
|
|
|
|
linePaint);
|
|
|
|
canvas->drawLine(i * SkIntToScalar(kTrans) + 0.5f,
|
|
|
|
0.5f,
|
|
|
|
i * SkIntToScalar(kTrans) + 0.5f,
|
|
|
|
SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f,
|
|
|
|
linePaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i <= kSubPixelSteps; ++i) {
|
|
|
|
for (int j = 0; j <= kSubPixelSteps; ++j) {
|
|
|
|
canvas->save();
|
|
|
|
// +1's account for the grid lines around each test case.
|
|
|
|
canvas->translate(j * (kTrans + 1.f/kSubPixelSteps) + 1,
|
|
|
|
i * (kTrans + 1.f/kSubPixelSteps) + 1);
|
|
|
|
this->drawElement(canvas);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void drawElement(SkCanvas*) = 0;
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2015-04-22 15:51:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class PointSnapGM : public PixelSnapGM {
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override { return SkString("pixel_snap_point"); }
|
2017-02-22 18:21:42 +00:00
|
|
|
void drawElement(SkCanvas* canvas) override {
|
|
|
|
const SkPoint pt = { 1, 1 };
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, &pt, paint);
|
|
|
|
}
|
2015-04-22 15:51:38 +00:00
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = PixelSnapGM;
|
2015-04-22 15:51:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class LineSnapGM : public PixelSnapGM {
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override { return SkString("pixel_snap_line"); }
|
|
|
|
void drawElement(SkCanvas* canvas) override {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
// Draw a horizontal and vertical line, each length 3.
|
|
|
|
canvas->drawLine(1, 1, 4, 1, paint);
|
|
|
|
canvas->drawLine(6, 1, 6, 4, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = PixelSnapGM;
|
2015-04-22 15:51:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class RectSnapGM : public PixelSnapGM {
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override { return SkString("pixel_snap_rect"); }
|
|
|
|
void drawElement(SkCanvas* canvas) override {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(1, 1, 3, 3), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = PixelSnapGM;
|
2015-04-22 15:51:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ComboSnapGM : public PixelSnapGM {
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override { return SkString("pixel_snap_combo"); }
|
|
|
|
void drawElement(SkCanvas* canvas) override {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(false);
|
|
|
|
// A rectangle that exactly covers a pixel, a point at each corner, 8 horiz/vert lines
|
|
|
|
// at rect corners (two at each corner, extending away from rect). They are drawn in this
|
|
|
|
// order lines (green), points (blue), rect(red).
|
|
|
|
SkRect rect = SkRect::MakeXYWH(3, 3, 1, 1);
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
2017-02-22 18:21:42 +00:00
|
|
|
const SkPoint lines[] = {
|
|
|
|
{ 3, 3 }, { 0, 3 },
|
|
|
|
{ 3, 3 }, { 3, 0 },
|
|
|
|
{ 4, 3 }, { 7, 3 },
|
|
|
|
{ 4, 3 }, { 4, 0 },
|
|
|
|
{ 3, 4 }, { 0, 4 },
|
|
|
|
{ 3, 4 }, { 3, 7 },
|
|
|
|
{ 4, 4 }, { 7, 4 },
|
|
|
|
{ 4, 4 }, { 4, 7 },
|
|
|
|
};
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, SK_ARRAY_COUNT(lines), lines, paint);
|
|
|
|
|
|
|
|
const SkPoint pts[] = {
|
|
|
|
{ 4, 3 }, { 4, 4, }, { 3, 3 }, { 3, 4 },
|
|
|
|
};
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(pts), pts, paint);
|
|
|
|
|
2015-04-22 15:51:38 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = PixelSnapGM;
|
2015-04-22 15:51:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new PointSnapGM;)
|
|
|
|
DEF_GM(return new LineSnapGM;)
|
|
|
|
DEF_GM(return new RectSnapGM;)
|
|
|
|
DEF_GM(return new ComboSnapGM;)
|