1 /*
2  *  Copyright (c) 2016 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_DESKTOP_FRAME_GENERATOR_H_
12 #define MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_GENERATOR_H_
13 
14 #include <memory>
15 
16 #include "modules/desktop_capture/desktop_frame.h"
17 #include "modules/desktop_capture/desktop_geometry.h"
18 #include "modules/desktop_capture/desktop_region.h"
19 #include "modules/desktop_capture/shared_memory.h"
20 
21 namespace webrtc {
22 
23 // An interface to generate a DesktopFrame.
24 class DesktopFrameGenerator {
25  public:
26   DesktopFrameGenerator();
27   virtual ~DesktopFrameGenerator();
28 
29   virtual std::unique_ptr<DesktopFrame> GetNextFrame(
30       SharedMemoryFactory* factory) = 0;
31 };
32 
33 // An interface to paint a DesktopFrame. This interface is used by
34 // PainterDesktopFrameGenerator.
35 class DesktopFramePainter {
36  public:
37   DesktopFramePainter();
38   virtual ~DesktopFramePainter();
39 
40   virtual bool Paint(DesktopFrame* frame, DesktopRegion* updated_region) = 0;
41 };
42 
43 // An implementation of DesktopFrameGenerator to take care about the
44 // DesktopFrame size, filling updated_region(), etc, but leaves the real
45 // painting work to a DesktopFramePainter implementation.
46 class PainterDesktopFrameGenerator final : public DesktopFrameGenerator {
47  public:
48   PainterDesktopFrameGenerator();
49   ~PainterDesktopFrameGenerator() override;
50 
51   std::unique_ptr<DesktopFrame> GetNextFrame(
52       SharedMemoryFactory* factory) override;
53 
54   // Sets the size of the frame which will be returned in next GetNextFrame()
55   // call.
56   DesktopSize* size();
57 
58   // Decides whether BaseDesktopFrameGenerator returns a frame in next Capture()
59   // callback. If return_frame_ is true, BaseDesktopFrameGenerator will create a
60   // frame according to both size_ and SharedMemoryFactory input, and uses
61   // Paint() function to paint it.
62   void set_return_frame(bool return_frame);
63 
64   // Decides whether MockScreenCapturer returns a frame with updated regions.
65   // MockScreenCapturer will keep DesktopFrame::updated_region() empty if this
66   // field is false.
67   void set_provide_updated_region_hints(bool provide_updated_region_hints);
68 
69   // Decides whether MockScreenCapturer randomly enlarges updated regions in the
70   // DesktopFrame. Set this field to true to simulate an inaccurate updated
71   // regions' return from OS APIs.
72   void set_enlarge_updated_region(bool enlarge_updated_region);
73 
74   // The range to enlarge a updated region if |enlarge_updated_region_| is true.
75   // If this field is less than zero, it will be treated as zero, and
76   // |enlarge_updated_region_| will be ignored.
77   void set_enlarge_range(int enlarge_range);
78 
79   // Decides whether BaseDesktopFrameGenerator randomly add some updated regions
80   // in the DesktopFrame. Set this field to true to simulate an inaccurate
81   // updated regions' return from OS APIs.
82   void set_add_random_updated_region(bool add_random_updated_region);
83 
84   // Sets the painter object to do the real painting work, if no |painter_| has
85   // been set to this instance, the DesktopFrame returned by GetNextFrame()
86   // function will keep in an undefined but valid state.
87   // PainterDesktopFrameGenerator does not take ownership of the |painter|.
88   void set_desktop_frame_painter(DesktopFramePainter* painter);
89 
90  private:
91   DesktopSize size_;
92   bool return_frame_;
93   bool provide_updated_region_hints_;
94   bool enlarge_updated_region_;
95   int enlarge_range_;
96   bool add_random_updated_region_;
97   DesktopFramePainter* painter_;
98 };
99 
100 // An implementation of DesktopFramePainter to paint black on
101 // mutable_updated_region(), and white elsewhere.
102 class BlackWhiteDesktopFramePainter final : public DesktopFramePainter {
103  public:
104   BlackWhiteDesktopFramePainter();
105   ~BlackWhiteDesktopFramePainter() override;
106 
107   // The black regions of the frame which will be returned in next Paint()
108   // call. BlackWhiteDesktopFramePainter will draw a white frame, with black
109   // in the updated_region_. Each Paint() call will consume updated_region_.
110   DesktopRegion* updated_region();
111 
112   // DesktopFramePainter interface.
113   bool Paint(DesktopFrame* frame, DesktopRegion* updated_region) override;
114 
115  private:
116   DesktopRegion updated_region_;
117 };
118 
119 }  // namespace webrtc
120 
121 #endif  // MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_GENERATOR_H_
122