| 1 |
/* |
| 2 |
* Copyright 2011 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBlurTypes.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/core/SkPoint.h" |
| 14 |
#include "include/core/SkScalar.h" |
| 15 |
#include "include/core/SkSize.h" |
| 16 |
#include "include/core/SkString.h" |
| 17 |
#include "src/base/SkRandom.h" |
| 18 |
|
| 19 |
#include <stddef.h> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
namespace skiagm { |
| 23 |
|
| 24 |
class PointsGM : public GM { |
| 25 |
public: |
| 26 |
PointsGM() {} |
| 27 |
|
| 28 |
protected: |
| 29 |
SkString getName() const override { return SkString("points"); } |
| 30 |
|
| 31 |
SkISize getISize() override { return SkISize::Make(640, 490); } |
| 32 |
|
| 33 |
static void fill_pts(SkSpan<SkPoint> pts, SkRandom* rand) { |
| 34 |
for (auto& p : pts) { |
| 35 |
// Compute these independently and store in variables, rather |
| 36 |
// than in the parameter-passing expression, to get consistent |
| 37 |
// evaluation order across compilers. |
| 38 |
SkScalar y = rand->nextUScalar1() * 480; |
| 39 |
SkScalar x = rand->nextUScalar1() * 640; |
| 40 |
p.set(x, y); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
void onDraw(SkCanvas* canvas) override { |
| 45 |
canvas->translate(SK_Scalar1, SK_Scalar1); |
| 46 |
|
| 47 |
SkRandom rand; |
| 48 |
SkPaint p0, p1, p2, p3; |
| 49 |
const size_t n = 99; |
| 50 |
|
| 51 |
p0.setColor(SK_ColorRED); |
| 52 |
p1.setColor(SK_ColorGREEN); |
| 53 |
p2.setColor(SK_ColorBLUE); |
| 54 |
p3.setColor(SK_ColorWHITE); |
| 55 |
|
| 56 |
p0.setStrokeWidth(SkIntToScalar(4)); |
| 57 |
p2.setStrokeCap(SkPaint::kRound_Cap); |
| 58 |
p2.setStrokeWidth(SkIntToScalar(6)); |
| 59 |
|
| 60 |
std::vector<SkPoint> pts(n); |
| 61 |
fill_pts(pts, &rand); |
| 62 |
|
| 63 |
canvas->drawPoints(SkCanvas::kPolygon_PointMode, pts, p0); |
| 64 |
canvas->drawPoints(SkCanvas::kLines_PointMode, pts, p1); |
| 65 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, pts, p2); |
| 66 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, pts, p3); |
| 67 |
} |
| 68 |
|
| 69 |
private: |
| 70 |
using INHERITED = GM; |
| 71 |
}; |
| 72 |
DEF_GM( return new PointsGM; ) |
| 73 |
} // namespace skiagm |
| 74 |
|
| 75 |
#include "include/core/SkMaskFilter.h" |
| 76 |
|
| 77 |
DEF_SIMPLE_GM(points_maskfilter, canvas, 512, 256) { |
| 78 |
constexpr int N = 30; |
| 79 |
SkPoint pts[N]; |
| 80 |
|
| 81 |
SkRandom rand; |
| 82 |
for (SkPoint& p : pts) { |
| 83 |
p.fX = rand.nextF() * 220 + 18; |
| 84 |
p.fY = rand.nextF() * 220 + 18; |
| 85 |
} |
| 86 |
|
| 87 |
auto mf = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 6); |
| 88 |
const SkPaint::Cap caps[] = { SkPaint::kSquare_Cap, SkPaint::kRound_Cap }; |
| 89 |
|
| 90 |
SkPaint paint; |
| 91 |
paint.setAntiAlias(true); |
| 92 |
paint.setStroke(true); |
| 93 |
paint.setStrokeWidth(10); |
| 94 |
|
| 95 |
for (auto cap : caps) { |
| 96 |
paint.setStrokeCap(cap); |
| 97 |
|
| 98 |
paint.setMaskFilter(mf); |
| 99 |
paint.setColor(SK_ColorBLACK); |
| 100 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, pts, paint); |
| 101 |
|
| 102 |
paint.setMaskFilter(nullptr); |
| 103 |
paint.setColor(SK_ColorRED); |
| 104 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, pts, paint); |
| 105 |
|
| 106 |
canvas->translate(256, 0); |
| 107 |
} |
| 108 |
} |