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.
|
|
|
|
*/
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkGraphics.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkVertices.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "src/core/SkOSFile.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
static sk_sp<SkShader> make_shader0(SkIPoint* size) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkBitmap bm;
|
2011-05-02 12:53:34 +00:00
|
|
|
size->set(2, 2);
|
|
|
|
SkPMColor color0 = SkPreMultiplyARGB(0x80, 0x80, 0xff, 0x80);
|
|
|
|
SkPMColor color1 = SkPreMultiplyARGB(0x40, 0xff, 0x00, 0xff);
|
2014-02-17 02:55:57 +00:00
|
|
|
bm.allocN32Pixels(size->fX, size->fY);
|
2011-05-02 12:53:34 +00:00
|
|
|
bm.eraseColor(color0);
|
|
|
|
uint32_t* pixels = (uint32_t*) bm.getPixels();
|
|
|
|
pixels[0] = pixels[2] = color0;
|
|
|
|
pixels[1] = pixels[3] = color1;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2019-04-08 17:20:23 +00:00
|
|
|
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
|
2011-05-19 19:58:58 +00:00
|
|
|
SkPoint pts[] = { { 0, 0 },
|
|
|
|
{ SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
|
2008-12-17 15:59:43 +00:00
|
|
|
SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
|
2016-03-09 02:50:00 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr,
|
2019-04-03 14:27:45 +00:00
|
|
|
SK_ARRAY_COUNT(colors), SkTileMode::kMirror);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class VerticesView : public Sample {
|
2016-03-09 02:50:00 +00:00
|
|
|
sk_sp<SkShader> fShader0;
|
|
|
|
sk_sp<SkShader> fShader1;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-02-07 15:30:46 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
VerticesView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkIPoint size;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fShader0 = make_shader0(&size);
|
|
|
|
fShader1 = make_shader1(size);
|
|
|
|
|
|
|
|
make_strip(&fRecs[0], size.fX, size.fY);
|
|
|
|
make_fan(&fRecs[1], size.fX, size.fY);
|
|
|
|
make_tris(&fRecs[2]);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fScale = SK_Scalar1;
|
2011-04-22 14:10:48 +00:00
|
|
|
|
|
|
|
this->setBGColor(SK_ColorGRAY);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "Vertices");
|
2008-12-17 15:59:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar fScale;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setDither(true);
|
2015-03-16 17:08:34 +00:00
|
|
|
paint.setFilterQuality(kLow_SkFilterQuality);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
|
2017-04-03 15:11:09 +00:00
|
|
|
auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
|
|
|
|
fRecs[i].fVerts, fRecs[i].fTexs,
|
|
|
|
nullptr);
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(nullptr);
|
2017-04-03 15:11:09 +00:00
|
|
|
canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(SkIntToScalar(250), 0);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setShader(fShader0);
|
2017-04-03 15:11:09 +00:00
|
|
|
canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(250), 0);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setShader(fShader1);
|
2017-04-03 15:11:09 +00:00
|
|
|
canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(250));
|
|
|
|
}
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
return new Click(this);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onClick(Click* click) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
// fCurrX = click->fICurr.fX;
|
|
|
|
// fCurrY = click->fICurr.fY;
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
struct Rec {
|
2017-04-03 15:11:09 +00:00
|
|
|
SkVertices::VertexMode fMode;
|
2008-12-17 15:59:43 +00:00
|
|
|
int fCount;
|
|
|
|
SkPoint* fVerts;
|
|
|
|
SkPoint* fTexs;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
|
2008-12-17 15:59:43 +00:00
|
|
|
~Rec() { delete[] fVerts; delete[] fTexs; }
|
|
|
|
};
|
|
|
|
|
|
|
|
void make_tris(Rec* rec) {
|
|
|
|
int n = 10;
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2017-04-03 15:11:09 +00:00
|
|
|
rec->fMode = SkVertices::kTriangles_VertexMode;
|
2008-12-17 15:59:43 +00:00
|
|
|
rec->fCount = n * 3;
|
|
|
|
rec->fVerts = new SkPoint[rec->fCount];
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
SkPoint* v = &rec->fVerts[i*3];
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void make_fan(Rec* rec, int texWidth, int texHeight) {
|
|
|
|
const SkScalar tx = SkIntToScalar(texWidth);
|
|
|
|
const SkScalar ty = SkIntToScalar(texHeight);
|
|
|
|
const int n = 24;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2017-04-03 15:11:09 +00:00
|
|
|
rec->fMode = SkVertices::kTriangleFan_VertexMode;
|
2008-12-17 15:59:43 +00:00
|
|
|
rec->fCount = n + 2;
|
|
|
|
rec->fVerts = new SkPoint[rec->fCount];
|
|
|
|
rec->fTexs = new SkPoint[rec->fCount];
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPoint* v = rec->fVerts;
|
|
|
|
SkPoint* t = rec->fTexs;
|
|
|
|
|
|
|
|
v[0].set(0, 0);
|
|
|
|
t[0].set(0, 0);
|
|
|
|
for (int i = 0; i < n; i++) {
|
2019-04-02 14:59:28 +00:00
|
|
|
SkScalar r = SK_ScalarPI * 2 * i / n,
|
|
|
|
sin = SkScalarSin(r),
|
|
|
|
cos = SkScalarCos(r);
|
2008-12-17 15:59:43 +00:00
|
|
|
v[i+1].set(cos, sin);
|
|
|
|
t[i+1].set(i*tx/n, ty);
|
|
|
|
}
|
|
|
|
v[n+1] = v[1];
|
|
|
|
t[n+1].set(tx, ty);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMatrix m;
|
|
|
|
m.setScale(SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
|
|
|
|
m.mapPoints(v, rec->fCount);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void make_strip(Rec* rec, int texWidth, int texHeight) {
|
|
|
|
const SkScalar tx = SkIntToScalar(texWidth);
|
|
|
|
const SkScalar ty = SkIntToScalar(texHeight);
|
|
|
|
const int n = 24;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2017-04-03 15:11:09 +00:00
|
|
|
rec->fMode = SkVertices::kTriangleStrip_VertexMode;
|
2008-12-17 15:59:43 +00:00
|
|
|
rec->fCount = 2 * (n + 1);
|
|
|
|
rec->fVerts = new SkPoint[rec->fCount];
|
|
|
|
rec->fTexs = new SkPoint[rec->fCount];
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPoint* v = rec->fVerts;
|
|
|
|
SkPoint* t = rec->fTexs;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
for (int i = 0; i < n; i++) {
|
2019-04-02 14:59:28 +00:00
|
|
|
SkScalar r = SK_ScalarPI * 2 * i / n,
|
|
|
|
sin = SkScalarSin(r),
|
|
|
|
cos = SkScalarCos(r);
|
2008-12-17 15:59:43 +00:00
|
|
|
v[i*2 + 0].set(cos/2, sin/2);
|
|
|
|
v[i*2 + 1].set(cos, sin);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
t[i*2 + 0].set(tx * i / n, ty);
|
|
|
|
t[i*2 + 1].set(tx * i / n, 0);
|
|
|
|
}
|
|
|
|
v[2*n + 0] = v[0];
|
|
|
|
v[2*n + 1] = v[1];
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
t[2*n + 0].set(tx, ty);
|
|
|
|
t[2*n + 1].set(tx, 0);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMatrix m;
|
|
|
|
m.setScale(SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
|
|
|
|
m.mapPoints(v, rec->fCount);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
Rec fRecs[3];
|
|
|
|
|
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 VerticesView(); )
|