| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> | 3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> |
| 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. | 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. |
| 5 * | 5 * |
| 6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
| 8 * are met: | 8 * are met: |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 #include "core/layout/LayoutHTMLCanvas.h" | 60 #include "core/layout/LayoutHTMLCanvas.h" |
| 61 #include "core/layout/api/LayoutViewItem.h" | 61 #include "core/layout/api/LayoutViewItem.h" |
| 62 #include "core/layout/compositing/PaintLayerCompositor.h" | 62 #include "core/layout/compositing/PaintLayerCompositor.h" |
| 63 #include "core/page/ChromeClient.h" | 63 #include "core/page/ChromeClient.h" |
| 64 #include "core/paint/PaintLayer.h" | 64 #include "core/paint/PaintLayer.h" |
| 65 #include "core/paint/PaintTiming.h" | 65 #include "core/paint/PaintTiming.h" |
| 66 #include "core/probe/CoreProbes.h" | 66 #include "core/probe/CoreProbes.h" |
| 67 #include "platform/Histogram.h" | 67 #include "platform/Histogram.h" |
| 68 #include "platform/RuntimeEnabledFeatures.h" | 68 #include "platform/RuntimeEnabledFeatures.h" |
| 69 #include "platform/graphics/Canvas2DImageBufferSurface.h" | 69 #include "platform/graphics/Canvas2DImageBufferSurface.h" |
| 70 #include "platform/graphics/CanvasHeuristicParameters.h" |
| 70 #include "platform/graphics/CanvasMetrics.h" | 71 #include "platform/graphics/CanvasMetrics.h" |
| 71 #include "platform/graphics/ExpensiveCanvasHeuristicParameters.h" | |
| 72 #include "platform/graphics/ImageBuffer.h" | 72 #include "platform/graphics/ImageBuffer.h" |
| 73 #include "platform/graphics/RecordingImageBufferSurface.h" | 73 #include "platform/graphics/RecordingImageBufferSurface.h" |
| 74 #include "platform/graphics/StaticBitmapImage.h" | 74 #include "platform/graphics/StaticBitmapImage.h" |
| 75 #include "platform/graphics/UnacceleratedImageBufferSurface.h" | 75 #include "platform/graphics/UnacceleratedImageBufferSurface.h" |
| 76 #include "platform/graphics/gpu/AcceleratedImageBufferSurface.h" | 76 #include "platform/graphics/gpu/AcceleratedImageBufferSurface.h" |
| 77 #include "platform/graphics/gpu/SharedGpuContext.h" | 77 #include "platform/graphics/gpu/SharedGpuContext.h" |
| 78 #include "platform/graphics/paint/PaintCanvas.h" | 78 #include "platform/graphics/paint/PaintCanvas.h" |
| 79 #include "platform/image-encoders/ImageEncoderUtils.h" | 79 #include "platform/image-encoders/ImageEncoderUtils.h" |
| 80 #include "platform/transforms/AffineTransform.h" | 80 #include "platform/transforms/AffineTransform.h" |
| 81 #include "platform/wtf/CheckedNumeric.h" | 81 #include "platform/wtf/CheckedNumeric.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 inline HTMLCanvasElement::HTMLCanvasElement(Document& document) | 135 inline HTMLCanvasElement::HTMLCanvasElement(Document& document) |
| 136 : HTMLElement(canvasTag, document), | 136 : HTMLElement(canvasTag, document), |
| 137 ContextLifecycleObserver(&document), | 137 ContextLifecycleObserver(&document), |
| 138 PageVisibilityObserver(document.GetPage()), | 138 PageVisibilityObserver(document.GetPage()), |
| 139 size_(kDefaultWidth, kDefaultHeight), | 139 size_(kDefaultWidth, kDefaultHeight), |
| 140 context_(this, nullptr), | 140 context_(this, nullptr), |
| 141 ignore_reset_(false), | 141 ignore_reset_(false), |
| 142 externally_allocated_memory_(0), | 142 externally_allocated_memory_(0), |
| 143 origin_clean_(true), | 143 origin_clean_(true), |
| 144 did_fail_to_create_image_buffer_(false), | 144 did_fail_to_create_image_buffer_(false), |
| 145 image_buffer_is_clear_(false), | 145 image_buffer_is_clear_(false) { |
| 146 num_frames_since_last_rendering_mode_switch_(0), | |
| 147 pending_rendering_mode_switch_(false) { | |
| 148 CanvasMetrics::CountCanvasContextUsage(CanvasMetrics::kCanvasCreated); | 146 CanvasMetrics::CountCanvasContextUsage(CanvasMetrics::kCanvasCreated); |
| 149 UseCounter::Count(document, UseCounter::kHTMLCanvasElement); | 147 UseCounter::Count(document, UseCounter::kHTMLCanvasElement); |
| 150 } | 148 } |
| 151 | 149 |
| 152 DEFINE_NODE_FACTORY(HTMLCanvasElement) | 150 DEFINE_NODE_FACTORY(HTMLCanvasElement) |
| 153 | 151 |
| 154 HTMLCanvasElement::~HTMLCanvasElement() { | 152 HTMLCanvasElement::~HTMLCanvasElement() { |
| 155 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | 153 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
| 156 -externally_allocated_memory_); | 154 -externally_allocated_memory_); |
| 157 } | 155 } |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 dirty_rect_.Intersect(src_rect); | 420 dirty_rect_.Intersect(src_rect); |
| 423 LayoutRect mapped_dirty_rect(EnclosingIntRect( | 421 LayoutRect mapped_dirty_rect(EnclosingIntRect( |
| 424 MapRect(dirty_rect_, src_rect, FloatRect(ro->ContentBoxRect())))); | 422 MapRect(dirty_rect_, src_rect, FloatRect(ro->ContentBoxRect())))); |
| 425 // For querying PaintLayer::compositingState() | 423 // For querying PaintLayer::compositingState() |
| 426 // FIXME: is this invalidation using the correct compositing state? | 424 // FIXME: is this invalidation using the correct compositing state? |
| 427 DisableCompositingQueryAsserts disabler; | 425 DisableCompositingQueryAsserts disabler; |
| 428 ro->InvalidatePaintRectangle(mapped_dirty_rect); | 426 ro->InvalidatePaintRectangle(mapped_dirty_rect); |
| 429 } | 427 } |
| 430 dirty_rect_ = FloatRect(); | 428 dirty_rect_ = FloatRect(); |
| 431 | 429 |
| 432 num_frames_since_last_rendering_mode_switch_++; | |
| 433 if (RuntimeEnabledFeatures:: | |
| 434 enableCanvas2dDynamicRenderingModeSwitchingEnabled() && | |
| 435 !RuntimeEnabledFeatures::canvas2dFixedRenderingModeEnabled()) { | |
| 436 if (Is2d() && GetImageBuffer() && GetImageBuffer()->IsAccelerated() && | |
| 437 num_frames_since_last_rendering_mode_switch_ >= | |
| 438 ExpensiveCanvasHeuristicParameters::kMinFramesBeforeSwitch && | |
| 439 !pending_rendering_mode_switch_) { | |
| 440 if (!context_->IsAccelerationOptimalForCanvasContent()) { | |
| 441 // The switch must be done asynchronously in order to avoid switching | |
| 442 // during the paint invalidation step. | |
| 443 Platform::Current()->CurrentThread()->GetWebTaskRunner()->PostTask( | |
| 444 BLINK_FROM_HERE, | |
| 445 WTF::Bind( | |
| 446 [](WeakPtr<ImageBuffer> buffer) { | |
| 447 if (buffer) { | |
| 448 buffer->DisableAcceleration(); | |
| 449 } | |
| 450 }, | |
| 451 image_buffer_->weak_ptr_factory_.CreateWeakPtr())); | |
| 452 num_frames_since_last_rendering_mode_switch_ = 0; | |
| 453 pending_rendering_mode_switch_ = true; | |
| 454 } | |
| 455 } | |
| 456 } | |
| 457 | |
| 458 if (pending_rendering_mode_switch_ && GetOrCreateImageBuffer() && | |
| 459 !GetOrCreateImageBuffer()->IsAccelerated()) { | |
| 460 pending_rendering_mode_switch_ = false; | |
| 461 } | |
| 462 | |
| 463 DCHECK(dirty_rect_.IsEmpty()); | 430 DCHECK(dirty_rect_.IsEmpty()); |
| 464 } | 431 } |
| 465 | 432 |
| 466 void HTMLCanvasElement::Reset() { | 433 void HTMLCanvasElement::Reset() { |
| 467 if (ignore_reset_) | 434 if (ignore_reset_) |
| 468 return; | 435 return; |
| 469 | 436 |
| 470 dirty_rect_ = FloatRect(); | 437 dirty_rect_ = FloatRect(); |
| 471 | 438 |
| 472 bool ok; | 439 bool ok; |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 851 #if 0 | 818 #if 0 |
| 852 // TODO(junov): re-enable this code once we solve the problem of recordi
ng | 819 // TODO(junov): re-enable this code once we solve the problem of recordi
ng |
| 853 // GPU-backed images to a PaintRecord for cross-context rendering crbug.
com/490328 | 820 // GPU-backed images to a PaintRecord for cross-context rendering crbug.
com/490328 |
| 854 | 821 |
| 855 // If the compositor provides GPU acceleration to display list canvases,
we | 822 // If the compositor provides GPU acceleration to display list canvases,
we |
| 856 // prefer that over direct acceleration. | 823 // prefer that over direct acceleration. |
| 857 if (document().viewportDescription().matchesHeuristicsForGpuRasterizatio
n()) | 824 if (document().viewportDescription().matchesHeuristicsForGpuRasterizatio
n()) |
| 858 return false; | 825 return false; |
| 859 #endif | 826 #endif |
| 860 // If the GPU resources would be very expensive, prefer a display list. | 827 // If the GPU resources would be very expensive, prefer a display list. |
| 861 if (canvas_pixel_count > ExpensiveCanvasHeuristicParameters:: | 828 if (canvas_pixel_count > |
| 862 kPreferDisplayListOverGpuSizeThreshold) | 829 CanvasHeuristicParameters::kPreferDisplayListOverGpuSizeThreshold) |
| 863 return false; | 830 return false; |
| 864 } | 831 } |
| 865 | 832 |
| 866 // Do not use acceleration for small canvas. | 833 // Do not use acceleration for small canvas. |
| 867 if (criteria != kIgnoreResourceLimitCriteria) { | 834 if (criteria != kIgnoreResourceLimitCriteria) { |
| 868 Settings* settings = GetDocument().GetSettings(); | 835 Settings* settings = GetDocument().GetSettings(); |
| 869 if (!settings || | 836 if (!settings || |
| 870 canvas_pixel_count < settings->GetMinimumAccelerated2dCanvasSize()) | 837 canvas_pixel_count < settings->GetMinimumAccelerated2dCanvasSize()) |
| 871 return false; | 838 return false; |
| 872 | 839 |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1218 context_->StyleDidChange(old_style, new_style); | 1185 context_->StyleDidChange(old_style, new_style); |
| 1219 } | 1186 } |
| 1220 | 1187 |
| 1221 void HTMLCanvasElement::DidMoveToNewDocument(Document& old_document) { | 1188 void HTMLCanvasElement::DidMoveToNewDocument(Document& old_document) { |
| 1222 ContextLifecycleObserver::SetContext(&GetDocument()); | 1189 ContextLifecycleObserver::SetContext(&GetDocument()); |
| 1223 PageVisibilityObserver::SetContext(GetDocument().GetPage()); | 1190 PageVisibilityObserver::SetContext(GetDocument().GetPage()); |
| 1224 HTMLElement::DidMoveToNewDocument(old_document); | 1191 HTMLElement::DidMoveToNewDocument(old_document); |
| 1225 } | 1192 } |
| 1226 | 1193 |
| 1227 void HTMLCanvasElement::WillDrawImageTo2DContext(CanvasImageSource* source) { | 1194 void HTMLCanvasElement::WillDrawImageTo2DContext(CanvasImageSource* source) { |
| 1228 if (ExpensiveCanvasHeuristicParameters::kEnableAccelerationToAvoidReadbacks && | 1195 if (CanvasHeuristicParameters::kEnableAccelerationToAvoidReadbacks && |
| 1229 SharedGpuContext::AllowSoftwareToAcceleratedCanvasUpgrade() && | 1196 SharedGpuContext::AllowSoftwareToAcceleratedCanvasUpgrade() && |
| 1230 source->IsAccelerated() && !GetOrCreateImageBuffer()->IsAccelerated() && | 1197 source->IsAccelerated() && !GetOrCreateImageBuffer()->IsAccelerated() && |
| 1231 ShouldAccelerate(kIgnoreResourceLimitCriteria)) { | 1198 ShouldAccelerate(kIgnoreResourceLimitCriteria)) { |
| 1232 OpacityMode opacity_mode = | 1199 OpacityMode opacity_mode = |
| 1233 context_->CreationAttributes().alpha() ? kNonOpaque : kOpaque; | 1200 context_->CreationAttributes().alpha() ? kNonOpaque : kOpaque; |
| 1234 int msaa_sample_count = 0; | 1201 int msaa_sample_count = 0; |
| 1235 std::unique_ptr<ImageBufferSurface> surface = | 1202 std::unique_ptr<ImageBufferSurface> surface = |
| 1236 CreateAcceleratedImageBufferSurface(opacity_mode, &msaa_sample_count); | 1203 CreateAcceleratedImageBufferSurface(opacity_mode, &msaa_sample_count); |
| 1237 if (surface) { | 1204 if (surface) { |
| 1238 GetOrCreateImageBuffer()->SetSurface(std::move(surface)); | 1205 GetOrCreateImageBuffer()->SetSurface(std::move(surface)); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1280 // Because WebGL sources always require making a copy of the back buffer, we | 1247 // Because WebGL sources always require making a copy of the back buffer, we |
| 1281 // use paintRenderingResultsToCanvas instead of getImage in order to keep a | 1248 // use paintRenderingResultsToCanvas instead of getImage in order to keep a |
| 1282 // cached copy of the backing in the canvas's ImageBuffer. | 1249 // cached copy of the backing in the canvas's ImageBuffer. |
| 1283 RenderingContext()->PaintRenderingResultsToCanvas(kBackBuffer); | 1250 RenderingContext()->PaintRenderingResultsToCanvas(kBackBuffer); |
| 1284 if (GetImageBuffer()) { | 1251 if (GetImageBuffer()) { |
| 1285 sk_image = GetImageBuffer()->NewSkImageSnapshot(hint, reason); | 1252 sk_image = GetImageBuffer()->NewSkImageSnapshot(hint, reason); |
| 1286 } else { | 1253 } else { |
| 1287 sk_image = CreateTransparentSkImage(Size()); | 1254 sk_image = CreateTransparentSkImage(Size()); |
| 1288 } | 1255 } |
| 1289 } else { | 1256 } else { |
| 1290 if (ExpensiveCanvasHeuristicParameters:: | 1257 if (CanvasHeuristicParameters::kDisableAccelerationToAvoidReadbacks && |
| 1291 kDisableAccelerationToAvoidReadbacks && | |
| 1292 !RuntimeEnabledFeatures::canvas2dFixedRenderingModeEnabled() && | 1258 !RuntimeEnabledFeatures::canvas2dFixedRenderingModeEnabled() && |
| 1293 hint == kPreferNoAcceleration && GetImageBuffer() && | 1259 hint == kPreferNoAcceleration && GetImageBuffer() && |
| 1294 GetImageBuffer()->IsAccelerated()) { | 1260 GetImageBuffer()->IsAccelerated()) { |
| 1295 GetImageBuffer()->DisableAcceleration(); | 1261 GetImageBuffer()->DisableAcceleration(); |
| 1296 } | 1262 } |
| 1297 RefPtr<Image> image = RenderingContext()->GetImage(hint, reason); | 1263 RefPtr<Image> image = RenderingContext()->GetImage(hint, reason); |
| 1298 if (image) { | 1264 if (image) { |
| 1299 sk_image = image->ImageForCurrentFrame(); | 1265 sk_image = image->ImageForCurrentFrame(); |
| 1300 } else { | 1266 } else { |
| 1301 sk_image = CreateTransparentSkImage(Size()); | 1267 sk_image = CreateTransparentSkImage(Size()); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1464 // Creates a placeholder layer first before Surface is created. | 1430 // Creates a placeholder layer first before Surface is created. |
| 1465 surface_layer_bridge_->CreateSolidColorLayer(); | 1431 surface_layer_bridge_->CreateSolidColorLayer(); |
| 1466 } | 1432 } |
| 1467 } | 1433 } |
| 1468 | 1434 |
| 1469 void HTMLCanvasElement::OnWebLayerReplaced() { | 1435 void HTMLCanvasElement::OnWebLayerReplaced() { |
| 1470 SetNeedsCompositingUpdate(); | 1436 SetNeedsCompositingUpdate(); |
| 1471 } | 1437 } |
| 1472 | 1438 |
| 1473 } // namespace blink | 1439 } // namespace blink |
| OLD | NEW |