sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURER_HELPER_H_ |
| 12 | #define MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURER_HELPER_H_ |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 13 | |
kwiberg | 2bb3afa | 2016-03-16 22:58:08 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "modules/desktop_capture/desktop_geometry.h" |
| 17 | #include "modules/desktop_capture/desktop_region.h" |
Niels Möller | b0d83cd | 2020-10-28 14:18:56 | [diff] [blame] | 18 | #include "rtc_base/synchronization/mutex.h" |
| 19 | #include "rtc_base/thread_annotations.h" |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | // ScreenCapturerHelper is intended to be used by an implementation of the |
| 24 | // ScreenCapturer interface. It maintains a thread-safe invalid region, and |
| 25 | // the size of the most recently captured screen, on behalf of the |
| 26 | // ScreenCapturer that owns it. |
| 27 | class ScreenCapturerHelper { |
| 28 | public: |
Niels Möller | b0d83cd | 2020-10-28 14:18:56 | [diff] [blame] | 29 | ScreenCapturerHelper() = default; |
| 30 | ~ScreenCapturerHelper() = default; |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 31 | |
Byoungchan Lee | 604fd2f | 2022-01-21 00:49:39 | [diff] [blame] | 32 | ScreenCapturerHelper(const ScreenCapturerHelper&) = delete; |
| 33 | ScreenCapturerHelper& operator=(const ScreenCapturerHelper&) = delete; |
| 34 | |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 35 | // Clear out the invalid region. |
| 36 | void ClearInvalidRegion(); |
| 37 | |
| 38 | // Invalidate the specified region. |
| 39 | void InvalidateRegion(const DesktopRegion& invalid_region); |
| 40 | |
| 41 | // Invalidate the entire screen, of a given size. |
| 42 | void InvalidateScreen(const DesktopSize& size); |
| 43 | |
Artem Titov | cec4343 | 2021-07-28 21:35:39 | [diff] [blame] | 44 | // Copies current invalid region to `invalid_region` clears invalid region |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 45 | // storage for the next frame. |
| 46 | void TakeInvalidRegion(DesktopRegion* invalid_region); |
| 47 | |
| 48 | // Access the size of the most recently captured screen. |
| 49 | const DesktopSize& size_most_recent() const; |
| 50 | void set_size_most_recent(const DesktopSize& size); |
| 51 | |
| 52 | // Lossy compression can result in color values leaking between pixels in one |
| 53 | // block. If part of a block changes, then unchanged parts of that block can |
| 54 | // be changed in the compressed output. So we need to re-render an entire |
| 55 | // block whenever part of the block changes. |
| 56 | // |
Artem Titov | cec4343 | 2021-07-28 21:35:39 | [diff] [blame] | 57 | // If `log_grid_size` is >= 1, then this function makes TakeInvalidRegion() |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 58 | // produce an invalid region expanded so that its vertices lie on a grid of |
Artem Titov | cec4343 | 2021-07-28 21:35:39 | [diff] [blame] | 59 | // size 2 ^ `log_grid_size`. The expanded region is then clipped to the size |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 60 | // of the most recently captured screen, as previously set by |
| 61 | // set_size_most_recent(). |
Artem Titov | cec4343 | 2021-07-28 21:35:39 | [diff] [blame] | 62 | // If `log_grid_size` is <= 0, then the invalid region is not expanded. |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 63 | void SetLogGridSize(int log_grid_size); |
| 64 | |
| 65 | // Expands a region so that its vertices all lie on a grid. |
Artem Titov | cec4343 | 2021-07-28 21:35:39 | [diff] [blame] | 66 | // The grid size must be >= 2, so `log_grid_size` must be >= 1. |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 67 | static void ExpandToGrid(const DesktopRegion& region, |
| 68 | int log_grid_size, |
| 69 | DesktopRegion* result); |
| 70 | |
| 71 | private: |
| 72 | // A region that has been manually invalidated (through InvalidateRegion). |
| 73 | // These will be returned as dirty_region in the capture data during the next |
| 74 | // capture. |
Niels Möller | b0d83cd | 2020-10-28 14:18:56 | [diff] [blame] | 75 | DesktopRegion invalid_region_ RTC_GUARDED_BY(invalid_region_mutex_); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 76 | |
Artem Titov | cec4343 | 2021-07-28 21:35:39 | [diff] [blame] | 77 | // A lock protecting `invalid_region_` across threads. |
Niels Möller | b0d83cd | 2020-10-28 14:18:56 | [diff] [blame] | 78 | Mutex invalid_region_mutex_; |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 79 | |
| 80 | // The size of the most recently captured screen. |
| 81 | DesktopSize size_most_recent_; |
| 82 | |
| 83 | // The log (base 2) of the size of the grid to which the invalid region is |
| 84 | // expanded. |
| 85 | // If the value is <= 0, then the invalid region is not expanded to a grid. |
Niels Möller | b0d83cd | 2020-10-28 14:18:56 | [diff] [blame] | 86 | int log_grid_size_ = 0; |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace webrtc |
| 90 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 91 | #endif // MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURER_HELPER_H_ |