[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" |
Xianzhu Wang | 6fef6595 | 2019-01-07 23:39:54 | [diff] [blame] | 10 | #include "cc/layers/picture_layer.h" |
kenrb | fc7c02c9 | 2015-05-29 22:20:58 | [diff] [blame] | 11 | #include "cc/layers/surface_layer.h" |
vmpstr | 55c7657ca | 2017-04-29 00:46:48 | [diff] [blame] | 12 | #include "cc/paint/paint_image.h" |
Vladimir Levin | 988d024 | 2017-08-08 00:02:57 | [diff] [blame] | 13 | #include "cc/paint/paint_image_builder.h" |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 14 | #include "content/renderer/child_frame_compositor.h" |
[email protected] | 73405fb | 2013-12-11 04:59:37 | [diff] [blame] | 15 | #include "skia/ext/image_operations.h" |
lfg | cccffbe | 2016-03-10 00:05:46 | [diff] [blame] | 16 | #include "third_party/skia/include/core/SkBitmap.h" |
| 17 | #include "third_party/skia/include/core/SkImage.h" |
| 18 | #include "ui/gfx/geometry/point_f.h" |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 19 | #include "ui/gfx/geometry/size.h" |
[email protected] | 73405fb | 2013-12-11 04:59:37 | [diff] [blame] | 20 | #include "ui/gfx/skia_util.h" |
[email protected] | 0f21e858 | 2013-01-11 11:06:56 | [diff] [blame] | 21 | |
| 22 | namespace content { |
| 23 | |
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 24 | ChildFrameCompositingHelper::ChildFrameCompositingHelper( |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 25 | ChildFrameCompositor* child_frame_compositor) |
| 26 | : child_frame_compositor_(child_frame_compositor) { |
| 27 | DCHECK(child_frame_compositor_); |
lfg | a7d368e | 2015-02-03 23:01:37 | [diff] [blame] | 28 | } |
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 29 | |
Xianzhu Wang | 5a90bca4 | 2019-01-24 01:43:48 | [diff] [blame] | 30 | ChildFrameCompositingHelper::~ChildFrameCompositingHelper() { |
| 31 | if (crash_ui_layer_) |
| 32 | crash_ui_layer_->ClearClient(); |
| 33 | } |
[email protected] | 94224ba6 | 2014-02-04 00:25:24 | [diff] [blame] | 34 | |
Fady Samuel | 4e717f5 | 2018-02-07 05:51:50 | [diff] [blame] | 35 | void ChildFrameCompositingHelper::ChildFrameGone( |
| 36 | const gfx::Size& frame_size_in_dip, |
| 37 | float device_scale_factor) { |
Saman Sami | c8fc00a | 2018-11-19 23:16:35 | [diff] [blame] | 38 | surface_id_ = viz::SurfaceId(); |
Xianzhu Wang | 6fef6595 | 2019-01-07 23:39:54 | [diff] [blame] | 39 | device_scale_factor_ = device_scale_factor; |
[email protected] | 94224ba6 | 2014-02-04 00:25:24 | [diff] [blame] | 40 | |
Xianzhu Wang | 5a90bca4 | 2019-01-24 01:43:48 | [diff] [blame] | 41 | crash_ui_layer_ = cc::PictureLayer::Create(this); |
| 42 | crash_ui_layer_->SetMasksToBounds(true); |
| 43 | crash_ui_layer_->SetIsDrawable(true); |
lfg | cccffbe | 2016-03-10 00:05:46 | [diff] [blame] | 44 | |
danakj | e1b6426 | 2018-05-05 01:35:54 | [diff] [blame] | 45 | bool prevent_contents_opaque_changes = false; |
Xianzhu Wang | 6fef6595 | 2019-01-07 23:39:54 | [diff] [blame] | 46 | bool is_surface_layer = false; |
Xianzhu Wang | 5a90bca4 | 2019-01-24 01:43:48 | [diff] [blame] | 47 | child_frame_compositor_->SetLayer( |
| 48 | crash_ui_layer_, prevent_contents_opaque_changes, is_surface_layer); |
[email protected] | f49722f | 2014-01-30 17:54:50 | [diff] [blame] | 49 | } |
| 50 | |
Saman Sami | 98c52405 | 2018-10-26 17:34:57 | [diff] [blame] | 51 | void ChildFrameCompositingHelper::SetSurfaceId( |
Fady Samuel | 1cc0522 | 2017-11-17 10:42:26 | [diff] [blame] | 52 | const viz::SurfaceId& surface_id, |
Vladimir Levin | 6220cb6 | 2018-04-05 02:44:38 | [diff] [blame] | 53 | const gfx::Size& frame_size_in_dip, |
| 54 | const cc::DeadlinePolicy& deadline) { |
Saman Sami | c8fc00a | 2018-11-19 23:16:35 | [diff] [blame] | 55 | if (surface_id_ == surface_id) |
Fady Samuel | 64eb4b4f | 2017-11-09 00:31:52 | [diff] [blame] | 56 | return; |
| 57 | |
Saman Sami | c8fc00a | 2018-11-19 23:16:35 | [diff] [blame] | 58 | surface_id_ = surface_id; |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 59 | |
kylechar | ee539faa0 | 2018-01-22 17:00:55 | [diff] [blame] | 60 | surface_layer_ = cc::SurfaceLayer::Create(); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 61 | surface_layer_->SetMasksToBounds(true); |
sunxd | 2869c92 | 2018-05-14 20:48:32 | [diff] [blame] | 62 | surface_layer_->SetSurfaceHitTestable(true); |
Fady Samuel | 02de6ee | 2018-01-05 21:20:21 | [diff] [blame] | 63 | surface_layer_->SetBackgroundColor(SK_ColorTRANSPARENT); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 64 | |
Saman Sami | 98c52405 | 2018-10-26 17:34:57 | [diff] [blame] | 65 | surface_layer_->SetSurfaceId(surface_id, deadline); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 66 | |
danakj | e1b6426 | 2018-05-05 01:35:54 | [diff] [blame] | 67 | // TODO(lfg): Investigate if it's possible to propagate the information |
| 68 | // about the child surface's opacity. https://crbug.com/629851. |
| 69 | bool prevent_contents_opaque_changes = true; |
danakj | 25f03011 | 2018-05-11 18:26:54 | [diff] [blame] | 70 | child_frame_compositor_->SetLayer(surface_layer_, |
Daniel Cheng | 02a0bc1 | 2018-09-19 00:42:09 | [diff] [blame] | 71 | prevent_contents_opaque_changes, |
| 72 | true /* is_surface_layer */); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 73 | |
| 74 | UpdateVisibility(true); |
| 75 | |
danakj | 25f03011 | 2018-05-11 18:26:54 | [diff] [blame] | 76 | surface_layer_->SetBounds(frame_size_in_dip); |
Fady Samuel | 509c2d5 | 2017-08-28 19:00:58 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 79 | void ChildFrameCompositingHelper::UpdateVisibility(bool visible) { |
danakj | 6a062b11 | 2018-05-17 16:25:45 | [diff] [blame] | 80 | cc::Layer* layer = child_frame_compositor_->GetLayer(); |
Xianda Sun | 83a15c83 | 2019-04-08 15:38:51 | [diff] [blame] | 81 | if (layer) { |
danakj | 6a062b11 | 2018-05-17 16:25:45 | [diff] [blame] | 82 | layer->SetIsDrawable(visible); |
Xianda Sun | 83a15c83 | 2019-04-08 15:38:51 | [diff] [blame] | 83 | layer->SetHitTestable(visible); |
| 84 | } |
[email protected] | 69b7912 | 2013-02-14 19:16:45 | [diff] [blame] | 85 | } |
| 86 | |
Xianzhu Wang | 6fef6595 | 2019-01-07 23:39:54 | [diff] [blame] | 87 | gfx::Rect ChildFrameCompositingHelper::PaintableRegion() { |
Xianzhu Wang | 5a90bca4 | 2019-01-24 01:43:48 | [diff] [blame] | 88 | DCHECK(crash_ui_layer_); |
| 89 | return gfx::Rect(crash_ui_layer_->bounds()); |
Xianzhu Wang | 6fef6595 | 2019-01-07 23:39:54 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | scoped_refptr<cc::DisplayItemList> |
| 93 | ChildFrameCompositingHelper::PaintContentsToDisplayList( |
| 94 | PaintingControlSetting) { |
Xianzhu Wang | 5a90bca4 | 2019-01-24 01:43:48 | [diff] [blame] | 95 | DCHECK(crash_ui_layer_); |
| 96 | auto layer_size = crash_ui_layer_->bounds(); |
Xianzhu Wang | 6fef6595 | 2019-01-07 23:39:54 | [diff] [blame] | 97 | auto display_list = base::MakeRefCounted<cc::DisplayItemList>(); |
| 98 | display_list->StartPaint(); |
| 99 | display_list->push<cc::DrawColorOp>(SK_ColorGRAY, SkBlendMode::kSrc); |
| 100 | |
| 101 | SkBitmap* sad_bitmap = child_frame_compositor_->GetSadPageBitmap(); |
| 102 | if (sad_bitmap) { |
| 103 | int paint_width = sad_bitmap->width() * device_scale_factor_; |
| 104 | int paint_height = sad_bitmap->height() * device_scale_factor_; |
| 105 | if (layer_size.width() >= paint_width && |
| 106 | layer_size.height() >= paint_height) { |
| 107 | int x = (layer_size.width() - paint_width) / 2; |
| 108 | int y = (layer_size.height() - paint_height) / 2; |
| 109 | if (device_scale_factor_ != 1.f) { |
| 110 | display_list->push<cc::SaveOp>(); |
| 111 | display_list->push<cc::TranslateOp>(x, y); |
| 112 | display_list->push<cc::ScaleOp>(device_scale_factor_, |
| 113 | device_scale_factor_); |
| 114 | x = 0; |
| 115 | y = 0; |
| 116 | } |
| 117 | |
| 118 | auto image = cc::PaintImageBuilder::WithDefault() |
| 119 | .set_id(cc::PaintImage::GetNextId()) |
| 120 | .set_image(SkImage::MakeFromBitmap(*sad_bitmap), |
| 121 | cc::PaintImage::GetNextContentId()) |
| 122 | .TakePaintImage(); |
| 123 | display_list->push<cc::DrawImageOp>(image, x, y, nullptr); |
| 124 | |
| 125 | if (device_scale_factor_ != 1.f) |
| 126 | display_list->push<cc::RestoreOp>(); |
| 127 | } |
| 128 | } |
| 129 | display_list->EndPaintOfUnpaired(gfx::Rect(layer_size)); |
| 130 | display_list->Finalize(); |
| 131 | return display_list; |
| 132 | } |
| 133 | |
| 134 | bool ChildFrameCompositingHelper::FillsBoundsCompletely() const { |
| 135 | // Because we paint a full opaque gray background. |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | size_t ChildFrameCompositingHelper::GetApproximateUnsharedMemoryUsage() const { |
| 140 | return sizeof(*this); |
| 141 | } |
| 142 | |
[email protected] | 0f21e858 | 2013-01-11 11:06:56 | [diff] [blame] | 143 | } // namespace content |