blob: 43b103c2c3f5a68910ebfd7e99cc590f8b7fc483 [file] [log] [blame]
reed71c3c762015-06-24 10:29:17 -07001/*
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
13class DrawAtlasGM : public skiagm::GM {
reed9ce9d672016-03-17 10:51:11 -070014 static sk_sp<SkImage> MakeAtlas(SkCanvas* caller, const SkRect& target) {
reed71c3c762015-06-24 10:29:17 -070015 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
reede8f30622016-03-23 18:59:25 -070016 auto surface(caller->makeSurface(info));
halcanary96fcdcc2015-08-27 07:41:13 -070017 if (nullptr == surface) {
reede8f30622016-03-23 18:59:25 -070018 surface = SkSurface::MakeRaster(info);
reed71c3c762015-06-24 10:29:17 -070019 }
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;
reed374772b2016-10-05 17:33:02 -070026 paint.setBlendMode(SkBlendMode::kClear);
reed71c3c762015-06-24 10:29:17 -070027 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);
reed374772b2016-10-05 17:33:02 -070031 paint.setBlendMode(SkBlendMode::kSrcOver);
reed71c3c762015-06-24 10:29:17 -070032 paint.setColor(SK_ColorBLUE);
33 paint.setAntiAlias(true);
34 canvas->drawOval(target, paint);
reed9ce9d672016-03-17 10:51:11 -070035 return surface->makeImageSnapshot();
reed71c3c762015-06-24 10:29:17 -070036 }
37
reed71c3c762015-06-24 10:29:17 -070038public:
39 DrawAtlasGM() {}
halcanary9d524f22016-03-29 09:03:52 -070040
reed71c3c762015-06-24 10:29:17 -070041protected:
halcanary9d524f22016-03-29 09:03:52 -070042
reed71c3c762015-06-24 10:29:17 -070043 SkString onShortName() override {
44 return SkString("draw-atlas");
45 }
halcanary9d524f22016-03-29 09:03:52 -070046
reed71c3c762015-06-24 10:29:17 -070047 SkISize onISize() override {
48 return SkISize::Make(640, 480);
49 }
halcanary9d524f22016-03-29 09:03:52 -070050
reed71c3c762015-06-24 10:29:17 -070051 void onDraw(SkCanvas* canvas) override {
52 const SkRect target = { 50, 50, 80, 90 };
brianosman95e8d0a2016-09-29 13:43:49 -070053 auto atlas = MakeAtlas(canvas, target);
reed71c3c762015-06-24 10:29:17 -070054
55 const struct {
56 SkScalar fScale;
57 SkScalar fDegrees;
58 SkScalar fTx;
59 SkScalar fTy;
halcanary9d524f22016-03-29 09:03:52 -070060
reed71c3c762015-06-24 10:29:17 -070061 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
brianosman95e8d0a2016-09-29 13:43:49 -070090 canvas->drawAtlas(atlas.get(), xform, tex, N, nullptr, &paint);
reed71c3c762015-06-24 10:29:17 -070091 canvas->translate(0, 100);
brianosman95e8d0a2016-09-29 13:43:49 -070092 canvas->drawAtlas(atlas.get(), xform, tex, colors, N, SkXfermode::kSrcIn_Mode, nullptr, &paint);
reed71c3c762015-06-24 10:29:17 -070093 }
halcanary9d524f22016-03-29 09:03:52 -070094
reed71c3c762015-06-24 10:29:17 -070095private:
96 typedef GM INHERITED;
97};
98DEF_GM( return new DrawAtlasGM; )
reed45561a02016-07-07 12:47:17 -070099
100///////////////////////////////////////////////////////////////////////////////////////////////////
101#include "SkPath.h"
102#include "SkPathMeasure.h"
103
104static 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);
reed7c70d7c2016-07-12 15:06:33 -0700109 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);
reed45561a02016-07-07 12:47:17 -0700115
116 for (int i = 0; i < count; ++i) {
reed7c70d7c2016-07-12 15:06:33 -0700117 // we want to position each character on the center of its advance
118 const SkScalar offset = SkScalarHalf(widths[i]);
reed45561a02016-07-07 12:47:17 -0700119 SkPoint pos;
120 SkVector tan;
reed7c70d7c2016-07-12 15:06:33 -0700121 if (!meas.getPosTan(xy[i].x() + offset, &pos, &tan)) {
reed45561a02016-07-07 12:47:17 -0700122 pos = xy[i];
123 tan.set(1, 0);
124 }
125 xform[i].fSCos = tan.x();
126 xform[i].fSSin = tan.y();
reed7c70d7c2016-07-12 15:06:33 -0700127 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;
reed45561a02016-07-07 12:47:17 -0700129 }
130
reed7c70d7c2016-07-12 15:06:33 -0700131 // 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 }
reed45561a02016-07-07 12:47:17 -0700144}
145
reed7c70d7c2016-07-12 15:06:33 -0700146DEF_SIMPLE_GM(drawTextRSXform, canvas, 860, 860) {
reeddde2b1f2016-07-08 03:31:09 -0700147 const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ";
reeddde2b1f2016-07-08 03:31:09 -0700148 const int N = sizeof(text0) - 1;
reed45561a02016-07-07 12:47:17 -0700149 SkPoint pos[N];
reed45561a02016-07-07 12:47:17 -0700150
151 SkPaint paint;
152 paint.setAntiAlias(true);
reed7c70d7c2016-07-12 15:06:33 -0700153 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 }
reed45561a02016-07-07 12:47:17 -0700160
161 SkPath path;
reed7c70d7c2016-07-12 15:06:33 -0700162 path.addOval(SkRect::MakeXYWH(160, 160, 540, 540));
reed45561a02016-07-07 12:47:17 -0700163
reed7c70d7c2016-07-12 15:06:33 -0700164 draw_text_on_path_rigid(canvas, text0, N, pos, path, paint);
reed45561a02016-07-07 12:47:17 -0700165
166 paint.setStyle(SkPaint::kStroke_Style);
167 canvas->drawPath(path, paint);
168}
169
170