blob: 20ac9942b94c11bcee7125f7f7b22c2def40dcaa [file] [log] [blame]
chudy@google.com902ebe52012-06-29 14:21:22 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
chudy@google.com902ebe52012-06-29 14:21:22 +000010#include "SkDrawCommand.h"
ethannicholas50a8dd02016-02-10 05:40:46 -080011#include "SkBlurMaskFilter.h"
12#include "SkColorFilter.h"
13#include "SkDashPathEffect.h"
14#include "SkImageFilter.h"
15#include "SkMaskFilter.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000016#include "SkObjectParser.h"
ethannicholas50a8dd02016-02-10 05:40:46 -080017#include "SkPaintDefaults.h"
18#include "SkPathEffect.h"
robertphillips9bafc302015-02-13 11:13:00 -080019#include "SkPicture.h"
fmalitab7425172014-08-26 07:56:44 -070020#include "SkTextBlob.h"
fmalitae77f2da2015-12-08 18:59:18 -080021#include "SkTextBlobRunIterator.h"
ethannicholas50a8dd02016-02-10 05:40:46 -080022#include "SkTHash.h"
23#include "SkTypeface.h"
24#include "SkValidatingReadBuffer.h"
25#include "SkWriteBuffer.h"
26
27#define SKDEBUGCANVAS_SEND_BINARIES false
28
29#define SKDEBUGCANVAS_ATTRIBUTE_COMMAND "command"
30#define SKDEBUGCANVAS_ATTRIBUTE_MATRIX "matrix"
31#define SKDEBUGCANVAS_ATTRIBUTE_COORDS "coords"
32#define SKDEBUGCANVAS_ATTRIBUTE_BOUNDS "bounds"
33#define SKDEBUGCANVAS_ATTRIBUTE_PAINT "paint"
34#define SKDEBUGCANVAS_ATTRIBUTE_OUTER "outer"
35#define SKDEBUGCANVAS_ATTRIBUTE_INNER "inner"
36#define SKDEBUGCANVAS_ATTRIBUTE_MODE "mode"
37#define SKDEBUGCANVAS_ATTRIBUTE_POINTS "points"
38#define SKDEBUGCANVAS_ATTRIBUTE_PATH "path"
39#define SKDEBUGCANVAS_ATTRIBUTE_TEXT "text"
40#define SKDEBUGCANVAS_ATTRIBUTE_COLOR "color"
41#define SKDEBUGCANVAS_ATTRIBUTE_ALPHA "alpha"
42#define SKDEBUGCANVAS_ATTRIBUTE_STYLE "style"
43#define SKDEBUGCANVAS_ATTRIBUTE_STROKEWIDTH "strokeWidth"
44#define SKDEBUGCANVAS_ATTRIBUTE_STROKEMITER "strokeMiter"
ethannicholas1446a9a2016-02-10 14:05:02 -080045#define SKDEBUGCANVAS_ATTRIBUTE_STROKEJOIN "strokeJoin"
ethannicholas50a8dd02016-02-10 05:40:46 -080046#define SKDEBUGCANVAS_ATTRIBUTE_CAP "cap"
47#define SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS "antiAlias"
ethannicholas1446a9a2016-02-10 14:05:02 -080048#define SKDEBUGCANVAS_ATTRIBUTE_DITHER "dither"
ethannicholas50a8dd02016-02-10 05:40:46 -080049#define SKDEBUGCANVAS_ATTRIBUTE_REGION "region"
50#define SKDEBUGCANVAS_ATTRIBUTE_REGIONOP "op"
51#define SKDEBUGCANVAS_ATTRIBUTE_EDGESTYLE "edgeStyle"
52#define SKDEBUGCANVAS_ATTRIBUTE_DEVICEREGION "deviceRegion"
53#define SKDEBUGCANVAS_ATTRIBUTE_BLUR "blur"
54#define SKDEBUGCANVAS_ATTRIBUTE_SIGMA "sigma"
55#define SKDEBUGCANVAS_ATTRIBUTE_QUALITY "quality"
56#define SKDEBUGCANVAS_ATTRIBUTE_TEXTALIGN "textAlign"
57#define SKDEBUGCANVAS_ATTRIBUTE_TEXTSIZE "textSize"
58#define SKDEBUGCANVAS_ATTRIBUTE_TEXTSCALEX "textScaleX"
59#define SKDEBUGCANVAS_ATTRIBUTE_TEXTSKEWX "textSkewX"
60#define SKDEBUGCANVAS_ATTRIBUTE_DASHING "dashing"
61#define SKDEBUGCANVAS_ATTRIBUTE_INTERVALS "intervals"
62#define SKDEBUGCANVAS_ATTRIBUTE_PHASE "phase"
63#define SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE "fillType"
64#define SKDEBUGCANVAS_ATTRIBUTE_VERBS "verbs"
65#define SKDEBUGCANVAS_ATTRIBUTE_NAME "name"
66#define SKDEBUGCANVAS_ATTRIBUTE_BYTES "bytes"
67#define SKDEBUGCANVAS_ATTRIBUTE_SHADER "shader"
68#define SKDEBUGCANVAS_ATTRIBUTE_PATHEFFECT "pathEffect"
69#define SKDEBUGCANVAS_ATTRIBUTE_MASKFILTER "maskFilter"
70#define SKDEBUGCANVAS_ATTRIBUTE_XFERMODE "xfermode"
ethannicholas1446a9a2016-02-10 14:05:02 -080071#define SKDEBUGCANVAS_ATTRIBUTE_LOOPER "looper"
ethannicholas50a8dd02016-02-10 05:40:46 -080072#define SKDEBUGCANVAS_ATTRIBUTE_BACKDROP "backdrop"
73#define SKDEBUGCANVAS_ATTRIBUTE_COLORFILTER "colorfilter"
74#define SKDEBUGCANVAS_ATTRIBUTE_IMAGEFILTER "imagefilter"
75#define SKDEBUGCANVAS_ATTRIBUTE_IMAGE "image"
76#define SKDEBUGCANVAS_ATTRIBUTE_BITMAP "bitmap"
77#define SKDEBUGCANVAS_ATTRIBUTE_SRC "src"
78#define SKDEBUGCANVAS_ATTRIBUTE_DST "dst"
79#define SKDEBUGCANVAS_ATTRIBUTE_CENTER "center"
80#define SKDEBUGCANVAS_ATTRIBUTE_STRICT "strict"
81#define SKDEBUGCANVAS_ATTRIBUTE_DESCRIPTION "description"
82#define SKDEBUGCANVAS_ATTRIBUTE_X "x"
83#define SKDEBUGCANVAS_ATTRIBUTE_Y "y"
84#define SKDEBUGCANVAS_ATTRIBUTE_RUNS "runs"
85#define SKDEBUGCANVAS_ATTRIBUTE_POSITIONS "positions"
86#define SKDEBUGCANVAS_ATTRIBUTE_GLYPHS "glyphs"
87#define SKDEBUGCANVAS_ATTRIBUTE_FONT "font"
88#define SKDEBUGCANVAS_ATTRIBUTE_TYPEFACE "typeface"
ethannicholas1446a9a2016-02-10 14:05:02 -080089#define SKDEBUGCANVAS_ATTRIBUTE_CUBICS "cubics"
90#define SKDEBUGCANVAS_ATTRIBUTE_COLORS "colors"
91#define SKDEBUGCANVAS_ATTRIBUTE_TEXTURECOORDS "textureCoords"
92#define SKDEBUGCANVAS_ATTRIBUTE_FILTERQUALITY "filterQuality"
ethannicholas50a8dd02016-02-10 05:40:46 -080093
94#define SKDEBUGCANVAS_VERB_MOVE "move"
95#define SKDEBUGCANVAS_VERB_LINE "line"
96#define SKDEBUGCANVAS_VERB_QUAD "quad"
97#define SKDEBUGCANVAS_VERB_CUBIC "cubic"
98#define SKDEBUGCANVAS_VERB_CONIC "conic"
99#define SKDEBUGCANVAS_VERB_CLOSE "close"
100
101#define SKDEBUGCANVAS_STYLE_FILL "fill"
102#define SKDEBUGCANVAS_STYLE_STROKE "stroke"
103#define SKDEBUGCANVAS_STYLE_STROKEANDFILL "strokeAndFill"
104
105#define SKDEBUGCANVAS_POINTMODE_POINTS "points"
106#define SKDEBUGCANVAS_POINTMODE_LINES "lines"
107#define SKDEBUGCANVAS_POINTMODE_POLYGON "polygon"
108
109#define SKDEBUGCANVAS_REGIONOP_DIFFERENCE "difference"
110#define SKDEBUGCANVAS_REGIONOP_INTERSECT "intersect"
111#define SKDEBUGCANVAS_REGIONOP_UNION "union"
112#define SKDEBUGCANVAS_REGIONOP_XOR "xor"
113#define SKDEBUGCANVAS_REGIONOP_REVERSE_DIFFERENCE "reverseDifference"
114#define SKDEBUGCANVAS_REGIONOP_REPLACE "replace"
115
116#define SKDEBUGCANVAS_BLURSTYLE_NORMAL "normal"
117#define SKDEBUGCANVAS_BLURSTYLE_SOLID "solid"
118#define SKDEBUGCANVAS_BLURSTYLE_OUTER "outer"
119#define SKDEBUGCANVAS_BLURSTYLE_INNER "inner"
120
121#define SKDEBUGCANVAS_BLURQUALITY_LOW "low"
122#define SKDEBUGCANVAS_BLURQUALITY_HIGH "high"
123
124#define SKDEBUGCANVAS_ALIGN_LEFT "left"
125#define SKDEBUGCANVAS_ALIGN_CENTER "center"
126#define SKDEBUGCANVAS_ALIGN_RIGHT "right"
127
128#define SKDEBUGCANVAS_FILLTYPE_WINDING "winding"
129#define SKDEBUGCANVAS_FILLTYPE_EVENODD "evenOdd"
130#define SKDEBUGCANVAS_FILLTYPE_INVERSEWINDING "inverseWinding"
131#define SKDEBUGCANVAS_FILLTYPE_INVERSEEVENODD "inverseEvenOdd"
132
133#define SKDEBUGCANVAS_CAP_BUTT "butt"
134#define SKDEBUGCANVAS_CAP_ROUND "round"
135#define SKDEBUGCANVAS_CAP_SQUARE "square"
136
ethannicholas1446a9a2016-02-10 14:05:02 -0800137#define SKDEBUGCANVAS_MITER_JOIN "miter"
138#define SKDEBUGCANVAS_ROUND_JOIN "round"
139#define SKDEBUGCANVAS_BEVEL_JOIN "bevel"
140
ethannicholas50a8dd02016-02-10 05:40:46 -0800141#define SKDEBUGCANVAS_COLORTYPE_ARGB4444 "ARGB4444"
142#define SKDEBUGCANVAS_COLORTYPE_RGBA8888 "RGBA8888"
143#define SKDEBUGCANVAS_COLORTYPE_BGRA8888 "BGRA8888"
144#define SKDEBUGCANVAS_COLORTYPE_565 "565"
145#define SKDEBUGCANVAS_COLORTYPE_GRAY8 "Gray8"
146#define SKDEBUGCANVAS_COLORTYPE_INDEX8 "Index8"
147#define SKDEBUGCANVAS_COLORTYPE_ALPHA8 "Alpha8"
148
149#define SKDEBUGCANVAS_ALPHATYPE_OPAQUE "opaque"
150#define SKDEBUGCANVAS_ALPHATYPE_PREMUL "premul"
151#define SKDEBUGCANVAS_ALPHATYPE_UNPREMUL "unpremul"
152
ethannicholas1446a9a2016-02-10 14:05:02 -0800153#define SKDEBUGCANVAS_FILTERQUALITY_NONE "none"
154#define SKDEBUGCANVAS_FILTERQUALITY_LOW "low"
155#define SKDEBUGCANVAS_FILTERQUALITY_MEDIUM "medium"
156#define SKDEBUGCANVAS_FILTERQUALITY_HIGH "high"
157
ethannicholas50a8dd02016-02-10 05:40:46 -0800158typedef SkDrawCommand* (*FROM_JSON)(Json::Value);
fmalitab7425172014-08-26 07:56:44 -0700159
chudy@google.com902ebe52012-06-29 14:21:22 +0000160// TODO(chudy): Refactor into non subclass model.
161
robertphillips9bafc302015-02-13 11:13:00 -0800162SkDrawCommand::SkDrawCommand(OpType type)
163 : fOpType(type)
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000164 , fVisible(true) {
165}
166
chudy@google.com902ebe52012-06-29 14:21:22 +0000167SkDrawCommand::~SkDrawCommand() {
chudy@google.com97cee972012-08-07 20:41:37 +0000168 fInfo.deleteAll();
chudy@google.com902ebe52012-06-29 14:21:22 +0000169}
170
robertphillips9bafc302015-02-13 11:13:00 -0800171const char* SkDrawCommand::GetCommandString(OpType type) {
chudy@google.com902ebe52012-06-29 14:21:22 +0000172 switch (type) {
fmalita160ebb22015-04-01 20:58:37 -0700173 case kBeginDrawPicture_OpType: return "BeginDrawPicture";
robertphillips9bafc302015-02-13 11:13:00 -0800174 case kClipPath_OpType: return "ClipPath";
175 case kClipRegion_OpType: return "ClipRegion";
176 case kClipRect_OpType: return "ClipRect";
177 case kClipRRect_OpType: return "ClipRRect";
robertphillips9bafc302015-02-13 11:13:00 -0800178 case kConcat_OpType: return "Concat";
179 case kDrawBitmap_OpType: return "DrawBitmap";
180 case kDrawBitmapNine_OpType: return "DrawBitmapNine";
181 case kDrawBitmapRect_OpType: return "DrawBitmapRect";
182 case kDrawClear_OpType: return "DrawClear";
183 case kDrawDRRect_OpType: return "DrawDRRect";
fmalita651c9202015-07-22 10:23:01 -0700184 case kDrawImage_OpType: return "DrawImage";
185 case kDrawImageRect_OpType: return "DrawImageRect";
robertphillips9bafc302015-02-13 11:13:00 -0800186 case kDrawOval_OpType: return "DrawOval";
187 case kDrawPaint_OpType: return "DrawPaint";
188 case kDrawPatch_OpType: return "DrawPatch";
189 case kDrawPath_OpType: return "DrawPath";
robertphillips9bafc302015-02-13 11:13:00 -0800190 case kDrawPoints_OpType: return "DrawPoints";
191 case kDrawPosText_OpType: return "DrawPosText";
192 case kDrawPosTextH_OpType: return "DrawPosTextH";
193 case kDrawRect_OpType: return "DrawRect";
194 case kDrawRRect_OpType: return "DrawRRect";
robertphillips9bafc302015-02-13 11:13:00 -0800195 case kDrawText_OpType: return "DrawText";
196 case kDrawTextBlob_OpType: return "DrawTextBlob";
197 case kDrawTextOnPath_OpType: return "DrawTextOnPath";
198 case kDrawVertices_OpType: return "DrawVertices";
fmalita160ebb22015-04-01 20:58:37 -0700199 case kEndDrawPicture_OpType: return "EndDrawPicture";
robertphillips9bafc302015-02-13 11:13:00 -0800200 case kRestore_OpType: return "Restore";
201 case kSave_OpType: return "Save";
202 case kSaveLayer_OpType: return "SaveLayer";
203 case kSetMatrix_OpType: return "SetMatrix";
chudy@google.com902ebe52012-06-29 14:21:22 +0000204 default:
robertphillips9bafc302015-02-13 11:13:00 -0800205 SkDebugf("OpType error 0x%08x\n", type);
chudy@google.com902ebe52012-06-29 14:21:22 +0000206 SkASSERT(0);
207 break;
208 }
209 SkDEBUGFAIL("DrawType UNUSED\n");
halcanary96fcdcc2015-08-27 07:41:13 -0700210 return nullptr;
chudy@google.com902ebe52012-06-29 14:21:22 +0000211}
212
fmalita8c89c522014-11-08 16:18:56 -0800213SkString SkDrawCommand::toString() const {
robertphillips9bafc302015-02-13 11:13:00 -0800214 return SkString(GetCommandString(fOpType));
chudy@google.com902ebe52012-06-29 14:21:22 +0000215}
216
ethannicholas50a8dd02016-02-10 05:40:46 -0800217Json::Value SkDrawCommand::toJSON() const {
218 Json::Value result;
219 result[SKDEBUGCANVAS_ATTRIBUTE_COMMAND] = this->GetCommandString(fOpType);
220 return result;
221}
222
223#define INSTALL_FACTORY(name) factories.set(SkString(GetCommandString(k ## name ##_OpType)), \
224 (FROM_JSON) Sk ## name ## Command::fromJSON)
225SkDrawCommand* SkDrawCommand::fromJSON(Json::Value& command) {
226 static SkTHashMap<SkString, FROM_JSON> factories;
227 static bool initialized = false;
228 if (!initialized) {
229 initialized = true;
230 INSTALL_FACTORY(Restore);
231 INSTALL_FACTORY(ClipPath);
232 INSTALL_FACTORY(ClipRegion);
233 INSTALL_FACTORY(ClipRect);
234 INSTALL_FACTORY(ClipRRect);
235 INSTALL_FACTORY(Concat);
236 INSTALL_FACTORY(DrawBitmap);
237 INSTALL_FACTORY(DrawBitmapRect);
238 INSTALL_FACTORY(DrawBitmapNine);
239 INSTALL_FACTORY(DrawImage);
240 INSTALL_FACTORY(DrawImageRect);
241 INSTALL_FACTORY(DrawOval);
242 INSTALL_FACTORY(DrawPaint);
243 INSTALL_FACTORY(DrawPath);
244 INSTALL_FACTORY(DrawPoints);
245 INSTALL_FACTORY(DrawText);
246 INSTALL_FACTORY(DrawPosText);
247 INSTALL_FACTORY(DrawTextOnPath);
248 INSTALL_FACTORY(DrawTextBlob);
249
250 INSTALL_FACTORY(DrawRect);
251 INSTALL_FACTORY(DrawRRect);
252 INSTALL_FACTORY(DrawDRRect);
ethannicholas1446a9a2016-02-10 14:05:02 -0800253 INSTALL_FACTORY(DrawPatch);
ethannicholas50a8dd02016-02-10 05:40:46 -0800254 INSTALL_FACTORY(Save);
255 INSTALL_FACTORY(SaveLayer);
256 INSTALL_FACTORY(SetMatrix);
257 }
258 SkString name = SkString(command[SKDEBUGCANVAS_ATTRIBUTE_COMMAND].asCString());
259 FROM_JSON* factory = factories.find(name);
260 if (factory == nullptr) {
261 SkDebugf("no JSON factory for '%s'\n", name.c_str());
262 return nullptr;
263 }
264 return (*factory)(command);
265}
266
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +0000267namespace {
268
269void xlate_and_scale_to_bounds(SkCanvas* canvas, const SkRect& bounds) {
270 const SkISize& size = canvas->getDeviceSize();
271
272 static const SkScalar kInsetFrac = 0.9f; // Leave a border around object
273
274 canvas->translate(size.fWidth/2.0f, size.fHeight/2.0f);
275 if (bounds.width() > bounds.height()) {
276 canvas->scale(SkDoubleToScalar((kInsetFrac*size.fWidth)/bounds.width()),
277 SkDoubleToScalar((kInsetFrac*size.fHeight)/bounds.width()));
278 } else {
279 canvas->scale(SkDoubleToScalar((kInsetFrac*size.fWidth)/bounds.height()),
280 SkDoubleToScalar((kInsetFrac*size.fHeight)/bounds.height()));
281 }
282 canvas->translate(-bounds.centerX(), -bounds.centerY());
283}
skia.committer@gmail.coma0090832013-06-07 07:01:06 +0000284
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +0000285
286void render_path(SkCanvas* canvas, const SkPath& path) {
287 canvas->clear(0xFFFFFFFF);
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +0000288
289 const SkRect& bounds = path.getBounds();
fmalitab0cd8b72015-10-06 07:24:03 -0700290 if (bounds.isEmpty()) {
291 return;
292 }
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +0000293
fmalitab0cd8b72015-10-06 07:24:03 -0700294 SkAutoCanvasRestore acr(canvas, true);
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +0000295 xlate_and_scale_to_bounds(canvas, bounds);
296
297 SkPaint p;
298 p.setColor(SK_ColorBLACK);
299 p.setStyle(SkPaint::kStroke_Style);
300
301 canvas->drawPath(path, p);
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +0000302}
303
halcanary96fcdcc2015-08-27 07:41:13 -0700304void render_bitmap(SkCanvas* canvas, const SkBitmap& input, const SkRect* srcRect = nullptr) {
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +0000305 const SkISize& size = canvas->getDeviceSize();
306
307 SkScalar xScale = SkIntToScalar(size.fWidth-2) / input.width();
308 SkScalar yScale = SkIntToScalar(size.fHeight-2) / input.height();
309
310 if (input.width() > input.height()) {
311 yScale *= input.height() / (float) input.width();
312 } else {
313 xScale *= input.width() / (float) input.height();
314 }
315
316 SkRect dst = SkRect::MakeXYWH(SK_Scalar1, SK_Scalar1,
317 xScale * input.width(),
318 yScale * input.height());
skia.committer@gmail.coma0090832013-06-07 07:01:06 +0000319
robertphillips96a5cff2015-09-24 06:56:27 -0700320 static const int kNumBlocks = 8;
321
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +0000322 canvas->clear(0xFFFFFFFF);
robertphillips96a5cff2015-09-24 06:56:27 -0700323 SkISize block = {
324 canvas->imageInfo().width()/kNumBlocks,
325 canvas->imageInfo().height()/kNumBlocks
326 };
327 for (int y = 0; y < kNumBlocks; ++y) {
328 for (int x = 0; x < kNumBlocks; ++x) {
329 SkPaint paint;
330 paint.setColor((x+y)%2 ? SK_ColorLTGRAY : SK_ColorDKGRAY);
331 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x*block.width()),
332 SkIntToScalar(y*block.height()),
333 SkIntToScalar(block.width()),
334 SkIntToScalar(block.height()));
335 canvas->drawRect(r, paint);
336 }
337 }
338
reede47829b2015-08-06 10:02:53 -0700339 canvas->drawBitmapRect(input, dst, nullptr);
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +0000340
bsalomon49f085d2014-09-05 13:34:00 -0700341 if (srcRect) {
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +0000342 SkRect r = SkRect::MakeLTRB(srcRect->fLeft * xScale + SK_Scalar1,
343 srcRect->fTop * yScale + SK_Scalar1,
344 srcRect->fRight * xScale + SK_Scalar1,
345 srcRect->fBottom * yScale + SK_Scalar1);
346 SkPaint p;
347 p.setColor(SK_ColorRED);
348 p.setStyle(SkPaint::kStroke_Style);
349
350 canvas->drawRect(r, p);
351 }
352}
353
354void render_rrect(SkCanvas* canvas, const SkRRect& rrect) {
355 canvas->clear(0xFFFFFFFF);
356 canvas->save();
357
358 const SkRect& bounds = rrect.getBounds();
359
360 xlate_and_scale_to_bounds(canvas, bounds);
361
362 SkPaint p;
363 p.setColor(SK_ColorBLACK);
364 p.setStyle(SkPaint::kStroke_Style);
365
366 canvas->drawRRect(rrect, p);
367 canvas->restore();
368}
skia.committer@gmail.coma0090832013-06-07 07:01:06 +0000369
commit-bot@chromium.org3d305202014-02-24 17:28:55 +0000370void render_drrect(SkCanvas* canvas, const SkRRect& outer, const SkRRect& inner) {
371 canvas->clear(0xFFFFFFFF);
372 canvas->save();
373
374 const SkRect& bounds = outer.getBounds();
375
376 xlate_and_scale_to_bounds(canvas, bounds);
377
378 SkPaint p;
379 p.setColor(SK_ColorBLACK);
380 p.setStyle(SkPaint::kStroke_Style);
381
382 canvas->drawDRRect(outer, inner, p);
383 canvas->restore();
384}
385
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +0000386};
387
ethannicholas1446a9a2016-02-10 14:05:02 -0800388static Json::Value make_json_color(const SkColor color) {
389 Json::Value result(Json::arrayValue);
390 result.append(Json::Value(SkColorGetA(color)));
391 result.append(Json::Value(SkColorGetR(color)));
392 result.append(Json::Value(SkColorGetG(color)));
393 result.append(Json::Value(SkColorGetB(color)));
394 return result;
395}
396
397
ethannicholas50a8dd02016-02-10 05:40:46 -0800398static Json::Value make_json_point(const SkPoint& point) {
399 Json::Value result(Json::arrayValue);
400 result.append(Json::Value(point.x()));
401 result.append(Json::Value(point.y()));
402 return result;
403}
404
405static Json::Value make_json_point(SkScalar x, SkScalar y) {
406 Json::Value result(Json::arrayValue);
407 result.append(Json::Value(x));
408 result.append(Json::Value(y));
409 return result;
410}
411
412static Json::Value make_json_rect(const SkRect& rect) {
413 Json::Value result(Json::arrayValue);
414 result.append(Json::Value(rect.left()));
415 result.append(Json::Value(rect.top()));
416 result.append(Json::Value(rect.right()));
417 result.append(Json::Value(rect.bottom()));
418 return result;
419}
420
421static Json::Value make_json_irect(const SkIRect& rect) {
422 Json::Value result(Json::arrayValue);
423 result.append(Json::Value(rect.left()));
424 result.append(Json::Value(rect.top()));
425 result.append(Json::Value(rect.right()));
426 result.append(Json::Value(rect.bottom()));
427 return result;
428}
429
430static Json::Value make_json_rrect(const SkRRect& rrect) {
431 Json::Value result(Json::arrayValue);
432 result.append(make_json_rect(rrect.rect()));
433 result.append(make_json_point(rrect.radii(SkRRect::kUpperLeft_Corner)));
434 result.append(make_json_point(rrect.radii(SkRRect::kUpperRight_Corner)));
435 result.append(make_json_point(rrect.radii(SkRRect::kLowerRight_Corner)));
436 result.append(make_json_point(rrect.radii(SkRRect::kLowerLeft_Corner)));
437 return result;
438}
439
440static Json::Value make_json_matrix(const SkMatrix& matrix) {
441 Json::Value result(Json::arrayValue);
442 Json::Value row1(Json::arrayValue);
443 row1.append(Json::Value(matrix[0]));
444 row1.append(Json::Value(matrix[1]));
445 row1.append(Json::Value(matrix[2]));
446 result.append(row1);
447 Json::Value row2(Json::arrayValue);
448 row2.append(Json::Value(matrix[3]));
449 row2.append(Json::Value(matrix[4]));
450 row2.append(Json::Value(matrix[5]));
451 result.append(row2);
452 Json::Value row3(Json::arrayValue);
453 row3.append(Json::Value(matrix[6]));
454 row3.append(Json::Value(matrix[7]));
455 row3.append(Json::Value(matrix[8]));
456 result.append(row3);
457 return result;
458}
ethannicholas1446a9a2016-02-10 14:05:02 -0800459
ethannicholas50a8dd02016-02-10 05:40:46 -0800460static Json::Value make_json_path(const SkPath& path) {
461 Json::Value result(Json::objectValue);
462 switch (path.getFillType()) {
463 case SkPath::kWinding_FillType:
464 result[SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE] = SKDEBUGCANVAS_FILLTYPE_WINDING;
465 break;
466 case SkPath::kEvenOdd_FillType:
467 result[SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE] = SKDEBUGCANVAS_FILLTYPE_EVENODD;
468 break;
469 case SkPath::kInverseWinding_FillType:
470 result[SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE] = SKDEBUGCANVAS_FILLTYPE_INVERSEWINDING;
471 break;
472 case SkPath::kInverseEvenOdd_FillType:
473 result[SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE] = SKDEBUGCANVAS_FILLTYPE_INVERSEEVENODD;
474 break;
475 }
476 Json::Value verbs(Json::arrayValue);
477 SkPath::Iter iter(path, false);
478 SkPoint pts[4];
479 SkPath::Verb verb;
480 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
481 switch (verb) {
482 case SkPath::kLine_Verb: {
483 Json::Value line(Json::objectValue);
484 line[SKDEBUGCANVAS_VERB_LINE] = make_json_point(pts[1]);
485 verbs.append(line);
486 break;
487 }
488 case SkPath::kQuad_Verb: {
489 Json::Value quad(Json::objectValue);
490 Json::Value coords(Json::arrayValue);
491 coords.append(make_json_point(pts[1]));
492 coords.append(make_json_point(pts[2]));
493 quad[SKDEBUGCANVAS_VERB_QUAD] = coords;
494 verbs.append(quad);
495 break;
496 }
497 case SkPath::kCubic_Verb: {
498 Json::Value cubic(Json::objectValue);
499 Json::Value coords(Json::arrayValue);
500 coords.append(make_json_point(pts[1]));
501 coords.append(make_json_point(pts[2]));
502 coords.append(make_json_point(pts[3]));
503 cubic[SKDEBUGCANVAS_VERB_CUBIC] = coords;
504 verbs.append(cubic);
505 break;
506 }
507 case SkPath::kConic_Verb: {
508 Json::Value conic(Json::objectValue);
509 Json::Value coords(Json::arrayValue);
510 coords.append(make_json_point(pts[1]));
511 coords.append(make_json_point(pts[2]));
512 coords.append(Json::Value(iter.conicWeight()));
513 conic[SKDEBUGCANVAS_VERB_CONIC] = coords;
514 verbs.append(conic);
515 break;
516 }
517 case SkPath::kMove_Verb: {
518 Json::Value move(Json::objectValue);
519 move[SKDEBUGCANVAS_VERB_MOVE] = make_json_point(pts[0]);
520 verbs.append(move);
521 break;
522 }
523 case SkPath::kClose_Verb:
524 verbs.append(Json::Value(SKDEBUGCANVAS_VERB_CLOSE));
525 break;
526 case SkPath::kDone_Verb:
527 break;
528 }
529 }
530 result[SKDEBUGCANVAS_ATTRIBUTE_VERBS] = verbs;
531 return result;
532}
533
534static Json::Value make_json_region(const SkRegion& region) {
535 return Json::Value("<unimplemented>");
536}
537
538static Json::Value make_json_regionop(SkRegion::Op op) {
539 switch (op) {
540 case SkRegion::kDifference_Op:
541 return Json::Value(SKDEBUGCANVAS_REGIONOP_DIFFERENCE);
542 case SkRegion::kIntersect_Op:
543 return Json::Value(SKDEBUGCANVAS_REGIONOP_INTERSECT);
544 case SkRegion::kUnion_Op:
545 return Json::Value(SKDEBUGCANVAS_REGIONOP_UNION);
546 case SkRegion::kXOR_Op:
547 return Json::Value(SKDEBUGCANVAS_REGIONOP_XOR);
548 case SkRegion::kReverseDifference_Op:
549 return Json::Value(SKDEBUGCANVAS_REGIONOP_REVERSE_DIFFERENCE);
550 case SkRegion::kReplace_Op:
551 return Json::Value(SKDEBUGCANVAS_REGIONOP_REPLACE);
552 default:
553 SkASSERT(false);
554 return Json::Value("<invalid region op>");
555 };
556}
557
558static Json::Value make_json_pointmode(SkCanvas::PointMode mode) {
559 switch (mode) {
560 case SkCanvas::kPoints_PointMode:
561 return Json::Value(SKDEBUGCANVAS_POINTMODE_POINTS);
562 case SkCanvas::kLines_PointMode:
563 return Json::Value(SKDEBUGCANVAS_POINTMODE_LINES);
564 case SkCanvas::kPolygon_PointMode:
565 return Json::Value(SKDEBUGCANVAS_POINTMODE_POLYGON);
566 default:
567 SkASSERT(false);
568 return Json::Value("<invalid point mode>");
569 };
570}
571
ethannicholasbd3dae82016-02-10 12:10:00 -0800572static void store_scalar(Json::Value* target, const char* key, SkScalar value,
573 SkScalar defaultValue) {
ethannicholas50a8dd02016-02-10 05:40:46 -0800574 if (value != defaultValue) {
575 (*target)[key] = Json::Value(value);
576 }
577}
578
ethannicholasbd3dae82016-02-10 12:10:00 -0800579static void store_bool(Json::Value* target, const char* key, bool value, bool defaultValue) {
ethannicholas50a8dd02016-02-10 05:40:46 -0800580 if (value != defaultValue) {
581 (*target)[key] = Json::Value(value);
582 }
583}
584
585static void encode_data(const void* data, size_t count, Json::Value* target) {
586 // just use a brain-dead JSON array for now, switch to base64 or something else smarter down the
587 // road
588 for (size_t i = 0; i < count; i++) {
589 target->append(((const uint8_t*)data)[i]);
590 }
591}
592
593static void flatten(const SkFlattenable* flattenable, Json::Value* target, bool sendBinaries) {
594 if (sendBinaries) {
595 SkWriteBuffer buffer;
596 flattenable->flatten(buffer);
597 void* data = sk_malloc_throw(buffer.bytesWritten());
598 buffer.writeToMemory(data);
599 Json::Value bytes;
600 encode_data(data, buffer.bytesWritten(), &bytes);
601 Json::Value jsonFlattenable;
602 jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_NAME] = Json::Value(flattenable->getTypeName());
603 jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_BYTES] = bytes;
604 (*target) = jsonFlattenable;
605 sk_free(data);
606 } else {
607 (*target)[SKDEBUGCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(flattenable->getTypeName());
608 }
609}
610
611static bool SK_WARN_UNUSED_RESULT flatten(const SkImage& image, Json::Value* target,
612 bool sendBinaries) {
613 if (sendBinaries) {
614 SkData* encoded = image.encode(SkImageEncoder::kPNG_Type, 100);
615 if (encoded == nullptr) {
616 // PNG encode doesn't necessarily support all color formats, convert to a different
617 // format
618 size_t rowBytes = 4 * image.width();
619 void* buffer = sk_malloc_throw(rowBytes * image.height());
620 SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(),
621 kN32_SkColorType, kPremul_SkAlphaType);
622 if (!image.readPixels(dstInfo, buffer, rowBytes, 0, 0)) {
623 SkDebugf("readPixels failed\n");
624 return false;
625 }
626 SkImage* converted = SkImage::NewRasterCopy(dstInfo, buffer, rowBytes);
627 encoded = converted->encode(SkImageEncoder::kPNG_Type, 100);
628 if (encoded == nullptr) {
629 SkDebugf("image encode failed\n");
630 return false;
631 }
632 sk_free(converted);
633 sk_free(buffer);
634 }
635 Json::Value bytes;
636 encode_data(encoded->data(), encoded->size(), &bytes);
637 (*target)[SKDEBUGCANVAS_ATTRIBUTE_BYTES] = bytes;
638 encoded->unref();
639 } else {
640 SkString description = SkStringPrintf("%dx%d pixel image", image.width(), image.height());
641 (*target)[SKDEBUGCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(description.c_str());
642 }
643 return true;
644}
645
646static const char* color_type_name(SkColorType colorType) {
647 switch (colorType) {
648 case kARGB_4444_SkColorType:
649 return SKDEBUGCANVAS_COLORTYPE_ARGB4444;
650 case kRGBA_8888_SkColorType:
651 return SKDEBUGCANVAS_COLORTYPE_RGBA8888;
652 case kBGRA_8888_SkColorType:
653 return SKDEBUGCANVAS_COLORTYPE_BGRA8888;
654 case kRGB_565_SkColorType:
655 return SKDEBUGCANVAS_COLORTYPE_565;
656 case kGray_8_SkColorType:
657 return SKDEBUGCANVAS_COLORTYPE_GRAY8;
658 case kIndex_8_SkColorType:
659 return SKDEBUGCANVAS_COLORTYPE_INDEX8;
660 case kAlpha_8_SkColorType:
661 return SKDEBUGCANVAS_COLORTYPE_ALPHA8;
662 default:
663 SkASSERT(false);
664 return SKDEBUGCANVAS_COLORTYPE_RGBA8888;
665 }
666}
667
668static const char* alpha_type_name(SkAlphaType alphaType) {
669 switch (alphaType) {
670 case kOpaque_SkAlphaType:
671 return SKDEBUGCANVAS_ALPHATYPE_OPAQUE;
672 case kPremul_SkAlphaType:
673 return SKDEBUGCANVAS_ALPHATYPE_PREMUL;
674 case kUnpremul_SkAlphaType:
675 return SKDEBUGCANVAS_ALPHATYPE_UNPREMUL;
676 default:
677 SkASSERT(false);
678 return SKDEBUGCANVAS_ALPHATYPE_OPAQUE;
679 }
680}
681
682// note that the caller is responsible for freeing the pointer
683static Json::ArrayIndex decode_data(Json::Value bytes, void** target) {
684 Json::ArrayIndex size = bytes.size();
685 *target = sk_malloc_throw(size);
686 for (Json::ArrayIndex i = 0; i < size; i++) {
687 ((uint8_t*) *target)[i] = bytes[i].asInt();
688 }
689 return size;
690}
691
692static SkFlattenable* load_flattenable(Json::Value jsonFlattenable) {
693 if (!jsonFlattenable.isMember(SKDEBUGCANVAS_ATTRIBUTE_NAME)) {
694 return nullptr;
695 }
696 const char* name = jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_NAME].asCString();
697 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(name);
698 if (factory == nullptr) {
699 SkDebugf("no factory for loading '%s'\n", name);
700 return nullptr;
701 }
702 void* data;
703 int size = decode_data(jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_BYTES], &data);
704 SkValidatingReadBuffer buffer(data, size);
705 SkFlattenable* result = factory(buffer);
706 sk_free(data);
707 if (!buffer.isValid()) {
708 SkDebugf("invalid buffer loading flattenable\n");
709 return nullptr;
710 }
711 return result;
712}
713
714static SkColorType colortype_from_name(const char* name) {
715 if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_ARGB4444)) {
716 return kARGB_4444_SkColorType;
717 }
718 else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_RGBA8888)) {
719 return kRGBA_8888_SkColorType;
720 }
721 else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_BGRA8888)) {
722 return kBGRA_8888_SkColorType;
723 }
724 else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_565)) {
725 return kRGB_565_SkColorType;
726 }
727 else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_GRAY8)) {
728 return kGray_8_SkColorType;
729 }
730 else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_INDEX8)) {
731 return kIndex_8_SkColorType;
732 }
733 else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_ALPHA8)) {
734 return kAlpha_8_SkColorType;
735 }
736 SkASSERT(false);
737 return kN32_SkColorType;
738}
739
740static SkBitmap* convert_colortype(SkBitmap* bitmap, SkColorType colorType) {
741 if (bitmap->colorType() == colorType ) {
742 return bitmap;
743 }
744 SkBitmap* dst = new SkBitmap();
745 if (bitmap->copyTo(dst, colorType)) {
746 delete bitmap;
747 return dst;
748 }
749 SkASSERT(false);
750 delete dst;
751 return bitmap;
752}
753
754// caller is responsible for freeing return value
755static SkBitmap* load_bitmap(const Json::Value& jsonBitmap) {
756 if (!jsonBitmap.isMember(SKDEBUGCANVAS_ATTRIBUTE_BYTES)) {
757 SkDebugf("invalid bitmap\n");
758 return nullptr;
759 }
760 void* data;
761 int size = decode_data(jsonBitmap[SKDEBUGCANVAS_ATTRIBUTE_BYTES], &data);
762 SkMemoryStream stream(data, size);
763 SkImageDecoder* decoder = SkImageDecoder::Factory(&stream);
764 SkBitmap* bitmap = new SkBitmap();
765 SkImageDecoder::Result result = decoder->decode(&stream, bitmap,
766 SkImageDecoder::kDecodePixels_Mode);
767 sk_free(decoder);
768 if (result != SkImageDecoder::kFailure) {
769 sk_free(data);
770 if (jsonBitmap.isMember(SKDEBUGCANVAS_ATTRIBUTE_COLOR)) {
771 const char* ctName = jsonBitmap[SKDEBUGCANVAS_ATTRIBUTE_COLOR].asCString();
772 SkColorType ct = colortype_from_name(ctName);
773 if (ct != kIndex_8_SkColorType) {
774 bitmap = convert_colortype(bitmap, ct);
775 }
776 }
777 return bitmap;
778 }
779 SkDebugf("image decode failed\n");
780 sk_free(data);
781 return nullptr;
782}
783
784static SkImage* load_image(const Json::Value& jsonImage) {
785 SkBitmap* bitmap = load_bitmap(jsonImage);
786 if (bitmap == nullptr) {
787 return nullptr;
788 }
789 SkImage* result = SkImage::NewFromBitmap(*bitmap);
790 delete bitmap;
791 return result;
792}
793
794static bool SK_WARN_UNUSED_RESULT flatten(const SkBitmap& bitmap, Json::Value* target,
795 bool sendBinaries) {
796 bitmap.lockPixels();
797 SkAutoTUnref<SkImage> image(SkImage::NewFromBitmap(bitmap));
798 bitmap.unlockPixels();
799 (*target)[SKDEBUGCANVAS_ATTRIBUTE_COLOR] = Json::Value(color_type_name(bitmap.colorType()));
800 (*target)[SKDEBUGCANVAS_ATTRIBUTE_ALPHA] = Json::Value(alpha_type_name(bitmap.alphaType()));
801 bool success = flatten(*image, target, sendBinaries);
802 return success;
803}
804
805static void apply_paint_color(const SkPaint& paint, Json::Value* target) {
806 SkColor color = paint.getColor();
807 if (color != SK_ColorBLACK) {
808 Json::Value colorValue(Json::arrayValue);
809 colorValue.append(Json::Value(SkColorGetA(color)));
810 colorValue.append(Json::Value(SkColorGetR(color)));
811 colorValue.append(Json::Value(SkColorGetG(color)));
812 colorValue.append(Json::Value(SkColorGetB(color)));
813 (*target)[SKDEBUGCANVAS_ATTRIBUTE_COLOR] = colorValue;;
814 }
815}
816
817static void apply_paint_style(const SkPaint& paint, Json::Value* target) {
818 SkPaint::Style style = paint.getStyle();
819 if (style != SkPaint::kFill_Style) {
820 switch (style) {
821 case SkPaint::kStroke_Style: {
822 Json::Value stroke(SKDEBUGCANVAS_STYLE_STROKE);
823 (*target)[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = stroke;
824 break;
825 }
826 case SkPaint::kStrokeAndFill_Style: {
827 Json::Value strokeAndFill(SKDEBUGCANVAS_STYLE_STROKEANDFILL);
828 (*target)[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = strokeAndFill;
829 break;
830 }
831 default: SkASSERT(false);
832 }
833 }
834}
835
836static void apply_paint_cap(const SkPaint& paint, Json::Value* target) {
837 SkPaint::Cap cap = paint.getStrokeCap();
838 if (cap != SkPaint::kDefault_Cap) {
839 switch (cap) {
ethannicholas1446a9a2016-02-10 14:05:02 -0800840 case SkPaint::kButt_Cap:
ethannicholas50a8dd02016-02-10 05:40:46 -0800841 (*target)[SKDEBUGCANVAS_ATTRIBUTE_CAP] = Json::Value(SKDEBUGCANVAS_CAP_BUTT);
842 break;
ethannicholas1446a9a2016-02-10 14:05:02 -0800843 case SkPaint::kRound_Cap:
ethannicholas50a8dd02016-02-10 05:40:46 -0800844 (*target)[SKDEBUGCANVAS_ATTRIBUTE_CAP] = Json::Value(SKDEBUGCANVAS_CAP_ROUND);
845 break;
ethannicholas1446a9a2016-02-10 14:05:02 -0800846 case SkPaint::kSquare_Cap:
ethannicholas50a8dd02016-02-10 05:40:46 -0800847 (*target)[SKDEBUGCANVAS_ATTRIBUTE_CAP] = Json::Value(SKDEBUGCANVAS_CAP_SQUARE);
848 break;
ethannicholas50a8dd02016-02-10 05:40:46 -0800849 default: SkASSERT(false);
850 }
851 }
852}
ethannicholas1446a9a2016-02-10 14:05:02 -0800853
854static void apply_paint_join(const SkPaint& paint, Json::Value* target) {
855 SkPaint::Join join = paint.getStrokeJoin();
856 if (join != SkPaint::kDefault_Join) {
857 switch (join) {
858 case SkPaint::kMiter_Join:
859 (*target)[SKDEBUGCANVAS_ATTRIBUTE_STROKEJOIN] = Json::Value(
860 SKDEBUGCANVAS_MITER_JOIN);
861 break;
862 case SkPaint::kRound_Join:
863 (*target)[SKDEBUGCANVAS_ATTRIBUTE_STROKEJOIN] = Json::Value(
864 SKDEBUGCANVAS_ROUND_JOIN);
865 break;
866 case SkPaint::kBevel_Join:
867 (*target)[SKDEBUGCANVAS_ATTRIBUTE_STROKEJOIN] = Json::Value(
868 SKDEBUGCANVAS_BEVEL_JOIN);
869 break;
870 default: SkASSERT(false);
871 }
872 }
873}
874
875static void apply_paint_filterquality(const SkPaint& paint, Json::Value* target) {
876 SkFilterQuality quality = paint.getFilterQuality();
877 switch (quality) {
878 case kNone_SkFilterQuality:
879 break;
880 case kLow_SkFilterQuality:
881 (*target)[SKDEBUGCANVAS_ATTRIBUTE_FILTERQUALITY] = Json::Value(
882 SKDEBUGCANVAS_FILTERQUALITY_LOW);
883 break;
884 case kMedium_SkFilterQuality:
885 (*target)[SKDEBUGCANVAS_ATTRIBUTE_FILTERQUALITY] = Json::Value(
886 SKDEBUGCANVAS_FILTERQUALITY_MEDIUM);
887 break;
888 case kHigh_SkFilterQuality:
889 (*target)[SKDEBUGCANVAS_ATTRIBUTE_FILTERQUALITY] = Json::Value(
890 SKDEBUGCANVAS_FILTERQUALITY_HIGH);
891 break;
892 }
893}
894
ethannicholas50a8dd02016-02-10 05:40:46 -0800895static void apply_paint_maskfilter(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
896 SkMaskFilter* maskFilter = paint.getMaskFilter();
897 if (maskFilter != nullptr) {
898 SkMaskFilter::BlurRec blurRec;
899 if (maskFilter->asABlur(&blurRec)) {
900 Json::Value blur(Json::objectValue);
901 blur[SKDEBUGCANVAS_ATTRIBUTE_SIGMA] = Json::Value(blurRec.fSigma);
902 switch (blurRec.fStyle) {
903 case SkBlurStyle::kNormal_SkBlurStyle:
ethannicholas1446a9a2016-02-10 14:05:02 -0800904 blur[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = Json::Value(
905 SKDEBUGCANVAS_BLURSTYLE_NORMAL);
ethannicholas50a8dd02016-02-10 05:40:46 -0800906 break;
907 case SkBlurStyle::kSolid_SkBlurStyle:
ethannicholas1446a9a2016-02-10 14:05:02 -0800908 blur[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = Json::Value(
909 SKDEBUGCANVAS_BLURSTYLE_SOLID);
ethannicholas50a8dd02016-02-10 05:40:46 -0800910 break;
911 case SkBlurStyle::kOuter_SkBlurStyle:
ethannicholas1446a9a2016-02-10 14:05:02 -0800912 blur[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = Json::Value(
913 SKDEBUGCANVAS_BLURSTYLE_OUTER);
ethannicholas50a8dd02016-02-10 05:40:46 -0800914 break;
915 case SkBlurStyle::kInner_SkBlurStyle:
ethannicholas1446a9a2016-02-10 14:05:02 -0800916 blur[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = Json::Value(
917 SKDEBUGCANVAS_BLURSTYLE_INNER);
ethannicholas50a8dd02016-02-10 05:40:46 -0800918 break;
919 default:
920 SkASSERT(false);
921 }
922 switch (blurRec.fQuality) {
923 case SkBlurQuality::kLow_SkBlurQuality:
ethannicholas1446a9a2016-02-10 14:05:02 -0800924 blur[SKDEBUGCANVAS_ATTRIBUTE_QUALITY] = Json::Value(
925 SKDEBUGCANVAS_BLURQUALITY_LOW);
ethannicholas50a8dd02016-02-10 05:40:46 -0800926 break;
927 case SkBlurQuality::kHigh_SkBlurQuality:
ethannicholas1446a9a2016-02-10 14:05:02 -0800928 blur[SKDEBUGCANVAS_ATTRIBUTE_QUALITY] = Json::Value(
929 SKDEBUGCANVAS_BLURQUALITY_HIGH);
ethannicholas50a8dd02016-02-10 05:40:46 -0800930 break;
931 default:
932 SkASSERT(false);
933 }
934 (*target)[SKDEBUGCANVAS_ATTRIBUTE_BLUR] = blur;
935 } else {
936 Json::Value jsonMaskFilter;
937 flatten(maskFilter, &jsonMaskFilter, sendBinaries);
938 (*target)[SKDEBUGCANVAS_ATTRIBUTE_MASKFILTER] = jsonMaskFilter;
939 }
940 }
941}
942
943static void apply_paint_patheffect(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
944 SkPathEffect* pathEffect = paint.getPathEffect();
945 if (pathEffect != nullptr) {
946 SkPathEffect::DashInfo dashInfo;
947 SkPathEffect::DashType dashType = pathEffect->asADash(&dashInfo);
948 if (dashType == SkPathEffect::kDash_DashType) {
949 dashInfo.fIntervals = (SkScalar*) sk_malloc_throw(dashInfo.fCount * sizeof(SkScalar));
950 pathEffect->asADash(&dashInfo);
951 Json::Value dashing(Json::objectValue);
952 Json::Value intervals(Json::arrayValue);
953 for (int32_t i = 0; i < dashInfo.fCount; i++) {
954 intervals.append(Json::Value(dashInfo.fIntervals[i]));
955 }
956 sk_free(dashInfo.fIntervals);
957 dashing[SKDEBUGCANVAS_ATTRIBUTE_INTERVALS] = intervals;
958 dashing[SKDEBUGCANVAS_ATTRIBUTE_PHASE] = dashInfo.fPhase;
959 (*target)[SKDEBUGCANVAS_ATTRIBUTE_DASHING] = dashing;
960 } else {
961 Json::Value jsonPathEffect;
962 flatten(pathEffect, &jsonPathEffect, sendBinaries);
963 (*target)[SKDEBUGCANVAS_ATTRIBUTE_PATHEFFECT] = jsonPathEffect;
964 }
965 }
966}
967
968static void apply_paint_textalign(const SkPaint& paint, Json::Value* target) {
969 SkPaint::Align textAlign = paint.getTextAlign();
970 if (textAlign != SkPaint::kLeft_Align) {
971 switch (textAlign) {
972 case SkPaint::kCenter_Align: {
973 (*target)[SKDEBUGCANVAS_ATTRIBUTE_TEXTALIGN] = SKDEBUGCANVAS_ALIGN_CENTER;
974 break;
975 }
976 case SkPaint::kRight_Align: {
977 (*target)[SKDEBUGCANVAS_ATTRIBUTE_TEXTALIGN] = SKDEBUGCANVAS_ALIGN_RIGHT;
978 break;
979 }
980 default: SkASSERT(false);
981 }
982 }
983}
984
985static void apply_paint_typeface(const SkPaint& paint, Json::Value* target,
986 bool sendBinaries) {
987 SkTypeface* typeface = paint.getTypeface();
988 if (typeface != nullptr) {
989 if (sendBinaries) {
990 Json::Value jsonTypeface;
991 SkDynamicMemoryWStream buffer;
992 typeface->serialize(&buffer);
993 void* data = sk_malloc_throw(buffer.bytesWritten());
994 buffer.copyTo(data);
995 Json::Value bytes;
996 encode_data(data, buffer.bytesWritten(), &bytes);
997 jsonTypeface[SKDEBUGCANVAS_ATTRIBUTE_BYTES] = bytes;
998 sk_free(data);
999 (*target)[SKDEBUGCANVAS_ATTRIBUTE_TYPEFACE] = jsonTypeface;
1000 }
1001 }
1002}
1003
1004static void apply_paint_shader(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
1005 SkFlattenable* shader = paint.getShader();
1006 if (shader != nullptr) {
1007 Json::Value jsonShader;
1008 flatten(shader, &jsonShader, sendBinaries);
1009 (*target)[SKDEBUGCANVAS_ATTRIBUTE_SHADER] = jsonShader;
1010 }
1011}
1012
1013static void apply_paint_xfermode(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
1014 SkFlattenable* xfermode = paint.getXfermode();
1015 if (xfermode != nullptr) {
1016 Json::Value jsonXfermode;
1017 flatten(xfermode, &jsonXfermode, sendBinaries);
1018 (*target)[SKDEBUGCANVAS_ATTRIBUTE_XFERMODE] = jsonXfermode;
1019 }
1020}
1021
1022static void apply_paint_imagefilter(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
1023 SkFlattenable* imageFilter = paint.getImageFilter();
1024 if (imageFilter != nullptr) {
1025 Json::Value jsonImageFilter;
1026 flatten(imageFilter, &jsonImageFilter, sendBinaries);
1027 (*target)[SKDEBUGCANVAS_ATTRIBUTE_IMAGEFILTER] = jsonImageFilter;
1028 }
1029}
1030
1031static void apply_paint_colorfilter(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
1032 SkFlattenable* colorFilter = paint.getColorFilter();
1033 if (colorFilter != nullptr) {
1034 Json::Value jsonColorFilter;
1035 flatten(colorFilter, &jsonColorFilter, sendBinaries);
1036 (*target)[SKDEBUGCANVAS_ATTRIBUTE_COLORFILTER] = jsonColorFilter;
1037 }
1038}
1039
ethannicholas1446a9a2016-02-10 14:05:02 -08001040static void apply_paint_looper(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
1041 SkFlattenable* looper = paint.getLooper();
1042 if (looper != nullptr) {
1043 Json::Value jsonLooper;
1044 flatten(looper, &jsonLooper, sendBinaries);
1045 (*target)[SKDEBUGCANVAS_ATTRIBUTE_LOOPER] = jsonLooper;
1046 }
1047}
1048
ethannicholas50a8dd02016-02-10 05:40:46 -08001049Json::Value make_json_paint(const SkPaint& paint, bool sendBinaries) {
1050 Json::Value result(Json::objectValue);
1051 store_scalar(&result, SKDEBUGCANVAS_ATTRIBUTE_STROKEWIDTH, paint.getStrokeWidth(), 0.0f);
1052 store_scalar(&result, SKDEBUGCANVAS_ATTRIBUTE_STROKEMITER, paint.getStrokeMiter(),
1053 SkPaintDefaults_MiterLimit);
1054 store_bool(&result, SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS, paint.isAntiAlias(), false);
ethannicholas1446a9a2016-02-10 14:05:02 -08001055 store_bool(&result, SKDEBUGCANVAS_ATTRIBUTE_DITHER, paint.isDither(), false);
ethannicholas50a8dd02016-02-10 05:40:46 -08001056 store_scalar(&result, SKDEBUGCANVAS_ATTRIBUTE_TEXTSIZE, paint.getTextSize(),
1057 SkPaintDefaults_TextSize);
1058 store_scalar(&result, SKDEBUGCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextScaleX(), SK_Scalar1);
1059 store_scalar(&result, SKDEBUGCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextSkewX(), 0.0f);
1060 apply_paint_color(paint, &result);
1061 apply_paint_style(paint, &result);
1062 apply_paint_cap(paint, &result);
ethannicholas1446a9a2016-02-10 14:05:02 -08001063 apply_paint_join(paint, &result);
1064 apply_paint_filterquality(paint, &result);
ethannicholas50a8dd02016-02-10 05:40:46 -08001065 apply_paint_textalign(paint, &result);
1066 apply_paint_patheffect(paint, &result, sendBinaries);
1067 apply_paint_maskfilter(paint, &result, sendBinaries);
1068 apply_paint_shader(paint, &result, sendBinaries);
1069 apply_paint_xfermode(paint, &result, sendBinaries);
ethannicholas1446a9a2016-02-10 14:05:02 -08001070 apply_paint_looper(paint, &result, sendBinaries);
ethannicholas50a8dd02016-02-10 05:40:46 -08001071 apply_paint_imagefilter(paint, &result, sendBinaries);
1072 apply_paint_colorfilter(paint, &result, sendBinaries);
1073 apply_paint_typeface(paint, &result, sendBinaries);
1074 return result;
1075}
1076
ethannicholas1446a9a2016-02-10 14:05:02 -08001077static SkPoint get_json_point(Json::Value point) {
1078 return SkPoint::Make(point[0].asFloat(), point[1].asFloat());
1079}
1080
1081static SkColor get_json_color(Json::Value color) {
1082 return SkColorSetARGB(color[0].asInt(), color[1].asInt(), color[2].asInt(), color[3].asInt());
1083}
1084
ethannicholas50a8dd02016-02-10 05:40:46 -08001085static void extract_json_paint_color(Json::Value& jsonPaint, SkPaint* target) {
1086 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_COLOR)) {
ethannicholas1446a9a2016-02-10 14:05:02 -08001087 target->setColor(get_json_color(jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_COLOR]));
ethannicholas50a8dd02016-02-10 05:40:46 -08001088 }
1089}
1090
1091static void extract_json_paint_shader(Json::Value& jsonPaint, SkPaint* target) {
1092 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_SHADER)) {
1093 Json::Value jsonShader = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_SHADER];
1094 SkShader* shader = (SkShader*) load_flattenable(jsonShader);
1095 if (shader != nullptr) {
1096 target->setShader(shader);
1097 shader->unref();
1098 }
1099 }
1100}
1101
1102static void extract_json_paint_patheffect(Json::Value& jsonPaint, SkPaint* target) {
1103 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_PATHEFFECT)) {
1104 Json::Value jsonPathEffect = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_PATHEFFECT];
1105 SkPathEffect* pathEffect = (SkPathEffect*) load_flattenable(jsonPathEffect);
1106 if (pathEffect != nullptr) {
1107 target->setPathEffect(pathEffect);
1108 pathEffect->unref();
1109 }
1110 }
1111}
1112
1113static void extract_json_paint_maskfilter(Json::Value& jsonPaint, SkPaint* target) {
1114 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_MASKFILTER)) {
1115 Json::Value jsonMaskFilter = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_MASKFILTER];
1116 SkMaskFilter* maskFilter = (SkMaskFilter*) load_flattenable(jsonMaskFilter);
1117 if (maskFilter != nullptr) {
1118 target->setMaskFilter(maskFilter);
1119 maskFilter->unref();
1120 }
1121 }
1122}
1123
1124static void extract_json_paint_colorfilter(Json::Value& jsonPaint, SkPaint* target) {
1125 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_COLORFILTER)) {
1126 Json::Value jsonColorFilter = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_COLORFILTER];
1127 SkColorFilter* colorFilter = (SkColorFilter*) load_flattenable(jsonColorFilter);
1128 if (colorFilter != nullptr) {
1129 target->setColorFilter(colorFilter);
1130 colorFilter->unref();
1131 }
1132 }
1133}
1134
1135static void extract_json_paint_xfermode(Json::Value& jsonPaint, SkPaint* target) {
1136 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_XFERMODE)) {
1137 Json::Value jsonXfermode = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_XFERMODE];
1138 SkXfermode* xfermode = (SkXfermode*) load_flattenable(jsonXfermode);
1139 if (xfermode != nullptr) {
1140 target->setXfermode(xfermode);
1141 xfermode->unref();
1142 }
1143 }
1144}
1145
ethannicholas1446a9a2016-02-10 14:05:02 -08001146static void extract_json_paint_looper(Json::Value& jsonPaint, SkPaint* target) {
1147 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_LOOPER)) {
1148 Json::Value jsonLooper = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_LOOPER];
1149 SkDrawLooper* looper = (SkDrawLooper*) load_flattenable(jsonLooper);
1150 if (looper != nullptr) {
1151 target->setLooper(looper);
1152 looper->unref();
1153 }
1154 }
1155}
1156
ethannicholas50a8dd02016-02-10 05:40:46 -08001157static void extract_json_paint_imagefilter(Json::Value& jsonPaint, SkPaint* target) {
1158 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_IMAGEFILTER)) {
1159 Json::Value jsonImageFilter = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_IMAGEFILTER];
1160 SkImageFilter* imageFilter = (SkImageFilter*) load_flattenable(jsonImageFilter);
1161 if (imageFilter != nullptr) {
1162 target->setImageFilter(imageFilter);
1163 imageFilter->unref();
1164 }
1165 }
1166}
1167
1168static void extract_json_paint_style(Json::Value& jsonPaint, SkPaint* target) {
1169 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_STYLE)) {
1170 const char* style = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_STYLE].asCString();
1171 if (!strcmp(style, SKDEBUGCANVAS_STYLE_FILL)) {
1172 target->setStyle(SkPaint::kFill_Style);
1173 }
1174 else if (!strcmp(style, SKDEBUGCANVAS_STYLE_STROKE)) {
1175 target->setStyle(SkPaint::kStroke_Style);
1176 }
1177 else if (!strcmp(style, SKDEBUGCANVAS_STYLE_STROKEANDFILL)) {
1178 target->setStyle(SkPaint::kStrokeAndFill_Style);
1179 }
1180 }
1181}
1182
1183static void extract_json_paint_strokewidth(Json::Value& jsonPaint, SkPaint* target) {
1184 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_STROKEWIDTH)) {
1185 float strokeWidth = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_STROKEWIDTH].asFloat();
1186 target->setStrokeWidth(strokeWidth);
1187 }
1188}
1189
1190static void extract_json_paint_strokemiter(Json::Value& jsonPaint, SkPaint* target) {
1191 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_STROKEMITER)) {
1192 float strokeMiter = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_STROKEMITER].asFloat();
1193 target->setStrokeMiter(strokeMiter);
1194 }
1195}
1196
ethannicholas1446a9a2016-02-10 14:05:02 -08001197static void extract_json_paint_strokejoin(Json::Value& jsonPaint, SkPaint* target) {
1198 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_STROKEJOIN)) {
1199 const char* join = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_STROKEJOIN].asCString();
1200 if (!strcmp(join, SKDEBUGCANVAS_MITER_JOIN)) {
1201 target->setStrokeJoin(SkPaint::kMiter_Join);
1202 }
1203 else if (!strcmp(join, SKDEBUGCANVAS_ROUND_JOIN)) {
1204 target->setStrokeJoin(SkPaint::kRound_Join);
1205 }
1206 else if (!strcmp(join, SKDEBUGCANVAS_BEVEL_JOIN)) {
1207 target->setStrokeJoin(SkPaint::kBevel_Join);
1208 }
1209 else {
1210 SkASSERT(false);
1211 }
1212 }
1213}
1214
ethannicholas50a8dd02016-02-10 05:40:46 -08001215static void extract_json_paint_cap(Json::Value& jsonPaint, SkPaint* target) {
1216 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_CAP)) {
1217 const char* cap = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_CAP].asCString();
1218 if (!strcmp(cap, SKDEBUGCANVAS_CAP_BUTT)) {
1219 target->setStrokeCap(SkPaint::kButt_Cap);
1220 }
1221 else if (!strcmp(cap, SKDEBUGCANVAS_CAP_ROUND)) {
1222 target->setStrokeCap(SkPaint::kRound_Cap);
1223 }
1224 else if (!strcmp(cap, SKDEBUGCANVAS_CAP_SQUARE)) {
1225 target->setStrokeCap(SkPaint::kSquare_Cap);
1226 }
1227 }
1228}
1229
ethannicholas1446a9a2016-02-10 14:05:02 -08001230static void extract_json_paint_filterquality(Json::Value& jsonPaint, SkPaint* target) {
1231 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_FILTERQUALITY)) {
1232 const char* quality = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_FILTERQUALITY].asCString();
1233 if (!strcmp(quality, SKDEBUGCANVAS_FILTERQUALITY_NONE)) {
1234 target->setFilterQuality(kNone_SkFilterQuality);
1235 }
1236 else if (!strcmp(quality, SKDEBUGCANVAS_FILTERQUALITY_LOW)) {
1237 target->setFilterQuality(kLow_SkFilterQuality);
1238 }
1239 else if (!strcmp(quality, SKDEBUGCANVAS_FILTERQUALITY_MEDIUM)) {
1240 target->setFilterQuality(kMedium_SkFilterQuality);
1241 }
1242 else if (!strcmp(quality, SKDEBUGCANVAS_FILTERQUALITY_HIGH)) {
1243 target->setFilterQuality(kHigh_SkFilterQuality);
1244 }
1245 }
1246}
1247
ethannicholas50a8dd02016-02-10 05:40:46 -08001248static void extract_json_paint_antialias(Json::Value& jsonPaint, SkPaint* target) {
1249 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS)) {
1250 target->setAntiAlias(jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
1251 }
1252}
1253
ethannicholas1446a9a2016-02-10 14:05:02 -08001254static void extract_json_paint_dither(Json::Value& jsonPaint, SkPaint* target) {
1255 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_DITHER)) {
1256 target->setDither(jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_DITHER].asBool());
1257 }
1258}
1259
ethannicholas50a8dd02016-02-10 05:40:46 -08001260static void extract_json_paint_blur(Json::Value& jsonPaint, SkPaint* target) {
1261 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_BLUR)) {
1262 Json::Value blur = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_BLUR];
1263 SkScalar sigma = blur[SKDEBUGCANVAS_ATTRIBUTE_SIGMA].asFloat();
1264 SkBlurStyle style;
1265 const char* jsonStyle = blur[SKDEBUGCANVAS_ATTRIBUTE_STYLE].asCString();
1266 if (!strcmp(jsonStyle, SKDEBUGCANVAS_BLURSTYLE_NORMAL)) {
1267 style = SkBlurStyle::kNormal_SkBlurStyle;
1268 }
1269 else if (!strcmp(jsonStyle, SKDEBUGCANVAS_BLURSTYLE_SOLID)) {
1270 style = SkBlurStyle::kSolid_SkBlurStyle;
1271 }
1272 else if (!strcmp(jsonStyle, SKDEBUGCANVAS_BLURSTYLE_OUTER)) {
1273 style = SkBlurStyle::kOuter_SkBlurStyle;
1274 }
1275 else if (!strcmp(jsonStyle, SKDEBUGCANVAS_BLURSTYLE_INNER)) {
1276 style = SkBlurStyle::kInner_SkBlurStyle;
1277 }
1278 else {
1279 SkASSERT(false);
1280 style = SkBlurStyle::kNormal_SkBlurStyle;
1281 }
1282 SkBlurMaskFilter::BlurFlags flags;
1283 const char* jsonQuality = blur[SKDEBUGCANVAS_ATTRIBUTE_QUALITY].asCString();
1284 if (!strcmp(jsonQuality, SKDEBUGCANVAS_BLURQUALITY_LOW)) {
1285 flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag;
1286 }
1287 else if (!strcmp(jsonQuality, SKDEBUGCANVAS_BLURQUALITY_HIGH)) {
1288 flags = SkBlurMaskFilter::BlurFlags::kHighQuality_BlurFlag;
1289 }
1290 else {
1291 SkASSERT(false);
1292 flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag;
1293 }
1294 target->setMaskFilter(SkBlurMaskFilter::Create(style, sigma, flags));
1295 }
1296}
1297
1298static void extract_json_paint_dashing(Json::Value& jsonPaint, SkPaint* target) {
1299 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_DASHING)) {
1300 Json::Value dash = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_DASHING];
1301 Json::Value jsonIntervals = dash[SKDEBUGCANVAS_ATTRIBUTE_INTERVALS];
1302 Json::ArrayIndex count = jsonIntervals.size();
1303 SkScalar* intervals = (SkScalar*) sk_malloc_throw(count * sizeof(SkScalar));
1304 for (Json::ArrayIndex i = 0; i < count; i++) {
1305 intervals[i] = jsonIntervals[i].asFloat();
1306 }
1307 SkScalar phase = dash[SKDEBUGCANVAS_ATTRIBUTE_PHASE].asFloat();
1308 target->setPathEffect(SkDashPathEffect::Create(intervals, count, phase));
1309 sk_free(intervals);
1310 }
1311}
1312
1313static void extract_json_paint_textalign(Json::Value& jsonPaint, SkPaint* target) {
1314 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_TEXTALIGN)) {
1315 SkPaint::Align textAlign;
1316 const char* jsonAlign = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_TEXTALIGN].asCString();
1317 if (!strcmp(jsonAlign, SKDEBUGCANVAS_ALIGN_LEFT)) {
1318 textAlign = SkPaint::kLeft_Align;
1319 }
1320 else if (!strcmp(jsonAlign, SKDEBUGCANVAS_ALIGN_CENTER)) {
1321 textAlign = SkPaint::kCenter_Align;
1322 }
1323 else if (!strcmp(jsonAlign, SKDEBUGCANVAS_ALIGN_RIGHT)) {
1324 textAlign = SkPaint::kRight_Align;
1325 }
1326 else {
1327 SkASSERT(false);
1328 textAlign = SkPaint::kLeft_Align;
1329 }
1330 target->setTextAlign(textAlign);
1331 }
1332}
1333
1334static void extract_json_paint_textsize(Json::Value& jsonPaint, SkPaint* target) {
1335 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_TEXTSIZE)) {
1336 float textSize = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_TEXTSIZE].asFloat();
1337 target->setTextSize(textSize);
1338 }
1339}
1340
1341static void extract_json_paint_textscalex(Json::Value& jsonPaint, SkPaint* target) {
1342 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_TEXTSCALEX)) {
1343 float textScaleX = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_TEXTSCALEX].asFloat();
1344 target->setTextScaleX(textScaleX);
1345 }
1346}
1347
1348static void extract_json_paint_textskewx(Json::Value& jsonPaint, SkPaint* target) {
1349 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_TEXTSKEWX)) {
1350 float textSkewX = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat();
1351 target->setTextSkewX(textSkewX);
1352 }
1353}
1354
1355static void extract_json_paint_typeface(Json::Value& jsonPaint, SkPaint* target) {
1356 if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_TYPEFACE)) {
1357 Json::Value jsonTypeface = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_TYPEFACE];
1358 Json::Value bytes = jsonTypeface[SKDEBUGCANVAS_ATTRIBUTE_BYTES];
1359 void* data;
1360 Json::ArrayIndex length = decode_data(bytes, &data);
1361 SkMemoryStream buffer(data, length);
1362 SkTypeface* typeface = SkTypeface::Deserialize(&buffer);
1363 sk_free(data);
1364 target->setTypeface(typeface);
1365 }
1366}
1367
1368static void extract_json_paint(Json::Value& paint, SkPaint* result) {
1369 extract_json_paint_color(paint, result);
1370 extract_json_paint_shader(paint, result);
1371 extract_json_paint_patheffect(paint, result);
1372 extract_json_paint_maskfilter(paint, result);
1373 extract_json_paint_colorfilter(paint, result);
1374 extract_json_paint_xfermode(paint, result);
ethannicholas1446a9a2016-02-10 14:05:02 -08001375 extract_json_paint_looper(paint, result);
ethannicholas50a8dd02016-02-10 05:40:46 -08001376 extract_json_paint_imagefilter(paint, result);
1377 extract_json_paint_style(paint, result);
1378 extract_json_paint_strokewidth(paint, result);
1379 extract_json_paint_strokemiter(paint, result);
ethannicholas1446a9a2016-02-10 14:05:02 -08001380 extract_json_paint_strokejoin(paint, result);
ethannicholas50a8dd02016-02-10 05:40:46 -08001381 extract_json_paint_cap(paint, result);
ethannicholas1446a9a2016-02-10 14:05:02 -08001382 extract_json_paint_filterquality(paint, result);
ethannicholas50a8dd02016-02-10 05:40:46 -08001383 extract_json_paint_antialias(paint, result);
ethannicholas1446a9a2016-02-10 14:05:02 -08001384 extract_json_paint_dither(paint, result);
ethannicholas50a8dd02016-02-10 05:40:46 -08001385 extract_json_paint_blur(paint, result);
1386 extract_json_paint_dashing(paint, result);
1387 extract_json_paint_textalign(paint, result);
1388 extract_json_paint_textsize(paint, result);
1389 extract_json_paint_textscalex(paint, result);
1390 extract_json_paint_textskewx(paint, result);
1391 extract_json_paint_typeface(paint, result);
1392}
1393
1394static void extract_json_rect(Json::Value& rect, SkRect* result) {
1395 result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3].asFloat());
1396}
1397
1398static void extract_json_irect(Json::Value& rect, SkIRect* result) {
1399 result->set(rect[0].asInt(), rect[1].asInt(), rect[2].asInt(), rect[3].asInt());
1400}
1401
1402static void extract_json_rrect(Json::Value& rrect, SkRRect* result) {
1403 SkVector radii[4] = {
1404 { rrect[1][0].asFloat(), rrect[1][1].asFloat() },
1405 { rrect[2][0].asFloat(), rrect[2][1].asFloat() },
1406 { rrect[3][0].asFloat(), rrect[3][1].asFloat() },
1407 { rrect[4][0].asFloat(), rrect[4][1].asFloat() }
1408 };
1409 result->setRectRadii(SkRect::MakeLTRB(rrect[0][0].asFloat(), rrect[0][1].asFloat(),
1410 rrect[0][2].asFloat(), rrect[0][3].asFloat()),
1411 radii);
1412}
1413
1414static void extract_json_matrix(Json::Value& matrix, SkMatrix* result) {
1415 SkScalar values[] = {
1416 matrix[0][0].asFloat(), matrix[0][1].asFloat(), matrix[0][2].asFloat(),
1417 matrix[1][0].asFloat(), matrix[1][1].asFloat(), matrix[1][2].asFloat(),
1418 matrix[2][0].asFloat(), matrix[2][1].asFloat(), matrix[2][2].asFloat()
1419 };
1420 result->set9(values);
1421}
1422
1423static void extract_json_path(Json::Value& path, SkPath* result) {
1424 const char* fillType = path[SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE].asCString();
1425 if (!strcmp(fillType, SKDEBUGCANVAS_FILLTYPE_WINDING)) {
1426 result->setFillType(SkPath::kWinding_FillType);
1427 }
1428 else if (!strcmp(fillType, SKDEBUGCANVAS_FILLTYPE_EVENODD)) {
1429 result->setFillType(SkPath::kEvenOdd_FillType);
1430 }
1431 else if (!strcmp(fillType, SKDEBUGCANVAS_FILLTYPE_INVERSEWINDING)) {
1432 result->setFillType(SkPath::kInverseWinding_FillType);
1433 }
1434 else if (!strcmp(fillType, SKDEBUGCANVAS_FILLTYPE_INVERSEEVENODD)) {
1435 result->setFillType(SkPath::kInverseEvenOdd_FillType);
1436 }
1437 Json::Value verbs = path[SKDEBUGCANVAS_ATTRIBUTE_VERBS];
1438 for (Json::ArrayIndex i = 0; i < verbs.size(); i++) {
1439 Json::Value verb = verbs[i];
1440 if (verb.isString()) {
1441 SkASSERT(!strcmp(verb.asCString(), SKDEBUGCANVAS_VERB_CLOSE));
1442 result->close();
1443 }
1444 else {
1445 if (verb.isMember(SKDEBUGCANVAS_VERB_MOVE)) {
1446 Json::Value move = verb[SKDEBUGCANVAS_VERB_MOVE];
1447 result->moveTo(move[0].asFloat(), move[1].asFloat());
1448 }
1449 else if (verb.isMember(SKDEBUGCANVAS_VERB_LINE)) {
1450 Json::Value line = verb[SKDEBUGCANVAS_VERB_LINE];
1451 result->lineTo(line[0].asFloat(), line[1].asFloat());
1452 }
1453 else if (verb.isMember(SKDEBUGCANVAS_VERB_QUAD)) {
1454 Json::Value quad = verb[SKDEBUGCANVAS_VERB_QUAD];
1455 result->quadTo(quad[0][0].asFloat(), quad[0][1].asFloat(),
1456 quad[1][0].asFloat(), quad[1][1].asFloat());
1457 }
1458 else if (verb.isMember(SKDEBUGCANVAS_VERB_CUBIC)) {
1459 Json::Value cubic = verb[SKDEBUGCANVAS_VERB_CUBIC];
1460 result->cubicTo(cubic[0][0].asFloat(), cubic[0][1].asFloat(),
1461 cubic[1][0].asFloat(), cubic[1][1].asFloat(),
1462 cubic[2][0].asFloat(), cubic[2][1].asFloat());
1463 }
1464 else if (verb.isMember(SKDEBUGCANVAS_VERB_CONIC)) {
1465 Json::Value conic = verb[SKDEBUGCANVAS_VERB_CONIC];
1466 result->conicTo(conic[0][0].asFloat(), conic[0][1].asFloat(),
1467 conic[1][0].asFloat(), conic[1][1].asFloat(),
1468 conic[2].asFloat());
1469 }
1470 else {
1471 SkASSERT(false);
1472 }
1473 }
1474 }
1475}
1476
1477SkRegion::Op get_json_regionop(Json::Value& jsonOp) {
1478 const char* op = jsonOp.asCString();
1479 if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_DIFFERENCE)) {
1480 return SkRegion::kDifference_Op;
1481 }
1482 else if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_INTERSECT)) {
1483 return SkRegion::kIntersect_Op;
1484 }
1485 else if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_UNION)) {
1486 return SkRegion::kUnion_Op;
1487 }
1488 else if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_XOR)) {
1489 return SkRegion::kXOR_Op;
1490 }
1491 else if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_REVERSE_DIFFERENCE)) {
1492 return SkRegion::kReverseDifference_Op;
1493 }
1494 else if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_REPLACE)) {
1495 return SkRegion::kReplace_Op;
1496 }
1497 SkASSERT(false);
1498 return SkRegion::kIntersect_Op;
1499}
1500
ethannicholas1446a9a2016-02-10 14:05:02 -08001501SkClearCommand::SkClearCommand(SkColor color) : INHERITED(kDrawClear_OpType) {
1502 fColor = color;
1503 fInfo.push(SkObjectParser::CustomTextToString("No Parameters"));
1504}
1505
1506void SkClearCommand::execute(SkCanvas* canvas) const {
1507 canvas->clear(fColor);
1508}
1509
1510Json::Value SkClearCommand::toJSON() const {
1511 Json::Value result = INHERITED::toJSON();
1512 result[SKDEBUGCANVAS_ATTRIBUTE_COLOR] = make_json_color(fColor);
1513 return result;
1514}
1515
1516 SkClearCommand* SkClearCommand::fromJSON(Json::Value& command) {
1517 Json::Value color = command[SKDEBUGCANVAS_ATTRIBUTE_COLOR];
1518 return new SkClearCommand(get_json_color(color));
1519}
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00001520
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00001521SkClipPathCommand::SkClipPathCommand(const SkPath& path, SkRegion::Op op, bool doAA)
robertphillips9bafc302015-02-13 11:13:00 -08001522 : INHERITED(kClipPath_OpType) {
robertphillips@google.com91217d02013-03-17 18:33:46 +00001523 fPath = path;
1524 fOp = op;
1525 fDoAA = doAA;
chudy@google.com902ebe52012-06-29 14:21:22 +00001526
robertphillips@google.com91217d02013-03-17 18:33:46 +00001527 fInfo.push(SkObjectParser::PathToString(path));
1528 fInfo.push(SkObjectParser::RegionOpToString(op));
1529 fInfo.push(SkObjectParser::BoolToString(doAA));
chudy@google.com902ebe52012-06-29 14:21:22 +00001530}
1531
fmalita8c89c522014-11-08 16:18:56 -08001532void SkClipPathCommand::execute(SkCanvas* canvas) const {
robertphillips@google.com91217d02013-03-17 18:33:46 +00001533 canvas->clipPath(fPath, fOp, fDoAA);
chudy@google.com902ebe52012-06-29 14:21:22 +00001534}
1535
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00001536bool SkClipPathCommand::render(SkCanvas* canvas) const {
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00001537 render_path(canvas, fPath);
1538 return true;
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +00001539}
1540
ethannicholas50a8dd02016-02-10 05:40:46 -08001541Json::Value SkClipPathCommand::toJSON() const {
1542 Json::Value result = INHERITED::toJSON();
1543 result[SKDEBUGCANVAS_ATTRIBUTE_PATH] = make_json_path(fPath);
1544 result[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP] = make_json_regionop(fOp);
1545 result[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS] = fDoAA;
1546 return result;
1547}
1548
1549SkClipPathCommand* SkClipPathCommand::fromJSON(Json::Value& command) {
1550 SkPath path;
1551 extract_json_path(command[SKDEBUGCANVAS_ATTRIBUTE_PATH], &path);
1552 return new SkClipPathCommand(path, get_json_regionop(command[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP]),
1553 command[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
1554}
1555
skia.committer@gmail.comdb35dab2014-03-27 03:02:48 +00001556SkClipRegionCommand::SkClipRegionCommand(const SkRegion& region, SkRegion::Op op)
robertphillips9bafc302015-02-13 11:13:00 -08001557 : INHERITED(kClipRegion_OpType) {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001558 fRegion = region;
1559 fOp = op;
chudy@google.com902ebe52012-06-29 14:21:22 +00001560
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001561 fInfo.push(SkObjectParser::RegionToString(region));
1562 fInfo.push(SkObjectParser::RegionOpToString(op));
chudy@google.com902ebe52012-06-29 14:21:22 +00001563}
1564
fmalita8c89c522014-11-08 16:18:56 -08001565void SkClipRegionCommand::execute(SkCanvas* canvas) const {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001566 canvas->clipRegion(fRegion, fOp);
chudy@google.com902ebe52012-06-29 14:21:22 +00001567}
1568
ethannicholas50a8dd02016-02-10 05:40:46 -08001569Json::Value SkClipRegionCommand::toJSON() const {
1570 Json::Value result = INHERITED::toJSON();
1571 result[SKDEBUGCANVAS_ATTRIBUTE_REGION] = make_json_region(fRegion);
1572 result[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP] = make_json_regionop(fOp);
1573 return result;
1574}
1575
1576SkClipRegionCommand* SkClipRegionCommand::fromJSON(Json::Value& command) {
1577 SkASSERT(false);
1578 return nullptr;
1579}
1580
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00001581SkClipRectCommand::SkClipRectCommand(const SkRect& rect, SkRegion::Op op, bool doAA)
robertphillips9bafc302015-02-13 11:13:00 -08001582 : INHERITED(kClipRect_OpType) {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00001583 fRect = rect;
1584 fOp = op;
1585 fDoAA = doAA;
chudy@google.com902ebe52012-06-29 14:21:22 +00001586
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00001587 fInfo.push(SkObjectParser::RectToString(rect));
1588 fInfo.push(SkObjectParser::RegionOpToString(op));
1589 fInfo.push(SkObjectParser::BoolToString(doAA));
chudy@google.com902ebe52012-06-29 14:21:22 +00001590}
1591
fmalita8c89c522014-11-08 16:18:56 -08001592void SkClipRectCommand::execute(SkCanvas* canvas) const {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00001593 canvas->clipRect(fRect, fOp, fDoAA);
chudy@google.com902ebe52012-06-29 14:21:22 +00001594}
1595
ethannicholas50a8dd02016-02-10 05:40:46 -08001596Json::Value SkClipRectCommand::toJSON() const {
1597 Json::Value result = INHERITED::toJSON();
1598 result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_rect(fRect);
1599 result[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP] = make_json_regionop(fOp);
1600 result[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS] = Json::Value(fDoAA);
1601 return result;
1602}
1603
1604SkClipRectCommand* SkClipRectCommand::fromJSON(Json::Value& command) {
1605 SkRect rect;
1606 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_COORDS], &rect);
1607 return new SkClipRectCommand(rect, get_json_regionop(command[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP]),
1608 command[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
1609}
1610
skia.committer@gmail.comdb35dab2014-03-27 03:02:48 +00001611SkClipRRectCommand::SkClipRRectCommand(const SkRRect& rrect, SkRegion::Op op, bool doAA)
robertphillips9bafc302015-02-13 11:13:00 -08001612 : INHERITED(kClipRRect_OpType) {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00001613 fRRect = rrect;
1614 fOp = op;
1615 fDoAA = doAA;
robertphillips@google.com67baba42013-01-02 20:20:31 +00001616
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00001617 fInfo.push(SkObjectParser::RRectToString(rrect));
1618 fInfo.push(SkObjectParser::RegionOpToString(op));
1619 fInfo.push(SkObjectParser::BoolToString(doAA));
robertphillips@google.com67baba42013-01-02 20:20:31 +00001620}
1621
fmalita8c89c522014-11-08 16:18:56 -08001622void SkClipRRectCommand::execute(SkCanvas* canvas) const {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00001623 canvas->clipRRect(fRRect, fOp, fDoAA);
robertphillips@google.com67baba42013-01-02 20:20:31 +00001624}
1625
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00001626bool SkClipRRectCommand::render(SkCanvas* canvas) const {
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00001627 render_rrect(canvas, fRRect);
1628 return true;
1629}
1630
ethannicholas50a8dd02016-02-10 05:40:46 -08001631Json::Value SkClipRRectCommand::toJSON() const {
1632 Json::Value result = INHERITED::toJSON();
1633 result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_rrect(fRRect);
1634 result[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP] = make_json_regionop(fOp);
1635 result[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS] = Json::Value(fDoAA);
1636 return result;
1637}
1638
1639SkClipRRectCommand* SkClipRRectCommand::fromJSON(Json::Value& command) {
1640 SkRRect rrect;
1641 extract_json_rrect(command[SKDEBUGCANVAS_ATTRIBUTE_COORDS], &rrect);
1642 return new SkClipRRectCommand(rrect,
1643 get_json_regionop(command[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP]),
1644 command[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
1645}
1646
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00001647SkConcatCommand::SkConcatCommand(const SkMatrix& matrix)
robertphillips9bafc302015-02-13 11:13:00 -08001648 : INHERITED(kConcat_OpType) {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001649 fMatrix = matrix;
chudy@google.com902ebe52012-06-29 14:21:22 +00001650
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001651 fInfo.push(SkObjectParser::MatrixToString(matrix));
chudy@google.com902ebe52012-06-29 14:21:22 +00001652}
1653
fmalita8c89c522014-11-08 16:18:56 -08001654void SkConcatCommand::execute(SkCanvas* canvas) const {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001655 canvas->concat(fMatrix);
chudy@google.com902ebe52012-06-29 14:21:22 +00001656}
1657
ethannicholas50a8dd02016-02-10 05:40:46 -08001658Json::Value SkConcatCommand::toJSON() const {
1659 Json::Value result = INHERITED::toJSON();
1660 result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = make_json_matrix(fMatrix);
1661 return result;
1662}
1663
1664SkConcatCommand* SkConcatCommand::fromJSON(Json::Value& command) {
1665 SkMatrix matrix;
1666 extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix);
1667 return new SkConcatCommand(matrix);
1668}
1669
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00001670SkDrawBitmapCommand::SkDrawBitmapCommand(const SkBitmap& bitmap, SkScalar left, SkScalar top,
robertphillipsb3f319f2014-08-13 10:46:23 -07001671 const SkPaint* paint)
robertphillips9bafc302015-02-13 11:13:00 -08001672 : INHERITED(kDrawBitmap_OpType) {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001673 fBitmap = bitmap;
1674 fLeft = left;
1675 fTop = top;
bsalomon49f085d2014-09-05 13:34:00 -07001676 if (paint) {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001677 fPaint = *paint;
1678 fPaintPtr = &fPaint;
1679 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001680 fPaintPtr = nullptr;
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001681 }
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001682
1683 fInfo.push(SkObjectParser::BitmapToString(bitmap));
1684 fInfo.push(SkObjectParser::ScalarToString(left, "SkScalar left: "));
1685 fInfo.push(SkObjectParser::ScalarToString(top, "SkScalar top: "));
bsalomon49f085d2014-09-05 13:34:00 -07001686 if (paint) {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001687 fInfo.push(SkObjectParser::PaintToString(*paint));
robertphillips@google.comb83b6b42013-01-22 14:32:09 +00001688 }
chudy@google.com902ebe52012-06-29 14:21:22 +00001689}
1690
fmalita8c89c522014-11-08 16:18:56 -08001691void SkDrawBitmapCommand::execute(SkCanvas* canvas) const {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001692 canvas->drawBitmap(fBitmap, fLeft, fTop, fPaintPtr);
chudy@google.com902ebe52012-06-29 14:21:22 +00001693}
1694
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00001695bool SkDrawBitmapCommand::render(SkCanvas* canvas) const {
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00001696 render_bitmap(canvas, fBitmap);
1697 return true;
robertphillips@google.com53ec73d2012-11-26 13:09:17 +00001698}
1699
ethannicholas50a8dd02016-02-10 05:40:46 -08001700Json::Value SkDrawBitmapCommand::toJSON() const {
1701 Json::Value result = INHERITED::toJSON();
1702 Json::Value encoded;
1703 if (flatten(fBitmap, &encoded, SKDEBUGCANVAS_SEND_BINARIES)) {
1704 Json::Value command(Json::objectValue);
1705 result[SKDEBUGCANVAS_ATTRIBUTE_BITMAP] = encoded;
1706 result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_point(fLeft, fTop);
1707 if (fPaintPtr != nullptr) {
ethannicholas1446a9a2016-02-10 14:05:02 -08001708 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaintPtr,
1709 SKDEBUGCANVAS_SEND_BINARIES);
ethannicholas50a8dd02016-02-10 05:40:46 -08001710 }
1711 }
1712 return result;
1713}
1714
1715SkDrawBitmapCommand* SkDrawBitmapCommand::fromJSON(Json::Value& command) {
1716 SkBitmap* bitmap = load_bitmap(command[SKDEBUGCANVAS_ATTRIBUTE_BITMAP]);
1717 if (bitmap == nullptr) {
1718 return nullptr;
1719 }
1720 Json::Value point = command[SKDEBUGCANVAS_ATTRIBUTE_COORDS];
1721 SkPaint* paintPtr;
1722 SkPaint paint;
1723 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
1724 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
1725 paintPtr = &paint;
1726 }
1727 else {
1728 paintPtr = nullptr;
1729 }
1730 SkDrawBitmapCommand* result = new SkDrawBitmapCommand(*bitmap, point[0].asFloat(),
1731 point[1].asFloat(), paintPtr);
1732 delete bitmap;
1733 return result;
1734}
1735
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00001736SkDrawBitmapNineCommand::SkDrawBitmapNineCommand(const SkBitmap& bitmap, const SkIRect& center,
skia.committer@gmail.comdb35dab2014-03-27 03:02:48 +00001737 const SkRect& dst, const SkPaint* paint)
robertphillips9bafc302015-02-13 11:13:00 -08001738 : INHERITED(kDrawBitmapNine_OpType) {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001739 fBitmap = bitmap;
1740 fCenter = center;
1741 fDst = dst;
bsalomon49f085d2014-09-05 13:34:00 -07001742 if (paint) {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001743 fPaint = *paint;
1744 fPaintPtr = &fPaint;
1745 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001746 fPaintPtr = nullptr;
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001747 }
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001748
1749 fInfo.push(SkObjectParser::BitmapToString(bitmap));
1750 fInfo.push(SkObjectParser::IRectToString(center));
1751 fInfo.push(SkObjectParser::RectToString(dst, "Dst: "));
bsalomon49f085d2014-09-05 13:34:00 -07001752 if (paint) {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001753 fInfo.push(SkObjectParser::PaintToString(*paint));
robertphillips@google.comb83b6b42013-01-22 14:32:09 +00001754 }
chudy@google.com902ebe52012-06-29 14:21:22 +00001755}
1756
fmalita8c89c522014-11-08 16:18:56 -08001757void SkDrawBitmapNineCommand::execute(SkCanvas* canvas) const {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00001758 canvas->drawBitmapNine(fBitmap, fCenter, fDst, fPaintPtr);
chudy@google.com902ebe52012-06-29 14:21:22 +00001759}
1760
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00001761bool SkDrawBitmapNineCommand::render(SkCanvas* canvas) const {
robertphillips2b829822015-11-18 12:59:42 -08001762 SkRect tmp = SkRect::Make(fCenter);
1763 render_bitmap(canvas, fBitmap, &tmp);
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00001764 return true;
robertphillips@google.com53ec73d2012-11-26 13:09:17 +00001765}
1766
ethannicholas50a8dd02016-02-10 05:40:46 -08001767Json::Value SkDrawBitmapNineCommand::toJSON() const {
1768 Json::Value result = INHERITED::toJSON();
1769 Json::Value encoded;
1770 if (flatten(fBitmap, &encoded, SKDEBUGCANVAS_SEND_BINARIES)) {
1771 result[SKDEBUGCANVAS_ATTRIBUTE_BITMAP] = encoded;
1772 result[SKDEBUGCANVAS_ATTRIBUTE_CENTER] = make_json_irect(fCenter);
1773 result[SKDEBUGCANVAS_ATTRIBUTE_DST] = make_json_rect(fDst);
1774 if (fPaintPtr != nullptr) {
1775 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaintPtr,
1776 SKDEBUGCANVAS_SEND_BINARIES);
1777 }
1778 }
1779 return result;
1780}
1781
1782SkDrawBitmapNineCommand* SkDrawBitmapNineCommand::fromJSON(Json::Value& command) {
1783 SkBitmap* bitmap = load_bitmap(command[SKDEBUGCANVAS_ATTRIBUTE_BITMAP]);
1784 if (bitmap == nullptr) {
1785 return nullptr;
1786 }
1787 SkIRect center;
1788 extract_json_irect(command[SKDEBUGCANVAS_ATTRIBUTE_CENTER], &center);
1789 SkRect dst;
1790 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_DST], &dst);
1791 SkPaint* paintPtr;
1792 SkPaint paint;
1793 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
1794 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
1795 paintPtr = &paint;
1796 }
1797 else {
1798 paintPtr = nullptr;
1799 }
1800 SkDrawBitmapNineCommand* result = new SkDrawBitmapNineCommand(*bitmap, center, dst, paintPtr);
1801 delete bitmap;
1802 return result;
1803}
1804
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00001805SkDrawBitmapRectCommand::SkDrawBitmapRectCommand(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001806 const SkRect& dst, const SkPaint* paint,
reeda5517e22015-07-14 10:54:12 -07001807 SkCanvas::SrcRectConstraint constraint)
robertphillips9bafc302015-02-13 11:13:00 -08001808 : INHERITED(kDrawBitmapRect_OpType) {
robertphillips@google.com91217d02013-03-17 18:33:46 +00001809 fBitmap = bitmap;
bsalomon49f085d2014-09-05 13:34:00 -07001810 if (src) {
robertphillips@google.com91217d02013-03-17 18:33:46 +00001811 fSrc = *src;
1812 } else {
1813 fSrc.setEmpty();
robertphillips@google.comb83b6b42013-01-22 14:32:09 +00001814 }
robertphillips@google.com91217d02013-03-17 18:33:46 +00001815 fDst = dst;
1816
bsalomon49f085d2014-09-05 13:34:00 -07001817 if (paint) {
robertphillips@google.com91217d02013-03-17 18:33:46 +00001818 fPaint = *paint;
1819 fPaintPtr = &fPaint;
1820 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001821 fPaintPtr = nullptr;
robertphillips@google.com91217d02013-03-17 18:33:46 +00001822 }
reeda5517e22015-07-14 10:54:12 -07001823 fConstraint = constraint;
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001824
robertphillips@google.com91217d02013-03-17 18:33:46 +00001825 fInfo.push(SkObjectParser::BitmapToString(bitmap));
bsalomon49f085d2014-09-05 13:34:00 -07001826 if (src) {
robertphillips@google.com91217d02013-03-17 18:33:46 +00001827 fInfo.push(SkObjectParser::RectToString(*src, "Src: "));
1828 }
1829 fInfo.push(SkObjectParser::RectToString(dst, "Dst: "));
bsalomon49f085d2014-09-05 13:34:00 -07001830 if (paint) {
robertphillips@google.com91217d02013-03-17 18:33:46 +00001831 fInfo.push(SkObjectParser::PaintToString(*paint));
robertphillips@google.comb83b6b42013-01-22 14:32:09 +00001832 }
reeda5517e22015-07-14 10:54:12 -07001833 fInfo.push(SkObjectParser::IntToString(fConstraint, "Constraint: "));
chudy@google.com902ebe52012-06-29 14:21:22 +00001834}
1835
fmalita8c89c522014-11-08 16:18:56 -08001836void SkDrawBitmapRectCommand::execute(SkCanvas* canvas) const {
reede47829b2015-08-06 10:02:53 -07001837 canvas->legacy_drawBitmapRect(fBitmap, this->srcRect(), fDst, fPaintPtr, fConstraint);
chudy@google.com902ebe52012-06-29 14:21:22 +00001838}
1839
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00001840bool SkDrawBitmapRectCommand::render(SkCanvas* canvas) const {
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00001841 render_bitmap(canvas, fBitmap, this->srcRect());
1842 return true;
robertphillips@google.com53ec73d2012-11-26 13:09:17 +00001843}
1844
ethannicholas50a8dd02016-02-10 05:40:46 -08001845Json::Value SkDrawBitmapRectCommand::toJSON() const {
1846 Json::Value result = INHERITED::toJSON();
1847 Json::Value encoded;
1848 if (flatten(fBitmap, &encoded, SKDEBUGCANVAS_SEND_BINARIES)) {
1849 result[SKDEBUGCANVAS_ATTRIBUTE_BITMAP] = encoded;
1850 if (!fSrc.isEmpty()) {
1851 result[SKDEBUGCANVAS_ATTRIBUTE_SRC] = make_json_rect(fSrc);
1852 }
1853 result[SKDEBUGCANVAS_ATTRIBUTE_DST] = make_json_rect(fDst);
1854 if (fPaintPtr != nullptr) {
1855 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaintPtr,
1856 SKDEBUGCANVAS_SEND_BINARIES);
1857 }
1858 if (fConstraint == SkCanvas::kStrict_SrcRectConstraint) {
1859 result[SKDEBUGCANVAS_ATTRIBUTE_STRICT] = Json::Value(true);
1860 }
1861 }
1862 return result;
1863}
1864
1865SkDrawBitmapRectCommand* SkDrawBitmapRectCommand::fromJSON(Json::Value& command) {
1866 SkBitmap* bitmap = load_bitmap(command[SKDEBUGCANVAS_ATTRIBUTE_BITMAP]);
1867 if (bitmap == nullptr) {
1868 return nullptr;
1869 }
1870 SkRect dst;
1871 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_DST], &dst);
1872 SkPaint* paintPtr;
1873 SkPaint paint;
1874 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
1875 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
1876 paintPtr = &paint;
1877 }
1878 else {
1879 paintPtr = nullptr;
1880 }
1881 SkCanvas::SrcRectConstraint constraint;
1882 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_STRICT) &&
1883 command[SKDEBUGCANVAS_ATTRIBUTE_STRICT].asBool()) {
1884 constraint = SkCanvas::kStrict_SrcRectConstraint;
1885 }
1886 else {
1887 constraint = SkCanvas::kFast_SrcRectConstraint;
1888 }
1889 SkRect* srcPtr;
1890 SkRect src;
1891 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_SRC)) {
1892 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_SRC], &src);
1893 srcPtr = &src;
1894 }
1895 else {
1896 srcPtr = nullptr;
1897 }
1898 SkDrawBitmapRectCommand* result = new SkDrawBitmapRectCommand(*bitmap, srcPtr, dst, paintPtr,
1899 constraint);
1900 delete bitmap;
1901 return result;
1902}
1903
fmalita651c9202015-07-22 10:23:01 -07001904SkDrawImageCommand::SkDrawImageCommand(const SkImage* image, SkScalar left, SkScalar top,
1905 const SkPaint* paint)
1906 : INHERITED(kDrawImage_OpType)
1907 , fImage(SkRef(image))
1908 , fLeft(left)
1909 , fTop(top) {
1910
fmalita7361bf02015-10-29 12:13:48 -07001911 fInfo.push(SkObjectParser::ImageToString(image));
1912 fInfo.push(SkObjectParser::ScalarToString(left, "Left: "));
1913 fInfo.push(SkObjectParser::ScalarToString(top, "Top: "));
1914
fmalita651c9202015-07-22 10:23:01 -07001915 if (paint) {
1916 fPaint.set(*paint);
fmalita7361bf02015-10-29 12:13:48 -07001917 fInfo.push(SkObjectParser::PaintToString(*paint));
fmalita651c9202015-07-22 10:23:01 -07001918 }
1919}
1920
1921void SkDrawImageCommand::execute(SkCanvas* canvas) const {
1922 canvas->drawImage(fImage, fLeft, fTop, fPaint.getMaybeNull());
1923}
1924
1925bool SkDrawImageCommand::render(SkCanvas* canvas) const {
1926 SkAutoCanvasRestore acr(canvas, true);
1927 canvas->clear(0xFFFFFFFF);
1928
1929 xlate_and_scale_to_bounds(canvas, SkRect::MakeXYWH(fLeft, fTop,
1930 SkIntToScalar(fImage->width()),
1931 SkIntToScalar(fImage->height())));
1932 this->execute(canvas);
1933 return true;
1934}
1935
ethannicholas50a8dd02016-02-10 05:40:46 -08001936Json::Value SkDrawImageCommand::toJSON() const {
1937 Json::Value result = INHERITED::toJSON();
1938 Json::Value encoded;
1939 if (flatten(*fImage, &encoded, SKDEBUGCANVAS_SEND_BINARIES)) {
1940 result[SKDEBUGCANVAS_ATTRIBUTE_IMAGE] = encoded;
1941 result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_point(fLeft, fTop);
1942 if (fPaint.isValid()) {
1943 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaint.get(),
1944 SKDEBUGCANVAS_SEND_BINARIES);
1945 }
1946 }
1947 return result;
1948}
1949
1950SkDrawImageCommand* SkDrawImageCommand::fromJSON(Json::Value& command) {
1951 SkImage* image = load_image(command[SKDEBUGCANVAS_ATTRIBUTE_IMAGE]);
1952 if (image == nullptr) {
1953 return nullptr;
1954 }
1955 Json::Value point = command[SKDEBUGCANVAS_ATTRIBUTE_COORDS];
1956 SkPaint* paintPtr;
1957 SkPaint paint;
1958 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
1959 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
1960 paintPtr = &paint;
1961 }
1962 else {
1963 paintPtr = nullptr;
1964 }
1965 SkDrawImageCommand* result = new SkDrawImageCommand(image, point[0].asFloat(),
1966 point[1].asFloat(), paintPtr);
1967 image->unref();
1968 return result;
1969}
1970
fmalita651c9202015-07-22 10:23:01 -07001971SkDrawImageRectCommand::SkDrawImageRectCommand(const SkImage* image, const SkRect* src,
1972 const SkRect& dst, const SkPaint* paint,
1973 SkCanvas::SrcRectConstraint constraint)
1974 : INHERITED(kDrawImageRect_OpType)
1975 , fImage(SkRef(image))
1976 , fDst(dst)
1977 , fConstraint(constraint) {
1978
1979 if (src) {
1980 fSrc.set(*src);
1981 }
1982
1983 if (paint) {
1984 fPaint.set(*paint);
1985 }
robertphillips80af6452015-08-24 08:27:38 -07001986
1987 fInfo.push(SkObjectParser::ImageToString(image));
1988 if (src) {
1989 fInfo.push(SkObjectParser::RectToString(*src, "Src: "));
1990 }
1991 fInfo.push(SkObjectParser::RectToString(dst, "Dst: "));
1992 if (paint) {
1993 fInfo.push(SkObjectParser::PaintToString(*paint));
1994 }
1995 fInfo.push(SkObjectParser::IntToString(fConstraint, "Constraint: "));
fmalita651c9202015-07-22 10:23:01 -07001996}
1997
1998void SkDrawImageRectCommand::execute(SkCanvas* canvas) const {
ethannicholas1446a9a2016-02-10 14:05:02 -08001999 canvas->legacy_drawImageRect(fImage, fSrc.getMaybeNull(), fDst, fPaint.getMaybeNull(),
2000 fConstraint);
fmalita651c9202015-07-22 10:23:01 -07002001}
2002
2003bool SkDrawImageRectCommand::render(SkCanvas* canvas) const {
2004 SkAutoCanvasRestore acr(canvas, true);
2005 canvas->clear(0xFFFFFFFF);
2006
2007 xlate_and_scale_to_bounds(canvas, fDst);
2008
2009 this->execute(canvas);
2010 return true;
2011}
2012
ethannicholas50a8dd02016-02-10 05:40:46 -08002013Json::Value SkDrawImageRectCommand::toJSON() const {
2014 Json::Value result = INHERITED::toJSON();
2015 Json::Value encoded;
2016 if (flatten(*fImage.get(), &encoded, SKDEBUGCANVAS_SEND_BINARIES)) {
2017 result[SKDEBUGCANVAS_ATTRIBUTE_BITMAP] = encoded;
2018 if (fSrc.isValid()) {
2019 result[SKDEBUGCANVAS_ATTRIBUTE_SRC] = make_json_rect(*fSrc.get());
2020 }
2021 result[SKDEBUGCANVAS_ATTRIBUTE_DST] = make_json_rect(fDst);
2022 if (fPaint.isValid()) {
2023 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaint.get(),
2024 SKDEBUGCANVAS_SEND_BINARIES);
2025 }
2026 if (fConstraint == SkCanvas::kStrict_SrcRectConstraint) {
2027 result[SKDEBUGCANVAS_ATTRIBUTE_STRICT] = Json::Value(true);
2028 }
2029 }
2030 return result;
2031}
2032
2033SkDrawImageRectCommand* SkDrawImageRectCommand::fromJSON(Json::Value& command) {
2034 SkImage* image = load_image(command[SKDEBUGCANVAS_ATTRIBUTE_IMAGE]);
2035 if (image == nullptr) {
2036 return nullptr;
2037 }
2038 SkRect dst;
2039 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_DST], &dst);
2040 SkPaint* paintPtr;
2041 SkPaint paint;
2042 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
2043 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2044 paintPtr = &paint;
2045 }
2046 else {
2047 paintPtr = nullptr;
2048 }
2049 SkCanvas::SrcRectConstraint constraint;
2050 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_STRICT) &&
2051 command[SKDEBUGCANVAS_ATTRIBUTE_STRICT].asBool()) {
2052 constraint = SkCanvas::kStrict_SrcRectConstraint;
2053 }
2054 else {
2055 constraint = SkCanvas::kFast_SrcRectConstraint;
2056 }
2057 SkRect* srcPtr;
2058 SkRect src;
2059 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_SRC)) {
2060 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_SRC], &src);
2061 srcPtr = &src;
2062 }
2063 else {
2064 srcPtr = nullptr;
2065 }
2066 SkDrawImageRectCommand* result = new SkDrawImageRectCommand(image, srcPtr, dst, paintPtr,
2067 constraint);
2068 image->unref();
2069 return result;
2070}
2071
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002072SkDrawOvalCommand::SkDrawOvalCommand(const SkRect& oval, const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002073 : INHERITED(kDrawOval_OpType) {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002074 fOval = oval;
2075 fPaint = paint;
robertphillips@google.com67baba42013-01-02 20:20:31 +00002076
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002077 fInfo.push(SkObjectParser::RectToString(oval));
2078 fInfo.push(SkObjectParser::PaintToString(paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +00002079}
2080
fmalita8c89c522014-11-08 16:18:56 -08002081void SkDrawOvalCommand::execute(SkCanvas* canvas) const {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002082 canvas->drawOval(fOval, fPaint);
robertphillips@google.com67baba42013-01-02 20:20:31 +00002083}
2084
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002085bool SkDrawOvalCommand::render(SkCanvas* canvas) const {
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00002086 canvas->clear(0xFFFFFFFF);
2087 canvas->save();
2088
2089 xlate_and_scale_to_bounds(canvas, fOval);
2090
2091 SkPaint p;
2092 p.setColor(SK_ColorBLACK);
2093 p.setStyle(SkPaint::kStroke_Style);
2094
2095 canvas->drawOval(fOval, p);
2096 canvas->restore();
2097
2098 return true;
2099}
2100
ethannicholas50a8dd02016-02-10 05:40:46 -08002101Json::Value SkDrawOvalCommand::toJSON() const {
2102 Json::Value result = INHERITED::toJSON();
2103 result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_rect(fOval);
2104 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
2105 return result;
2106}
2107
2108SkDrawOvalCommand* SkDrawOvalCommand::fromJSON(Json::Value& command) {
2109 SkRect coords;
2110 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_COORDS], &coords);
2111 SkPaint paint;
2112 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2113 return new SkDrawOvalCommand(coords, paint);
2114}
2115
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002116SkDrawPaintCommand::SkDrawPaintCommand(const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002117 : INHERITED(kDrawPaint_OpType) {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002118 fPaint = paint;
chudy@google.com902ebe52012-06-29 14:21:22 +00002119
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002120 fInfo.push(SkObjectParser::PaintToString(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +00002121}
2122
fmalita8c89c522014-11-08 16:18:56 -08002123void SkDrawPaintCommand::execute(SkCanvas* canvas) const {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002124 canvas->drawPaint(fPaint);
chudy@google.com902ebe52012-06-29 14:21:22 +00002125}
2126
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002127bool SkDrawPaintCommand::render(SkCanvas* canvas) const {
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00002128 canvas->clear(0xFFFFFFFF);
2129 canvas->drawPaint(fPaint);
2130 return true;
2131}
2132
ethannicholas50a8dd02016-02-10 05:40:46 -08002133Json::Value SkDrawPaintCommand::toJSON() const {
2134 Json::Value result = INHERITED::toJSON();
2135 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
2136 return result;
2137}
2138
2139SkDrawPaintCommand* SkDrawPaintCommand::fromJSON(Json::Value& command) {
2140 SkPaint paint;
2141 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2142 return new SkDrawPaintCommand(paint);
2143}
2144
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002145SkDrawPathCommand::SkDrawPathCommand(const SkPath& path, const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002146 : INHERITED(kDrawPath_OpType) {
robertphillips@google.com91217d02013-03-17 18:33:46 +00002147 fPath = path;
2148 fPaint = paint;
chudy@google.com902ebe52012-06-29 14:21:22 +00002149
robertphillips@google.com91217d02013-03-17 18:33:46 +00002150 fInfo.push(SkObjectParser::PathToString(path));
2151 fInfo.push(SkObjectParser::PaintToString(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +00002152}
2153
fmalita8c89c522014-11-08 16:18:56 -08002154void SkDrawPathCommand::execute(SkCanvas* canvas) const {
robertphillips@google.com91217d02013-03-17 18:33:46 +00002155 canvas->drawPath(fPath, fPaint);
chudy@google.com902ebe52012-06-29 14:21:22 +00002156}
2157
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002158bool SkDrawPathCommand::render(SkCanvas* canvas) const {
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00002159 render_path(canvas, fPath);
2160 return true;
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +00002161}
2162
ethannicholas50a8dd02016-02-10 05:40:46 -08002163Json::Value SkDrawPathCommand::toJSON() const {
2164 Json::Value result = INHERITED::toJSON();
2165 result[SKDEBUGCANVAS_ATTRIBUTE_PATH] = make_json_path(fPath);
2166 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
2167 return result;
2168}
2169
2170SkDrawPathCommand* SkDrawPathCommand::fromJSON(Json::Value& command) {
2171 SkPath path;
2172 extract_json_path(command[SKDEBUGCANVAS_ATTRIBUTE_PATH], &path);
2173 SkPaint paint;
2174 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2175 return new SkDrawPathCommand(path, paint);
2176}
2177
fmalita160ebb22015-04-01 20:58:37 -07002178SkBeginDrawPictureCommand::SkBeginDrawPictureCommand(const SkPicture* picture,
2179 const SkMatrix* matrix,
2180 const SkPaint* paint)
2181 : INHERITED(kBeginDrawPicture_OpType)
2182 , fPicture(SkRef(picture)) {
2183
2184 SkString* str = new SkString;
2185 str->appendf("SkPicture: L: %f T: %f R: %f B: %f",
2186 picture->cullRect().fLeft, picture->cullRect().fTop,
2187 picture->cullRect().fRight, picture->cullRect().fBottom);
2188 fInfo.push(str);
robertphillipsb3f319f2014-08-13 10:46:23 -07002189
bsalomon49f085d2014-09-05 13:34:00 -07002190 if (matrix) {
fmalita160ebb22015-04-01 20:58:37 -07002191 fMatrix.set(*matrix);
robertphillipsb3f319f2014-08-13 10:46:23 -07002192 fInfo.push(SkObjectParser::MatrixToString(*matrix));
2193 }
fmalita160ebb22015-04-01 20:58:37 -07002194
bsalomon49f085d2014-09-05 13:34:00 -07002195 if (paint) {
fmalita160ebb22015-04-01 20:58:37 -07002196 fPaint.set(*paint);
robertphillipsb3f319f2014-08-13 10:46:23 -07002197 fInfo.push(SkObjectParser::PaintToString(*paint));
2198 }
fmalita160ebb22015-04-01 20:58:37 -07002199
2200}
2201
2202void SkBeginDrawPictureCommand::execute(SkCanvas* canvas) const {
2203 if (fPaint.isValid()) {
2204 SkRect bounds = fPicture->cullRect();
2205 if (fMatrix.isValid()) {
2206 fMatrix.get()->mapRect(&bounds);
2207 }
2208 canvas->saveLayer(&bounds, fPaint.get());
2209 }
2210
2211 if (fMatrix.isValid()) {
2212 if (!fPaint.isValid()) {
2213 canvas->save();
2214 }
2215 canvas->concat(*fMatrix.get());
2216 }
chudy@google.com902ebe52012-06-29 14:21:22 +00002217}
2218
fmalita160ebb22015-04-01 20:58:37 -07002219bool SkBeginDrawPictureCommand::render(SkCanvas* canvas) const {
commit-bot@chromium.orge898e9c2013-11-21 17:08:12 +00002220 canvas->clear(0xFFFFFFFF);
2221 canvas->save();
2222
robertphillipsa8d7f0b2014-08-29 08:03:56 -07002223 xlate_and_scale_to_bounds(canvas, fPicture->cullRect());
commit-bot@chromium.orge898e9c2013-11-21 17:08:12 +00002224
robertphillips9b14f262014-06-04 05:40:44 -07002225 canvas->drawPicture(fPicture.get());
commit-bot@chromium.orge898e9c2013-11-21 17:08:12 +00002226
2227 canvas->restore();
2228
2229 return true;
2230}
2231
fmalita160ebb22015-04-01 20:58:37 -07002232SkEndDrawPictureCommand::SkEndDrawPictureCommand(bool restore)
2233 : INHERITED(kEndDrawPicture_OpType) , fRestore(restore) { }
2234
2235void SkEndDrawPictureCommand::execute(SkCanvas* canvas) const {
2236 if (fRestore) {
2237 canvas->restore();
2238 }
2239}
2240
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002241SkDrawPointsCommand::SkDrawPointsCommand(SkCanvas::PointMode mode, size_t count,
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002242 const SkPoint pts[], const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002243 : INHERITED(kDrawPoints_OpType) {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002244 fMode = mode;
2245 fCount = count;
2246 fPts = new SkPoint[count];
2247 memcpy(fPts, pts, count * sizeof(SkPoint));
2248 fPaint = paint;
chudy@google.com902ebe52012-06-29 14:21:22 +00002249
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002250 fInfo.push(SkObjectParser::PointsToString(pts, count));
2251 fInfo.push(SkObjectParser::ScalarToString(SkIntToScalar((unsigned int)count),
2252 "Points: "));
2253 fInfo.push(SkObjectParser::PointModeToString(mode));
2254 fInfo.push(SkObjectParser::PaintToString(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +00002255}
2256
fmalita8c89c522014-11-08 16:18:56 -08002257void SkDrawPointsCommand::execute(SkCanvas* canvas) const {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002258 canvas->drawPoints(fMode, fCount, fPts, fPaint);
chudy@google.com902ebe52012-06-29 14:21:22 +00002259}
2260
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002261bool SkDrawPointsCommand::render(SkCanvas* canvas) const {
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00002262 canvas->clear(0xFFFFFFFF);
2263 canvas->save();
2264
2265 SkRect bounds;
2266
2267 bounds.setEmpty();
2268 for (unsigned int i = 0; i < fCount; ++i) {
2269 bounds.growToInclude(fPts[i].fX, fPts[i].fY);
2270 }
skia.committer@gmail.coma0090832013-06-07 07:01:06 +00002271
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00002272 xlate_and_scale_to_bounds(canvas, bounds);
2273
2274 SkPaint p;
2275 p.setColor(SK_ColorBLACK);
2276 p.setStyle(SkPaint::kStroke_Style);
2277
2278 canvas->drawPoints(fMode, fCount, fPts, p);
2279 canvas->restore();
2280
2281 return true;
2282}
2283
ethannicholas50a8dd02016-02-10 05:40:46 -08002284Json::Value SkDrawPointsCommand::toJSON() const {
2285 Json::Value result = INHERITED::toJSON();
2286 result[SKDEBUGCANVAS_ATTRIBUTE_MODE] = make_json_pointmode(fMode);
2287 Json::Value points(Json::arrayValue);
2288 for (size_t i = 0; i < fCount; i++) {
2289 points.append(make_json_point(fPts[i]));
2290 }
2291 result[SKDEBUGCANVAS_ATTRIBUTE_POINTS] = points;
2292 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
2293 return result;
2294}
2295
2296SkDrawPointsCommand* SkDrawPointsCommand::fromJSON(Json::Value& command) {
2297 SkCanvas::PointMode mode;
2298 const char* jsonMode = command[SKDEBUGCANVAS_ATTRIBUTE_MODE].asCString();
2299 if (!strcmp(jsonMode, SKDEBUGCANVAS_POINTMODE_POINTS)) {
2300 mode = SkCanvas::kPoints_PointMode;
2301 }
2302 else if (!strcmp(jsonMode, SKDEBUGCANVAS_POINTMODE_LINES)) {
2303 mode = SkCanvas::kLines_PointMode;
2304 }
2305 else if (!strcmp(jsonMode, SKDEBUGCANVAS_POINTMODE_POLYGON)) {
2306 mode = SkCanvas::kPolygon_PointMode;
2307 }
2308 else {
2309 SkASSERT(false);
2310 return nullptr;
2311 }
2312 Json::Value jsonPoints = command[SKDEBUGCANVAS_ATTRIBUTE_POINTS];
2313 int count = (int) jsonPoints.size();
2314 SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint));
2315 for (int i = 0; i < count; i++) {
2316 points[i] = SkPoint::Make(jsonPoints[i][0].asFloat(), jsonPoints[i][1].asFloat());
2317 }
2318 SkPaint paint;
2319 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2320 SkDrawPointsCommand* result = new SkDrawPointsCommand(mode, count, points, paint);
2321 sk_free(points);
2322 return result;
2323}
2324
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002325SkDrawPosTextCommand::SkDrawPosTextCommand(const void* text, size_t byteLength,
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002326 const SkPoint pos[], const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002327 : INHERITED(kDrawPosText_OpType) {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002328 size_t numPts = paint.countText(text, byteLength);
chudy@google.com902ebe52012-06-29 14:21:22 +00002329
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002330 fText = new char[byteLength];
2331 memcpy(fText, text, byteLength);
2332 fByteLength = byteLength;
2333
2334 fPos = new SkPoint[numPts];
2335 memcpy(fPos, pos, numPts * sizeof(SkPoint));
2336
2337 fPaint = paint;
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002338
2339 fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding()));
chudy@google.com902ebe52012-06-29 14:21:22 +00002340 // TODO(chudy): Test that this works.
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002341 fInfo.push(SkObjectParser::PointsToString(pos, 1));
2342 fInfo.push(SkObjectParser::PaintToString(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +00002343}
2344
fmalita8c89c522014-11-08 16:18:56 -08002345void SkDrawPosTextCommand::execute(SkCanvas* canvas) const {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002346 canvas->drawPosText(fText, fByteLength, fPos, fPaint);
chudy@google.com902ebe52012-06-29 14:21:22 +00002347}
2348
ethannicholas50a8dd02016-02-10 05:40:46 -08002349Json::Value SkDrawPosTextCommand::toJSON() const {
2350 Json::Value result = INHERITED::toJSON();
2351 result[SKDEBUGCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) fText,
2352 ((const char*) fText) + fByteLength);
2353 Json::Value coords(Json::arrayValue);
2354 for (size_t i = 0; i < fByteLength; i++) {
2355 coords.append(make_json_point(fPos[i]));
2356 }
2357 result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = coords;
2358 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
2359 return result;
2360}
2361
2362SkDrawPosTextCommand* SkDrawPosTextCommand::fromJSON(Json::Value& command) {
2363 const char* text = command[SKDEBUGCANVAS_ATTRIBUTE_TEXT].asCString();
2364 SkPaint paint;
2365 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2366 Json::Value coords = command[SKDEBUGCANVAS_ATTRIBUTE_COORDS];
2367 int count = (int) coords.size();
2368 SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint));
2369 for (int i = 0; i < count; i++) {
2370 points[i] = SkPoint::Make(coords[i][0].asFloat(), coords[i][1].asFloat());
2371 }
2372 return new SkDrawPosTextCommand(text, strlen(text), points, paint);
2373}
chudy@google.com902ebe52012-06-29 14:21:22 +00002374
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002375SkDrawPosTextHCommand::SkDrawPosTextHCommand(const void* text, size_t byteLength,
2376 const SkScalar xpos[], SkScalar constY,
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002377 const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002378 : INHERITED(kDrawPosTextH_OpType) {
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002379 size_t numPts = paint.countText(text, byteLength);
2380
2381 fText = new char[byteLength];
2382 memcpy(fText, text, byteLength);
robertphillips@google.com91217d02013-03-17 18:33:46 +00002383 fByteLength = byteLength;
robertphillips@google.coma3a09ab2013-03-22 12:25:30 +00002384
2385 fXpos = new SkScalar[numPts];
2386 memcpy(fXpos, xpos, numPts * sizeof(SkScalar));
2387
robertphillips@google.com91217d02013-03-17 18:33:46 +00002388 fConstY = constY;
2389 fPaint = paint;
chudy@google.com902ebe52012-06-29 14:21:22 +00002390
robertphillips@google.com91217d02013-03-17 18:33:46 +00002391 fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding()));
2392 fInfo.push(SkObjectParser::ScalarToString(xpos[0], "XPOS: "));
2393 fInfo.push(SkObjectParser::ScalarToString(constY, "SkScalar constY: "));
2394 fInfo.push(SkObjectParser::PaintToString(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +00002395}
2396
fmalita8c89c522014-11-08 16:18:56 -08002397void SkDrawPosTextHCommand::execute(SkCanvas* canvas) const {
robertphillips@google.com91217d02013-03-17 18:33:46 +00002398 canvas->drawPosTextH(fText, fByteLength, fXpos, fConstY, fPaint);
chudy@google.com902ebe52012-06-29 14:21:22 +00002399}
2400
fmalita0d48e412016-01-04 08:08:04 -08002401static const char* gPositioningLabels[] = {
2402 "kDefault_Positioning",
2403 "kHorizontal_Positioning",
2404 "kFull_Positioning",
2405};
2406
fmalitab7425172014-08-26 07:56:44 -07002407SkDrawTextBlobCommand::SkDrawTextBlobCommand(const SkTextBlob* blob, SkScalar x, SkScalar y,
2408 const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002409 : INHERITED(kDrawTextBlob_OpType)
fmalitae77f2da2015-12-08 18:59:18 -08002410 , fBlob(SkRef(blob))
fmalitab7425172014-08-26 07:56:44 -07002411 , fXPos(x)
2412 , fYPos(y)
2413 , fPaint(paint) {
2414
fmalitae77f2da2015-12-08 18:59:18 -08002415 SkAutoTDelete<SkString> runsStr(new SkString);
fmalitab7425172014-08-26 07:56:44 -07002416 fInfo.push(SkObjectParser::ScalarToString(x, "XPOS: "));
fmalitaff3106c2014-12-09 05:28:20 -08002417 fInfo.push(SkObjectParser::ScalarToString(y, "YPOS: "));
2418 fInfo.push(SkObjectParser::RectToString(fBlob->bounds(), "Bounds: "));
fmalitae77f2da2015-12-08 18:59:18 -08002419 fInfo.push(runsStr);
fmalitab7425172014-08-26 07:56:44 -07002420 fInfo.push(SkObjectParser::PaintToString(paint));
fmalitae77f2da2015-12-08 18:59:18 -08002421
2422 unsigned runs = 0;
2423 SkPaint runPaint(paint);
2424 SkTextBlobRunIterator iter(blob);
2425 while (!iter.done()) {
fmalita0d48e412016-01-04 08:08:04 -08002426 SkAutoTDelete<SkString> tmpStr(new SkString);
2427 tmpStr->printf("==== Run [%d] ====", runs++);
2428 fInfo.push(tmpStr.release());
fmalitae77f2da2015-12-08 18:59:18 -08002429
2430 fInfo.push(SkObjectParser::IntToString(iter.glyphCount(), "GlyphCount: "));
fmalita0d48e412016-01-04 08:08:04 -08002431 tmpStr.reset(new SkString("GlyphPositioning: "));
2432 tmpStr->append(gPositioningLabels[iter.positioning()]);
2433 fInfo.push(tmpStr.release());
2434
fmalitae77f2da2015-12-08 18:59:18 -08002435 iter.applyFontToPaint(&runPaint);
2436 fInfo.push(SkObjectParser::PaintToString(runPaint));
2437
2438 iter.next();
2439 }
2440
2441 runsStr->printf("Runs: %d", runs);
2442 // runStr is owned by fInfo at this point.
2443 runsStr.release();
fmalitab7425172014-08-26 07:56:44 -07002444}
2445
fmalita8c89c522014-11-08 16:18:56 -08002446void SkDrawTextBlobCommand::execute(SkCanvas* canvas) const {
fmalitab7425172014-08-26 07:56:44 -07002447 canvas->drawTextBlob(fBlob, fXPos, fYPos, fPaint);
2448}
2449
fmalita55773872014-08-29 15:08:20 -07002450bool SkDrawTextBlobCommand::render(SkCanvas* canvas) const {
2451 canvas->clear(SK_ColorWHITE);
2452 canvas->save();
2453
2454 SkRect bounds = fBlob->bounds().makeOffset(fXPos, fYPos);
2455 xlate_and_scale_to_bounds(canvas, bounds);
2456
2457 canvas->drawTextBlob(fBlob.get(), fXPos, fYPos, fPaint);
2458
2459 canvas->restore();
2460
2461 return true;
2462}
2463
ethannicholas50a8dd02016-02-10 05:40:46 -08002464Json::Value SkDrawTextBlobCommand::toJSON() const {
2465 Json::Value result = INHERITED::toJSON();
2466 Json::Value runs(Json::arrayValue);
2467 SkTextBlobRunIterator iter(fBlob.get());
2468 while (!iter.done()) {
2469 Json::Value run(Json::objectValue);
2470 Json::Value jsonPositions(Json::arrayValue);
2471 Json::Value jsonGlyphs(Json::arrayValue);
2472 const SkScalar* iterPositions = iter.pos();
2473 const uint16_t* iterGlyphs = iter.glyphs();
2474 for (uint32_t i = 0; i < iter.glyphCount(); i++) {
2475 switch (iter.positioning()) {
2476 case SkTextBlob::kFull_Positioning:
2477 jsonPositions.append(make_json_point(iterPositions[i * 2],
2478 iterPositions[i * 2 + 1]));
2479 break;
2480 case SkTextBlob::kHorizontal_Positioning:
2481 jsonPositions.append(Json::Value(iterPositions[i]));
2482 break;
2483 case SkTextBlob::kDefault_Positioning:
2484 break;
2485 }
2486 jsonGlyphs.append(Json::Value(iterGlyphs[i]));
2487 }
2488 if (iter.positioning() != SkTextBlob::kDefault_Positioning) {
2489 run[SKDEBUGCANVAS_ATTRIBUTE_POSITIONS] = jsonPositions;
2490 }
2491 run[SKDEBUGCANVAS_ATTRIBUTE_GLYPHS] = jsonGlyphs;
2492 SkPaint fontPaint;
2493 iter.applyFontToPaint(&fontPaint);
2494 run[SKDEBUGCANVAS_ATTRIBUTE_FONT] = make_json_paint(fontPaint, SKDEBUGCANVAS_SEND_BINARIES);
2495 run[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_point(iter.offset());
2496 runs.append(run);
2497 iter.next();
2498 }
2499 result[SKDEBUGCANVAS_ATTRIBUTE_RUNS] = runs;
2500 result[SKDEBUGCANVAS_ATTRIBUTE_X] = Json::Value(fXPos);
2501 result[SKDEBUGCANVAS_ATTRIBUTE_Y] = Json::Value(fYPos);
2502 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
2503 return result;
2504}
2505
2506SkDrawTextBlobCommand* SkDrawTextBlobCommand::fromJSON(Json::Value& command) {
2507 SkTextBlobBuilder builder;
2508 Json::Value runs = command[SKDEBUGCANVAS_ATTRIBUTE_RUNS];
2509 for (Json::ArrayIndex i = 0 ; i < runs.size(); i++) {
2510 Json::Value run = runs[i];
2511 SkPaint font;
2512 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
2513 extract_json_paint(run[SKDEBUGCANVAS_ATTRIBUTE_FONT], &font);
2514 Json::Value glyphs = run[SKDEBUGCANVAS_ATTRIBUTE_GLYPHS];
2515 int count = glyphs.size();
2516 Json::Value coords = run[SKDEBUGCANVAS_ATTRIBUTE_COORDS];
2517 SkScalar x = coords[0].asFloat();
2518 SkScalar y = coords[1].asFloat();
2519 if (run.isMember(SKDEBUGCANVAS_ATTRIBUTE_POSITIONS)) {
2520 Json::Value positions = run[SKDEBUGCANVAS_ATTRIBUTE_POSITIONS];
2521 if (positions.size() > 0 && positions[0].isNumeric()) {
2522 SkTextBlobBuilder::RunBuffer buffer = builder.allocRunPosH(font, count, y);
2523 for (int j = 0; j < count; j++) {
2524 buffer.glyphs[j] = glyphs[j].asUInt();
2525 buffer.pos[j] = positions[j].asFloat();
2526 }
2527 }
2528 else {
2529 SkTextBlobBuilder::RunBuffer buffer = builder.allocRunPos(font, count);
2530 for (int j = 0; j < count; j++) {
2531 buffer.glyphs[j] = glyphs[j].asUInt();
2532 buffer.pos[j * 2] = positions[j][0].asFloat();
2533 buffer.pos[j * 2 + 1] = positions[j][1].asFloat();
2534 }
2535 }
2536 }
2537 else {
2538 SkTextBlobBuilder::RunBuffer buffer = builder.allocRun(font, count, x, y);
2539 for (int j = 0; j < count; j++) {
2540 buffer.glyphs[j] = glyphs[j].asUInt();
2541 }
2542 }
2543 }
2544 SkScalar x = command[SKDEBUGCANVAS_ATTRIBUTE_X].asFloat();
2545 SkScalar y = command[SKDEBUGCANVAS_ATTRIBUTE_Y].asFloat();
2546 SkPaint paint;
2547 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2548 return new SkDrawTextBlobCommand(builder.build(), x, y, paint);
2549}
2550
robertphillips9bafc302015-02-13 11:13:00 -08002551SkDrawPatchCommand::SkDrawPatchCommand(const SkPoint cubics[12], const SkColor colors[4],
2552 const SkPoint texCoords[4], SkXfermode* xfermode,
2553 const SkPaint& paint)
2554 : INHERITED(kDrawPatch_OpType) {
2555 memcpy(fCubics, cubics, sizeof(fCubics));
ethannicholas1446a9a2016-02-10 14:05:02 -08002556 if (colors != nullptr) {
2557 memcpy(fColors, colors, sizeof(fColors));
2558 fColorsPtr = fColors;
2559 } else {
2560 fColorsPtr = nullptr;
2561 }
2562 if (texCoords != nullptr) {
2563 memcpy(fTexCoords, texCoords, sizeof(fTexCoords));
2564 fTexCoordsPtr = fTexCoords;
2565 } else {
2566 fTexCoordsPtr = nullptr;
2567 }
2568 if (xfermode != nullptr) {
2569 fXfermode.reset(SkRef(xfermode));
2570 }
robertphillips9bafc302015-02-13 11:13:00 -08002571 fPaint = paint;
2572
2573 fInfo.push(SkObjectParser::PaintToString(paint));
2574}
2575
2576void SkDrawPatchCommand::execute(SkCanvas* canvas) const {
ethannicholas1446a9a2016-02-10 14:05:02 -08002577 canvas->drawPatch(fCubics, fColorsPtr, fTexCoordsPtr, fXfermode, fPaint);
2578}
2579
2580Json::Value SkDrawPatchCommand::toJSON() const {
2581 Json::Value result = INHERITED::toJSON();
2582 Json::Value cubics = Json::Value(Json::arrayValue);
2583 for (int i = 0; i < 12; i++) {
2584 cubics.append(make_json_point(fCubics[i]));
2585 }
2586 result[SKDEBUGCANVAS_ATTRIBUTE_CUBICS] = cubics;
2587 if (fColorsPtr != nullptr) {
2588 Json::Value colors = Json::Value(Json::arrayValue);
2589 for (int i = 0; i < 4; i++) {
2590 colors.append(make_json_color(fColorsPtr[i]));
2591 }
2592 result[SKDEBUGCANVAS_ATTRIBUTE_COLORS] = colors;
2593 }
2594 if (fTexCoordsPtr != nullptr) {
2595 Json::Value texCoords = Json::Value(Json::arrayValue);
2596 for (int i = 0; i < 4; i++) {
2597 texCoords.append(make_json_point(fTexCoords[i]));
2598 }
2599 result[SKDEBUGCANVAS_ATTRIBUTE_TEXTURECOORDS] = texCoords;
2600 }
2601 if (fXfermode.get() != nullptr) {
2602 Json::Value jsonXfermode;
2603 flatten(fXfermode, &jsonXfermode, SKDEBUGCANVAS_SEND_BINARIES);
2604 result[SKDEBUGCANVAS_ATTRIBUTE_XFERMODE] = jsonXfermode;
2605 }
2606 return result;
2607}
2608
2609SkDrawPatchCommand* SkDrawPatchCommand::fromJSON(Json::Value& command) {
2610 Json::Value jsonCubics = command[SKDEBUGCANVAS_ATTRIBUTE_CUBICS];
2611 SkPoint cubics[12];
2612 for (int i = 0; i < 12; i++) {
2613 cubics[i] = get_json_point(jsonCubics[i]);
2614 }
2615 SkColor* colorsPtr;
2616 SkColor colors[4];
2617 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_COLORS)) {
2618 Json::Value jsonColors = command[SKDEBUGCANVAS_ATTRIBUTE_COLORS];
2619 for (int i = 0; i < 4; i++) {
2620 colors[i] = get_json_color(jsonColors[i]);
2621 }
2622 colorsPtr = colors;
2623 }
2624 else {
2625 colorsPtr = nullptr;
2626 }
2627 SkPoint* texCoordsPtr;
2628 SkPoint texCoords[4];
2629 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_TEXTURECOORDS)) {
2630 Json::Value jsonTexCoords = command[SKDEBUGCANVAS_ATTRIBUTE_TEXTURECOORDS];
2631 for (int i = 0; i < 4; i++) {
2632 texCoords[i] = get_json_point(jsonTexCoords[i]);
2633 }
2634 texCoordsPtr = texCoords;
2635 }
2636 else {
2637 texCoordsPtr = nullptr;
2638 }
2639 SkAutoTUnref<SkXfermode> xfermode;
2640 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_XFERMODE)) {
2641 Json::Value jsonXfermode = command[SKDEBUGCANVAS_ATTRIBUTE_XFERMODE];
2642 xfermode.reset((SkXfermode*) load_flattenable(jsonXfermode));
2643 }
2644 SkPaint paint;
2645 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2646 return new SkDrawPatchCommand(cubics, colorsPtr, texCoordsPtr, xfermode, paint);
robertphillips9bafc302015-02-13 11:13:00 -08002647}
2648
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002649SkDrawRectCommand::SkDrawRectCommand(const SkRect& rect, const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002650 : INHERITED(kDrawRect_OpType) {
robertphillips@google.com91217d02013-03-17 18:33:46 +00002651 fRect = rect;
2652 fPaint = paint;
chudy@google.com902ebe52012-06-29 14:21:22 +00002653
robertphillips@google.com91217d02013-03-17 18:33:46 +00002654 fInfo.push(SkObjectParser::RectToString(rect));
2655 fInfo.push(SkObjectParser::PaintToString(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +00002656}
2657
fmalita8c89c522014-11-08 16:18:56 -08002658void SkDrawRectCommand::execute(SkCanvas* canvas) const {
robertphillips@google.com91217d02013-03-17 18:33:46 +00002659 canvas->drawRect(fRect, fPaint);
chudy@google.com902ebe52012-06-29 14:21:22 +00002660}
2661
ethannicholas50a8dd02016-02-10 05:40:46 -08002662Json::Value SkDrawRectCommand::toJSON() const {
2663 Json::Value result = INHERITED::toJSON();
2664 result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_rect(fRect);
2665 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
2666 return result;
2667}
2668
2669SkDrawRectCommand* SkDrawRectCommand::fromJSON(Json::Value& command) {
2670 SkRect coords;
2671 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_COORDS], &coords);
2672 SkPaint paint;
2673 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2674 return new SkDrawRectCommand(coords, paint);
2675}
2676
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002677SkDrawRRectCommand::SkDrawRRectCommand(const SkRRect& rrect, const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002678 : INHERITED(kDrawRRect_OpType) {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002679 fRRect = rrect;
2680 fPaint = paint;
robertphillips@google.com67baba42013-01-02 20:20:31 +00002681
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002682 fInfo.push(SkObjectParser::RRectToString(rrect));
2683 fInfo.push(SkObjectParser::PaintToString(paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +00002684}
2685
fmalita8c89c522014-11-08 16:18:56 -08002686void SkDrawRRectCommand::execute(SkCanvas* canvas) const {
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +00002687 canvas->drawRRect(fRRect, fPaint);
robertphillips@google.com67baba42013-01-02 20:20:31 +00002688}
2689
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002690bool SkDrawRRectCommand::render(SkCanvas* canvas) const {
robertphillips@google.com6ede1fe2013-06-06 23:59:28 +00002691 render_rrect(canvas, fRRect);
2692 return true;
2693}
2694
ethannicholas50a8dd02016-02-10 05:40:46 -08002695Json::Value SkDrawRRectCommand::toJSON() const {
2696 Json::Value result = INHERITED::toJSON();
2697 result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_rrect(fRRect);
2698 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
2699 return result;
2700}
2701
2702SkDrawRRectCommand* SkDrawRRectCommand::fromJSON(Json::Value& command) {
2703 SkRRect coords;
2704 extract_json_rrect(command[SKDEBUGCANVAS_ATTRIBUTE_COORDS], &coords);
2705 SkPaint paint;
2706 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2707 return new SkDrawRRectCommand(coords, paint);
2708}
2709
skia.committer@gmail.com90667ba2014-02-25 03:05:18 +00002710SkDrawDRRectCommand::SkDrawDRRectCommand(const SkRRect& outer,
commit-bot@chromium.org3d305202014-02-24 17:28:55 +00002711 const SkRRect& inner,
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002712 const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002713 : INHERITED(kDrawDRRect_OpType) {
commit-bot@chromium.org3d305202014-02-24 17:28:55 +00002714 fOuter = outer;
2715 fInner = inner;
2716 fPaint = paint;
commit-bot@chromium.org3d305202014-02-24 17:28:55 +00002717
2718 fInfo.push(SkObjectParser::RRectToString(outer));
2719 fInfo.push(SkObjectParser::RRectToString(inner));
2720 fInfo.push(SkObjectParser::PaintToString(paint));
2721}
2722
fmalita8c89c522014-11-08 16:18:56 -08002723void SkDrawDRRectCommand::execute(SkCanvas* canvas) const {
commit-bot@chromium.org3d305202014-02-24 17:28:55 +00002724 canvas->drawDRRect(fOuter, fInner, fPaint);
2725}
2726
2727bool SkDrawDRRectCommand::render(SkCanvas* canvas) const {
2728 render_drrect(canvas, fOuter, fInner);
2729 return true;
2730}
2731
ethannicholas50a8dd02016-02-10 05:40:46 -08002732Json::Value SkDrawDRRectCommand::toJSON() const {
2733 Json::Value result = INHERITED::toJSON();
2734 result[SKDEBUGCANVAS_ATTRIBUTE_OUTER] = make_json_rrect(fOuter);
2735 result[SKDEBUGCANVAS_ATTRIBUTE_INNER] = make_json_rrect(fInner);
2736 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
2737 return result;
2738}
2739
2740SkDrawDRRectCommand* SkDrawDRRectCommand::fromJSON(Json::Value& command) {
2741 SkRRect outer;
2742 extract_json_rrect(command[SKDEBUGCANVAS_ATTRIBUTE_INNER], &outer);
2743 SkRRect inner;
2744 extract_json_rrect(command[SKDEBUGCANVAS_ATTRIBUTE_INNER], &inner);
2745 SkPaint paint;
2746 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2747 return new SkDrawDRRectCommand(outer, inner, paint);
2748}
2749
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002750SkDrawTextCommand::SkDrawTextCommand(const void* text, size_t byteLength, SkScalar x, SkScalar y,
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002751 const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002752 : INHERITED(kDrawText_OpType) {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002753 fText = new char[byteLength];
2754 memcpy(fText, text, byteLength);
2755 fByteLength = byteLength;
2756 fX = x;
2757 fY = y;
2758 fPaint = paint;
chudy@google.com902ebe52012-06-29 14:21:22 +00002759
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002760 fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding()));
2761 fInfo.push(SkObjectParser::ScalarToString(x, "SkScalar x: "));
2762 fInfo.push(SkObjectParser::ScalarToString(y, "SkScalar y: "));
2763 fInfo.push(SkObjectParser::PaintToString(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +00002764}
2765
fmalita8c89c522014-11-08 16:18:56 -08002766void SkDrawTextCommand::execute(SkCanvas* canvas) const {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002767 canvas->drawText(fText, fByteLength, fX, fY, fPaint);
chudy@google.com902ebe52012-06-29 14:21:22 +00002768}
2769
ethannicholas50a8dd02016-02-10 05:40:46 -08002770Json::Value SkDrawTextCommand::toJSON() const {
2771 Json::Value result = INHERITED::toJSON();
2772 result[SKDEBUGCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) fText,
2773 ((const char*) fText) + fByteLength);
2774 Json::Value coords(Json::arrayValue);
2775 result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_point(fX, fY);
2776 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
2777 return result;
2778}
2779
2780SkDrawTextCommand* SkDrawTextCommand::fromJSON(Json::Value& command) {
2781 const char* text = command[SKDEBUGCANVAS_ATTRIBUTE_TEXT].asCString();
2782 SkPaint paint;
2783 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2784 Json::Value coords = command[SKDEBUGCANVAS_ATTRIBUTE_COORDS];
2785 return new SkDrawTextCommand(text, strlen(text), coords[0].asFloat(), coords[1].asFloat(),
2786 paint);
2787}
2788
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002789SkDrawTextOnPathCommand::SkDrawTextOnPathCommand(const void* text, size_t byteLength,
2790 const SkPath& path, const SkMatrix* matrix,
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002791 const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002792 : INHERITED(kDrawTextOnPath_OpType) {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002793 fText = new char[byteLength];
2794 memcpy(fText, text, byteLength);
2795 fByteLength = byteLength;
2796 fPath = path;
bsalomon49f085d2014-09-05 13:34:00 -07002797 if (matrix) {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002798 fMatrix = *matrix;
2799 } else {
2800 fMatrix.setIdentity();
2801 }
2802 fPaint = paint;
chudy@google.com902ebe52012-06-29 14:21:22 +00002803
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002804 fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding()));
2805 fInfo.push(SkObjectParser::PathToString(path));
bsalomon49f085d2014-09-05 13:34:00 -07002806 if (matrix) {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002807 fInfo.push(SkObjectParser::MatrixToString(*matrix));
2808 }
2809 fInfo.push(SkObjectParser::PaintToString(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +00002810}
2811
fmalita8c89c522014-11-08 16:18:56 -08002812void SkDrawTextOnPathCommand::execute(SkCanvas* canvas) const {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002813 canvas->drawTextOnPath(fText, fByteLength, fPath,
halcanary96fcdcc2015-08-27 07:41:13 -07002814 fMatrix.isIdentity() ? nullptr : &fMatrix,
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002815 fPaint);
chudy@google.com902ebe52012-06-29 14:21:22 +00002816}
2817
ethannicholas50a8dd02016-02-10 05:40:46 -08002818Json::Value SkDrawTextOnPathCommand::toJSON() const {
2819 Json::Value result = INHERITED::toJSON();
2820 result[SKDEBUGCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) fText,
2821 ((const char*) fText) + fByteLength);
2822 Json::Value coords(Json::arrayValue);
2823 result[SKDEBUGCANVAS_ATTRIBUTE_PATH] = make_json_path(fPath);
2824 if (!fMatrix.isIdentity()) {
2825 result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = make_json_matrix(fMatrix);
2826 }
2827 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
2828 return result;
2829}
2830
2831SkDrawTextOnPathCommand* SkDrawTextOnPathCommand::fromJSON(Json::Value& command) {
2832 const char* text = command[SKDEBUGCANVAS_ATTRIBUTE_TEXT].asCString();
2833 SkPaint paint;
2834 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
2835 SkPath path;
2836 extract_json_path(command[SKDEBUGCANVAS_ATTRIBUTE_PATH], &path);
2837 SkMatrix* matrixPtr;
2838 SkMatrix matrix;
2839 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_MATRIX)) {
2840 extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix);
2841 matrixPtr = &matrix;
2842 }
2843 else {
2844 matrixPtr = nullptr;
2845 }
2846 return new SkDrawTextOnPathCommand(text, strlen(text), path, matrixPtr, paint);
2847}
2848
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002849SkDrawVerticesCommand::SkDrawVerticesCommand(SkCanvas::VertexMode vmode, int vertexCount,
2850 const SkPoint vertices[], const SkPoint texs[],
2851 const SkColor colors[], SkXfermode* xfermode,
2852 const uint16_t indices[], int indexCount,
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002853 const SkPaint& paint)
robertphillips9bafc302015-02-13 11:13:00 -08002854 : INHERITED(kDrawVertices_OpType) {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002855 fVmode = vmode;
2856
2857 fVertexCount = vertexCount;
2858
2859 fVertices = new SkPoint[vertexCount];
2860 memcpy(fVertices, vertices, vertexCount * sizeof(SkPoint));
2861
bsalomon49f085d2014-09-05 13:34:00 -07002862 if (texs) {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002863 fTexs = new SkPoint[vertexCount];
2864 memcpy(fTexs, texs, vertexCount * sizeof(SkPoint));
2865 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07002866 fTexs = nullptr;
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002867 }
2868
bsalomon49f085d2014-09-05 13:34:00 -07002869 if (colors) {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002870 fColors = new SkColor[vertexCount];
2871 memcpy(fColors, colors, vertexCount * sizeof(SkColor));
2872 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07002873 fColors = nullptr;
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002874 }
2875
2876 fXfermode = xfermode;
bsalomon49f085d2014-09-05 13:34:00 -07002877 if (fXfermode) {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002878 fXfermode->ref();
2879 }
2880
2881 if (indexCount > 0) {
2882 fIndices = new uint16_t[indexCount];
2883 memcpy(fIndices, indices, indexCount * sizeof(uint16_t));
2884 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07002885 fIndices = nullptr;
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002886 }
2887
2888 fIndexCount = indexCount;
2889 fPaint = paint;
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002890
chudy@google.com902ebe52012-06-29 14:21:22 +00002891 // TODO(chudy)
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002892 fInfo.push(SkObjectParser::CustomTextToString("To be implemented."));
2893 fInfo.push(SkObjectParser::PaintToString(paint));
2894}
2895
commit-bot@chromium.org7a115912013-06-18 20:20:55 +00002896SkDrawVerticesCommand::~SkDrawVerticesCommand() {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002897 delete [] fVertices;
2898 delete [] fTexs;
2899 delete [] fColors;
2900 SkSafeUnref(fXfermode);
2901 delete [] fIndices;
chudy@google.com902ebe52012-06-29 14:21:22 +00002902}
2903
fmalita8c89c522014-11-08 16:18:56 -08002904void SkDrawVerticesCommand::execute(SkCanvas* canvas) const {
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00002905 canvas->drawVertices(fVmode, fVertexCount, fVertices,
2906 fTexs, fColors, fXfermode, fIndices,
2907 fIndexCount, fPaint);
chudy@google.com902ebe52012-06-29 14:21:22 +00002908}
2909
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00002910SkRestoreCommand::SkRestoreCommand()
robertphillips9bafc302015-02-13 11:13:00 -08002911 : INHERITED(kRestore_OpType) {
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00002912 fInfo.push(SkObjectParser::CustomTextToString("No Parameters"));
chudy@google.com902ebe52012-06-29 14:21:22 +00002913}
2914
fmalita8c89c522014-11-08 16:18:56 -08002915void SkRestoreCommand::execute(SkCanvas* canvas) const {
chudy@google.com902ebe52012-06-29 14:21:22 +00002916 canvas->restore();
2917}
2918
ethannicholas50a8dd02016-02-10 05:40:46 -08002919SkRestoreCommand* SkRestoreCommand::fromJSON(Json::Value& command) {
2920 return new SkRestoreCommand();
2921}
2922
Florin Malita5f6102d2014-06-30 10:13:28 -04002923SkSaveCommand::SkSaveCommand()
robertphillips9bafc302015-02-13 11:13:00 -08002924 : INHERITED(kSave_OpType) {
chudy@google.com902ebe52012-06-29 14:21:22 +00002925}
2926
fmalita8c89c522014-11-08 16:18:56 -08002927void SkSaveCommand::execute(SkCanvas* canvas) const {
Florin Malita5f6102d2014-06-30 10:13:28 -04002928 canvas->save();
chudy@google.com902ebe52012-06-29 14:21:22 +00002929}
2930
ethannicholas50a8dd02016-02-10 05:40:46 -08002931SkSaveCommand* SkSaveCommand::fromJSON(Json::Value& command) {
2932 return new SkSaveCommand();
2933}
2934
reed4960eee2015-12-18 07:09:18 -08002935SkSaveLayerCommand::SkSaveLayerCommand(const SkCanvas::SaveLayerRec& rec)
robertphillips9bafc302015-02-13 11:13:00 -08002936 : INHERITED(kSaveLayer_OpType) {
reed4960eee2015-12-18 07:09:18 -08002937 if (rec.fBounds) {
2938 fBounds = *rec.fBounds;
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00002939 } else {
2940 fBounds.setEmpty();
2941 }
chudy@google.com902ebe52012-06-29 14:21:22 +00002942
reed4960eee2015-12-18 07:09:18 -08002943 if (rec.fPaint) {
2944 fPaint = *rec.fPaint;
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00002945 fPaintPtr = &fPaint;
2946 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07002947 fPaintPtr = nullptr;
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00002948 }
reed4960eee2015-12-18 07:09:18 -08002949 fSaveLayerFlags = rec.fSaveLayerFlags;
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00002950
ethannicholas50a8dd02016-02-10 05:40:46 -08002951 if (rec.fBackdrop) {
2952 fBackdrop = rec.fBackdrop;
2953 fBackdrop->ref();
2954 } else {
2955 fBackdrop = nullptr;
2956 }
2957
reed4960eee2015-12-18 07:09:18 -08002958 if (rec.fBounds) {
2959 fInfo.push(SkObjectParser::RectToString(*rec.fBounds, "Bounds: "));
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00002960 }
reed4960eee2015-12-18 07:09:18 -08002961 if (rec.fPaint) {
2962 fInfo.push(SkObjectParser::PaintToString(*rec.fPaint));
robertphillips@google.com24bfdac2013-03-22 16:33:31 +00002963 }
reed4960eee2015-12-18 07:09:18 -08002964 fInfo.push(SkObjectParser::SaveLayerFlagsToString(fSaveLayerFlags));
chudy@google.com902ebe52012-06-29 14:21:22 +00002965}
2966
ethannicholas50a8dd02016-02-10 05:40:46 -08002967SkSaveLayerCommand::~SkSaveLayerCommand() {
2968 if (fBackdrop != nullptr) {
2969 fBackdrop->unref();
2970 }
2971}
2972
fmalita8c89c522014-11-08 16:18:56 -08002973void SkSaveLayerCommand::execute(SkCanvas* canvas) const {
reed4960eee2015-12-18 07:09:18 -08002974 canvas->saveLayer(SkCanvas::SaveLayerRec(fBounds.isEmpty() ? nullptr : &fBounds,
2975 fPaintPtr,
2976 fSaveLayerFlags));
chudy@google.com902ebe52012-06-29 14:21:22 +00002977}
2978
fmalita8c89c522014-11-08 16:18:56 -08002979void SkSaveLayerCommand::vizExecute(SkCanvas* canvas) const {
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +00002980 canvas->save();
2981}
2982
ethannicholas50a8dd02016-02-10 05:40:46 -08002983Json::Value SkSaveLayerCommand::toJSON() const {
2984 Json::Value result = INHERITED::toJSON();
2985 if (!fBounds.isEmpty()) {
2986 result[SKDEBUGCANVAS_ATTRIBUTE_BOUNDS] = make_json_rect(fBounds);
2987 }
2988 if (fPaintPtr != nullptr) {
2989 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaintPtr,
2990 SKDEBUGCANVAS_SEND_BINARIES);
2991 }
2992 if (fBackdrop != nullptr) {
2993 Json::Value jsonBackdrop;
2994 flatten(fBackdrop, &jsonBackdrop, SKDEBUGCANVAS_SEND_BINARIES);
2995 result[SKDEBUGCANVAS_ATTRIBUTE_BACKDROP] = jsonBackdrop;
2996 }
2997 if (fSaveLayerFlags != 0) {
2998 SkDebugf("unsupported: saveLayer flags\n");
2999 SkASSERT(false);
3000 }
3001 return result;
3002}
3003
3004SkSaveLayerCommand* SkSaveLayerCommand::fromJSON(Json::Value& command) {
3005 SkCanvas::SaveLayerRec rec;
3006 SkRect bounds;
3007 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_BOUNDS)) {
3008 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_BOUNDS], &bounds);
3009 rec.fBounds = &bounds;
3010 }
3011 SkPaint paint;
3012 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
3013 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
3014 rec.fPaint = &paint;
3015 }
3016 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_BACKDROP)) {
3017 Json::Value backdrop = command[SKDEBUGCANVAS_ATTRIBUTE_BACKDROP];
3018 rec.fBackdrop = (SkImageFilter*) load_flattenable(backdrop);
3019 }
3020 SkSaveLayerCommand* result = new SkSaveLayerCommand(rec);
3021 if (rec.fBackdrop != nullptr) {
3022 rec.fBackdrop->unref();
3023 }
3024 return result;
3025}
3026
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +00003027SkSetMatrixCommand::SkSetMatrixCommand(const SkMatrix& matrix)
robertphillips9bafc302015-02-13 11:13:00 -08003028 : INHERITED(kSetMatrix_OpType) {
robertphillips70171682014-10-16 14:28:28 -07003029 fUserMatrix.reset();
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00003030 fMatrix = matrix;
robertphillips@google.com0df2a9a2013-03-25 11:50:42 +00003031 fInfo.push(SkObjectParser::MatrixToString(matrix));
chudy@google.com902ebe52012-06-29 14:21:22 +00003032}
3033
robertphillips70171682014-10-16 14:28:28 -07003034void SkSetMatrixCommand::setUserMatrix(const SkMatrix& userMatrix) {
3035 fUserMatrix = userMatrix;
3036}
3037
fmalita8c89c522014-11-08 16:18:56 -08003038void SkSetMatrixCommand::execute(SkCanvas* canvas) const {
robertphillips70171682014-10-16 14:28:28 -07003039 SkMatrix temp = SkMatrix::Concat(fUserMatrix, fMatrix);
3040 canvas->setMatrix(temp);
chudy@google.com902ebe52012-06-29 14:21:22 +00003041}
3042
ethannicholas50a8dd02016-02-10 05:40:46 -08003043Json::Value SkSetMatrixCommand::toJSON() const {
3044 Json::Value result = INHERITED::toJSON();
3045 result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = make_json_matrix(fMatrix);
3046 return result;
3047}
3048
3049SkSetMatrixCommand* SkSetMatrixCommand::fromJSON(Json::Value& command) {
3050 SkMatrix matrix;
3051 extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix);
3052 return new SkSetMatrixCommand(matrix);
3053}