[Histograms]: Add histograms for requestIdleCallback.
[chromium-blink-merge.git] / ui / gl / gl_surface_win.cc
blob93410cae6aed0293c2c5b48467a1d05b4c505e87
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/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)
26 #endif
28 namespace gfx {
30 // This OSMesa GL surface can use GDI to swap the contents of the buffer to a
31 // view.
32 class NativeViewGLSurfaceOSMesa : public GLSurfaceOSMesa {
33 public:
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;
44 private:
45 ~NativeViewGLSurfaceOSMesa() override;
47 gfx::AcceleratedWidget window_;
48 HDC device_context_;
50 DISALLOW_COPY_AND_ASSIGN(NativeViewGLSurfaceOSMesa);
53 class WinVSyncProvider : public VSyncProvider {
54 public:
55 explicit WinVSyncProvider(gfx::AcceleratedWidget window) :
56 window_(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
71 // precise values.
72 if (use_dwm_) {
73 DWM_TIMING_INFO timing_info;
74 timing_info.cbSize = sizeof(timing_info);
75 HRESULT result = DwmGetCompositionTimingInfo(NULL, &timing_info);
76 if (result == S_OK) {
77 dwm_active = true;
79 // Calculate an interval value using the rateRefresh numerator and
80 // denominator.
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;
109 } else {
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;
122 if (!dwm_active) {
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);
132 if (result) {
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);
151 private:
152 DISALLOW_COPY_AND_ASSIGN(WinVSyncProvider);
154 gfx::AcceleratedWidget window_;
155 bool use_dwm_;
158 // Helper routine that does one-off initialization like determining the
159 // pixel format.
160 bool GLSurface::InitializeOneOffInternal() {
161 switch (GetGLImplementation()) {
162 case kGLImplementationDesktopGL:
163 if (!GLSurfaceWGL::InitializeOneOff()) {
164 LOG(ERROR) << "GLSurfaceWGL::InitializeOneOff failed.";
165 return false;
167 break;
168 case kGLImplementationEGLGLES2:
169 if (!GLSurfaceEGL::InitializeOneOff()) {
170 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
171 return false;
173 break;
174 case kGLImplementationNone:
175 case kGLImplementationDesktopGLCoreProfile:
176 case kGLImplementationAppleGL:
177 NOTREACHED();
178 case kGLImplementationOSMesaGL:
179 case kGLImplementationMockGL:
180 break;
182 return true;
185 NativeViewGLSurfaceOSMesa::NativeViewGLSurfaceOSMesa(
186 gfx::AcceleratedWidget window)
187 : GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA, gfx::Size(1, 1)),
188 window_(window),
189 device_context_(NULL) {
190 DCHECK(window);
193 NativeViewGLSurfaceOSMesa::~NativeViewGLSurfaceOSMesa() {
194 Destroy();
197 bool NativeViewGLSurfaceOSMesa::Initialize() {
198 if (!GLSurfaceOSMesa::Initialize())
199 return false;
201 device_context_ = GetDC(window_);
202 return true;
205 void NativeViewGLSurfaceOSMesa::Destroy() {
206 if (window_ && device_context_)
207 ReleaseDC(window_, device_context_);
209 device_context_ = NULL;
211 GLSurfaceOSMesa::Destroy();
214 bool NativeViewGLSurfaceOSMesa::IsOffscreen() {
215 return false;
218 gfx::SwapResult NativeViewGLSurfaceOSMesa::SwapBuffers() {
219 DCHECK(device_context_);
221 gfx::Size size = GetSize();
223 // Note: negating the height below causes GDI to treat the bitmap data as row
224 // 0 being at the top.
225 BITMAPV4HEADER info = { sizeof(BITMAPV4HEADER) };
226 info.bV4Width = size.width();
227 info.bV4Height = -size.height();
228 info.bV4Planes = 1;
229 info.bV4BitCount = 32;
230 info.bV4V4Compression = BI_BITFIELDS;
231 info.bV4RedMask = 0x000000FF;
232 info.bV4GreenMask = 0x0000FF00;
233 info.bV4BlueMask = 0x00FF0000;
234 info.bV4AlphaMask = 0xFF000000;
236 // Copy the back buffer to the window's device context. Do not check whether
237 // StretchDIBits succeeds or not. It will fail if the window has been
238 // destroyed but it is preferable to allow rendering to silently fail if the
239 // window is destroyed. This is because the primary application of this
240 // class of GLContext is for testing and we do not want every GL related ui /
241 // browser test to become flaky if there is a race condition between GL
242 // context destruction and window destruction.
243 StretchDIBits(device_context_,
244 0, 0, size.width(), size.height(),
245 0, 0, size.width(), size.height(),
246 GetHandle(),
247 reinterpret_cast<BITMAPINFO*>(&info),
248 DIB_RGB_COLORS,
249 SRCCOPY);
251 return gfx::SwapResult::SWAP_ACK;
254 bool NativeViewGLSurfaceOSMesa::SupportsPostSubBuffer() {
255 return true;
258 gfx::SwapResult NativeViewGLSurfaceOSMesa::PostSubBuffer(int x,
259 int y,
260 int width,
261 int height) {
262 DCHECK(device_context_);
264 gfx::Size size = GetSize();
266 // Note: negating the height below causes GDI to treat the bitmap data as row
267 // 0 being at the top.
268 BITMAPV4HEADER info = { sizeof(BITMAPV4HEADER) };
269 info.bV4Width = size.width();
270 info.bV4Height = -size.height();
271 info.bV4Planes = 1;
272 info.bV4BitCount = 32;
273 info.bV4V4Compression = BI_BITFIELDS;
274 info.bV4RedMask = 0x000000FF;
275 info.bV4GreenMask = 0x0000FF00;
276 info.bV4BlueMask = 0x00FF0000;
277 info.bV4AlphaMask = 0xFF000000;
279 // Copy the back buffer to the window's device context. Do not check whether
280 // StretchDIBits succeeds or not. It will fail if the window has been
281 // destroyed but it is preferable to allow rendering to silently fail if the
282 // window is destroyed. This is because the primary application of this
283 // class of GLContext is for testing and we do not want every GL related ui /
284 // browser test to become flaky if there is a race condition between GL
285 // context destruction and window destruction.
286 StretchDIBits(device_context_,
287 x, size.height() - y - height, width, height,
288 x, y, width, height,
289 GetHandle(),
290 reinterpret_cast<BITMAPINFO*>(&info),
291 DIB_RGB_COLORS,
292 SRCCOPY);
294 return gfx::SwapResult::SWAP_ACK;
297 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface(
298 gfx::AcceleratedWidget window) {
299 TRACE_EVENT0("gpu", "GLSurface::CreateViewGLSurface");
300 switch (GetGLImplementation()) {
301 case kGLImplementationOSMesaGL: {
302 scoped_refptr<GLSurface> surface(
303 new NativeViewGLSurfaceOSMesa(window));
304 if (!surface->Initialize())
305 return NULL;
307 return surface;
309 case kGLImplementationEGLGLES2: {
310 DCHECK(window != gfx::kNullAcceleratedWidget);
311 scoped_refptr<NativeViewGLSurfaceEGL> surface(
312 new NativeViewGLSurfaceEGL(window));
313 scoped_ptr<VSyncProvider> sync_provider;
314 sync_provider.reset(new WinVSyncProvider(window));
315 if (!surface->Initialize(sync_provider.Pass()))
316 return NULL;
318 return surface;
320 case kGLImplementationDesktopGL: {
321 scoped_refptr<GLSurface> surface(new NativeViewGLSurfaceWGL(
322 window));
323 if (!surface->Initialize())
324 return NULL;
326 return surface;
328 case kGLImplementationMockGL:
329 return new GLSurfaceStub;
330 default:
331 NOTREACHED();
332 return NULL;
336 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface(
337 const gfx::Size& size) {
338 TRACE_EVENT0("gpu", "GLSurface::CreateOffscreenGLSurface");
339 switch (GetGLImplementation()) {
340 case kGLImplementationOSMesaGL: {
341 scoped_refptr<GLSurface> surface(
342 new GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA, size));
343 if (!surface->Initialize())
344 return NULL;
346 return surface;
348 case kGLImplementationEGLGLES2: {
349 scoped_refptr<GLSurface> surface(new PbufferGLSurfaceEGL(size));
350 if (!surface->Initialize())
351 return NULL;
353 return surface;
355 case kGLImplementationDesktopGL: {
356 scoped_refptr<GLSurface> surface(new PbufferGLSurfaceWGL(size));
357 if (!surface->Initialize())
358 return NULL;
360 return surface;
362 case kGLImplementationMockGL:
363 return new GLSurfaceStub;
364 default:
365 NOTREACHED();
366 return NULL;
370 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
371 return GetDC(NULL);
374 } // namespace gfx