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/constructor_magic.h"
19 #include "rtc_base/synchronization/rw_lock_wrapper.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();
30   ~ScreenCapturerHelper();
31 
32   // Clear out the invalid region.
33   void ClearInvalidRegion();
34 
35   // Invalidate the specified region.
36   void InvalidateRegion(const DesktopRegion& invalid_region);
37 
38   // Invalidate the entire screen, of a given size.
39   void InvalidateScreen(const DesktopSize& size);
40 
41   // Copies current invalid region to |invalid_region| clears invalid region
42   // storage for the next frame.
43   void TakeInvalidRegion(DesktopRegion* invalid_region);
44 
45   // Access the size of the most recently captured screen.
46   const DesktopSize& size_most_recent() const;
47   void set_size_most_recent(const DesktopSize& size);
48 
49   // Lossy compression can result in color values leaking between pixels in one
50   // block. If part of a block changes, then unchanged parts of that block can
51   // be changed in the compressed output. So we need to re-render an entire
52   // block whenever part of the block changes.
53   //
54   // If |log_grid_size| is >= 1, then this function makes TakeInvalidRegion()
55   // produce an invalid region expanded so that its vertices lie on a grid of
56   // size 2 ^ |log_grid_size|. The expanded region is then clipped to the size
57   // of the most recently captured screen, as previously set by
58   // set_size_most_recent().
59   // If |log_grid_size| is <= 0, then the invalid region is not expanded.
60   void SetLogGridSize(int log_grid_size);
61 
62   // Expands a region so that its vertices all lie on a grid.
63   // The grid size must be >= 2, so |log_grid_size| must be >= 1.
64   static void ExpandToGrid(const DesktopRegion& region,
65                            int log_grid_size,
66                            DesktopRegion* result);
67 
68  private:
69   // A region that has been manually invalidated (through InvalidateRegion).
70   // These will be returned as dirty_region in the capture data during the next
71   // capture.
72   DesktopRegion invalid_region_;
73 
74   // A lock protecting |invalid_region_| across threads.
75   std::unique_ptr<RWLockWrapper> invalid_region_lock_;
76 
77   // The size of the most recently captured screen.
78   DesktopSize size_most_recent_;
79 
80   // The log (base 2) of the size of the grid to which the invalid region is
81   // expanded.
82   // If the value is <= 0, then the invalid region is not expanded to a grid.
83   int log_grid_size_;
84 
85   RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerHelper);
86 };
87 
88 }  // namespace webrtc
89 
90 #endif  // MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURER_HELPER_H_
91