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/io_surface_draw_quad.h"
10 #include "cc/quads/solid_color_draw_quad.h"
11 #include "cc/quads/stream_video_draw_quad.h"
12 #include "cc/quads/texture_draw_quad.h"
13 #include "cc/resources/resource_provider.h"
14 #include "ui/gfx/geometry/point3_f.h"
15 #include "ui/gfx/geometry/rect_conversions.h"
16 #include "ui/gfx/transform.h"
20 OverlayStrategyCommon::OverlayStrategyCommon(
21 OverlayCandidateValidator
* capability_checker
)
22 : capability_checker_(capability_checker
) {
25 OverlayStrategyCommon::~OverlayStrategyCommon() {
28 bool OverlayStrategyCommon::Attempt(RenderPassList
* render_passes_in_draw_order
,
29 OverlayCandidateList
* candidate_list
) {
30 if (!capability_checker_
)
32 RenderPass
* root_render_pass
= render_passes_in_draw_order
->back();
33 DCHECK(root_render_pass
);
35 QuadList
& quad_list
= root_render_pass
->quad_list
;
36 for (auto it
= quad_list
.begin(); it
!= quad_list
.end(); ++it
) {
37 OverlayCandidate candidate
;
38 const DrawQuad
* draw_quad
= *it
;
39 if (IsOverlayQuad(draw_quad
) &&
40 GetCandidateQuadInfo(*draw_quad
, &candidate
) &&
41 TryOverlay(capability_checker_
, render_passes_in_draw_order
,
42 candidate_list
, candidate
, it
))
48 bool OverlayStrategyCommon::IsOverlayQuad(const DrawQuad
* draw_quad
) {
49 switch (draw_quad
->material
) {
50 case DrawQuad::TEXTURE_CONTENT
:
51 return TextureDrawQuad::MaterialCast(draw_quad
)->allow_overlay();
52 case DrawQuad::STREAM_VIDEO_CONTENT
:
53 return StreamVideoDrawQuad::MaterialCast(draw_quad
)->allow_overlay();
54 case DrawQuad::IO_SURFACE_CONTENT
:
55 return IOSurfaceDrawQuad::MaterialCast(draw_quad
)->allow_overlay
;
61 bool OverlayStrategyCommon::IsInvisibleQuad(const DrawQuad
* draw_quad
) {
62 if (draw_quad
->material
== DrawQuad::SOLID_COLOR
) {
63 const SolidColorDrawQuad
* solid_quad
=
64 SolidColorDrawQuad::MaterialCast(draw_quad
);
65 SkColor color
= solid_quad
->color
;
66 float opacity
= solid_quad
->shared_quad_state
->opacity
;
67 float alpha
= (SkColorGetA(color
) * (1.0f
/ 255.0f
)) * opacity
;
68 return solid_quad
->ShouldDrawWithBlending() &&
69 alpha
< std::numeric_limits
<float>::epsilon();
74 bool OverlayStrategyCommon::GetTextureQuadInfo(const TextureDrawQuad
& quad
,
75 OverlayCandidate
* quad_info
) {
76 gfx::OverlayTransform overlay_transform
=
77 OverlayCandidate::GetOverlayTransform(
78 quad
.shared_quad_state
->quad_to_target_transform
, quad
.y_flipped
);
79 if (quad
.background_color
!= SK_ColorTRANSPARENT
||
80 quad
.premultiplied_alpha
||
81 overlay_transform
== gfx::OVERLAY_TRANSFORM_INVALID
)
83 quad_info
->resource_id
= quad
.resource_id();
84 quad_info
->resource_size_in_pixels
= quad
.resource_size_in_pixels();
85 quad_info
->transform
= overlay_transform
;
86 quad_info
->uv_rect
= BoundingRect(quad
.uv_top_left
, quad
.uv_bottom_right
);
90 bool OverlayStrategyCommon::GetVideoQuadInfo(const StreamVideoDrawQuad
& quad
,
91 OverlayCandidate
* quad_info
) {
92 gfx::OverlayTransform overlay_transform
=
93 OverlayCandidate::GetOverlayTransform(
94 quad
.shared_quad_state
->quad_to_target_transform
, false);
95 if (overlay_transform
== gfx::OVERLAY_TRANSFORM_INVALID
)
97 if (!quad
.matrix
.IsScaleOrTranslation()) {
98 // We cannot handle anything other than scaling & translation for texture
102 quad_info
->resource_id
= quad
.resource_id();
103 quad_info
->resource_size_in_pixels
= quad
.resource_size_in_pixels();
104 quad_info
->transform
= overlay_transform
;
106 gfx::Point3F uv0
= gfx::Point3F(0, 0, 0);
107 gfx::Point3F uv1
= gfx::Point3F(1, 1, 0);
108 quad
.matrix
.TransformPoint(&uv0
);
109 quad
.matrix
.TransformPoint(&uv1
);
110 gfx::Vector3dF delta
= uv1
- uv0
;
112 quad_info
->transform
= OverlayCandidate::ModifyTransform(
113 quad_info
->transform
, gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL
);
117 delta
.set_x(-delta
.x());
121 // In this situation, uv0y < uv1y. Since we overlay inverted, a request
122 // to invert the source texture means we can just output the texture
123 // normally and it will be correct.
124 quad_info
->uv_rect
= gfx::RectF(uv0
.x(), uv1
.y(), delta
.x(), -delta
.y());
126 quad_info
->transform
= OverlayCandidate::ModifyTransform(
127 quad_info
->transform
, gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL
);
128 quad_info
->uv_rect
= gfx::RectF(uv0
.x(), uv0
.y(), delta
.x(), delta
.y());
133 bool OverlayStrategyCommon::GetIOSurfaceQuadInfo(const IOSurfaceDrawQuad
& quad
,
134 OverlayCandidate
* quad_info
) {
135 gfx::OverlayTransform overlay_transform
=
136 OverlayCandidate::GetOverlayTransform(
137 quad
.shared_quad_state
->quad_to_target_transform
, false);
138 if (overlay_transform
!= gfx::OVERLAY_TRANSFORM_NONE
)
140 quad_info
->resource_id
= quad
.io_surface_resource_id();
141 quad_info
->resource_size_in_pixels
= quad
.io_surface_size
;
142 quad_info
->transform
= overlay_transform
;
143 quad_info
->uv_rect
= gfx::Rect(0, 0, 1, 1);
147 bool OverlayStrategyCommon::GetCandidateQuadInfo(const DrawQuad
& draw_quad
,
148 OverlayCandidate
* quad_info
) {
150 if (draw_quad
.needs_blending
|| draw_quad
.shared_quad_state
->opacity
!= 1.f
||
151 draw_quad
.shared_quad_state
->blend_mode
!= SkXfermode::kSrcOver_Mode
)
154 switch (draw_quad
.material
) {
155 case DrawQuad::TEXTURE_CONTENT
: {
156 auto* quad
= TextureDrawQuad::MaterialCast(&draw_quad
);
157 if (!GetTextureQuadInfo(*quad
, quad_info
))
160 case DrawQuad::STREAM_VIDEO_CONTENT
: {
161 auto* quad
= StreamVideoDrawQuad::MaterialCast(&draw_quad
);
162 if (!GetVideoQuadInfo(*quad
, quad_info
))
165 case DrawQuad::IO_SURFACE_CONTENT
: {
166 auto* quad
= IOSurfaceDrawQuad::MaterialCast(&draw_quad
);
167 if (!GetIOSurfaceQuadInfo(*quad
, quad_info
))
174 quad_info
->format
= RGBA_8888
;
175 quad_info
->display_rect
= OverlayCandidate::GetOverlayRect(
176 draw_quad
.shared_quad_state
->quad_to_target_transform
, draw_quad
.rect
);