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

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

Issue 1662063003: Improved support for images/bitmaps in SkJSONCanvas (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 "SkImageFilter.h"
10 #include "SkMaskFilter.h" 10 #include "SkMaskFilter.h"
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 free(data); 174 free(data);
175 } 175 }
176 else { 176 else {
177 (*target)[SKJSONCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(flattenable- >getTypeName()); 177 (*target)[SKJSONCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(flattenable- >getTypeName());
178 } 178 }
179 } 179 }
180 180
181 static bool SK_WARN_UNUSED_RESULT flatten(const SkImage& image, Json::Value* tar get, 181 static bool SK_WARN_UNUSED_RESULT flatten(const SkImage& image, Json::Value* tar get,
182 bool sendBinaries) { 182 bool sendBinaries) {
183 if (sendBinaries) { 183 if (sendBinaries) {
184 SkData* png = image.encode(SkImageEncoder::kPNG_Type, 100); 184 SkData* encoded = image.encode(SkImageEncoder::kPNG_Type, 100);
185 if (png == nullptr) { 185 if (encoded == nullptr) {
186 SkDebugf("could not encode image\n"); 186 // PNG encode doesn't necessarily support all color formats, convert to a different
187 return false; 187 // format
188 size_t rowBytes = 4 * image.width();
189 void* buffer = sk_malloc_throw(rowBytes * image.height());
190 SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height( ),
191 kN32_SkColorType, kPremul_Sk AlphaType);
192 if (!image.readPixels(dstInfo, buffer, rowBytes, 0, 0)) {
193 SkDebugf("readPixels failed\n");
194 return false;
195 }
196 SkImage* converted = SkImage::NewRasterCopy(dstInfo, buffer, rowByte s);
197 encoded = converted->encode(SkImageEncoder::kPNG_Type, 100);
198 if (encoded == nullptr) {
199 SkDebugf("image encode failed\n");
200 return false;
201 }
202 free(converted);
203 free(buffer);
188 } 204 }
189 Json::Value bytes; 205 Json::Value bytes;
190 encode_data(png->data(), png->size(), &bytes); 206 encode_data(encoded->data(), encoded->size(), &bytes);
191 (*target)[SKJSONCANVAS_ATTRIBUTE_BYTES] = bytes; 207 (*target)[SKJSONCANVAS_ATTRIBUTE_BYTES] = bytes;
192 png->unref(); 208 encoded->unref();
193 } 209 }
194 else { 210 else {
195 SkString description = SkStringPrintf("%dx%d pixel image", image.width() , image.height()); 211 SkString description = SkStringPrintf("%dx%d pixel image", image.width() , image.height());
196 (*target)[SKJSONCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(description. c_str()); 212 (*target)[SKJSONCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(description. c_str());
197 } 213 }
198 return true; 214 return true;
199 } 215 }
200 216
217 static const char* color_type_name(SkColorType colorType) {
218 switch (colorType) {
219 case kARGB_4444_SkColorType:
220 return SKJSONCANVAS_COLORTYPE_ARGB4444;
221 case kRGBA_8888_SkColorType:
222 return SKJSONCANVAS_COLORTYPE_RGBA8888;
223 case kBGRA_8888_SkColorType:
224 return SKJSONCANVAS_COLORTYPE_BGRA8888;
225 case kRGB_565_SkColorType:
226 return SKJSONCANVAS_COLORTYPE_565;
227 case kGray_8_SkColorType:
228 return SKJSONCANVAS_COLORTYPE_GRAY8;
229 case kIndex_8_SkColorType:
230 return SKJSONCANVAS_COLORTYPE_INDEX8;
231 case kAlpha_8_SkColorType:
232 return SKJSONCANVAS_COLORTYPE_ALPHA8;
233 default:
234 SkASSERT(false);
235 return SKJSONCANVAS_COLORTYPE_RGBA8888;
236 }
237 }
238
239 static const char* alpha_type_name(SkAlphaType alphaType) {
240 switch (alphaType) {
241 case kOpaque_SkAlphaType:
242 return SKJSONCANVAS_ALPHATYPE_OPAQUE;
243 case kPremul_SkAlphaType:
244 return SKJSONCANVAS_ALPHATYPE_PREMUL;
245 case kUnpremul_SkAlphaType:
246 return SKJSONCANVAS_ALPHATYPE_UNPREMUL;
247 default:
248 SkASSERT(false);
249 return SKJSONCANVAS_ALPHATYPE_OPAQUE;
250 }
251 }
252
201 static bool SK_WARN_UNUSED_RESULT flatten(const SkBitmap& bitmap, Json::Value* t arget, 253 static bool SK_WARN_UNUSED_RESULT flatten(const SkBitmap& bitmap, Json::Value* t arget,
202 bool sendBinaries) { 254 bool sendBinaries) {
203 SkImage* image = SkImage::NewFromBitmap(bitmap); 255 bitmap.lockPixels();
256 SkAutoTUnref<SkImage> image(SkImage::NewFromBitmap(bitmap));
257 bitmap.unlockPixels();
258 (*target)[SKJSONCANVAS_ATTRIBUTE_COLOR] = Json::Value(color_type_name(bitmap .colorType()));
259 (*target)[SKJSONCANVAS_ATTRIBUTE_ALPHA] = Json::Value(alpha_type_name(bitmap .alphaType()));
204 bool success = flatten(*image, target, sendBinaries); 260 bool success = flatten(*image, target, sendBinaries);
205 image->unref();
206 return success; 261 return success;
207 } 262 }
208 263
209 static void apply_paint_color(const SkPaint& paint, Json::Value* target) { 264 static void apply_paint_color(const SkPaint& paint, Json::Value* target) {
210 SkColor color = paint.getColor(); 265 SkColor color = paint.getColor();
211 if (color != SK_ColorBLACK) { 266 if (color != SK_ColorBLACK) {
212 Json::Value colorValue(Json::arrayValue); 267 Json::Value colorValue(Json::arrayValue);
213 colorValue.append(Json::Value(SkColorGetA(color))); 268 colorValue.append(Json::Value(SkColorGetA(color)));
214 colorValue.append(Json::Value(SkColorGetR(color))); 269 colorValue.append(Json::Value(SkColorGetR(color)));
215 colorValue.append(Json::Value(SkColorGetG(color))); 270 colorValue.append(Json::Value(SkColorGetG(color)));
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 409
355 static void apply_paint_xfermode(const SkPaint& paint, Json::Value* target, bool sendBinaries) { 410 static void apply_paint_xfermode(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
356 SkFlattenable* xfermode = paint.getXfermode(); 411 SkFlattenable* xfermode = paint.getXfermode();
357 if (xfermode != nullptr) { 412 if (xfermode != nullptr) {
358 Json::Value jsonXfermode; 413 Json::Value jsonXfermode;
359 flatten(xfermode, &jsonXfermode, sendBinaries); 414 flatten(xfermode, &jsonXfermode, sendBinaries);
360 (*target)[SKJSONCANVAS_ATTRIBUTE_XFERMODE] = jsonXfermode; 415 (*target)[SKJSONCANVAS_ATTRIBUTE_XFERMODE] = jsonXfermode;
361 } 416 }
362 } 417 }
363 418
419 static void apply_paint_imagefilter(const SkPaint& paint, Json::Value* target, b ool sendBinaries) {
420 SkFlattenable* imageFilter = paint.getImageFilter();
421 if (imageFilter != nullptr) {
422 Json::Value jsonImageFilter;
423 flatten(imageFilter, &jsonImageFilter, sendBinaries);
424 (*target)[SKJSONCANVAS_ATTRIBUTE_IMAGEFILTER] = jsonImageFilter;
425 }
426 }
427
364 Json::Value SkJSONCanvas::makePaint(const SkPaint& paint) { 428 Json::Value SkJSONCanvas::makePaint(const SkPaint& paint) {
365 Json::Value result(Json::objectValue); 429 Json::Value result(Json::objectValue);
366 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH, paint.getStrokeWid th(), 0.0f); 430 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH, paint.getStrokeWid th(), 0.0f);
367 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_STROKEMITER, paint.getStrokeMit er(), 431 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_STROKEMITER, paint.getStrokeMit er(),
368 SkPaintDefaults_MiterLimit); 432 SkPaintDefaults_MiterLimit);
369 store_bool(&result, SKJSONCANVAS_ATTRIBUTE_ANTIALIAS, paint.isAntiAlias(), f alse); 433 store_bool(&result, SKJSONCANVAS_ATTRIBUTE_ANTIALIAS, paint.isAntiAlias(), f alse);
370 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSIZE, paint.getTextSize(), 434 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSIZE, paint.getTextSize(),
371 SkPaintDefaults_TextSize); 435 SkPaintDefaults_TextSize);
372 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextScaleX (), SK_Scalar1); 436 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextScaleX (), SK_Scalar1);
373 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextSkewX( ), 0.0f); 437 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextSkewX( ), 0.0f);
374 apply_paint_color(paint, &result); 438 apply_paint_color(paint, &result);
375 apply_paint_style(paint, &result); 439 apply_paint_style(paint, &result);
376 apply_paint_cap(paint, &result); 440 apply_paint_cap(paint, &result);
377 apply_paint_textalign(paint, &result); 441 apply_paint_textalign(paint, &result);
378 apply_paint_patheffect(paint, &result, fSendBinaries); 442 apply_paint_patheffect(paint, &result, fSendBinaries);
379 apply_paint_maskfilter(paint, &result, fSendBinaries); 443 apply_paint_maskfilter(paint, &result, fSendBinaries);
380 apply_paint_shader(paint, &result, fSendBinaries); 444 apply_paint_shader(paint, &result, fSendBinaries);
381 apply_paint_xfermode(paint, &result, fSendBinaries); 445 apply_paint_xfermode(paint, &result, fSendBinaries);
446 apply_paint_imagefilter(paint, &result, fSendBinaries);
382 return result; 447 return result;
383 } 448 }
384 449
385 Json::Value SkJSONCanvas::makeMatrix(const SkMatrix& matrix) { 450 Json::Value SkJSONCanvas::makeMatrix(const SkMatrix& matrix) {
386 Json::Value result(Json::arrayValue); 451 Json::Value result(Json::arrayValue);
387 Json::Value row1(Json::arrayValue); 452 Json::Value row1(Json::arrayValue);
388 row1.append(Json::Value(matrix[0])); 453 row1.append(Json::Value(matrix[0]));
389 row1.append(Json::Value(matrix[1])); 454 row1.append(Json::Value(matrix[1]));
390 row1.append(Json::Value(matrix[2])); 455 row1.append(Json::Value(matrix[2]));
391 result.append(row1); 456 result.append(row1);
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 } 650 }
586 } 651 }
587 652
588 void SkJSONCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, c onst SkRect& dst, 653 void SkJSONCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, c onst SkRect& dst,
589 const SkPaint* paint, SkCanvas::SrcRectConstr aint constraint) { 654 const SkPaint* paint, SkCanvas::SrcRectConstr aint constraint) {
590 Json::Value encoded; 655 Json::Value encoded;
591 if (flatten(bitmap, &encoded, fSendBinaries)) { 656 if (flatten(bitmap, &encoded, fSendBinaries)) {
592 this->updateMatrix(); 657 this->updateMatrix();
593 Json::Value command(Json::objectValue); 658 Json::Value command(Json::objectValue);
594 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_BITMAPR ECT); 659 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_BITMAPR ECT);
595 command[SKJSONCANVAS_ATTRIBUTE_IMAGE] = encoded; 660 command[SKJSONCANVAS_ATTRIBUTE_BITMAP] = encoded;
596 if (src != nullptr) { 661 if (src != nullptr) {
597 command[SKJSONCANVAS_ATTRIBUTE_SRC] = this->makeRect(*src); 662 command[SKJSONCANVAS_ATTRIBUTE_SRC] = this->makeRect(*src);
598 } 663 }
599 command[SKJSONCANVAS_ATTRIBUTE_DST] = this->makeRect(dst); 664 command[SKJSONCANVAS_ATTRIBUTE_DST] = this->makeRect(dst);
600 if (paint != nullptr) { 665 if (paint != nullptr) {
601 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(*paint); 666 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(*paint);
602 } 667 }
603 if (constraint == SkCanvas::kStrict_SrcRectConstraint) { 668 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
604 command[SKJSONCANVAS_ATTRIBUTE_STRICT] = Json::Value(true); 669 command[SKJSONCANVAS_ATTRIBUTE_STRICT] = Json::Value(true);
605 } 670 }
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 fCommands.append(command); 778 fCommands.append(command);
714 } 779 }
715 780
716 void SkJSONCanvas::willRestore() { 781 void SkJSONCanvas::willRestore() {
717 Json::Value command(Json::objectValue); 782 Json::Value command(Json::objectValue);
718 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_RESTORE); 783 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_RESTORE);
719 fCommands.append(command); 784 fCommands.append(command);
720 } 785 }
721 786
722 SkCanvas::SaveLayerStrategy SkJSONCanvas::getSaveLayerStrategy(const SaveLayerRe c& rec) { 787 SkCanvas::SaveLayerStrategy SkJSONCanvas::getSaveLayerStrategy(const SaveLayerRe c& rec) {
788 this->updateMatrix();
723 Json::Value command(Json::objectValue); 789 Json::Value command(Json::objectValue);
724 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_SAVELAYER); 790 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_SAVELAYER);
725 if (rec.fBounds != nullptr) { 791 if (rec.fBounds != nullptr) {
726 command[SKJSONCANVAS_ATTRIBUTE_BOUNDS] = this->makeRect(*rec.fBounds); 792 command[SKJSONCANVAS_ATTRIBUTE_BOUNDS] = this->makeRect(*rec.fBounds);
727 } 793 }
728 if (rec.fPaint != nullptr) { 794 if (rec.fPaint != nullptr) {
729 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(*rec.fPaint); 795 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(*rec.fPaint);
730 } 796 }
731 if (rec.fBackdrop != nullptr) { 797 if (rec.fBackdrop != nullptr) {
732 Json::Value backdrop; 798 Json::Value backdrop;
733 flatten(rec.fBackdrop, &backdrop, fSendBinaries); 799 flatten(rec.fBackdrop, &backdrop, fSendBinaries);
734 command[SKJSONCANVAS_ATTRIBUTE_BACKDROP] = backdrop; 800 command[SKJSONCANVAS_ATTRIBUTE_BACKDROP] = backdrop;
735 } 801 }
736 if (rec.fSaveLayerFlags != 0) { 802 if (rec.fSaveLayerFlags != 0) {
737 SkDebugf("unsupported: saveLayer flags\n"); 803 SkDebugf("unsupported: saveLayer flags\n");
738 } 804 }
739 fCommands.append(command); 805 fCommands.append(command);
740 return this->INHERITED::getSaveLayerStrategy(rec); 806 return this->INHERITED::getSaveLayerStrategy(rec);
741 } 807 }
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