2013-08-20 12:11: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"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRRect.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkRandom.h"
|
2013-08-20 12:11:31 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
// Test out various combinations of nested rects, ovals and rrects.
|
|
|
|
class NestedGM : public GM {
|
|
|
|
public:
|
2015-05-19 11:35:38 +00:00
|
|
|
NestedGM(bool doAA, bool flipped) : fDoAA(doAA), fFlipped(flipped) {
|
2018-08-16 14:17:03 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2013-08-20 12:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-08-20 12:11:31 +00:00
|
|
|
SkString name("nested");
|
2015-05-19 11:35:38 +00:00
|
|
|
if (fFlipped) {
|
|
|
|
name.append("_flipY");
|
|
|
|
}
|
2013-08-20 12:11:31 +00:00
|
|
|
if (fDoAA) {
|
|
|
|
name.append("_aa");
|
|
|
|
} else {
|
|
|
|
name.append("_bw");
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(kImageWidth, kImageHeight);
|
2013-08-20 12:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Shapes {
|
|
|
|
kRect_Shape = 0,
|
|
|
|
kRRect_Shape,
|
|
|
|
kOval_Shape,
|
|
|
|
kShapeCount
|
|
|
|
};
|
|
|
|
|
2019-11-22 18:34:02 +00:00
|
|
|
static void AddShape(SkPath* path, const SkRect& rect, Shapes shape, SkPathDirection dir) {
|
2013-08-20 12:11:31 +00:00
|
|
|
switch (shape) {
|
|
|
|
case kRect_Shape:
|
|
|
|
path->addRect(rect, dir);
|
|
|
|
break;
|
|
|
|
case kRRect_Shape: {
|
|
|
|
SkRRect rr;
|
|
|
|
rr.setRectXY(rect, 5, 5);
|
|
|
|
path->addRRect(rr, dir);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kOval_Shape:
|
|
|
|
path->addOval(rect, dir);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-08-20 12:11:31 +00:00
|
|
|
|
|
|
|
SkPaint shapePaint;
|
|
|
|
shapePaint.setColor(SK_ColorBLACK);
|
|
|
|
shapePaint.setAntiAlias(fDoAA);
|
|
|
|
|
|
|
|
SkRect outerRect = SkRect::MakeWH(40, 40);
|
|
|
|
|
|
|
|
SkRect innerRects[] = {
|
|
|
|
{ 10, 10, 30, 30 }, // small
|
|
|
|
{ .5f, 18, 4.5f, 22 } // smaller and offset to left
|
|
|
|
};
|
|
|
|
|
|
|
|
// draw a background pattern to make transparency errors more apparent
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-08-20 12:11:31 +00:00
|
|
|
|
|
|
|
for (int y = 0; y < kImageHeight; y += 10) {
|
|
|
|
for (int x = 0; x < kImageWidth; x += 10) {
|
2013-08-21 07:01:29 +00:00
|
|
|
SkRect r = SkRect::MakeXYWH(SkIntToScalar(x),
|
|
|
|
SkIntToScalar(y),
|
2013-08-20 12:11:31 +00:00
|
|
|
10, 10);
|
|
|
|
SkPaint p;
|
|
|
|
p.setColor(rand.nextU() | 0xFF000000);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 11:35:38 +00:00
|
|
|
SkScalar xOff = 2, yOff = 2;
|
2013-08-20 12:11:31 +00:00
|
|
|
for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) {
|
|
|
|
for (int innerShape = 0; innerShape < kShapeCount; ++innerShape) {
|
|
|
|
for (size_t innerRect = 0; innerRect < SK_ARRAY_COUNT(innerRects); ++innerRect) {
|
|
|
|
SkPath path;
|
|
|
|
|
2019-11-22 18:34:02 +00:00
|
|
|
AddShape(&path, outerRect, (Shapes) outerShape, SkPathDirection::kCW);
|
2013-08-21 07:01:29 +00:00
|
|
|
AddShape(&path, innerRects[innerRect], (Shapes) innerShape,
|
2019-11-22 18:34:02 +00:00
|
|
|
SkPathDirection::kCCW);
|
2013-08-20 12:11:31 +00:00
|
|
|
|
2015-05-19 11:35:38 +00:00
|
|
|
canvas->save();
|
|
|
|
if (fFlipped) {
|
|
|
|
canvas->scale(1.0f, -1.0f);
|
|
|
|
canvas->translate(xOff, -yOff - 40.0f);
|
|
|
|
} else {
|
|
|
|
canvas->translate(xOff, yOff);
|
|
|
|
}
|
|
|
|
|
2013-08-20 12:11:31 +00:00
|
|
|
canvas->drawPath(path, shapePaint);
|
2015-05-19 11:35:38 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
xOff += 45;
|
2013-08-20 12:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 11:35:38 +00:00
|
|
|
|
|
|
|
xOff = 2;
|
|
|
|
yOff += 45;
|
2013-08-20 12:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kImageWidth = 269;
|
|
|
|
static constexpr int kImageHeight = 134;
|
2013-08-20 12:11:31 +00:00
|
|
|
|
|
|
|
bool fDoAA;
|
2015-05-19 11:35:38 +00:00
|
|
|
bool fFlipped;
|
2013-08-20 12:11:31 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-05-19 11:35:38 +00:00
|
|
|
DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ false); )
|
|
|
|
DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ false); )
|
|
|
|
DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ true); )
|
|
|
|
DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ true); )
|
2013-08-20 12:11:31 +00:00
|
|
|
|
|
|
|
}
|