Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / cc / output / overlay_strategy_common.cc
blob93bd15c190abe0f0255f7204e63abc32104d9037
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/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"
17 namespace cc {
19 OverlayStrategyCommon::OverlayStrategyCommon(
20 OverlayCandidateValidator* capability_checker,
21 ResourceProvider* resource_provider)
22 : capability_checker_(capability_checker),
23 resource_provider_(resource_provider) {
26 OverlayStrategyCommon::~OverlayStrategyCommon() {
29 bool OverlayStrategyCommon::IsOverlayQuad(const DrawQuad* draw_quad) {
30 unsigned int resource_id;
31 switch (draw_quad->material) {
32 case DrawQuad::TEXTURE_CONTENT:
33 resource_id = TextureDrawQuad::MaterialCast(draw_quad)->resource_id();
34 break;
35 case DrawQuad::STREAM_VIDEO_CONTENT:
36 resource_id = StreamVideoDrawQuad::MaterialCast(draw_quad)->resource_id();
37 break;
38 default:
39 return false;
41 return resource_provider_->AllowOverlay(resource_id);
44 bool OverlayStrategyCommon::IsInvisibleQuad(const DrawQuad* draw_quad) {
45 if (draw_quad->material == DrawQuad::SOLID_COLOR) {
46 const SolidColorDrawQuad* solid_quad =
47 SolidColorDrawQuad::MaterialCast(draw_quad);
48 SkColor color = solid_quad->color;
49 float opacity = solid_quad->opacity();
50 float alpha = (SkColorGetA(color) * (1.0f / 255.0f)) * opacity;
51 return solid_quad->ShouldDrawWithBlending() &&
52 alpha < std::numeric_limits<float>::epsilon();
54 return false;
57 bool OverlayStrategyCommon::GetTextureQuadInfo(const TextureDrawQuad& quad,
58 OverlayCandidate* quad_info) {
59 gfx::OverlayTransform overlay_transform =
60 OverlayCandidate::GetOverlayTransform(quad.quadTransform(),
61 quad.y_flipped);
62 if (quad.background_color != SK_ColorTRANSPARENT ||
63 quad.premultiplied_alpha ||
64 overlay_transform == gfx::OVERLAY_TRANSFORM_INVALID)
65 return false;
66 quad_info->resource_id = quad.resource_id();
67 quad_info->transform = overlay_transform;
68 quad_info->uv_rect = BoundingRect(quad.uv_top_left, quad.uv_bottom_right);
69 return true;
72 bool OverlayStrategyCommon::GetVideoQuadInfo(const StreamVideoDrawQuad& quad,
73 OverlayCandidate* quad_info) {
74 gfx::OverlayTransform overlay_transform =
75 OverlayCandidate::GetOverlayTransform(quad.quadTransform(), false);
76 if (overlay_transform == gfx::OVERLAY_TRANSFORM_INVALID)
77 return false;
78 if (!quad.matrix.IsScaleOrTranslation()) {
79 // We cannot handle anything other than scaling & translation for texture
80 // coordinates yet.
81 return false;
83 quad_info->resource_id = quad.resource_id();
84 quad_info->transform = overlay_transform;
86 gfx::Point3F uv0 = gfx::Point3F(0, 0, 0);
87 gfx::Point3F uv1 = gfx::Point3F(1, 1, 0);
88 quad.matrix.TransformPoint(&uv0);
89 quad.matrix.TransformPoint(&uv1);
90 gfx::Vector3dF delta = uv1 - uv0;
91 if (delta.x() < 0) {
92 quad_info->transform = OverlayCandidate::ModifyTransform(
93 quad_info->transform, gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL);
94 float x0 = uv0.x();
95 uv0.set_x(uv1.x());
96 uv1.set_x(x0);
97 delta.set_x(-delta.x());
100 if (delta.y() < 0) {
101 // In this situation, uv0y < uv1y. Since we overlay inverted, a request
102 // to invert the source texture means we can just output the texture
103 // normally and it will be correct.
104 quad_info->uv_rect = gfx::RectF(uv0.x(), uv1.y(), delta.x(), -delta.y());
105 } else {
106 quad_info->transform = OverlayCandidate::ModifyTransform(
107 quad_info->transform, gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL);
108 quad_info->uv_rect = gfx::RectF(uv0.x(), uv0.y(), delta.x(), delta.y());
110 return true;
113 bool OverlayStrategyCommon::GetCandidateQuadInfo(const DrawQuad& draw_quad,
114 OverlayCandidate* quad_info) {
115 // All quad checks.
116 if (draw_quad.needs_blending || draw_quad.shared_quad_state->opacity != 1.f ||
117 draw_quad.shared_quad_state->blend_mode != SkXfermode::kSrcOver_Mode)
118 return false;
120 if (draw_quad.material == DrawQuad::TEXTURE_CONTENT) {
121 const TextureDrawQuad& quad = *TextureDrawQuad::MaterialCast(&draw_quad);
122 if (!GetTextureQuadInfo(quad, quad_info))
123 return false;
124 } else if (draw_quad.material == DrawQuad::STREAM_VIDEO_CONTENT) {
125 const StreamVideoDrawQuad& quad =
126 *StreamVideoDrawQuad::MaterialCast(&draw_quad);
127 if (!GetVideoQuadInfo(quad, quad_info))
128 return false;
131 quad_info->format = RGBA_8888;
132 quad_info->display_rect = OverlayCandidate::GetOverlayRect(
133 draw_quad.quadTransform(), draw_quad.rect);
134 return true;
137 } // namespace cc