1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_SURFACE_ACCELERATED_SURFACE_WIN_H_
6 #define UI_SURFACE_ACCELERATED_SURFACE_WIN_H_
10 #include "base/callback_forward.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/synchronization/lock.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/time.h"
16 #include "base/win/scoped_comptr.h"
17 #include "ui/gfx/native_widget_types.h"
18 #include "ui/gfx/size.h"
19 #include "ui/surface/surface_export.h"
31 class SURFACE_EXPORT AcceleratedPresenter
32 : public base::RefCountedThreadSafe
<AcceleratedPresenter
> {
34 typedef base::Callback
<void(bool,
36 base::TimeDelta
)> CompletionTask
;
38 explicit AcceleratedPresenter(gfx::PluginWindowHandle window
);
40 // Returns a thread safe reference to the presenter for the given window or
41 // null is no such presenter exists. The thread safe refptr ensures the
42 // presenter will not be destroyed. This can be called on any thread.
43 static scoped_refptr
<AcceleratedPresenter
> GetForWindow(
44 gfx::PluginWindowHandle window
);
46 // Schedule a frame to be presented. The completion callback will be invoked
47 // when it is safe to write to the surface on another thread. The lock for
48 // this surface will be held while the completion callback runs. This can be
49 // called on any thread.
50 void AsyncPresentAndAcknowledge(
51 const gfx::Size
& size
,
53 const CompletionTask
& completion_task
);
55 // Schedule the presenter to free all its resources. This can be called on any
59 // Indicates that the presenter has become invisible.
62 // Called when the Windows session is locked or unlocked.
63 void SetIsSessionLocked(bool locked
);
65 // Schedule the presenter to release its reference to the shared surface.
66 void ReleaseSurface();
68 // The public member functions are called on the main thread.
70 void AsyncCopyTo(const gfx::Rect
& src_subrect
,
71 const gfx::Size
& dst_size
,
72 const base::Callback
<void(bool, const SkBitmap
&)>& callback
);
73 void AsyncCopyToVideoFrame(
74 const gfx::Rect
& src_subrect
,
75 const scoped_refptr
<media::VideoFrame
>& target
,
76 const base::Callback
<void(bool)>& callback
);
79 // Destroy any D3D resources owned by the given present thread. Called on
80 // the given present thread.
81 void ResetPresentThread(PresentThread
* present_thread
);
84 // TODO(scottmg): This is a temporary hack until we have a two-worlds ash/aura
86 void SetNewTargetWindow(gfx::PluginWindowHandle window
);
90 friend class base::RefCountedThreadSafe
<AcceleratedPresenter
>;
92 ~AcceleratedPresenter();
94 // These member functions are called on the PresentThread with which the
95 // presenter has affinity.
96 void DoPresentAndAcknowledge(
97 const gfx::Size
& size
,
99 const CompletionTask
& completion_task
);
101 void DoPresent(const base::Closure
& composite_task
);
102 void DoReleaseSurface();
103 void DoCopyToAndAcknowledge(
104 const gfx::Rect
& src_subrect
,
105 const gfx::Size
& dst_size
,
106 scoped_refptr
<base::SingleThreadTaskRunner
> callback_runner
,
107 const base::Callback
<void(bool, const SkBitmap
&)>& callback
);
108 void DoCopyToVideoFrameAndAcknowledge(
109 const gfx::Rect
& src_subrect
,
110 const scoped_refptr
<media::VideoFrame
>& target
,
111 const scoped_refptr
<base::SingleThreadTaskRunner
>& callback_runner
,
112 const base::Callback
<void(bool)>& callback
);
113 bool DoCopyToYUV(const gfx::Rect
& src_subrect
,
114 const scoped_refptr
<media::VideoFrame
>& frame
);
115 bool DoCopyToARGB(const gfx::Rect
& src_subrect
,
116 const gfx::Size
& dst_size
,
119 void PresentWithGDI(HDC dc
);
120 gfx::Size
GetWindowSize();
122 // This function tries to guess whether Direct3D will be able to reliably
123 // present to the window. When the window is resizing, presenting with
124 // Direct3D causes other regions of the window rendered with GDI to
125 // flicker transparent / non-transparent.
126 bool CheckDirect3DWillWork();
128 // The thread with which this presenter has affinity.
129 PresentThread
* const present_thread_
;
131 // The window that is presented to.
132 gfx::PluginWindowHandle window_
;
134 // UI thread can wait on this event to ensure a present is finished.
135 base::WaitableEvent event_
;
137 // The current size of the swap chain. This is only accessed on the thread
138 // with which the surface has affinity. The swap chain size is rounded up and
139 // is not necessarily the same as the window size.
140 gfx::Size quantized_size_
;
142 // The size of the window on the last present. This is used to trigger the
143 // compositor instead of presenting the last frame in the case where the
144 // window has been resized.
145 gfx::Size present_size_
;
147 // This is a shared texture that is being presented from.
148 base::win::ScopedComPtr
<IDirect3DTexture9
> source_texture_
;
150 // The swap chain is presented to the child window. Copy semantics
151 // are used so it is possible to represent it to quickly validate the window.
152 base::win::ScopedComPtr
<IDirect3DSwapChain9
> swap_chain_
;
154 // Whether the window is hidden or has not been presented to since it was
158 // Set to true if the first present after the tab is unhidden needs to be done
160 bool do_present_with_GDI_
;
162 // Set to true when the Windows session is locked.
163 bool is_session_locked_
;
165 // These are used to detect when the window is resizing. For some reason,
166 // presenting with D3D while the window resizes causes those parts not
167 // drawn with D3D (e.g. with GDI) to flicker visible / invisible.
168 // http://crbug.com/120904
169 gfx::Size last_window_size_
;
170 base::Time last_window_resize_time_
;
172 DISALLOW_COPY_AND_ASSIGN(AcceleratedPresenter
);
175 class SURFACE_EXPORT AcceleratedSurface
{
177 AcceleratedSurface(gfx::PluginWindowHandle window
);
178 ~AcceleratedSurface();
180 // Synchronously present a frame with no acknowledgement.
181 void Present(HDC dc
);
183 // Transfer the contents of the surface to an SkBitmap, and invoke a callback
185 void AsyncCopyTo(const gfx::Rect
& src_subrect
,
186 const gfx::Size
& dst_size
,
187 const base::Callback
<void(bool, const SkBitmap
&)>& callback
);
189 // Transfer the contents of the surface to an already-allocated YV12
190 // VideoFrame, and invoke a callback to indicate success or failure.
191 void AsyncCopyToVideoFrame(
192 const gfx::Rect
& src_subrect
,
193 const scoped_refptr
<media::VideoFrame
>& target
,
194 const base::Callback
<void(bool)>& callback
);
196 // Temporarily release resources until a new surface is asynchronously
197 // presented. Present will not be able to represent the last surface after
198 // calling this and will return false.
201 // Indicates that the surface has become invisible.
204 // Called when the Windows session in locked or unlocked.
205 void SetIsSessionLocked(bool locked
);
208 const scoped_refptr
<AcceleratedPresenter
> presenter_
;
209 DISALLOW_COPY_AND_ASSIGN(AcceleratedSurface
);
212 #endif // UI_SURFACE_ACCELERATED_SURFACE_WIN_H_