blob: e05ca9235bf5b6a1df60f6dd83fa9faa33d92510 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkColorPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkMatrix.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTileMode.h"
23#include "include/core/SkTypes.h"
reed@google.comab973972011-09-19 19:01:38 +000024
Brian Osmand1e67e72017-03-15 12:19:37 -040025constexpr SkBlendMode gModes[] = {
26 SkBlendMode::kClear,
27 SkBlendMode::kSrc,
28 SkBlendMode::kDst,
29 SkBlendMode::kSrcOver,
30 SkBlendMode::kDstOver,
31 SkBlendMode::kSrcIn,
32 SkBlendMode::kDstIn,
33 SkBlendMode::kSrcOut,
34 SkBlendMode::kDstOut,
35 SkBlendMode::kSrcATop,
36 SkBlendMode::kDstATop,
37 SkBlendMode::kXor,
reed@google.comab973972011-09-19 19:01:38 +000038};
39
40const int gWidth = 64;
41const int gHeight = 64;
42const SkScalar W = SkIntToScalar(gWidth);
43const SkScalar H = SkIntToScalar(gHeight);
44
reed374772b2016-10-05 17:33:02 -070045static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) {
reed@google.comab973972011-09-19 19:01:38 +000046
47 SkPaint paint;
48 paint.setAntiAlias(true);
49
50 SkRect r = SkRect::MakeWH(W, H);
51 r.inset(W/10, H/10);
52
53 paint.setColor(SK_ColorBLUE);
54 paint.setAlpha(a0);
55 canvas->drawOval(r, paint);
56
57 paint.setColor(SK_ColorRED);
58 paint.setAlpha(a1);
reed374772b2016-10-05 17:33:02 -070059 paint.setBlendMode(mode);
reed@google.comab973972011-09-19 19:01:38 +000060 for (int angle = 0; angle < 24; ++angle) {
61 SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth;
62 SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight;
63 paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24);
64 canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint);
65 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000066
reed@google.comab973972011-09-19 19:01:38 +000067 return H;
68}
69
reed1a9b9642016-03-13 14:13:58 -070070static sk_sp<SkShader> make_bg_shader() {
reed@google.comab973972011-09-19 19:01:38 +000071 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000072 bm.allocN32Pixels(2, 2);
reed@google.comab973972011-09-19 19:01:38 +000073 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
caryclark12596012015-07-29 05:27:47 -070074 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE, 0xCF, 0xCE);
reed@google.comab973972011-09-19 19:01:38 +000075
reed@google.comab973972011-09-19 19:01:38 +000076 SkMatrix m;
77 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
Mike Reed50acf8f2019-04-08 13:20:23 -040078 return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
reed@google.comab973972011-09-19 19:01:38 +000079}
80
81namespace skiagm {
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
reed@google.comab973972011-09-19 19:01:38 +000083 class HairModesGM : public GM {
84 SkPaint fBGPaint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
reed@google.comab973972011-09-19 19:01:38 +000086 protected:
mtklein36352bf2015-03-25 18:17:31 -070087 SkString onShortName() override {
reed@google.comab973972011-09-19 19:01:38 +000088 return SkString("hairmodes");
89 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
mtklein36352bf2015-03-25 18:17:31 -070091 virtual SkISize onISize() override { return SkISize::Make(640, 480); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
mtklein36352bf2015-03-25 18:17:31 -070093 void onOnceBeforeDraw() override {
reed1a9b9642016-03-13 14:13:58 -070094 fBGPaint.setShader(make_bg_shader());
reed@google.com7775d662012-11-27 15:15:58 +000095 }
96
mtklein36352bf2015-03-25 18:17:31 -070097 void onDraw(SkCanvas* canvas) override {
reed@google.comab973972011-09-19 19:01:38 +000098 const SkRect bounds = SkRect::MakeWH(W, H);
mtkleindbfd7ab2016-09-01 11:24:54 -070099 constexpr SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@google.comab973972011-09-19 19:01:38 +0000101 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
reed@google.comab973972011-09-19 19:01:38 +0000103 for (int alpha = 0; alpha < 4; ++alpha) {
104 canvas->save();
105 canvas->save();
106 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
107 if (6 == i) {
108 canvas->restore();
109 canvas->translate(W * 5, 0);
110 canvas->save();
111 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
reed@google.comab973972011-09-19 19:01:38 +0000113 canvas->drawRect(bounds, fBGPaint);
halcanary96fcdcc2015-08-27 07:41:13 -0700114 canvas->saveLayer(&bounds, nullptr);
Brian Osmand1e67e72017-03-15 12:19:37 -0400115 SkScalar dy = drawCell(canvas, gModes[i],
reed@google.comab973972011-09-19 19:01:38 +0000116 gAlphaValue[alpha & 1],
117 gAlphaValue[alpha & 2]);
118 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
reed@google.comab973972011-09-19 19:01:38 +0000120 canvas->translate(0, dy * 5 / 4);
reed@google.comab973972011-09-19 19:01:38 +0000121 }
122 canvas->restore();
123 canvas->restore();
124 canvas->translate(W * 5 / 4, 0);
125 }
126 }
reed@google.comfbc21172011-09-19 19:08:33 +0000127
reed@google.comab973972011-09-19 19:01:38 +0000128 private:
129 typedef GM INHERITED;
130 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed@google.comab973972011-09-19 19:01:38 +0000132 //////////////////////////////////////////////////////////////////////////////
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
Hal Canarye964c182019-01-23 10:22:01 -0500134 DEF_GM( return new HairModesGM; )
reed@google.comab973972011-09-19 19:01:38 +0000135}