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" |
| 14 | |
| 15 | class DrawAtlasGM : public skiagm::GM { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 16 | static sk_sp<SkImage> MakeAtlas(SkCanvas* caller, const SkRect& target) { |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 17 | SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 18 | auto surface(caller->makeSurface(info)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 19 | if (nullptr == surface) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 20 | surface = SkSurface::MakeRaster(info); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 21 | } |
| 22 | SkCanvas* canvas = surface->getCanvas(); |
| 23 | // draw red everywhere, but we don't expect to see it in the draw, testing the notion |
| 24 | // that drawAtlas draws a subset-region of the atlas. |
| 25 | canvas->clear(SK_ColorRED); |
| 26 | |
| 27 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 28 | paint.setBlendMode(SkBlendMode::kClear); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 29 | SkRect r(target); |
| 30 | r.inset(-1, -1); |
| 31 | // zero out a place (with a 1-pixel border) to land our drawing. |
| 32 | canvas->drawRect(r, paint); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 33 | paint.setBlendMode(SkBlendMode::kSrcOver); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 34 | paint.setColor(SK_ColorBLUE); |
| 35 | paint.setAntiAlias(true); |
| 36 | canvas->drawOval(target, paint); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 37 | return surface->makeImageSnapshot(); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 38 | } |
| 39 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 40 | public: |
| 41 | DrawAtlasGM() {} |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 42 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 43 | protected: |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 44 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 45 | SkString onShortName() override { |
| 46 | return SkString("draw-atlas"); |
| 47 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 48 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 49 | SkISize onISize() override { |
| 50 | return SkISize::Make(640, 480); |
| 51 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 52 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 53 | void onDraw(SkCanvas* canvas) override { |
| 54 | const SkRect target = { 50, 50, 80, 90 }; |
brianosman | 95e8d0a | 2016-09-29 13:43:49 -0700 | [diff] [blame] | 55 | auto atlas = MakeAtlas(canvas, target); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 56 | |
| 57 | const struct { |
| 58 | SkScalar fScale; |
| 59 | SkScalar fDegrees; |
| 60 | SkScalar fTx; |
| 61 | SkScalar fTy; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 62 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 63 | void apply(SkRSXform* xform) const { |
| 64 | const SkScalar rad = SkDegreesToRadians(fDegrees); |
| 65 | xform->fSCos = fScale * SkScalarCos(rad); |
| 66 | xform->fSSin = fScale * SkScalarSin(rad); |
| 67 | xform->fTx = fTx; |
| 68 | xform->fTy = fTy; |
| 69 | } |
| 70 | } rec[] = { |
| 71 | { 1, 0, 10, 10 }, // just translate |
| 72 | { 2, 0, 110, 10 }, // scale + translate |
| 73 | { 1, 30, 210, 10 }, // rotate + translate |
| 74 | { 2, -30, 310, 30 }, // scale + rotate + translate |
| 75 | }; |
| 76 | |
| 77 | const int N = SK_ARRAY_COUNT(rec); |
| 78 | SkRSXform xform[N]; |
| 79 | SkRect tex[N]; |
| 80 | SkColor colors[N]; |
| 81 | |
| 82 | for (int i = 0; i < N; ++i) { |
| 83 | rec[i].apply(&xform[i]); |
| 84 | tex[i] = target; |
| 85 | colors[i] = 0x80FF0000 + (i * 40 * 256); |
| 86 | } |
| 87 | |
| 88 | SkPaint paint; |
| 89 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 90 | paint.setAntiAlias(true); |
| 91 | |
brianosman | 95e8d0a | 2016-09-29 13:43:49 -0700 | [diff] [blame] | 92 | canvas->drawAtlas(atlas.get(), xform, tex, N, nullptr, &paint); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 93 | canvas->translate(0, 100); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 94 | canvas->drawAtlas(atlas.get(), xform, tex, colors, N, SkBlendMode::kSrcIn, nullptr, &paint); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 95 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 96 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 97 | private: |
| 98 | typedef GM INHERITED; |
| 99 | }; |
| 100 | DEF_GM( return new DrawAtlasGM; ) |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 101 | |
| 102 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 103 | #include "SkPath.h" |
| 104 | #include "SkPathMeasure.h" |
| 105 | |
| 106 | static void draw_text_on_path_rigid(SkCanvas* canvas, const void* text, size_t length, |
| 107 | const SkPoint xy[], const SkPath& path, const SkPaint& paint) { |
| 108 | SkPathMeasure meas(path, false); |
| 109 | |
| 110 | int count = paint.countText(text, length); |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 111 | size_t size = count * (sizeof(SkRSXform) + sizeof(SkScalar)); |
| 112 | SkAutoSMalloc<512> storage(size); |
| 113 | SkRSXform* xform = (SkRSXform*)storage.get(); |
| 114 | SkScalar* widths = (SkScalar*)(xform + count); |
| 115 | |
| 116 | paint.getTextWidths(text, length, widths); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 117 | |
| 118 | for (int i = 0; i < count; ++i) { |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 119 | // we want to position each character on the center of its advance |
| 120 | const SkScalar offset = SkScalarHalf(widths[i]); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 121 | SkPoint pos; |
| 122 | SkVector tan; |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 123 | if (!meas.getPosTan(xy[i].x() + offset, &pos, &tan)) { |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 124 | pos = xy[i]; |
| 125 | tan.set(1, 0); |
| 126 | } |
| 127 | xform[i].fSCos = tan.x(); |
| 128 | xform[i].fSSin = tan.y(); |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 129 | xform[i].fTx = pos.x() - tan.y() * xy[i].y() - tan.x() * offset; |
| 130 | xform[i].fTy = pos.y() + tan.x() * xy[i].y() - tan.y() * offset; |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 131 | } |
| 132 | |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 133 | // Compute a conservative bounds so we can cull the draw |
| 134 | const SkRect font = paint.getFontBounds(); |
| 135 | const SkScalar max = SkTMax(SkTMax(SkScalarAbs(font.fLeft), SkScalarAbs(font.fRight)), |
| 136 | SkTMax(SkScalarAbs(font.fTop), SkScalarAbs(font.fBottom))); |
| 137 | const SkRect bounds = path.getBounds().makeOutset(max, max); |
| 138 | |
| 139 | canvas->drawTextRSXform(text, length, &xform[0], &bounds, paint); |
| 140 | |
| 141 | if (true) { |
| 142 | SkPaint p; |
| 143 | p.setStyle(SkPaint::kStroke_Style); |
| 144 | canvas->drawRect(bounds, p); |
| 145 | } |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 146 | } |
| 147 | |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 148 | DEF_SIMPLE_GM(drawTextRSXform, canvas, 860, 860) { |
reed | dde2b1f | 2016-07-08 03:31:09 -0700 | [diff] [blame] | 149 | const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ"; |
reed | dde2b1f | 2016-07-08 03:31:09 -0700 | [diff] [blame] | 150 | const int N = sizeof(text0) - 1; |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 151 | SkPoint pos[N]; |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 152 | |
| 153 | SkPaint paint; |
| 154 | paint.setAntiAlias(true); |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 155 | paint.setTextSize(100); |
| 156 | |
| 157 | SkScalar x = 0; |
| 158 | for (int i = 0; i < N; ++i) { |
| 159 | pos[i].set(x, 0); |
| 160 | x += paint.measureText(&text0[i], 1); |
| 161 | } |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 162 | |
| 163 | SkPath path; |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 164 | path.addOval(SkRect::MakeXYWH(160, 160, 540, 540)); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 165 | |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 166 | draw_text_on_path_rigid(canvas, text0, N, pos, path, paint); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 167 | |
| 168 | paint.setStyle(SkPaint::kStroke_Style); |
| 169 | canvas->drawPath(path, paint); |
| 170 | } |
| 171 | |
| 172 | |