summaryrefslogtreecommitdiffstats
path: root/cc/trees/occlusion.cc
blob: e86a1a05e32cffd9d5946dffe5c1a8ddcaa11a30 (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
// Copyright 2014 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/trees/occlusion.h"

#include "cc/base/math_util.h"
#include "ui/gfx/geometry/rect.h"

namespace cc {

Occlusion::Occlusion() {
}

Occlusion::Occlusion(const gfx::Transform& draw_transform,
                     const SimpleEnclosedRegion& occlusion_from_outside_target,
                     const SimpleEnclosedRegion& occlusion_from_inside_target)
    : draw_transform_(draw_transform),
      occlusion_from_outside_target_(occlusion_from_outside_target),
      occlusion_from_inside_target_(occlusion_from_inside_target) {
}

Occlusion Occlusion::GetOcclusionWithGivenDrawTransform(
    const gfx::Transform& transform) const {
  return Occlusion(
      transform, occlusion_from_outside_target_, occlusion_from_inside_target_);
}

bool Occlusion::HasOcclusion() const {
  return !occlusion_from_inside_target_.IsEmpty() ||
         !occlusion_from_outside_target_.IsEmpty();
}

bool Occlusion::IsOccluded(const gfx::Rect& content_rect) const {
  if (content_rect.IsEmpty())
    return true;

  if (!HasOcclusion())
    return false;

  gfx::Rect unoccluded_rect_in_target_surface =
      GetUnoccludedRectInTargetSurface(content_rect);
  return unoccluded_rect_in_target_surface.IsEmpty();
}

gfx::Rect Occlusion::GetUnoccludedContentRect(
    const gfx::Rect& content_rect) const {
  if (content_rect.IsEmpty())
    return content_rect;

  if (!HasOcclusion())
    return content_rect;

  gfx::Rect unoccluded_rect_in_target_surface =
      GetUnoccludedRectInTargetSurface(content_rect);
  if (unoccluded_rect_in_target_surface.IsEmpty())
    return gfx::Rect();

  gfx::Transform inverse_draw_transform(gfx::Transform::kSkipInitialization);
  bool ok = draw_transform_.GetInverse(&inverse_draw_transform);
  // TODO(ajuma): Skip drawing layers with uninvertible draw transforms, and
  // change this to a DCHECK. crbug.com/517170
  if (!ok)
    return content_rect;

  gfx::Rect unoccluded_rect = MathUtil::ProjectEnclosingClippedRect(
      inverse_draw_transform, unoccluded_rect_in_target_surface);
  unoccluded_rect.Intersect(content_rect);

  return unoccluded_rect;
}

gfx::Rect Occlusion::GetUnoccludedRectInTargetSurface(
    const gfx::Rect& content_rect) const {
  // Take the ToEnclosingRect at each step, as we want to contain any unoccluded
  // partial pixels in the resulting Rect.
  gfx::Rect unoccluded_rect_in_target_surface =
      MathUtil::MapEnclosingClippedRect(draw_transform_, content_rect);
  DCHECK_LE(occlusion_from_inside_target_.GetRegionComplexity(), 1u);
  DCHECK_LE(occlusion_from_outside_target_.GetRegionComplexity(), 1u);
  // These subtract operations are more lossy than if we did both operations at
  // once.
  unoccluded_rect_in_target_surface.Subtract(
      occlusion_from_inside_target_.bounds());
  unoccluded_rect_in_target_surface.Subtract(
      occlusion_from_outside_target_.bounds());

  return unoccluded_rect_in_target_surface;
}

bool Occlusion::IsEqual(const Occlusion& other) const {
  return draw_transform_ == other.draw_transform_ &&
         occlusion_from_inside_target_ == other.occlusion_from_inside_target_ &&
         occlusion_from_outside_target_ == other.occlusion_from_outside_target_;
}

std::string Occlusion::ToString() const {
  return draw_transform_.ToString() + "outside(" +
         occlusion_from_outside_target_.ToString() + ") inside(" +
         occlusion_from_inside_target_.ToString() + ")";
}

}  // namespace cc