| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC |
| 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" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColorFilter.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPath.h" |
| 16 |
#include "include/core/SkPathBuilder.h" |
| 17 |
#include "include/core/SkRRect.h" |
| 18 |
#include "include/effects/SkGradient.h" |
| 19 |
#include "src/core/SkColorFilterPriv.h" |
| 20 |
#include "src/core/SkColorPriv.h" |
| 21 |
#include "tools/DecodeUtils.h" |
| 22 |
#include "tools/GpuToolUtils.h" |
| 23 |
#include "tools/Resources.h" |
| 24 |
#include "tools/ToolUtils.h" |
| 25 |
|
| 26 |
namespace { |
| 27 |
|
| 28 |
sk_sp<SkShader> create_gradient_shader(SkRect r, |
| 29 |
const std::array<SkColor, 3>& colors, |
| 30 |
const std::array<float, 3>& offsets) { |
| 31 |
SkPoint pts[2] = { {r.fLeft, r.fTop}, {r.fRight, r.fTop} }; |
| 32 |
SkColorConverter conv(colors); |
| 33 |
|
| 34 |
return SkShaders::LinearGradient(pts, {{conv.colors4f(), offsets, SkTileMode::kClamp}, {}}); |
| 35 |
} |
| 36 |
|
| 37 |
sk_sp<SkShader> create_image_shader(SkCanvas* destCanvas, SkTileMode tmX, SkTileMode tmY) { |
| 38 |
SkBitmap bitmap; |
| 39 |
|
| 40 |
{ |
| 41 |
SkImageInfo ii = SkImageInfo::Make(64, 64, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 42 |
bitmap.allocPixels(ii); |
| 43 |
bitmap.eraseColor(SK_ColorWHITE); |
| 44 |
|
| 45 |
SkCanvas tmpCanvas(bitmap); |
| 46 |
|
| 47 |
SkColor colors[3][3] = { |
| 48 |
{ SK_ColorRED, SK_ColorDKGRAY, SK_ColorBLUE }, |
| 49 |
{ SK_ColorLTGRAY, SK_ColorCYAN, SK_ColorYELLOW }, |
| 50 |
{ SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA } |
| 51 |
}; |
| 52 |
|
| 53 |
for (int y = 0; y < 3; ++y) { |
| 54 |
for (int x = 0; x < 3; ++x) { |
| 55 |
SkPaint paint; |
| 56 |
paint.setColor(colors[y][x]); |
| 57 |
tmpCanvas.drawRect(SkRect::MakeXYWH(x*21, y*21, 22, 22), paint); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
bitmap.setAlphaType(kOpaque_SkAlphaType); |
| 62 |
bitmap.setImmutable(); |
| 63 |
} |
| 64 |
|
| 65 |
sk_sp<SkImage> img = SkImages::RasterFromBitmap(bitmap); |
| 66 |
img = ToolUtils::MakeTextureImage(destCanvas, std::move(img)); |
| 67 |
if (img) { |
| 68 |
return img->makeShader(tmX, tmY, SkSamplingOptions()); |
| 69 |
} else { |
| 70 |
return nullptr; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
sk_sp<SkShader> create_blend_shader(SkCanvas* destCanvas, SkBlendMode bm) { |
| 75 |
constexpr SkColor4f kTransYellow = {1.0f, 1.0f, 0.0f, 0.5f}; |
| 76 |
|
| 77 |
sk_sp<SkShader> dst = SkShaders::Color(kTransYellow, nullptr); |
| 78 |
return SkShaders::Blend(bm, |
| 79 |
std::move(dst), |
| 80 |
create_image_shader(destCanvas, |
| 81 |
SkTileMode::kRepeat, SkTileMode::kRepeat)); |
| 82 |
} |
| 83 |
|
| 84 |
sk_sp<SkColorFilter> create_grayscale_colorfilter() { |
| 85 |
float matrix[20] = {}; |
| 86 |
matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 87 |
matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 88 |
matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 89 |
matrix[18] = 1.0f; |
| 90 |
return SkColorFilters::Matrix(matrix); |
| 91 |
} |
| 92 |
|
| 93 |
void draw_image_shader_tile(SkCanvas* canvas, SkRect clipRect) { |
| 94 |
SkPaint p; |
| 95 |
p.setShader(create_image_shader(canvas, SkTileMode::kClamp, SkTileMode::kRepeat)); |
| 96 |
|
| 97 |
SkPath path = SkPathBuilder() |
| 98 |
.moveTo(1, 1) |
| 99 |
.lineTo(32, 127) |
| 100 |
.lineTo(96, 127) |
| 101 |
.lineTo(127, 1) |
| 102 |
.lineTo(63, 32) |
| 103 |
.close() |
| 104 |
.detach(); |
| 105 |
|
| 106 |
canvas->save(); |
| 107 |
canvas->clipRect(clipRect); |
| 108 |
canvas->scale(0.5f, 0.5f); |
| 109 |
canvas->drawPath(path, p); |
| 110 |
|
| 111 |
canvas->save(); |
| 112 |
canvas->concat(SkMatrix::RotateDeg(90, {64, 64})); |
| 113 |
canvas->translate(128, 0); |
| 114 |
canvas->drawPath(path, p); |
| 115 |
canvas->restore(); |
| 116 |
canvas->restore(); |
| 117 |
} |
| 118 |
|
| 119 |
void draw_gradient_tile(SkCanvas* canvas, SkRect clipRect) { |
| 120 |
SkRect r{1, 1, 127, 127}; |
| 121 |
SkPaint p; |
| 122 |
p.setShader(create_gradient_shader(r, |
| 123 |
{ SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }, |
| 124 |
{ 0.0f, 0.75f, 1.0f })); |
| 125 |
|
| 126 |
canvas->save(); |
| 127 |
canvas->clipRect(clipRect); |
| 128 |
canvas->translate(128, 0); |
| 129 |
canvas->scale(0.5f, 0.5f); |
| 130 |
canvas->drawRect(r, p); |
| 131 |
|
| 132 |
canvas->save(); |
| 133 |
canvas->concat(SkMatrix::RotateDeg(90, {64, 64})); |
| 134 |
canvas->translate(128, 0); |
| 135 |
canvas->drawRect(r, p); |
| 136 |
canvas->restore(); |
| 137 |
canvas->restore(); |
| 138 |
} |
| 139 |
|
| 140 |
void draw_colorfilter_swatches(SkCanvas* canvas, SkRect clipRect) { |
| 141 |
static constexpr int kNumTilesPerSide = 3; |
| 142 |
|
| 143 |
SkSize tileSize = { clipRect.width() / kNumTilesPerSide, clipRect.height() / kNumTilesPerSide }; |
| 144 |
|
| 145 |
// Quantize to four colors |
| 146 |
uint8_t table1[256]; |
| 147 |
for (int i = 0; i < 256; ++i) { |
| 148 |
table1[i] = (i/64) * 85; |
| 149 |
} |
| 150 |
|
| 151 |
// table2 is a band-pass filter for 85-170. |
| 152 |
// table3 re-expands that range to 0..255 |
| 153 |
uint8_t table2[256], table3[256]; |
| 154 |
for (int i = 0; i < 256; ++i) { |
| 155 |
if (i >= 85 && i <= 170) { |
| 156 |
table2[i] = i; |
| 157 |
table3[i] = ((i - 85) / 85.0f) * 255.0f; |
| 158 |
} else { |
| 159 |
table2[i] = 0; |
| 160 |
table3[i] = 0; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
constexpr SkColor SK_ColorGREY = SkColorSetARGB(0xFF, 0x80, 0x80, 0x80); |
| 165 |
|
| 166 |
sk_sp<SkColorFilter> colorFilters[kNumTilesPerSide*kNumTilesPerSide]; |
| 167 |
static const std::array<SkColor, 3> kGradientColors[kNumTilesPerSide*kNumTilesPerSide] = { |
| 168 |
{ SK_ColorBLACK, SK_ColorGREY, SK_ColorWHITE }, |
| 169 |
{ SK_ColorBLACK, SK_ColorGREY, SK_ColorWHITE }, |
| 170 |
{ SK_ColorBLACK, SK_ColorGREY, SK_ColorWHITE }, |
| 171 |
{ SK_ColorBLACK, SK_ColorGREY, SK_ColorWHITE }, |
| 172 |
{ 0x00000000, 0x80000000, 0xFF000000 }, // the Gaussian CF uses alpha only |
| 173 |
{ SK_ColorBLACK, SK_ColorGREY, SK_ColorWHITE }, |
| 174 |
{ SK_ColorBLACK, SK_ColorGREY, SK_ColorWHITE }, |
| 175 |
{ SK_ColorBLACK, SK_ColorGREY, SK_ColorWHITE }, |
| 176 |
{ SK_ColorBLACK, SK_ColorGREY, SK_ColorWHITE }, |
| 177 |
}; |
| 178 |
|
| 179 |
colorFilters[0] = SkColorFilters::Lighting(SK_ColorLTGRAY, 0xFF440000); |
| 180 |
colorFilters[1] = SkColorFilters::Table(table1); |
| 181 |
colorFilters[2] = SkColorFilters::Compose(SkColorFilters::TableARGB(nullptr, table3, |
| 182 |
table3, table3), |
| 183 |
SkColorFilters::TableARGB(nullptr, table2, |
| 184 |
table2, table2)); |
| 185 |
colorFilters[3] = SkColorFilters::Blend(SK_ColorGREEN, SkBlendMode::kMultiply); |
| 186 |
colorFilters[4] = SkColorFilterPriv::MakeGaussian(); |
| 187 |
|
| 188 |
colorFilters[5] = SkColorFilters::LinearToSRGBGamma(); |
| 189 |
colorFilters[6] = SkColorFilters::SRGBToLinearGamma(); |
| 190 |
|
| 191 |
SkPaint p; |
| 192 |
|
| 193 |
canvas->save(); |
| 194 |
canvas->clipRect(clipRect); |
| 195 |
canvas->translate(clipRect.fLeft, clipRect.fTop); |
| 196 |
|
| 197 |
for (int y = 0; y < kNumTilesPerSide; ++y) { |
| 198 |
for (int x = 0; x < kNumTilesPerSide; ++x) { |
| 199 |
SkRect r = SkRect::MakeXYWH(x * tileSize.width(), y * tileSize.height(), |
| 200 |
tileSize.width(), tileSize.height()).makeInset(1.0f, |
| 201 |
1.0f); |
| 202 |
int colorFilterIndex = x*kNumTilesPerSide+y; |
| 203 |
p.setShader(create_gradient_shader(r, |
| 204 |
kGradientColors[colorFilterIndex], |
| 205 |
{ 0.0f, 0.5f, 1.0f })); |
| 206 |
p.setColorFilter(colorFilters[colorFilterIndex]); |
| 207 |
canvas->drawRect(r, p); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
canvas->restore(); |
| 212 |
} |
| 213 |
|
| 214 |
void draw_blend_mode_swatches(SkCanvas* canvas, SkRect clipRect) { |
| 215 |
static const int kTileHeight = 16; |
| 216 |
static const int kTileWidth = 16; |
| 217 |
static const SkColor4f kOpaqueWhite { 1.0f, 1.0f, 1.0f, 1.0f }; |
| 218 |
static const SkColor4f kTransBluish { 0.0f, 0.5f, 1.0f, 0.5f }; |
| 219 |
static const SkColor4f kTransWhite { 1.0f, 1.0f, 1.0f, 0.75f }; |
| 220 |
|
| 221 |
SkPaint dstPaint; |
| 222 |
dstPaint.setColor(kOpaqueWhite); |
| 223 |
dstPaint.setBlendMode(SkBlendMode::kSrc); |
| 224 |
dstPaint.setAntiAlias(false); |
| 225 |
|
| 226 |
SkPaint srcPaint; |
| 227 |
srcPaint.setColor(kTransBluish); |
| 228 |
srcPaint.setAntiAlias(false); |
| 229 |
|
| 230 |
SkRect r = SkRect::MakeXYWH(clipRect.fLeft, clipRect.fTop, kTileWidth, kTileHeight); |
| 231 |
|
| 232 |
// For the first pass we draw: transparent bluish on top of opaque white |
| 233 |
// For the second pass we draw: transparent white on top of transparent bluish |
| 234 |
for (int passes = 0; passes < 2; ++passes) { |
| 235 |
for (int i = 0; i <= (int)SkBlendMode::kLastCoeffMode; ++i) { |
| 236 |
if (r.fLeft+kTileWidth > clipRect.fRight) { |
| 237 |
r.offsetTo(clipRect.fLeft, r.fTop+kTileHeight); |
| 238 |
} |
| 239 |
|
| 240 |
canvas->drawRect(r.makeInset(1.0f, 1.0f), dstPaint); |
| 241 |
srcPaint.setBlendMode(static_cast<SkBlendMode>(i)); |
| 242 |
canvas->drawRect(r.makeInset(2.0f, 2.0f), srcPaint); |
| 243 |
|
| 244 |
r.offset(kTileWidth, 0.0f); |
| 245 |
} |
| 246 |
|
| 247 |
r.offsetTo(clipRect.fLeft, r.fTop+kTileHeight); |
| 248 |
srcPaint.setColor(kTransWhite); |
| 249 |
dstPaint.setColor(kTransBluish); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
} // anonymous namespace |
| 254 |
|
| 255 |
namespace skiagm { |
| 256 |
|
| 257 |
// This is just for bootstrapping Graphite. |
| 258 |
class GraphiteStartGM : public GM { |
| 259 |
public: |
| 260 |
GraphiteStartGM() = default; |
| 261 |
|
| 262 |
protected: |
| 263 |
static constexpr int kTileWidth = 128; |
| 264 |
static constexpr int kTileHeight = 128; |
| 265 |
static constexpr int kWidth = 3 * kTileWidth; |
| 266 |
static constexpr int kHeight = 3 * kTileHeight; |
| 267 |
static constexpr int kClipInset = 4; |
| 268 |
|
| 269 |
void onOnceBeforeDraw() override { |
| 270 |
this->setBGColor(SK_ColorBLACK); |
| 271 |
ToolUtils::GetResourceAsBitmap("images/color_wheel.gif", &fBitmap); |
| 272 |
} |
| 273 |
|
| 274 |
SkString getName() const override { return SkString("graphitestart"); } |
| 275 |
|
| 276 |
SkISize getISize() override { return SkISize::Make(kWidth, kHeight); } |
| 277 |
|
| 278 |
void onDraw(SkCanvas* canvas) override { |
| 279 |
|
| 280 |
const SkRect clipRect = SkRect::MakeWH(kWidth, kHeight).makeInset(kClipInset, kClipInset); |
| 281 |
|
| 282 |
canvas->save(); |
| 283 |
canvas->clipRRect(SkRRect::MakeRectXY(clipRect, 32.f, 32.f), true); |
| 284 |
|
| 285 |
// Upper-left corner |
| 286 |
draw_image_shader_tile(canvas, SkRect::MakeXYWH(0, 0, kTileWidth, kTileHeight)); |
| 287 |
|
| 288 |
// Upper-middle tile |
| 289 |
draw_gradient_tile(canvas, SkRect::MakeXYWH(kTileWidth, 0, kTileWidth, kTileHeight)); |
| 290 |
|
| 291 |
// Upper-right corner |
| 292 |
draw_colorfilter_swatches(canvas, SkRect::MakeXYWH(2*kTileWidth, 0, |
| 293 |
kTileWidth, kTileWidth)); |
| 294 |
|
| 295 |
// Middle-left tile |
| 296 |
{ |
| 297 |
SkPaint p; |
| 298 |
p.setColor(SK_ColorRED); |
| 299 |
|
| 300 |
SkRect r = SkRect::MakeXYWH(0, kTileHeight, kTileWidth, kTileHeight); |
| 301 |
canvas->drawRect(r.makeInset(1.0f, 1.0f), p); |
| 302 |
} |
| 303 |
|
| 304 |
// Middle-middle tile |
| 305 |
{ |
| 306 |
SkPaint p; |
| 307 |
p.setShader(create_blend_shader(canvas, SkBlendMode::kModulate)); |
| 308 |
|
| 309 |
SkRect r = SkRect::MakeXYWH(kTileWidth, kTileHeight, kTileWidth, kTileHeight); |
| 310 |
canvas->drawRect(r.makeInset(1.0f, 1.0f), p); |
| 311 |
} |
| 312 |
|
| 313 |
// Middle-right tile |
| 314 |
{ |
| 315 |
sk_sp<SkImage> image(ToolUtils::GetResourceAsImage("images/mandrill_128.png")); |
| 316 |
sk_sp<SkShader> shader; |
| 317 |
|
| 318 |
if (image) { |
| 319 |
shader = image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, {}); |
| 320 |
shader = shader->makeWithColorFilter(create_grayscale_colorfilter()); |
| 321 |
} |
| 322 |
|
| 323 |
SkPaint p; |
| 324 |
p.setShader(std::move(shader)); |
| 325 |
|
| 326 |
SkRect r = SkRect::MakeXYWH(2*kTileWidth, kTileHeight, kTileWidth, kTileHeight); |
| 327 |
canvas->drawRect(r.makeInset(1.0f, 1.0f), p); |
| 328 |
} |
| 329 |
|
| 330 |
canvas->restore(); |
| 331 |
|
| 332 |
// Bottom-left corner |
| 333 |
#if defined(SK_GRAPHITE) |
| 334 |
// TODO: failing serialize test on Linux, not sure what's going on |
| 335 |
canvas->writePixels(fBitmap, 0, 2*kTileHeight); |
| 336 |
#endif |
| 337 |
|
| 338 |
// Bottom-middle tile |
| 339 |
draw_blend_mode_swatches(canvas, SkRect::MakeXYWH(kTileWidth, 2*kTileHeight, |
| 340 |
kTileWidth, kTileHeight)); |
| 341 |
|
| 342 |
// Bottom-right corner |
| 343 |
{ |
| 344 |
const SkRect kTile = SkRect::MakeXYWH(2*kTileWidth, 2*kTileHeight, |
| 345 |
kTileWidth, kTileHeight); |
| 346 |
|
| 347 |
SkPaint circlePaint; |
| 348 |
circlePaint.setColor(SK_ColorBLUE); |
| 349 |
circlePaint.setBlendMode(SkBlendMode::kSrc); |
| 350 |
|
| 351 |
canvas->clipRect(kTile); |
| 352 |
canvas->drawRect(kTile.makeInset(10, 20), circlePaint); |
| 353 |
|
| 354 |
SkPaint restorePaint; |
| 355 |
restorePaint.setBlendMode(SkBlendMode::kPlus); |
| 356 |
|
| 357 |
canvas->saveLayer(nullptr, &restorePaint); |
| 358 |
circlePaint.setColor(SK_ColorRED); |
| 359 |
circlePaint.setBlendMode(SkBlendMode::kSrc); |
| 360 |
|
| 361 |
canvas->drawRect(kTile.makeInset(15, 25), circlePaint); |
| 362 |
canvas->restore(); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
private: |
| 367 |
SkBitmap fBitmap; |
| 368 |
}; |
| 369 |
|
| 370 |
////////////////////////////////////////////////////////////////////////////// |
| 371 |
|
| 372 |
DEF_GM(return new GraphiteStartGM;) |
| 373 |
|
| 374 |
} // namespace skiagm |