Roll src/third_party/WebKit 6f84130:7353389 (svn 184386:184391)
[chromium-blink-merge.git] / ui / gl / gl_surface_win.cc
blob8a0e87cb8dac4dee7586f175e7d02e086ae54352
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 #include "ui/gl/gl_surface.h"
7 #include <dwmapi.h>
9 #include "base/command_line.h"
10 #include "base/debug/trace_event.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/win/windows_version.h"
14 #include "ui/gfx/frame_time.h"
15 #include "ui/gfx/native_widget_types.h"
16 #include "ui/gl/gl_bindings.h"
17 #include "ui/gl/gl_implementation.h"
18 #include "ui/gl/gl_surface_egl.h"
19 #include "ui/gl/gl_surface_osmesa.h"
20 #include "ui/gl/gl_surface_stub.h"
21 #include "ui/gl/gl_surface_wgl.h"
23 // From ANGLE's egl/eglext.h.
24 #if !defined(EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE)
25 #define EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE \
26 reinterpret_cast<EGLNativeDisplayType>(-2)
27 #endif
28 #if !defined(EGL_PLATFORM_ANGLE_ANGLE)
29 #define EGL_PLATFORM_ANGLE_ANGLE 0x3201
30 #endif
31 #if !defined(EGL_PLATFORM_ANGLE_TYPE_ANGLE)
32 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3202
33 #endif
34 #if !defined(EGL_PLATFORM_ANGLE_TYPE_D3D11_WARP_ANGLE)
35 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_WARP_ANGLE 0x3206
36 #endif
38 namespace gfx {
40 // This OSMesa GL surface can use GDI to swap the contents of the buffer to a
41 // view.
42 class NativeViewGLSurfaceOSMesa : public GLSurfaceOSMesa {
43 public:
44 explicit NativeViewGLSurfaceOSMesa(gfx::AcceleratedWidget window);
45 virtual ~NativeViewGLSurfaceOSMesa();
47 // Implement subset of GLSurface.
48 virtual bool Initialize() override;
49 virtual void Destroy() override;
50 virtual bool IsOffscreen() override;
51 virtual bool SwapBuffers() override;
52 virtual bool SupportsPostSubBuffer() override;
53 virtual bool PostSubBuffer(int x, int y, int width, int height) override;
55 private:
56 gfx::AcceleratedWidget window_;
57 HDC device_context_;
59 DISALLOW_COPY_AND_ASSIGN(NativeViewGLSurfaceOSMesa);
62 class WinVSyncProvider : public VSyncProvider {
63 public:
64 explicit WinVSyncProvider(gfx::AcceleratedWidget window) :
65 window_(window)
67 use_dwm_ = (base::win::GetVersion() >= base::win::VERSION_VISTA);
70 virtual ~WinVSyncProvider() {}
72 virtual void GetVSyncParameters(const UpdateVSyncCallback& callback) {
73 TRACE_EVENT0("gpu", "WinVSyncProvider::GetVSyncParameters");
75 base::TimeTicks timebase;
76 base::TimeDelta interval;
77 bool dwm_active = false;
79 // Query the DWM timing info first if available. This will provide the most
80 // precise values.
81 if (use_dwm_) {
82 DWM_TIMING_INFO timing_info;
83 timing_info.cbSize = sizeof(timing_info);
84 HRESULT result = DwmGetCompositionTimingInfo(NULL, &timing_info);
85 if (result == S_OK) {
86 dwm_active = true;
87 if (gfx::FrameTime::TimestampsAreHighRes()) {
88 // qpcRefreshPeriod is very accurate but noisy, and must be used with
89 // a high resolution timebase to avoid frequently missing Vsync.
90 timebase = gfx::FrameTime::FromQPCValue(
91 static_cast<LONGLONG>(timing_info.qpcVBlank));
92 interval = base::TimeDelta::FromQPCValue(
93 static_cast<LONGLONG>(timing_info.qpcRefreshPeriod));
94 } else if (timing_info.rateRefresh.uiDenominator > 0 &&
95 timing_info.rateRefresh.uiNumerator > 0) {
96 // If FrameTime is not high resolution, we do not want to translate
97 // the QPC value provided by DWM into the low-resolution timebase,
98 // which would be error prone and jittery. As a fallback, we assume
99 // the timebase is zero and use rateRefresh, which may be rounded but
100 // isn't noisy like qpcRefreshPeriod, instead. The fact that we don't
101 // have a timebase here may lead to brief periods of jank when our
102 // scheduling becomes offset from the hardware vsync.
104 // Swap the numerator/denominator to convert frequency to period.
105 interval = base::TimeDelta::FromMicroseconds(
106 timing_info.rateRefresh.uiDenominator *
107 base::Time::kMicrosecondsPerSecond /
108 timing_info.rateRefresh.uiNumerator);
113 if (!dwm_active) {
114 // When DWM compositing is active all displays are normalized to the
115 // refresh rate of the primary display, and won't composite any faster.
116 // If DWM compositing is disabled, though, we can use the refresh rates
117 // reported by each display, which will help systems that have mis-matched
118 // displays that run at different frequencies.
119 HMONITOR monitor = MonitorFromWindow(window_, MONITOR_DEFAULTTONEAREST);
120 MONITORINFOEX monitor_info;
121 monitor_info.cbSize = sizeof(MONITORINFOEX);
122 BOOL result = GetMonitorInfo(monitor, &monitor_info);
123 if (result) {
124 DEVMODE display_info;
125 display_info.dmSize = sizeof(DEVMODE);
126 display_info.dmDriverExtra = 0;
127 result = EnumDisplaySettings(monitor_info.szDevice,
128 ENUM_CURRENT_SETTINGS, &display_info);
129 if (result && display_info.dmDisplayFrequency > 1) {
130 interval = base::TimeDelta::FromMicroseconds(
131 (1.0 / static_cast<double>(display_info.dmDisplayFrequency)) *
132 base::Time::kMicrosecondsPerSecond);
137 if (interval.ToInternalValue() != 0) {
138 callback.Run(timebase, interval);
142 private:
143 DISALLOW_COPY_AND_ASSIGN(WinVSyncProvider);
145 gfx::AcceleratedWidget window_;
146 bool use_dwm_;
149 // Helper routine that does one-off initialization like determining the
150 // pixel format.
151 bool GLSurface::InitializeOneOffInternal() {
152 switch (GetGLImplementation()) {
153 case kGLImplementationDesktopGL:
154 if (!GLSurfaceWGL::InitializeOneOff()) {
155 LOG(ERROR) << "GLSurfaceWGL::InitializeOneOff failed.";
156 return false;
158 break;
159 case kGLImplementationEGLGLES2:
160 if (!GLSurfaceEGL::InitializeOneOff()) {
161 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
162 return false;
164 break;
166 return true;
169 NativeViewGLSurfaceOSMesa::NativeViewGLSurfaceOSMesa(
170 gfx::AcceleratedWidget window)
171 : GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA, gfx::Size(1, 1)),
172 window_(window),
173 device_context_(NULL) {
174 DCHECK(window);
177 NativeViewGLSurfaceOSMesa::~NativeViewGLSurfaceOSMesa() {
178 Destroy();
181 bool NativeViewGLSurfaceOSMesa::Initialize() {
182 if (!GLSurfaceOSMesa::Initialize())
183 return false;
185 device_context_ = GetDC(window_);
186 return true;
189 void NativeViewGLSurfaceOSMesa::Destroy() {
190 if (window_ && device_context_)
191 ReleaseDC(window_, device_context_);
193 device_context_ = NULL;
195 GLSurfaceOSMesa::Destroy();
198 bool NativeViewGLSurfaceOSMesa::IsOffscreen() {
199 return false;
202 bool NativeViewGLSurfaceOSMesa::SwapBuffers() {
203 DCHECK(device_context_);
205 gfx::Size size = GetSize();
207 // Note: negating the height below causes GDI to treat the bitmap data as row
208 // 0 being at the top.
209 BITMAPV4HEADER info = { sizeof(BITMAPV4HEADER) };
210 info.bV4Width = size.width();
211 info.bV4Height = -size.height();
212 info.bV4Planes = 1;
213 info.bV4BitCount = 32;
214 info.bV4V4Compression = BI_BITFIELDS;
215 info.bV4RedMask = 0x000000FF;
216 info.bV4GreenMask = 0x0000FF00;
217 info.bV4BlueMask = 0x00FF0000;
218 info.bV4AlphaMask = 0xFF000000;
220 // Copy the back buffer to the window's device context. Do not check whether
221 // StretchDIBits succeeds or not. It will fail if the window has been
222 // destroyed but it is preferable to allow rendering to silently fail if the
223 // window is destroyed. This is because the primary application of this
224 // class of GLContext is for testing and we do not want every GL related ui /
225 // browser test to become flaky if there is a race condition between GL
226 // context destruction and window destruction.
227 StretchDIBits(device_context_,
228 0, 0, size.width(), size.height(),
229 0, 0, size.width(), size.height(),
230 GetHandle(),
231 reinterpret_cast<BITMAPINFO*>(&info),
232 DIB_RGB_COLORS,
233 SRCCOPY);
235 return true;
238 bool NativeViewGLSurfaceOSMesa::SupportsPostSubBuffer() {
239 return true;
242 bool NativeViewGLSurfaceOSMesa::PostSubBuffer(
243 int x, int y, int width, int height) {
244 DCHECK(device_context_);
246 gfx::Size size = GetSize();
248 // Note: negating the height below causes GDI to treat the bitmap data as row
249 // 0 being at the top.
250 BITMAPV4HEADER info = { sizeof(BITMAPV4HEADER) };
251 info.bV4Width = size.width();
252 info.bV4Height = -size.height();
253 info.bV4Planes = 1;
254 info.bV4BitCount = 32;
255 info.bV4V4Compression = BI_BITFIELDS;
256 info.bV4RedMask = 0x000000FF;
257 info.bV4GreenMask = 0x0000FF00;
258 info.bV4BlueMask = 0x00FF0000;
259 info.bV4AlphaMask = 0xFF000000;
261 // Copy the back buffer to the window's device context. Do not check whether
262 // StretchDIBits succeeds or not. It will fail if the window has been
263 // destroyed but it is preferable to allow rendering to silently fail if the
264 // window is destroyed. This is because the primary application of this
265 // class of GLContext is for testing and we do not want every GL related ui /
266 // browser test to become flaky if there is a race condition between GL
267 // context destruction and window destruction.
268 StretchDIBits(device_context_,
269 x, size.height() - y - height, width, height,
270 x, y, width, height,
271 GetHandle(),
272 reinterpret_cast<BITMAPINFO*>(&info),
273 DIB_RGB_COLORS,
274 SRCCOPY);
276 return true;
279 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface(
280 gfx::AcceleratedWidget window) {
281 TRACE_EVENT0("gpu", "GLSurface::CreateViewGLSurface");
282 switch (GetGLImplementation()) {
283 case kGLImplementationOSMesaGL: {
284 scoped_refptr<GLSurface> surface(
285 new NativeViewGLSurfaceOSMesa(window));
286 if (!surface->Initialize())
287 return NULL;
289 return surface;
291 case kGLImplementationEGLGLES2: {
292 DCHECK(window != gfx::kNullAcceleratedWidget);
293 scoped_refptr<NativeViewGLSurfaceEGL> surface(
294 new NativeViewGLSurfaceEGL(window));
295 scoped_ptr<VSyncProvider> sync_provider;
296 sync_provider.reset(new WinVSyncProvider(window));
297 if (!surface->Initialize(sync_provider.Pass()))
298 return NULL;
300 return surface;
302 case kGLImplementationDesktopGL: {
303 scoped_refptr<GLSurface> surface(new NativeViewGLSurfaceWGL(
304 window));
305 if (!surface->Initialize())
306 return NULL;
308 return surface;
310 case kGLImplementationMockGL:
311 return new GLSurfaceStub;
312 default:
313 NOTREACHED();
314 return NULL;
318 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface(
319 const gfx::Size& size) {
320 TRACE_EVENT0("gpu", "GLSurface::CreateOffscreenGLSurface");
321 switch (GetGLImplementation()) {
322 case kGLImplementationOSMesaGL: {
323 scoped_refptr<GLSurface> surface(
324 new GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA, size));
325 if (!surface->Initialize())
326 return NULL;
328 return surface;
330 case kGLImplementationEGLGLES2: {
331 scoped_refptr<GLSurface> surface(new PbufferGLSurfaceEGL(size));
332 if (!surface->Initialize())
333 return NULL;
335 return surface;
337 case kGLImplementationDesktopGL: {
338 scoped_refptr<GLSurface> surface(new PbufferGLSurfaceWGL(size));
339 if (!surface->Initialize())
340 return NULL;
342 return surface;
344 case kGLImplementationMockGL:
345 return new GLSurfaceStub;
346 default:
347 NOTREACHED();
348 return NULL;
352 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
353 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableD3D11) ||
354 CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseWarp))
355 return GetDC(NULL);
356 return EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE;
359 } // namespace gfx