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"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/trace_event/trace_event.h"
13 #include "base/win/windows_version.h"
14 #include "ui/gfx/native_widget_types.h"
15 #include "ui/gl/gl_bindings.h"
16 #include "ui/gl/gl_implementation.h"
17 #include "ui/gl/gl_surface_egl.h"
18 #include "ui/gl/gl_surface_osmesa.h"
19 #include "ui/gl/gl_surface_stub.h"
20 #include "ui/gl/gl_surface_wgl.h"
22 // From ANGLE's egl/eglext.h.
23 #if !defined(EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE)
24 #define EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE \
25 reinterpret_cast<EGLNativeDisplayType>(-2)
30 // This OSMesa GL surface can use GDI to swap the contents of the buffer to a
32 class NativeViewGLSurfaceOSMesa
: public GLSurfaceOSMesa
{
34 explicit NativeViewGLSurfaceOSMesa(gfx::AcceleratedWidget window
);
36 // Implement subset of GLSurface.
37 bool Initialize() override
;
38 void Destroy() override
;
39 bool IsOffscreen() override
;
40 gfx::SwapResult
SwapBuffers() override
;
41 bool SupportsPostSubBuffer() override
;
42 gfx::SwapResult
PostSubBuffer(int x
, int y
, int width
, int height
) override
;
45 ~NativeViewGLSurfaceOSMesa() override
;
47 gfx::AcceleratedWidget window_
;
50 DISALLOW_COPY_AND_ASSIGN(NativeViewGLSurfaceOSMesa
);
53 class WinVSyncProvider
: public VSyncProvider
{
55 explicit WinVSyncProvider(gfx::AcceleratedWidget window
) :
58 use_dwm_
= (base::win::GetVersion() >= base::win::VERSION_WIN7
);
61 ~WinVSyncProvider() override
{}
63 void GetVSyncParameters(const UpdateVSyncCallback
& callback
) override
{
64 TRACE_EVENT0("gpu", "WinVSyncProvider::GetVSyncParameters");
66 base::TimeTicks timebase
;
67 base::TimeDelta interval
;
68 bool dwm_active
= false;
70 // Query the DWM timing info first if available. This will provide the most
73 DWM_TIMING_INFO timing_info
;
74 timing_info
.cbSize
= sizeof(timing_info
);
75 HRESULT result
= DwmGetCompositionTimingInfo(NULL
, &timing_info
);
79 // Calculate an interval value using the rateRefresh numerator and
81 base::TimeDelta rate_interval
;
82 if (timing_info
.rateRefresh
.uiDenominator
> 0 &&
83 timing_info
.rateRefresh
.uiNumerator
> 0) {
84 // Swap the numerator/denominator to convert frequency to period.
85 rate_interval
= base::TimeDelta::FromMicroseconds(
86 timing_info
.rateRefresh
.uiDenominator
*
87 base::Time::kMicrosecondsPerSecond
/
88 timing_info
.rateRefresh
.uiNumerator
);
91 if (base::TimeTicks::IsHighResolution()) {
92 // qpcRefreshPeriod is very accurate but noisy, and must be used with
93 // a high resolution timebase to avoid frequently missing Vsync.
94 timebase
= base::TimeTicks::FromQPCValue(
95 static_cast<LONGLONG
>(timing_info
.qpcVBlank
));
96 interval
= base::TimeDelta::FromQPCValue(
97 static_cast<LONGLONG
>(timing_info
.qpcRefreshPeriod
));
98 // Check for interval values that are impossibly low. A 29 microsecond
99 // interval was seen (from a qpcRefreshPeriod of 60).
100 if (interval
< base::TimeDelta::FromMilliseconds(1)) {
101 interval
= rate_interval
;
103 // Check for the qpcRefreshPeriod interval being improbably small
104 // compared to the rateRefresh calculated interval, as another
105 // attempt at detecting driver bugs.
106 if (!rate_interval
.is_zero() && interval
< rate_interval
/ 2) {
107 interval
= rate_interval
;
110 // If FrameTime is not high resolution, we do not want to translate
111 // the QPC value provided by DWM into the low-resolution timebase,
112 // which would be error prone and jittery. As a fallback, we assume
113 // the timebase is zero and use rateRefresh, which may be rounded but
114 // isn't noisy like qpcRefreshPeriod, instead. The fact that we don't
115 // have a timebase here may lead to brief periods of jank when our
116 // scheduling becomes offset from the hardware vsync.
117 interval
= rate_interval
;
123 // When DWM compositing is active all displays are normalized to the
124 // refresh rate of the primary display, and won't composite any faster.
125 // If DWM compositing is disabled, though, we can use the refresh rates
126 // reported by each display, which will help systems that have mis-matched
127 // displays that run at different frequencies.
128 HMONITOR monitor
= MonitorFromWindow(window_
, MONITOR_DEFAULTTONEAREST
);
129 MONITORINFOEX monitor_info
;
130 monitor_info
.cbSize
= sizeof(MONITORINFOEX
);
131 BOOL result
= GetMonitorInfo(monitor
, &monitor_info
);
133 DEVMODE display_info
;
134 display_info
.dmSize
= sizeof(DEVMODE
);
135 display_info
.dmDriverExtra
= 0;
136 result
= EnumDisplaySettings(monitor_info
.szDevice
,
137 ENUM_CURRENT_SETTINGS
, &display_info
);
138 if (result
&& display_info
.dmDisplayFrequency
> 1) {
139 interval
= base::TimeDelta::FromMicroseconds(
140 (1.0 / static_cast<double>(display_info
.dmDisplayFrequency
)) *
141 base::Time::kMicrosecondsPerSecond
);
146 if (interval
.ToInternalValue() != 0) {
147 callback
.Run(timebase
, interval
);
152 DISALLOW_COPY_AND_ASSIGN(WinVSyncProvider
);
154 gfx::AcceleratedWidget window_
;
158 // Helper routine that does one-off initialization like determining the
160 bool GLSurface::InitializeOneOffInternal() {
161 switch (GetGLImplementation()) {
162 case kGLImplementationDesktopGL
:
163 if (!GLSurfaceWGL::InitializeOneOff()) {
164 LOG(ERROR
) << "GLSurfaceWGL::InitializeOneOff failed.";
168 case kGLImplementationEGLGLES2
:
169 if (!GLSurfaceEGL::InitializeOneOff()) {
170 LOG(ERROR
) << "GLSurfaceEGL::InitializeOneOff failed.";
178 NativeViewGLSurfaceOSMesa::NativeViewGLSurfaceOSMesa(
179 gfx::AcceleratedWidget window
)
180 : GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA
, gfx::Size(1, 1)),
182 device_context_(NULL
) {
186 NativeViewGLSurfaceOSMesa::~NativeViewGLSurfaceOSMesa() {
190 bool NativeViewGLSurfaceOSMesa::Initialize() {
191 if (!GLSurfaceOSMesa::Initialize())
194 device_context_
= GetDC(window_
);
198 void NativeViewGLSurfaceOSMesa::Destroy() {
199 if (window_
&& device_context_
)
200 ReleaseDC(window_
, device_context_
);
202 device_context_
= NULL
;
204 GLSurfaceOSMesa::Destroy();
207 bool NativeViewGLSurfaceOSMesa::IsOffscreen() {
211 gfx::SwapResult
NativeViewGLSurfaceOSMesa::SwapBuffers() {
212 DCHECK(device_context_
);
214 gfx::Size size
= GetSize();
216 // Note: negating the height below causes GDI to treat the bitmap data as row
217 // 0 being at the top.
218 BITMAPV4HEADER info
= { sizeof(BITMAPV4HEADER
) };
219 info
.bV4Width
= size
.width();
220 info
.bV4Height
= -size
.height();
222 info
.bV4BitCount
= 32;
223 info
.bV4V4Compression
= BI_BITFIELDS
;
224 info
.bV4RedMask
= 0x000000FF;
225 info
.bV4GreenMask
= 0x0000FF00;
226 info
.bV4BlueMask
= 0x00FF0000;
227 info
.bV4AlphaMask
= 0xFF000000;
229 // Copy the back buffer to the window's device context. Do not check whether
230 // StretchDIBits succeeds or not. It will fail if the window has been
231 // destroyed but it is preferable to allow rendering to silently fail if the
232 // window is destroyed. This is because the primary application of this
233 // class of GLContext is for testing and we do not want every GL related ui /
234 // browser test to become flaky if there is a race condition between GL
235 // context destruction and window destruction.
236 StretchDIBits(device_context_
,
237 0, 0, size
.width(), size
.height(),
238 0, 0, size
.width(), size
.height(),
240 reinterpret_cast<BITMAPINFO
*>(&info
),
244 return gfx::SwapResult::SWAP_ACK
;
247 bool NativeViewGLSurfaceOSMesa::SupportsPostSubBuffer() {
251 gfx::SwapResult
NativeViewGLSurfaceOSMesa::PostSubBuffer(int x
,
255 DCHECK(device_context_
);
257 gfx::Size size
= GetSize();
259 // Note: negating the height below causes GDI to treat the bitmap data as row
260 // 0 being at the top.
261 BITMAPV4HEADER info
= { sizeof(BITMAPV4HEADER
) };
262 info
.bV4Width
= size
.width();
263 info
.bV4Height
= -size
.height();
265 info
.bV4BitCount
= 32;
266 info
.bV4V4Compression
= BI_BITFIELDS
;
267 info
.bV4RedMask
= 0x000000FF;
268 info
.bV4GreenMask
= 0x0000FF00;
269 info
.bV4BlueMask
= 0x00FF0000;
270 info
.bV4AlphaMask
= 0xFF000000;
272 // Copy the back buffer to the window's device context. Do not check whether
273 // StretchDIBits succeeds or not. It will fail if the window has been
274 // destroyed but it is preferable to allow rendering to silently fail if the
275 // window is destroyed. This is because the primary application of this
276 // class of GLContext is for testing and we do not want every GL related ui /
277 // browser test to become flaky if there is a race condition between GL
278 // context destruction and window destruction.
279 StretchDIBits(device_context_
,
280 x
, size
.height() - y
- height
, width
, height
,
283 reinterpret_cast<BITMAPINFO
*>(&info
),
287 return gfx::SwapResult::SWAP_ACK
;
290 scoped_refptr
<GLSurface
> GLSurface::CreateViewGLSurface(
291 gfx::AcceleratedWidget window
) {
292 TRACE_EVENT0("gpu", "GLSurface::CreateViewGLSurface");
293 switch (GetGLImplementation()) {
294 case kGLImplementationOSMesaGL
: {
295 scoped_refptr
<GLSurface
> surface(
296 new NativeViewGLSurfaceOSMesa(window
));
297 if (!surface
->Initialize())
302 case kGLImplementationEGLGLES2
: {
303 DCHECK(window
!= gfx::kNullAcceleratedWidget
);
304 scoped_refptr
<NativeViewGLSurfaceEGL
> surface(
305 new NativeViewGLSurfaceEGL(window
));
306 scoped_ptr
<VSyncProvider
> sync_provider
;
307 sync_provider
.reset(new WinVSyncProvider(window
));
308 if (!surface
->Initialize(sync_provider
.Pass()))
313 case kGLImplementationDesktopGL
: {
314 scoped_refptr
<GLSurface
> surface(new NativeViewGLSurfaceWGL(
316 if (!surface
->Initialize())
321 case kGLImplementationMockGL
:
322 return new GLSurfaceStub
;
329 scoped_refptr
<GLSurface
> GLSurface::CreateOffscreenGLSurface(
330 const gfx::Size
& size
) {
331 TRACE_EVENT0("gpu", "GLSurface::CreateOffscreenGLSurface");
332 switch (GetGLImplementation()) {
333 case kGLImplementationOSMesaGL
: {
334 scoped_refptr
<GLSurface
> surface(
335 new GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA
, size
));
336 if (!surface
->Initialize())
341 case kGLImplementationEGLGLES2
: {
342 scoped_refptr
<GLSurface
> surface(new PbufferGLSurfaceEGL(size
));
343 if (!surface
->Initialize())
348 case kGLImplementationDesktopGL
: {
349 scoped_refptr
<GLSurface
> surface(new PbufferGLSurfaceWGL(size
));
350 if (!surface
->Initialize())
355 case kGLImplementationMockGL
:
356 return new GLSurfaceStub
;
363 EGLNativeDisplayType
GetPlatformDefaultEGLNativeDisplay() {