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