blob: 1be3c1d326c90b888c80a589335186af0f964cdb [file] [log] [blame]
Robert Phillipsaea785b2018-08-03 11:15:24 -04001/*
2 * Copyright 2018 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"
9#include "include/core/SkBitmap.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040010#include "include/core/SkBlendMode.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkFont.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/utils/SkTextUtils.h"
21#include "tools/ToolUtils.h"
Robert Phillipsaea785b2018-08-03 11:15:24 -040022
Ben Wagnerd1701ba2019-04-30 13:44:26 -040023#include <initializer_list>
24
Robert Phillipsaea785b2018-08-03 11:15:24 -040025namespace skiagm {
26
27// This GM recreates the blend mode images from the Android documentation
28class AndroidBlendModesGM : public GM {
29public:
30 AndroidBlendModesGM() {
31 this->setBGColor(SK_ColorBLACK);
32 }
33
34protected:
35 SkString onShortName() override {
36 return SkString("androidblendmodes");
37 }
38
39 SkISize onISize() override {
40 return SkISize::Make(kNumCols * kBitmapSize, kNumRows * kBitmapSize);
41 }
42
43 void onOnceBeforeDraw() override {
44 SkImageInfo ii = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
45 {
46 fCompositeSrc.allocPixels(ii);
47 SkCanvas tmp(fCompositeSrc);
48 tmp.clear(SK_ColorTRANSPARENT);
49 SkPaint p;
50 p.setAntiAlias(true);
Mike Kleinea3f0142019-03-20 11:12:10 -050051 p.setColor(ToolUtils::color_to_565(kBlue));
Robert Phillipsaea785b2018-08-03 11:15:24 -040052 tmp.drawRect(SkRect::MakeLTRB(16, 96, 160, 240), p);
53 }
54
55 {
56 fCompositeDst.allocPixels(ii);
57 SkCanvas tmp(fCompositeDst);
58 tmp.clear(SK_ColorTRANSPARENT);
59 SkPaint p;
60 p.setAntiAlias(true);
Mike Kleinea3f0142019-03-20 11:12:10 -050061 p.setColor(ToolUtils::color_to_565(kRed));
Robert Phillipsaea785b2018-08-03 11:15:24 -040062 tmp.drawCircle(160, 95, 80, p);
63 }
64 }
65
66 void drawTile(SkCanvas* canvas, int xOffset, int yOffset, SkBlendMode mode) {
67 canvas->translate(xOffset, yOffset);
68
69 canvas->clipRect(SkRect::MakeXYWH(0, 0, 256, 256));
70
71 canvas->saveLayer(nullptr, nullptr);
72
73 SkPaint p;
74 canvas->drawBitmap(fCompositeDst, 0, 0, &p);
75 p.setBlendMode(mode);
76 canvas->drawBitmap(fCompositeSrc, 0, 0, &p);
77 }
78
79 void onDraw(SkCanvas* canvas) override {
Mike Kleinea3f0142019-03-20 11:12:10 -050080 SkFont font(ToolUtils::create_portable_typeface());
Robert Phillipsaea785b2018-08-03 11:15:24 -040081
Mike Kleinea3f0142019-03-20 11:12:10 -050082 ToolUtils::draw_checkerboard(canvas, kWhite, kGrey, 32);
Robert Phillipsaea785b2018-08-03 11:15:24 -040083
84 int xOffset = 0, yOffset = 0;
85
86 // Android doesn't expose all the blend modes
87 // Note that the Android documentation calls:
88 // Skia's kPlus, add
89 // Skia's kModulate, multiply
90 for (SkBlendMode mode : { SkBlendMode::kPlus /* add */, SkBlendMode::kClear,
91 SkBlendMode::kDarken, SkBlendMode::kDst,
92 SkBlendMode::kDstATop, SkBlendMode::kDstIn,
93 SkBlendMode::kDstOut, SkBlendMode::kDstOver,
94 SkBlendMode::kLighten, SkBlendMode::kModulate /* multiply */,
95 SkBlendMode::kOverlay, SkBlendMode::kScreen,
96 SkBlendMode::kSrc, SkBlendMode::kSrcATop,
97 SkBlendMode::kSrcIn, SkBlendMode::kSrcOut,
98 SkBlendMode::kSrcOver, SkBlendMode::kXor } ) {
99
100 int saveCount = canvas->save();
101 this->drawTile(canvas, xOffset, yOffset, mode);
102 canvas->restoreToCount(saveCount);
103
Mike Reed331ccfd2018-10-25 12:36:06 -0400104 SkTextUtils::DrawString(canvas, SkBlendMode_Name(mode),
Robert Phillipsaea785b2018-08-03 11:15:24 -0400105 xOffset + kBitmapSize/2.0f,
106 yOffset + kBitmapSize,
Mike Reeddc5863c2018-12-23 23:19:14 -0500107 font, SkPaint(), SkTextUtils::kCenter_Align);
Robert Phillipsaea785b2018-08-03 11:15:24 -0400108
109 xOffset += 256;
110 if (xOffset >= 1024) {
111 xOffset = 0;
112 yOffset += 256;
113 }
114 }
115 }
116
117private:
118 static const int kBitmapSize = 256;
119 static const int kNumRows = 5;
120 static const int kNumCols = 4;
121
122 static const SkColor kBlue = SkColorSetARGB(255, 22, 150, 243);
123 static const SkColor kRed = SkColorSetARGB(255, 233, 30, 99);
124 static const SkColor kWhite = SkColorSetARGB(255, 243, 243, 243);
125 static const SkColor kGrey = SkColorSetARGB(255, 222, 222, 222);
126
127 SkBitmap fCompositeSrc;
128 SkBitmap fCompositeDst;
129
130 typedef GM INHERITED;
131};
132
133//////////////////////////////////////////////////////////////////////////////
134
135DEF_GM(return new AndroidBlendModesGM;)
136}