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