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