blob: 1250cbfa31aaafc2f20069eac8cfb0b587dd99f7 [file] [log] [blame]
brianosman2c4b64e2016-09-07 07:04:44 -07001/*
2 * Copyright 2016 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"
brianosman2c4b64e2016-09-07 07:04:44 -070010
brianosman2c4b64e2016-09-07 07:04:44 -070011#include "SkGradientShader.h"
Brian Osman0d4ff6c2017-01-17 16:10:07 -050012#include "SkImagePriv.h"
raftias7c602de2016-10-13 10:45:44 -070013#include "SkSurface.h"
Mike Reed887cdf12017-04-03 11:11:09 -040014#include "SkVertices.h"
brianosman2c4b64e2016-09-07 07:04:44 -070015
16static const int gRectSize = 50;
17static const SkScalar gScalarSize = SkIntToScalar(gRectSize);
18static const int gTestWidth = 700;
19static const int gTestHeight = 300;
20
21struct CellRenderer {
22 virtual void draw(SkCanvas* canvas) = 0;
23 virtual const char* label() = 0;
24 virtual ~CellRenderer() {}
25};
26
27struct PaintColorCellRenderer : public CellRenderer {
28 PaintColorCellRenderer(SkColor color) : fColor(color) {}
29 void draw(SkCanvas* canvas) override {
30 canvas->drawColor(fColor);
31 }
32 const char* label() override {
33 return "Paint Color";
34 }
35protected:
36 SkColor fColor;
37};
38
39struct BitmapCellRenderer : public CellRenderer {
40 BitmapCellRenderer(SkColor color, SkFilterQuality quality, float scale = 1.0f)
41 : fQuality(quality) {
Mike Reed35ee0e02017-08-06 22:29:57 -040042 int scaledSize = sk_float_round2int(scale * gRectSize);
brianosman2c4b64e2016-09-07 07:04:44 -070043 fBitmap.allocPixels(SkImageInfo::MakeS32(scaledSize, scaledSize, kPremul_SkAlphaType));
44 fBitmap.eraseColor(color);
Greg Daniela4ead652018-02-07 10:21:48 -050045 fBitmap.setImmutable();
brianosman2c4b64e2016-09-07 07:04:44 -070046 const char* qualityNames[] = { "None", "Low", "Medium", "High" };
47 fLabel = SkStringPrintf("Bitmap (%s)", qualityNames[quality]);
48 }
49 void draw(SkCanvas* canvas) override {
50 SkPaint paint;
51 paint.setFilterQuality(fQuality);
52 canvas->drawBitmapRect(fBitmap, SkRect::MakeIWH(gRectSize, gRectSize), &paint);
53 }
54 const char* label() override {
55 return fLabel.c_str();
56 }
57protected:
58 SkFilterQuality fQuality;
59 SkBitmap fBitmap;
60 SkString fLabel;
61};
62
63struct GradientCellRenderer : public CellRenderer {
brianosmand4546092016-09-22 12:31:58 -070064 GradientCellRenderer(SkColor colorOne, SkColor colorTwo, bool manyStops) {
brianosman2c4b64e2016-09-07 07:04:44 -070065 fColors[0] = colorOne;
66 fColors[1] = colorTwo;
brianosmand4546092016-09-22 12:31:58 -070067 fManyStops = manyStops;
brianosman2c4b64e2016-09-07 07:04:44 -070068 }
69 void draw(SkCanvas* canvas) override {
70 SkPoint points[2] = {
71 SkPoint::Make(0, 0),
72 SkPoint::Make(0, gScalarSize)
73 };
74 SkPaint paint;
brianosmand4546092016-09-22 12:31:58 -070075 if (fManyStops) {
76 SkColor colors[4] ={
77 fColors[0], fColors[0], fColors[1], fColors[1]
78 };
79 paint.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 4,
80 SkShader::kClamp_TileMode));
81 } else {
82 paint.setShader(SkGradientShader::MakeLinear(points, fColors, nullptr, 2,
83 SkShader::kClamp_TileMode));
84 }
brianosman2c4b64e2016-09-07 07:04:44 -070085 canvas->drawPaint(paint);
86 }
87 const char* label() override {
88 return "Linear Gradient";
89 }
90protected:
91 SkColor fColors[2];
brianosmand4546092016-09-22 12:31:58 -070092 bool fManyStops;
brianosman2c4b64e2016-09-07 07:04:44 -070093};
94
95struct VerticesCellRenderer : public CellRenderer {
96 VerticesCellRenderer(SkColor colorOne, SkColor colorTwo) {
97 fColors[0] = fColors[1] = colorOne;
98 fColors[2] = fColors[3] = colorTwo;
99 }
100 void draw(SkCanvas* canvas) override {
101 SkPaint paint;
102 SkPoint vertices[4] = {
103 SkPoint::Make(0, 0),
104 SkPoint::Make(gScalarSize, 0),
105 SkPoint::Make(gScalarSize, gScalarSize),
106 SkPoint::Make(0, gScalarSize)
107 };
Mike Reed887cdf12017-04-03 11:11:09 -0400108 canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, vertices,
109 nullptr, fColors),
110 SkBlendMode::kModulate, paint);
brianosman2c4b64e2016-09-07 07:04:44 -0700111 }
112 const char* label() override {
113 return "Vertices";
114 }
115protected:
116 SkColor fColors[4];
117};
118
Ben Wagner145dbcd2016-11-03 14:40:50 -0400119static void draw_gamut_grid(SkCanvas* canvas, SkTArray<std::unique_ptr<CellRenderer>>& renderers) {
brianosman2c4b64e2016-09-07 07:04:44 -0700120 // We want our colors in our wide gamut to be obviously visibly distorted from sRGB, so we use
121 // Wide Gamut RGB (with sRGB gamma, for HW acceleration) as the working space for this test:
122 const float gWideGamutRGB_toXYZD50[]{
brianosmande68d6c2016-09-09 10:36:17 -0700123 0.7161046f, 0.1009296f, 0.1471858f, // -> X
124 0.2581874f, 0.7249378f, 0.0168748f, // -> Y
125 0.0000000f, 0.0517813f, 0.7734287f, // -> Z
brianosman2c4b64e2016-09-07 07:04:44 -0700126 };
127
Mike Klein4429a4f2018-10-04 09:06:00 -0400128 SkMatrix44 wideGamutRGB_toXYZD50;
brianosman2c4b64e2016-09-07 07:04:44 -0700129 wideGamutRGB_toXYZD50.set3x3RowMajorf(gWideGamutRGB_toXYZD50);
130
131 // Use the original canvas' color type, but account for gamma requirements
fmalitaed387722016-09-30 06:14:49 -0700132 SkImageInfo origInfo = canvas->imageInfo();
raftias94888332016-10-18 10:02:51 -0700133 sk_sp<SkColorSpace> srgbCS;
134 sk_sp<SkColorSpace> wideCS;
fmalitaed387722016-09-30 06:14:49 -0700135 switch (origInfo.colorType()) {
brianosman2c4b64e2016-09-07 07:04:44 -0700136 case kRGBA_8888_SkColorType:
137 case kBGRA_8888_SkColorType:
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500138 srgbCS = SkColorSpace::MakeSRGB();
Brian Osman526972e2016-10-24 09:24:02 -0400139 wideCS = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
raftias94888332016-10-18 10:02:51 -0700140 wideGamutRGB_toXYZD50);
brianosman2c4b64e2016-09-07 07:04:44 -0700141 break;
142 case kRGBA_F16_SkColorType:
Mike Klein37854712018-06-26 11:43:06 -0400143 case kRGBA_F32_SkColorType:
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500144 srgbCS = SkColorSpace::MakeSRGBLinear();
Brian Osman526972e2016-10-24 09:24:02 -0400145 wideCS = SkColorSpace::MakeRGB(SkColorSpace::kLinear_RenderTargetGamma,
raftias94888332016-10-18 10:02:51 -0700146 wideGamutRGB_toXYZD50);
brianosman2c4b64e2016-09-07 07:04:44 -0700147 break;
148 default:
149 return;
150 }
raftias94888332016-10-18 10:02:51 -0700151 SkASSERT(srgbCS);
152 SkASSERT(wideCS);
brianosman2c4b64e2016-09-07 07:04:44 -0700153
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500154 // Make our two working surfaces (one sRGB, one Wide)
fmalitaed387722016-09-30 06:14:49 -0700155 SkImageInfo srgbGamutInfo = SkImageInfo::Make(gRectSize, gRectSize, origInfo.colorType(),
brianosman2c4b64e2016-09-07 07:04:44 -0700156 kPremul_SkAlphaType, srgbCS);
fmalitaed387722016-09-30 06:14:49 -0700157 SkImageInfo wideGamutInfo = SkImageInfo::Make(gRectSize, gRectSize, origInfo.colorType(),
brianosman2c4b64e2016-09-07 07:04:44 -0700158 kPremul_SkAlphaType, wideCS);
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500159
brianosman2c4b64e2016-09-07 07:04:44 -0700160 sk_sp<SkSurface> srgbGamutSurface = canvas->makeSurface(srgbGamutInfo);
161 sk_sp<SkSurface> wideGamutSurface = canvas->makeSurface(wideGamutInfo);
fmalitaed387722016-09-30 06:14:49 -0700162 if (!srgbGamutSurface || !wideGamutSurface) {
163 return;
brianosman2c4b64e2016-09-07 07:04:44 -0700164 }
165 SkCanvas* srgbGamutCanvas = srgbGamutSurface->getCanvas();
166 SkCanvas* wideGamutCanvas = wideGamutSurface->getCanvas();
167
168 SkPaint textPaint;
169 textPaint.setAntiAlias(true);
170 textPaint.setColor(SK_ColorWHITE);
171 sk_tool_utils::set_portable_typeface(&textPaint);
172
173 SkScalar x = 0, y = 0;
174 SkScalar textHeight = textPaint.getFontSpacing();
175
176 for (const auto& renderer : renderers) {
177 srgbGamutCanvas->clear(SK_ColorBLACK);
178 renderer->draw(srgbGamutCanvas);
179 wideGamutCanvas->clear(SK_ColorBLACK);
180 renderer->draw(wideGamutCanvas);
181
Cary Clark2a475ea2017-04-28 15:35:12 -0400182 canvas->drawString(renderer->label(), x, y + textHeight, textPaint);
brianosman2c4b64e2016-09-07 07:04:44 -0700183
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500184 // Re-interpret the off-screen images, so we can see the raw data (eg, Wide gamut squares
185 // will look desaturated, relative to sRGB).
186 auto srgbImage = srgbGamutSurface->makeImageSnapshot();
187 srgbImage = SkImageMakeRasterCopyAndAssignColorSpace(srgbImage.get(),
188 origInfo.colorSpace());
189 canvas->drawImage(srgbImage, x, y + textHeight + 5);
brianosman2c4b64e2016-09-07 07:04:44 -0700190 x += (gScalarSize + 1);
191
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500192 auto wideImage = wideGamutSurface->makeImageSnapshot();
193 wideImage = SkImageMakeRasterCopyAndAssignColorSpace(wideImage.get(),
194 origInfo.colorSpace());
195 canvas->drawImage(wideImage, x, y + textHeight + 5);
brianosman2c4b64e2016-09-07 07:04:44 -0700196 x += (gScalarSize + 10);
197
198 if (x + (2 * gScalarSize + 1) > gTestWidth) {
199 x = 0;
200 y += (textHeight + gScalarSize + 10);
201 }
202 }
203}
204
205DEF_SIMPLE_GM_BG(gamut, canvas, gTestWidth, gTestHeight, SK_ColorBLACK) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400206 SkTArray<std::unique_ptr<CellRenderer>> renderers;
brianosman2c4b64e2016-09-07 07:04:44 -0700207
208 // sRGB primaries, rendered as paint color
Ben Wagner145dbcd2016-11-03 14:40:50 -0400209 renderers.emplace_back(new PaintColorCellRenderer(SK_ColorRED));
210 renderers.emplace_back(new PaintColorCellRenderer(SK_ColorGREEN));
brianosman2c4b64e2016-09-07 07:04:44 -0700211
212 // sRGB primaries, rendered as bitmaps
Ben Wagner145dbcd2016-11-03 14:40:50 -0400213 renderers.emplace_back(new BitmapCellRenderer(SK_ColorRED, kNone_SkFilterQuality));
214 renderers.emplace_back(new BitmapCellRenderer(SK_ColorGREEN, kLow_SkFilterQuality));
brianosman2c4b64e2016-09-07 07:04:44 -0700215 // Larger bitmap to trigger mipmaps
Ben Wagner145dbcd2016-11-03 14:40:50 -0400216 renderers.emplace_back(new BitmapCellRenderer(SK_ColorRED, kMedium_SkFilterQuality, 2.0f));
brianosman2c4b64e2016-09-07 07:04:44 -0700217 // Smaller bitmap to trigger bicubic
Ben Wagner145dbcd2016-11-03 14:40:50 -0400218 renderers.emplace_back(new BitmapCellRenderer(SK_ColorGREEN, kHigh_SkFilterQuality, 0.5f));
brianosman2c4b64e2016-09-07 07:04:44 -0700219
220 // Various gradients involving sRGB primaries and white/black
brianosmand4546092016-09-22 12:31:58 -0700221
222 // First with just two stops (implemented with uniforms on GPU)
Ben Wagner145dbcd2016-11-03 14:40:50 -0400223 renderers.emplace_back(new GradientCellRenderer(SK_ColorRED, SK_ColorGREEN, false));
224 renderers.emplace_back(new GradientCellRenderer(SK_ColorGREEN, SK_ColorBLACK, false));
225 renderers.emplace_back(new GradientCellRenderer(SK_ColorGREEN, SK_ColorWHITE, false));
brianosmand4546092016-09-22 12:31:58 -0700226
227 // ... and then with four stops (implemented with textures on GPU)
Ben Wagner145dbcd2016-11-03 14:40:50 -0400228 renderers.emplace_back(new GradientCellRenderer(SK_ColorRED, SK_ColorGREEN, true));
229 renderers.emplace_back(new GradientCellRenderer(SK_ColorGREEN, SK_ColorBLACK, true));
230 renderers.emplace_back(new GradientCellRenderer(SK_ColorGREEN, SK_ColorWHITE, true));
brianosman2c4b64e2016-09-07 07:04:44 -0700231
232 // Vertex colors
Ben Wagner145dbcd2016-11-03 14:40:50 -0400233 renderers.emplace_back(new VerticesCellRenderer(SK_ColorRED, SK_ColorRED));
234 renderers.emplace_back(new VerticesCellRenderer(SK_ColorRED, SK_ColorGREEN));
brianosman2c4b64e2016-09-07 07:04:44 -0700235
236 draw_gamut_grid(canvas, renderers);
237}