blob: de44b6d7a700bf16608843bca014207ab9c3c4e7 [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"
14
15class DrawAtlasGM : public skiagm::GM {
reed9ce9d672016-03-17 10:51:11 -070016 static sk_sp<SkImage> MakeAtlas(SkCanvas* caller, const SkRect& target) {
reed71c3c762015-06-24 10:29:17 -070017 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
reede8f30622016-03-23 18:59:25 -070018 auto surface(caller->makeSurface(info));
halcanary96fcdcc2015-08-27 07:41:13 -070019 if (nullptr == surface) {
reede8f30622016-03-23 18:59:25 -070020 surface = SkSurface::MakeRaster(info);
reed71c3c762015-06-24 10:29:17 -070021 }
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;
reed374772b2016-10-05 17:33:02 -070028 paint.setBlendMode(SkBlendMode::kClear);
reed71c3c762015-06-24 10:29:17 -070029 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);
reed374772b2016-10-05 17:33:02 -070033 paint.setBlendMode(SkBlendMode::kSrcOver);
reed71c3c762015-06-24 10:29:17 -070034 paint.setColor(SK_ColorBLUE);
35 paint.setAntiAlias(true);
36 canvas->drawOval(target, paint);
reed9ce9d672016-03-17 10:51:11 -070037 return surface->makeImageSnapshot();
reed71c3c762015-06-24 10:29:17 -070038 }
39
reed71c3c762015-06-24 10:29:17 -070040public:
41 DrawAtlasGM() {}
halcanary9d524f22016-03-29 09:03:52 -070042
reed71c3c762015-06-24 10:29:17 -070043protected:
halcanary9d524f22016-03-29 09:03:52 -070044
reed71c3c762015-06-24 10:29:17 -070045 SkString onShortName() override {
46 return SkString("draw-atlas");
47 }
halcanary9d524f22016-03-29 09:03:52 -070048
reed71c3c762015-06-24 10:29:17 -070049 SkISize onISize() override {
50 return SkISize::Make(640, 480);
51 }
halcanary9d524f22016-03-29 09:03:52 -070052
reed71c3c762015-06-24 10:29:17 -070053 void onDraw(SkCanvas* canvas) override {
54 const SkRect target = { 50, 50, 80, 90 };
brianosman95e8d0a2016-09-29 13:43:49 -070055 auto atlas = MakeAtlas(canvas, target);
reed71c3c762015-06-24 10:29:17 -070056
57 const struct {
58 SkScalar fScale;
59 SkScalar fDegrees;
60 SkScalar fTx;
61 SkScalar fTy;
halcanary9d524f22016-03-29 09:03:52 -070062
reed71c3c762015-06-24 10:29:17 -070063 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
brianosman95e8d0a2016-09-29 13:43:49 -070092 canvas->drawAtlas(atlas.get(), xform, tex, N, nullptr, &paint);
reed71c3c762015-06-24 10:29:17 -070093 canvas->translate(0, 100);
Mike Reed7d954ad2016-10-28 15:42:34 -040094 canvas->drawAtlas(atlas.get(), xform, tex, colors, N, SkBlendMode::kSrcIn, nullptr, &paint);
reed71c3c762015-06-24 10:29:17 -070095 }
halcanary9d524f22016-03-29 09:03:52 -070096
reed71c3c762015-06-24 10:29:17 -070097private:
98 typedef GM INHERITED;
99};
100DEF_GM( return new DrawAtlasGM; )
reed45561a02016-07-07 12:47:17 -0700101
102///////////////////////////////////////////////////////////////////////////////////////////////////
103#include "SkPath.h"
104#include "SkPathMeasure.h"
105
106static 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);
reed7c70d7c2016-07-12 15:06:33 -0700111 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);
reed45561a02016-07-07 12:47:17 -0700117
118 for (int i = 0; i < count; ++i) {
reed7c70d7c2016-07-12 15:06:33 -0700119 // we want to position each character on the center of its advance
120 const SkScalar offset = SkScalarHalf(widths[i]);
reed45561a02016-07-07 12:47:17 -0700121 SkPoint pos;
122 SkVector tan;
reed7c70d7c2016-07-12 15:06:33 -0700123 if (!meas.getPosTan(xy[i].x() + offset, &pos, &tan)) {
reed45561a02016-07-07 12:47:17 -0700124 pos = xy[i];
125 tan.set(1, 0);
126 }
127 xform[i].fSCos = tan.x();
128 xform[i].fSSin = tan.y();
reed7c70d7c2016-07-12 15:06:33 -0700129 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;
reed45561a02016-07-07 12:47:17 -0700131 }
132
reed7c70d7c2016-07-12 15:06:33 -0700133 // 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 }
reed45561a02016-07-07 12:47:17 -0700146}
147
reed7c70d7c2016-07-12 15:06:33 -0700148DEF_SIMPLE_GM(drawTextRSXform, canvas, 860, 860) {
reeddde2b1f2016-07-08 03:31:09 -0700149 const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ";
reeddde2b1f2016-07-08 03:31:09 -0700150 const int N = sizeof(text0) - 1;
reed45561a02016-07-07 12:47:17 -0700151 SkPoint pos[N];
reed45561a02016-07-07 12:47:17 -0700152
153 SkPaint paint;
154 paint.setAntiAlias(true);
reed7c70d7c2016-07-12 15:06:33 -0700155 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 }
reed45561a02016-07-07 12:47:17 -0700162
163 SkPath path;
reed7c70d7c2016-07-12 15:06:33 -0700164 path.addOval(SkRect::MakeXYWH(160, 160, 540, 540));
reed45561a02016-07-07 12:47:17 -0700165
reed7c70d7c2016-07-12 15:06:33 -0700166 draw_text_on_path_rigid(canvas, text0, N, pos, path, paint);
reed45561a02016-07-07 12:47:17 -0700167
168 paint.setStyle(SkPaint::kStroke_Style);
169 canvas->drawPath(path, paint);
170}
171
Mike Reed93cb2522017-04-28 10:02:47 -0400172#include "Resources.h"
173#include "SkColorFilter.h"
174#include "SkVertices.h"
reed45561a02016-07-07 12:47:17 -0700175
Mike Reed93cb2522017-04-28 10:02:47 -0400176static sk_sp<SkVertices> make_vertices(sk_sp<SkImage> image, const SkRect& r,
177 SkColor color) {
178 SkPoint pos[4];
179 r.toQuad(pos);
180 SkColor colors[4] = { color, color, color, color };
181 return SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4,
182 pos, pos, colors);
183}
184
185/*
186 * drawAtlas and drawVertices have several things in common:
187 * - can create compound "shaders", combining texture and colors
188 * - these are combined via an explicit blendmode
189 * - like drawImage, they only respect parts of the paint
190 * - colorfilter, imagefilter, blendmode, alpha
191 *
192 * This GM produces a series of pairs of images (atlas | vertices).
193 * Each pair should look the same, and each set shows a different combination
194 * of alpha | colorFilter | mode
195 */
196DEF_SIMPLE_GM(compare_atlas_vertices, canvas, 560, 585) {
197 const SkRect tex = SkRect::MakeWH(128, 128);
198 const SkRSXform xform = SkRSXform::Make(1, 0, 0, 0);
199 const SkColor color = 0x884488CC;
200
201 auto image = GetResourceAsImage("mandrill_128.png");
202 auto verts = make_vertices(image, tex, color);
203 const sk_sp<SkColorFilter> filters[] = {
204 nullptr,
205 SkColorFilter::MakeModeFilter(0xFF00FF88, SkBlendMode::kModulate),
206 };
207 const SkBlendMode modes[] = {
208 SkBlendMode::kSrcOver,
209 SkBlendMode::kPlus,
210 };
211
212 canvas->translate(10, 10);
213 SkPaint paint;
214 for (SkBlendMode mode : modes) {
215 for (int alpha : { 0xFF, 0x7F }) {
216 paint.setAlpha(alpha);
217 canvas->save();
218 for (auto cf : filters) {
219 paint.setColorFilter(cf);
220 canvas->drawAtlas(image, &xform, &tex, &color, 1,
221 mode, &tex, &paint);
222 canvas->translate(128, 0);
Mike Reed0acd7952017-04-28 11:12:19 -0400223 paint.setShader(image->makeShader());
Mike Reed93cb2522017-04-28 10:02:47 -0400224 canvas->drawVertices(verts, mode, paint);
225 paint.setShader(nullptr);
226 canvas->translate(145, 0);
227 }
228 canvas->restore();
229 canvas->translate(0, 145);
230 }
231 }
232}