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" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkRSXform.h" |
| 11 | #include "SkSurface.h" |
| 12 | |
| 13 | class DrawAtlasGM : public skiagm::GM { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 14 | static sk_sp<SkImage> MakeAtlas(SkCanvas* caller, const SkRect& target) { |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 15 | SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 16 | auto surface(caller->makeSurface(info)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 17 | if (nullptr == surface) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 18 | surface = SkSurface::MakeRaster(info); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 19 | } |
| 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 | |
| 104 | static void draw_text_on_path_rigid(SkCanvas* canvas, const void* text, size_t length, |
| 105 | const SkPoint xy[], const SkPath& path, const SkPaint& paint) { |
| 106 | SkPathMeasure meas(path, false); |
| 107 | |
| 108 | int count = paint.countText(text, length); |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 109 | size_t size = count * (sizeof(SkRSXform) + sizeof(SkScalar)); |
| 110 | SkAutoSMalloc<512> storage(size); |
| 111 | SkRSXform* xform = (SkRSXform*)storage.get(); |
| 112 | SkScalar* widths = (SkScalar*)(xform + count); |
| 113 | |
| 114 | paint.getTextWidths(text, length, widths); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 115 | |
| 116 | for (int i = 0; i < count; ++i) { |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 117 | // we want to position each character on the center of its advance |
| 118 | const SkScalar offset = SkScalarHalf(widths[i]); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 119 | SkPoint pos; |
| 120 | SkVector tan; |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 121 | if (!meas.getPosTan(xy[i].x() + offset, &pos, &tan)) { |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 122 | pos = xy[i]; |
| 123 | tan.set(1, 0); |
| 124 | } |
| 125 | xform[i].fSCos = tan.x(); |
| 126 | xform[i].fSSin = tan.y(); |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 127 | xform[i].fTx = pos.x() - tan.y() * xy[i].y() - tan.x() * offset; |
| 128 | xform[i].fTy = pos.y() + tan.x() * xy[i].y() - tan.y() * offset; |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 129 | } |
| 130 | |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 131 | // Compute a conservative bounds so we can cull the draw |
| 132 | const SkRect font = paint.getFontBounds(); |
| 133 | const SkScalar max = SkTMax(SkTMax(SkScalarAbs(font.fLeft), SkScalarAbs(font.fRight)), |
| 134 | SkTMax(SkScalarAbs(font.fTop), SkScalarAbs(font.fBottom))); |
| 135 | const SkRect bounds = path.getBounds().makeOutset(max, max); |
| 136 | |
| 137 | canvas->drawTextRSXform(text, length, &xform[0], &bounds, paint); |
| 138 | |
| 139 | if (true) { |
| 140 | SkPaint p; |
| 141 | p.setStyle(SkPaint::kStroke_Style); |
| 142 | canvas->drawRect(bounds, p); |
| 143 | } |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 144 | } |
| 145 | |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 146 | DEF_SIMPLE_GM(drawTextRSXform, canvas, 860, 860) { |
reed | dde2b1f | 2016-07-08 03:31:09 -0700 | [diff] [blame] | 147 | const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ"; |
reed | dde2b1f | 2016-07-08 03:31:09 -0700 | [diff] [blame] | 148 | const int N = sizeof(text0) - 1; |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 149 | SkPoint pos[N]; |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 150 | |
| 151 | SkPaint paint; |
| 152 | paint.setAntiAlias(true); |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 153 | paint.setTextSize(100); |
| 154 | |
| 155 | SkScalar x = 0; |
| 156 | for (int i = 0; i < N; ++i) { |
| 157 | pos[i].set(x, 0); |
| 158 | x += paint.measureText(&text0[i], 1); |
| 159 | } |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 160 | |
| 161 | SkPath path; |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 162 | path.addOval(SkRect::MakeXYWH(160, 160, 540, 540)); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 163 | |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 164 | draw_text_on_path_rigid(canvas, text0, N, pos, path, paint); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 165 | |
| 166 | paint.setStyle(SkPaint::kStroke_Style); |
| 167 | canvas->drawPath(path, paint); |
| 168 | } |
| 169 | |
| 170 | |