2013-05-14 16:37:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 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"
|
2021-07-13 15:09:52 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
2021-03-05 21:59:01 +00:00
|
|
|
#include "include/core/SkRRect.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2021-07-13 15:09:52 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2013-05-14 16:37:31 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
// Draw various width thin rects at 1/8 horizontal pixel increments
|
|
|
|
class ThinRectsGM : public GM {
|
|
|
|
public:
|
2021-03-05 21:59:01 +00:00
|
|
|
ThinRectsGM(bool round) : fRound(round) {
|
2013-05-14 16:37:31 +00:00
|
|
|
this->setBGColor(0xFF000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2021-03-05 21:59:01 +00:00
|
|
|
return SkString(fRound ? "thinroundrects" : "thinrects");
|
2013-05-14 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(240, 320);
|
2013-05-14 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-05-14 16:37:31 +00:00
|
|
|
|
|
|
|
SkPaint white;
|
|
|
|
white.setColor(SK_ColorWHITE);
|
|
|
|
white.setAntiAlias(true);
|
|
|
|
|
|
|
|
SkPaint green;
|
|
|
|
green.setColor(SK_ColorGREEN);
|
|
|
|
green.setAntiAlias(true);
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; ++i) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(i*0.125f, i*40.0f);
|
2021-03-05 21:59:01 +00:00
|
|
|
this->drawVertRects(canvas, white);
|
2013-05-14 16:37:31 +00:00
|
|
|
|
|
|
|
canvas->translate(40.0f, 0.0f);
|
2021-03-05 21:59:01 +00:00
|
|
|
this->drawVertRects(canvas, green);
|
2013-05-14 16:37:31 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(80.0f, i*40.0f + i*0.125f);
|
2021-03-05 21:59:01 +00:00
|
|
|
this->drawHorizRects(canvas, white);
|
2013-05-14 16:37:31 +00:00
|
|
|
|
|
|
|
canvas->translate(40.0f, 0.0f);
|
2021-03-05 21:59:01 +00:00
|
|
|
this->drawHorizRects(canvas, green);
|
2013-05-14 16:37:31 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
2013-05-15 07:01:09 +00:00
|
|
|
canvas->translate(160.0f + i*0.125f,
|
2013-05-14 16:37:31 +00:00
|
|
|
i*40.0f + i*0.125f);
|
2021-03-05 21:59:01 +00:00
|
|
|
this->drawSquares(canvas, white);
|
2013-05-14 16:37:31 +00:00
|
|
|
|
|
|
|
canvas->translate(40.0f, 0.0f);
|
2021-03-05 21:59:01 +00:00
|
|
|
this->drawSquares(canvas, green);
|
2013-05-14 16:37:31 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-03-05 21:59:01 +00:00
|
|
|
void drawVertRects(SkCanvas* canvas, const SkPaint& p) {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkRect vertRects[] = {
|
2013-05-14 16:37:31 +00:00
|
|
|
{ 1, 1, 5.0f, 21 }, // 4 pix wide
|
|
|
|
{ 8, 1, 10.0f, 21 }, // 2 pix wide
|
|
|
|
{ 13, 1, 14.0f, 21 }, // 1 pix wide
|
|
|
|
{ 17, 1, 17.5f, 21 }, // 1/2 pix wide
|
|
|
|
{ 21, 1, 21.25f, 21 }, // 1/4 pix wide
|
|
|
|
{ 25, 1, 25.125f, 21 }, // 1/8 pix wide
|
|
|
|
{ 29, 1, 29.0f, 21 } // 0 pix wide
|
|
|
|
};
|
|
|
|
|
2021-03-05 21:59:01 +00:00
|
|
|
static constexpr SkVector radii[4] = {{1/32.f, 2/32.f}, {3/32.f, 1/32.f}, {2/32.f, 3/32.f},
|
|
|
|
{1/32.f, 3/32.f}};
|
|
|
|
SkRRect rrect;
|
2013-05-14 16:43:02 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(vertRects); ++j) {
|
2021-03-05 21:59:01 +00:00
|
|
|
if (fRound) {
|
|
|
|
rrect.setRectRadii(vertRects[j], radii);
|
|
|
|
canvas->drawRRect(rrect, p);
|
|
|
|
} else {
|
|
|
|
canvas->drawRect(vertRects[j], p);
|
|
|
|
}
|
2013-05-14 16:37:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 21:59:01 +00:00
|
|
|
void drawHorizRects(SkCanvas* canvas, const SkPaint& p) {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkRect horizRects[] = {
|
2013-05-14 16:37:31 +00:00
|
|
|
{ 1, 1, 21, 5.0f }, // 4 pix high
|
|
|
|
{ 1, 8, 21, 10.0f }, // 2 pix high
|
|
|
|
{ 1, 13, 21, 14.0f }, // 1 pix high
|
|
|
|
{ 1, 17, 21, 17.5f }, // 1/2 pix high
|
|
|
|
{ 1, 21, 21, 21.25f }, // 1/4 pix high
|
|
|
|
{ 1, 25, 21, 25.125f }, // 1/8 pix high
|
|
|
|
{ 1, 29, 21, 29.0f } // 0 pix high
|
|
|
|
};
|
|
|
|
|
2021-03-05 21:59:01 +00:00
|
|
|
SkRRect rrect;
|
2013-05-14 16:43:02 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(horizRects); ++j) {
|
2021-03-05 21:59:01 +00:00
|
|
|
if (fRound) {
|
|
|
|
rrect.setNinePatch(horizRects[j], 1/32.f, 2/32.f, 3/32.f, 4/32.f);
|
|
|
|
canvas->drawRRect(rrect, p);
|
|
|
|
} else {
|
|
|
|
canvas->drawRect(horizRects[j], p);
|
|
|
|
}
|
2013-05-14 16:37:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 21:59:01 +00:00
|
|
|
void drawSquares(SkCanvas* canvas, const SkPaint& p) {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkRect squares[] = {
|
2013-05-14 16:37:31 +00:00
|
|
|
{ 1, 1, 5.0f, 5.0f }, // 4 pix
|
|
|
|
{ 8, 8, 10.0f, 10.0f }, // 2 pix
|
|
|
|
{ 13, 13, 14.0f, 14.0f }, // 1 pix
|
|
|
|
{ 17, 17, 17.5f, 17.5f }, // 1/2 pix
|
|
|
|
{ 21, 21, 21.25f, 21.25f }, // 1/4 pix
|
|
|
|
{ 25, 25, 25.125f, 25.125f }, // 1/8 pix
|
|
|
|
{ 29, 29, 29.0f, 29.0f } // 0 pix
|
|
|
|
};
|
|
|
|
|
2021-03-05 21:59:01 +00:00
|
|
|
SkRRect rrect;
|
2013-05-14 16:43:02 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(squares); ++j) {
|
2021-03-05 21:59:01 +00:00
|
|
|
if (fRound) {
|
|
|
|
rrect.setRectXY(squares[j], 1/32.f, 2/32.f);
|
|
|
|
canvas->drawRRect(rrect, p);
|
|
|
|
} else {
|
|
|
|
canvas->drawRect(squares[j], p);
|
|
|
|
}
|
2013-05-14 16:37:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 21:59:01 +00:00
|
|
|
const bool fRound;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2013-05-14 16:37:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-03-05 21:59:01 +00:00
|
|
|
DEF_GM( return new ThinRectsGM(false); )
|
|
|
|
DEF_GM( return new ThinRectsGM(true); )
|
2013-05-14 16:37:31 +00:00
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|
2021-07-13 15:09:52 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM_CAN_FAIL(clipped_thinrect, canvas, errorMsg, 256, 256) {
|
|
|
|
auto zoomed = canvas->makeSurface(canvas->imageInfo().makeWH(10, 10));
|
|
|
|
if (!zoomed) {
|
|
|
|
errorMsg->printf("makeSurface not supported");
|
|
|
|
return skiagm::DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
auto zoomedCanvas = zoomed->getCanvas();
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
|
|
|
zoomedCanvas->save();
|
|
|
|
zoomedCanvas->clipRect(SkRect::MakeXYWH(0, 5, 256, 10), true /*doAntialias*/);
|
|
|
|
zoomedCanvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 5.5), p);
|
|
|
|
zoomedCanvas->restore();
|
|
|
|
|
|
|
|
// Zoom-in. Should see one line of red representing zoomed in 1/2px coverage and *not*
|
|
|
|
// two lines of varying coverage from hairline rendering.
|
|
|
|
auto img = zoomed->makeImageSnapshot();
|
|
|
|
canvas->drawImageRect(img, SkRect::MakeXYWH(0, 10, 200, 200), SkSamplingOptions());
|
|
|
|
return skiagm::DrawResult::kOk;
|
|
|
|
}
|