1 // Copyright 2015 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_common.h"
9 #include "cc/quads/solid_color_draw_quad.h"
10 #include "cc/quads/stream_video_draw_quad.h"
11 #include "cc/quads/texture_draw_quad.h"
12 #include "cc/resources/resource_provider.h"
13 #include "ui/gfx/geometry/point3_f.h"
14 #include "ui/gfx/geometry/rect_conversions.h"
15 #include "ui/gfx/transform.h"
19 OverlayStrategyCommon::OverlayStrategyCommon(
20 OverlayCandidateValidator
* capability_checker
)
21 : capability_checker_(capability_checker
) {
24 OverlayStrategyCommon::~OverlayStrategyCommon() {
27 bool OverlayStrategyCommon::Attempt(RenderPassList
* render_passes_in_draw_order
,
28 OverlayCandidateList
* candidate_list
) {
29 if (!capability_checker_
)
31 RenderPass
* root_render_pass
= render_passes_in_draw_order
->back();
32 DCHECK(root_render_pass
);
34 QuadList
& quad_list
= root_render_pass
->quad_list
;
35 for (auto it
= quad_list
.begin(); it
!= quad_list
.end(); ++it
) {
36 OverlayCandidate candidate
;
37 const DrawQuad
* draw_quad
= *it
;
38 if (IsOverlayQuad(draw_quad
) &&
39 GetCandidateQuadInfo(*draw_quad
, &candidate
) &&
40 TryOverlay(capability_checker_
, render_passes_in_draw_order
,
41 candidate_list
, candidate
, it
))
47 bool OverlayStrategyCommon::IsOverlayQuad(const DrawQuad
* draw_quad
) {
48 switch (draw_quad
->material
) {
49 case DrawQuad::TEXTURE_CONTENT
:
50 return TextureDrawQuad::MaterialCast(draw_quad
)->allow_overlay();
51 case DrawQuad::STREAM_VIDEO_CONTENT
:
52 return StreamVideoDrawQuad::MaterialCast(draw_quad
)->allow_overlay();
58 bool OverlayStrategyCommon::IsInvisibleQuad(const DrawQuad
* draw_quad
) {
59 if (draw_quad
->material
== DrawQuad::SOLID_COLOR
) {
60 const SolidColorDrawQuad
* solid_quad
=
61 SolidColorDrawQuad::MaterialCast(draw_quad
);
62 SkColor color
= solid_quad
->color
;
63 float opacity
= solid_quad
->shared_quad_state
->opacity
;
64 float alpha
= (SkColorGetA(color
) * (1.0f
/ 255.0f
)) * opacity
;
65 return solid_quad
->ShouldDrawWithBlending() &&
66 alpha
< std::numeric_limits
<float>::epsilon();
71 bool OverlayStrategyCommon::GetTextureQuadInfo(const TextureDrawQuad
& quad
,
72 OverlayCandidate
* quad_info
) {
73 gfx::OverlayTransform overlay_transform
=
74 OverlayCandidate::GetOverlayTransform(
75 quad
.shared_quad_state
->quad_to_target_transform
, quad
.y_flipped
);
76 if (quad
.background_color
!= SK_ColorTRANSPARENT
||
77 quad
.premultiplied_alpha
||
78 overlay_transform
== gfx::OVERLAY_TRANSFORM_INVALID
)
80 quad_info
->resource_id
= quad
.resource_id();
81 quad_info
->resource_size_in_pixels
= quad
.resource_size_in_pixels();
82 quad_info
->transform
= overlay_transform
;
83 quad_info
->uv_rect
= BoundingRect(quad
.uv_top_left
, quad
.uv_bottom_right
);
87 bool OverlayStrategyCommon::GetVideoQuadInfo(const StreamVideoDrawQuad
& quad
,
88 OverlayCandidate
* quad_info
) {
89 gfx::OverlayTransform overlay_transform
=
90 OverlayCandidate::GetOverlayTransform(
91 quad
.shared_quad_state
->quad_to_target_transform
, false);
92 if (overlay_transform
== gfx::OVERLAY_TRANSFORM_INVALID
)
94 if (!quad
.matrix
.IsScaleOrTranslation()) {
95 // We cannot handle anything other than scaling & translation for texture
99 quad_info
->resource_id
= quad
.resource_id();
100 quad_info
->resource_size_in_pixels
= quad
.resource_size_in_pixels();
101 quad_info
->transform
= overlay_transform
;
103 gfx::Point3F uv0
= gfx::Point3F(0, 0, 0);
104 gfx::Point3F uv1
= gfx::Point3F(1, 1, 0);
105 quad
.matrix
.TransformPoint(&uv0
);
106 quad
.matrix
.TransformPoint(&uv1
);
107 gfx::Vector3dF delta
= uv1
- uv0
;
109 quad_info
->transform
= OverlayCandidate::ModifyTransform(
110 quad_info
->transform
, gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL
);
114 delta
.set_x(-delta
.x());
118 // In this situation, uv0y < uv1y. Since we overlay inverted, a request
119 // to invert the source texture means we can just output the texture
120 // normally and it will be correct.
121 quad_info
->uv_rect
= gfx::RectF(uv0
.x(), uv1
.y(), delta
.x(), -delta
.y());
123 quad_info
->transform
= OverlayCandidate::ModifyTransform(
124 quad_info
->transform
, gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL
);
125 quad_info
->uv_rect
= gfx::RectF(uv0
.x(), uv0
.y(), delta
.x(), delta
.y());
130 bool OverlayStrategyCommon::GetCandidateQuadInfo(const DrawQuad
& draw_quad
,
131 OverlayCandidate
* quad_info
) {
133 if (draw_quad
.needs_blending
|| draw_quad
.shared_quad_state
->opacity
!= 1.f
||
134 draw_quad
.shared_quad_state
->blend_mode
!= SkXfermode::kSrcOver_Mode
)
137 if (draw_quad
.material
== DrawQuad::TEXTURE_CONTENT
) {
138 const TextureDrawQuad
& quad
= *TextureDrawQuad::MaterialCast(&draw_quad
);
139 if (!GetTextureQuadInfo(quad
, quad_info
))
141 } else if (draw_quad
.material
== DrawQuad::STREAM_VIDEO_CONTENT
) {
142 const StreamVideoDrawQuad
& quad
=
143 *StreamVideoDrawQuad::MaterialCast(&draw_quad
);
144 if (!GetVideoQuadInfo(quad
, quad_info
))
148 quad_info
->format
= RGBA_8888
;
149 quad_info
->display_rect
= OverlayCandidate::GetOverlayRect(
150 draw_quad
.shared_quad_state
->quad_to_target_transform
, draw_quad
.rect
);