| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #include "SkDrawCommand.h" | 10 #include "SkDrawCommand.h" |
| (...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 613 // PNG encode doesn't necessarily support all color formats, convert to
a different | 613 // PNG encode doesn't necessarily support all color formats, convert to
a different |
| 614 // format | 614 // format |
| 615 size_t rowBytes = 4 * image.width(); | 615 size_t rowBytes = 4 * image.width(); |
| 616 void* buffer = sk_malloc_throw(rowBytes * image.height()); | 616 void* buffer = sk_malloc_throw(rowBytes * image.height()); |
| 617 SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(), | 617 SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(), |
| 618 kN32_SkColorType, kPremul_SkAlph
aType); | 618 kN32_SkColorType, kPremul_SkAlph
aType); |
| 619 if (!image.readPixels(dstInfo, buffer, rowBytes, 0, 0)) { | 619 if (!image.readPixels(dstInfo, buffer, rowBytes, 0, 0)) { |
| 620 SkDebugf("readPixels failed\n"); | 620 SkDebugf("readPixels failed\n"); |
| 621 return false; | 621 return false; |
| 622 } | 622 } |
| 623 SkImage* converted = SkImage::NewRasterCopy(dstInfo, buffer, rowBytes); | 623 sk_sp<SkImage> converted = SkImage::MakeRasterCopy(SkPixmap(dstInfo, buf
fer, rowBytes)); |
| 624 encoded = converted->encode(SkImageEncoder::kPNG_Type, 100); | 624 encoded = converted->encode(SkImageEncoder::kPNG_Type, 100); |
| 625 if (encoded == nullptr) { | 625 if (encoded == nullptr) { |
| 626 SkDebugf("image encode failed\n"); | 626 SkDebugf("image encode failed\n"); |
| 627 return false; | 627 return false; |
| 628 } | 628 } |
| 629 sk_free(converted); | |
| 630 sk_free(buffer); | 629 sk_free(buffer); |
| 631 } | 630 } |
| 632 Json::Value jsonData; | 631 Json::Value jsonData; |
| 633 encode_data(encoded->data(), encoded->size(), "image/png", urlDataManager, &
jsonData); | 632 encode_data(encoded->data(), encoded->size(), "image/png", urlDataManager, &
jsonData); |
| 634 (*target)[SKDEBUGCANVAS_ATTRIBUTE_DATA] = jsonData; | 633 (*target)[SKDEBUGCANVAS_ATTRIBUTE_DATA] = jsonData; |
| 635 encoded->unref(); | 634 encoded->unref(); |
| 636 return true; | 635 return true; |
| 637 } | 636 } |
| 638 | 637 |
| 639 static const char* color_type_name(SkColorType colorType) { | 638 static const char* color_type_name(SkColorType colorType) { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 } | 747 } |
| 749 | 748 |
| 750 // caller is responsible for freeing return value | 749 // caller is responsible for freeing return value |
| 751 static SkBitmap* load_bitmap(const Json::Value& jsonBitmap, UrlDataManager& urlD
ataManager) { | 750 static SkBitmap* load_bitmap(const Json::Value& jsonBitmap, UrlDataManager& urlD
ataManager) { |
| 752 if (!jsonBitmap.isMember(SKDEBUGCANVAS_ATTRIBUTE_DATA)) { | 751 if (!jsonBitmap.isMember(SKDEBUGCANVAS_ATTRIBUTE_DATA)) { |
| 753 SkDebugf("invalid bitmap\n"); | 752 SkDebugf("invalid bitmap\n"); |
| 754 return nullptr; | 753 return nullptr; |
| 755 } | 754 } |
| 756 const void* data; | 755 const void* data; |
| 757 int size = decode_data(jsonBitmap[SKDEBUGCANVAS_ATTRIBUTE_DATA], urlDataMana
ger, &data); | 756 int size = decode_data(jsonBitmap[SKDEBUGCANVAS_ATTRIBUTE_DATA], urlDataMana
ger, &data); |
| 758 SkAutoTUnref<SkData> encoded(SkData::NewWithoutCopy(data, size)); | 757 sk_sp<SkData> encoded(SkData::NewWithoutCopy(data, size)); |
| 759 SkAutoTDelete<SkImage> image(SkImage::NewFromEncoded(encoded, nullptr)); | 758 sk_sp<SkImage> image(SkImage::MakeFromEncoded(std::move(encoded), nullptr)); |
| 760 | 759 |
| 761 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap()); | 760 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap()); |
| 762 if (nullptr != image) { | 761 if (nullptr != image) { |
| 763 if (!image->asLegacyBitmap(bitmap, SkImage::kRW_LegacyBitmapMode)) { | 762 if (!image->asLegacyBitmap(bitmap, SkImage::kRW_LegacyBitmapMode)) { |
| 764 SkDebugf("image decode failed\n"); | 763 SkDebugf("image decode failed\n"); |
| 765 return nullptr; | 764 return nullptr; |
| 766 } | 765 } |
| 767 | 766 |
| 768 if (jsonBitmap.isMember(SKDEBUGCANVAS_ATTRIBUTE_COLOR)) { | 767 if (jsonBitmap.isMember(SKDEBUGCANVAS_ATTRIBUTE_COLOR)) { |
| 769 const char* ctName = jsonBitmap[SKDEBUGCANVAS_ATTRIBUTE_COLOR].asCSt
ring(); | 768 const char* ctName = jsonBitmap[SKDEBUGCANVAS_ATTRIBUTE_COLOR].asCSt
ring(); |
| 770 SkColorType ct = colortype_from_name(ctName); | 769 SkColorType ct = colortype_from_name(ctName); |
| 771 if (ct != kIndex_8_SkColorType) { | 770 if (ct != kIndex_8_SkColorType) { |
| 772 bitmap.reset(convert_colortype(bitmap.release(), ct)); | 771 bitmap.reset(convert_colortype(bitmap.release(), ct)); |
| 773 } | 772 } |
| 774 } | 773 } |
| 775 return bitmap.release(); | 774 return bitmap.release(); |
| 776 } | 775 } |
| 777 SkDebugf("image decode failed\n"); | 776 SkDebugf("image decode failed\n"); |
| 778 return nullptr; | 777 return nullptr; |
| 779 } | 778 } |
| 780 | 779 |
| 781 static SkImage* load_image(const Json::Value& jsonImage, UrlDataManager& urlData
Manager) { | 780 static sk_sp<SkImage> load_image(const Json::Value& jsonImage, UrlDataManager& u
rlDataManager) { |
| 782 SkBitmap* bitmap = load_bitmap(jsonImage, urlDataManager); | 781 SkBitmap* bitmap = load_bitmap(jsonImage, urlDataManager); |
| 783 if (bitmap == nullptr) { | 782 if (bitmap == nullptr) { |
| 784 return nullptr; | 783 return nullptr; |
| 785 } | 784 } |
| 786 SkImage* result = SkImage::NewFromBitmap(*bitmap); | 785 auto result = SkImage::MakeFromBitmap(*bitmap); |
| 787 delete bitmap; | 786 delete bitmap; |
| 788 return result; | 787 return result; |
| 789 } | 788 } |
| 790 | 789 |
| 791 static bool SK_WARN_UNUSED_RESULT flatten(const SkBitmap& bitmap, Json::Value* t
arget, | 790 static bool SK_WARN_UNUSED_RESULT flatten(const SkBitmap& bitmap, Json::Value* t
arget, |
| 792 UrlDataManager& urlDataManager) { | 791 UrlDataManager& urlDataManager) { |
| 793 bitmap.lockPixels(); | 792 bitmap.lockPixels(); |
| 794 SkAutoTUnref<SkImage> image(SkImage::NewFromBitmap(bitmap)); | 793 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bitmap)); |
| 795 bitmap.unlockPixels(); | 794 bitmap.unlockPixels(); |
| 796 (*target)[SKDEBUGCANVAS_ATTRIBUTE_COLOR] = Json::Value(color_type_name(bitma
p.colorType())); | 795 (*target)[SKDEBUGCANVAS_ATTRIBUTE_COLOR] = Json::Value(color_type_name(bitma
p.colorType())); |
| 797 (*target)[SKDEBUGCANVAS_ATTRIBUTE_ALPHA] = Json::Value(alpha_type_name(bitma
p.alphaType())); | 796 (*target)[SKDEBUGCANVAS_ATTRIBUTE_ALPHA] = Json::Value(alpha_type_name(bitma
p.alphaType())); |
| 798 bool success = flatten(*image, target, urlDataManager); | 797 bool success = flatten(*image, target, urlDataManager); |
| 799 return success; | 798 return success; |
| 800 } | 799 } |
| 801 | 800 |
| 802 static void apply_paint_color(const SkPaint& paint, Json::Value* target) { | 801 static void apply_paint_color(const SkPaint& paint, Json::Value* target) { |
| 803 SkColor color = paint.getColor(); | 802 SkColor color = paint.getColor(); |
| 804 if (color != SK_ColorBLACK) { | 803 if (color != SK_ColorBLACK) { |
| (...skipping 1153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1958 result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_point(fLeft, fTop); | 1957 result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_point(fLeft, fTop); |
| 1959 if (fPaint.isValid()) { | 1958 if (fPaint.isValid()) { |
| 1960 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaint.get(
), urlDataManager); | 1959 result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaint.get(
), urlDataManager); |
| 1961 } | 1960 } |
| 1962 } | 1961 } |
| 1963 return result; | 1962 return result; |
| 1964 } | 1963 } |
| 1965 | 1964 |
| 1966 SkDrawImageCommand* SkDrawImageCommand::fromJSON(Json::Value& command, | 1965 SkDrawImageCommand* SkDrawImageCommand::fromJSON(Json::Value& command, |
| 1967 UrlDataManager& urlDataManager)
{ | 1966 UrlDataManager& urlDataManager)
{ |
| 1968 SkImage* image = load_image(command[SKDEBUGCANVAS_ATTRIBUTE_IMAGE], urlDataM
anager); | 1967 sk_sp<SkImage> image = load_image(command[SKDEBUGCANVAS_ATTRIBUTE_IMAGE], ur
lDataManager); |
| 1969 if (image == nullptr) { | 1968 if (image == nullptr) { |
| 1970 return nullptr; | 1969 return nullptr; |
| 1971 } | 1970 } |
| 1972 Json::Value point = command[SKDEBUGCANVAS_ATTRIBUTE_COORDS]; | 1971 Json::Value point = command[SKDEBUGCANVAS_ATTRIBUTE_COORDS]; |
| 1973 SkPaint* paintPtr; | 1972 SkPaint* paintPtr; |
| 1974 SkPaint paint; | 1973 SkPaint paint; |
| 1975 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) { | 1974 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) { |
| 1976 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], urlDataManage
r, &paint); | 1975 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], urlDataManage
r, &paint); |
| 1977 paintPtr = &paint; | 1976 paintPtr = &paint; |
| 1978 } | 1977 } |
| 1979 else { | 1978 else { |
| 1980 paintPtr = nullptr; | 1979 paintPtr = nullptr; |
| 1981 } | 1980 } |
| 1982 SkDrawImageCommand* result = new SkDrawImageCommand(image, point[0].asFloat(
), | 1981 SkDrawImageCommand* result = new SkDrawImageCommand(image.get(), point[0].as
Float(), |
| 1983 point[1].asFloat(), pain
tPtr); | 1982 point[1].asFloat(), pain
tPtr); |
| 1984 image->unref(); | |
| 1985 return result; | 1983 return result; |
| 1986 } | 1984 } |
| 1987 | 1985 |
| 1988 SkDrawImageRectCommand::SkDrawImageRectCommand(const SkImage* image, const SkRec
t* src, | 1986 SkDrawImageRectCommand::SkDrawImageRectCommand(const SkImage* image, const SkRec
t* src, |
| 1989 const SkRect& dst, const SkPaint*
paint, | 1987 const SkRect& dst, const SkPaint*
paint, |
| 1990 SkCanvas::SrcRectConstraint const
raint) | 1988 SkCanvas::SrcRectConstraint const
raint) |
| 1991 : INHERITED(kDrawImageRect_OpType) | 1989 : INHERITED(kDrawImageRect_OpType) |
| 1992 , fImage(SkRef(image)) | 1990 , fImage(SkRef(image)) |
| 1993 , fDst(dst) | 1991 , fDst(dst) |
| 1994 , fConstraint(constraint) { | 1992 , fConstraint(constraint) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2041 } | 2039 } |
| 2042 if (fConstraint == SkCanvas::kStrict_SrcRectConstraint) { | 2040 if (fConstraint == SkCanvas::kStrict_SrcRectConstraint) { |
| 2043 result[SKDEBUGCANVAS_ATTRIBUTE_STRICT] = Json::Value(true); | 2041 result[SKDEBUGCANVAS_ATTRIBUTE_STRICT] = Json::Value(true); |
| 2044 } | 2042 } |
| 2045 } | 2043 } |
| 2046 return result; | 2044 return result; |
| 2047 } | 2045 } |
| 2048 | 2046 |
| 2049 SkDrawImageRectCommand* SkDrawImageRectCommand::fromJSON(Json::Value& command, | 2047 SkDrawImageRectCommand* SkDrawImageRectCommand::fromJSON(Json::Value& command, |
| 2050 UrlDataManager& urlData
Manager) { | 2048 UrlDataManager& urlData
Manager) { |
| 2051 SkImage* image = load_image(command[SKDEBUGCANVAS_ATTRIBUTE_IMAGE], urlDataM
anager); | 2049 sk_sp<SkImage> image = load_image(command[SKDEBUGCANVAS_ATTRIBUTE_IMAGE], ur
lDataManager); |
| 2052 if (image == nullptr) { | 2050 if (image == nullptr) { |
| 2053 return nullptr; | 2051 return nullptr; |
| 2054 } | 2052 } |
| 2055 SkRect dst; | 2053 SkRect dst; |
| 2056 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_DST], &dst); | 2054 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_DST], &dst); |
| 2057 SkPaint* paintPtr; | 2055 SkPaint* paintPtr; |
| 2058 SkPaint paint; | 2056 SkPaint paint; |
| 2059 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) { | 2057 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) { |
| 2060 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], urlDataManage
r, &paint); | 2058 extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], urlDataManage
r, &paint); |
| 2061 paintPtr = &paint; | 2059 paintPtr = &paint; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 2073 } | 2071 } |
| 2074 SkRect* srcPtr; | 2072 SkRect* srcPtr; |
| 2075 SkRect src; | 2073 SkRect src; |
| 2076 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_SRC)) { | 2074 if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_SRC)) { |
| 2077 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_SRC], &src); | 2075 extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_SRC], &src); |
| 2078 srcPtr = &src; | 2076 srcPtr = &src; |
| 2079 } | 2077 } |
| 2080 else { | 2078 else { |
| 2081 srcPtr = nullptr; | 2079 srcPtr = nullptr; |
| 2082 } | 2080 } |
| 2083 SkDrawImageRectCommand* result = new SkDrawImageRectCommand(image, srcPtr, d
st, paintPtr, | 2081 SkDrawImageRectCommand* result = new SkDrawImageRectCommand(image.get(), src
Ptr, dst, paintPtr, |
| 2084 constraint); | 2082 constraint); |
| 2085 image->unref(); | |
| 2086 return result; | 2083 return result; |
| 2087 } | 2084 } |
| 2088 | 2085 |
| 2089 SkDrawOvalCommand::SkDrawOvalCommand(const SkRect& oval, const SkPaint& paint) | 2086 SkDrawOvalCommand::SkDrawOvalCommand(const SkRect& oval, const SkPaint& paint) |
| 2090 : INHERITED(kDrawOval_OpType) { | 2087 : INHERITED(kDrawOval_OpType) { |
| 2091 fOval = oval; | 2088 fOval = oval; |
| 2092 fPaint = paint; | 2089 fPaint = paint; |
| 2093 | 2090 |
| 2094 fInfo.push(SkObjectParser::RectToString(oval)); | 2091 fInfo.push(SkObjectParser::RectToString(oval)); |
| 2095 fInfo.push(SkObjectParser::PaintToString(paint)); | 2092 fInfo.push(SkObjectParser::PaintToString(paint)); |
| (...skipping 1010 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3106 result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = MakeJsonMatrix(fMatrix); | 3103 result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = MakeJsonMatrix(fMatrix); |
| 3107 return result; | 3104 return result; |
| 3108 } | 3105 } |
| 3109 | 3106 |
| 3110 SkSetMatrixCommand* SkSetMatrixCommand::fromJSON(Json::Value& command, | 3107 SkSetMatrixCommand* SkSetMatrixCommand::fromJSON(Json::Value& command, |
| 3111 UrlDataManager& urlDataManager)
{ | 3108 UrlDataManager& urlDataManager)
{ |
| 3112 SkMatrix matrix; | 3109 SkMatrix matrix; |
| 3113 extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix); | 3110 extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix); |
| 3114 return new SkSetMatrixCommand(matrix); | 3111 return new SkSetMatrixCommand(matrix); |
| 3115 } | 3112 } |
| OLD | NEW |