blob: 75f1128fe722b2bf05b3e4742e0048a75acdf9c6 [file] [log] [blame]
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/modules/desktop_capture/screen_capturer_helper.h"
12
henrike@webrtc.orgf2aafe42014-04-29 17:54:17 +000013#include <assert.h>
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000014#include <algorithm>
15
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000016namespace webrtc {
17
18ScreenCapturerHelper::ScreenCapturerHelper()
19 : invalid_region_lock_(RWLockWrapper::CreateRWLock()),
20 log_grid_size_(0) {
21}
22
23ScreenCapturerHelper::~ScreenCapturerHelper() {
24}
25
26void ScreenCapturerHelper::ClearInvalidRegion() {
27 WriteLockScoped scoped_invalid_region_lock(*invalid_region_lock_);
28 invalid_region_.Clear();
29}
30
31void ScreenCapturerHelper::InvalidateRegion(
32 const DesktopRegion& invalid_region) {
33 WriteLockScoped scoped_invalid_region_lock(*invalid_region_lock_);
34 invalid_region_.AddRegion(invalid_region);
35}
36
37void ScreenCapturerHelper::InvalidateScreen(const DesktopSize& size) {
38 WriteLockScoped scoped_invalid_region_lock(*invalid_region_lock_);
39 invalid_region_.AddRect(DesktopRect::MakeSize(size));
40}
41
42void ScreenCapturerHelper::TakeInvalidRegion(
43 DesktopRegion* invalid_region) {
44 invalid_region->Clear();
45
46 {
47 WriteLockScoped scoped_invalid_region_lock(*invalid_region_lock_);
48 invalid_region->Swap(&invalid_region_);
49 }
50
51 if (log_grid_size_ > 0) {
52 DesktopRegion expanded_region;
53 ExpandToGrid(*invalid_region, log_grid_size_, &expanded_region);
54 expanded_region.Swap(invalid_region);
55
56 invalid_region->IntersectWith(DesktopRect::MakeSize(size_most_recent_));
57 }
58}
59
60void ScreenCapturerHelper::SetLogGridSize(int log_grid_size) {
61 log_grid_size_ = log_grid_size;
62}
63
64const DesktopSize& ScreenCapturerHelper::size_most_recent() const {
65 return size_most_recent_;
66}
67
68void ScreenCapturerHelper::set_size_most_recent(
69 const DesktopSize& size) {
70 size_most_recent_ = size;
71}
72
73// Returns the largest multiple of |n| that is <= |x|.
74// |n| must be a power of 2. |nMask| is ~(|n| - 1).
75static int DownToMultiple(int x, int nMask) {
76 return (x & nMask);
77}
78
79// Returns the smallest multiple of |n| that is >= |x|.
80// |n| must be a power of 2. |nMask| is ~(|n| - 1).
81static int UpToMultiple(int x, int n, int nMask) {
82 return ((x + n - 1) & nMask);
83}
84
85void ScreenCapturerHelper::ExpandToGrid(const DesktopRegion& region,
86 int log_grid_size,
87 DesktopRegion* result) {
88 assert(log_grid_size >= 1);
89 int grid_size = 1 << log_grid_size;
90 int grid_size_mask = ~(grid_size - 1);
91
92 result->Clear();
93 for (DesktopRegion::Iterator it(region); !it.IsAtEnd(); it.Advance()) {
94 int left = DownToMultiple(it.rect().left(), grid_size_mask);
95 int right = UpToMultiple(it.rect().right(), grid_size, grid_size_mask);
96 int top = DownToMultiple(it.rect().top(), grid_size_mask);
97 int bottom = UpToMultiple(it.rect().bottom(), grid_size, grid_size_mask);
98 result->AddRect(DesktopRect::MakeLTRB(left, top, right, bottom));
99 }
100}
101
102} // namespace webrtc