Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: tools/json/SkJSONCanvas.cpp

Issue 1662503003: support for more features when rendering to/from JSON (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/json/SkJSONCanvas.h ('k') | tools/json/SkJSONRenderer.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkJSONCanvas.h" 8 #include "SkJSONCanvas.h"
9 #include "SkImageFilter.h"
9 #include "SkMaskFilter.h" 10 #include "SkMaskFilter.h"
10 #include "SkPaintDefaults.h" 11 #include "SkPaintDefaults.h"
11 #include "SkPath.h" 12 #include "SkPath.h"
12 #include "SkPathEffect.h" 13 #include "SkPathEffect.h"
13 #include "SkRRect.h" 14 #include "SkRRect.h"
15 #include "SkWriteBuffer.h"
14 16
15 SkJSONCanvas::SkJSONCanvas(int width, int height, SkWStream& out) 17 SkJSONCanvas::SkJSONCanvas(int width, int height, SkWStream& out, bool sendBinar ies)
16 : INHERITED(width, height) 18 : INHERITED(width, height)
17 , fOut(out) 19 , fOut(out)
18 , fRoot(Json::objectValue) 20 , fRoot(Json::objectValue)
19 , fCommands(Json::arrayValue) { 21 , fCommands(Json::arrayValue)
22 , fSendBinaries(sendBinaries) {
20 fRoot[SKJSONCANVAS_VERSION] = Json::Value(1); 23 fRoot[SKJSONCANVAS_VERSION] = Json::Value(1);
21 } 24 }
22 25
23 void SkJSONCanvas::finish() { 26 void SkJSONCanvas::finish() {
24 fRoot[SKJSONCANVAS_COMMANDS] = fCommands; 27 fRoot[SKJSONCANVAS_COMMANDS] = fCommands;
25 fOut.writeText(Json::FastWriter().write(fRoot).c_str()); 28 fOut.writeText(Json::FastWriter().write(fRoot).c_str());
26 } 29 }
27 30
28 Json::Value SkJSONCanvas::makePoint(const SkPoint& point) { 31 Json::Value SkJSONCanvas::makePoint(const SkPoint& point) {
29 Json::Value result(Json::arrayValue); 32 Json::Value result(Json::arrayValue);
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 (*target)[key] = Json::Value(value); 144 (*target)[key] = Json::Value(value);
142 } 145 }
143 } 146 }
144 147
145 void store_bool(Json::Value* target, const char* key, bool value, bool defaultVa lue) { 148 void store_bool(Json::Value* target, const char* key, bool value, bool defaultVa lue) {
146 if (value != defaultValue) { 149 if (value != defaultValue) {
147 (*target)[key] = Json::Value(value); 150 (*target)[key] = Json::Value(value);
148 } 151 }
149 } 152 }
150 153
151 Json::Value SkJSONCanvas::makePaint(const SkPaint& paint) { 154 static void encode_data(const void* data, size_t count, Json::Value* target) {
152 Json::Value result(Json::objectValue); 155 // just use a brain-dead JSON array for now, switch to base64 or something e lse smarter down the
156 // road
157 for (size_t i = 0; i < count; i++) {
158 target->append(((const uint8_t*)data)[i]);
159 }
160 }
161
162 static void flatten(const SkFlattenable* flattenable, Json::Value* target, bool sendBinaries) {
163 if (sendBinaries) {
164 SkWriteBuffer buffer;
165 flattenable->flatten(buffer);
166 void* data = sk_malloc_throw(buffer.bytesWritten());
167 buffer.writeToMemory(data);
168 Json::Value bytes;
169 encode_data(data, buffer.bytesWritten(), &bytes);
170 Json::Value jsonFlattenable;
171 jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_NAME] = Json::Value(flattenable-> getTypeName());
172 jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_BYTES] = bytes;
173 (*target) = jsonFlattenable;
174 free(data);
175 }
176 else {
177 (*target)[SKJSONCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(flattenable- >getTypeName());
178 }
179 }
180
181 static bool SK_WARN_UNUSED_RESULT flatten(const SkImage& image, Json::Value* tar get,
182 bool sendBinaries) {
183 if (sendBinaries) {
184 SkData* png = image.encode(SkImageEncoder::kPNG_Type, 100);
185 if (png == nullptr) {
186 SkDebugf("could not encode image\n");
187 return false;
188 }
189 Json::Value bytes;
190 encode_data(png->data(), png->size(), &bytes);
191 (*target)[SKJSONCANVAS_ATTRIBUTE_BYTES] = bytes;
192 png->unref();
193 }
194 else {
195 SkString description = SkStringPrintf("%dx%d pixel image", image.width() , image.height());
196 (*target)[SKJSONCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(description. c_str());
197 }
198 return true;
199 }
200
201 static bool SK_WARN_UNUSED_RESULT flatten(const SkBitmap& bitmap, Json::Value* t arget,
202 bool sendBinaries) {
203 SkImage* image = SkImage::NewFromBitmap(bitmap);
204 bool success = flatten(*image, target, sendBinaries);
205 image->unref();
206 return success;
207 }
208
209 static void apply_paint_color(const SkPaint& paint, Json::Value* target) {
153 SkColor color = paint.getColor(); 210 SkColor color = paint.getColor();
154 if (color != SK_ColorBLACK) { 211 if (color != SK_ColorBLACK) {
155 Json::Value colorValue(Json::arrayValue); 212 Json::Value colorValue(Json::arrayValue);
156 colorValue.append(Json::Value(SkColorGetA(color))); 213 colorValue.append(Json::Value(SkColorGetA(color)));
157 colorValue.append(Json::Value(SkColorGetR(color))); 214 colorValue.append(Json::Value(SkColorGetR(color)));
158 colorValue.append(Json::Value(SkColorGetG(color))); 215 colorValue.append(Json::Value(SkColorGetG(color)));
159 colorValue.append(Json::Value(SkColorGetB(color))); 216 colorValue.append(Json::Value(SkColorGetB(color)));
160 result[SKJSONCANVAS_ATTRIBUTE_COLOR] = colorValue;; 217 (*target)[SKJSONCANVAS_ATTRIBUTE_COLOR] = colorValue;;
161 } 218 }
219 }
220
221 static void apply_paint_style(const SkPaint& paint, Json::Value* target) {
162 SkPaint::Style style = paint.getStyle(); 222 SkPaint::Style style = paint.getStyle();
163 if (style != SkPaint::kFill_Style) { 223 if (style != SkPaint::kFill_Style) {
164 switch (style) { 224 switch (style) {
165 case SkPaint::kStroke_Style: { 225 case SkPaint::kStroke_Style: {
166 Json::Value stroke(SKJSONCANVAS_STYLE_STROKE); 226 Json::Value stroke(SKJSONCANVAS_STYLE_STROKE);
167 result[SKJSONCANVAS_ATTRIBUTE_STYLE] = stroke; 227 (*target)[SKJSONCANVAS_ATTRIBUTE_STYLE] = stroke;
168 break; 228 break;
169 } 229 }
170 case SkPaint::kStrokeAndFill_Style: { 230 case SkPaint::kStrokeAndFill_Style: {
171 Json::Value strokeAndFill(SKJSONCANVAS_STYLE_STROKEANDFILL); 231 Json::Value strokeAndFill(SKJSONCANVAS_STYLE_STROKEANDFILL);
172 result[SKJSONCANVAS_ATTRIBUTE_STYLE] = strokeAndFill; 232 (*target)[SKJSONCANVAS_ATTRIBUTE_STYLE] = strokeAndFill;
173 break; 233 break;
174 } 234 }
175 default: SkASSERT(false); 235 default: SkASSERT(false);
176 } 236 }
177 } 237 }
178 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH, paint.getStrokeWid th(), 0.0f); 238 }
179 store_bool(&result, SKJSONCANVAS_ATTRIBUTE_ANTIALIAS, paint.isAntiAlias(), f alse); 239
240 static void apply_paint_cap(const SkPaint& paint, Json::Value* target) {
241 SkPaint::Cap cap = paint.getStrokeCap();
242 if (cap != SkPaint::kDefault_Cap) {
243 switch (cap) {
244 case SkPaint::kButt_Cap: {
245 (*target)[SKJSONCANVAS_ATTRIBUTE_CAP] = Json::Value(SKJSONCANVAS _CAP_BUTT);
246 break;
247 }
248 case SkPaint::kRound_Cap: {
249 (*target)[SKJSONCANVAS_ATTRIBUTE_CAP] = Json::Value(SKJSONCANVAS _CAP_ROUND);
250 break;
251 }
252 case SkPaint::kSquare_Cap: {
253 (*target)[SKJSONCANVAS_ATTRIBUTE_CAP] = Json::Value(SKJSONCANVAS _CAP_SQUARE);
254 break;
255 }
256 default: SkASSERT(false);
257 }
258 }
259 }
260 static void apply_paint_maskfilter(const SkPaint& paint, Json::Value* target, bo ol sendBinaries) {
180 SkMaskFilter* maskFilter = paint.getMaskFilter(); 261 SkMaskFilter* maskFilter = paint.getMaskFilter();
181 if (maskFilter != nullptr) { 262 if (maskFilter != nullptr) {
182 SkMaskFilter::BlurRec blurRec; 263 SkMaskFilter::BlurRec blurRec;
183 if (maskFilter->asABlur(&blurRec)) { 264 if (maskFilter->asABlur(&blurRec)) {
184 Json::Value blur(Json::objectValue); 265 Json::Value blur(Json::objectValue);
185 blur[SKJSONCANVAS_ATTRIBUTE_SIGMA] = Json::Value(blurRec.fSigma); 266 blur[SKJSONCANVAS_ATTRIBUTE_SIGMA] = Json::Value(blurRec.fSigma);
186 switch (blurRec.fStyle) { 267 switch (blurRec.fStyle) {
187 case SkBlurStyle::kNormal_SkBlurStyle: 268 case SkBlurStyle::kNormal_SkBlurStyle:
188 blur[SKJSONCANVAS_ATTRIBUTE_STYLE] = Json::Value(SKJSONCANVA S_BLURSTYLE_NORMAL); 269 blur[SKJSONCANVAS_ATTRIBUTE_STYLE] = Json::Value(SKJSONCANVA S_BLURSTYLE_NORMAL);
189 break; 270 break;
(...skipping 12 matching lines...) Expand all
202 switch (blurRec.fQuality) { 283 switch (blurRec.fQuality) {
203 case SkBlurQuality::kLow_SkBlurQuality: 284 case SkBlurQuality::kLow_SkBlurQuality:
204 blur[SKJSONCANVAS_ATTRIBUTE_QUALITY] = Json::Value(SKJSONCAN VAS_BLURQUALITY_LOW); 285 blur[SKJSONCANVAS_ATTRIBUTE_QUALITY] = Json::Value(SKJSONCAN VAS_BLURQUALITY_LOW);
205 break; 286 break;
206 case SkBlurQuality::kHigh_SkBlurQuality: 287 case SkBlurQuality::kHigh_SkBlurQuality:
207 blur[SKJSONCANVAS_ATTRIBUTE_QUALITY] = Json::Value(SKJSONCAN VAS_BLURQUALITY_HIGH); 288 blur[SKJSONCANVAS_ATTRIBUTE_QUALITY] = Json::Value(SKJSONCAN VAS_BLURQUALITY_HIGH);
208 break; 289 break;
209 default: 290 default:
210 SkASSERT(false); 291 SkASSERT(false);
211 } 292 }
212 result[SKJSONCANVAS_ATTRIBUTE_BLUR] = blur; 293 (*target)[SKJSONCANVAS_ATTRIBUTE_BLUR] = blur;
213 } 294 }
214 else { 295 else {
215 SkDebugf("unimplemented: non-blur maskfilter"); 296 Json::Value jsonMaskFilter;
216 SkASSERT(false); 297 flatten(maskFilter, &jsonMaskFilter, sendBinaries);
298 (*target)[SKJSONCANVAS_ATTRIBUTE_MASKFILTER] = jsonMaskFilter;
217 } 299 }
218 } 300 }
301 }
302
303 static void apply_paint_patheffect(const SkPaint& paint, Json::Value* target, bo ol sendBinaries) {
219 SkPathEffect* pathEffect = paint.getPathEffect(); 304 SkPathEffect* pathEffect = paint.getPathEffect();
220 if (pathEffect != nullptr) { 305 if (pathEffect != nullptr) {
221 SkPathEffect::DashInfo dashInfo; 306 SkPathEffect::DashInfo dashInfo;
222 SkPathEffect::DashType dashType = pathEffect->asADash(&dashInfo); 307 SkPathEffect::DashType dashType = pathEffect->asADash(&dashInfo);
223 if (dashType == SkPathEffect::kDash_DashType) { 308 if (dashType == SkPathEffect::kDash_DashType) {
224 dashInfo.fIntervals = (SkScalar*) sk_malloc_throw(dashInfo.fCount * sizeof(SkScalar)); 309 dashInfo.fIntervals = (SkScalar*) sk_malloc_throw(dashInfo.fCount * sizeof(SkScalar));
225 pathEffect->asADash(&dashInfo); 310 pathEffect->asADash(&dashInfo);
226 Json::Value dashing(Json::objectValue); 311 Json::Value dashing(Json::objectValue);
227 Json::Value intervals(Json::arrayValue); 312 Json::Value intervals(Json::arrayValue);
228 for (int32_t i = 0; i < dashInfo.fCount; i++) { 313 for (int32_t i = 0; i < dashInfo.fCount; i++) {
229 intervals.append(Json::Value(dashInfo.fIntervals[i])); 314 intervals.append(Json::Value(dashInfo.fIntervals[i]));
230 } 315 }
231 free(dashInfo.fIntervals); 316 free(dashInfo.fIntervals);
232 dashing[SKJSONCANVAS_ATTRIBUTE_INTERVALS] = intervals; 317 dashing[SKJSONCANVAS_ATTRIBUTE_INTERVALS] = intervals;
233 dashing[SKJSONCANVAS_ATTRIBUTE_PHASE] = dashInfo.fPhase; 318 dashing[SKJSONCANVAS_ATTRIBUTE_PHASE] = dashInfo.fPhase;
234 result[SKJSONCANVAS_ATTRIBUTE_DASHING] = dashing; 319 (*target)[SKJSONCANVAS_ATTRIBUTE_DASHING] = dashing;
235 } 320 }
236 else { 321 else {
237 SkDebugf("unimplemented: non-dash patheffect"); 322 Json::Value jsonPathEffect;
238 SkASSERT(false); 323 flatten(pathEffect, &jsonPathEffect, sendBinaries);
324 (*target)[SKJSONCANVAS_ATTRIBUTE_PATHEFFECT] = jsonPathEffect;
239 } 325 }
240 } 326 }
241 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSIZE, paint.getTextSize(), 327 }
242 SkPaintDefaults_TextSize); 328
243 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextScaleX (), SK_Scalar1); 329 static void apply_paint_textalign(const SkPaint& paint, Json::Value* target) {
244 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextSkewX( ), 0.0f);
245 SkPaint::Align textAlign = paint.getTextAlign(); 330 SkPaint::Align textAlign = paint.getTextAlign();
246 if (textAlign != SkPaint::kLeft_Align) { 331 if (textAlign != SkPaint::kLeft_Align) {
247 switch (textAlign) { 332 switch (textAlign) {
248 case SkPaint::kCenter_Align: { 333 case SkPaint::kCenter_Align: {
249 result[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN] = SKJSONCANVAS_ALIGN_CE NTER; 334 (*target)[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN] = SKJSONCANVAS_ALIGN _CENTER;
250 break; 335 break;
251 } 336 }
252 case SkPaint::kRight_Align: { 337 case SkPaint::kRight_Align: {
253 result[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN] = SKJSONCANVAS_ALIGN_RI GHT; 338 (*target)[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN] = SKJSONCANVAS_ALIGN _RIGHT;
254 break; 339 break;
255 } 340 }
256 default: SkASSERT(false); 341 default: SkASSERT(false);
257 } 342 }
258 } 343 }
344 }
345
346 static void apply_paint_shader(const SkPaint& paint, Json::Value* target, bool s endBinaries) {
347 SkFlattenable* shader = paint.getShader();
348 if (shader != nullptr) {
349 Json::Value jsonShader;
350 flatten(shader, &jsonShader, sendBinaries);
351 (*target)[SKJSONCANVAS_ATTRIBUTE_SHADER] = jsonShader;
352 }
353 }
354
355 static void apply_paint_xfermode(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
356 SkFlattenable* xfermode = paint.getXfermode();
357 if (xfermode != nullptr) {
358 Json::Value jsonXfermode;
359 flatten(xfermode, &jsonXfermode, sendBinaries);
360 (*target)[SKJSONCANVAS_ATTRIBUTE_XFERMODE] = jsonXfermode;
361 }
362 }
363
364 Json::Value SkJSONCanvas::makePaint(const SkPaint& paint) {
365 Json::Value result(Json::objectValue);
366 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH, paint.getStrokeWid th(), 0.0f);
367 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_STROKEMITER, paint.getStrokeMit er(),
368 SkPaintDefaults_MiterLimit);
369 store_bool(&result, SKJSONCANVAS_ATTRIBUTE_ANTIALIAS, paint.isAntiAlias(), f alse);
370 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSIZE, paint.getTextSize(),
371 SkPaintDefaults_TextSize);
372 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextScaleX (), SK_Scalar1);
373 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextSkewX( ), 0.0f);
374 apply_paint_color(paint, &result);
375 apply_paint_style(paint, &result);
376 apply_paint_cap(paint, &result);
377 apply_paint_textalign(paint, &result);
378 apply_paint_patheffect(paint, &result, fSendBinaries);
379 apply_paint_maskfilter(paint, &result, fSendBinaries);
380 apply_paint_shader(paint, &result, fSendBinaries);
381 apply_paint_xfermode(paint, &result, fSendBinaries);
259 return result; 382 return result;
260 } 383 }
261 384
262 Json::Value SkJSONCanvas::makeMatrix(const SkMatrix& matrix) { 385 Json::Value SkJSONCanvas::makeMatrix(const SkMatrix& matrix) {
263 Json::Value result(Json::arrayValue); 386 Json::Value result(Json::arrayValue);
264 Json::Value row1(Json::arrayValue); 387 Json::Value row1(Json::arrayValue);
265 row1.append(Json::Value(matrix[0])); 388 row1.append(Json::Value(matrix[0]));
266 row1.append(Json::Value(matrix[1])); 389 row1.append(Json::Value(matrix[1]));
267 row1.append(Json::Value(matrix[2])); 390 row1.append(Json::Value(matrix[2]));
268 result.append(row1); 391 result.append(row1);
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 519
397 void SkJSONCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) { 520 void SkJSONCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
398 this->updateMatrix(); 521 this->updateMatrix();
399 Json::Value command(Json::objectValue); 522 Json::Value command(Json::objectValue);
400 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_PATH); 523 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_PATH);
401 command[SKJSONCANVAS_ATTRIBUTE_PATH] = this->makePath(path); 524 command[SKJSONCANVAS_ATTRIBUTE_PATH] = this->makePath(path);
402 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint); 525 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint);
403 fCommands.append(command); 526 fCommands.append(command);
404 } 527 }
405 528
406 void SkJSONCanvas::onDrawImage(const SkImage*, SkScalar dx, SkScalar dy, const S kPaint*) { 529 void SkJSONCanvas::onDrawImage(const SkImage* image, SkScalar dx, SkScalar dy,
407 SkDebugf("unsupported: drawImage\n"); 530 const SkPaint* paint) {
531 Json::Value encoded;
532 if (flatten(*image, &encoded, fSendBinaries)) {
533 this->updateMatrix();
534 Json::Value command(Json::objectValue);
535 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_IMAGE);
536 command[SKJSONCANVAS_ATTRIBUTE_IMAGE] = encoded;
537 command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makePoint(dx, dy);
538 if (paint != nullptr) {
539 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(*paint);
540 }
541 fCommands.append(command);
542 }
408 } 543 }
409 544
410 void SkJSONCanvas::onDrawImageRect(const SkImage*, const SkRect*, const SkRect&, const SkPaint*, 545 void SkJSONCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, cons t SkRect& dst,
411 SkCanvas::SrcRectConstraint) { 546 const SkPaint* paint, SkCanvas::SrcRectConstr aint constraint) {
412 SkDebugf("unsupported: drawImageRect\n"); 547 Json::Value encoded;
548 if (flatten(*image, &encoded, fSendBinaries)) {
549 this->updateMatrix();
550 Json::Value command(Json::objectValue);
551 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_IMAGERE CT);
552 command[SKJSONCANVAS_ATTRIBUTE_IMAGE] = encoded;
553 if (src != nullptr) {
554 command[SKJSONCANVAS_ATTRIBUTE_SRC] = this->makeRect(*src);
555 }
556 command[SKJSONCANVAS_ATTRIBUTE_DST] = this->makeRect(dst);
557 if (paint != nullptr) {
558 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(*paint);
559 }
560 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
561 command[SKJSONCANVAS_ATTRIBUTE_STRICT] = Json::Value(true);
562 }
563 fCommands.append(command);
564 }
413 } 565 }
414 566
415 void SkJSONCanvas::onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst, 567 void SkJSONCanvas::onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
416 const SkPaint*) { 568 const SkPaint*) {
417 SkDebugf("unsupported: drawImageNine\n"); 569 SkDebugf("unsupported: drawImageNine\n");
418 } 570 }
419 571
420 void SkJSONCanvas::onDrawBitmap(const SkBitmap&, SkScalar dx, SkScalar dy, const SkPaint*) { 572 void SkJSONCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar dx, SkScalar dy ,
421 SkDebugf("unsupported: drawBitmap\n"); 573 const SkPaint* paint) {
574 Json::Value encoded;
575 if (flatten(bitmap, &encoded, fSendBinaries)) {
576 this->updateMatrix();
577 Json::Value command(Json::objectValue);
578 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_BITMAP) ;
579 command[SKJSONCANVAS_ATTRIBUTE_BITMAP] = encoded;
580 command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makePoint(dx, dy);
581 if (paint != nullptr) {
582 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(*paint);
583 }
584 fCommands.append(command);
585 }
422 } 586 }
423 587
424 void SkJSONCanvas::onDrawBitmapRect(const SkBitmap&, const SkRect*, const SkRect &, const SkPaint*, 588 void SkJSONCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, c onst SkRect& dst,
425 SkCanvas::SrcRectConstraint) { 589 const SkPaint* paint, SkCanvas::SrcRectConstr aint constraint) {
426 SkDebugf("unsupported: drawBitmapRect\n"); 590 Json::Value encoded;
591 if (flatten(bitmap, &encoded, fSendBinaries)) {
592 this->updateMatrix();
593 Json::Value command(Json::objectValue);
594 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_BITMAPR ECT);
595 command[SKJSONCANVAS_ATTRIBUTE_IMAGE] = encoded;
596 if (src != nullptr) {
597 command[SKJSONCANVAS_ATTRIBUTE_SRC] = this->makeRect(*src);
598 }
599 command[SKJSONCANVAS_ATTRIBUTE_DST] = this->makeRect(dst);
600 if (paint != nullptr) {
601 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(*paint);
602 }
603 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
604 command[SKJSONCANVAS_ATTRIBUTE_STRICT] = Json::Value(true);
605 }
606 fCommands.append(command);
607 }
427 } 608 }
428 609
429 void SkJSONCanvas::onDrawBitmapNine(const SkBitmap&, const SkIRect& center, cons t SkRect& dst, 610 void SkJSONCanvas::onDrawBitmapNine(const SkBitmap&, const SkIRect& center, cons t SkRect& dst,
430 const SkPaint*) { 611 const SkPaint*) {
431 SkDebugf("unsupported: drawBitmapNine\n"); 612 SkDebugf("unsupported: drawBitmapNine\n");
432 } 613 }
433 614
434 void SkJSONCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, 615 void SkJSONCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x,
435 SkScalar y, const SkPaint& paint) { 616 SkScalar y, const SkPaint& paint) {
436 this->updateMatrix(); 617 this->updateMatrix();
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 Json::Value command(Json::objectValue); 711 Json::Value command(Json::objectValue);
531 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_SAVE); 712 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_SAVE);
532 fCommands.append(command); 713 fCommands.append(command);
533 } 714 }
534 715
535 void SkJSONCanvas::willRestore() { 716 void SkJSONCanvas::willRestore() {
536 Json::Value command(Json::objectValue); 717 Json::Value command(Json::objectValue);
537 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_RESTORE); 718 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_RESTORE);
538 fCommands.append(command); 719 fCommands.append(command);
539 } 720 }
721
722 SkCanvas::SaveLayerStrategy SkJSONCanvas::getSaveLayerStrategy(const SaveLayerRe c& rec) {
723 Json::Value command(Json::objectValue);
724 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_SAVELAYER);
725 if (rec.fBounds != nullptr) {
726 command[SKJSONCANVAS_ATTRIBUTE_BOUNDS] = this->makeRect(*rec.fBounds);
727 }
728 if (rec.fPaint != nullptr) {
729 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(*rec.fPaint);
730 }
731 if (rec.fBackdrop != nullptr) {
732 Json::Value backdrop;
733 flatten(rec.fBackdrop, &backdrop, fSendBinaries);
734 command[SKJSONCANVAS_ATTRIBUTE_BACKDROP] = backdrop;
735 }
736 if (rec.fSaveLayerFlags != 0) {
737 SkDebugf("unsupported: saveLayer flags\n");
738 }
739 fCommands.append(command);
740 return this->INHERITED::getSaveLayerStrategy(rec);
741 }
OLDNEW
« no previous file with comments | « tools/json/SkJSONCanvas.h ('k') | tools/json/SkJSONRenderer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698