blob: 4f85c198836d5d3c607c63799b109d30c27e5df6 [file] [log] [blame]
reed@google.comab973972011-09-19 19:01:38 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkColorPriv.h"
11#include "SkShader.h"
12
reed@google.comab973972011-09-19 19:01:38 +000013static const struct {
14 SkXfermode::Mode fMode;
15 const char* fLabel;
16} gModes[] = {
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" },
29};
30
31const int gWidth = 64;
32const int gHeight = 64;
33const SkScalar W = SkIntToScalar(gWidth);
34const SkScalar H = SkIntToScalar(gHeight);
35
36static SkScalar drawCell(SkCanvas* canvas, SkXfermode* mode, SkAlpha a0, SkAlpha a1) {
37
38 SkPaint paint;
39 paint.setAntiAlias(true);
40
41 SkRect r = SkRect::MakeWH(W, H);
42 r.inset(W/10, H/10);
43
44 paint.setColor(SK_ColorBLUE);
45 paint.setAlpha(a0);
46 canvas->drawOval(r, paint);
47
48 paint.setColor(SK_ColorRED);
49 paint.setAlpha(a1);
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);
56 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000057
reed@google.comab973972011-09-19 19:01:38 +000058 return H;
59}
60
61static SkShader* make_bg_shader() {
62 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000063 bm.allocN32Pixels(2, 2);
reed@google.comab973972011-09-19 19:01:38 +000064 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
caryclark12596012015-07-29 05:27:47 -070065 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE, 0xCF, 0xCE);
reed@google.comab973972011-09-19 19:01:38 +000066
reed@google.comab973972011-09-19 19:01:38 +000067 SkMatrix m;
68 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000069 return SkShader::CreateBitmapShader(bm,
70 SkShader::kRepeat_TileMode,
71 SkShader::kRepeat_TileMode,
72 &m);
reed@google.comab973972011-09-19 19:01:38 +000073}
74
75namespace skiagm {
rmistry@google.comae933ce2012-08-23 18:19:56 +000076
reed@google.comab973972011-09-19 19:01:38 +000077 class HairModesGM : public GM {
78 SkPaint fBGPaint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000079
reed@google.comab973972011-09-19 19:01:38 +000080 protected:
mtklein36352bf2015-03-25 18:17:31 -070081 SkString onShortName() override {
reed@google.comab973972011-09-19 19:01:38 +000082 return SkString("hairmodes");
83 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
mtklein36352bf2015-03-25 18:17:31 -070085 virtual SkISize onISize() override { return SkISize::Make(640, 480); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
mtklein36352bf2015-03-25 18:17:31 -070087 void onOnceBeforeDraw() override {
reed@google.com7775d662012-11-27 15:15:58 +000088 fBGPaint.setShader(make_bg_shader())->unref();
89 }
90
mtklein36352bf2015-03-25 18:17:31 -070091 void onDraw(SkCanvas* canvas) override {
reed@google.comab973972011-09-19 19:01:38 +000092 const SkRect bounds = SkRect::MakeWH(W, H);
93 static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
reed@google.comab973972011-09-19 19:01:38 +000095 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
reed@google.comab973972011-09-19 19:01:38 +000097 for (int alpha = 0; alpha < 4; ++alpha) {
98 canvas->save();
99 canvas->save();
100 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
101 if (6 == i) {
102 canvas->restore();
103 canvas->translate(W * 5, 0);
104 canvas->save();
105 }
106 SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
reed@google.comab973972011-09-19 19:01:38 +0000108 canvas->drawRect(bounds, fBGPaint);
halcanary96fcdcc2015-08-27 07:41:13 -0700109 canvas->saveLayer(&bounds, nullptr);
reed@google.comab973972011-09-19 19:01:38 +0000110 SkScalar dy = drawCell(canvas, mode,
111 gAlphaValue[alpha & 1],
112 gAlphaValue[alpha & 2]);
113 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
reed@google.comab973972011-09-19 19:01:38 +0000115 canvas->translate(0, dy * 5 / 4);
116 SkSafeUnref(mode);
117 }
118 canvas->restore();
119 canvas->restore();
120 canvas->translate(W * 5 / 4, 0);
121 }
122 }
reed@google.comfbc21172011-09-19 19:08:33 +0000123
reed@google.comab973972011-09-19 19:01:38 +0000124 private:
125 typedef GM INHERITED;
126 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
reed@google.comab973972011-09-19 19:01:38 +0000128 //////////////////////////////////////////////////////////////////////////////
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@google.comab973972011-09-19 19:01:38 +0000130 static GM* MyFactory(void*) { return new HairModesGM; }
131 static GMRegistry reg(MyFactory);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed@google.comab973972011-09-19 19:01:38 +0000133}