1 /*
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 "modules/desktop_capture/screen_capturer_helper.h"
12
13
14 namespace webrtc {
15
ClearInvalidRegion()16 void ScreenCapturerHelper::ClearInvalidRegion() {
17 MutexLock scoped_invalid_region_lock(&invalid_region_mutex_);
18 invalid_region_.Clear();
19 }
20
InvalidateRegion(const DesktopRegion & invalid_region)21 void ScreenCapturerHelper::InvalidateRegion(
22 const DesktopRegion& invalid_region) {
23 MutexLock scoped_invalid_region_lock(&invalid_region_mutex_);
24 invalid_region_.AddRegion(invalid_region);
25 }
26
InvalidateScreen(const DesktopSize & size)27 void ScreenCapturerHelper::InvalidateScreen(const DesktopSize& size) {
28 MutexLock scoped_invalid_region_lock(&invalid_region_mutex_);
29 invalid_region_.AddRect(DesktopRect::MakeSize(size));
30 }
31
TakeInvalidRegion(DesktopRegion * invalid_region)32 void ScreenCapturerHelper::TakeInvalidRegion(DesktopRegion* invalid_region) {
33 invalid_region->Clear();
34
35 {
36 MutexLock scoped_invalid_region_lock(&invalid_region_mutex_);
37 invalid_region->Swap(&invalid_region_);
38 }
39
40 if (log_grid_size_ > 0) {
41 DesktopRegion expanded_region;
42 ExpandToGrid(*invalid_region, log_grid_size_, &expanded_region);
43 expanded_region.Swap(invalid_region);
44
45 invalid_region->IntersectWith(DesktopRect::MakeSize(size_most_recent_));
46 }
47 }
48
SetLogGridSize(int log_grid_size)49 void ScreenCapturerHelper::SetLogGridSize(int log_grid_size) {
50 log_grid_size_ = log_grid_size;
51 }
52
size_most_recent() const53 const DesktopSize& ScreenCapturerHelper::size_most_recent() const {
54 return size_most_recent_;
55 }
56
set_size_most_recent(const DesktopSize & size)57 void ScreenCapturerHelper::set_size_most_recent(const DesktopSize& size) {
58 size_most_recent_ = size;
59 }
60
61 // Returns the largest multiple of `n` that is <= `x`.
62 // `n` must be a power of 2. `nMask` is ~(`n` - 1).
DownToMultiple(int x,int nMask)63 static int DownToMultiple(int x, int nMask) {
64 return (x & nMask);
65 }
66
67 // Returns the smallest multiple of `n` that is >= `x`.
68 // `n` must be a power of 2. `nMask` is ~(`n` - 1).
UpToMultiple(int x,int n,int nMask)69 static int UpToMultiple(int x, int n, int nMask) {
70 return ((x + n - 1) & nMask);
71 }
72
ExpandToGrid(const DesktopRegion & region,int log_grid_size,DesktopRegion * result)73 void ScreenCapturerHelper::ExpandToGrid(const DesktopRegion& region,
74 int log_grid_size,
75 DesktopRegion* result) {
76 RTC_DCHECK_GE(log_grid_size, 1);
77 int grid_size = 1 << log_grid_size;
78 int grid_size_mask = ~(grid_size - 1);
79
80 result->Clear();
81 for (DesktopRegion::Iterator it(region); !it.IsAtEnd(); it.Advance()) {
82 int left = DownToMultiple(it.rect().left(), grid_size_mask);
83 int right = UpToMultiple(it.rect().right(), grid_size, grid_size_mask);
84 int top = DownToMultiple(it.rect().top(), grid_size_mask);
85 int bottom = UpToMultiple(it.rect().bottom(), grid_size, grid_size_mask);
86 result->AddRect(DesktopRect::MakeLTRB(left, top, right, bottom));
87 }
88 }
89
90 } // namespace webrtc
91