1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/output/overlay_strategy_single_on_top.h"
7 #include "cc/quads/draw_quad.h"
8 #include "cc/quads/texture_draw_quad.h"
9 #include "ui/gfx/geometry/rect_conversions.h"
10 #include "ui/gfx/transform.h"
14 OverlayStrategySingleOnTop::OverlayStrategySingleOnTop(
15 OverlayCandidateValidator
* capability_checker
,
16 ResourceProvider
* resource_provider
)
17 : capability_checker_(capability_checker
),
18 resource_provider_(resource_provider
) {}
20 bool OverlayStrategySingleOnTop::Attempt(
21 RenderPassList
* render_passes_in_draw_order
,
22 OverlayCandidateList
* candidate_list
) {
23 // Only attempt to handle very simple case for now.
24 if (!capability_checker_
)
27 RenderPass
* root_render_pass
= render_passes_in_draw_order
->back();
28 DCHECK(root_render_pass
);
30 QuadList
& quad_list
= root_render_pass
->quad_list
;
31 auto candidate_iterator
= quad_list
.end();
32 for (auto it
= quad_list
.begin(); it
!= quad_list
.end(); ++it
) {
33 const DrawQuad
* draw_quad
= &*it
;
34 if (draw_quad
->material
== DrawQuad::TEXTURE_CONTENT
) {
35 const TextureDrawQuad
& quad
= *TextureDrawQuad::MaterialCast(draw_quad
);
36 if (!resource_provider_
->AllowOverlay(quad
.resource_id
)) {
39 // Check that no prior quads overlap it.
40 bool intersects
= false;
41 gfx::RectF rect
= draw_quad
->rect
;
42 draw_quad
->quadTransform().TransformRect(&rect
);
43 for (auto overlap_iter
= quad_list
.cbegin(); overlap_iter
!= it
;
45 gfx::RectF overlap_rect
= overlap_iter
->rect
;
46 overlap_iter
->quadTransform().TransformRect(&overlap_rect
);
47 if (rect
.Intersects(overlap_rect
)) {
54 candidate_iterator
= it
;
58 if (candidate_iterator
== quad_list
.end())
60 const TextureDrawQuad
& quad
=
61 *TextureDrawQuad::MaterialCast(&*candidate_iterator
);
64 gfx::OverlayTransform overlay_transform
=
65 OverlayCandidate::GetOverlayTransform(quad
.quadTransform(), quad
.flipped
);
66 if (overlay_transform
== gfx::OVERLAY_TRANSFORM_INVALID
||
67 !quad
.quadTransform().IsIdentityOrTranslation() || quad
.needs_blending
||
68 quad
.shared_quad_state
->opacity
!= 1.f
||
69 quad
.shared_quad_state
->blend_mode
!= SkXfermode::kSrcOver_Mode
||
70 quad
.premultiplied_alpha
|| quad
.background_color
!= SK_ColorTRANSPARENT
)
73 // Add our primary surface.
74 OverlayCandidateList candidates
;
75 OverlayCandidate main_image
;
76 main_image
.display_rect
= root_render_pass
->output_rect
;
77 main_image
.format
= RGBA_8888
;
78 candidates
.push_back(main_image
);
81 OverlayCandidate candidate
;
82 candidate
.transform
= overlay_transform
;
83 candidate
.display_rect
=
84 OverlayCandidate::GetOverlayRect(quad
.quadTransform(), quad
.rect
);
85 candidate
.uv_rect
= BoundingRect(quad
.uv_top_left
, quad
.uv_bottom_right
);
86 candidate
.format
= RGBA_8888
;
87 candidate
.resource_id
= quad
.resource_id
;
88 candidate
.plane_z_order
= 1;
89 candidates
.push_back(candidate
);
92 capability_checker_
->CheckOverlaySupport(&candidates
);
94 // If the candidate can be handled by an overlay, create a pass for it.
95 if (candidates
[1].overlay_handled
) {
96 quad_list
.EraseAndInvalidateAllPointers(candidate_iterator
);
97 candidate_list
->swap(candidates
);