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