xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/screen_capturer_helper.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 <memory>
15 
16 #include "modules/desktop_capture/desktop_geometry.h"
17 #include "modules/desktop_capture/desktop_region.h"
18 #include "rtc_base/synchronization/mutex.h"
19 #include "rtc_base/thread_annotations.h"
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:
29   ScreenCapturerHelper() = default;
30   ~ScreenCapturerHelper() = default;
31 
32   ScreenCapturerHelper(const ScreenCapturerHelper&) = delete;
33   ScreenCapturerHelper& operator=(const ScreenCapturerHelper&) = delete;
34 
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 
44   // Copies current invalid region to `invalid_region` clears invalid region
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   //
57   // If `log_grid_size` is >= 1, then this function makes TakeInvalidRegion()
58   // produce an invalid region expanded so that its vertices lie on a grid of
59   // size 2 ^ `log_grid_size`. The expanded region is then clipped to the size
60   // of the most recently captured screen, as previously set by
61   // set_size_most_recent().
62   // If `log_grid_size` is <= 0, then the invalid region is not expanded.
63   void SetLogGridSize(int log_grid_size);
64 
65   // Expands a region so that its vertices all lie on a grid.
66   // The grid size must be >= 2, so `log_grid_size` must be >= 1.
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.
75   DesktopRegion invalid_region_ RTC_GUARDED_BY(invalid_region_mutex_);
76 
77   // A lock protecting `invalid_region_` across threads.
78   Mutex invalid_region_mutex_;
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.
86   int log_grid_size_ = 0;
87 };
88 
89 }  // namespace webrtc
90 
91 #endif  // MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURER_HELPER_H_
92