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)
31 // This OSMesa GL surface can use GDI to swap the contents of the buffer to a
33 class NativeViewGLSurfaceOSMesa
: public GLSurfaceOSMesa
{
35 explicit NativeViewGLSurfaceOSMesa(gfx::AcceleratedWidget window
);
36 virtual ~NativeViewGLSurfaceOSMesa();
38 // Implement subset of GLSurface.
39 virtual bool Initialize() OVERRIDE
;
40 virtual void Destroy() OVERRIDE
;
41 virtual bool IsOffscreen() OVERRIDE
;
42 virtual bool SwapBuffers() OVERRIDE
;
43 virtual bool SupportsPostSubBuffer() OVERRIDE
;
44 virtual bool PostSubBuffer(int x
, int y
, int width
, int height
) OVERRIDE
;
47 gfx::AcceleratedWidget window_
;
50 DISALLOW_COPY_AND_ASSIGN(NativeViewGLSurfaceOSMesa
);
53 class DWMVSyncProvider
: public VSyncProvider
{
55 explicit DWMVSyncProvider() {}
57 virtual ~DWMVSyncProvider() {}
59 virtual void GetVSyncParameters(const UpdateVSyncCallback
& callback
) {
60 TRACE_EVENT0("gpu", "DWMVSyncProvider::GetVSyncParameters");
61 DWM_TIMING_INFO timing_info
;
62 timing_info
.cbSize
= sizeof(timing_info
);
63 HRESULT result
= DwmGetCompositionTimingInfo(NULL
, &timing_info
);
67 base::TimeTicks timebase
;
68 // If FrameTime is not high resolution, we do not want to translate the
69 // QPC value provided by DWM into the low-resolution timebase, which
70 // would be error prone and jittery. As a fallback, we assume the timebase
72 if (gfx::FrameTime::TimestampsAreHighRes()) {
73 timebase
= gfx::FrameTime::FromQPCValue(
74 static_cast<LONGLONG
>(timing_info
.qpcVBlank
));
77 // Swap the numerator/denominator to convert frequency to period.
78 if (timing_info
.rateRefresh
.uiDenominator
> 0 &&
79 timing_info
.rateRefresh
.uiNumerator
> 0) {
80 base::TimeDelta interval
= base::TimeDelta::FromMicroseconds(
81 timing_info
.rateRefresh
.uiDenominator
*
82 base::Time::kMicrosecondsPerSecond
/
83 timing_info
.rateRefresh
.uiNumerator
);
84 callback
.Run(timebase
, interval
);
89 DISALLOW_COPY_AND_ASSIGN(DWMVSyncProvider
);
92 // Helper routine that does one-off initialization like determining the
94 bool GLSurface::InitializeOneOffInternal() {
95 switch (GetGLImplementation()) {
96 case kGLImplementationDesktopGL
:
97 if (!GLSurfaceWGL::InitializeOneOff()) {
98 LOG(ERROR
) << "GLSurfaceWGL::InitializeOneOff failed.";
102 case kGLImplementationEGLGLES2
:
103 if (!GLSurfaceEGL::InitializeOneOff()) {
104 LOG(ERROR
) << "GLSurfaceEGL::InitializeOneOff failed.";
112 NativeViewGLSurfaceOSMesa::NativeViewGLSurfaceOSMesa(
113 gfx::AcceleratedWidget window
)
114 : GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA
, gfx::Size(1, 1)),
116 device_context_(NULL
) {
120 NativeViewGLSurfaceOSMesa::~NativeViewGLSurfaceOSMesa() {
124 bool NativeViewGLSurfaceOSMesa::Initialize() {
125 if (!GLSurfaceOSMesa::Initialize())
128 device_context_
= GetDC(window_
);
132 void NativeViewGLSurfaceOSMesa::Destroy() {
133 if (window_
&& device_context_
)
134 ReleaseDC(window_
, device_context_
);
136 device_context_
= NULL
;
138 GLSurfaceOSMesa::Destroy();
141 bool NativeViewGLSurfaceOSMesa::IsOffscreen() {
145 bool NativeViewGLSurfaceOSMesa::SwapBuffers() {
146 DCHECK(device_context_
);
148 gfx::Size size
= GetSize();
150 // Note: negating the height below causes GDI to treat the bitmap data as row
151 // 0 being at the top.
152 BITMAPV4HEADER info
= { sizeof(BITMAPV4HEADER
) };
153 info
.bV4Width
= size
.width();
154 info
.bV4Height
= -size
.height();
156 info
.bV4BitCount
= 32;
157 info
.bV4V4Compression
= BI_BITFIELDS
;
158 info
.bV4RedMask
= 0x000000FF;
159 info
.bV4GreenMask
= 0x0000FF00;
160 info
.bV4BlueMask
= 0x00FF0000;
161 info
.bV4AlphaMask
= 0xFF000000;
163 // Copy the back buffer to the window's device context. Do not check whether
164 // StretchDIBits succeeds or not. It will fail if the window has been
165 // destroyed but it is preferable to allow rendering to silently fail if the
166 // window is destroyed. This is because the primary application of this
167 // class of GLContext is for testing and we do not want every GL related ui /
168 // browser test to become flaky if there is a race condition between GL
169 // context destruction and window destruction.
170 StretchDIBits(device_context_
,
171 0, 0, size
.width(), size
.height(),
172 0, 0, size
.width(), size
.height(),
174 reinterpret_cast<BITMAPINFO
*>(&info
),
181 bool NativeViewGLSurfaceOSMesa::SupportsPostSubBuffer() {
185 bool NativeViewGLSurfaceOSMesa::PostSubBuffer(
186 int x
, int y
, int width
, int height
) {
187 DCHECK(device_context_
);
189 gfx::Size size
= GetSize();
191 // Note: negating the height below causes GDI to treat the bitmap data as row
192 // 0 being at the top.
193 BITMAPV4HEADER info
= { sizeof(BITMAPV4HEADER
) };
194 info
.bV4Width
= size
.width();
195 info
.bV4Height
= -size
.height();
197 info
.bV4BitCount
= 32;
198 info
.bV4V4Compression
= BI_BITFIELDS
;
199 info
.bV4RedMask
= 0x000000FF;
200 info
.bV4GreenMask
= 0x0000FF00;
201 info
.bV4BlueMask
= 0x00FF0000;
202 info
.bV4AlphaMask
= 0xFF000000;
204 // Copy the back buffer to the window's device context. Do not check whether
205 // StretchDIBits succeeds or not. It will fail if the window has been
206 // destroyed but it is preferable to allow rendering to silently fail if the
207 // window is destroyed. This is because the primary application of this
208 // class of GLContext is for testing and we do not want every GL related ui /
209 // browser test to become flaky if there is a race condition between GL
210 // context destruction and window destruction.
211 StretchDIBits(device_context_
,
212 x
, size
.height() - y
- height
, width
, height
,
215 reinterpret_cast<BITMAPINFO
*>(&info
),
222 scoped_refptr
<GLSurface
> GLSurface::CreateViewGLSurface(
223 gfx::AcceleratedWidget window
) {
224 TRACE_EVENT0("gpu", "GLSurface::CreateViewGLSurface");
225 switch (GetGLImplementation()) {
226 case kGLImplementationOSMesaGL
: {
227 scoped_refptr
<GLSurface
> surface(
228 new NativeViewGLSurfaceOSMesa(window
));
229 if (!surface
->Initialize())
234 case kGLImplementationEGLGLES2
: {
235 DCHECK(window
!= gfx::kNullAcceleratedWidget
);
236 scoped_refptr
<NativeViewGLSurfaceEGL
> surface(
237 new NativeViewGLSurfaceEGL(window
));
238 scoped_ptr
<VSyncProvider
> sync_provider
;
239 if (base::win::GetVersion() >= base::win::VERSION_VISTA
)
240 sync_provider
.reset(new DWMVSyncProvider
);
241 if (!surface
->Initialize(sync_provider
.Pass()))
246 case kGLImplementationDesktopGL
: {
247 scoped_refptr
<GLSurface
> surface(new NativeViewGLSurfaceWGL(
249 if (!surface
->Initialize())
254 case kGLImplementationMockGL
:
255 return new GLSurfaceStub
;
262 scoped_refptr
<GLSurface
> GLSurface::CreateOffscreenGLSurface(
263 const gfx::Size
& size
) {
264 TRACE_EVENT0("gpu", "GLSurface::CreateOffscreenGLSurface");
265 switch (GetGLImplementation()) {
266 case kGLImplementationOSMesaGL
: {
267 scoped_refptr
<GLSurface
> surface(
268 new GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA
, size
));
269 if (!surface
->Initialize())
274 case kGLImplementationEGLGLES2
: {
275 scoped_refptr
<GLSurface
> surface(new PbufferGLSurfaceEGL(size
));
276 if (!surface
->Initialize())
281 case kGLImplementationDesktopGL
: {
282 scoped_refptr
<GLSurface
> surface(new PbufferGLSurfaceWGL(size
));
283 if (!surface
->Initialize())
288 case kGLImplementationMockGL
:
289 return new GLSurfaceStub
;
296 EGLNativeDisplayType
GetPlatformDefaultEGLNativeDisplay() {
297 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableD3D11
))
298 return EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE
;
300 return EGL_DEFAULT_DISPLAY
;