blob: 006734f2404f0da6a70e93a5e6f460f6fd570449 [file] [log] [blame]
bsalomon@google.coma3108262011-10-10 14:08:47 +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 "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColorPriv.h"
11#include "include/core/SkShader.h"
12#include "samplecode/Sample.h"
bsalomon@google.coma3108262011-10-10 14:08:47 +000013
Brian Osmand1e67e72017-03-15 12:19:37 -040014static const SkBlendMode gModes[] = {
15 SkBlendMode::kClear,
16 SkBlendMode::kSrc,
17 SkBlendMode::kDst,
18 SkBlendMode::kSrcOver,
19 SkBlendMode::kDstOver,
20 SkBlendMode::kSrcIn,
21 SkBlendMode::kDstIn,
22 SkBlendMode::kSrcOut,
23 SkBlendMode::kDstOut,
24 SkBlendMode::kSrcATop,
25 SkBlendMode::kDstATop,
26 SkBlendMode::kXor,
bsalomon@google.coma3108262011-10-10 14:08:47 +000027};
28
29const int gWidth = 64;
30const int gHeight = 64;
31const SkScalar W = SkIntToScalar(gWidth);
32const SkScalar H = SkIntToScalar(gHeight);
33
reed374772b2016-10-05 17:33:02 -070034static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) {
bsalomon@google.coma3108262011-10-10 14:08:47 +000035 SkPaint paint;
36 paint.setAntiAlias(true);
37
38 SkRect r = SkRect::MakeWH(W, H);
39 r.inset(W/10, H/10);
40
41 paint.setColor(SK_ColorBLUE);
42 paint.setAlpha(a0);
43 canvas->drawOval(r, paint);
44
45 paint.setColor(SK_ColorRED);
46 paint.setAlpha(a1);
reed374772b2016-10-05 17:33:02 -070047 paint.setBlendMode(mode);
bsalomon@google.coma3108262011-10-10 14:08:47 +000048
49 SkScalar offset = SK_Scalar1 / 3;
50 SkRect rect = SkRect::MakeXYWH(W / 4 + offset,
51 H / 4 + offset,
52 W / 2, H / 2);
53 canvas->drawRect(rect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000054
bsalomon@google.coma3108262011-10-10 14:08:47 +000055 return H;
56}
57
reed8a21c9f2016-03-08 18:50:00 -080058static sk_sp<SkShader> make_bg_shader() {
bsalomon@google.coma3108262011-10-10 14:08:47 +000059 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000060 bm.allocN32Pixels(2, 2);
bsalomon@google.coma3108262011-10-10 14:08:47 +000061 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
62 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC,
63 0xCC, 0xCC);
64
bsalomon@google.coma3108262011-10-10 14:08:47 +000065 SkMatrix m;
66 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000067
Mike Reed50acf8f2019-04-08 13:20:23 -040068 return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
bsalomon@google.coma3108262011-10-10 14:08:47 +000069}
70
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040071class AARectsModesView : public Sample {
bsalomon@google.coma3108262011-10-10 14:08:47 +000072 SkPaint fBGPaint;
73public:
74 AARectsModesView () {
reed8a21c9f2016-03-08 18:50:00 -080075 fBGPaint.setShader(make_bg_shader());
bsalomon@google.coma3108262011-10-10 14:08:47 +000076 }
77
78protected:
Hal Canary8a027312019-07-03 10:55:44 -040079 virtual SkString name() { return SkString("AARectsModes"); }
bsalomon@google.coma3108262011-10-10 14:08:47 +000080
81 virtual void onDrawContent(SkCanvas* canvas) {
82 const SkRect bounds = SkRect::MakeWH(W, H);
83 static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
84
85 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
86
87 for (int alpha = 0; alpha < 4; ++alpha) {
88 canvas->save();
89 canvas->save();
90 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
91 if (6 == i) {
92 canvas->restore();
93 canvas->translate(W * 5, 0);
94 canvas->save();
95 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
bsalomon@google.coma3108262011-10-10 14:08:47 +000097 canvas->drawRect(bounds, fBGPaint);
halcanary96fcdcc2015-08-27 07:41:13 -070098 canvas->saveLayer(&bounds, nullptr);
Brian Osmand1e67e72017-03-15 12:19:37 -040099 SkScalar dy = drawCell(canvas, gModes[i], gAlphaValue[alpha & 1],
bsalomon@google.coma3108262011-10-10 14:08:47 +0000100 gAlphaValue[alpha & 2]);
101 canvas->restore();
102
103 canvas->translate(0, dy * 5 / 4);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000104 }
105 canvas->restore();
106 canvas->restore();
107 canvas->translate(W * 5 / 4, 0);
108 }
109 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
bsalomon@google.coma3108262011-10-10 14:08:47 +0000111private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400112 typedef Sample INHERITED;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000113};
114
115///////////////////////////////////////////////////////////////////////////////
116
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400117DEF_SAMPLE( return new AARectsModesView(); )