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.
|
|
|
|
*/
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRandom.h"
|
2017-12-01 15:52:28 +00:00
|
|
|
#include "SkString.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkTime.h"
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class PolyToPolyView : public Sample {
|
2011-02-17 14:21:01 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
PolyToPolyView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
// tests
|
|
|
|
{
|
2011-05-19 19:58:58 +00:00
|
|
|
SkPoint src[] = { { 0, 0 },
|
|
|
|
{ SK_Scalar1, 0 },
|
|
|
|
{ 0, SK_Scalar1 } };
|
|
|
|
SkPoint dst[] = { { 0, 0 },
|
|
|
|
{ 2*SK_Scalar1, 0 },
|
|
|
|
{ 0, 2*SK_Scalar1 } };
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMatrix m1, m2;
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2013-02-07 18:58:35 +00:00
|
|
|
(void) m1.setPolyToPoly(src, dst, 3);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
m2.reset();
|
2008-12-17 15:59:43 +00:00
|
|
|
m2.set(SkMatrix::kMScaleX, dst[1].fX - dst[0].fX);
|
|
|
|
m2.set(SkMatrix::kMSkewX, dst[2].fX - dst[0].fX);
|
|
|
|
m2.set(SkMatrix::kMTransX, dst[0].fX);
|
|
|
|
m2.set(SkMatrix::kMSkewY, dst[1].fY - dst[0].fY);
|
|
|
|
m2.set(SkMatrix::kMScaleY, dst[2].fY - dst[0].fY);
|
|
|
|
m2.set(SkMatrix::kMTransY, dst[0].fY);
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
m1.reset();
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
const SkScalar src1[] = {
|
2013-11-25 19:44:07 +00:00
|
|
|
0, 0, 0, 427, 316, 427, 316, 0
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
const SkScalar dst1[] = {
|
2013-11-25 19:44:07 +00:00
|
|
|
158, 177.5f, 158, 249.5f,
|
|
|
|
158, 604.5f, 158, -177.5f
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2013-02-07 18:58:35 +00:00
|
|
|
(void) m2.setPolyToPoly((const SkPoint*)src1, (SkPoint*)dst1, 4);
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
{
|
|
|
|
const SkPoint src[] = {
|
2011-05-19 19:58:58 +00:00
|
|
|
{ SkIntToScalar(1), SkIntToScalar(0) },
|
|
|
|
{ SkIntToScalar(4), SkIntToScalar(7) },
|
|
|
|
{ SkIntToScalar(10), SkIntToScalar(2) }
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
const SkPoint dst[] = {
|
2011-05-19 19:58:58 +00:00
|
|
|
{ SkIntToScalar(4), SkIntToScalar(2) },
|
|
|
|
{ SkIntToScalar(45), SkIntToScalar(26) },
|
|
|
|
{ SkIntToScalar(32), SkIntToScalar(17) }
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2011-10-24 21:09:40 +00:00
|
|
|
SkMatrix m0;
|
2008-12-17 15:59:43 +00:00
|
|
|
m0.setPolyToPoly(src, dst, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
virtual bool onQuery(Sample::Event* evt) {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "PolyToPolyView");
|
2008-12-17 15:59:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[],
|
|
|
|
const int idst[], int count) {
|
|
|
|
SkMatrix matrix;
|
|
|
|
SkPoint src[4], dst[4];
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
|
|
|
|
dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
|
|
|
|
}
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
|
|
|
matrix.setPolyToPoly(src, dst, count);
|
|
|
|
canvas->concat(matrix);
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint->setColor(SK_ColorGRAY);
|
|
|
|
paint->setStyle(SkPaint::kStroke_Style);
|
|
|
|
const SkScalar D = SkIntToScalar(64);
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(D, D), *paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawLine(0, 0, D, D, *paint);
|
|
|
|
canvas->drawLine(0, D, D, 0, *paint);
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint::FontMetrics fm;
|
|
|
|
paint->getFontMetrics(&fm);
|
|
|
|
paint->setColor(SK_ColorRED);
|
|
|
|
paint->setStyle(SkPaint::kFill_Style);
|
|
|
|
SkScalar x = D/2;
|
|
|
|
float y = D/2 - (fm.fAscent + fm.fDescent)/2;
|
|
|
|
SkString str;
|
|
|
|
str.appendS32(count);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(str,
|
2013-11-25 19:44:07 +00:00
|
|
|
x, y,
|
2012-05-08 13:15:37 +00:00
|
|
|
*paint);
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2011-05-11 05:58:58 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(4));
|
|
|
|
paint.setTextSize(SkIntToScalar(40));
|
|
|
|
paint.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
|
|
|
|
// translate (1 point)
|
|
|
|
const int src1[] = { 0, 0 };
|
|
|
|
const int dst1[] = { 5, 5 };
|
|
|
|
doDraw(canvas, &paint, src1, dst1, 1);
|
|
|
|
canvas->restore();
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(160), SkIntToScalar(10));
|
|
|
|
// rotate/uniform-scale (2 points)
|
|
|
|
const int src2[] = { 32, 32, 64, 32 };
|
|
|
|
const int dst2[] = { 32, 32, 64, 48 };
|
|
|
|
doDraw(canvas, &paint, src2, dst2, 2);
|
|
|
|
canvas->restore();
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(110));
|
|
|
|
// rotate/skew (3 points)
|
|
|
|
const int src3[] = { 0, 0, 64, 0, 0, 64 };
|
|
|
|
const int dst3[] = { 0, 0, 96, 0, 24, 64 };
|
|
|
|
doDraw(canvas, &paint, src3, dst3, 3);
|
|
|
|
canvas->restore();
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(160), SkIntToScalar(110));
|
|
|
|
// perspective (4 points)
|
|
|
|
const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 };
|
|
|
|
const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 };
|
|
|
|
doDraw(canvas, &paint, src4, dst4, 4);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new PolyToPolyView(); )
|