| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 | 8 |
| 9 #include "SkDrawCommand.h" | 9 #include "SkDrawCommand.h" |
| 10 | 10 |
| 11 #include "SkBlurMaskFilter.h" | 11 #include "SkBlurMaskFilter.h" |
| 12 #include "SkColorFilter.h" | 12 #include "SkColorFilter.h" |
| 13 #include "SkDashPathEffect.h" | 13 #include "SkDashPathEffect.h" |
| 14 #include "SkImageFilter.h" | 14 #include "SkImageFilter.h" |
| 15 #include "SkJsonWriteBuffer.h" | 15 #include "SkJsonWriteBuffer.h" |
| 16 #include "SkMaskFilter.h" | 16 #include "SkMaskFilter.h" |
| 17 #include "SkObjectParser.h" | 17 #include "SkObjectParser.h" |
| 18 #include "SkPaintDefaults.h" | 18 #include "SkPaintDefaults.h" |
| 19 #include "SkPathEffect.h" | 19 #include "SkPathEffect.h" |
| 20 #include "SkPicture.h" | 20 #include "SkPicture.h" |
| 21 #include "SkTextBlob.h" | 21 #include "SkTextBlob.h" |
| 22 #include "SkTextBlobRunIterator.h" | 22 #include "SkTextBlobRunIterator.h" |
| 23 #include "SkTHash.h" | 23 #include "SkTHash.h" |
| 24 #include "SkTypeface.h" | 24 #include "SkTypeface.h" |
| 25 #include "SkValidatingReadBuffer.h" | 25 #include "SkValidatingReadBuffer.h" |
| 26 #include "SkWriteBuffer.h" | 26 #include "SkWriteBuffer.h" |
| 27 #include "picture_utils.h" |
| 27 | 28 |
| 28 #define SKDEBUGCANVAS_ATTRIBUTE_COMMAND "command" | 29 #define SKDEBUGCANVAS_ATTRIBUTE_COMMAND "command" |
| 29 #define SKDEBUGCANVAS_ATTRIBUTE_VISIBLE "visible" | 30 #define SKDEBUGCANVAS_ATTRIBUTE_VISIBLE "visible" |
| 30 #define SKDEBUGCANVAS_ATTRIBUTE_MATRIX "matrix" | 31 #define SKDEBUGCANVAS_ATTRIBUTE_MATRIX "matrix" |
| 31 #define SKDEBUGCANVAS_ATTRIBUTE_COORDS "coords" | 32 #define SKDEBUGCANVAS_ATTRIBUTE_COORDS "coords" |
| 32 #define SKDEBUGCANVAS_ATTRIBUTE_BOUNDS "bounds" | 33 #define SKDEBUGCANVAS_ATTRIBUTE_BOUNDS "bounds" |
| 33 #define SKDEBUGCANVAS_ATTRIBUTE_PAINT "paint" | 34 #define SKDEBUGCANVAS_ATTRIBUTE_PAINT "paint" |
| 34 #define SKDEBUGCANVAS_ATTRIBUTE_OUTER "outer" | 35 #define SKDEBUGCANVAS_ATTRIBUTE_OUTER "outer" |
| 35 #define SKDEBUGCANVAS_ATTRIBUTE_INNER "inner" | 36 #define SKDEBUGCANVAS_ATTRIBUTE_INNER "inner" |
| 36 #define SKDEBUGCANVAS_ATTRIBUTE_MODE "mode" | 37 #define SKDEBUGCANVAS_ATTRIBUTE_MODE "mode" |
| (...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 (*target) = jsonFlattenable; | 611 (*target) = jsonFlattenable; |
| 611 sk_free(data); | 612 sk_free(data); |
| 612 } | 613 } |
| 613 | 614 |
| 614 static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t l
ength) { | 615 static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t l
ength) { |
| 615 SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr); | 616 SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr); |
| 616 out->write(data, length); | 617 out->write(data, length); |
| 617 } | 618 } |
| 618 | 619 |
| 619 void SkDrawCommand::WritePNG(const png_bytep rgba, png_uint_32 width, png_uint_3
2 height, | 620 void SkDrawCommand::WritePNG(const png_bytep rgba, png_uint_32 width, png_uint_3
2 height, |
| 620 SkWStream& out) { | 621 SkWStream& out, bool isOpaque) { |
| 621 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL); | 622 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL); |
| 622 SkASSERT(png != nullptr); | 623 SkASSERT(png != nullptr); |
| 623 png_infop info_ptr = png_create_info_struct(png); | 624 png_infop info_ptr = png_create_info_struct(png); |
| 624 SkASSERT(info_ptr != nullptr); | 625 SkASSERT(info_ptr != nullptr); |
| 625 if (setjmp(png_jmpbuf(png))) { | 626 if (setjmp(png_jmpbuf(png))) { |
| 626 SkFAIL("png encode error"); | 627 SkFAIL("png encode error"); |
| 627 } | 628 } |
| 628 png_set_IHDR(png, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERL
ACE_NONE, | 629 png_set_write_fn(png, &out, write_png_callback, NULL); |
| 630 int colorType = isOpaque ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA; |
| 631 png_set_IHDR(png, info_ptr, width, height, 8, colorType, PNG_INTERLACE_NONE, |
| 629 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); | 632 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 630 png_set_compression_level(png, 1); | 633 png_set_compression_level(png, 1); |
| 631 png_bytepp rows = (png_bytepp) sk_malloc_throw(height * sizeof(png_byte*)); | 634 png_bytepp rows = (png_bytepp) sk_malloc_throw(height * sizeof(png_byte*)); |
| 632 png_bytep pixels = (png_bytep) sk_malloc_throw(width * height * 3); | 635 png_bytep pixels = (png_bytep) sk_malloc_throw(width * height * 4); |
| 633 for (png_size_t y = 0; y < height; ++y) { | 636 for (png_size_t y = 0; y < height; ++y) { |
| 634 const png_bytep src = rgba + y * width * 4; | 637 const png_bytep src = rgba + y * width * 4; |
| 635 rows[y] = pixels + y * width * 3; | 638 rows[y] = pixels + y * width * 4; |
| 636 // convert from RGBA to RGB | |
| 637 for (png_size_t x = 0; x < width; ++x) { | 639 for (png_size_t x = 0; x < width; ++x) { |
| 638 rows[y][x * 3] = src[x * 4]; | 640 rows[y][x * 4] = src[x * 4]; |
| 639 rows[y][x * 3 + 1] = src[x * 4 + 1]; | 641 rows[y][x * 4 + 1] = src[x * 4 + 1]; |
| 640 rows[y][x * 3 + 2] = src[x * 4 + 2]; | 642 rows[y][x * 4 + 2] = src[x * 4 + 2]; |
| 643 rows[y][x * 4 + 3] = src[x * 4 + 3]; |
| 641 } | 644 } |
| 642 } | 645 } |
| 646 png_write_info(png, info_ptr); |
| 647 if (isOpaque) { |
| 648 png_set_filler(png, 0xFF, PNG_FILLER_AFTER); |
| 649 } |
| 643 png_set_filter(png, 0, PNG_NO_FILTERS); | 650 png_set_filter(png, 0, PNG_NO_FILTERS); |
| 644 png_set_rows(png, info_ptr, &rows[0]); | 651 png_write_image(png, &rows[0]); |
| 645 png_set_write_fn(png, &out, write_png_callback, NULL); | |
| 646 png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); | |
| 647 png_destroy_write_struct(&png, NULL); | 652 png_destroy_write_struct(&png, NULL); |
| 648 sk_free(rows); | 653 sk_free(rows); |
| 649 sk_free(pixels); | 654 sk_free(pixels); |
| 650 } | 655 } |
| 651 | 656 |
| 652 bool SkDrawCommand::flatten(const SkImage& image, Json::Value* target, | 657 bool SkDrawCommand::flatten(const SkImage& image, Json::Value* target, |
| 653 UrlDataManager& urlDataManager) { | 658 UrlDataManager& urlDataManager) { |
| 654 size_t rowBytes = 4 * image.width(); | 659 size_t rowBytes = 4 * image.width(); |
| 655 SkAutoFree buffer(sk_malloc_throw(rowBytes * image.height())); | 660 SkAutoFree buffer(sk_malloc_throw(rowBytes * image.height())); |
| 656 SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(), | 661 SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(), |
| 657 kN32_SkColorType, kPremul_SkAlphaTyp
e); | 662 kN32_SkColorType, kPremul_SkAlphaTyp
e); |
| 658 if (!image.readPixels(dstInfo, buffer.get(), rowBytes, 0, 0)) { | 663 if (!image.readPixels(dstInfo, buffer.get(), rowBytes, 0, 0)) { |
| 659 SkDebugf("readPixels failed\n"); | 664 SkDebugf("readPixels failed\n"); |
| 660 return false; | 665 return false; |
| 661 } | 666 } |
| 667 |
| 668 SkBitmap bm; |
| 669 bm.installPixels(dstInfo, buffer.get(), rowBytes); |
| 670 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bm); |
| 671 |
| 662 SkDynamicMemoryWStream out; | 672 SkDynamicMemoryWStream out; |
| 663 SkDrawCommand::WritePNG((png_bytep) buffer.get(), image.width(), image.heigh
t(), out); | 673 SkDrawCommand::WritePNG((const png_bytep) encodedBitmap->bytes(), image.widt
h(), image.height(), |
| 674 out, false); |
| 664 SkData* encoded = out.copyToData(); | 675 SkData* encoded = out.copyToData(); |
| 665 Json::Value jsonData; | 676 Json::Value jsonData; |
| 666 encode_data(encoded->data(), encoded->size(), "image/png", urlDataManager, &
jsonData); | 677 encode_data(encoded->data(), encoded->size(), "image/png", urlDataManager, &
jsonData); |
| 667 (*target)[SKDEBUGCANVAS_ATTRIBUTE_DATA] = jsonData; | 678 (*target)[SKDEBUGCANVAS_ATTRIBUTE_DATA] = jsonData; |
| 668 encoded->unref(); | 679 encoded->unref(); |
| 669 return true; | 680 return true; |
| 670 } | 681 } |
| 671 | 682 |
| 672 static const char* color_type_name(SkColorType colorType) { | 683 static const char* color_type_name(SkColorType colorType) { |
| 673 switch (colorType) { | 684 switch (colorType) { |
| (...skipping 2458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3132 result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = MakeJsonMatrix(fMatrix); | 3143 result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = MakeJsonMatrix(fMatrix); |
| 3133 return result; | 3144 return result; |
| 3134 } | 3145 } |
| 3135 | 3146 |
| 3136 SkSetMatrixCommand* SkSetMatrixCommand::fromJSON(Json::Value& command, | 3147 SkSetMatrixCommand* SkSetMatrixCommand::fromJSON(Json::Value& command, |
| 3137 UrlDataManager& urlDataManager)
{ | 3148 UrlDataManager& urlDataManager)
{ |
| 3138 SkMatrix matrix; | 3149 SkMatrix matrix; |
| 3139 extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix); | 3150 extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix); |
| 3140 return new SkSetMatrixCommand(matrix); | 3151 return new SkSetMatrixCommand(matrix); |
| 3141 } | 3152 } |
| OLD | NEW |