Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / cc / output / overlay_strategy_common.cc
blob243c19990c09c4940a28264874d9572d4221d264
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"
7 #include <limits>
9 #include "cc/base/math_util.h"
10 #include "cc/quads/io_surface_draw_quad.h"
11 #include "cc/quads/solid_color_draw_quad.h"
12 #include "cc/quads/stream_video_draw_quad.h"
13 #include "cc/quads/texture_draw_quad.h"
14 #include "cc/resources/resource_provider.h"
15 #include "ui/gfx/geometry/point3_f.h"
16 #include "ui/gfx/geometry/rect_conversions.h"
17 #include "ui/gfx/transform.h"
19 namespace cc {
21 OverlayStrategyCommon::OverlayStrategyCommon(
22 OverlayCandidateValidator* capability_checker,
23 OverlayStrategyCommonDelegate* delegate)
24 : capability_checker_(capability_checker), delegate_(delegate) {}
26 OverlayStrategyCommon::~OverlayStrategyCommon() {
29 bool OverlayStrategyCommon::Attempt(RenderPassList* render_passes_in_draw_order,
30 OverlayCandidateList* candidate_list,
31 float device_scale_factor) {
32 if (!capability_checker_)
33 return false;
34 RenderPass* root_render_pass = render_passes_in_draw_order->back();
35 DCHECK(root_render_pass);
37 // Add our primary surface.
38 OverlayCandidate main_image;
39 main_image.display_rect = gfx::RectF(root_render_pass->output_rect);
40 DCHECK(candidate_list->empty());
41 candidate_list->push_back(main_image);
43 bool created_overlay = false;
44 QuadList& quad_list = root_render_pass->quad_list;
45 for (auto it = quad_list.begin(); it != quad_list.end();) {
46 OverlayCandidate candidate;
47 if (!GetCandidateQuadInfo(**it, &candidate)) {
48 ++it;
49 continue;
52 OverlayResult result = delegate_->TryOverlay(
53 capability_checker_, render_passes_in_draw_order, candidate_list,
54 candidate, &it, device_scale_factor);
55 switch (result) {
56 case DID_NOT_CREATE_OVERLAY:
57 ++it;
58 break;
59 case CREATED_OVERLAY_STOP_LOOKING:
60 return true;
61 case CREATED_OVERLAY_KEEP_LOOKING:
62 created_overlay = true;
63 break;
67 if (!created_overlay) {
68 DCHECK_EQ(1u, candidate_list->size());
69 candidate_list->clear();
72 return created_overlay;
75 // static
76 bool OverlayStrategyCommon::IsInvisibleQuad(const DrawQuad* draw_quad) {
77 if (draw_quad->material == DrawQuad::SOLID_COLOR) {
78 const SolidColorDrawQuad* solid_quad =
79 SolidColorDrawQuad::MaterialCast(draw_quad);
80 SkColor color = solid_quad->color;
81 float opacity = solid_quad->shared_quad_state->opacity;
82 float alpha = (SkColorGetA(color) * (1.0f / 255.0f)) * opacity;
83 return solid_quad->ShouldDrawWithBlending() &&
84 alpha < std::numeric_limits<float>::epsilon();
86 return false;
89 bool OverlayStrategyCommon::GetTextureQuadInfo(const TextureDrawQuad& quad,
90 OverlayCandidate* quad_info) {
91 if (!quad.allow_overlay())
92 return false;
93 gfx::OverlayTransform overlay_transform =
94 OverlayCandidate::GetOverlayTransform(
95 quad.shared_quad_state->quad_to_target_transform, quad.y_flipped);
96 if (quad.background_color != SK_ColorTRANSPARENT ||
97 quad.premultiplied_alpha ||
98 overlay_transform == gfx::OVERLAY_TRANSFORM_INVALID)
99 return false;
100 quad_info->resource_id = quad.resource_id();
101 quad_info->resource_size_in_pixels = quad.resource_size_in_pixels();
102 quad_info->transform = overlay_transform;
103 quad_info->uv_rect = BoundingRect(quad.uv_top_left, quad.uv_bottom_right);
104 quad_info->quad_rect_in_target_space = MathUtil::MapEnclosingClippedRect(
105 quad.shared_quad_state->quad_to_target_transform, quad.rect);
106 quad_info->clip_rect = quad.shared_quad_state->clip_rect;
107 quad_info->is_clipped = quad.shared_quad_state->is_clipped;
108 return true;
111 bool OverlayStrategyCommon::GetVideoQuadInfo(const StreamVideoDrawQuad& quad,
112 OverlayCandidate* quad_info) {
113 if (!quad.allow_overlay())
114 return false;
115 gfx::OverlayTransform overlay_transform =
116 OverlayCandidate::GetOverlayTransform(
117 quad.shared_quad_state->quad_to_target_transform, false);
118 if (overlay_transform == gfx::OVERLAY_TRANSFORM_INVALID)
119 return false;
120 if (!quad.matrix.IsScaleOrTranslation()) {
121 // We cannot handle anything other than scaling & translation for texture
122 // coordinates yet.
123 return false;
125 quad_info->resource_id = quad.resource_id();
126 quad_info->resource_size_in_pixels = quad.resource_size_in_pixels();
127 quad_info->transform = overlay_transform;
128 quad_info->quad_rect_in_target_space = MathUtil::MapEnclosingClippedRect(
129 quad.shared_quad_state->quad_to_target_transform, quad.rect);
130 quad_info->clip_rect = quad.shared_quad_state->clip_rect;
131 quad_info->is_clipped = quad.shared_quad_state->is_clipped;
133 gfx::Point3F uv0 = gfx::Point3F(0, 0, 0);
134 gfx::Point3F uv1 = gfx::Point3F(1, 1, 0);
135 quad.matrix.TransformPoint(&uv0);
136 quad.matrix.TransformPoint(&uv1);
137 gfx::Vector3dF delta = uv1 - uv0;
138 if (delta.x() < 0) {
139 quad_info->transform = OverlayCandidate::ModifyTransform(
140 quad_info->transform, gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL);
141 float x0 = uv0.x();
142 uv0.set_x(uv1.x());
143 uv1.set_x(x0);
144 delta.set_x(-delta.x());
147 if (delta.y() < 0) {
148 // In this situation, uv0y < uv1y. Since we overlay inverted, a request
149 // to invert the source texture means we can just output the texture
150 // normally and it will be correct.
151 quad_info->uv_rect = gfx::RectF(uv0.x(), uv1.y(), delta.x(), -delta.y());
152 } else {
153 quad_info->transform = OverlayCandidate::ModifyTransform(
154 quad_info->transform, gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL);
155 quad_info->uv_rect = gfx::RectF(uv0.x(), uv0.y(), delta.x(), delta.y());
157 return true;
160 bool OverlayStrategyCommon::GetIOSurfaceQuadInfo(const IOSurfaceDrawQuad& quad,
161 OverlayCandidate* quad_info) {
162 if (!quad.allow_overlay)
163 return false;
164 gfx::OverlayTransform overlay_transform =
165 OverlayCandidate::GetOverlayTransform(
166 quad.shared_quad_state->quad_to_target_transform, false);
167 if (overlay_transform != gfx::OVERLAY_TRANSFORM_NONE)
168 return false;
169 quad_info->resource_id = quad.io_surface_resource_id();
170 quad_info->resource_size_in_pixels = quad.io_surface_size;
171 quad_info->transform = overlay_transform;
172 quad_info->uv_rect = gfx::RectF(1.f, 1.f);
173 quad_info->quad_rect_in_target_space = MathUtil::MapEnclosingClippedRect(
174 quad.shared_quad_state->quad_to_target_transform, quad.rect);
175 quad_info->clip_rect = quad.shared_quad_state->clip_rect;
176 quad_info->is_clipped = quad.shared_quad_state->is_clipped;
177 return true;
180 bool OverlayStrategyCommon::GetCandidateQuadInfo(const DrawQuad& draw_quad,
181 OverlayCandidate* quad_info) {
182 // All quad checks.
183 if (draw_quad.needs_blending || draw_quad.shared_quad_state->opacity != 1.f ||
184 draw_quad.shared_quad_state->blend_mode != SkXfermode::kSrcOver_Mode)
185 return false;
187 switch (draw_quad.material) {
188 case DrawQuad::TEXTURE_CONTENT: {
189 auto* quad = TextureDrawQuad::MaterialCast(&draw_quad);
190 if (!GetTextureQuadInfo(*quad, quad_info))
191 return false;
192 } break;
193 case DrawQuad::STREAM_VIDEO_CONTENT: {
194 auto* quad = StreamVideoDrawQuad::MaterialCast(&draw_quad);
195 if (!GetVideoQuadInfo(*quad, quad_info))
196 return false;
197 } break;
198 case DrawQuad::IO_SURFACE_CONTENT: {
199 auto* quad = IOSurfaceDrawQuad::MaterialCast(&draw_quad);
200 if (!GetIOSurfaceQuadInfo(*quad, quad_info))
201 return false;
202 } break;
203 default:
204 return false;
207 quad_info->format = RGBA_8888;
208 quad_info->display_rect = OverlayCandidate::GetOverlayRect(
209 draw_quad.shared_quad_state->quad_to_target_transform, draw_quad.rect);
210 return true;
213 } // namespace cc