| 1 |
/* |
| 2 |
* Copyright 2014 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/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkColorFilter.h" |
| 14 |
#include "include/core/SkFont.h" |
| 15 |
#include "include/core/SkMaskFilter.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkScalar.h" |
| 18 |
#include "include/core/SkShader.h" |
| 19 |
#include "include/core/SkSize.h" |
| 20 |
#include "include/core/SkString.h" |
| 21 |
#include "include/core/SkSurface.h" |
| 22 |
#include "include/core/SkTypeface.h" |
| 23 |
#include "include/utils/SkTextUtils.h" |
| 24 |
#include "src/core/SkBlurMask.h" |
| 25 |
#include "src/effects/SkEmbossMaskFilter.h" |
| 26 |
#include "tools/fonts/FontToolUtils.h" |
| 27 |
|
| 28 |
static sk_sp<SkImage> make_bm() { |
| 29 |
auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100)); |
| 30 |
|
| 31 |
SkPaint paint; |
| 32 |
paint.setAntiAlias(true); |
| 33 |
surf->getCanvas()->drawCircle(50, 50, 50, paint); |
| 34 |
return surf->makeImageSnapshot(); |
| 35 |
} |
| 36 |
|
| 37 |
class EmbossGM : public skiagm::GM { |
| 38 |
public: |
| 39 |
EmbossGM() { |
| 40 |
} |
| 41 |
|
| 42 |
protected: |
| 43 |
SkString getName() const override { return SkString("emboss"); } |
| 44 |
|
| 45 |
SkISize getISize() override { return SkISize::Make(600, 120); } |
| 46 |
|
| 47 |
void onDraw(SkCanvas* canvas) override { |
| 48 |
SkPaint paint; |
| 49 |
auto img = make_bm(); |
| 50 |
canvas->drawImage(img, 10, 10); |
| 51 |
canvas->translate(img->width() + SkIntToScalar(10), 0); |
| 52 |
|
| 53 |
paint.setMaskFilter(SkEmbossMaskFilter::Make( |
| 54 |
SkBlurMask::ConvertRadiusToSigma(3), |
| 55 |
{ { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 })); |
| 56 |
canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint); |
| 57 |
canvas->translate(img->width() + SkIntToScalar(10), 0); |
| 58 |
|
| 59 |
// this combination of emboss+colorfilter used to crash -- so we exercise it to |
| 60 |
// confirm that we have a fix. |
| 61 |
paint.setColorFilter(SkColorFilters::Blend(0xFFFF0000, SkBlendMode::kSrcATop)); |
| 62 |
canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint); |
| 63 |
canvas->translate(img->width() + SkIntToScalar(10), 0); |
| 64 |
|
| 65 |
paint.setAntiAlias(true); |
| 66 |
paint.setStyle(SkPaint::kStroke_Style); |
| 67 |
paint.setStrokeWidth(SkIntToScalar(10)); |
| 68 |
paint.setMaskFilter(SkEmbossMaskFilter::Make( |
| 69 |
SkBlurMask::ConvertRadiusToSigma(4), |
| 70 |
{ { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 })); |
| 71 |
paint.setColorFilter(nullptr); |
| 72 |
paint.setShader(SkShaders::Color(SK_ColorBLUE)); |
| 73 |
paint.setDither(true); |
| 74 |
canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50), |
| 75 |
SkIntToScalar(30), paint); |
| 76 |
canvas->translate(SkIntToScalar(100), 0); |
| 77 |
|
| 78 |
SkFont font = SkFont(ToolUtils::DefaultPortableTypeface(), 50); |
| 79 |
paint.setStyle(SkPaint::kFill_Style); |
| 80 |
canvas->drawString("Hello", 0, 50, font, paint); |
| 81 |
|
| 82 |
paint.setShader(nullptr); |
| 83 |
paint.setColor(SK_ColorGREEN); |
| 84 |
canvas->drawString("World", 0, 100, font, paint); |
| 85 |
} |
| 86 |
|
| 87 |
private: |
| 88 |
using INHERITED = skiagm::GM; |
| 89 |
}; |
| 90 |
|
| 91 |
DEF_GM(return new EmbossGM;) |
| 92 |
|
| 93 |
|
| 94 |
void draw_emboss_example(SkCanvas* canvas, |
| 95 |
const SkRect& bounds, |
| 96 |
const SkEmbossMaskFilter::Light& light, |
| 97 |
SkScalar blurSigma, |
| 98 |
const char* label) { |
| 99 |
canvas->save(); |
| 100 |
canvas->clipRect(bounds); |
| 101 |
canvas->translate(bounds.left(), bounds.top()); |
| 102 |
|
| 103 |
SkRect contentBounds = SkRect::MakeWH(bounds.width(), bounds.height()); |
| 104 |
|
| 105 |
sk_sp<SkMaskFilter> embossFilter = SkEmbossMaskFilter::Make(blurSigma, light); |
| 106 |
if (!embossFilter) { |
| 107 |
SkDebugf("Failed to create emboss filter for: %s\n", label); |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
SkPaint paint1, paint2; |
| 112 |
paint1.setAntiAlias(true); |
| 113 |
paint1.setColor(SK_ColorRED); |
| 114 |
paint1.setMaskFilter(embossFilter); // Apply the same filter |
| 115 |
|
| 116 |
paint2.setAntiAlias(true); |
| 117 |
paint2.setColor(SK_ColorBLUE); |
| 118 |
paint2.setMaskFilter(embossFilter); // Apply the same filter |
| 119 |
|
| 120 |
// Draw shapes overlapping |
| 121 |
SkScalar radius = contentBounds.width() * 0.25f; |
| 122 |
SkScalar offset = radius * 0.3f; |
| 123 |
|
| 124 |
canvas->drawCircle(contentBounds.centerX() - offset, contentBounds.centerY(), radius, paint1); |
| 125 |
|
| 126 |
SkRect rect = SkRect::MakeXYWH(contentBounds.centerX() + offset - radius, |
| 127 |
contentBounds.centerY() - radius, |
| 128 |
radius * 2, radius * 2); |
| 129 |
canvas->drawRect(rect, paint2); |
| 130 |
|
| 131 |
SkPaint textPaint; |
| 132 |
textPaint.setColor(SK_ColorBLACK); |
| 133 |
SkTextUtils::DrawString(canvas, label, 5, 15, ToolUtils::DefaultPortableFont(), textPaint); |
| 134 |
|
| 135 |
canvas->restore(); |
| 136 |
} |
| 137 |
|
| 138 |
std::map<SkBlendMode, std::string> blendModeStrs = { |
| 139 |
{SkBlendMode::kSrc, "Src"}, |
| 140 |
{SkBlendMode::kDst, "Dst"}, |
| 141 |
{SkBlendMode::kSrcOver, "SrcOver"}, |
| 142 |
{SkBlendMode::kDstOver, "DstOver"}, |
| 143 |
{SkBlendMode::kSrcIn, "SrcIn"}, |
| 144 |
{SkBlendMode::kDstIn, "DstIn"}, |
| 145 |
{SkBlendMode::kSrcOut, "SrcOut"}, |
| 146 |
{SkBlendMode::kDstOut, "DstOut"}, |
| 147 |
{SkBlendMode::kSrcATop, "SrcATop"}, |
| 148 |
{SkBlendMode::kDstATop, "DstATop"}, |
| 149 |
{SkBlendMode::kXor, "Xor"}, |
| 150 |
{SkBlendMode::kPlus, "Plus"}, |
| 151 |
{SkBlendMode::kModulate, "Modulate"}, |
| 152 |
{SkBlendMode::kScreen, "Screen"}, |
| 153 |
{SkBlendMode::kOverlay, "Overlay"}, |
| 154 |
{SkBlendMode::kMultiply, "Multiply"}, |
| 155 |
{SkBlendMode::kDarken, "Darken"}, |
| 156 |
{SkBlendMode::kLighten, "Lighten"}, |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
void draw_emboss_blend_example(SkCanvas* canvas, |
| 161 |
const SkRect& bounds, |
| 162 |
sk_sp<SkMaskFilter> embossFilter, |
| 163 |
SkBlendMode blendMode, |
| 164 |
const char* label) { |
| 165 |
canvas->save(); |
| 166 |
canvas->clipRect(bounds); |
| 167 |
canvas->translate(bounds.left(), bounds.top()); |
| 168 |
|
| 169 |
SkRect contentBounds = SkRect::MakeWH(bounds.width(), bounds.height()); |
| 170 |
|
| 171 |
SkPaint paint1, paint2; |
| 172 |
paint1.setAntiAlias(true); |
| 173 |
paint1.setColor(SK_ColorRED); |
| 174 |
|
| 175 |
paint2.setAntiAlias(true); |
| 176 |
paint2.setColor(SK_ColorBLUE); |
| 177 |
paint2.setMaskFilter(embossFilter); |
| 178 |
paint2.setBlendMode(blendMode); // Apply the specified blend mode |
| 179 |
|
| 180 |
// Draw shapes overlapping |
| 181 |
SkScalar radius = contentBounds.width() * 0.25f; |
| 182 |
SkScalar offset = radius * 0.3f; |
| 183 |
|
| 184 |
// Draw red circle |
| 185 |
canvas->drawCircle(contentBounds.centerX() - offset, contentBounds.centerY(), radius, paint1); |
| 186 |
|
| 187 |
// Draw blue rect over the red circle |
| 188 |
SkRect rect = SkRect::MakeXYWH(contentBounds.centerX() + offset - radius, |
| 189 |
contentBounds.centerY() - radius, |
| 190 |
radius * 2, radius * 2); |
| 191 |
canvas->drawRect(rect, paint2); |
| 192 |
|
| 193 |
SkPaint textPaint; |
| 194 |
textPaint.setColor(SK_ColorRED); |
| 195 |
SkTextUtils::DrawString(canvas, label, 5, 15, ToolUtils::DefaultPortableFont(), textPaint); |
| 196 |
|
| 197 |
canvas->restore(); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
DEF_SIMPLE_GM(embossmaskfilter, canvas, 640, 960) { |
| 203 |
const int totalWidth = 640; |
| 204 |
const int topHeight = 480; |
| 205 |
const int bottomHeight = 480; |
| 206 |
|
| 207 |
// Test different values for light: |
| 208 |
{ |
| 209 |
const int gridCols = 2; |
| 210 |
const int gridRows = 2; |
| 211 |
const float cellWidth = (float)totalWidth / gridCols; |
| 212 |
const float cellHeight = (float)topHeight / gridRows; |
| 213 |
|
| 214 |
std::vector<SkEmbossMaskFilter::Light> embossRecs; |
| 215 |
std::vector<std::string> labels; |
| 216 |
SkScalar blurSigma = SkDoubleToScalar(2.5); |
| 217 |
|
| 218 |
SkEmbossMaskFilter::Light rec; |
| 219 |
|
| 220 |
// Example 1: Top-left light, medium ambient, low specular |
| 221 |
rec = {{-1.0f, -1.0f, 1.0f}, 0, 80, 16}; |
| 222 |
embossRecs.push_back(rec); |
| 223 |
labels.push_back("Light: TL, Amb: 80, Spec: 16"); |
| 224 |
|
| 225 |
// Example 2: Bottom-right light, high ambient, low specular |
| 226 |
rec = {{1.0f, 1.0f, 0.8f}, 0, 180, 16}; |
| 227 |
embossRecs.push_back(rec); |
| 228 |
labels.push_back("Light: BR, Amb: 180, Spec: 16"); |
| 229 |
|
| 230 |
// Example 3: Direct top light, low ambient, high specular |
| 231 |
rec = {{0.0f, -1.0f, 1.0f}, 0, 30, 128}; |
| 232 |
embossRecs.push_back(rec); |
| 233 |
labels.push_back("Light: Top, Amb: 30, Spec: 128"); |
| 234 |
|
| 235 |
// Example 4: Left light, medium ambient, medium specular |
| 236 |
rec = {{-1.0f, 0.0f, 0.5f}, 0, 80, 64}; |
| 237 |
embossRecs.push_back(rec); |
| 238 |
labels.push_back("Light: Left, Amb: 80, Spec: 64"); |
| 239 |
|
| 240 |
size_t exampleIndex = 0; |
| 241 |
for (int r = 0; r < gridRows; ++r) { |
| 242 |
for (int c = 0; c < gridCols; ++c) { |
| 243 |
if (exampleIndex < embossRecs.size()) { |
| 244 |
// Calculate bounds within the top half |
| 245 |
SkRect cellBounds = SkRect::MakeXYWH(c * cellWidth, r * cellHeight, cellWidth, cellHeight); |
| 246 |
draw_emboss_example(canvas, |
| 247 |
cellBounds.makeInset(10, 10), // Add padding |
| 248 |
embossRecs[exampleIndex], |
| 249 |
blurSigma, |
| 250 |
labels[exampleIndex].c_str()); |
| 251 |
exampleIndex++; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
// Test different blend modes with the mask filter |
| 258 |
{ |
| 259 |
const int gridCols = 6; |
| 260 |
const int gridRows = 3; |
| 261 |
const float cellWidth = (float)totalWidth / gridCols; |
| 262 |
const float cellHeight = (float)bottomHeight / gridRows; |
| 263 |
|
| 264 |
SkEmbossMaskFilter::Light embossRec = {{-0.707f, -0.707f, 0.707f}, 0, 60, 48}; |
| 265 |
SkScalar blurSigma = SkDoubleToScalar(2.0); |
| 266 |
|
| 267 |
sk_sp<SkMaskFilter> embossMF = SkEmbossMaskFilter::Make(blurSigma, embossRec); |
| 268 |
if (!embossMF) { |
| 269 |
SkDebugf("Failed to create shared emboss filter for blend examples.\n"); |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
// Blend Modes to Test |
| 275 |
std::vector<SkBlendMode> blendModesToTest = { |
| 276 |
SkBlendMode::kSrc, // Incorrect for Graphite |
| 277 |
SkBlendMode::kDst, |
| 278 |
SkBlendMode::kSrcOver, |
| 279 |
SkBlendMode::kDstOver, |
| 280 |
SkBlendMode::kSrcIn, // Incorrect for Graphite |
| 281 |
SkBlendMode::kDstIn, // Incorrect for Graphite |
| 282 |
SkBlendMode::kSrcOut, // Incorrect for Graphite |
| 283 |
SkBlendMode::kDstOut, |
| 284 |
SkBlendMode::kSrcATop, |
| 285 |
SkBlendMode::kDstATop, // Incorrect for Graphite |
| 286 |
SkBlendMode::kXor, |
| 287 |
SkBlendMode::kPlus, // Incorrect for Graphite |
| 288 |
SkBlendMode::kModulate, |
| 289 |
SkBlendMode::kScreen, |
| 290 |
SkBlendMode::kOverlay, |
| 291 |
SkBlendMode::kMultiply, |
| 292 |
SkBlendMode::kDarken, |
| 293 |
SkBlendMode::kLighten, |
| 294 |
}; |
| 295 |
|
| 296 |
size_t exampleIndex = 0; |
| 297 |
for (int r = 0; r < gridRows; ++r) { |
| 298 |
for (int c = 0; c < gridCols; ++c) { |
| 299 |
if (exampleIndex < blendModesToTest.size()) { |
| 300 |
SkBlendMode currentMode = blendModesToTest[exampleIndex]; |
| 301 |
std::string modeName = "Unknown"; |
| 302 |
auto it = blendModeStrs.find(currentMode); |
| 303 |
if (it != blendModeStrs.end()) { |
| 304 |
modeName = it->second; |
| 305 |
} |
| 306 |
|
| 307 |
SkString label; |
| 308 |
label.appendf("%s", modeName.c_str()); |
| 309 |
|
| 310 |
// Calculate bounds within the bottom half (offset by topHeight) |
| 311 |
SkRect cellBounds = SkRect::MakeXYWH(c * cellWidth, |
| 312 |
topHeight + r * cellHeight, // Offset Y |
| 313 |
cellWidth, cellHeight); |
| 314 |
draw_emboss_blend_example(canvas, |
| 315 |
cellBounds.makeInset(10, 10), // Add padding |
| 316 |
embossMF, // Use the same filter instance |
| 317 |
currentMode, |
| 318 |
label.c_str()); |
| 319 |
exampleIndex++; |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
DEF_SIMPLE_GM(smallemboss, canvas, 50, 50) { |
| 327 |
sk_sp<SkMaskFilter> embossFilter = SkEmbossMaskFilter::Make(3, {{1, 1, 1}, 0, 0, 16}); |
| 328 |
SkPaint paint; |
| 329 |
paint.setAntiAlias(true); |
| 330 |
paint.setColor(SK_ColorBLACK); |
| 331 |
paint.setMaskFilter(embossFilter); |
| 332 |
|
| 333 |
auto surface = canvas->makeSurface(SkImageInfo::MakeN32Premul(50, 50)); |
| 334 |
if (!surface) { |
| 335 |
surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(50, 50)); |
| 336 |
} |
| 337 |
SkCanvas* canv = surface->getCanvas(); |
| 338 |
|
| 339 |
SkRect rect = SkRect::MakeXYWH(1, 1, 3, 3); |
| 340 |
canv->drawRect(rect, paint); |
| 341 |
|
| 342 |
canvas->scale(30, 30); |
| 343 |
auto img = surface->makeImageSnapshot(); |
| 344 |
canvas->drawImage(img, 0, 0); |
| 345 |
} |