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