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