Add ICU message format support
[chromium-blink-merge.git] / ui / gl / gl_context_wgl.cc
blob81926a644005fb57332802e799f776e7ed98d6b7
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 // This file implements the GLContextWGL and PbufferGLContext classes.
7 #include "ui/gl/gl_context_wgl.h"
9 #include "base/logging.h"
10 #include "base/trace_event/trace_event.h"
11 #include "ui/gl/gl_bindings.h"
12 #include "ui/gl/gl_implementation.h"
13 #include "ui/gl/gl_surface_wgl.h"
15 namespace gfx {
17 GLContextWGL::GLContextWGL(GLShareGroup* share_group)
18 : GLContextReal(share_group), context_(nullptr) {
21 bool GLContextWGL::Initialize(
22 GLSurface* compatible_surface, GpuPreference gpu_preference) {
23 // Get the handle of another initialized context in the share group _before_
24 // setting context_. Otherwise this context will be considered initialized
25 // and could potentially be returned by GetHandle.
26 HGLRC share_handle = static_cast<HGLRC>(share_group()->GetHandle());
28 context_ = wglCreateContext(
29 static_cast<HDC>(compatible_surface->GetHandle()));
30 if (!context_) {
31 LOG(ERROR) << "Failed to create GL context.";
32 Destroy();
33 return false;
36 if (share_handle) {
37 if (!wglShareLists(share_handle, context_)) {
38 LOG(ERROR) << "Could not share GL contexts.";
39 Destroy();
40 return false;
44 return true;
47 void GLContextWGL::Destroy() {
48 if (context_) {
49 wglDeleteContext(context_);
50 context_ = nullptr;
54 bool GLContextWGL::MakeCurrent(GLSurface* surface) {
55 DCHECK(context_);
56 if (IsCurrent(surface))
57 return true;
59 ScopedReleaseCurrent release_current;
60 TRACE_EVENT0("gpu", "GLContextWGL::MakeCurrent");
62 if (!wglMakeCurrent(static_cast<HDC>(surface->GetHandle()), context_)) {
63 LOG(ERROR) << "Unable to make gl context current.";
64 return false;
67 // Set this as soon as the context is current, since we might call into GL.
68 SetRealGLApi();
70 SetCurrent(surface);
71 if (!InitializeDynamicBindings()) {
72 return false;
75 if (!surface->OnMakeCurrent(this)) {
76 LOG(ERROR) << "Could not make current.";
77 return false;
80 release_current.Cancel();
81 return true;
84 void GLContextWGL::ReleaseCurrent(GLSurface* surface) {
85 if (!IsCurrent(surface))
86 return;
88 SetCurrent(nullptr);
89 wglMakeCurrent(nullptr, nullptr);
92 bool GLContextWGL::IsCurrent(GLSurface* surface) {
93 bool native_context_is_current =
94 wglGetCurrentContext() == context_;
96 // If our context is current then our notion of which GLContext is
97 // current must be correct. On the other hand, third-party code
98 // using OpenGL might change the current context.
99 DCHECK(!native_context_is_current || (GetRealCurrent() == this));
101 if (!native_context_is_current)
102 return false;
104 if (surface) {
105 if (wglGetCurrentDC() != surface->GetHandle())
106 return false;
109 return true;
112 void* GLContextWGL::GetHandle() {
113 return context_;
116 void GLContextWGL::OnSetSwapInterval(int interval) {
117 DCHECK(IsCurrent(nullptr));
118 if (gfx::g_driver_wgl.ext.b_WGL_EXT_swap_control) {
119 wglSwapIntervalEXT(interval);
120 } else {
121 LOG(WARNING) <<
122 "Could not disable vsync: driver does not "
123 "support WGL_EXT_swap_control";
127 std::string GLContextWGL::GetExtensions() {
128 const char* extensions = nullptr;
129 if (g_driver_wgl.fn.wglGetExtensionsStringARBFn)
130 extensions = wglGetExtensionsStringARB(GLSurfaceWGL::GetDisplayDC());
131 else if (g_driver_wgl.fn.wglGetExtensionsStringEXTFn)
132 extensions = wglGetExtensionsStringEXT();
134 if (extensions)
135 return GLContext::GetExtensions() + " " + extensions;
137 return GLContext::GetExtensions();
140 GLContextWGL::~GLContextWGL() {
141 Destroy();
144 } // namespace gfx