2013-12-30 04:07:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
2013-12-30 04:20:38 +00:00
|
|
|
#include "SkRandom.h"
|
2013-12-30 04:07:34 +00:00
|
|
|
|
2013-12-30 15:51:25 +00:00
|
|
|
static SkShader* make_shader(SkScalar w, SkScalar h) {
|
|
|
|
const SkColor colors[] = {
|
|
|
|
SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE,
|
|
|
|
SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW,
|
|
|
|
};
|
|
|
|
const SkPoint pts[] = { { w/4, 0 }, { 3*w/4, h } };
|
|
|
|
|
|
|
|
return SkGradientShader::CreateLinear(pts, colors, NULL,
|
2013-12-30 04:07:34 +00:00
|
|
|
SK_ARRAY_COUNT(colors),
|
|
|
|
SkShader::kMirror_TileMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
class VerticesGM : public skiagm::GM {
|
|
|
|
SkPoint fPts[9];
|
|
|
|
SkPoint fTexs[9];
|
|
|
|
SkColor fColors[9];
|
|
|
|
SkShader* fShader;
|
2014-05-05 20:41:21 +00:00
|
|
|
unsigned fAlpha;
|
2013-12-30 04:07:34 +00:00
|
|
|
|
|
|
|
public:
|
2014-05-05 20:41:21 +00:00
|
|
|
VerticesGM(unsigned alpha) : fShader(NULL), fAlpha(alpha) {
|
2013-12-30 04:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~VerticesGM() {
|
|
|
|
SkSafeUnref(fShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2013-12-30 15:51:25 +00:00
|
|
|
const SkScalar X = 150;
|
|
|
|
const SkScalar Y = 150;
|
|
|
|
|
|
|
|
fPts[0].set(0, 0); fPts[1].set(X/2, 10); fPts[2].set(X, 0);
|
|
|
|
fPts[3].set(10, Y/2); fPts[4].set(X/2, Y/2); fPts[5].set(X-10, Y/2);
|
|
|
|
fPts[6].set(0, Y); fPts[7].set(X/2, Y-10); fPts[8].set(X, Y);
|
2013-12-30 04:07:34 +00:00
|
|
|
|
2013-12-30 15:51:25 +00:00
|
|
|
const SkScalar w = 200;
|
|
|
|
const SkScalar h = 200;
|
2013-12-30 04:07:34 +00:00
|
|
|
|
|
|
|
fTexs[0].set(0, 0); fTexs[1].set(w/2, 0); fTexs[2].set(w, 0);
|
|
|
|
fTexs[3].set(0, h/2); fTexs[4].set(w/2, h/2); fTexs[5].set(w, h/2);
|
|
|
|
fTexs[6].set(0, h); fTexs[7].set(w/2, h); fTexs[8].set(w, h);
|
|
|
|
|
|
|
|
fShader = make_shader(w, h);
|
|
|
|
|
|
|
|
SkRandom rand;
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fColors); ++i) {
|
2013-12-30 15:51:25 +00:00
|
|
|
fColors[i] = rand.nextU() | 0xFF000000;
|
2013-12-30 04:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-05-05 20:41:21 +00:00
|
|
|
SkString name("vertices");
|
|
|
|
if (0xFF != fAlpha) {
|
|
|
|
name.appendf("_%02X", fAlpha);
|
|
|
|
}
|
|
|
|
return name;
|
2013-12-30 04:07:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-12-30 15:51:25 +00:00
|
|
|
return SkISize::Make(600, 600);
|
2013-12-30 04:07:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-12-30 15:51:25 +00:00
|
|
|
// start with the center of a 3x3 grid
|
2013-12-30 04:07:34 +00:00
|
|
|
static const uint16_t fan[] = {
|
|
|
|
4,
|
|
|
|
0, 1, 2, 5, 8, 7, 6, 3, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct {
|
|
|
|
const SkColor* fColors;
|
|
|
|
const SkPoint* fTexs;
|
|
|
|
} rec[] = {
|
|
|
|
{ fColors, NULL },
|
|
|
|
{ NULL, fTexs },
|
|
|
|
{ fColors, fTexs },
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkXfermode::Mode modes[] = {
|
|
|
|
SkXfermode::kSrc_Mode,
|
|
|
|
SkXfermode::kDst_Mode,
|
|
|
|
SkXfermode::kModulate_Mode,
|
|
|
|
};
|
2013-12-30 07:01:37 +00:00
|
|
|
|
2013-12-30 04:07:34 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(fShader);
|
2014-05-05 20:41:21 +00:00
|
|
|
paint.setAlpha(fAlpha);
|
2013-12-30 04:07:34 +00:00
|
|
|
|
|
|
|
canvas->translate(20, 20);
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(modes); ++j) {
|
|
|
|
SkXfermode* xfer = SkXfermode::Create(modes[j]);
|
|
|
|
canvas->save();
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
|
|
|
|
canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode,
|
|
|
|
SK_ARRAY_COUNT(fPts), fPts,
|
|
|
|
rec[i].fTexs, rec[i].fColors,
|
|
|
|
xfer, fan, SK_ARRAY_COUNT(fan), paint);
|
2013-12-30 15:51:25 +00:00
|
|
|
canvas->translate(200, 0);
|
2013-12-30 04:07:34 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2013-12-30 15:51:25 +00:00
|
|
|
canvas->translate(0, 200);
|
2013-12-30 04:07:34 +00:00
|
|
|
xfer->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-05-05 20:41:21 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM( return SkNEW_ARGS(VerticesGM, (0xFF)); )
|
|
|
|
DEF_GM( return SkNEW_ARGS(VerticesGM, (0x80)); )
|