OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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" | |
9 | |
10 #include "SkSurface.h" | |
11 #include "SkGradientShader.h" | |
12 #include "SkPM4fPriv.h" | |
13 | |
14 static const int gRectSize = 50; | |
15 static const SkScalar gScalarSize = SkIntToScalar(gRectSize); | |
16 static const int gTestWidth = 700; | |
17 static const int gTestHeight = 300; | |
18 | |
19 struct CellRenderer { | |
20 virtual void draw(SkCanvas* canvas) = 0; | |
21 virtual const char* label() = 0; | |
22 virtual ~CellRenderer() {} | |
23 }; | |
24 | |
25 struct PaintColorCellRenderer : public CellRenderer { | |
26 PaintColorCellRenderer(SkColor color) : fColor(color) {} | |
27 void draw(SkCanvas* canvas) override { | |
28 canvas->drawColor(fColor); | |
29 } | |
30 const char* label() override { | |
31 return "Paint Color"; | |
32 } | |
33 protected: | |
34 SkColor fColor; | |
35 }; | |
36 | |
37 struct BitmapCellRenderer : public CellRenderer { | |
38 BitmapCellRenderer(SkColor color, SkFilterQuality quality) : fQuality(qualit y) { | |
39 fBitmap.allocPixels(SkImageInfo::MakeS32(gRectSize, gRectSize, kPremul_S kAlphaType)); | |
40 fBitmap.eraseColor(color); | |
41 const char* qualityNames[] = { "None", "Low", "Medium", "High" }; | |
42 fLabel = SkStringPrintf("Bitmap (%s)", qualityNames[quality]); | |
43 } | |
44 void draw(SkCanvas* canvas) override { | |
45 SkPaint paint; | |
46 paint.setFilterQuality(fQuality); | |
47 canvas->drawBitmap(fBitmap, 0, 0, &paint); | |
48 } | |
49 const char* label() override { | |
50 return fLabel.c_str(); | |
51 } | |
52 protected: | |
53 SkFilterQuality fQuality; | |
54 SkBitmap fBitmap; | |
55 SkString fLabel; | |
56 }; | |
57 | |
58 struct GradientCellRenderer : public CellRenderer { | |
59 GradientCellRenderer(SkColor colorOne, SkColor colorTwo) { | |
60 fColors[0] = colorOne; | |
61 fColors[1] = colorTwo; | |
62 } | |
63 void draw(SkCanvas* canvas) override { | |
64 SkPoint points[2] = { | |
65 SkPoint::Make(0, 0), | |
66 SkPoint::Make(0, gScalarSize) | |
67 }; | |
68 SkPaint paint; | |
69 paint.setShader(SkGradientShader::MakeLinear(points, fColors, nullptr, 2 , | |
70 SkShader::kClamp_TileMode)) ; | |
71 canvas->drawPaint(paint); | |
72 } | |
73 const char* label() override { | |
74 return "Linear Gradient"; | |
75 } | |
76 protected: | |
77 SkColor fColors[2]; | |
78 }; | |
79 | |
80 struct VerticesCellRenderer : public CellRenderer { | |
81 VerticesCellRenderer(SkColor colorOne, SkColor colorTwo) { | |
82 fColors[0] = fColors[1] = colorOne; | |
83 fColors[2] = fColors[3] = colorTwo; | |
84 } | |
85 void draw(SkCanvas* canvas) override { | |
86 SkPaint paint; | |
87 SkPoint vertices[4] = { | |
88 SkPoint::Make(0, 0), | |
89 SkPoint::Make(gScalarSize, 0), | |
90 SkPoint::Make(gScalarSize, gScalarSize), | |
91 SkPoint::Make(0, gScalarSize) | |
92 }; | |
93 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, vertices, nul lptr, fColors, | |
94 nullptr, nullptr, 0, paint); | |
95 } | |
96 const char* label() override { | |
97 return "Vertices"; | |
98 } | |
99 protected: | |
100 SkColor fColors[4]; | |
101 }; | |
102 | |
103 static void draw_gamut_grid(SkCanvas* canvas, SkTArray<CellRenderer*>& renderers ) { | |
104 // We want our colors in our wide gamut to be obviously visibly distorted fr om sRGB, so we use | |
105 // Wide Gamut RGB (with sRGB gamma, for HW acceleration) as the working spac e for this test: | |
106 const float gWideGamutRGB_toXYZD50[]{ | |
107 0.7161046f, 0.2581874f, 0.0000000f, // * R | |
108 0.1009296f, 0.7249378f, 0.0517813f, // * G | |
109 0.1471858f, 0.0168748f, 0.7734287f, // * B | |
110 }; | |
111 | |
112 SkMatrix44 wideGamutRGB_toXYZD50(SkMatrix44::kUninitialized_Constructor); | |
113 wideGamutRGB_toXYZD50.set3x3RowMajorf(gWideGamutRGB_toXYZD50); | |
114 | |
115 // Use the original canvas' color type, but account for gamma requirements | |
116 SkImageInfo origInfo = canvas->imageInfo(); | |
117 auto srgbCS = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); | |
118 auto wideCS = SkColorSpace::NewRGB(SkColorSpace::kSRGB_GammaNamed, wideGamut RGB_toXYZD50); | |
119 switch (origInfo.colorType()) { | |
120 case kRGBA_8888_SkColorType: | |
121 case kBGRA_8888_SkColorType: | |
122 break; | |
123 case kRGBA_F16_SkColorType: | |
124 srgbCS = srgbCS->makeLinearGamma(); | |
125 wideCS = wideCS->makeLinearGamma(); | |
126 break; | |
127 default: | |
128 return; | |
129 } | |
130 | |
131 // Make our two working surfaces (one sRGB, one Adobe) | |
132 SkImageInfo srgbGamutInfo = SkImageInfo::Make(gRectSize, gRectSize, origInfo .colorType(), | |
133 kPremul_SkAlphaType, srgbCS); | |
134 SkImageInfo wideGamutInfo = SkImageInfo::Make(gRectSize, gRectSize, origInfo .colorType(), | |
135 kPremul_SkAlphaType, wideCS); | |
136 // readPixels doesn't do color conversion (yet), so we can use it to see the raw (wide) data | |
msarett
2016/09/06 20:07:54
This is fine, but is there another hack to access
Brian Osman
2016/09/07 13:26:02
I don't think we have another way to get at this d
| |
137 SkImageInfo dstInfo = srgbGamutInfo.makeColorSpace(nullptr); | |
138 sk_sp<SkSurface> srgbGamutSurface = canvas->makeSurface(srgbGamutInfo); | |
139 sk_sp<SkSurface> wideGamutSurface = canvas->makeSurface(wideGamutInfo); | |
140 if (!srgbGamutSurface || !wideGamutSurface) { | |
141 return; | |
142 } | |
143 SkCanvas* srgbGamutCanvas = srgbGamutSurface->getCanvas(); | |
144 SkCanvas* wideGamutCanvas = wideGamutSurface->getCanvas(); | |
145 | |
146 SkPaint textPaint; | |
147 textPaint.setAntiAlias(true); | |
148 textPaint.setColor(SK_ColorWHITE); | |
149 sk_tool_utils::set_portable_typeface(&textPaint); | |
150 | |
151 SkScalar x = 0, y = 0; | |
152 SkScalar textHeight = textPaint.getFontSpacing(); | |
153 | |
154 for (auto renderer : renderers) { | |
155 srgbGamutCanvas->clear(SK_ColorBLACK); | |
156 renderer->draw(srgbGamutCanvas); | |
157 wideGamutCanvas->clear(SK_ColorBLACK); | |
158 renderer->draw(wideGamutCanvas); | |
159 | |
160 canvas->drawText(renderer->label(), strlen(renderer->label()), x, y + te xtHeight, | |
161 textPaint); | |
162 | |
163 SkBitmap srgbBitmap; | |
164 srgbBitmap.setInfo(dstInfo); | |
165 srgbGamutCanvas->readPixels(&srgbBitmap, 0, 0); | |
166 canvas->drawBitmap(srgbBitmap, x, y + textHeight + 5); | |
167 x += (gScalarSize + 1); | |
168 | |
169 SkBitmap wideBitmap; | |
170 wideBitmap.setInfo(dstInfo); | |
171 wideGamutCanvas->readPixels(&wideBitmap, 0, 0); | |
172 canvas->drawBitmap(wideBitmap, x, y + textHeight + 5); | |
173 x += (gScalarSize + 10); | |
174 | |
175 if (x + (2 * gScalarSize + 1) > gTestWidth) { | |
176 x = 0; | |
177 y += (textHeight + gScalarSize + 10); | |
178 } | |
179 } | |
180 } | |
181 | |
182 DEF_SIMPLE_GM_BG(gamut, canvas, gTestWidth, gTestHeight, SK_ColorBLACK) { | |
183 SkTArray<CellRenderer*> renderers; | |
184 | |
185 // sRGB primaries, rendered as paint color | |
186 renderers.push_back(new PaintColorCellRenderer(SK_ColorRED)); | |
187 renderers.push_back(new PaintColorCellRenderer(SK_ColorGREEN)); | |
188 | |
189 // sRGB primaries, rendered as bitmaps | |
190 renderers.push_back(new BitmapCellRenderer(SK_ColorRED, kNone_SkFilterQualit y)); | |
191 renderers.push_back(new BitmapCellRenderer(SK_ColorGREEN, kLow_SkFilterQuali ty)); | |
192 renderers.push_back(new BitmapCellRenderer(SK_ColorRED, kMedium_SkFilterQual ity)); | |
193 renderers.push_back(new BitmapCellRenderer(SK_ColorGREEN, kHigh_SkFilterQual ity)); | |
194 | |
195 // Various gradients involving sRGB primaries and white/black | |
196 renderers.push_back(new GradientCellRenderer(SK_ColorRED, SK_ColorGREEN)); | |
197 renderers.push_back(new GradientCellRenderer(SK_ColorGREEN, SK_ColorBLACK)); | |
198 renderers.push_back(new GradientCellRenderer(SK_ColorGREEN, SK_ColorWHITE)); | |
199 | |
200 // Vertex colors | |
201 renderers.push_back(new VerticesCellRenderer(SK_ColorRED, SK_ColorRED)); | |
202 renderers.push_back(new VerticesCellRenderer(SK_ColorRED, SK_ColorGREEN)); | |
203 | |
204 draw_gamut_grid(canvas, renderers); | |
205 | |
206 for (auto renderer : renderers) { | |
msarett
2016/09/06 20:07:54
Can we make these auto deletes or something?
Brian Osman
2016/09/07 13:26:02
Done.
| |
207 delete renderer; | |
208 } | |
209 } | |
OLD | NEW |