Roll src/breakpad/src 3ea146d:57c3d7c (svn 1405:1407)
[chromium-blink-merge.git] / ui / gl / gl_surface_win.cc
blob5cf68afac762b24aa7af01059aa4470bd57e086e
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_ANGLE)
35 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3207
36 #endif
37 #if !defined(EGL_PLATFORM_ANGLE_USE_WARP_ANGLE)
38 #define EGL_PLATFORM_ANGLE_USE_WARP_ANGLE 0x3208
39 #endif
41 namespace gfx {
43 // This OSMesa GL surface can use GDI to swap the contents of the buffer to a
44 // view.
45 class NativeViewGLSurfaceOSMesa : public GLSurfaceOSMesa {
46 public:
47 explicit NativeViewGLSurfaceOSMesa(gfx::AcceleratedWidget window);
48 virtual ~NativeViewGLSurfaceOSMesa();
50 // Implement subset of GLSurface.
51 virtual bool Initialize() override;
52 virtual void Destroy() override;
53 virtual bool IsOffscreen() override;
54 virtual bool SwapBuffers() override;
55 virtual bool SupportsPostSubBuffer() override;
56 virtual bool PostSubBuffer(int x, int y, int width, int height) override;
58 private:
59 gfx::AcceleratedWidget window_;
60 HDC device_context_;
62 DISALLOW_COPY_AND_ASSIGN(NativeViewGLSurfaceOSMesa);
65 class WinVSyncProvider : public VSyncProvider {
66 public:
67 explicit WinVSyncProvider(gfx::AcceleratedWidget window) :
68 window_(window)
70 use_dwm_ = (base::win::GetVersion() >= base::win::VERSION_WIN7);
73 virtual ~WinVSyncProvider() {}
75 virtual void GetVSyncParameters(const UpdateVSyncCallback& callback) {
76 TRACE_EVENT0("gpu", "WinVSyncProvider::GetVSyncParameters");
78 base::TimeTicks timebase;
79 base::TimeDelta interval;
80 bool dwm_active = false;
82 // Query the DWM timing info first if available. This will provide the most
83 // precise values.
84 if (use_dwm_) {
85 DWM_TIMING_INFO timing_info;
86 timing_info.cbSize = sizeof(timing_info);
87 HRESULT result = DwmGetCompositionTimingInfo(NULL, &timing_info);
88 if (result == S_OK) {
89 dwm_active = true;
90 if (gfx::FrameTime::TimestampsAreHighRes()) {
91 // qpcRefreshPeriod is very accurate but noisy, and must be used with
92 // a high resolution timebase to avoid frequently missing Vsync.
93 timebase = gfx::FrameTime::FromQPCValue(
94 static_cast<LONGLONG>(timing_info.qpcVBlank));
95 interval = base::TimeDelta::FromQPCValue(
96 static_cast<LONGLONG>(timing_info.qpcRefreshPeriod));
97 } else if (timing_info.rateRefresh.uiDenominator > 0 &&
98 timing_info.rateRefresh.uiNumerator > 0) {
99 // If FrameTime is not high resolution, we do not want to translate
100 // the QPC value provided by DWM into the low-resolution timebase,
101 // which would be error prone and jittery. As a fallback, we assume
102 // the timebase is zero and use rateRefresh, which may be rounded but
103 // isn't noisy like qpcRefreshPeriod, instead. The fact that we don't
104 // have a timebase here may lead to brief periods of jank when our
105 // scheduling becomes offset from the hardware vsync.
107 // Swap the numerator/denominator to convert frequency to period.
108 interval = base::TimeDelta::FromMicroseconds(
109 timing_info.rateRefresh.uiDenominator *
110 base::Time::kMicrosecondsPerSecond /
111 timing_info.rateRefresh.uiNumerator);
116 if (!dwm_active) {
117 // When DWM compositing is active all displays are normalized to the
118 // refresh rate of the primary display, and won't composite any faster.
119 // If DWM compositing is disabled, though, we can use the refresh rates
120 // reported by each display, which will help systems that have mis-matched
121 // displays that run at different frequencies.
122 HMONITOR monitor = MonitorFromWindow(window_, MONITOR_DEFAULTTONEAREST);
123 MONITORINFOEX monitor_info;
124 monitor_info.cbSize = sizeof(MONITORINFOEX);
125 BOOL result = GetMonitorInfo(monitor, &monitor_info);
126 if (result) {
127 DEVMODE display_info;
128 display_info.dmSize = sizeof(DEVMODE);
129 display_info.dmDriverExtra = 0;
130 result = EnumDisplaySettings(monitor_info.szDevice,
131 ENUM_CURRENT_SETTINGS, &display_info);
132 if (result && display_info.dmDisplayFrequency > 1) {
133 interval = base::TimeDelta::FromMicroseconds(
134 (1.0 / static_cast<double>(display_info.dmDisplayFrequency)) *
135 base::Time::kMicrosecondsPerSecond);
140 if (interval.ToInternalValue() != 0) {
141 callback.Run(timebase, interval);
145 private:
146 DISALLOW_COPY_AND_ASSIGN(WinVSyncProvider);
148 gfx::AcceleratedWidget window_;
149 bool use_dwm_;
152 // Helper routine that does one-off initialization like determining the
153 // pixel format.
154 bool GLSurface::InitializeOneOffInternal() {
155 switch (GetGLImplementation()) {
156 case kGLImplementationDesktopGL:
157 if (!GLSurfaceWGL::InitializeOneOff()) {
158 LOG(ERROR) << "GLSurfaceWGL::InitializeOneOff failed.";
159 return false;
161 break;
162 case kGLImplementationEGLGLES2:
163 if (!GLSurfaceEGL::InitializeOneOff()) {
164 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
165 return false;
167 break;
169 return true;
172 NativeViewGLSurfaceOSMesa::NativeViewGLSurfaceOSMesa(
173 gfx::AcceleratedWidget window)
174 : GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA, gfx::Size(1, 1)),
175 window_(window),
176 device_context_(NULL) {
177 DCHECK(window);
180 NativeViewGLSurfaceOSMesa::~NativeViewGLSurfaceOSMesa() {
181 Destroy();
184 bool NativeViewGLSurfaceOSMesa::Initialize() {
185 if (!GLSurfaceOSMesa::Initialize())
186 return false;
188 device_context_ = GetDC(window_);
189 return true;
192 void NativeViewGLSurfaceOSMesa::Destroy() {
193 if (window_ && device_context_)
194 ReleaseDC(window_, device_context_);
196 device_context_ = NULL;
198 GLSurfaceOSMesa::Destroy();
201 bool NativeViewGLSurfaceOSMesa::IsOffscreen() {
202 return false;
205 bool NativeViewGLSurfaceOSMesa::SwapBuffers() {
206 DCHECK(device_context_);
208 gfx::Size size = GetSize();
210 // Note: negating the height below causes GDI to treat the bitmap data as row
211 // 0 being at the top.
212 BITMAPV4HEADER info = { sizeof(BITMAPV4HEADER) };
213 info.bV4Width = size.width();
214 info.bV4Height = -size.height();
215 info.bV4Planes = 1;
216 info.bV4BitCount = 32;
217 info.bV4V4Compression = BI_BITFIELDS;
218 info.bV4RedMask = 0x000000FF;
219 info.bV4GreenMask = 0x0000FF00;
220 info.bV4BlueMask = 0x00FF0000;
221 info.bV4AlphaMask = 0xFF000000;
223 // Copy the back buffer to the window's device context. Do not check whether
224 // StretchDIBits succeeds or not. It will fail if the window has been
225 // destroyed but it is preferable to allow rendering to silently fail if the
226 // window is destroyed. This is because the primary application of this
227 // class of GLContext is for testing and we do not want every GL related ui /
228 // browser test to become flaky if there is a race condition between GL
229 // context destruction and window destruction.
230 StretchDIBits(device_context_,
231 0, 0, size.width(), size.height(),
232 0, 0, size.width(), size.height(),
233 GetHandle(),
234 reinterpret_cast<BITMAPINFO*>(&info),
235 DIB_RGB_COLORS,
236 SRCCOPY);
238 return true;
241 bool NativeViewGLSurfaceOSMesa::SupportsPostSubBuffer() {
242 return true;
245 bool NativeViewGLSurfaceOSMesa::PostSubBuffer(
246 int x, int y, int width, int height) {
247 DCHECK(device_context_);
249 gfx::Size size = GetSize();
251 // Note: negating the height below causes GDI to treat the bitmap data as row
252 // 0 being at the top.
253 BITMAPV4HEADER info = { sizeof(BITMAPV4HEADER) };
254 info.bV4Width = size.width();
255 info.bV4Height = -size.height();
256 info.bV4Planes = 1;
257 info.bV4BitCount = 32;
258 info.bV4V4Compression = BI_BITFIELDS;
259 info.bV4RedMask = 0x000000FF;
260 info.bV4GreenMask = 0x0000FF00;
261 info.bV4BlueMask = 0x00FF0000;
262 info.bV4AlphaMask = 0xFF000000;
264 // Copy the back buffer to the window's device context. Do not check whether
265 // StretchDIBits succeeds or not. It will fail if the window has been
266 // destroyed but it is preferable to allow rendering to silently fail if the
267 // window is destroyed. This is because the primary application of this
268 // class of GLContext is for testing and we do not want every GL related ui /
269 // browser test to become flaky if there is a race condition between GL
270 // context destruction and window destruction.
271 StretchDIBits(device_context_,
272 x, size.height() - y - height, width, height,
273 x, y, width, height,
274 GetHandle(),
275 reinterpret_cast<BITMAPINFO*>(&info),
276 DIB_RGB_COLORS,
277 SRCCOPY);
279 return true;
282 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface(
283 gfx::AcceleratedWidget window) {
284 TRACE_EVENT0("gpu", "GLSurface::CreateViewGLSurface");
285 switch (GetGLImplementation()) {
286 case kGLImplementationOSMesaGL: {
287 scoped_refptr<GLSurface> surface(
288 new NativeViewGLSurfaceOSMesa(window));
289 if (!surface->Initialize())
290 return NULL;
292 return surface;
294 case kGLImplementationEGLGLES2: {
295 DCHECK(window != gfx::kNullAcceleratedWidget);
296 scoped_refptr<NativeViewGLSurfaceEGL> surface(
297 new NativeViewGLSurfaceEGL(window));
298 scoped_ptr<VSyncProvider> sync_provider;
299 sync_provider.reset(new WinVSyncProvider(window));
300 if (!surface->Initialize(sync_provider.Pass()))
301 return NULL;
303 return surface;
305 case kGLImplementationDesktopGL: {
306 scoped_refptr<GLSurface> surface(new NativeViewGLSurfaceWGL(
307 window));
308 if (!surface->Initialize())
309 return NULL;
311 return surface;
313 case kGLImplementationMockGL:
314 return new GLSurfaceStub;
315 default:
316 NOTREACHED();
317 return NULL;
321 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface(
322 const gfx::Size& size) {
323 TRACE_EVENT0("gpu", "GLSurface::CreateOffscreenGLSurface");
324 switch (GetGLImplementation()) {
325 case kGLImplementationOSMesaGL: {
326 scoped_refptr<GLSurface> surface(
327 new GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA, size));
328 if (!surface->Initialize())
329 return NULL;
331 return surface;
333 case kGLImplementationEGLGLES2: {
334 scoped_refptr<GLSurface> surface(new PbufferGLSurfaceEGL(size));
335 if (!surface->Initialize())
336 return NULL;
338 return surface;
340 case kGLImplementationDesktopGL: {
341 scoped_refptr<GLSurface> surface(new PbufferGLSurfaceWGL(size));
342 if (!surface->Initialize())
343 return NULL;
345 return surface;
347 case kGLImplementationMockGL:
348 return new GLSurfaceStub;
349 default:
350 NOTREACHED();
351 return NULL;
355 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
356 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableD3D11) ||
357 CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseWarp))
358 return GetDC(NULL);
359 return EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE;
362 } // namespace gfx