Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / desktop_capture / win / screen_capturer_win_magnifier.h
1 /*
2  *  Copyright (c) 2014 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 WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_
12 #define WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_
13
14 #include <windows.h>
15 #include <magnification.h>
16 #include <wincodec.h>
17
18 #include "webrtc/base/constructormagic.h"
19 #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h"
20 #include "webrtc/modules/desktop_capture/screen_capturer.h"
21 #include "webrtc/modules/desktop_capture/screen_capturer_helper.h"
22 #include "webrtc/modules/desktop_capture/win/scoped_thread_desktop.h"
23 #include "webrtc/system_wrappers/interface/atomic32.h"
24 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
25
26 namespace webrtc {
27
28 class DesktopFrame;
29 class DesktopRect;
30 class Differ;
31
32 // Captures the screen using the Magnification API to support window exclusion.
33 // Each capturer must run on a dedicated thread because it uses thread local
34 // storage for redirecting the library callback. Also the thread must have a UI
35 // message loop to handle the window messages for the magnifier window.
36 class ScreenCapturerWinMagnifier : public ScreenCapturer {
37  public:
38   // |fallback_capturer| will be used to capture the screen if a non-primary
39   // screen is being captured, or the OS does not support Magnification API, or
40   // the magnifier capturer fails (e.g. in Windows8 Metro mode).
41   explicit ScreenCapturerWinMagnifier(
42       scoped_ptr<ScreenCapturer> fallback_capturer);
43   virtual ~ScreenCapturerWinMagnifier();
44
45   // Overridden from ScreenCapturer:
46   virtual void Start(Callback* callback) OVERRIDE;
47   virtual void Capture(const DesktopRegion& region) OVERRIDE;
48   virtual bool GetScreenList(ScreenList* screens) OVERRIDE;
49   virtual bool SelectScreen(ScreenId id) OVERRIDE;
50   virtual void SetExcludedWindow(WindowId window) OVERRIDE;
51
52  private:
53   typedef BOOL(WINAPI* MagImageScalingCallback)(HWND hwnd,
54                                                 void* srcdata,
55                                                 MAGIMAGEHEADER srcheader,
56                                                 void* destdata,
57                                                 MAGIMAGEHEADER destheader,
58                                                 RECT unclipped,
59                                                 RECT clipped,
60                                                 HRGN dirty);
61   typedef BOOL(WINAPI* MagInitializeFunc)(void);
62   typedef BOOL(WINAPI* MagUninitializeFunc)(void);
63   typedef BOOL(WINAPI* MagSetWindowSourceFunc)(HWND hwnd, RECT rect);
64   typedef BOOL(WINAPI* MagSetWindowFilterListFunc)(HWND hwnd,
65                                                    DWORD dwFilterMode,
66                                                    int count,
67                                                    HWND* pHWND);
68   typedef BOOL(WINAPI* MagSetImageScalingCallbackFunc)(
69       HWND hwnd,
70       MagImageScalingCallback callback);
71
72   static BOOL WINAPI OnMagImageScalingCallback(HWND hwnd,
73                                                void* srcdata,
74                                                MAGIMAGEHEADER srcheader,
75                                                void* destdata,
76                                                MAGIMAGEHEADER destheader,
77                                                RECT unclipped,
78                                                RECT clipped,
79                                                HRGN dirty);
80
81   // Captures the screen within |rect| in the desktop coordinates. Returns true
82   // if succeeded.
83   // It can only capture the primary screen for now. The magnification library
84   // crashes under some screen configurations (e.g. secondary screen on top of
85   // primary screen) if it tries to capture a non-primary screen. The caller
86   // must make sure not calling it on non-primary screens.
87   bool CaptureImage(const DesktopRect& rect);
88
89   // Helper method for setting up the magnifier control. Returns true if
90   // succeeded.
91   bool InitializeMagnifier();
92
93   // Called by OnMagImageScalingCallback to output captured data.
94   void OnCaptured(void* data, const MAGIMAGEHEADER& header);
95
96   // Makes sure the current frame exists and matches |size|.
97   void CreateCurrentFrameIfNecessary(const DesktopSize& size);
98
99   // Returns true if we are capturing the primary screen only.
100   bool IsCapturingPrimaryScreenOnly() const;
101
102   // Start the fallback capturer and select the screen.
103   void StartFallbackCapturer();
104
105   static Atomic32 tls_index_;
106
107   scoped_ptr<ScreenCapturer> fallback_capturer_;
108   bool fallback_capturer_started_;
109   Callback* callback_;
110   ScreenId current_screen_id_;
111   std::wstring current_device_key_;
112   HWND excluded_window_;
113
114   // A thread-safe list of invalid rectangles, and the size of the most
115   // recently captured screen.
116   ScreenCapturerHelper helper_;
117
118   // Queue of the frames buffers.
119   ScreenCaptureFrameQueue queue_;
120
121   // Class to calculate the difference between two screen bitmaps.
122   scoped_ptr<Differ> differ_;
123
124   // Used to suppress duplicate logging of SetThreadExecutionState errors.
125   bool set_thread_execution_state_failed_;
126
127   ScopedThreadDesktop desktop_;
128
129   // Used for getting the screen dpi.
130   HDC desktop_dc_;
131
132   HMODULE mag_lib_handle_;
133   MagInitializeFunc mag_initialize_func_;
134   MagUninitializeFunc mag_uninitialize_func_;
135   MagSetWindowSourceFunc set_window_source_func_;
136   MagSetWindowFilterListFunc set_window_filter_list_func_;
137   MagSetImageScalingCallbackFunc set_image_scaling_callback_func_;
138
139   // The hidden window hosting the magnifier control.
140   HWND host_window_;
141   // The magnifier control that captures the screen.
142   HWND magnifier_window_;
143
144   // True if the magnifier control has been successfully initialized.
145   bool magnifier_initialized_;
146
147   // True if the last OnMagImageScalingCallback was called and handled
148   // successfully. Reset at the beginning of each CaptureImage call.
149   bool magnifier_capture_succeeded_;
150
151   DISALLOW_COPY_AND_ASSIGN(ScreenCapturerWinMagnifier);
152 };
153
154 }  // namespace webrtc
155
156 #endif  // WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_