Landing Recent QUIC changes until 8/19/2015 17:00 UTC.
[chromium-blink-merge.git] / cc / output / overlay_strategy_common.cc
blob5d79cd09878555a375dd715f0421b92079738617
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/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"
18 namespace cc {
20 OverlayStrategyCommon::OverlayStrategyCommon(
21 OverlayCandidateValidator* capability_checker,
22 OverlayStrategyCommonDelegate* delegate)
23 : capability_checker_(capability_checker), delegate_(delegate) {}
25 OverlayStrategyCommon::~OverlayStrategyCommon() {
28 bool OverlayStrategyCommon::Attempt(RenderPassList* render_passes_in_draw_order,
29 OverlayCandidateList* candidate_list,
30 float device_scale_factor) {
31 if (!capability_checker_)
32 return false;
33 RenderPass* root_render_pass = render_passes_in_draw_order->back();
34 DCHECK(root_render_pass);
36 QuadList& quad_list = root_render_pass->quad_list;
37 for (auto it = quad_list.begin(); it != quad_list.end(); ++it) {
38 OverlayCandidate candidate;
39 const DrawQuad* draw_quad = *it;
40 if (IsOverlayQuad(draw_quad) &&
41 GetCandidateQuadInfo(*draw_quad, &candidate) &&
42 delegate_->TryOverlay(capability_checker_, render_passes_in_draw_order,
43 candidate_list, candidate, it,
44 device_scale_factor))
45 return true;
47 return false;
50 bool OverlayStrategyCommon::IsOverlayQuad(const DrawQuad* draw_quad) {
51 switch (draw_quad->material) {
52 case DrawQuad::TEXTURE_CONTENT:
53 return TextureDrawQuad::MaterialCast(draw_quad)->allow_overlay();
54 case DrawQuad::STREAM_VIDEO_CONTENT:
55 return StreamVideoDrawQuad::MaterialCast(draw_quad)->allow_overlay();
56 case DrawQuad::IO_SURFACE_CONTENT:
57 return IOSurfaceDrawQuad::MaterialCast(draw_quad)->allow_overlay;
58 default:
59 return false;
63 // static
64 bool OverlayStrategyCommon::IsInvisibleQuad(const DrawQuad* draw_quad) {
65 if (draw_quad->material == DrawQuad::SOLID_COLOR) {
66 const SolidColorDrawQuad* solid_quad =
67 SolidColorDrawQuad::MaterialCast(draw_quad);
68 SkColor color = solid_quad->color;
69 float opacity = solid_quad->shared_quad_state->opacity;
70 float alpha = (SkColorGetA(color) * (1.0f / 255.0f)) * opacity;
71 return solid_quad->ShouldDrawWithBlending() &&
72 alpha < std::numeric_limits<float>::epsilon();
74 return false;
77 bool OverlayStrategyCommon::GetTextureQuadInfo(const TextureDrawQuad& quad,
78 OverlayCandidate* quad_info) {
79 gfx::OverlayTransform overlay_transform =
80 OverlayCandidate::GetOverlayTransform(
81 quad.shared_quad_state->quad_to_target_transform, quad.y_flipped);
82 if (quad.background_color != SK_ColorTRANSPARENT ||
83 quad.premultiplied_alpha ||
84 overlay_transform == gfx::OVERLAY_TRANSFORM_INVALID)
85 return false;
86 quad_info->resource_id = quad.resource_id();
87 quad_info->resource_size_in_pixels = quad.resource_size_in_pixels();
88 quad_info->transform = overlay_transform;
89 quad_info->uv_rect = BoundingRect(quad.uv_top_left, quad.uv_bottom_right);
90 return true;
93 bool OverlayStrategyCommon::GetVideoQuadInfo(const StreamVideoDrawQuad& quad,
94 OverlayCandidate* quad_info) {
95 gfx::OverlayTransform overlay_transform =
96 OverlayCandidate::GetOverlayTransform(
97 quad.shared_quad_state->quad_to_target_transform, false);
98 if (overlay_transform == gfx::OVERLAY_TRANSFORM_INVALID)
99 return false;
100 if (!quad.matrix.IsScaleOrTranslation()) {
101 // We cannot handle anything other than scaling & translation for texture
102 // coordinates yet.
103 return false;
105 quad_info->resource_id = quad.resource_id();
106 quad_info->resource_size_in_pixels = quad.resource_size_in_pixels();
107 quad_info->transform = overlay_transform;
109 gfx::Point3F uv0 = gfx::Point3F(0, 0, 0);
110 gfx::Point3F uv1 = gfx::Point3F(1, 1, 0);
111 quad.matrix.TransformPoint(&uv0);
112 quad.matrix.TransformPoint(&uv1);
113 gfx::Vector3dF delta = uv1 - uv0;
114 if (delta.x() < 0) {
115 quad_info->transform = OverlayCandidate::ModifyTransform(
116 quad_info->transform, gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL);
117 float x0 = uv0.x();
118 uv0.set_x(uv1.x());
119 uv1.set_x(x0);
120 delta.set_x(-delta.x());
123 if (delta.y() < 0) {
124 // In this situation, uv0y < uv1y. Since we overlay inverted, a request
125 // to invert the source texture means we can just output the texture
126 // normally and it will be correct.
127 quad_info->uv_rect = gfx::RectF(uv0.x(), uv1.y(), delta.x(), -delta.y());
128 } else {
129 quad_info->transform = OverlayCandidate::ModifyTransform(
130 quad_info->transform, gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL);
131 quad_info->uv_rect = gfx::RectF(uv0.x(), uv0.y(), delta.x(), delta.y());
133 return true;
136 bool OverlayStrategyCommon::GetIOSurfaceQuadInfo(const IOSurfaceDrawQuad& quad,
137 OverlayCandidate* quad_info) {
138 gfx::OverlayTransform overlay_transform =
139 OverlayCandidate::GetOverlayTransform(
140 quad.shared_quad_state->quad_to_target_transform, false);
141 if (overlay_transform != gfx::OVERLAY_TRANSFORM_NONE)
142 return false;
143 quad_info->resource_id = quad.io_surface_resource_id();
144 quad_info->resource_size_in_pixels = quad.io_surface_size;
145 quad_info->transform = overlay_transform;
146 quad_info->uv_rect = gfx::Rect(0, 0, 1, 1);
147 return true;
150 bool OverlayStrategyCommon::GetCandidateQuadInfo(const DrawQuad& draw_quad,
151 OverlayCandidate* quad_info) {
152 // All quad checks.
153 if (draw_quad.needs_blending || draw_quad.shared_quad_state->opacity != 1.f ||
154 draw_quad.shared_quad_state->blend_mode != SkXfermode::kSrcOver_Mode)
155 return false;
157 switch (draw_quad.material) {
158 case DrawQuad::TEXTURE_CONTENT: {
159 auto* quad = TextureDrawQuad::MaterialCast(&draw_quad);
160 if (!GetTextureQuadInfo(*quad, quad_info))
161 return false;
162 } break;
163 case DrawQuad::STREAM_VIDEO_CONTENT: {
164 auto* quad = StreamVideoDrawQuad::MaterialCast(&draw_quad);
165 if (!GetVideoQuadInfo(*quad, quad_info))
166 return false;
167 } break;
168 case DrawQuad::IO_SURFACE_CONTENT: {
169 auto* quad = IOSurfaceDrawQuad::MaterialCast(&draw_quad);
170 if (!GetIOSurfaceQuadInfo(*quad, quad_info))
171 return false;
172 } break;
173 default:
174 return false;
177 quad_info->format = RGBA_8888;
178 quad_info->display_rect = OverlayCandidate::GetOverlayRect(
179 draw_quad.shared_quad_state->quad_to_target_transform, draw_quad.rect);
180 return true;
183 } // namespace cc