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