1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gl/gl_surface.h"
#include <dwmapi.h>
#include "base/command_line.h"
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/win/windows_version.h"
#include "ui/gfx/frame_time.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_surface_egl.h"
#include "ui/gl/gl_surface_osmesa.h"
#include "ui/gl/gl_surface_stub.h"
#include "ui/gl/gl_surface_wgl.h"
// From ANGLE's egl/eglext.h.
#if !defined(EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE)
#define EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE \
reinterpret_cast<EGLNativeDisplayType>(-2)
#endif
#if !defined(EGL_PLATFORM_ANGLE_ANGLE)
#define EGL_PLATFORM_ANGLE_ANGLE 0x3201
#endif
#if !defined(EGL_PLATFORM_ANGLE_TYPE_ANGLE)
#define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3202
#endif
#if !defined(EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE)
#define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3207
#endif
#if !defined(EGL_PLATFORM_ANGLE_USE_WARP_ANGLE)
#define EGL_PLATFORM_ANGLE_USE_WARP_ANGLE 0x3208
#endif
namespace gfx {
// This OSMesa GL surface can use GDI to swap the contents of the buffer to a
// view.
class NativeViewGLSurfaceOSMesa : public GLSurfaceOSMesa {
public:
explicit NativeViewGLSurfaceOSMesa(gfx::AcceleratedWidget window);
virtual ~NativeViewGLSurfaceOSMesa();
// Implement subset of GLSurface.
virtual bool Initialize() override;
virtual void Destroy() override;
virtual bool IsOffscreen() override;
virtual bool SwapBuffers() override;
virtual bool SupportsPostSubBuffer() override;
virtual bool PostSubBuffer(int x, int y, int width, int height) override;
private:
gfx::AcceleratedWidget window_;
HDC device_context_;
DISALLOW_COPY_AND_ASSIGN(NativeViewGLSurfaceOSMesa);
};
class WinVSyncProvider : public VSyncProvider {
public:
explicit WinVSyncProvider(gfx::AcceleratedWidget window) :
window_(window)
{
use_dwm_ = (base::win::GetVersion() >= base::win::VERSION_WIN7);
}
virtual ~WinVSyncProvider() {}
virtual void GetVSyncParameters(const UpdateVSyncCallback& callback) {
TRACE_EVENT0("gpu", "WinVSyncProvider::GetVSyncParameters");
base::TimeTicks timebase;
base::TimeDelta interval;
bool dwm_active = false;
// Query the DWM timing info first if available. This will provide the most
// precise values.
if (use_dwm_) {
DWM_TIMING_INFO timing_info;
timing_info.cbSize = sizeof(timing_info);
HRESULT result = DwmGetCompositionTimingInfo(NULL, &timing_info);
if (result == S_OK) {
dwm_active = true;
// If FrameTime is not high resolution, we do not want to translate the
// QPC value provided by DWM into the low-resolution timebase, which
// would be error prone and jittery. As a fallback, we assume the
// timebase is zero.
if (gfx::FrameTime::TimestampsAreHighRes()) {
timebase = gfx::FrameTime::FromQPCValue(
static_cast<LONGLONG>(timing_info.qpcVBlank));
}
// Swap the numerator/denominator to convert frequency to period.
if (timing_info.rateRefresh.uiDenominator > 0 &&
timing_info.rateRefresh.uiNumerator > 0) {
interval = base::TimeDelta::FromMicroseconds(
timing_info.rateRefresh.uiDenominator *
base::Time::kMicrosecondsPerSecond /
timing_info.rateRefresh.uiNumerator);
}
}
}
// Double check DWM values against per-display refresh rates.
// When DWM compositing is active all displays are normalized to the
// refresh rate of the primary display, and won't composite any faster.
// If the display refresh rate is higher than the DWM reported value we will
// favor the DWM value because any additional frames produced will be
// discarded by the OS. If the display refresh rate is lower, however, we
// can use that to limit the frames we produce more intelligently.
// If DWM compositing is not active we will always use the display refresh.
HMONITOR monitor = MonitorFromWindow(window_, MONITOR_DEFAULTTONEAREST);
MONITORINFOEX monitor_info;
monitor_info.cbSize = sizeof(MONITORINFOEX);
BOOL result = GetMonitorInfo(monitor, &monitor_info);
if (result) {
DEVMODE display_info;
display_info.dmSize = sizeof(DEVMODE);
display_info.dmDriverExtra = 0;
result = EnumDisplaySettings(monitor_info.szDevice, ENUM_CURRENT_SETTINGS,
&display_info);
if (result && display_info.dmDisplayFrequency > 1) {
base::TimeDelta display_interval = base::TimeDelta::FromMicroseconds(
(1.0 / static_cast<double>(display_info.dmDisplayFrequency)) *
base::Time::kMicrosecondsPerSecond);
if (!dwm_active || display_interval > interval) {
interval = display_interval;
}
}
}
if (interval.ToInternalValue() != 0) {
callback.Run(timebase, interval);
}
}
private:
DISALLOW_COPY_AND_ASSIGN(WinVSyncProvider);
gfx::AcceleratedWidget window_;
bool use_dwm_;
};
// Helper routine that does one-off initialization like determining the
// pixel format.
bool GLSurface::InitializeOneOffInternal() {
switch (GetGLImplementation()) {
case kGLImplementationDesktopGL:
if (!GLSurfaceWGL::InitializeOneOff()) {
LOG(ERROR) << "GLSurfaceWGL::InitializeOneOff failed.";
return false;
}
break;
case kGLImplementationEGLGLES2:
if (!GLSurfaceEGL::InitializeOneOff()) {
LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
return false;
}
break;
}
return true;
}
NativeViewGLSurfaceOSMesa::NativeViewGLSurfaceOSMesa(
gfx::AcceleratedWidget window)
: GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA, gfx::Size(1, 1)),
window_(window),
device_context_(NULL) {
DCHECK(window);
}
NativeViewGLSurfaceOSMesa::~NativeViewGLSurfaceOSMesa() {
Destroy();
}
bool NativeViewGLSurfaceOSMesa::Initialize() {
if (!GLSurfaceOSMesa::Initialize())
return false;
device_context_ = GetDC(window_);
return true;
}
void NativeViewGLSurfaceOSMesa::Destroy() {
if (window_ && device_context_)
ReleaseDC(window_, device_context_);
device_context_ = NULL;
GLSurfaceOSMesa::Destroy();
}
bool NativeViewGLSurfaceOSMesa::IsOffscreen() {
return false;
}
bool NativeViewGLSurfaceOSMesa::SwapBuffers() {
DCHECK(device_context_);
gfx::Size size = GetSize();
// Note: negating the height below causes GDI to treat the bitmap data as row
// 0 being at the top.
BITMAPV4HEADER info = { sizeof(BITMAPV4HEADER) };
info.bV4Width = size.width();
info.bV4Height = -size.height();
info.bV4Planes = 1;
info.bV4BitCount = 32;
info.bV4V4Compression = BI_BITFIELDS;
info.bV4RedMask = 0x000000FF;
info.bV4GreenMask = 0x0000FF00;
info.bV4BlueMask = 0x00FF0000;
info.bV4AlphaMask = 0xFF000000;
// Copy the back buffer to the window's device context. Do not check whether
// StretchDIBits succeeds or not. It will fail if the window has been
// destroyed but it is preferable to allow rendering to silently fail if the
// window is destroyed. This is because the primary application of this
// class of GLContext is for testing and we do not want every GL related ui /
// browser test to become flaky if there is a race condition between GL
// context destruction and window destruction.
StretchDIBits(device_context_,
0, 0, size.width(), size.height(),
0, 0, size.width(), size.height(),
GetHandle(),
reinterpret_cast<BITMAPINFO*>(&info),
DIB_RGB_COLORS,
SRCCOPY);
return true;
}
bool NativeViewGLSurfaceOSMesa::SupportsPostSubBuffer() {
return true;
}
bool NativeViewGLSurfaceOSMesa::PostSubBuffer(
int x, int y, int width, int height) {
DCHECK(device_context_);
gfx::Size size = GetSize();
// Note: negating the height below causes GDI to treat the bitmap data as row
// 0 being at the top.
BITMAPV4HEADER info = { sizeof(BITMAPV4HEADER) };
info.bV4Width = size.width();
info.bV4Height = -size.height();
info.bV4Planes = 1;
info.bV4BitCount = 32;
info.bV4V4Compression = BI_BITFIELDS;
info.bV4RedMask = 0x000000FF;
info.bV4GreenMask = 0x0000FF00;
info.bV4BlueMask = 0x00FF0000;
info.bV4AlphaMask = 0xFF000000;
// Copy the back buffer to the window's device context. Do not check whether
// StretchDIBits succeeds or not. It will fail if the window has been
// destroyed but it is preferable to allow rendering to silently fail if the
// window is destroyed. This is because the primary application of this
// class of GLContext is for testing and we do not want every GL related ui /
// browser test to become flaky if there is a race condition between GL
// context destruction and window destruction.
StretchDIBits(device_context_,
x, size.height() - y - height, width, height,
x, y, width, height,
GetHandle(),
reinterpret_cast<BITMAPINFO*>(&info),
DIB_RGB_COLORS,
SRCCOPY);
return true;
}
scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface(
gfx::AcceleratedWidget window) {
TRACE_EVENT0("gpu", "GLSurface::CreateViewGLSurface");
switch (GetGLImplementation()) {
case kGLImplementationOSMesaGL: {
scoped_refptr<GLSurface> surface(
new NativeViewGLSurfaceOSMesa(window));
if (!surface->Initialize())
return NULL;
return surface;
}
case kGLImplementationEGLGLES2: {
DCHECK(window != gfx::kNullAcceleratedWidget);
scoped_refptr<NativeViewGLSurfaceEGL> surface(
new NativeViewGLSurfaceEGL(window));
scoped_ptr<VSyncProvider> sync_provider;
sync_provider.reset(new WinVSyncProvider(window));
if (!surface->Initialize(sync_provider.Pass()))
return NULL;
return surface;
}
case kGLImplementationDesktopGL: {
scoped_refptr<GLSurface> surface(new NativeViewGLSurfaceWGL(
window));
if (!surface->Initialize())
return NULL;
return surface;
}
case kGLImplementationMockGL:
return new GLSurfaceStub;
default:
NOTREACHED();
return NULL;
}
}
scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface(
const gfx::Size& size) {
TRACE_EVENT0("gpu", "GLSurface::CreateOffscreenGLSurface");
switch (GetGLImplementation()) {
case kGLImplementationOSMesaGL: {
scoped_refptr<GLSurface> surface(
new GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA, size));
if (!surface->Initialize())
return NULL;
return surface;
}
case kGLImplementationEGLGLES2: {
scoped_refptr<GLSurface> surface(new PbufferGLSurfaceEGL(size));
if (!surface->Initialize())
return NULL;
return surface;
}
case kGLImplementationDesktopGL: {
scoped_refptr<GLSurface> surface(new PbufferGLSurfaceWGL(size));
if (!surface->Initialize())
return NULL;
return surface;
}
case kGLImplementationMockGL:
return new GLSurfaceStub;
default:
NOTREACHED();
return NULL;
}
}
EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableD3D11) ||
base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseWarp))
return GetDC(NULL);
return EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE;
}
} // namespace gfx
|