Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/canvas2d/BaseRenderingContext2D.h" | 5 #include "modules/canvas2d/BaseRenderingContext2D.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionMessages.h" | 7 #include "bindings/core/v8/ExceptionMessages.h" |
| 8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | 9 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
| 10 #include "core/css/parser/CSSParser.h" | 10 #include "core/css/parser/CSSParser.h" |
| (...skipping 838 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 849 return value.getAsHTMLCanvasElement().get(); | 849 return value.getAsHTMLCanvasElement().get(); |
| 850 if (value.isImageBitmap()) | 850 if (value.isImageBitmap()) |
| 851 return value.getAsImageBitmap().get(); | 851 return value.getAsImageBitmap().get(); |
| 852 ASSERT_NOT_REACHED(); | 852 ASSERT_NOT_REACHED(); |
| 853 return nullptr; | 853 return nullptr; |
| 854 } | 854 } |
| 855 | 855 |
| 856 void BaseRenderingContext2D::drawImage(const CanvasImageSourceUnion& imageSource , double x, double y, ExceptionState& exceptionState) | 856 void BaseRenderingContext2D::drawImage(const CanvasImageSourceUnion& imageSource , double x, double y, ExceptionState& exceptionState) |
| 857 { | 857 { |
| 858 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource); | 858 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource); |
| 859 FloatSize sourceRectSize = imageSourceInternal->elementSize(); | 859 FloatSize defaultObjectSize(width(), height()); |
| 860 FloatSize destRectSize = imageSourceInternal->defaultDestinationSize(); | 860 FloatSize sourceRectSize = imageSourceInternal->elementSize(defaultObjectSiz e); |
| 861 FloatSize destRectSize = imageSourceInternal->defaultDestinationSize(default ObjectSize); | |
| 861 drawImage(imageSourceInternal, 0, 0, sourceRectSize.width(), sourceRectSize. height(), x, y, destRectSize.width(), destRectSize.height(), exceptionState); | 862 drawImage(imageSourceInternal, 0, 0, sourceRectSize.width(), sourceRectSize. height(), x, y, destRectSize.width(), destRectSize.height(), exceptionState); |
| 862 } | 863 } |
| 863 | 864 |
| 864 void BaseRenderingContext2D::drawImage(const CanvasImageSourceUnion& imageSource , | 865 void BaseRenderingContext2D::drawImage(const CanvasImageSourceUnion& imageSource , |
| 865 double x, double y, double width, double height, ExceptionState& exceptionSt ate) | 866 double x, double y, double width, double height, ExceptionState& exceptionSt ate) |
| 866 { | 867 { |
| 867 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource); | 868 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource); |
| 868 FloatSize sourceRectSize = imageSourceInternal->elementSize(); | 869 FloatSize defaultObjectSize(this->width(), this->height()); |
| 870 FloatSize sourceRectSize = imageSourceInternal->elementSize(defaultObjectSiz e); | |
| 869 drawImage(imageSourceInternal, 0, 0, sourceRectSize.width(), sourceRectSize. height(), x, y, width, height, exceptionState); | 871 drawImage(imageSourceInternal, 0, 0, sourceRectSize.width(), sourceRectSize. height(), x, y, width, height, exceptionState); |
| 870 } | 872 } |
| 871 | 873 |
| 872 void BaseRenderingContext2D::drawImage(const CanvasImageSourceUnion& imageSource , | 874 void BaseRenderingContext2D::drawImage(const CanvasImageSourceUnion& imageSource , |
| 873 double sx, double sy, double sw, double sh, | 875 double sx, double sy, double sw, double sh, |
| 874 double dx, double dy, double dw, double dh, ExceptionState& exceptionState) | 876 double dx, double dy, double dw, double dh, ExceptionState& exceptionState) |
| 875 { | 877 { |
| 876 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource); | 878 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource); |
| 877 drawImage(imageSourceInternal, sx, sy, sw, sh, dx, dy, dw, dh, exceptionStat e); | 879 drawImage(imageSourceInternal, sx, sy, sw, sh, dx, dy, dw, dh, exceptionStat e); |
| 878 } | 880 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 964 } | 966 } |
| 965 | 967 |
| 966 void BaseRenderingContext2D::drawImage(CanvasImageSource* imageSource, | 968 void BaseRenderingContext2D::drawImage(CanvasImageSource* imageSource, |
| 967 double sx, double sy, double sw, double sh, | 969 double sx, double sy, double sw, double sh, |
| 968 double dx, double dy, double dw, double dh, ExceptionState& exceptionState) | 970 double dx, double dy, double dw, double dh, ExceptionState& exceptionState) |
| 969 { | 971 { |
| 970 if (!drawingCanvas()) | 972 if (!drawingCanvas()) |
| 971 return; | 973 return; |
| 972 | 974 |
| 973 RefPtr<Image> image; | 975 RefPtr<Image> image; |
| 976 FloatSize defaultObjectSize(width(), height()); | |
| 974 SourceImageStatus sourceImageStatus = InvalidSourceImageStatus; | 977 SourceImageStatus sourceImageStatus = InvalidSourceImageStatus; |
| 975 if (!imageSource->isVideoElement()) { | 978 if (!imageSource->isVideoElement()) { |
| 976 AccelerationHint hint = imageBuffer()->isAccelerated() ? PreferAccelerat ion : PreferNoAcceleration; | 979 AccelerationHint hint = imageBuffer()->isAccelerated() ? PreferAccelerat ion : PreferNoAcceleration; |
| 977 image = imageSource->getSourceImageForCanvas(&sourceImageStatus, hint, S napshotReasonDrawImage); | 980 image = imageSource->getSourceImageForCanvas(&sourceImageStatus, hint, S napshotReasonDrawImage, defaultObjectSize); |
| 978 if (sourceImageStatus == UndecodableSourceImageStatus) | 981 if (sourceImageStatus == UndecodableSourceImageStatus) |
| 979 exceptionState.throwDOMException(InvalidStateError, "The HTMLImageEl ement provided is in the 'broken' state."); | 982 exceptionState.throwDOMException(InvalidStateError, "The HTMLImageEl ement provided is in the 'broken' state."); |
| 980 if (!image || !image->width() || !image->height()) | 983 if (!image || !image->width() || !image->height()) |
| 981 return; | 984 return; |
| 982 } else { | 985 } else { |
| 983 if (!static_cast<HTMLVideoElement*>(imageSource)->hasAvailableVideoFrame ()) | 986 if (!static_cast<HTMLVideoElement*>(imageSource)->hasAvailableVideoFrame ()) |
| 984 return; | 987 return; |
| 985 } | 988 } |
| 986 | 989 |
| 987 if (!std::isfinite(dx) || !std::isfinite(dy) || !std::isfinite(dw) || !std:: isfinite(dh) | 990 if (!std::isfinite(dx) || !std::isfinite(dy) || !std::isfinite(dw) || !std:: isfinite(dh) |
| 988 || !std::isfinite(sx) || !std::isfinite(sy) || !std::isfinite(sw) || !st d::isfinite(sh) | 991 || !std::isfinite(sx) || !std::isfinite(sy) || !std::isfinite(sw) || !st d::isfinite(sh) |
| 989 || !dw || !dh || !sw || !sh) | 992 || !dw || !dh || !sw || !sh) |
| 990 return; | 993 return; |
| 991 | 994 |
| 992 FloatRect srcRect = normalizeRect(FloatRect(sx, sy, sw, sh)); | 995 FloatRect srcRect = normalizeRect(FloatRect(sx, sy, sw, sh)); |
| 993 FloatRect dstRect = normalizeRect(FloatRect(dx, dy, dw, dh)); | 996 FloatRect dstRect = normalizeRect(FloatRect(dx, dy, dw, dh)); |
| 994 | 997 |
| 995 clipRectsToImageRect(FloatRect(FloatPoint(), imageSource->elementSize()), &s rcRect, &dstRect); | 998 clipRectsToImageRect(FloatRect(FloatPoint(), imageSource->elementSize(defaul tObjectSize)), &srcRect, &dstRect); |
| 996 | 999 |
| 997 imageSource->adjustDrawRects(&srcRect, &dstRect); | 1000 imageSource->adjustDrawRects(&srcRect, &dstRect); |
| 998 | 1001 |
| 999 if (srcRect.isEmpty()) | 1002 if (srcRect.isEmpty()) |
| 1000 return; | 1003 return; |
| 1001 | 1004 |
| 1002 DisableDeferralReason reason = DisableDeferralReasonUnknown; | 1005 DisableDeferralReason reason = DisableDeferralReasonUnknown; |
| 1003 if (shouldDisableDeferral(imageSource, &reason) || image->isTextureBacked()) | 1006 if (shouldDisableDeferral(imageSource, &reason) || image->isTextureBacked()) |
| 1004 disableDeferral(reason); | 1007 disableDeferral(reason); |
| 1005 | 1008 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1016 }, dstRect, CanvasRenderingContext2DState::ImagePaintType, | 1019 }, dstRect, CanvasRenderingContext2DState::ImagePaintType, |
| 1017 imageSource->isOpaque() ? CanvasRenderingContext2DState::OpaqueImage : C anvasRenderingContext2DState::NonOpaqueImage); | 1020 imageSource->isOpaque() ? CanvasRenderingContext2DState::OpaqueImage : C anvasRenderingContext2DState::NonOpaqueImage); |
| 1018 | 1021 |
| 1019 validateStateStack(); | 1022 validateStateStack(); |
| 1020 | 1023 |
| 1021 bool isExpensive = false; | 1024 bool isExpensive = false; |
| 1022 | 1025 |
| 1023 if (ExpensiveCanvasHeuristicParameters::SVGImageSourcesAreExpensive && image Source->isSVGSource()) | 1026 if (ExpensiveCanvasHeuristicParameters::SVGImageSourcesAreExpensive && image Source->isSVGSource()) |
| 1024 isExpensive = true; | 1027 isExpensive = true; |
| 1025 | 1028 |
| 1026 if (imageSource->elementSize().width() * imageSource->elementSize().height() > width() * height() * ExpensiveCanvasHeuristicParameters::ExpensiveImageSizeRa tio) | 1029 if (imageSource->elementSize(defaultObjectSize).width() * imageSource->eleme ntSize(defaultObjectSize).height() > width() * height() * ExpensiveCanvasHeurist icParameters::ExpensiveImageSizeRatio) |
|
fs
2016/03/04 14:31:26
Nit: Maybe store the result of imageSource->elemen
Justin Novosad
2016/03/04 14:38:59
+1
davve
2016/03/04 15:10:36
Done.
| |
| 1027 isExpensive = true; | 1030 isExpensive = true; |
| 1028 | 1031 |
| 1029 if (isExpensive) { | 1032 if (isExpensive) { |
| 1030 ImageBuffer* buffer = imageBuffer(); | 1033 ImageBuffer* buffer = imageBuffer(); |
| 1031 if (buffer) | 1034 if (buffer) |
| 1032 buffer->setHasExpensiveOp(); | 1035 buffer->setHasExpensiveOp(); |
| 1033 } | 1036 } |
| 1034 | 1037 |
| 1035 if (imageSource->isCanvasElement() && static_cast<HTMLCanvasElement*>(imageS ource)->is3D()) { | 1038 if (imageSource->isCanvasElement() && static_cast<HTMLCanvasElement*>(imageS ource)->is3D()) { |
| 1036 // WebGL to 2D canvas: must flush graphics context to prevent a race | 1039 // WebGL to 2D canvas: must flush graphics context to prevent a race |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1076 } | 1079 } |
| 1077 | 1080 |
| 1078 CanvasPattern* BaseRenderingContext2D::createPattern(const CanvasImageSourceUnio n& imageSource, const String& repetitionType, ExceptionState& exceptionState) | 1081 CanvasPattern* BaseRenderingContext2D::createPattern(const CanvasImageSourceUnio n& imageSource, const String& repetitionType, ExceptionState& exceptionState) |
| 1079 { | 1082 { |
| 1080 Pattern::RepeatMode repeatMode = CanvasPattern::parseRepetitionType(repetiti onType, exceptionState); | 1083 Pattern::RepeatMode repeatMode = CanvasPattern::parseRepetitionType(repetiti onType, exceptionState); |
| 1081 if (exceptionState.hadException()) | 1084 if (exceptionState.hadException()) |
| 1082 return nullptr; | 1085 return nullptr; |
| 1083 | 1086 |
| 1084 SourceImageStatus status; | 1087 SourceImageStatus status; |
| 1085 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource); | 1088 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource); |
| 1086 RefPtr<Image> imageForRendering = imageSourceInternal->getSourceImageForCanv as(&status, PreferNoAcceleration, SnapshotReasonCreatePattern); | 1089 FloatSize defaultObjectSize(width(), height()); |
| 1090 RefPtr<Image> imageForRendering = imageSourceInternal->getSourceImageForCanv as(&status, PreferNoAcceleration, SnapshotReasonCreatePattern, defaultObjectSize ); | |
| 1087 | 1091 |
| 1088 switch (status) { | 1092 switch (status) { |
| 1089 case NormalSourceImageStatus: | 1093 case NormalSourceImageStatus: |
| 1090 break; | 1094 break; |
| 1091 case ZeroSizeCanvasSourceImageStatus: | 1095 case ZeroSizeCanvasSourceImageStatus: |
| 1092 exceptionState.throwDOMException(InvalidStateError, String::format("The canvas %s is 0.", imageSourceInternal->elementSize().width() ? "height" : "width ")); | 1096 exceptionState.throwDOMException(InvalidStateError, String::format("The canvas %s is 0.", imageSourceInternal->elementSize(defaultObjectSize).width() ? "height" : "width")); |
| 1093 return nullptr; | 1097 return nullptr; |
| 1094 case UndecodableSourceImageStatus: | 1098 case UndecodableSourceImageStatus: |
| 1095 exceptionState.throwDOMException(InvalidStateError, "Source image is in the 'broken' state."); | 1099 exceptionState.throwDOMException(InvalidStateError, "Source image is in the 'broken' state."); |
| 1096 return nullptr; | 1100 return nullptr; |
| 1097 case InvalidSourceImageStatus: | 1101 case InvalidSourceImageStatus: |
| 1098 imageForRendering = Image::nullImage(); | 1102 imageForRendering = Image::nullImage(); |
| 1099 break; | 1103 break; |
| 1100 case IncompleteSourceImageStatus: | 1104 case IncompleteSourceImageStatus: |
| 1101 return nullptr; | 1105 return nullptr; |
| 1102 default: | 1106 default: |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1357 | 1361 |
| 1358 imageBuffer()->willOverwriteCanvas(); | 1362 imageBuffer()->willOverwriteCanvas(); |
| 1359 } | 1363 } |
| 1360 | 1364 |
| 1361 DEFINE_TRACE(BaseRenderingContext2D) | 1365 DEFINE_TRACE(BaseRenderingContext2D) |
| 1362 { | 1366 { |
| 1363 visitor->trace(m_stateStack); | 1367 visitor->trace(m_stateStack); |
| 1364 } | 1368 } |
| 1365 | 1369 |
| 1366 } // namespace blink | 1370 } // namespace blink |
| OLD | NEW |