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