2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkCullPoints.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
static void addbump(SkPath* path, const SkPoint pts[2], SkScalar bump) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkVector tang;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
tang.setLength(pts[1].fX - pts[0].fX, pts[1].fY - pts[0].fY, bump);
|
|
|
|
|
|
|
|
path->lineTo(SkScalarHalf(pts[0].fX + pts[1].fX) - tang.fY,
|
|
|
|
SkScalarHalf(pts[0].fY + pts[1].fY) + tang.fX);
|
|
|
|
path->lineTo(pts[1]);
|
|
|
|
}
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
static void subdivide(SkPath* path, SkScalar bump) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPath::Iter iter(*path, false);
|
|
|
|
SkPoint pts[4];
|
|
|
|
SkPath tmp;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
for (;;)
|
|
|
|
switch (iter.next(pts)) {
|
|
|
|
case SkPath::kMove_Verb:
|
|
|
|
tmp.moveTo(pts[0]);
|
|
|
|
break;
|
|
|
|
case SkPath::kLine_Verb:
|
|
|
|
addbump(&tmp, pts, bump);
|
|
|
|
bump = -bump;
|
|
|
|
break;
|
|
|
|
case SkPath::kDone_Verb:
|
|
|
|
goto FINISH;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FINISH:
|
|
|
|
path->swap(tmp);
|
|
|
|
}
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
static SkIPoint* getpts(const SkPath& path, int* count) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPoint pts[4];
|
|
|
|
int n = 1;
|
|
|
|
SkIPoint* array;
|
|
|
|
|
|
|
|
{
|
|
|
|
SkPath::Iter iter(path, false);
|
|
|
|
for (;;)
|
|
|
|
switch (iter.next(pts)) {
|
|
|
|
case SkPath::kLine_Verb:
|
|
|
|
n += 1;
|
|
|
|
break;
|
|
|
|
case SkPath::kDone_Verb:
|
|
|
|
goto FINISHED;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FINISHED:
|
|
|
|
array = new SkIPoint[n];
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
SkPath::Iter iter(path, false);
|
|
|
|
for (;;)
|
|
|
|
switch (iter.next(pts)) {
|
|
|
|
case SkPath::kMove_Verb:
|
|
|
|
array[n++].set(SkScalarRound(pts[0].fX), SkScalarRound(pts[0].fY));
|
|
|
|
break;
|
|
|
|
case SkPath::kLine_Verb:
|
|
|
|
array[n++].set(SkScalarRound(pts[1].fX), SkScalarRound(pts[1].fY));
|
|
|
|
break;
|
|
|
|
case SkPath::kDone_Verb:
|
|
|
|
goto FINISHED2;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
FINISHED2:
|
|
|
|
*count = n;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
static SkScalar nextScalarRange(SkRandom& rand, SkScalar min, SkScalar max) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return min + SkScalarMul(rand.nextUScalar1(), max - min);
|
|
|
|
}
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
class CullView : public SampleView {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
CullView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
fClip.set(0, 0, SkIntToScalar(160), SkIntToScalar(160));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkRandom rand;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
for (int i = 0; i < 50; i++) {
|
2011-04-14 17:53:24 +00:00
|
|
|
SkScalar x = nextScalarRange(rand, -fClip.width()*1, fClip.width()*2);
|
|
|
|
SkScalar y = nextScalarRange(rand, -fClip.height()*1, fClip.height()*2);
|
2008-12-17 15:59:43 +00:00
|
|
|
if (i == 0)
|
|
|
|
fPath.moveTo(x, y);
|
|
|
|
else
|
|
|
|
fPath.lineTo(x, y);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar bump = fClip.width()/8;
|
|
|
|
subdivide(&fPath, bump);
|
|
|
|
subdivide(&fPath, bump);
|
|
|
|
subdivide(&fPath, bump);
|
|
|
|
fPoints = getpts(fPath, &fPtCount);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
virtual ~CullView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
delete[] fPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2011-05-05 14:03:48 +00:00
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SampleCode::TitleR(evt, "Culling");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkAutoCanvasRestore ar(canvas, true);
|
|
|
|
|
|
|
|
canvas->translate( SkScalarHalf(this->width() - fClip.width()),
|
|
|
|
SkScalarHalf(this->height() - fClip.height()));
|
|
|
|
|
|
|
|
// canvas->scale(SK_Scalar1*3, SK_Scalar1*3, 0, 0);
|
|
|
|
|
|
|
|
SkPaint paint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// paint.setAntiAliasOn(true);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
canvas->drawRect(fClip, paint);
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
paint.setColor(0xFF555555);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(2));
|
|
|
|
// paint.setPathEffect(new SkCornerPathEffect(SkIntToScalar(30)))->unref();
|
|
|
|
canvas->drawPath(fPath, paint);
|
|
|
|
// paint.setPathEffect(NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SkPath tmp;
|
|
|
|
SkIRect iclip;
|
|
|
|
fClip.round(&iclip);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkCullPointsPath cpp(iclip, &tmp);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
cpp.moveTo(fPoints[0].fX, fPoints[0].fY);
|
|
|
|
for (int i = 0; i < fPtCount; i++)
|
|
|
|
cpp.lineTo(fPoints[i].fX, fPoints[i].fY);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(3));
|
|
|
|
paint.setStrokeJoin(SkPaint::kRound_Join);
|
|
|
|
canvas->drawPath(tmp, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
this->inval(NULL);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkRect fClip;
|
|
|
|
SkIPoint* fPoints;
|
|
|
|
SkPath fPath;
|
|
|
|
int fPtCount;
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new CullView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|