2011-09-08 13:36:30 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
|
|
|
|
static const struct {
|
2016-10-05 19:59:51 +00:00
|
|
|
SkXfermode::Mode fMode;
|
|
|
|
const char* fLabel;
|
2011-09-08 13:36:30 +00:00
|
|
|
} gModes[] = {
|
2016-10-05 19:59:51 +00:00
|
|
|
{ SkXfermode::kClear_Mode, "Clear" },
|
|
|
|
{ SkXfermode::kSrc_Mode, "Src" },
|
|
|
|
{ SkXfermode::kDst_Mode, "Dst" },
|
|
|
|
{ SkXfermode::kSrcOver_Mode, "SrcOver" },
|
|
|
|
{ SkXfermode::kDstOver_Mode, "DstOver" },
|
|
|
|
{ SkXfermode::kSrcIn_Mode, "SrcIn" },
|
|
|
|
{ SkXfermode::kDstIn_Mode, "DstIn" },
|
|
|
|
{ SkXfermode::kSrcOut_Mode, "SrcOut" },
|
|
|
|
{ SkXfermode::kDstOut_Mode, "DstOut" },
|
|
|
|
{ SkXfermode::kSrcATop_Mode, "SrcATop" },
|
|
|
|
{ SkXfermode::kDstATop_Mode, "DstATop" },
|
|
|
|
{ SkXfermode::kXor_Mode, "Xor" },
|
2011-09-08 13:36:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const int gWidth = 64;
|
|
|
|
const int gHeight = 64;
|
|
|
|
const SkScalar W = SkIntToScalar(gWidth);
|
|
|
|
const SkScalar H = SkIntToScalar(gHeight);
|
|
|
|
|
2016-10-05 19:59:51 +00:00
|
|
|
static SkScalar drawCell(SkCanvas* canvas, sk_sp<SkXfermode> mode, SkAlpha a0, SkAlpha a1) {
|
2011-09-08 13:36:30 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(W, H);
|
|
|
|
r.inset(W/10, H/10);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setAlpha(a0);
|
|
|
|
canvas->drawOval(r, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setAlpha(a1);
|
2016-10-05 19:59:51 +00:00
|
|
|
paint.setXfermode(mode);
|
2011-09-08 13:36:30 +00:00
|
|
|
for (int angle = 0; angle < 24; ++angle) {
|
|
|
|
SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth;
|
|
|
|
SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight;
|
|
|
|
paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24);
|
|
|
|
canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-09-08 13:36:30 +00:00
|
|
|
return H;
|
|
|
|
}
|
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
static sk_sp<SkShader> make_bg_shader() {
|
2011-09-08 13:36:30 +00:00
|
|
|
SkBitmap bm;
|
2014-02-17 02:55:57 +00:00
|
|
|
bm.allocN32Pixels(2, 2);
|
2011-09-08 13:36:30 +00:00
|
|
|
*bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
|
|
|
|
*bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC, 0xCC, 0xCC);
|
|
|
|
|
|
|
|
SkMatrix m;
|
|
|
|
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
|
2014-04-28 14:55:39 +00:00
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode, &m);
|
2011-09-08 13:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class HairModesView : public SampleView {
|
|
|
|
SkPaint fBGPaint;
|
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
HairModesView() {
|
2016-03-09 02:50:00 +00:00
|
|
|
fBGPaint.setShader(make_bg_shader());
|
2011-09-08 13:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "HairlineModes");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
|
|
|
const SkRect bounds = SkRect::MakeWH(W, H);
|
|
|
|
static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
|
|
|
|
|
|
|
|
for (int alpha = 0; alpha < 4; ++alpha) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->save();
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
|
|
|
|
if (6 == i) {
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(W * 5, 0);
|
|
|
|
canvas->save();
|
|
|
|
}
|
|
|
|
canvas->drawRect(bounds, fBGPaint);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&bounds, nullptr);
|
2016-10-05 19:59:51 +00:00
|
|
|
SkScalar dy = drawCell(canvas, SkXfermode::Make(gModes[i].fMode),
|
2011-09-08 13:36:30 +00:00
|
|
|
gAlphaValue[alpha & 1],
|
|
|
|
gAlphaValue[alpha & 2]);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(0, dy * 5 / 4);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(W * 5 / 4, 0);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-09-08 13:36:30 +00:00
|
|
|
private:
|
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new HairModesView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|