2011-10-10 15:42:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2016-11-09 20:00:49 +00:00
|
|
|
#include "SkAAClip.h"
|
2011-10-10 15:42:36 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkPaint.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2011-10-10 15:42:36 +00:00
|
|
|
#include "SkRandom.h"
|
2016-12-12 15:02:12 +00:00
|
|
|
#include "SkClipOpPriv.h"
|
2011-10-10 15:42:36 +00:00
|
|
|
|
2016-11-09 20:00:49 +00:00
|
|
|
constexpr int W = 150;
|
|
|
|
constexpr int H = 200;
|
2011-10-10 15:42:36 +00:00
|
|
|
|
2011-10-24 12:19:46 +00:00
|
|
|
static void show_text(SkCanvas* canvas, bool doAA) {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2011-10-10 15:42:36 +00:00
|
|
|
SkPaint paint;
|
2011-10-24 12:19:46 +00:00
|
|
|
paint.setAntiAlias(doAA);
|
|
|
|
paint.setLCDRenderText(true);
|
2011-10-10 15:42:36 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(20));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-24 12:19:46 +00:00
|
|
|
for (int i = 0; i < 200; ++i) {
|
2011-10-10 15:42:36 +00:00
|
|
|
paint.setColor((SK_A32_MASK << SK_A32_SHIFT) | rand.nextU());
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString("Hamburgefons",
|
2011-10-24 12:19:46 +00:00
|
|
|
rand.nextSScalar1() * W, rand.nextSScalar1() * H + 20,
|
2011-10-10 15:42:36 +00:00
|
|
|
paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-24 12:19:46 +00:00
|
|
|
static void show_fill(SkCanvas* canvas, bool doAA) {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2011-10-10 15:42:36 +00:00
|
|
|
SkPaint paint;
|
2011-10-24 12:19:46 +00:00
|
|
|
paint.setAntiAlias(doAA);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-24 12:19:46 +00:00
|
|
|
for (int i = 0; i < 50; ++i) {
|
2011-10-10 15:42:36 +00:00
|
|
|
SkRect r;
|
|
|
|
SkPath p;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-10 15:42:36 +00:00
|
|
|
r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
|
|
|
|
rand.nextUScalar1() * W, rand.nextUScalar1() * H);
|
|
|
|
paint.setColor(rand.nextU());
|
|
|
|
canvas->drawRect(r, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-10 15:42:36 +00:00
|
|
|
r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
|
|
|
|
rand.nextUScalar1() * W, rand.nextUScalar1() * H);
|
2011-10-24 12:19:46 +00:00
|
|
|
paint.setColor(rand.nextU());
|
|
|
|
p.addOval(r);
|
|
|
|
canvas->drawPath(p, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
static SkScalar randRange(SkRandom& rand, SkScalar min, SkScalar max) {
|
2011-10-24 12:19:46 +00:00
|
|
|
SkASSERT(min <= max);
|
2017-02-14 15:59:19 +00:00
|
|
|
return min + rand.nextUScalar1() * (max - min);
|
2011-10-24 12:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_stroke(SkCanvas* canvas, bool doAA, SkScalar strokeWidth, int n) {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2011-10-24 12:19:46 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(doAA);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(strokeWidth);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-24 12:19:46 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
SkRect r;
|
|
|
|
SkPath p;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-24 12:19:46 +00:00
|
|
|
r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
|
|
|
|
rand.nextUScalar1() * W, rand.nextUScalar1() * H);
|
2011-10-10 15:42:36 +00:00
|
|
|
paint.setColor(rand.nextU());
|
|
|
|
canvas->drawRect(r, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-10 15:42:36 +00:00
|
|
|
r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
|
|
|
|
rand.nextUScalar1() * W, rand.nextUScalar1() * H);
|
|
|
|
paint.setColor(rand.nextU());
|
|
|
|
p.addOval(r);
|
|
|
|
canvas->drawPath(p, paint);
|
2011-10-24 12:19:46 +00:00
|
|
|
|
|
|
|
const SkScalar minx = -SkIntToScalar(W)/4;
|
|
|
|
const SkScalar maxx = 5*SkIntToScalar(W)/4;
|
|
|
|
const SkScalar miny = -SkIntToScalar(H)/4;
|
|
|
|
const SkScalar maxy = 5*SkIntToScalar(H)/4;
|
|
|
|
paint.setColor(rand.nextU());
|
|
|
|
canvas->drawLine(randRange(rand, minx, maxx), randRange(rand, miny, maxy),
|
|
|
|
randRange(rand, minx, maxx), randRange(rand, miny, maxy),
|
|
|
|
paint);
|
2011-10-10 15:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-24 12:19:46 +00:00
|
|
|
static void show_hair(SkCanvas* canvas, bool doAA) {
|
|
|
|
show_stroke(canvas, doAA, 0, 150);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_thick(SkCanvas* canvas, bool doAA) {
|
|
|
|
show_stroke(canvas, doAA, SkIntToScalar(5), 50);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*CanvasProc)(SkCanvas*, bool);
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class ClipView : public Sample {
|
2011-10-10 15:42:36 +00:00
|
|
|
public:
|
|
|
|
ClipView() {
|
2011-10-24 12:19:46 +00:00
|
|
|
SkAAClip clip;
|
|
|
|
SkIRect r = { -2, -3, 842, 18 };
|
|
|
|
clip.setRect(r);
|
2011-10-10 15:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ClipView() {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
virtual bool onQuery(Sample::Event* evt) {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "Clip");
|
2011-10-10 15:42:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2011-10-24 12:19:46 +00:00
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
2011-10-10 15:42:36 +00:00
|
|
|
canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
|
|
|
|
|
|
|
|
static const CanvasProc gProc[] = {
|
2011-10-24 12:19:46 +00:00
|
|
|
show_text, show_thick, show_hair, show_fill
|
2011-10-10 15:42:36 +00:00
|
|
|
};
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-10 15:42:36 +00:00
|
|
|
SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) };
|
|
|
|
SkPath clipPath;
|
|
|
|
r.inset(SK_Scalar1 / 4, SK_Scalar1 / 4);
|
2011-10-24 12:19:46 +00:00
|
|
|
clipPath.addRoundRect(r, SkIntToScalar(20), SkIntToScalar(20));
|
|
|
|
|
|
|
|
// clipPath.toggleInverseFillType();
|
2011-10-10 15:42:36 +00:00
|
|
|
|
|
|
|
for (int aa = 0; aa <= 1; ++aa) {
|
|
|
|
canvas->save();
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); ++i) {
|
|
|
|
canvas->save();
|
2016-12-09 14:00:50 +00:00
|
|
|
canvas->clipPath(clipPath, kIntersect_SkClipOp, SkToBool(aa));
|
2011-10-24 12:19:46 +00:00
|
|
|
// canvas->drawColor(SK_ColorWHITE);
|
|
|
|
gProc[i](canvas, SkToBool(aa));
|
2011-10-10 15:42:36 +00:00
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(W * SK_Scalar1 * 8 / 7, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, H * SK_Scalar1 * 8 / 7);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-10 15:42:36 +00:00
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2011-10-10 15:42:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new ClipView(); )
|