blob: 096620d82ad0c5e61b08b5fc53166953639da219 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkImage.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkRSXform.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkScalar.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/core/SkTypeface.h"
24#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/ToolUtils.h"
robertphillips29ccdf82015-07-24 10:20:45 -070026
27// Create a square atlas of:
28// opaque white | opaque red
29// ------------------------------------
30// opaque green | transparent black
31//
reed9ce9d672016-03-17 10:51:11 -070032static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) {
robertphillips29ccdf82015-07-24 10:20:45 -070033 const int kBlockSize = atlasSize/2;
34
35 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
Mike Kleinea3f0142019-03-20 11:12:10 -050036 auto surface(ToolUtils::makeSurface(caller, info));
robertphillips29ccdf82015-07-24 10:20:45 -070037 SkCanvas* canvas = surface->getCanvas();
38
39 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070040 paint.setBlendMode(SkBlendMode::kSrc);
robertphillips29ccdf82015-07-24 10:20:45 -070041
42 paint.setColor(SK_ColorWHITE);
halcanary9d524f22016-03-29 09:03:52 -070043 SkRect r = SkRect::MakeXYWH(0, 0,
robertphillips29ccdf82015-07-24 10:20:45 -070044 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
45 canvas->drawRect(r, paint);
46
47 paint.setColor(SK_ColorRED);
halcanary9d524f22016-03-29 09:03:52 -070048 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0,
robertphillips29ccdf82015-07-24 10:20:45 -070049 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
50 canvas->drawRect(r, paint);
51
52 paint.setColor(SK_ColorGREEN);
halcanary9d524f22016-03-29 09:03:52 -070053 r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize),
robertphillips29ccdf82015-07-24 10:20:45 -070054 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
55 canvas->drawRect(r, paint);
56
57 paint.setColor(SK_ColorTRANSPARENT);
halcanary9d524f22016-03-29 09:03:52 -070058 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize),
robertphillips29ccdf82015-07-24 10:20:45 -070059 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
60 canvas->drawRect(r, paint);
61
reed9ce9d672016-03-17 10:51:11 -070062 return surface->makeImageSnapshot();
robertphillips29ccdf82015-07-24 10:20:45 -070063}
64
65// This GM tests the drawAtlas API with colors, different xfer modes
66// and transparency in the atlas image
67class DrawAtlasColorsGM : public skiagm::GM {
68public:
69 DrawAtlasColorsGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040070 this->setBGColor(0xFFCCCCCC);
robertphillips29ccdf82015-07-24 10:20:45 -070071 }
halcanary9d524f22016-03-29 09:03:52 -070072
robertphillips29ccdf82015-07-24 10:20:45 -070073protected:
74 SkString onShortName() override {
75 return SkString("draw-atlas-colors");
76 }
halcanary9d524f22016-03-29 09:03:52 -070077
robertphillips29ccdf82015-07-24 10:20:45 -070078 SkISize onISize() override {
79 return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad,
80 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad);
81 }
halcanary9d524f22016-03-29 09:03:52 -070082
robertphillips29ccdf82015-07-24 10:20:45 -070083 void onDraw(SkCanvas* canvas) override {
84 const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize));
85
brianosman95e8d0a2016-09-29 13:43:49 -070086 auto atlas = make_atlas(canvas, kAtlasSize);
robertphillips29ccdf82015-07-24 10:20:45 -070087
Brian Osmand1e67e72017-03-15 12:19:37 -040088 const SkBlendMode gModes[] = {
89 SkBlendMode::kClear,
90 SkBlendMode::kSrc,
91 SkBlendMode::kDst,
92 SkBlendMode::kSrcOver,
93 SkBlendMode::kDstOver,
94 SkBlendMode::kSrcIn,
95 SkBlendMode::kDstIn,
96 SkBlendMode::kSrcOut,
97 SkBlendMode::kDstOut,
98 SkBlendMode::kSrcATop,
99 SkBlendMode::kDstATop,
100 SkBlendMode::kXor,
101 SkBlendMode::kPlus,
102 SkBlendMode::kModulate,
103 SkBlendMode::kScreen,
104 SkBlendMode::kOverlay,
105 SkBlendMode::kDarken,
106 SkBlendMode::kLighten,
107 SkBlendMode::kColorDodge,
108 SkBlendMode::kColorBurn,
109 SkBlendMode::kHardLight,
110 SkBlendMode::kSoftLight,
111 SkBlendMode::kDifference,
112 SkBlendMode::kExclusion,
113 SkBlendMode::kMultiply,
114 SkBlendMode::kHue,
115 SkBlendMode::kSaturation,
116 SkBlendMode::kColor,
117 SkBlendMode::kLuminosity,
robertphillips29ccdf82015-07-24 10:20:45 -0700118 };
119
120 SkColor gColors[] = {
121 SK_ColorWHITE,
122 SK_ColorRED,
123 0x88888888, // transparent grey
124 0x88000088 // transparent blue
125 };
126
127 const int numModes = SK_ARRAY_COUNT(gModes);
128 SkASSERT(numModes == kNumXferModes);
129 const int numColors = SK_ARRAY_COUNT(gColors);
130 SkASSERT(numColors == kNumColors);
131 SkRSXform xforms[numColors];
132 SkRect rects[numColors];
133 SkColor quadColors[numColors];
134
135 SkPaint paint;
136 paint.setAntiAlias(true);
137
138 for (int i = 0; i < numColors; ++i) {
139 xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad));
140 rects[i] = target;
141 quadColors[i] = gColors[i];
142 }
143
Mike Kleinea3f0142019-03-20 11:12:10 -0500144 SkFont font(ToolUtils::create_portable_typeface(), kTextPad);
robertphillips29ccdf82015-07-24 10:20:45 -0700145
146 for (int i = 0; i < numModes; ++i) {
Brian Osmand1e67e72017-03-15 12:19:37 -0400147 const char* label = SkBlendMode_Name(gModes[i]);
Mike Reedc4745d62019-01-07 09:31:58 -0500148 canvas->drawString(label, i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad),
149 font, paint);
robertphillips29ccdf82015-07-24 10:20:45 -0700150 }
151
152 for (int i = 0; i < numModes; ++i) {
halcanary9d524f22016-03-29 09:03:52 -0700153 canvas->save();
robertphillips29ccdf82015-07-24 10:20:45 -0700154 canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
155 SkIntToScalar(kTextPad+kPad));
156 // w/o a paint
brianosman95e8d0a2016-09-29 13:43:49 -0700157 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
Brian Osmand1e67e72017-03-15 12:19:37 -0400158 gModes[i], nullptr, nullptr);
robertphillips29ccdf82015-07-24 10:20:45 -0700159 canvas->translate(0.0f, numColors*(target.height()+kPad));
160 // w a paint
brianosman95e8d0a2016-09-29 13:43:49 -0700161 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
Brian Osmand1e67e72017-03-15 12:19:37 -0400162 gModes[i], nullptr, &paint);
halcanary9d524f22016-03-29 09:03:52 -0700163 canvas->restore();
robertphillips29ccdf82015-07-24 10:20:45 -0700164 }
165 }
halcanary9d524f22016-03-29 09:03:52 -0700166
robertphillips29ccdf82015-07-24 10:20:45 -0700167private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700168 static constexpr int kNumXferModes = 29;
169 static constexpr int kNumColors = 4;
170 static constexpr int kAtlasSize = 30;
171 static constexpr int kPad = 2;
172 static constexpr int kTextPad = 8;
robertphillips29ccdf82015-07-24 10:20:45 -0700173
robertphillips29ccdf82015-07-24 10:20:45 -0700174 typedef GM INHERITED;
175};
176DEF_GM( return new DrawAtlasColorsGM; )