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/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)
28 #if !defined(EGL_PLATFORM_ANGLE_ANGLE)
29 #define EGL_PLATFORM_ANGLE_ANGLE 0x3201
31 #if !defined(EGL_PLATFORM_ANGLE_TYPE_ANGLE)
32 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3202
34 #if !defined(EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE)
35 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3207
37 #if !defined(EGL_PLATFORM_ANGLE_USE_WARP_ANGLE)
38 #define EGL_PLATFORM_ANGLE_USE_WARP_ANGLE 0x3208
43 // This OSMesa GL surface can use GDI to swap the contents of the buffer to a
45 class NativeViewGLSurfaceOSMesa
: public GLSurfaceOSMesa
{
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
;
59 gfx::AcceleratedWidget window_
;
62 DISALLOW_COPY_AND_ASSIGN(NativeViewGLSurfaceOSMesa
);
65 class WinVSyncProvider
: public VSyncProvider
{
67 explicit WinVSyncProvider(gfx::AcceleratedWidget 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
85 DWM_TIMING_INFO timing_info
;
86 timing_info
.cbSize
= sizeof(timing_info
);
87 HRESULT result
= DwmGetCompositionTimingInfo(NULL
, &timing_info
);
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
);
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
);
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
);
146 DISALLOW_COPY_AND_ASSIGN(WinVSyncProvider
);
148 gfx::AcceleratedWidget window_
;
152 // Helper routine that does one-off initialization like determining the
154 bool GLSurface::InitializeOneOffInternal() {
155 switch (GetGLImplementation()) {
156 case kGLImplementationDesktopGL
:
157 if (!GLSurfaceWGL::InitializeOneOff()) {
158 LOG(ERROR
) << "GLSurfaceWGL::InitializeOneOff failed.";
162 case kGLImplementationEGLGLES2
:
163 if (!GLSurfaceEGL::InitializeOneOff()) {
164 LOG(ERROR
) << "GLSurfaceEGL::InitializeOneOff failed.";
172 NativeViewGLSurfaceOSMesa::NativeViewGLSurfaceOSMesa(
173 gfx::AcceleratedWidget window
)
174 : GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA
, gfx::Size(1, 1)),
176 device_context_(NULL
) {
180 NativeViewGLSurfaceOSMesa::~NativeViewGLSurfaceOSMesa() {
184 bool NativeViewGLSurfaceOSMesa::Initialize() {
185 if (!GLSurfaceOSMesa::Initialize())
188 device_context_
= GetDC(window_
);
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() {
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();
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(),
234 reinterpret_cast<BITMAPINFO
*>(&info
),
241 bool NativeViewGLSurfaceOSMesa::SupportsPostSubBuffer() {
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();
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
,
275 reinterpret_cast<BITMAPINFO
*>(&info
),
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())
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()))
305 case kGLImplementationDesktopGL
: {
306 scoped_refptr
<GLSurface
> surface(new NativeViewGLSurfaceWGL(
308 if (!surface
->Initialize())
313 case kGLImplementationMockGL
:
314 return new GLSurfaceStub
;
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())
333 case kGLImplementationEGLGLES2
: {
334 scoped_refptr
<GLSurface
> surface(new PbufferGLSurfaceEGL(size
));
335 if (!surface
->Initialize())
340 case kGLImplementationDesktopGL
: {
341 scoped_refptr
<GLSurface
> surface(new PbufferGLSurfaceWGL(size
));
342 if (!surface
->Initialize())
347 case kGLImplementationMockGL
:
348 return new GLSurfaceStub
;
355 EGLNativeDisplayType
GetPlatformDefaultEGLNativeDisplay() {
356 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
357 switches::kDisableD3D11
) ||
358 base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseWarp
))
360 return EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE
;