[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() |
Khushal | 73760a3 | 2018-02-16 19:29:51 | [diff] [blame] | 52 | .set_id(cc::PaintImage::GetNextId()) |
| 53 | .set_image(SkImage::MakeFromBitmap(*sad_bitmap), |
| 54 | cc::PaintImage::GetNextContentId()) |
Adrienne Walker | 3178dcb | 2018-02-13 23:49:40 | [diff] [blame] | 55 | .TakePaintImage(), |
| 56 | SkMatrix::I(), false); |
lfg | cccffbe | 2016-03-10 00:05:46 | [diff] [blame] | 57 | sad_layer->SetBounds( |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 58 | gfx::Size(sad_bitmap->width() * device_scale_factor, |
| 59 | sad_bitmap->height() * device_scale_factor)); |
| 60 | sad_layer->SetPosition( |
| 61 | gfx::PointF((frame_size_in_dip.width() - sad_bitmap->width()) / 2, |
| 62 | (frame_size_in_dip.height() - sad_bitmap->height()) / 2)); |
lfg | cccffbe | 2016-03-10 00:05:46 | [diff] [blame] | 63 | sad_layer->SetIsDrawable(true); |
| 64 | |
| 65 | crashed_layer->AddChild(sad_layer); |
| 66 | } |
| 67 | } |
| 68 | |
danakj | e1b6426 | 2018-05-05 01:35:54 | [diff] [blame] | 69 | bool prevent_contents_opaque_changes = false; |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 70 | child_frame_compositor_->SetLayer( |
danakj | e1b6426 | 2018-05-05 01:35:54 | [diff] [blame] | 71 | std::make_unique<cc_blink::WebLayerImpl>(crashed_layer), |
| 72 | prevent_contents_opaque_changes); |
[email protected] | f49722f | 2014-01-30 17:54:50 | [diff] [blame] | 73 | } |
| 74 | |
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 75 | void ChildFrameCompositingHelper::SetPrimarySurfaceId( |
| 76 | const viz::SurfaceId& surface_id, |
Vladimir Levin | 6220cb6 | 2018-04-05 02:44:38 | [diff] [blame] | 77 | const gfx::Size& frame_size_in_dip, |
| 78 | const cc::DeadlinePolicy& deadline) { |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 79 | if (primary_surface_id_ == surface_id) |
Fady Samuel | 64eb4b4f | 2017-11-09 00:31:52 | [diff] [blame] | 80 | return; |
| 81 | |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 82 | primary_surface_id_ = surface_id; |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 83 | |
kylechar | ee539faa0 | 2018-01-22 17:00:55 | [diff] [blame] | 84 | surface_layer_ = cc::SurfaceLayer::Create(); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 85 | surface_layer_->SetMasksToBounds(true); |
sunxd | 26e9bae | 2018-03-22 19:32:33 | [diff] [blame] | 86 | surface_layer_->SetHitTestable(true); |
Fady Samuel | 02de6ee | 2018-01-05 21:20:21 | [diff] [blame] | 87 | surface_layer_->SetBackgroundColor(SK_ColorTRANSPARENT); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 88 | |
Vladimir Levin | 6220cb6 | 2018-04-05 02:44:38 | [diff] [blame] | 89 | surface_layer_->SetPrimarySurfaceId(surface_id, deadline); |
Fady Samuel | b47442d | 2017-10-13 04:17:34 | [diff] [blame] | 90 | surface_layer_->SetFallbackSurfaceId(fallback_surface_id_); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 91 | |
danakj | e1b6426 | 2018-05-05 01:35:54 | [diff] [blame] | 92 | auto layer_owned = std::make_unique<cc_blink::WebLayerImpl>(surface_layer_); |
| 93 | auto* layer = layer_owned.get(); |
| 94 | |
| 95 | // TODO(lfg): Investigate if it's possible to propagate the information |
| 96 | // about the child surface's opacity. https://crbug.com/629851. |
| 97 | bool prevent_contents_opaque_changes = true; |
| 98 | child_frame_compositor_->SetLayer(std::move(layer_owned), |
| 99 | prevent_contents_opaque_changes); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 100 | |
| 101 | UpdateVisibility(true); |
| 102 | |
danakj | e1b6426 | 2018-05-05 01:35:54 | [diff] [blame] | 103 | layer->SetBounds(frame_size_in_dip); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 104 | } |
| 105 | |
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 106 | void ChildFrameCompositingHelper::SetFallbackSurfaceId( |
| 107 | const viz::SurfaceId& surface_id, |
kylechar | ee539faa0 | 2018-01-22 17:00:55 | [diff] [blame] | 108 | const gfx::Size& frame_size_in_dip) { |
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 109 | if (fallback_surface_id_ == surface_id) |
Fady Samuel | 64eb4b4f | 2017-11-09 00:31:52 | [diff] [blame] | 110 | return; |
| 111 | |
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 112 | fallback_surface_id_ = surface_id; |
kenrb | 6ef29da | 2017-06-09 22:16:21 | [diff] [blame] | 113 | |
Fady Samuel | d10aadd8 | 2017-11-03 18:23:57 | [diff] [blame] | 114 | if (!surface_layer_) { |
Vladimir Levin | 6220cb6 | 2018-04-05 02:44:38 | [diff] [blame] | 115 | SetPrimarySurfaceId(surface_id, frame_size_in_dip, |
| 116 | cc::DeadlinePolicy::UseDefaultDeadline()); |
Fady Samuel | d10aadd8 | 2017-11-03 18:23:57 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 120 | surface_layer_->SetFallbackSurfaceId(surface_id); |
kenrb | fc7c02c9 | 2015-05-29 22:20:58 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 123 | void ChildFrameCompositingHelper::UpdateVisibility(bool visible) { |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 124 | blink::WebLayer* web_layer = child_frame_compositor_->GetLayer(); |
| 125 | if (web_layer) |
| 126 | web_layer->SetDrawsContent(visible); |
[email protected] | 69b7912 | 2013-02-14 19:16:45 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 0f21e858 | 2013-01-11 11:06:56 | [diff] [blame] | 129 | } // namespace content |