Flutter Engine
The Flutter Engine
androidblendmodes.cpp
Go to the documentation of this file.
1/*
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
8#include "gm/gm.h"
13#include "include/core/SkFont.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
21#include "tools/ToolUtils.h"
23
24#include <initializer_list>
25
26namespace skiagm {
27
28// This GM recreates the blend mode images from the Android documentation
29class AndroidBlendModesGM : public GM {
30public:
33 }
34
35protected:
36 SkString getName() const override { return SkString("androidblendmodes"); }
37
38 SkISize getISize() override {
39 return SkISize::Make(kNumCols * kBitmapSize, kNumRows * kBitmapSize);
40 }
41
42 void onOnceBeforeDraw() override {
43 SkImageInfo ii = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
44 {
45 fCompositeSrc.allocPixels(ii);
46 SkCanvas tmp(fCompositeSrc);
48 SkPaint p;
49 p.setAntiAlias(true);
50 p.setColor(ToolUtils::color_to_565(kBlue));
51 tmp.drawRect(SkRect::MakeLTRB(16, 96, 160, 240), p);
52 }
53
54 {
55 fCompositeDst.allocPixels(ii);
56 SkCanvas tmp(fCompositeDst);
58 SkPaint p;
59 p.setAntiAlias(true);
60 p.setColor(ToolUtils::color_to_565(kRed));
61 tmp.drawCircle(160, 95, 80, p);
62 }
63 }
64
65 void drawTile(SkCanvas* canvas, int xOffset, int yOffset, SkBlendMode mode) {
66 canvas->translate(xOffset, yOffset);
67
68 canvas->clipRect(SkRect::MakeXYWH(0, 0, 256, 256));
69
70 canvas->saveLayer(nullptr, nullptr);
71
72 SkPaint p;
73 canvas->drawImage(fCompositeDst.asImage(), 0, 0, SkSamplingOptions(), &p);
74 p.setBlendMode(mode);
75 canvas->drawImage(fCompositeSrc.asImage(), 0, 0, SkSamplingOptions(), &p);
76 }
77
78 void onDraw(SkCanvas* canvas) override {
80
81 ToolUtils::draw_checkerboard(canvas, kWhite, kGrey, 32);
82
83 int xOffset = 0, yOffset = 0;
84
85 // Android doesn't expose all the blend modes
86 // Note that the Android documentation calls:
87 // Skia's kPlus, add
88 // Skia's kModulate, multiply
98
99 int saveCount = canvas->save();
100 this->drawTile(canvas, xOffset, yOffset, mode);
101 canvas->restoreToCount(saveCount);
102
104 xOffset + kBitmapSize/2.0f,
105 yOffset + kBitmapSize,
107
108 xOffset += 256;
109 if (xOffset >= 1024) {
110 xOffset = 0;
111 yOffset += 256;
112 }
113 }
114 }
115
116private:
117 static const int kBitmapSize = 256;
118 static const int kNumRows = 5;
119 static const int kNumCols = 4;
120
121 static const SkColor kBlue = SkColorSetARGB(255, 22, 150, 243);
122 static const SkColor kRed = SkColorSetARGB(255, 233, 30, 99);
123 static const SkColor kWhite = SkColorSetARGB(255, 243, 243, 243);
124 static const SkColor kGrey = SkColorSetARGB(255, 222, 222, 222);
125
126 SkBitmap fCompositeSrc;
127 SkBitmap fCompositeDst;
128
129 using INHERITED = GM;
130};
131
132//////////////////////////////////////////////////////////////////////////////
133
134DEF_GM(return new AndroidBlendModesGM;)
135} // namespace skiagm
SK_API const char * SkBlendMode_Name(SkBlendMode blendMode)
SkBlendMode
Definition: SkBlendMode.h:38
@ kSrcOut
r = s * (1-da)
@ kPlus
r = min(s + d, 1)
@ kLighten
rc = s + d - min(s*da, d*sa), ra = kSrcOver
@ kDstIn
r = d * sa
@ kModulate
r = s*d
@ kScreen
r = s + d - s*d
@ kSrcOver
r = s + (1-sa)*d
@ kXor
r = s*(1-da) + d*(1-sa)
@ kOverlay
multiply or screen, depending on destination
@ kSrcATop
r = s*da + d*(1-sa)
@ kDstATop
r = d*sa + s*(1-da)
@ kDstOver
r = d + (1-da)*s
@ kDstOut
r = d * (1-sa)
@ kDarken
rc = s + d - max(s*da, d*sa), ra = kSrcOver
@ kSrcIn
r = s * da
@ kClear
r = 0
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clear(SkColor color)
Definition: SkCanvas.h:1199
void restoreToCount(int saveCount)
Definition: SkCanvas.cpp:478
int save()
Definition: SkCanvas.cpp:447
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
Definition: SkFont.h:35
static void DrawString(SkCanvas *canvas, const char text[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint, Align align=kLeft_Align)
Definition: SkTextUtils.h:34
SkString getName() const override
void drawTile(SkCanvas *canvas, int xOffset, int yOffset, SkBlendMode mode)
void onDraw(SkCanvas *canvas) override
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void setBGColor(SkColor)
Definition: gm.cpp:159
void draw_checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
Definition: ToolUtils.cpp:174
SkFont DefaultPortableFont()
SkColor color_to_565(SkColor color)
Definition: ToolUtils.cpp:139
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
font
Font Metadata and Metrics.
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646