Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
drawatlascolor.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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"
12#include "include/core/SkFont.h"
17#include "include/core/SkRect.h"
20#include "include/core/SkSize.h"
25#include "tools/ToolUtils.h"
27
28// Create a square atlas of:
29// opaque white | opaque red
30// ------------------------------------
31// opaque green | transparent black
32//
33static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) {
34 const int kBlockSize = atlasSize/2;
35
36 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
37 auto surface(ToolUtils::makeSurface(caller, info));
38 SkCanvas* canvas = surface->getCanvas();
39
41 paint.setBlendMode(SkBlendMode::kSrc);
42
43 paint.setColor(SK_ColorWHITE);
44 SkRect r = SkRect::MakeXYWH(0, 0,
45 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
46 canvas->drawRect(r, paint);
47
48 paint.setColor(SK_ColorRED);
49 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0,
50 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
51 canvas->drawRect(r, paint);
52
53 paint.setColor(SK_ColorGREEN);
54 r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize),
55 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
56 canvas->drawRect(r, paint);
57
58 paint.setColor(SK_ColorTRANSPARENT);
59 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize),
60 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
61 canvas->drawRect(r, paint);
62
63 return surface->makeImageSnapshot();
64}
65
66// This GM tests the drawAtlas API with colors, different xfer modes
67// and transparency in the atlas image
69public:
71 this->setBGColor(0xFFCCCCCC);
72 }
73
74protected:
75 SkString getName() const override { return SkString("draw-atlas-colors"); }
76
77 SkISize getISize() override {
78 return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad,
79 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad);
80 }
81
82 void onDraw(SkCanvas* canvas) override {
83 const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize));
84
85 auto atlas = make_atlas(canvas, kAtlasSize);
86
87 const SkBlendMode gModes[] = {
117 };
118
119 SkColor gColors[] = {
122 0x88888888, // transparent grey
123 0x88000088 // transparent blue
124 };
125
126 const int numModes = std::size(gModes);
127 SkASSERT(numModes == kNumXferModes);
128 const int numColors = std::size(gColors);
129 SkASSERT(numColors == kNumColors);
130 SkRSXform xforms[numColors];
131 SkRect rects[numColors];
132 SkColor quadColors[numColors];
133
135 paint.setAntiAlias(true);
136
137 for (int i = 0; i < numColors; ++i) {
138 xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad));
139 rects[i] = target;
140 quadColors[i] = gColors[i];
141 }
142
144
145 for (int i = 0; i < numModes; ++i) {
146 const char* label = SkBlendMode_Name(gModes[i]);
147 canvas->drawString(label, i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad),
148 font, paint);
149 }
150
151 for (int i = 0; i < numModes; ++i) {
152 canvas->save();
153 canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
154 SkIntToScalar(kTextPad+kPad));
155 // w/o a paint
156 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
157 gModes[i], SkSamplingOptions(), nullptr, nullptr);
158 canvas->translate(0.0f, numColors*(target.height()+kPad));
159 // w a paint
160 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
161 gModes[i], SkSamplingOptions(), nullptr, &paint);
162 canvas->restore();
163 }
164 }
165
166private:
167 inline static constexpr int kNumXferModes = 29;
168 inline static constexpr int kNumColors = 4;
169 inline static constexpr int kAtlasSize = 30;
170 inline static constexpr int kPad = 2;
171 inline static constexpr int kTextPad = 8;
172
173 using INHERITED = GM;
174};
175DEF_GM( return new DrawAtlasColorsGM; )
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
static const SkColor gColors[]
#define SkASSERT(cond)
Definition SkAssert.h:116
SK_API const char * SkBlendMode_Name(SkBlendMode blendMode)
SkBlendMode
Definition SkBlendMode.h:38
@ kSrcOut
r = s * (1-da)
@ kExclusion
rc = s + d - two(s*d), ra = kSrcOver
@ kSaturation
saturation of source with hue and luminosity of destination
@ kColorBurn
darken destination to reflect source
@ kPlus
r = min(s + d, 1)
@ kLighten
rc = s + d - min(s*da, d*sa), ra = kSrcOver
@ kHue
hue of source with saturation and luminosity of destination
@ kDstIn
r = d * sa
@ kModulate
r = s*d
@ kMultiply
r = s*(1-da) + d*(1-sa) + s*d
@ kColorDodge
brighten destination to reflect source
@ kScreen
r = s + d - s*d
@ kSrcOver
r = s + (1-sa)*d
@ kXor
r = s*(1-da) + d*(1-sa)
@ kLuminosity
luminosity of source with hue and saturation of destination
@ kSoftLight
lighten or darken, depending on source
@ kDifference
rc = s + d - 2*(min(s*da, d*sa)), ra = kSrcOver
@ 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
@ kColor
hue and saturation of source with luminosity of destination
@ kHardLight
multiply or screen, depending on source
@ 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
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
#define SkIntToScalar(x)
Definition SkScalar.h:57
constexpr SkBlendMode gModes[]
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
SkString getName() const override
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
int save()
Definition SkCanvas.cpp:451
void drawAtlas(const SkImage *atlas, const SkRSXform xform[], const SkRect tex[], const SkColor colors[], int count, SkBlendMode mode, const SkSamplingOptions &sampling, const SkRect *cullRect, const SkPaint *paint)
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition SkCanvas.h:1803
T * get() const
Definition SkRefCnt.h:303
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition gm.cpp:81
void setBGColor(SkColor)
Definition gm.cpp:159
const Paint & paint
static sk_sp< SkImage > make_atlas(SkCanvas *caller, int atlasSize)
VkSurfaceKHR surface
Definition main.cc:49
uint32_t * target
#define DEF_GM(CODE)
Definition gm.h:40
sk_sp< SkTypeface > DefaultPortableTypeface()
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
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 MakeWH(float w, float h)
Definition SkRect.h:609