[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 0f21e858 | 2013-01-11 11:06:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 5 | #include "content/renderer/child_frame_compositing_helper.h" |
[email protected] | 0f21e858 | 2013-01-11 11:06:56 | [diff] [blame] | 6 | |
dcheng | 07945f63 | 2015-12-26 07:59:32 | [diff] [blame] | 7 | #include <utility> |
8 | |||||
kylechar | 5e03700 | 2018-01-05 15:30:19 | [diff] [blame] | 9 | #include "build/build_config.h" |
[email protected] | 3856462 | 2014-08-19 02:47:18 | [diff] [blame] | 10 | #include "cc/blink/web_layer_impl.h" |
lfg | cccffbe | 2016-03-10 00:05:46 | [diff] [blame] | 11 | #include "cc/layers/picture_image_layer.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 12 | #include "cc/layers/solid_color_layer.h" |
kenrb | fc7c02c9 | 2015-05-29 22:20:58 | [diff] [blame] | 13 | #include "cc/layers/surface_layer.h" |
vmpstr | 55c7657ca | 2017-04-29 00:46:48 | [diff] [blame] | 14 | #include "cc/paint/paint_image.h" |
Vladimir Levin | 988d024 | 2017-08-08 00:02:57 | [diff] [blame] | 15 | #include "cc/paint/paint_image_builder.h" |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 16 | #include "content/renderer/child_frame_compositor.h" |
[email protected] | 73405fb | 2013-12-11 04:59:37 | [diff] [blame] | 17 | #include "skia/ext/image_operations.h" |
lfg | cccffbe | 2016-03-10 00:05:46 | [diff] [blame] | 18 | #include "third_party/skia/include/core/SkBitmap.h" |
19 | #include "third_party/skia/include/core/SkImage.h" | ||||
20 | #include "ui/gfx/geometry/point_f.h" | ||||
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 21 | #include "ui/gfx/geometry/size.h" |
[email protected] | 73405fb | 2013-12-11 04:59:37 | [diff] [blame] | 22 | #include "ui/gfx/skia_util.h" |
[email protected] | 0f21e858 | 2013-01-11 11:06:56 | [diff] [blame] | 23 | |
24 | namespace content { | ||||
25 | |||||
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 26 | ChildFrameCompositingHelper::ChildFrameCompositingHelper( |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 27 | ChildFrameCompositor* child_frame_compositor) |
28 | : child_frame_compositor_(child_frame_compositor) { | ||||
29 | DCHECK(child_frame_compositor_); | ||||
lfg | a7d368e | 2015-02-03 23:01:37 | [diff] [blame] | 30 | } |
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 31 | |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 32 | ChildFrameCompositingHelper::~ChildFrameCompositingHelper() = default; |
[email protected] | 94224ba6 | 2014-02-04 00:25:24 | [diff] [blame] | 33 | |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 34 | void ChildFrameCompositingHelper::ChildFrameGone( |
35 | const gfx::Size& frame_size_in_dip, | ||||
36 | float device_scale_factor) { | ||||
37 | primary_surface_id_ = viz::SurfaceId(); | ||||
38 | fallback_surface_id_ = viz::SurfaceId(); | ||||
[email protected] | 94224ba6 | 2014-02-04 00:25:24 | [diff] [blame] | 39 | |
lfg | fd437a27 | 2015-11-24 21:24:53 | [diff] [blame] | 40 | scoped_refptr<cc::SolidColorLayer> crashed_layer = |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 41 | cc::SolidColorLayer::Create(); |
lfg | fd437a27 | 2015-11-24 21:24:53 | [diff] [blame] | 42 | crashed_layer->SetMasksToBounds(true); |
lfg | cccffbe | 2016-03-10 00:05:46 | [diff] [blame] | 43 | crashed_layer->SetBackgroundColor(SK_ColorBLACK); |
44 | |||||
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 45 | if (child_frame_compositor_->GetLayer()) { |
46 | SkBitmap* sad_bitmap = child_frame_compositor_->GetSadPageBitmap(); | ||||
47 | if (sad_bitmap && frame_size_in_dip.width() > sad_bitmap->width() && | ||||
48 | frame_size_in_dip.height() > sad_bitmap->height()) { | ||||
lfg | cccffbe | 2016-03-10 00:05:46 | [diff] [blame] | 49 | scoped_refptr<cc::PictureImageLayer> sad_layer = |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 50 | cc::PictureImageLayer::Create(); |
Vladimir Levin | f67b0fc0 | 2017-10-06 01:48:11 | [diff] [blame] | 51 | sad_layer->SetImage(cc::PaintImageBuilder::WithDefault() |
Vladimir Levin | 988d024 | 2017-08-08 00:02:57 | [diff] [blame] | 52 | .set_id(cc::PaintImage::kNonLazyStableId) |
53 | .set_image(SkImage::MakeFromBitmap(*sad_bitmap)) | ||||
54 | .TakePaintImage()); | ||||
lfg | cccffbe | 2016-03-10 00:05:46 | [diff] [blame] | 55 | sad_layer->SetBounds( |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 56 | gfx::Size(sad_bitmap->width() * device_scale_factor, |
57 | sad_bitmap->height() * device_scale_factor)); | ||||
58 | sad_layer->SetPosition( | ||||
59 | gfx::PointF((frame_size_in_dip.width() - sad_bitmap->width()) / 2, | ||||
60 | (frame_size_in_dip.height() - sad_bitmap->height()) / 2)); | ||||
lfg | cccffbe | 2016-03-10 00:05:46 | [diff] [blame] | 61 | sad_layer->SetIsDrawable(true); |
62 | |||||
63 | crashed_layer->AddChild(sad_layer); | ||||
64 | } | ||||
65 | } | ||||
66 | |||||
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 67 | child_frame_compositor_->SetLayer( |
68 | std::make_unique<cc_blink::WebLayerImpl>(crashed_layer)); | ||||
[email protected] | f49722f | 2014-01-30 17:54:50 | [diff] [blame] | 69 | } |
70 | |||||
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 71 | void ChildFrameCompositingHelper::SetPrimarySurfaceId( |
72 | const viz::SurfaceId& surface_id, | ||||
73 | const gfx::Size& frame_size_in_dip) { | ||||
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 74 | if (primary_surface_id_ == surface_id) |
Fady Samuel | 64eb4b4f | 2017-11-09 00:31:52 | [diff] [blame] | 75 | return; |
76 | |||||
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 77 | primary_surface_id_ = surface_id; |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 78 | |
kylechar | ee539faa0 | 2018-01-22 17:00:55 | [diff] [blame] | 79 | surface_layer_ = cc::SurfaceLayer::Create(); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 80 | surface_layer_->SetMasksToBounds(true); |
Fady Samuel | 02de6ee | 2018-01-05 21:20:21 | [diff] [blame] | 81 | surface_layer_->SetBackgroundColor(SK_ColorTRANSPARENT); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 82 | |
Fady Samuel | b46e0ef | 2018-01-30 04:48:41 | [diff] [blame] | 83 | surface_layer_->SetPrimarySurfaceId(surface_id, |
84 | cc::DeadlinePolicy::UseDefaultDeadline()); | ||||
Fady Samuel | b47442d | 2017-10-13 04:17:34 | [diff] [blame] | 85 | surface_layer_->SetFallbackSurfaceId(fallback_surface_id_); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 86 | |
87 | std::unique_ptr<cc_blink::WebLayerImpl> layer( | ||||
88 | new cc_blink::WebLayerImpl(surface_layer_)); | ||||
89 | // TODO(lfg): Investigate if it's possible to propagate the information about | ||||
90 | // the child surface's opacity. https://crbug.com/629851. | ||||
91 | layer->SetOpaque(false); | ||||
92 | layer->SetContentsOpaqueIsFixed(true); | ||||
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 93 | child_frame_compositor_->SetLayer(std::move(layer)); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 94 | |
95 | UpdateVisibility(true); | ||||
96 | |||||
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 97 | static_cast<cc_blink::WebLayerImpl*>(child_frame_compositor_->GetLayer()) |
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 98 | ->layer() |
99 | ->SetBounds(frame_size_in_dip); | ||||
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 100 | } |
101 | |||||
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 102 | void ChildFrameCompositingHelper::SetFallbackSurfaceId( |
103 | const viz::SurfaceId& surface_id, | ||||
kylechar | ee539faa0 | 2018-01-22 17:00:55 | [diff] [blame] | 104 | const gfx::Size& frame_size_in_dip) { |
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 105 | if (fallback_surface_id_ == surface_id) |
Fady Samuel | 64eb4b4f | 2017-11-09 00:31:52 | [diff] [blame] | 106 | return; |
107 | |||||
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 108 | fallback_surface_id_ = surface_id; |
kenrb | 6ef29da | 2017-06-09 22:16:21 | [diff] [blame] | 109 | |
Fady Samuel | d10aadd8 | 2017-11-03 18:23:57 | [diff] [blame] | 110 | if (!surface_layer_) { |
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 111 | SetPrimarySurfaceId(surface_id, frame_size_in_dip); |
Fady Samuel | d10aadd8 | 2017-11-03 18:23:57 | [diff] [blame] | 112 | return; |
113 | } | ||||
114 | |||||
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 115 | surface_layer_->SetFallbackSurfaceId(surface_id); |
kenrb | fc7c02c9 | 2015-05-29 22:20:58 | [diff] [blame] | 116 | } |
117 | |||||
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 118 | void ChildFrameCompositingHelper::UpdateVisibility(bool visible) { |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 119 | blink::WebLayer* web_layer = child_frame_compositor_->GetLayer(); |
120 | if (web_layer) | ||||
121 | web_layer->SetDrawsContent(visible); | ||||
[email protected] | 69b7912 | 2013-02-14 19:16:45 | [diff] [blame] | 122 | } |
123 | |||||
[email protected] | 0f21e858 | 2013-01-11 11:06:56 | [diff] [blame] | 124 | } // namespace content |