blob: 9c016e3f824c6b1399cbaca69a6d436a610501f9 [file] [log] [blame]
[email protected]bffc8302014-01-23 20:52:161// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]0f21e8582013-01-11 11:06:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]bffc8302014-01-23 20:52:165#include "content/renderer/child_frame_compositing_helper.h"
[email protected]0f21e8582013-01-11 11:06:566
dcheng07945f632015-12-26 07:59:327#include <utility>
8
kylechar5e037002018-01-05 15:30:199#include "build/build_config.h"
Xianzhu Wang6fef65952019-01-07 23:39:5410#include "cc/layers/picture_layer.h"
kenrbfc7c02c92015-05-29 22:20:5811#include "cc/layers/surface_layer.h"
vmpstr55c7657ca2017-04-29 00:46:4812#include "cc/paint/paint_image.h"
Vladimir Levin988d0242017-08-08 00:02:5713#include "cc/paint/paint_image_builder.h"
Fady Samuel4e717f52018-02-07 05:51:5014#include "content/renderer/child_frame_compositor.h"
[email protected]73405fb2013-12-11 04:59:3715#include "skia/ext/image_operations.h"
lfgcccffbe2016-03-10 00:05:4616#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 Samuel4e717f52018-02-07 05:51:5019#include "ui/gfx/geometry/size.h"
[email protected]73405fb2013-12-11 04:59:3720#include "ui/gfx/skia_util.h"
[email protected]0f21e8582013-01-11 11:06:5621
22namespace content {
23
[email protected]bffc8302014-01-23 20:52:1624ChildFrameCompositingHelper::ChildFrameCompositingHelper(
Fady Samuel4e717f52018-02-07 05:51:5025 ChildFrameCompositor* child_frame_compositor)
26 : child_frame_compositor_(child_frame_compositor) {
27 DCHECK(child_frame_compositor_);
lfga7d368e2015-02-03 23:01:3728}
[email protected]bffc8302014-01-23 20:52:1629
Xianzhu Wang5a90bca42019-01-24 01:43:4830ChildFrameCompositingHelper::~ChildFrameCompositingHelper() {
31 if (crash_ui_layer_)
32 crash_ui_layer_->ClearClient();
33}
[email protected]94224ba62014-02-04 00:25:2434
Fady Samuel4e717f52018-02-07 05:51:5035void ChildFrameCompositingHelper::ChildFrameGone(
36 const gfx::Size& frame_size_in_dip,
37 float device_scale_factor) {
Saman Samic8fc00a2018-11-19 23:16:3538 surface_id_ = viz::SurfaceId();
Xianzhu Wang6fef65952019-01-07 23:39:5439 device_scale_factor_ = device_scale_factor;
[email protected]94224ba62014-02-04 00:25:2440
Xianzhu Wang5a90bca42019-01-24 01:43:4841 crash_ui_layer_ = cc::PictureLayer::Create(this);
42 crash_ui_layer_->SetMasksToBounds(true);
43 crash_ui_layer_->SetIsDrawable(true);
lfgcccffbe2016-03-10 00:05:4644
danakje1b64262018-05-05 01:35:5445 bool prevent_contents_opaque_changes = false;
Xianzhu Wang6fef65952019-01-07 23:39:5446 bool is_surface_layer = false;
Xianzhu Wang5a90bca42019-01-24 01:43:4847 child_frame_compositor_->SetLayer(
48 crash_ui_layer_, prevent_contents_opaque_changes, is_surface_layer);
[email protected]f49722f2014-01-30 17:54:5049}
50
Saman Sami98c524052018-10-26 17:34:5751void ChildFrameCompositingHelper::SetSurfaceId(
Fady Samuel1cc05222017-11-17 10:42:2652 const viz::SurfaceId& surface_id,
Vladimir Levin6220cb62018-04-05 02:44:3853 const gfx::Size& frame_size_in_dip,
54 const cc::DeadlinePolicy& deadline) {
Saman Samic8fc00a2018-11-19 23:16:3555 if (surface_id_ == surface_id)
Fady Samuel64eb4b4f2017-11-09 00:31:5256 return;
57
Saman Samic8fc00a2018-11-19 23:16:3558 surface_id_ = surface_id;
Fady Samuel509c2d52017-08-28 19:00:5859
kylecharee539faa02018-01-22 17:00:5560 surface_layer_ = cc::SurfaceLayer::Create();
Fady Samuel509c2d52017-08-28 19:00:5861 surface_layer_->SetMasksToBounds(true);
sunxd2869c922018-05-14 20:48:3262 surface_layer_->SetSurfaceHitTestable(true);
Fady Samuel02de6ee2018-01-05 21:20:2163 surface_layer_->SetBackgroundColor(SK_ColorTRANSPARENT);
Fady Samuel509c2d52017-08-28 19:00:5864
Saman Sami98c524052018-10-26 17:34:5765 surface_layer_->SetSurfaceId(surface_id, deadline);
Fady Samuel509c2d52017-08-28 19:00:5866
danakje1b64262018-05-05 01:35:5467 // 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;
danakj25f030112018-05-11 18:26:5470 child_frame_compositor_->SetLayer(surface_layer_,
Daniel Cheng02a0bc12018-09-19 00:42:0971 prevent_contents_opaque_changes,
72 true /* is_surface_layer */);
Fady Samuel509c2d52017-08-28 19:00:5873
74 UpdateVisibility(true);
75
danakj25f030112018-05-11 18:26:5476 surface_layer_->SetBounds(frame_size_in_dip);
Fady Samuel509c2d52017-08-28 19:00:5877}
78
[email protected]bffc8302014-01-23 20:52:1679void ChildFrameCompositingHelper::UpdateVisibility(bool visible) {
danakj6a062b112018-05-17 16:25:4580 cc::Layer* layer = child_frame_compositor_->GetLayer();
Xianda Sun83a15c832019-04-08 15:38:5181 if (layer) {
danakj6a062b112018-05-17 16:25:4582 layer->SetIsDrawable(visible);
Xianda Sun83a15c832019-04-08 15:38:5183 layer->SetHitTestable(visible);
84 }
[email protected]69b79122013-02-14 19:16:4585}
86
Xianzhu Wang6fef65952019-01-07 23:39:5487gfx::Rect ChildFrameCompositingHelper::PaintableRegion() {
Xianzhu Wang5a90bca42019-01-24 01:43:4888 DCHECK(crash_ui_layer_);
89 return gfx::Rect(crash_ui_layer_->bounds());
Xianzhu Wang6fef65952019-01-07 23:39:5490}
91
92scoped_refptr<cc::DisplayItemList>
93ChildFrameCompositingHelper::PaintContentsToDisplayList(
94 PaintingControlSetting) {
Xianzhu Wang5a90bca42019-01-24 01:43:4895 DCHECK(crash_ui_layer_);
96 auto layer_size = crash_ui_layer_->bounds();
Xianzhu Wang6fef65952019-01-07 23:39:5497 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
134bool ChildFrameCompositingHelper::FillsBoundsCompletely() const {
135 // Because we paint a full opaque gray background.
136 return true;
137}
138
139size_t ChildFrameCompositingHelper::GetApproximateUnsharedMemoryUsage() const {
140 return sizeof(*this);
141}
142
[email protected]0f21e8582013-01-11 11:06:56143} // namespace content