blob: c0bc014c72c9df054fbf6ef49f3ae1d1968cbf46 [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"
Hal Canary95e3c052017-01-11 12:44:43 -05009
10#include "SkAutoMalloc.h"
reed71c3c762015-06-24 10:29:17 -070011#include "SkCanvas.h"
12#include "SkRSXform.h"
13#include "SkSurface.h"
Mike Reed46596ae2018-01-02 15:40:29 -050014#include "sk_tool_utils.h"
reed71c3c762015-06-24 10:29:17 -070015
16class DrawAtlasGM : public skiagm::GM {
reed9ce9d672016-03-17 10:51:11 -070017 static sk_sp<SkImage> MakeAtlas(SkCanvas* caller, const SkRect& target) {
reed71c3c762015-06-24 10:29:17 -070018 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
Cary Clarka24712e2018-09-05 18:41:40 +000019 auto surface(sk_tool_utils::makeSurface(caller, info));
reed71c3c762015-06-24 10:29:17 -070020 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);
Mike Reed7d954ad2016-10-28 15:42:34 -040092 canvas->drawAtlas(atlas.get(), xform, tex, colors, N, SkBlendMode::kSrcIn, 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///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Reed7d1eb332018-12-04 17:35:56 -0500101#include "SkFont.h"
102#include "SkFontPriv.h"
reed45561a02016-07-07 12:47:17 -0700103#include "SkPath.h"
104#include "SkPathMeasure.h"
105
Mike Reed488fbd12018-01-29 13:33:06 -0500106static void draw_text_on_path(SkCanvas* canvas, const void* text, size_t length,
107 const SkPoint xy[], const SkPath& path, const SkPaint& paint,
Mike Reed1eb9af92018-10-01 12:16:59 -0400108 float baseline_offset) {
reed45561a02016-07-07 12:47:17 -0700109 SkPathMeasure meas(path, false);
110
Mike Reed7d1eb332018-12-04 17:35:56 -0500111 SkFont font = SkFont::LEGACY_ExtractFromPaint(paint);
112
113 int count = font.countText(text, length, kUTF8_SkTextEncoding);
reed7c70d7c2016-07-12 15:06:33 -0700114 size_t size = count * (sizeof(SkRSXform) + sizeof(SkScalar));
115 SkAutoSMalloc<512> storage(size);
116 SkRSXform* xform = (SkRSXform*)storage.get();
117 SkScalar* widths = (SkScalar*)(xform + count);
118
reed7c70d7c2016-07-12 15:06:33 -0700119 // Compute a conservative bounds so we can cull the draw
Mike Reed7d1eb332018-12-04 17:35:56 -0500120 const SkRect fontb = SkFontPriv::GetFontBounds(font);
121 const SkScalar max = SkTMax(SkTMax(SkScalarAbs(fontb.fLeft), SkScalarAbs(fontb.fRight)),
122 SkTMax(SkScalarAbs(fontb.fTop), SkScalarAbs(fontb.fBottom)));
reed7c70d7c2016-07-12 15:06:33 -0700123 const SkRect bounds = path.getBounds().makeOutset(max, max);
124
Mike Reed1eb9af92018-10-01 12:16:59 -0400125 paint.getTextWidths(text, length, widths);
Mike Reed488fbd12018-01-29 13:33:06 -0500126
Mike Reed1eb9af92018-10-01 12:16:59 -0400127 for (int i = 0; i < count; ++i) {
128 // we want to position each character on the center of its advance
129 const SkScalar offset = SkScalarHalf(widths[i]);
130 SkPoint pos;
131 SkVector tan;
132 if (!meas.getPosTan(xy[i].x() + offset, &pos, &tan)) {
133 pos = xy[i];
134 tan.set(1, 0);
Mike Reed488fbd12018-01-29 13:33:06 -0500135 }
Mike Reed1eb9af92018-10-01 12:16:59 -0400136 pos += SkVector::Make(-tan.fY, tan.fX) * baseline_offset;
Mike Reed488fbd12018-01-29 13:33:06 -0500137
Mike Reed1eb9af92018-10-01 12:16:59 -0400138 xform[i].fSCos = tan.x();
139 xform[i].fSSin = tan.y();
140 xform[i].fTx = pos.x() - tan.y() * xy[i].y() - tan.x() * offset;
141 xform[i].fTy = pos.y() + tan.x() * xy[i].y() - tan.y() * offset;
Mike Reed488fbd12018-01-29 13:33:06 -0500142 }
reed7c70d7c2016-07-12 15:06:33 -0700143
Mike Reed1eb9af92018-10-01 12:16:59 -0400144 canvas->drawTextRSXform(text, length, &xform[0], &bounds, paint);
145
reed7c70d7c2016-07-12 15:06:33 -0700146 if (true) {
147 SkPaint p;
148 p.setStyle(SkPaint::kStroke_Style);
149 canvas->drawRect(bounds, p);
150 }
reed45561a02016-07-07 12:47:17 -0700151}
152
Mike Reed9c2916e2018-03-15 13:37:08 -0400153#include "SkGradientShader.h"
154static 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 Reed1eb9af92018-10-01 12:16:59 -0400160static void drawTextPath(SkCanvas* canvas, bool doStroke) {
reeddde2b1f2016-07-08 03:31:09 -0700161 const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ";
reeddde2b1f2016-07-08 03:31:09 -0700162 const int N = sizeof(text0) - 1;
reed45561a02016-07-07 12:47:17 -0700163 SkPoint pos[N];
reed45561a02016-07-07 12:47:17 -0700164
165 SkPaint paint;
Mike Reed9c2916e2018-03-15 13:37:08 -0400166 paint.setShader(make_shader());
reed45561a02016-07-07 12:47:17 -0700167 paint.setAntiAlias(true);
reed7c70d7c2016-07-12 15:06:33 -0700168 paint.setTextSize(100);
Mike Reedcaaf2112018-01-29 14:32:38 -0500169 if (doStroke) {
170 paint.setStyle(SkPaint::kStroke_Style);
171 paint.setStrokeWidth(2.25f);
172 paint.setStrokeJoin(SkPaint::kRound_Join);
173 }
reed7c70d7c2016-07-12 15:06:33 -0700174
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 }
reed45561a02016-07-07 12:47:17 -0700180
181 SkPath path;
Mike Reed488fbd12018-01-29 13:33:06 -0500182 const float baseline_offset = -5;
reed45561a02016-07-07 12:47:17 -0700183
Mike Reed488fbd12018-01-29 13:33:06 -0500184 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);
Mike Reed1eb9af92018-10-01 12:16:59 -0400190 draw_text_on_path(canvas, text0, N, pos, path, paint, baseline_offset);
Mike Reed488fbd12018-01-29 13:33:06 -0500191 }
reed45561a02016-07-07 12:47:17 -0700192
Mike Reedcaaf2112018-01-29 14:32:38 -0500193 paint.reset();
reed45561a02016-07-07 12:47:17 -0700194 paint.setStyle(SkPaint::kStroke_Style);
195 canvas->drawPath(path, paint);
196}
197
Mike Reed1eb9af92018-10-01 12:16:59 -0400198DEF_SIMPLE_GM(drawTextRSXform, canvas, 430, 860) {
Mike Reed488fbd12018-01-29 13:33:06 -0500199 canvas->scale(0.5f, 0.5f);
Mike Reedcaaf2112018-01-29 14:32:38 -0500200 const bool doStroke[] = { false, true };
201 for (auto st : doStroke) {
Mike Reed1eb9af92018-10-01 12:16:59 -0400202 drawTextPath(canvas, st);
Mike Reedcaaf2112018-01-29 14:32:38 -0500203 canvas->translate(0, 860);
204 }
Mike Reed488fbd12018-01-29 13:33:06 -0500205}
206
Mike Reed93cb2522017-04-28 10:02:47 -0400207#include "Resources.h"
208#include "SkColorFilter.h"
209#include "SkVertices.h"
reed45561a02016-07-07 12:47:17 -0700210
Mike Reed93cb2522017-04-28 10:02:47 -0400211static sk_sp<SkVertices> make_vertices(sk_sp<SkImage> image, const SkRect& r,
212 SkColor color) {
213 SkPoint pos[4];
214 r.toQuad(pos);
215 SkColor colors[4] = { color, color, color, color };
216 return SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4,
217 pos, pos, colors);
218}
219
220/*
221 * drawAtlas and drawVertices have several things in common:
222 * - can create compound "shaders", combining texture and colors
223 * - these are combined via an explicit blendmode
224 * - like drawImage, they only respect parts of the paint
225 * - colorfilter, imagefilter, blendmode, alpha
226 *
227 * This GM produces a series of pairs of images (atlas | vertices).
228 * Each pair should look the same, and each set shows a different combination
229 * of alpha | colorFilter | mode
230 */
231DEF_SIMPLE_GM(compare_atlas_vertices, canvas, 560, 585) {
232 const SkRect tex = SkRect::MakeWH(128, 128);
233 const SkRSXform xform = SkRSXform::Make(1, 0, 0, 0);
234 const SkColor color = 0x884488CC;
235
Hal Canaryc465d132017-12-08 10:21:31 -0500236 auto image = GetResourceAsImage("images/mandrill_128.png");
Mike Reed93cb2522017-04-28 10:02:47 -0400237 auto verts = make_vertices(image, tex, color);
238 const sk_sp<SkColorFilter> filters[] = {
239 nullptr,
240 SkColorFilter::MakeModeFilter(0xFF00FF88, SkBlendMode::kModulate),
241 };
242 const SkBlendMode modes[] = {
243 SkBlendMode::kSrcOver,
244 SkBlendMode::kPlus,
245 };
246
247 canvas->translate(10, 10);
248 SkPaint paint;
249 for (SkBlendMode mode : modes) {
250 for (int alpha : { 0xFF, 0x7F }) {
251 paint.setAlpha(alpha);
252 canvas->save();
253 for (auto cf : filters) {
254 paint.setColorFilter(cf);
255 canvas->drawAtlas(image, &xform, &tex, &color, 1,
256 mode, &tex, &paint);
257 canvas->translate(128, 0);
Mike Reed0acd7952017-04-28 11:12:19 -0400258 paint.setShader(image->makeShader());
Mike Reed93cb2522017-04-28 10:02:47 -0400259 canvas->drawVertices(verts, mode, paint);
260 paint.setShader(nullptr);
261 canvas->translate(145, 0);
262 }
263 canvas->restore();
264 canvas->translate(0, 145);
265 }
266 }
267}