2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkColorPriv.h"
14 SkXfermode::Mode fMode;
17 { SkXfermode::kClear_Mode, "Clear" },
18 { SkXfermode::kSrc_Mode, "Src" },
19 { SkXfermode::kDst_Mode, "Dst" },
20 { SkXfermode::kSrcOver_Mode, "SrcOver" },
21 { SkXfermode::kDstOver_Mode, "DstOver" },
22 { SkXfermode::kSrcIn_Mode, "SrcIn" },
23 { SkXfermode::kDstIn_Mode, "DstIn" },
24 { SkXfermode::kSrcOut_Mode, "SrcOut" },
25 { SkXfermode::kDstOut_Mode, "DstOut" },
26 { SkXfermode::kSrcATop_Mode, "SrcATop" },
27 { SkXfermode::kDstATop_Mode, "DstATop" },
28 { SkXfermode::kXor_Mode, "Xor" },
31 const int gWidth = 64;
32 const int gHeight = 64;
33 const SkScalar W = SkIntToScalar(gWidth);
34 const SkScalar H = SkIntToScalar(gHeight);
36 static SkScalar drawCell(SkCanvas* canvas, SkXfermode* mode, SkAlpha a0, SkAlpha a1) {
39 paint.setAntiAlias(true);
41 SkRect r = SkRect::MakeWH(W, H);
44 paint.setColor(SK_ColorBLUE);
46 canvas->drawOval(r, paint);
48 paint.setColor(SK_ColorRED);
50 paint.setXfermode(mode);
51 for (int angle = 0; angle < 24; ++angle) {
52 SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth;
53 SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight;
54 paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24);
55 canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint);
61 static SkShader* make_bg_shader() {
63 bm.allocN32Pixels(2, 2);
64 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
65 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC, 0xCC, 0xCC);
68 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
69 return SkShader::CreateBitmapShader(bm,
70 SkShader::kRepeat_TileMode,
71 SkShader::kRepeat_TileMode,
77 class HairModesGM : public GM {
81 SkString onShortName() SK_OVERRIDE {
82 return SkString("hairmodes");
85 virtual SkISize onISize() SK_OVERRIDE { return SkISize::Make(640, 480); }
87 void onOnceBeforeDraw() SK_OVERRIDE {
88 fBGPaint.setShader(make_bg_shader())->unref();
91 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
92 const SkRect bounds = SkRect::MakeWH(W, H);
93 static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
95 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
97 for (int alpha = 0; alpha < 4; ++alpha) {
100 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
103 canvas->translate(W * 5, 0);
106 SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
108 canvas->drawRect(bounds, fBGPaint);
109 canvas->saveLayer(&bounds, NULL);
110 SkScalar dy = drawCell(canvas, mode,
111 gAlphaValue[alpha & 1],
112 gAlphaValue[alpha & 2]);
115 canvas->translate(0, dy * 5 / 4);
120 canvas->translate(W * 5 / 4, 0);
125 typedef GM INHERITED;
128 //////////////////////////////////////////////////////////////////////////////
130 static GM* MyFactory(void*) { return new HairModesGM; }
131 static GMRegistry reg(MyFactory);