2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/desktop_capture/screen_capturer_helper.h"
16 #include "webrtc/system_wrappers/interface/logging.h"
20 ScreenCapturerHelper::ScreenCapturerHelper()
21 : invalid_region_lock_(RWLockWrapper::CreateRWLock()),
25 ScreenCapturerHelper::~ScreenCapturerHelper() {
28 void ScreenCapturerHelper::ClearInvalidRegion() {
29 WriteLockScoped scoped_invalid_region_lock(*invalid_region_lock_);
30 invalid_region_.Clear();
33 void ScreenCapturerHelper::InvalidateRegion(
34 const DesktopRegion& invalid_region) {
35 WriteLockScoped scoped_invalid_region_lock(*invalid_region_lock_);
36 invalid_region_.AddRegion(invalid_region);
39 void ScreenCapturerHelper::InvalidateScreen(const DesktopSize& size) {
40 WriteLockScoped scoped_invalid_region_lock(*invalid_region_lock_);
41 invalid_region_.AddRect(DesktopRect::MakeSize(size));
44 void ScreenCapturerHelper::TakeInvalidRegion(
45 DesktopRegion* invalid_region) {
46 invalid_region->Clear();
49 WriteLockScoped scoped_invalid_region_lock(*invalid_region_lock_);
50 invalid_region->Swap(&invalid_region_);
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);
58 invalid_region->IntersectWith(DesktopRect::MakeSize(size_most_recent_));
62 void ScreenCapturerHelper::SetLogGridSize(int log_grid_size) {
63 log_grid_size_ = log_grid_size;
66 const DesktopSize& ScreenCapturerHelper::size_most_recent() const {
67 return size_most_recent_;
70 void ScreenCapturerHelper::set_size_most_recent(
71 const DesktopSize& size) {
72 size_most_recent_ = size;
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) {
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);
87 void ScreenCapturerHelper::ExpandToGrid(const DesktopRegion& region,
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);
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));
104 } // namespace webrtc