1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/layers/surface_layer_impl.h"
#include <stdint.h>
#include <algorithm>
#include <utility>
#include "base/memory/ptr_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/trace_event/traced_value.h"
#include "cc/debug/debug_colors.h"
#include "cc/layers/append_quads_data.h"
#include "cc/trees/layer_tree_impl.h"
#include "cc/trees/occlusion.h"
#include "components/viz/common/quads/solid_color_draw_quad.h"
#include "components/viz/common/quads/surface_draw_quad.h"
#include "ui/gfx/geometry/vector2d_conversions.h"
namespace cc {
// static
std::unique_ptr<SurfaceLayerImpl> SurfaceLayerImpl::Create(
LayerTreeImpl* tree_impl,
int id,
UpdateSubmissionStateCB update_submission_state_callback) {
return base::WrapUnique(new SurfaceLayerImpl(
tree_impl, id, std::move(update_submission_state_callback)));
}
// static
std::unique_ptr<SurfaceLayerImpl> SurfaceLayerImpl::Create(
LayerTreeImpl* tree_impl,
int id) {
return base::WrapUnique(new SurfaceLayerImpl(
tree_impl, id, base::BindRepeating([](bool, base::WaitableEvent* event) {
if (event)
event->Signal();
})));
}
SurfaceLayerImpl::SurfaceLayerImpl(
LayerTreeImpl* tree_impl,
int id,
UpdateSubmissionStateCB update_submission_state_callback)
: LayerImpl(tree_impl, id),
update_submission_state_callback_(
std::move(update_submission_state_callback)) {}
SurfaceLayerImpl::~SurfaceLayerImpl() {
// Do not call `update_submission_state_callback_` here. There is only very
// loose synchronization between when a layer gets a new impl layer and when
// the old layer is destroyed. For example, when a layer is moved to a new
// tree, the old tree's impl layer might be destroyed after drawing has
// started in the new tree with a new impl layer. In that case, we'd be
// clobbering the visibility state. Instead, trust that SurfaceLayer has done
// the right thing already.
}
mojom::LayerType SurfaceLayerImpl::GetLayerType() const {
return mojom::LayerType::kSurface;
}
std::unique_ptr<LayerImpl> SurfaceLayerImpl::CreateLayerImpl(
LayerTreeImpl* tree_impl) const {
return SurfaceLayerImpl::Create(tree_impl, id(),
std::move(update_submission_state_callback_));
}
void SurfaceLayerImpl::SetRange(const viz::SurfaceRange& surface_range,
std::optional<uint32_t> deadline_in_frames) {
if (surface_range_ == surface_range &&
deadline_in_frames_ == deadline_in_frames) {
return;
}
if (surface_range_.end() != surface_range.end() &&
surface_range.end().local_surface_id().is_valid()) {
TRACE_EVENT_WITH_FLOW2(
TRACE_DISABLED_BY_DEFAULT("viz.surface_id_flow"),
"LocalSurfaceId.Embed.Flow",
TRACE_ID_GLOBAL(
surface_range.end().local_surface_id().embed_trace_id()),
TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT, "step",
"ImplSetSurfaceId", "surface_id", surface_range.end().ToString());
}
surface_range_ = surface_range;
deadline_in_frames_ = deadline_in_frames;
NoteLayerPropertyChanged();
}
void SurfaceLayerImpl::SetStretchContentToFillBounds(bool stretch_content) {
if (stretch_content_to_fill_bounds_ == stretch_content)
return;
stretch_content_to_fill_bounds_ = stretch_content;
NoteLayerPropertyChanged();
}
void SurfaceLayerImpl::SetSurfaceHitTestable(bool surface_hit_testable) {
if (surface_hit_testable_ == surface_hit_testable)
return;
surface_hit_testable_ = surface_hit_testable;
NoteLayerPropertyChanged();
}
void SurfaceLayerImpl::SetHasPointerEventsNone(bool has_pointer_events_none) {
if (has_pointer_events_none_ == has_pointer_events_none)
return;
has_pointer_events_none_ = has_pointer_events_none;
NoteLayerPropertyChanged();
}
void SurfaceLayerImpl::SetIsReflection(bool is_reflection) {
if (is_reflection_ == is_reflection)
return;
is_reflection_ = is_reflection;
NoteLayerPropertyChanged();
}
void SurfaceLayerImpl::SetOverrideChildPaintFlags(
bool override_child_paint_flags) {
if (override_child_paint_flags_ == override_child_paint_flags) {
return;
}
override_child_paint_flags_ = override_child_paint_flags;
NoteLayerPropertyChanged();
}
void SurfaceLayerImpl::ResetStateForUpdateSubmissionStateCallback() {
will_draw_needs_reset_ = true;
NoteLayerPropertyChanged();
}
void SurfaceLayerImpl::PushPropertiesTo(LayerImpl* layer) {
LayerImpl::PushPropertiesTo(layer);
SurfaceLayerImpl* layer_impl = static_cast<SurfaceLayerImpl*>(layer);
layer_impl->SetRange(surface_range_, std::move(deadline_in_frames_));
// Unless the client explicitly specifies otherwise, don't block on
// |surface_range_| more than once.
deadline_in_frames_ = 0u;
layer_impl->SetStretchContentToFillBounds(stretch_content_to_fill_bounds_);
layer_impl->SetSurfaceHitTestable(surface_hit_testable_);
layer_impl->SetHasPointerEventsNone(has_pointer_events_none_);
layer_impl->SetIsReflection(is_reflection_);
layer_impl->SetOverrideChildPaintFlags(override_child_paint_flags_);
if (layer_impl->IsActive() && will_draw_needs_reset_) {
layer_impl->will_draw_ = false;
will_draw_needs_reset_ = false;
}
}
bool SurfaceLayerImpl::WillDraw(
DrawMode draw_mode,
viz::ClientResourceProvider* resource_provider) {
bool will_draw = LayerImpl::WillDraw(draw_mode, resource_provider);
// If we have a change in WillDraw (meaning that visibility has changed), we
// want to inform the VideoFrameSubmitter to start or stop submitting
// compositor frames.
if (will_draw_ != will_draw) {
will_draw_ = will_draw;
if (update_submission_state_callback_) {
// If we're in synchronous composite mode, ensure that we finish running
// the update submission state callback. This is important to avoid race
// conditions in web_tests which results from a thread hop that happens in
// the callback.
if (layer_tree_impl()->IsInSynchronousComposite()) {
base::WaitableEvent event;
update_submission_state_callback_.Run(will_draw, &event);
event.Wait();
} else {
update_submission_state_callback_.Run(will_draw, nullptr);
}
}
}
return will_draw;
}
void SurfaceLayerImpl::AppendQuads(const AppendQuadsContext& context,
viz::CompositorRenderPass* render_pass,
AppendQuadsData* append_quads_data) {
AppendRainbowDebugBorder(render_pass);
float device_scale_factor = layer_tree_impl()->device_scale_factor();
gfx::Rect quad_rect(gfx::ScaleToEnclosingRect(
gfx::Rect(bounds()), device_scale_factor, device_scale_factor));
gfx::Rect visible_quad_rect =
draw_properties().occlusion_in_content_space.GetUnoccludedContentRect(
gfx::Rect(bounds()));
visible_quad_rect = gfx::ScaleToEnclosingRect(
visible_quad_rect, device_scale_factor, device_scale_factor);
visible_quad_rect = gfx::IntersectRects(quad_rect, visible_quad_rect);
if (visible_quad_rect.IsEmpty())
return;
viz::SharedQuadState* shared_quad_state =
render_pass->CreateAndAppendSharedQuadState();
PopulateScaledSharedQuadState(shared_quad_state, device_scale_factor,
contents_opaque());
if (surface_range_.IsValid()) {
auto* quad = render_pass->CreateAndAppendDrawQuad<viz::SurfaceDrawQuad>();
quad->SetNew(shared_quad_state, quad_rect, visible_quad_rect,
surface_range_, background_color(),
stretch_content_to_fill_bounds_);
quad->is_reflection = is_reflection_;
if (override_child_paint_flags()) {
quad->override_child_filter_quality = GetFilterQuality();
quad->override_child_dynamic_range_limit = GetDynamicRangeLimit();
}
// Add the primary surface ID as a dependency.
append_quads_data->activation_dependencies.push_back(surface_range_.end());
if (deadline_in_frames_) {
if (!append_quads_data->deadline_in_frames)
append_quads_data->deadline_in_frames = 0u;
append_quads_data->deadline_in_frames = std::max(
*append_quads_data->deadline_in_frames, *deadline_in_frames_);
} else {
append_quads_data->use_default_lower_bound_deadline = true;
}
} else {
auto* quad =
render_pass->CreateAndAppendDrawQuad<viz::SolidColorDrawQuad>();
quad->SetNew(shared_quad_state, quad_rect, visible_quad_rect,
background_color(), false /* force_anti_aliasing_off */);
}
// Unless the client explicitly specifies otherwise, don't block on
// |surface_range_| more than once.
deadline_in_frames_ = 0u;
if (!base::FeatureList::IsEnabled(
features::kAlignSurfaceLayerImplToPixelGrid)) {
return;
}
// Don't allow DrawQuads to align on non-pixel boundaries.
gfx::Vector2dF quad_rect_offset = quad_rect.OffsetFromOrigin();
gfx::PointF rect_offset_in_target =
shared_quad_state->quad_to_target_transform.MapPoint(
gfx::PointF(quad_rect_offset.x(), quad_rect_offset.x()));
gfx::Vector2dF adjustment =
gfx::Vector2dF(rect_offset_in_target.x(), rect_offset_in_target.y()) -
gfx::Vector2dF(gfx::ToRoundedVector2d(gfx::Vector2dF(
rect_offset_in_target.x(), rect_offset_in_target.y())));
if (!adjustment.IsZero()) {
shared_quad_state->quad_to_target_transform.PostTranslate(-adjustment);
}
}
bool SurfaceLayerImpl::is_surface_layer() const {
return true;
}
gfx::Rect SurfaceLayerImpl::GetEnclosingVisibleRectInTargetSpace() const {
return GetScaledEnclosingVisibleRectInTargetSpace(
layer_tree_impl()->device_scale_factor());
}
void SurfaceLayerImpl::GetDebugBorderProperties(SkColor4f* color,
float* width) const {
if (color)
*color = DebugColors::SurfaceLayerBorderColor();
if (width)
*width = DebugColors::SurfaceLayerBorderWidth(
layer_tree_impl() ? layer_tree_impl()->device_scale_factor() : 1);
}
void SurfaceLayerImpl::AppendRainbowDebugBorder(
viz::CompositorRenderPass* render_pass) {
if (!ShowDebugBorders(DebugBorderType::SURFACE))
return;
viz::SharedQuadState* shared_quad_state =
render_pass->CreateAndAppendSharedQuadState();
PopulateSharedQuadState(shared_quad_state, contents_opaque());
float border_width = DebugColors::SurfaceLayerBorderWidth(
layer_tree_impl() ? layer_tree_impl()->device_scale_factor() : 1);
auto colors = std::to_array<SkColor4f>({
SkColor4f(1.0f, 0.0f, 0.0f, 0.5f), // Red.
SkColor4f(1.0f, 0.65f, 0.0f, 0.5f), // Orange.
SkColor4f(1.0f, 1.0f, 0.0f, 0.5f), // Yellow.
SkColor4f(0.0f, 0.5f, 0.0f, 0.5f), // Green.
SkColor4f(0.0f, 0.0f, 1.0f, 0.50f), // Blue.
SkColor4f(0.93f, 0.51f, 0.93f, 0.5f), // Violet.
});
const int kStripeWidth = 300;
const int kStripeHeight = 300;
for (int i = 0;; ++i) {
// For horizontal lines.
int x = kStripeWidth * i;
int width = std::min(kStripeWidth, bounds().width() - x - 1);
// For vertical lines.
int y = kStripeHeight * i;
int height = std::min(kStripeHeight, bounds().height() - y - 1);
gfx::Rect top(x, 0, width, border_width);
gfx::Rect bottom(x, bounds().height() - border_width, width, border_width);
gfx::Rect left(0, y, border_width, height);
gfx::Rect right(bounds().width() - border_width, y, border_width, height);
if (top.IsEmpty() && left.IsEmpty())
break;
if (!top.IsEmpty()) {
bool force_anti_aliasing_off = false;
auto* top_quad =
render_pass->CreateAndAppendDrawQuad<viz::SolidColorDrawQuad>();
top_quad->SetNew(shared_quad_state, top, top, colors[i % colors.size()],
force_anti_aliasing_off);
auto* bottom_quad =
render_pass->CreateAndAppendDrawQuad<viz::SolidColorDrawQuad>();
bottom_quad->SetNew(shared_quad_state, bottom, bottom,
colors[colors.size() - 1 - (i % colors.size())],
force_anti_aliasing_off);
if (contents_opaque()) {
// Draws a stripe filling the layer vertically with the same color and
// width as the horizontal stipes along the layer's top border.
auto* solid_quad =
render_pass->CreateAndAppendDrawQuad<viz::SolidColorDrawQuad>();
// The inner fill is more transparent then the border.
static const float kFillOpacity = 0.1f;
SkColor4f fill_color = colors[i % colors.size()];
fill_color.fA *= kFillOpacity;
gfx::Rect fill_rect(x, 0, width, bounds().height());
solid_quad->SetNew(shared_quad_state, fill_rect, fill_rect, fill_color,
force_anti_aliasing_off);
}
}
if (!left.IsEmpty()) {
bool force_anti_aliasing_off = false;
auto* left_quad =
render_pass->CreateAndAppendDrawQuad<viz::SolidColorDrawQuad>();
left_quad->SetNew(shared_quad_state, left, left,
colors[colors.size() - 1 - (i % colors.size())],
force_anti_aliasing_off);
auto* right_quad =
render_pass->CreateAndAppendDrawQuad<viz::SolidColorDrawQuad>();
right_quad->SetNew(shared_quad_state, right, right,
colors[i % colors.size()], force_anti_aliasing_off);
}
}
}
void SurfaceLayerImpl::AsValueInto(base::trace_event::TracedValue* dict) const {
LayerImpl::AsValueInto(dict);
dict->SetString("surface_range", surface_range_.ToString());
}
} // namespace cc
|