blob: 8d53e31425a1f9055de872dde1d29839dae98017 [file] [log] [blame]
robertphillips29ccdf82015-07-24 10:20:45 -07001/*
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.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
robertphillips29ccdf82015-07-24 10:20:45 -070010#include "SkCanvas.h"
11#include "SkRSXform.h"
12#include "SkSurface.h"
13
14// Create a square atlas of:
15// opaque white | opaque red
16// ------------------------------------
17// opaque green | transparent black
18//
reed9ce9d672016-03-17 10:51:11 -070019static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) {
robertphillips29ccdf82015-07-24 10:20:45 -070020 const int kBlockSize = atlasSize/2;
21
22 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
Mike Reed46596ae2018-01-02 15:40:29 -050023 auto surface(sk_tool_utils::makeSurface(caller, info));
robertphillips29ccdf82015-07-24 10:20:45 -070024 SkCanvas* canvas = surface->getCanvas();
25
26 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070027 paint.setBlendMode(SkBlendMode::kSrc);
robertphillips29ccdf82015-07-24 10:20:45 -070028
29 paint.setColor(SK_ColorWHITE);
halcanary9d524f22016-03-29 09:03:52 -070030 SkRect r = SkRect::MakeXYWH(0, 0,
robertphillips29ccdf82015-07-24 10:20:45 -070031 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
32 canvas->drawRect(r, paint);
33
34 paint.setColor(SK_ColorRED);
halcanary9d524f22016-03-29 09:03:52 -070035 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0,
robertphillips29ccdf82015-07-24 10:20:45 -070036 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
37 canvas->drawRect(r, paint);
38
39 paint.setColor(SK_ColorGREEN);
halcanary9d524f22016-03-29 09:03:52 -070040 r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize),
robertphillips29ccdf82015-07-24 10:20:45 -070041 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
42 canvas->drawRect(r, paint);
43
44 paint.setColor(SK_ColorTRANSPARENT);
halcanary9d524f22016-03-29 09:03:52 -070045 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize),
robertphillips29ccdf82015-07-24 10:20:45 -070046 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
47 canvas->drawRect(r, paint);
48
reed9ce9d672016-03-17 10:51:11 -070049 return surface->makeImageSnapshot();
robertphillips29ccdf82015-07-24 10:20:45 -070050}
51
52// This GM tests the drawAtlas API with colors, different xfer modes
53// and transparency in the atlas image
54class DrawAtlasColorsGM : public skiagm::GM {
55public:
56 DrawAtlasColorsGM() {
57 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
58 }
halcanary9d524f22016-03-29 09:03:52 -070059
robertphillips29ccdf82015-07-24 10:20:45 -070060protected:
61 SkString onShortName() override {
62 return SkString("draw-atlas-colors");
63 }
halcanary9d524f22016-03-29 09:03:52 -070064
robertphillips29ccdf82015-07-24 10:20:45 -070065 SkISize onISize() override {
66 return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad,
67 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad);
68 }
halcanary9d524f22016-03-29 09:03:52 -070069
robertphillips29ccdf82015-07-24 10:20:45 -070070 void onDraw(SkCanvas* canvas) override {
71 const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize));
72
brianosman95e8d0a2016-09-29 13:43:49 -070073 auto atlas = make_atlas(canvas, kAtlasSize);
robertphillips29ccdf82015-07-24 10:20:45 -070074
Brian Osmand1e67e72017-03-15 12:19:37 -040075 const SkBlendMode gModes[] = {
76 SkBlendMode::kClear,
77 SkBlendMode::kSrc,
78 SkBlendMode::kDst,
79 SkBlendMode::kSrcOver,
80 SkBlendMode::kDstOver,
81 SkBlendMode::kSrcIn,
82 SkBlendMode::kDstIn,
83 SkBlendMode::kSrcOut,
84 SkBlendMode::kDstOut,
85 SkBlendMode::kSrcATop,
86 SkBlendMode::kDstATop,
87 SkBlendMode::kXor,
88 SkBlendMode::kPlus,
89 SkBlendMode::kModulate,
90 SkBlendMode::kScreen,
91 SkBlendMode::kOverlay,
92 SkBlendMode::kDarken,
93 SkBlendMode::kLighten,
94 SkBlendMode::kColorDodge,
95 SkBlendMode::kColorBurn,
96 SkBlendMode::kHardLight,
97 SkBlendMode::kSoftLight,
98 SkBlendMode::kDifference,
99 SkBlendMode::kExclusion,
100 SkBlendMode::kMultiply,
101 SkBlendMode::kHue,
102 SkBlendMode::kSaturation,
103 SkBlendMode::kColor,
104 SkBlendMode::kLuminosity,
robertphillips29ccdf82015-07-24 10:20:45 -0700105 };
106
107 SkColor gColors[] = {
108 SK_ColorWHITE,
109 SK_ColorRED,
110 0x88888888, // transparent grey
111 0x88000088 // transparent blue
112 };
113
114 const int numModes = SK_ARRAY_COUNT(gModes);
115 SkASSERT(numModes == kNumXferModes);
116 const int numColors = SK_ARRAY_COUNT(gColors);
117 SkASSERT(numColors == kNumColors);
118 SkRSXform xforms[numColors];
119 SkRect rects[numColors];
120 SkColor quadColors[numColors];
121
122 SkPaint paint;
123 paint.setAntiAlias(true);
124
125 for (int i = 0; i < numColors; ++i) {
126 xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad));
127 rects[i] = target;
128 quadColors[i] = gColors[i];
129 }
130
131 SkPaint textP;
132 textP.setTextSize(SkIntToScalar(kTextPad));
133 textP.setAntiAlias(true);
halcanary96fcdcc2015-08-27 07:41:13 -0700134 sk_tool_utils::set_portable_typeface(&textP, nullptr);
robertphillips29ccdf82015-07-24 10:20:45 -0700135
136 for (int i = 0; i < numModes; ++i) {
Brian Osmand1e67e72017-03-15 12:19:37 -0400137 const char* label = SkBlendMode_Name(gModes[i]);
Cary Clark2a475ea2017-04-28 15:35:12 -0400138 canvas->drawString(label,
robertphillips29ccdf82015-07-24 10:20:45 -0700139 i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad),
140 textP);
141 }
142
143 for (int i = 0; i < numModes; ++i) {
halcanary9d524f22016-03-29 09:03:52 -0700144 canvas->save();
robertphillips29ccdf82015-07-24 10:20:45 -0700145 canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
146 SkIntToScalar(kTextPad+kPad));
147 // w/o a paint
brianosman95e8d0a2016-09-29 13:43:49 -0700148 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
Brian Osmand1e67e72017-03-15 12:19:37 -0400149 gModes[i], nullptr, nullptr);
robertphillips29ccdf82015-07-24 10:20:45 -0700150 canvas->translate(0.0f, numColors*(target.height()+kPad));
151 // w a paint
brianosman95e8d0a2016-09-29 13:43:49 -0700152 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
Brian Osmand1e67e72017-03-15 12:19:37 -0400153 gModes[i], nullptr, &paint);
halcanary9d524f22016-03-29 09:03:52 -0700154 canvas->restore();
robertphillips29ccdf82015-07-24 10:20:45 -0700155 }
156 }
halcanary9d524f22016-03-29 09:03:52 -0700157
robertphillips29ccdf82015-07-24 10:20:45 -0700158private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700159 static constexpr int kNumXferModes = 29;
160 static constexpr int kNumColors = 4;
161 static constexpr int kAtlasSize = 30;
162 static constexpr int kPad = 2;
163 static constexpr int kTextPad = 8;
robertphillips29ccdf82015-07-24 10:20:45 -0700164
robertphillips29ccdf82015-07-24 10:20:45 -0700165 typedef GM INHERITED;
166};
167DEF_GM( return new DrawAtlasColorsGM; )