blob: 592cf7104233d39fc1449f71d3f0a1bf2675bc1e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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 */
reed@google.combf0001d2014-01-13 14:53:55 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColorFilter.h"
11#include "include/core/SkColorPriv.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkGraphics.h"
14#include "include/core/SkPath.h"
15#include "include/core/SkRegion.h"
16#include "include/core/SkShader.h"
17#include "include/core/SkTime.h"
18#include "include/core/SkTypeface.h"
19#include "include/effects/SkCornerPathEffect.h"
20#include "include/effects/SkGradientShader.h"
21#include "include/utils/SkRandom.h"
22#include "include/utils/SkTextUtils.h"
23#include "samplecode/Sample.h"
24#include "src/core/SkBlurMask.h"
25#include "src/utils/SkUTF.h"
bsalomon@google.com271cffc2011-05-20 14:13:56 +000026
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/core/SkColorPriv.h"
Mike Reedfe2d3c72019-12-30 10:09:43 -050028#include "include/core/SkMaskFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "include/core/SkStream.h"
bsalomon@google.com271cffc2011-05-20 14:13:56 +000030
Mike Reedb579f072019-01-03 15:45:53 -050031static void setNamedTypeface(SkFont* font, const char name[]) {
32 font->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000033}
34
35static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
36
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040037class XfermodesBlurView : public Sample {
bsalomon@google.com271cffc2011-05-20 14:13:56 +000038 SkBitmap fBG;
39 SkBitmap fSrcB, fDstB;
40
reed374772b2016-10-05 17:33:02 -070041 void draw_mode(SkCanvas* canvas, SkBlendMode mode, int alpha, SkScalar x, SkScalar y) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042 SkPaint p;
Mike Reed1be1f8d2018-03-14 13:01:17 -040043 p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
44 SkBlurMask::ConvertRadiusToSigma(5)));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000045
46 SkScalar ww = SkIntToScalar(W);
47 SkScalar hh = SkIntToScalar(H);
48
49 // draw a circle covering the upper
50 // left three quarters of the canvas
51 p.setColor(0xFFCC44FF);
52 SkRect r;
Mike Reed92b33352019-08-24 19:39:13 -040053 r.setLTRB(0, 0, ww*3/4, hh*3/4);
bsalomon@google.com271cffc2011-05-20 14:13:56 +000054 r.offset(x, y);
55 canvas->drawOval(r, p);
56
reed374772b2016-10-05 17:33:02 -070057 p.setBlendMode(mode);
bsalomon@google.com271cffc2011-05-20 14:13:56 +000058
59 // draw a square overlapping the circle
60 // in the lower right of the canvas
61 p.setColor(0x00AA6633 | alpha << 24);
Mike Reed92b33352019-08-24 19:39:13 -040062 r.setLTRB(ww/3, hh/3, ww*19/20, hh*19/20);
bsalomon@google.com271cffc2011-05-20 14:13:56 +000063 r.offset(x, y);
64 canvas->drawRect(r, p);
65 }
66
67public:
68 const static int W = 64;
69 const static int H = 64;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000070 XfermodesBlurView() {
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000071 fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType, kPremul_SkAlphaType),
72 gBG, 4);
bsalomon@google.com271cffc2011-05-20 14:13:56 +000073 }
74
75protected:
Hal Canary8a027312019-07-03 10:55:44 -040076 virtual SkString name() { return SkString("XfermodesBlur"); }
bsalomon@google.com271cffc2011-05-20 14:13:56 +000077
78 virtual void onDrawContent(SkCanvas* canvas) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +000079 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
80
Brian Osmand1e67e72017-03-15 12:19:37 -040081 const SkBlendMode gModes[] = {
82 SkBlendMode::kClear,
83 SkBlendMode::kSrc,
84 SkBlendMode::kDst,
85 SkBlendMode::kSrcOver,
86 SkBlendMode::kDstOver,
87 SkBlendMode::kSrcIn,
88 SkBlendMode::kDstIn,
89 SkBlendMode::kSrcOut,
90 SkBlendMode::kDstOut,
91 SkBlendMode::kSrcATop,
92 SkBlendMode::kDstATop,
93 SkBlendMode::kXor,
94 SkBlendMode::kPlus,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000095 };
96
97 const SkScalar w = SkIntToScalar(W);
98 const SkScalar h = SkIntToScalar(H);
bsalomon@google.com271cffc2011-05-20 14:13:56 +000099 SkMatrix m;
100 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
Mike Reed50acf8f2019-04-08 13:20:23 -0400101 auto s = fBG.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000102
Mike Reedb579f072019-01-03 15:45:53 -0500103 SkFont font;
104 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
105 setNamedTypeface(&font, "Menlo Regular");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000106
107 const int W = 5;
108
109 SkScalar x0 = 0;
110 for (int twice = 0; twice < 2; twice++) {
111 SkScalar x = x0, y = 0;
112 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000113 SkRect r;
Mike Reed92b33352019-08-24 19:39:13 -0400114 r.setLTRB(x, y, x+w, y+h);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000115
116 SkPaint p;
117 p.setStyle(SkPaint::kFill_Style);
118 p.setShader(s);
119 canvas->drawRect(r, p);
120
halcanary96fcdcc2015-08-27 07:41:13 -0700121 canvas->saveLayer(&r, nullptr);
Brian Osmand1e67e72017-03-15 12:19:37 -0400122 draw_mode(canvas, gModes[i], twice ? 0x88 : 0xFF, r.fLeft, r.fTop);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000123 canvas->restore();
124
125 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
126 p.setStyle(SkPaint::kStroke_Style);
halcanary96fcdcc2015-08-27 07:41:13 -0700127 p.setShader(nullptr);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000128 canvas->drawRect(r, p);
129
Brian Osmand1e67e72017-03-15 12:19:37 -0400130 const char* label = SkBlendMode_Name(gModes[i]);
Mike Reedb579f072019-01-03 15:45:53 -0500131 SkTextUtils::DrawString(canvas, label, x + w/2, y - font.getSize()/2, font, SkPaint(),
Mike Reed3a42ec02018-10-30 12:53:21 -0400132 SkTextUtils::kCenter_Align);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000133 x += w + SkIntToScalar(10);
134 if ((i % W) == W - 1) {
135 x = x0;
136 y += h + SkIntToScalar(30);
137 }
138 }
139 x0 += SkIntToScalar(400);
140 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000141 }
142
143private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400144 typedef Sample INHERITED;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000145};
146
147//////////////////////////////////////////////////////////////////////////////
148
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400149DEF_SAMPLE( return new XfermodesBlurView(); )