| 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 canvasSize(width(), height()); |
| 860 FloatSize destRectSize = imageSourceInternal->defaultDestinationSize(); | 860 FloatSize sourceRectSize = imageSourceInternal->elementSize(canvasSize); |
| 861 FloatSize destRectSize = imageSourceInternal->defaultDestinationSize(canvasS
ize); |
| 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 canvasSize(this->width(), this->height()); |
| 870 FloatSize sourceRectSize = imageSourceInternal->elementSize(canvasSize); |
| 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 970 if (!drawingCanvas()) | 972 if (!drawingCanvas()) |
| 971 return; | 973 return; |
| 972 | 974 |
| 973 RefPtr<Image> image; | 975 RefPtr<Image> image; |
| 974 SourceImageStatus sourceImageStatus = InvalidSourceImageStatus; | 976 SourceImageStatus sourceImageStatus = InvalidSourceImageStatus; |
| 975 if (!imageSource->isVideoElement()) { | 977 if (!imageSource->isVideoElement()) { |
| 976 AccelerationHint hint = imageBuffer()->isAccelerated() ? PreferAccelerat
ion : PreferNoAcceleration; | 978 AccelerationHint hint = imageBuffer()->isAccelerated() ? PreferAccelerat
ion : PreferNoAcceleration; |
| 977 image = imageSource->getSourceImageForCanvas(&sourceImageStatus, hint, S
napshotReasonDrawImage); | 979 image = imageSource->getSourceImageForCanvas(&sourceImageStatus, hint, S
napshotReasonDrawImage); |
| 978 if (sourceImageStatus == UndecodableSourceImageStatus) | 980 if (sourceImageStatus == UndecodableSourceImageStatus) |
| 979 exceptionState.throwDOMException(InvalidStateError, "The HTMLImageEl
ement provided is in the 'broken' state."); | 981 exceptionState.throwDOMException(InvalidStateError, "The HTMLImageEl
ement provided is in the 'broken' state."); |
| 980 if (!image || !image->width() || !image->height()) | 982 if (!image || !image->defaultConcreteObjectSizeWidth() || !image->defaul
tConcreteObjectSizeHeight()) |
| 981 return; | 983 return; |
| 982 } else { | 984 } else { |
| 983 if (!static_cast<HTMLVideoElement*>(imageSource)->hasAvailableVideoFrame
()) | 985 if (!static_cast<HTMLVideoElement*>(imageSource)->hasAvailableVideoFrame
()) |
| 984 return; | 986 return; |
| 985 } | 987 } |
| 986 | 988 |
| 987 if (!std::isfinite(dx) || !std::isfinite(dy) || !std::isfinite(dw) || !std::
isfinite(dh) | 989 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) | 990 || !std::isfinite(sx) || !std::isfinite(sy) || !std::isfinite(sw) || !st
d::isfinite(sh) |
| 989 || !dw || !dh || !sw || !sh) | 991 || !dw || !dh || !sw || !sh) |
| 990 return; | 992 return; |
| 991 | 993 |
| 992 FloatRect srcRect = normalizeRect(FloatRect(sx, sy, sw, sh)); | 994 FloatRect srcRect = normalizeRect(FloatRect(sx, sy, sw, sh)); |
| 993 FloatRect dstRect = normalizeRect(FloatRect(dx, dy, dw, dh)); | 995 FloatRect dstRect = normalizeRect(FloatRect(dx, dy, dw, dh)); |
| 994 | 996 |
| 995 clipRectsToImageRect(FloatRect(FloatPoint(), imageSource->elementSize()), &s
rcRect, &dstRect); | 997 FloatSize canvasSize(width(), height()); |
| 998 FloatSize elementSize(imageSource->elementSize(canvasSize)); |
| 999 clipRectsToImageRect(FloatRect(FloatPoint(), elementSize), &srcRect, &dstRec
t); |
| 996 | 1000 |
| 997 imageSource->adjustDrawRects(&srcRect, &dstRect); | 1001 imageSource->adjustDrawRects(&srcRect, &dstRect); |
| 998 | 1002 |
| 999 if (srcRect.isEmpty()) | 1003 if (srcRect.isEmpty()) |
| 1000 return; | 1004 return; |
| 1001 | 1005 |
| 1002 DisableDeferralReason reason = DisableDeferralReasonUnknown; | 1006 DisableDeferralReason reason = DisableDeferralReasonUnknown; |
| 1003 if (shouldDisableDeferral(imageSource, &reason) || image->isTextureBacked()) | 1007 if (shouldDisableDeferral(imageSource, &reason) || image->isTextureBacked()) |
| 1004 disableDeferral(reason); | 1008 disableDeferral(reason); |
| 1005 | 1009 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1016 }, dstRect, CanvasRenderingContext2DState::ImagePaintType, | 1020 }, dstRect, CanvasRenderingContext2DState::ImagePaintType, |
| 1017 imageSource->isOpaque() ? CanvasRenderingContext2DState::OpaqueImage : C
anvasRenderingContext2DState::NonOpaqueImage); | 1021 imageSource->isOpaque() ? CanvasRenderingContext2DState::OpaqueImage : C
anvasRenderingContext2DState::NonOpaqueImage); |
| 1018 | 1022 |
| 1019 validateStateStack(); | 1023 validateStateStack(); |
| 1020 | 1024 |
| 1021 bool isExpensive = false; | 1025 bool isExpensive = false; |
| 1022 | 1026 |
| 1023 if (ExpensiveCanvasHeuristicParameters::SVGImageSourcesAreExpensive && image
Source->isSVGSource()) | 1027 if (ExpensiveCanvasHeuristicParameters::SVGImageSourcesAreExpensive && image
Source->isSVGSource()) |
| 1024 isExpensive = true; | 1028 isExpensive = true; |
| 1025 | 1029 |
| 1026 if (imageSource->elementSize().width() * imageSource->elementSize().height()
> width() * height() * ExpensiveCanvasHeuristicParameters::ExpensiveImageSizeRa
tio) | 1030 if (elementSize.width() * elementSize.height() > width() * height() * Expens
iveCanvasHeuristicParameters::ExpensiveImageSizeRatio) |
| 1027 isExpensive = true; | 1031 isExpensive = true; |
| 1028 | 1032 |
| 1029 if (isExpensive) { | 1033 if (isExpensive) { |
| 1030 ImageBuffer* buffer = imageBuffer(); | 1034 ImageBuffer* buffer = imageBuffer(); |
| 1031 if (buffer) | 1035 if (buffer) |
| 1032 buffer->setHasExpensiveOp(); | 1036 buffer->setHasExpensiveOp(); |
| 1033 } | 1037 } |
| 1034 | 1038 |
| 1035 if (imageSource->isCanvasElement() && static_cast<HTMLCanvasElement*>(imageS
ource)->is3D()) { | 1039 if (imageSource->isCanvasElement() && static_cast<HTMLCanvasElement*>(imageS
ource)->is3D()) { |
| 1036 // WebGL to 2D canvas: must flush graphics context to prevent a race | 1040 // WebGL to 2D canvas: must flush graphics context to prevent a race |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1082 return nullptr; | 1086 return nullptr; |
| 1083 | 1087 |
| 1084 SourceImageStatus status; | 1088 SourceImageStatus status; |
| 1085 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource); | 1089 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource); |
| 1086 RefPtr<Image> imageForRendering = imageSourceInternal->getSourceImageForCanv
as(&status, PreferNoAcceleration, SnapshotReasonCreatePattern); | 1090 RefPtr<Image> imageForRendering = imageSourceInternal->getSourceImageForCanv
as(&status, PreferNoAcceleration, SnapshotReasonCreatePattern); |
| 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(FloatSize(width(), height()))
.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 |