summaryrefslogtreecommitdiffstats
path: root/cc/output/overlay_strategy_sandwich.cc
blob: 6aef45df36a3bbcaee485cbdf1527154d81944f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/output/overlay_strategy_sandwich.h"

#include "cc/base/math_util.h"
#include "cc/base/region.h"
#include "cc/output/overlay_candidate_validator.h"
#include "cc/quads/draw_quad.h"
#include "cc/quads/solid_color_draw_quad.h"

namespace cc {

namespace {

void ClipDisplayAndUVRects(gfx::Rect* display_rect,
                           gfx::RectF* uv_rect,
                           const gfx::Rect& clip_rect) {
  gfx::Rect display_cropped_rect =
      gfx::IntersectRects(*display_rect, clip_rect);

  gfx::RectF uv_cropped_rect = gfx::RectF(display_cropped_rect);
  uv_cropped_rect -= gfx::Vector2dF(display_rect->x(), display_rect->y());
  uv_cropped_rect.Scale(uv_rect->width() / display_rect->width(),
                        uv_rect->height() / display_rect->height());
  uv_cropped_rect += gfx::Vector2dF(uv_rect->x(), uv_rect->y());

  *display_rect = display_cropped_rect;
  *uv_rect = uv_cropped_rect;
}

}  // namespace

OverlayStrategySandwich::OverlayStrategySandwich(
    OverlayCandidateValidator* capability_checker)
    : capability_checker_(capability_checker) {
  DCHECK(capability_checker);
}

OverlayStrategySandwich::~OverlayStrategySandwich() {}

bool OverlayStrategySandwich::Attempt(ResourceProvider* resource_provider,
                                      RenderPassList* render_passes,
                                      OverlayCandidateList* candidate_list,
                                      gfx::Rect* damage_rect) {
  QuadList& quad_list = render_passes->back()->quad_list;
  for (auto it = quad_list.begin(); it != quad_list.end();) {
    OverlayCandidate candidate;
    if (OverlayCandidate::FromDrawQuad(resource_provider, *it, &candidate)) {
      it = TryOverlay(render_passes->back().get(), candidate_list, candidate,
                      it);
    } else {
      ++it;
    }
  }

  return candidate_list->size() > 1;
}

QuadList::Iterator OverlayStrategySandwich::TryOverlay(
    RenderPass* render_pass,
    OverlayCandidateList* candidate_list,
    const OverlayCandidate& candidate,
    QuadList::Iterator candidate_iter_in_quad_list) {
  QuadList& quad_list = render_pass->quad_list;
  gfx::Rect pixel_bounds = render_pass->output_rect;

  const DrawQuad* candidate_quad = *candidate_iter_in_quad_list;
  const gfx::Transform& candidate_transform =
      candidate_quad->shared_quad_state->quad_to_target_transform;
  gfx::Transform candidate_inverse_transform;
  if (!candidate_transform.GetInverse(&candidate_inverse_transform))
    return ++candidate_iter_in_quad_list;

  // Compute the candidate's rect in display space (pixels on the screen).
  gfx::Rect candidate_pixel_rect = candidate.quad_rect_in_target_space;
  gfx::RectF candidate_uv_rect = candidate.uv_rect;
  if (candidate.is_clipped &&
      !candidate.clip_rect.Contains(candidate_pixel_rect)) {
    ClipDisplayAndUVRects(&candidate_pixel_rect, &candidate_uv_rect,
                          candidate.clip_rect);
  }

  // Don't allow overlapping overlays for now.
  for (const OverlayCandidate& other_candidate : *candidate_list) {
    if (other_candidate.display_rect.Intersects(candidate.display_rect) &&
        other_candidate.plane_z_order == 1) {
      return ++candidate_iter_in_quad_list;
    }
  }

  // Iterate through the quads in front of |candidate|, and compute the region
  // of |candidate| that is covered.
  Region pixel_covered_region;
  for (auto overlap_iter = quad_list.cbegin();
       overlap_iter != candidate_iter_in_quad_list; ++overlap_iter) {
    if (OverlayCandidate::IsInvisibleQuad(*overlap_iter))
      continue;
    // Compute the quad's bounds in display space.
    gfx::Rect pixel_covered_rect = MathUtil::MapEnclosingClippedRect(
        overlap_iter->shared_quad_state->quad_to_target_transform,
        overlap_iter->rect);

    // Include the intersection of that quad with the candidate's quad in the
    // covered region.
    pixel_covered_rect.Intersect(candidate_pixel_rect);
    pixel_covered_region.Union(pixel_covered_rect);
  }

  // Add the candidate's overlay.
  DCHECK(candidate.resource_id);
  OverlayCandidateList new_candidate_list = *candidate_list;
  new_candidate_list.push_back(candidate);
  OverlayCandidate& new_candidate = new_candidate_list.back();
  new_candidate.plane_z_order = 1;
  new_candidate.display_rect = gfx::RectF(candidate_pixel_rect);
  new_candidate.quad_rect_in_target_space = candidate_pixel_rect;
  new_candidate.uv_rect = candidate_uv_rect;

  // Add an overlay of the primary surface for any part of the candidate's
  // quad that was covered.
  std::vector<gfx::Rect> pixel_covered_rects;
  for (Region::Iterator it(pixel_covered_region); it.has_rect(); it.next())
    pixel_covered_rects.push_back(it.rect());
  for (const gfx::Rect& pixel_covered_rect : pixel_covered_rects) {
    OverlayCandidate main_image_on_top;
    main_image_on_top.display_rect = gfx::RectF(pixel_covered_rect);
    main_image_on_top.uv_rect = gfx::RectF(pixel_covered_rect);
    main_image_on_top.uv_rect.Scale(1.f / pixel_bounds.width(),
                                    1.f / pixel_bounds.height());
    main_image_on_top.plane_z_order = 2;
    main_image_on_top.transform = gfx::OVERLAY_TRANSFORM_NONE;
    main_image_on_top.use_output_surface_for_resource = true;
    new_candidate_list.push_back(main_image_on_top);
  }

  // Check for support.
  capability_checker_->CheckOverlaySupport(&new_candidate_list);
  for (const OverlayCandidate& candidate : new_candidate_list) {
    if (!candidate.overlay_handled)
      return ++candidate_iter_in_quad_list;
  }

  // Remove the quad for the overlay quad. Replace it with a transparent quad
  // if we're putting a new overlay on top.
  if (pixel_covered_rects.empty()) {
    candidate_iter_in_quad_list =
        quad_list.EraseAndInvalidateAllPointers(candidate_iter_in_quad_list);
  } else {
    // Cache the information from the candidate quad that we'll need to
    // construct the solid color quads.
    const SharedQuadState* candidate_shared_quad_state =
        candidate_quad->shared_quad_state;
    const gfx::Rect candidate_rect = candidate_quad->rect;

    // Reserve space in the quad list for the transparent quads.
    quad_list.ReplaceExistingElement<SolidColorDrawQuad>(
        candidate_iter_in_quad_list);
    candidate_iter_in_quad_list =
        quad_list.InsertBeforeAndInvalidateAllPointers<SolidColorDrawQuad>(
            candidate_iter_in_quad_list, pixel_covered_rects.size() - 1);

    // Cover the region with transparent quads.
    for (const gfx::Rect& pixel_covered_rect : pixel_covered_rects) {
      gfx::Rect quad_space_covered_rect = MathUtil::MapEnclosingClippedRect(
          candidate_inverse_transform, pixel_covered_rect);
      quad_space_covered_rect.Intersect(candidate_rect);

      SolidColorDrawQuad* transparent_quad =
          static_cast<SolidColorDrawQuad*>(*candidate_iter_in_quad_list);
      transparent_quad->SetAll(candidate_shared_quad_state,
                               quad_space_covered_rect, quad_space_covered_rect,
                               quad_space_covered_rect, false,
                               SK_ColorTRANSPARENT, true);
      ++candidate_iter_in_quad_list;
    }
  }

  candidate_list->swap(new_candidate_list);
  return candidate_iter_in_quad_list;
}

}  // namespace cc