GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / cc / output / overlay_strategy_common.cc
blobb03fbba5fe2d52d1fa98f1d11ea3881e7f52b02f
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 "ui/gfx/geometry/point3_f.h"
13 #include "ui/gfx/geometry/rect_conversions.h"
14 #include "ui/gfx/transform.h"
16 namespace cc {
18 OverlayStrategyCommon::OverlayStrategyCommon(
19 OverlayCandidateValidator* capability_checker,
20 ResourceProvider* resource_provider)
21 : capability_checker_(capability_checker),
22 resource_provider_(resource_provider) {
25 OverlayStrategyCommon::~OverlayStrategyCommon() {
28 bool OverlayStrategyCommon::IsOverlayQuad(const DrawQuad* draw_quad) {
29 unsigned int resource_id;
30 switch (draw_quad->material) {
31 case DrawQuad::TEXTURE_CONTENT:
32 resource_id = TextureDrawQuad::MaterialCast(draw_quad)->resource_id;
33 break;
34 case DrawQuad::STREAM_VIDEO_CONTENT:
35 resource_id = StreamVideoDrawQuad::MaterialCast(draw_quad)->resource_id;
36 break;
37 default:
38 return false;
40 return resource_provider_->AllowOverlay(resource_id);
43 bool OverlayStrategyCommon::IsInvisibleQuad(const DrawQuad* draw_quad) {
44 if (draw_quad->material == DrawQuad::SOLID_COLOR) {
45 const SolidColorDrawQuad* solid_quad =
46 SolidColorDrawQuad::MaterialCast(draw_quad);
47 SkColor color = solid_quad->color;
48 float opacity = solid_quad->opacity();
49 float alpha = (SkColorGetA(color) * (1.0f / 255.0f)) * opacity;
50 return solid_quad->ShouldDrawWithBlending() &&
51 alpha < std::numeric_limits<float>::epsilon();
53 return false;
56 bool OverlayStrategyCommon::GetTextureQuadInfo(const TextureDrawQuad& quad,
57 OverlayCandidate* quad_info) {
58 gfx::OverlayTransform overlay_transform =
59 OverlayCandidate::GetOverlayTransform(quad.quadTransform(), quad.flipped);
60 if (quad.background_color != SK_ColorTRANSPARENT ||
61 quad.premultiplied_alpha ||
62 overlay_transform == gfx::OVERLAY_TRANSFORM_INVALID)
63 return false;
64 quad_info->resource_id = quad.resource_id;
65 quad_info->transform = overlay_transform;
66 quad_info->uv_rect = BoundingRect(quad.uv_top_left, quad.uv_bottom_right);
67 return true;
70 bool OverlayStrategyCommon::GetVideoQuadInfo(const StreamVideoDrawQuad& quad,
71 OverlayCandidate* quad_info) {
72 gfx::OverlayTransform overlay_transform =
73 OverlayCandidate::GetOverlayTransform(quad.quadTransform(), false);
74 if (overlay_transform == gfx::OVERLAY_TRANSFORM_INVALID)
75 return false;
76 if (!quad.matrix.IsScaleOrTranslation()) {
77 // We cannot handle anything other than scaling & translation for texture
78 // coordinates yet.
79 return false;
81 quad_info->resource_id = quad.resource_id;
82 quad_info->transform = overlay_transform;
84 gfx::Point3F uv0 = gfx::Point3F(0, 0, 0);
85 gfx::Point3F uv1 = gfx::Point3F(1, 1, 0);
86 quad.matrix.TransformPoint(&uv0);
87 quad.matrix.TransformPoint(&uv1);
88 gfx::Vector3dF delta = uv1 - uv0;
89 if (delta.x() < 0) {
90 quad_info->transform = OverlayCandidate::ModifyTransform(
91 quad_info->transform, gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL);
92 float x0 = uv0.x();
93 uv0.set_x(uv1.x());
94 uv1.set_x(x0);
95 delta.set_x(-delta.x());
98 if (delta.y() < 0) {
99 // In this situation, uv0y < uv1y. Since we overlay inverted, a request
100 // to invert the source texture means we can just output the texture
101 // normally and it will be correct.
102 quad_info->uv_rect = gfx::RectF(uv0.x(), uv1.y(), delta.x(), -delta.y());
103 } else {
104 quad_info->transform = OverlayCandidate::ModifyTransform(
105 quad_info->transform, gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL);
106 quad_info->uv_rect = gfx::RectF(uv0.x(), uv0.y(), delta.x(), delta.y());
108 return true;
111 bool OverlayStrategyCommon::GetCandidateQuadInfo(const DrawQuad& draw_quad,
112 OverlayCandidate* quad_info) {
113 // All quad checks.
114 if (draw_quad.needs_blending || draw_quad.shared_quad_state->opacity != 1.f ||
115 draw_quad.shared_quad_state->blend_mode != SkXfermode::kSrcOver_Mode)
116 return false;
118 if (draw_quad.material == DrawQuad::TEXTURE_CONTENT) {
119 const TextureDrawQuad& quad = *TextureDrawQuad::MaterialCast(&draw_quad);
120 if (!GetTextureQuadInfo(quad, quad_info))
121 return false;
122 } else if (draw_quad.material == DrawQuad::STREAM_VIDEO_CONTENT) {
123 const StreamVideoDrawQuad& quad =
124 *StreamVideoDrawQuad::MaterialCast(&draw_quad);
125 if (!GetVideoQuadInfo(quad, quad_info))
126 return false;
129 quad_info->format = RGBA_8888;
130 quad_info->display_rect = OverlayCandidate::GetOverlayRect(
131 draw_quad.quadTransform(), draw_quad.rect);
132 return true;
135 } // namespace cc