| 1 |
/* |
| 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 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBlendMode.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorFilter.h" |
| 13 |
#include "include/core/SkFont.h" |
| 14 |
#include "include/core/SkFontMgr.h" |
| 15 |
#include "include/core/SkFontTypes.h" |
| 16 |
#include "include/core/SkImage.h" |
| 17 |
#include "include/core/SkImageInfo.h" |
| 18 |
#include "include/core/SkPaint.h" |
| 19 |
#include "include/core/SkPath.h" |
| 20 |
#include "include/core/SkPathMeasure.h" |
| 21 |
#include "include/core/SkPoint.h" |
| 22 |
#include "include/core/SkRSXform.h" |
| 23 |
#include "include/core/SkRect.h" |
| 24 |
#include "include/core/SkRefCnt.h" |
| 25 |
#include "include/core/SkScalar.h" |
| 26 |
#include "include/core/SkShader.h" |
| 27 |
#include "include/core/SkSize.h" |
| 28 |
#include "include/core/SkStream.h" |
| 29 |
#include "include/core/SkString.h" |
| 30 |
#include "include/core/SkSurface.h" |
| 31 |
#include "include/core/SkTextBlob.h" |
| 32 |
#include "include/core/SkTileMode.h" |
| 33 |
#include "include/core/SkTypeface.h" |
| 34 |
#include "include/core/SkTypes.h" |
| 35 |
#include "include/core/SkVertices.h" |
| 36 |
#include "include/effects/SkGradient.h" |
| 37 |
#include "include/private/base/SkTemplates.h" |
| 38 |
#include "src/base/SkAutoMalloc.h" |
| 39 |
#include "src/core/SkFontPriv.h" |
| 40 |
#include "tools/DecodeUtils.h" |
| 41 |
#include "tools/Resources.h" |
| 42 |
#include "tools/ToolUtils.h" |
| 43 |
#include "tools/fonts/FontToolUtils.h" |
| 44 |
|
| 45 |
#include <initializer_list> |
| 46 |
|
| 47 |
using namespace skia_private; |
| 48 |
|
| 49 |
class DrawAtlasGM : public skiagm::GM { |
| 50 |
static sk_sp<SkImage> MakeAtlas(SkCanvas* caller, const SkRect& target) { |
| 51 |
SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 52 |
auto surface(ToolUtils::makeSurface(caller, info)); |
| 53 |
SkCanvas* canvas = surface->getCanvas(); |
| 54 |
// draw red everywhere, but we don't expect to see it in the draw, testing the notion |
| 55 |
// that drawAtlas draws a subset-region of the atlas. |
| 56 |
canvas->clear(SK_ColorRED); |
| 57 |
|
| 58 |
SkPaint paint; |
| 59 |
paint.setBlendMode(SkBlendMode::kClear); |
| 60 |
SkRect r(target); |
| 61 |
r.inset(-1, -1); |
| 62 |
// zero out a place (with a 1-pixel border) to land our drawing. |
| 63 |
canvas->drawRect(r, paint); |
| 64 |
paint.setBlendMode(SkBlendMode::kSrcOver); |
| 65 |
paint.setColor(SK_ColorBLUE); |
| 66 |
paint.setAntiAlias(true); |
| 67 |
canvas->drawOval(target, paint); |
| 68 |
return surface->makeImageSnapshot(); |
| 69 |
} |
| 70 |
|
| 71 |
public: |
| 72 |
DrawAtlasGM() {} |
| 73 |
|
| 74 |
protected: |
| 75 |
SkString getName() const override { return SkString("draw-atlas"); } |
| 76 |
|
| 77 |
SkISize getISize() override { return SkISize::Make(640, 480); } |
| 78 |
|
| 79 |
void onDraw(SkCanvas* canvas) override { |
| 80 |
const SkRect target = { 50, 50, 80, 90 }; |
| 81 |
auto atlas = MakeAtlas(canvas, target); |
| 82 |
|
| 83 |
const struct { |
| 84 |
SkScalar fScale; |
| 85 |
SkScalar fDegrees; |
| 86 |
SkScalar fTx; |
| 87 |
SkScalar fTy; |
| 88 |
|
| 89 |
void apply(SkRSXform* xform) const { |
| 90 |
const SkScalar rad = SkDegreesToRadians(fDegrees); |
| 91 |
xform->fSCos = fScale * SkScalarCos(rad); |
| 92 |
xform->fSSin = fScale * SkScalarSin(rad); |
| 93 |
xform->fTx = fTx; |
| 94 |
xform->fTy = fTy; |
| 95 |
} |
| 96 |
} rec[] = { |
| 97 |
{ 1, 0, 10, 10 }, // just translate |
| 98 |
{ 2, 0, 110, 10 }, // scale + translate |
| 99 |
{ 1, 30, 210, 10 }, // rotate + translate |
| 100 |
{ 2, -30, 310, 30 }, // scale + rotate + translate |
| 101 |
}; |
| 102 |
|
| 103 |
const int N = std::size(rec); |
| 104 |
SkRSXform xform[N]; |
| 105 |
SkRect tex[N]; |
| 106 |
SkColor colors[N]; |
| 107 |
|
| 108 |
for (int i = 0; i < N; ++i) { |
| 109 |
rec[i].apply(&xform[i]); |
| 110 |
tex[i] = target; |
| 111 |
colors[i] = 0x80FF0000 + (i * 40 * 256); |
| 112 |
} |
| 113 |
|
| 114 |
SkPaint paint; |
| 115 |
paint.setAntiAlias(true); |
| 116 |
SkSamplingOptions sampling(SkFilterMode::kLinear); |
| 117 |
|
| 118 |
canvas->drawAtlas(atlas.get(), xform, tex, {}, SkBlendMode::kDst, |
| 119 |
sampling, nullptr, &paint); |
| 120 |
canvas->translate(0, 100); |
| 121 |
canvas->drawAtlas(atlas.get(), xform, tex, colors, SkBlendMode::kSrcIn, |
| 122 |
sampling, nullptr, &paint); |
| 123 |
} |
| 124 |
|
| 125 |
private: |
| 126 |
using INHERITED = GM; |
| 127 |
}; |
| 128 |
DEF_GM( return new DrawAtlasGM; ) |
| 129 |
|
| 130 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 131 |
|
| 132 |
static void draw_text_on_path(SkCanvas* canvas, const void* text, size_t length, |
| 133 |
const SkPoint xy[], const SkPath& path, const SkFont& font, const SkPaint& paint, |
| 134 |
float baseline_offset) { |
| 135 |
SkPathMeasure meas(path, false); |
| 136 |
|
| 137 |
size_t count = font.countText(text, length, SkTextEncoding::kUTF8); |
| 138 |
size_t size = count * (sizeof(SkRSXform) + sizeof(SkScalar)); |
| 139 |
SkAutoSMalloc<512> storage(size); |
| 140 |
SkRSXform* xform = (SkRSXform*)storage.get(); |
| 141 |
SkScalar* widths = (SkScalar*)(xform + count); |
| 142 |
|
| 143 |
// Compute a conservative bounds so we can cull the draw |
| 144 |
const SkRect fontb = SkFontPriv::GetFontBounds(font); |
| 145 |
const SkScalar max = std::max(std::max(SkScalarAbs(fontb.fLeft), SkScalarAbs(fontb.fRight)), |
| 146 |
std::max(SkScalarAbs(fontb.fTop), SkScalarAbs(fontb.fBottom))); |
| 147 |
const SkRect bounds = path.getBounds().makeOutset(max, max); |
| 148 |
|
| 149 |
AutoTArray<SkGlyphID> glyphs(count); |
| 150 |
font.textToGlyphs(text, length, SkTextEncoding::kUTF8, glyphs); |
| 151 |
font.getWidths(glyphs, {widths, count}); |
| 152 |
|
| 153 |
for (size_t i = 0; i < count; ++i) { |
| 154 |
// we want to position each character on the center of its advance |
| 155 |
const SkScalar offset = SkScalarHalf(widths[i]); |
| 156 |
SkPoint pos; |
| 157 |
SkVector tan; |
| 158 |
if (!meas.getPosTan(xy[i].x() + offset, &pos, &tan)) { |
| 159 |
pos = xy[i]; |
| 160 |
tan.set(1, 0); |
| 161 |
} |
| 162 |
pos += SkVector::Make(-tan.fY, tan.fX) * baseline_offset; |
| 163 |
|
| 164 |
xform[i].fSCos = tan.x(); |
| 165 |
xform[i].fSSin = tan.y(); |
| 166 |
xform[i].fTx = pos.x() - tan.y() * xy[i].y() - tan.x() * offset; |
| 167 |
xform[i].fTy = pos.y() + tan.x() * xy[i].y() - tan.y() * offset; |
| 168 |
} |
| 169 |
|
| 170 |
canvas->drawTextBlob(SkTextBlob::MakeFromRSXformGlyphs(glyphs, {xform, count}, font), |
| 171 |
0, 0, paint); |
| 172 |
|
| 173 |
if (true) { |
| 174 |
SkPaint p; |
| 175 |
p.setStyle(SkPaint::kStroke_Style); |
| 176 |
canvas->drawRect(bounds, p); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
static sk_sp<SkShader> make_shader() { |
| 181 |
const SkPoint pts[2] = {{0, 0}, {220, 0}}; |
| 182 |
const SkColor4f colors[2] = {SkColors::kRed, SkColors::kBlue}; |
| 183 |
return SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kMirror}, {}}); |
| 184 |
} |
| 185 |
|
| 186 |
static void drawTextPath(SkCanvas* canvas, bool doStroke) { |
| 187 |
const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ"; |
| 188 |
const int N = sizeof(text0) - 1; |
| 189 |
SkPoint pos[N]; |
| 190 |
|
| 191 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 192 |
font.setSize(100); |
| 193 |
|
| 194 |
SkPaint paint; |
| 195 |
paint.setShader(make_shader()); |
| 196 |
paint.setAntiAlias(true); |
| 197 |
if (doStroke) { |
| 198 |
paint.setStyle(SkPaint::kStroke_Style); |
| 199 |
paint.setStrokeWidth(2.25f); |
| 200 |
paint.setStrokeJoin(SkPaint::kRound_Join); |
| 201 |
} |
| 202 |
|
| 203 |
SkScalar x = 0; |
| 204 |
for (int i = 0; i < N; ++i) { |
| 205 |
pos[i].set(x, 0); |
| 206 |
x += font.measureText(&text0[i], 1, SkTextEncoding::kUTF8, nullptr, &paint); |
| 207 |
} |
| 208 |
|
| 209 |
SkPath path; |
| 210 |
const float baseline_offset = -5; |
| 211 |
|
| 212 |
const SkPathDirection dirs[] = { |
| 213 |
SkPathDirection::kCW, SkPathDirection::kCCW, |
| 214 |
}; |
| 215 |
for (auto d : dirs) { |
| 216 |
path = SkPath::Oval(SkRect::MakeXYWH(160, 160, 540, 540), d); |
| 217 |
draw_text_on_path(canvas, text0, N, pos, path, font, paint, baseline_offset); |
| 218 |
} |
| 219 |
|
| 220 |
paint.reset(); |
| 221 |
paint.setStyle(SkPaint::kStroke_Style); |
| 222 |
canvas->drawPath(path, paint); |
| 223 |
} |
| 224 |
|
| 225 |
DEF_SIMPLE_GM(drawTextRSXform, canvas, 430, 860) { |
| 226 |
canvas->scale(0.5f, 0.5f); |
| 227 |
const bool doStroke[] = { false, true }; |
| 228 |
for (auto st : doStroke) { |
| 229 |
drawTextPath(canvas, st); |
| 230 |
canvas->translate(0, 860); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// Exercise xform blob and its bounds |
| 235 |
DEF_SIMPLE_GM(blob_rsxform, canvas, 500, 100) { |
| 236 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 237 |
font.setSize(50); |
| 238 |
|
| 239 |
const char text[] = "CrazyXform"; |
| 240 |
constexpr size_t len = sizeof(text) - 1; |
| 241 |
|
| 242 |
SkRSXform xforms[len]; |
| 243 |
SkScalar scale = 1; |
| 244 |
SkScalar x = 0, y = 0; |
| 245 |
for (size_t i = 0; i < len; ++i) { |
| 246 |
scale = SkScalarSin(i * SK_ScalarPI / (len-1)) * 0.75f + 0.5f; |
| 247 |
xforms[i] = SkRSXform::Make(scale, 0, x, y); |
| 248 |
x += 50 * scale; |
| 249 |
} |
| 250 |
|
| 251 |
auto blob = SkTextBlob::MakeFromRSXform(text, len, xforms, font); |
| 252 |
|
| 253 |
SkPoint offset = { 20, 70 }; |
| 254 |
SkPaint paint; |
| 255 |
paint.setColor(0xFFCCCCCC); |
| 256 |
canvas->drawRect(blob->bounds().makeOffset(offset.fX, offset.fY), paint); |
| 257 |
paint.setColor(SK_ColorBLACK); |
| 258 |
canvas->drawTextBlob(blob, offset.fX, offset.fY, paint); |
| 259 |
} |
| 260 |
|
| 261 |
// Exercise xform blob and its tight bounds |
| 262 |
DEF_SIMPLE_GM(blob_rsxform_distortable, canvas, 500, 100) { |
| 263 |
sk_sp<SkTypeface> typeface; |
| 264 |
std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf")); |
| 265 |
if (distortable) { |
| 266 |
sk_sp<SkFontMgr> fm = ToolUtils::TestFontMgr(); |
| 267 |
const SkFontArguments::VariationPosition::Coordinate position[] = { |
| 268 |
{ SkSetFourByteTag('w','g','h','t'), 1.618033988749895f } |
| 269 |
}; |
| 270 |
SkFontArguments params; |
| 271 |
params.setVariationDesignPosition({position, std::size(position)}); |
| 272 |
typeface = fm->makeFromStream(std::move(distortable), params); |
| 273 |
} |
| 274 |
if (!typeface) { |
| 275 |
typeface = ToolUtils::DefaultPortableTypeface(); |
| 276 |
} |
| 277 |
|
| 278 |
SkFont font(typeface, 50); |
| 279 |
|
| 280 |
const char text[] = "abcabcabc"; |
| 281 |
constexpr size_t len = sizeof(text) - 1; |
| 282 |
|
| 283 |
SkRSXform xforms[len]; |
| 284 |
SkScalar scale = 1; |
| 285 |
SkScalar x = 0, y = 0; |
| 286 |
for (size_t i = 0; i < len; ++i) { |
| 287 |
scale = SkScalarSin(i * SK_ScalarPI / (len-1)) * 0.75f + 0.5f; |
| 288 |
xforms[i] = SkRSXform::Make(scale, 0, x, y); |
| 289 |
x += 50 * scale; |
| 290 |
} |
| 291 |
|
| 292 |
auto blob = SkTextBlob::MakeFromRSXform(text, len, xforms, font); |
| 293 |
|
| 294 |
SkPoint offset = { 20, 70 }; |
| 295 |
SkPaint paint; |
| 296 |
paint.setColor(0xFFCCCCCC); |
| 297 |
canvas->drawRect(blob->bounds().makeOffset(offset.fX, offset.fY), paint); |
| 298 |
paint.setColor(SK_ColorBLACK); |
| 299 |
canvas->drawTextBlob(blob, offset.fX, offset.fY, paint); |
| 300 |
} |
| 301 |
|
| 302 |
static sk_sp<SkVertices> make_vertices(sk_sp<SkImage> image, const SkRect& r, |
| 303 |
SkColor color) { |
| 304 |
const std::array<SkPoint, 4> pos = r.toQuad(); |
| 305 |
SkColor colors[4] = { color, color, color, color }; |
| 306 |
return SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, |
| 307 |
pos.data(), pos.data(), colors); |
| 308 |
} |
| 309 |
|
| 310 |
/* |
| 311 |
* drawAtlas and drawVertices have several things in common: |
| 312 |
* - can create compound "shaders", combining texture and colors |
| 313 |
* - these are combined via an explicit blendmode |
| 314 |
* - like drawImage, they only respect parts of the paint |
| 315 |
* - colorfilter, imagefilter, blendmode, alpha |
| 316 |
* |
| 317 |
* This GM produces a series of pairs of images (atlas | vertices). |
| 318 |
* Each pair should look the same, and each set shows a different combination |
| 319 |
* of alpha | colorFilter | mode |
| 320 |
*/ |
| 321 |
DEF_SIMPLE_GM(compare_atlas_vertices, canvas, 560, 585) { |
| 322 |
const SkRect tex = SkRect::MakeWH(128, 128); |
| 323 |
const SkRSXform xform = SkRSXform::Make(1, 0, 0, 0); |
| 324 |
const SkColor color = 0x884488CC; |
| 325 |
|
| 326 |
auto image = ToolUtils::GetResourceAsImage("images/mandrill_128.png"); |
| 327 |
auto verts = make_vertices(image, tex, color); |
| 328 |
const sk_sp<SkColorFilter> filters[] = { |
| 329 |
nullptr, |
| 330 |
SkColorFilters::Blend(0xFF00FF88, SkBlendMode::kModulate), |
| 331 |
}; |
| 332 |
const SkBlendMode modes[] = { |
| 333 |
SkBlendMode::kSrcOver, |
| 334 |
SkBlendMode::kPlus, |
| 335 |
}; |
| 336 |
|
| 337 |
canvas->translate(10, 10); |
| 338 |
SkPaint paint; |
| 339 |
for (SkBlendMode mode : modes) { |
| 340 |
for (float alpha : { 1.0f, 0.5f }) { |
| 341 |
paint.setAlphaf(alpha); |
| 342 |
canvas->save(); |
| 343 |
for (const sk_sp<SkColorFilter>& cf : filters) { |
| 344 |
paint.setColorFilter(cf); |
| 345 |
canvas->drawAtlas(image.get(), {&xform, 1}, {&tex, 1}, {&color, 1}, mode, |
| 346 |
SkSamplingOptions(), &tex, &paint); |
| 347 |
canvas->translate(128, 0); |
| 348 |
paint.setShader(image->makeShader(SkSamplingOptions())); |
| 349 |
canvas->drawVertices(verts, mode, paint); |
| 350 |
paint.setShader(nullptr); |
| 351 |
canvas->translate(145, 0); |
| 352 |
} |
| 353 |
canvas->restore(); |
| 354 |
canvas->translate(0, 145); |
| 355 |
} |
| 356 |
} |
| 357 |
} |