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; |
| 26 | paint.setXfermodeMode(SkXfermode::kClear_Mode); |
| 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 31 | paint.setXfermode(nullptr); |
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 | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 38 | sk_sp<SkImage> fAtlas; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 39 | |
| 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 }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | if (nullptr == fAtlas) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 56 | fAtlas = MakeAtlas(canvas, target); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | const struct { |
| 60 | SkScalar fScale; |
| 61 | SkScalar fDegrees; |
| 62 | SkScalar fTx; |
| 63 | SkScalar fTy; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 64 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 65 | void apply(SkRSXform* xform) const { |
| 66 | const SkScalar rad = SkDegreesToRadians(fDegrees); |
| 67 | xform->fSCos = fScale * SkScalarCos(rad); |
| 68 | xform->fSSin = fScale * SkScalarSin(rad); |
| 69 | xform->fTx = fTx; |
| 70 | xform->fTy = fTy; |
| 71 | } |
| 72 | } rec[] = { |
| 73 | { 1, 0, 10, 10 }, // just translate |
| 74 | { 2, 0, 110, 10 }, // scale + translate |
| 75 | { 1, 30, 210, 10 }, // rotate + translate |
| 76 | { 2, -30, 310, 30 }, // scale + rotate + translate |
| 77 | }; |
| 78 | |
| 79 | const int N = SK_ARRAY_COUNT(rec); |
| 80 | SkRSXform xform[N]; |
| 81 | SkRect tex[N]; |
| 82 | SkColor colors[N]; |
| 83 | |
| 84 | for (int i = 0; i < N; ++i) { |
| 85 | rec[i].apply(&xform[i]); |
| 86 | tex[i] = target; |
| 87 | colors[i] = 0x80FF0000 + (i * 40 * 256); |
| 88 | } |
| 89 | |
| 90 | SkPaint paint; |
| 91 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 92 | paint.setAntiAlias(true); |
| 93 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 94 | canvas->drawAtlas(fAtlas.get(), xform, tex, N, nullptr, &paint); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 95 | canvas->translate(0, 100); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 96 | canvas->drawAtlas(fAtlas.get(), xform, tex, colors, N, SkXfermode::kSrcIn_Mode, nullptr, &paint); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 97 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 98 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 99 | private: |
| 100 | typedef GM INHERITED; |
| 101 | }; |
| 102 | DEF_GM( return new DrawAtlasGM; ) |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 103 | |
| 104 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 105 | #include "SkPath.h" |
| 106 | #include "SkPathMeasure.h" |
| 107 | |
| 108 | static void draw_text_on_path_rigid(SkCanvas* canvas, const void* text, size_t length, |
| 109 | const SkPoint xy[], const SkPath& path, const SkPaint& paint) { |
| 110 | SkPathMeasure meas(path, false); |
| 111 | |
| 112 | int count = paint.countText(text, length); |
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 | |
| 118 | paint.getTextWidths(text, length, widths); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 119 | |
| 120 | for (int i = 0; i < count; ++i) { |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 121 | // we want to position each character on the center of its advance |
| 122 | const SkScalar offset = SkScalarHalf(widths[i]); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 123 | SkPoint pos; |
| 124 | SkVector tan; |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 125 | if (!meas.getPosTan(xy[i].x() + offset, &pos, &tan)) { |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 126 | pos = xy[i]; |
| 127 | tan.set(1, 0); |
| 128 | } |
| 129 | xform[i].fSCos = tan.x(); |
| 130 | xform[i].fSSin = tan.y(); |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 131 | xform[i].fTx = pos.x() - tan.y() * xy[i].y() - tan.x() * offset; |
| 132 | xform[i].fTy = pos.y() + tan.x() * xy[i].y() - tan.y() * offset; |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 133 | } |
| 134 | |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 135 | // Compute a conservative bounds so we can cull the draw |
| 136 | const SkRect font = paint.getFontBounds(); |
| 137 | const SkScalar max = SkTMax(SkTMax(SkScalarAbs(font.fLeft), SkScalarAbs(font.fRight)), |
| 138 | SkTMax(SkScalarAbs(font.fTop), SkScalarAbs(font.fBottom))); |
| 139 | const SkRect bounds = path.getBounds().makeOutset(max, max); |
| 140 | |
| 141 | canvas->drawTextRSXform(text, length, &xform[0], &bounds, paint); |
| 142 | |
| 143 | if (true) { |
| 144 | SkPaint p; |
| 145 | p.setStyle(SkPaint::kStroke_Style); |
| 146 | canvas->drawRect(bounds, p); |
| 147 | } |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 148 | } |
| 149 | |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 150 | DEF_SIMPLE_GM(drawTextRSXform, canvas, 860, 860) { |
reed | dde2b1f | 2016-07-08 03:31:09 -0700 | [diff] [blame] | 151 | const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ"; |
reed | dde2b1f | 2016-07-08 03:31:09 -0700 | [diff] [blame] | 152 | const int N = sizeof(text0) - 1; |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 153 | SkPoint pos[N]; |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 154 | |
| 155 | SkPaint paint; |
| 156 | paint.setAntiAlias(true); |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 157 | paint.setTextSize(100); |
| 158 | |
| 159 | SkScalar x = 0; |
| 160 | for (int i = 0; i < N; ++i) { |
| 161 | pos[i].set(x, 0); |
| 162 | x += paint.measureText(&text0[i], 1); |
| 163 | } |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 164 | |
| 165 | SkPath path; |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 166 | path.addOval(SkRect::MakeXYWH(160, 160, 540, 540)); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 167 | |
reed | 7c70d7c | 2016-07-12 15:06:33 -0700 | [diff] [blame] | 168 | draw_text_on_path_rigid(canvas, text0, N, pos, path, paint); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 169 | |
| 170 | paint.setStyle(SkPaint::kStroke_Style); |
| 171 | canvas->drawPath(path, paint); |
| 172 | } |
| 173 | |
| 174 | |