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