Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / gl / gl_surface_win.cc
blob000110368d94a0cbe547213979675b1fcffef10c
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
29 namespace gfx {
31 // This OSMesa GL surface can use GDI to swap the contents of the buffer to a
32 // view.
33 class NativeViewGLSurfaceOSMesa : public GLSurfaceOSMesa {
34 public:
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;
46 private:
47 gfx::AcceleratedWidget window_;
48 HDC device_context_;
50 DISALLOW_COPY_AND_ASSIGN(NativeViewGLSurfaceOSMesa);
53 class DWMVSyncProvider : public VSyncProvider {
54 public:
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);
64 if (result != S_OK)
65 return;
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
71 // is zero.
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);
88 private:
89 DISALLOW_COPY_AND_ASSIGN(DWMVSyncProvider);
92 // Helper routine that does one-off initialization like determining the
93 // pixel format.
94 bool GLSurface::InitializeOneOffInternal() {
95 switch (GetGLImplementation()) {
96 case kGLImplementationDesktopGL:
97 if (!GLSurfaceWGL::InitializeOneOff()) {
98 LOG(ERROR) << "GLSurfaceWGL::InitializeOneOff failed.";
99 return false;
101 break;
102 case kGLImplementationEGLGLES2:
103 if (!GLSurfaceEGL::InitializeOneOff()) {
104 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
105 return false;
107 break;
109 return true;
112 NativeViewGLSurfaceOSMesa::NativeViewGLSurfaceOSMesa(
113 gfx::AcceleratedWidget window)
114 : GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA, gfx::Size(1, 1)),
115 window_(window),
116 device_context_(NULL) {
117 DCHECK(window);
120 NativeViewGLSurfaceOSMesa::~NativeViewGLSurfaceOSMesa() {
121 Destroy();
124 bool NativeViewGLSurfaceOSMesa::Initialize() {
125 if (!GLSurfaceOSMesa::Initialize())
126 return false;
128 device_context_ = GetDC(window_);
129 return true;
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() {
142 return false;
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();
155 info.bV4Planes = 1;
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(),
173 GetHandle(),
174 reinterpret_cast<BITMAPINFO*>(&info),
175 DIB_RGB_COLORS,
176 SRCCOPY);
178 return true;
181 bool NativeViewGLSurfaceOSMesa::SupportsPostSubBuffer() {
182 return true;
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();
196 info.bV4Planes = 1;
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,
213 x, y, width, height,
214 GetHandle(),
215 reinterpret_cast<BITMAPINFO*>(&info),
216 DIB_RGB_COLORS,
217 SRCCOPY);
219 return true;
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())
230 return NULL;
232 return surface;
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()))
242 return NULL;
244 return surface;
246 case kGLImplementationDesktopGL: {
247 scoped_refptr<GLSurface> surface(new NativeViewGLSurfaceWGL(
248 window));
249 if (!surface->Initialize())
250 return NULL;
252 return surface;
254 case kGLImplementationMockGL:
255 return new GLSurfaceStub;
256 default:
257 NOTREACHED();
258 return NULL;
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())
270 return NULL;
272 return surface;
274 case kGLImplementationEGLGLES2: {
275 scoped_refptr<GLSurface> surface(new PbufferGLSurfaceEGL(size));
276 if (!surface->Initialize())
277 return NULL;
279 return surface;
281 case kGLImplementationDesktopGL: {
282 scoped_refptr<GLSurface> surface(new PbufferGLSurfaceWGL(size));
283 if (!surface->Initialize())
284 return NULL;
286 return surface;
288 case kGLImplementationMockGL:
289 return new GLSurfaceStub;
290 default:
291 NOTREACHED();
292 return NULL;
296 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
297 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableD3D11))
298 return EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE;
300 return EGL_DEFAULT_DISPLAY;
303 } // namespace gfx