Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ui / gl / gl_context_osmesa.cc
bloba279177011f7e85c428c8bc4e02088cf9b1292f2
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_context_osmesa.h"
7 #include <GL/osmesa.h>
9 #include "base/logging.h"
10 #include "ui/gfx/size.h"
11 #include "ui/gl/gl_bindings.h"
12 #include "ui/gl/gl_surface.h"
14 namespace gfx {
16 GLContextOSMesa::GLContextOSMesa(GLShareGroup* share_group)
17 : GLContext(share_group),
18 context_(NULL) {
21 bool GLContextOSMesa::Initialize(GLSurface* compatible_surface,
22 GpuPreference gpu_preference) {
23 DCHECK(!context_);
25 OSMesaContext share_handle = static_cast<OSMesaContext>(
26 share_group() ? share_group()->GetHandle() : NULL);
28 GLuint format = compatible_surface->GetFormat();
29 DCHECK_NE(format, (unsigned)0);
30 context_ = OSMesaCreateContextExt(format,
31 0, // depth bits
32 0, // stencil bits
33 0, // accum bits
34 share_handle);
35 if (!context_) {
36 LOG(ERROR) << "OSMesaCreateContextExt failed.";
37 return false;
40 return true;
43 void GLContextOSMesa::Destroy() {
44 if (context_) {
45 OSMesaDestroyContext(static_cast<OSMesaContext>(context_));
46 context_ = NULL;
50 bool GLContextOSMesa::MakeCurrent(GLSurface* surface) {
51 DCHECK(context_);
53 gfx::Size size = surface->GetSize();
55 if (!OSMesaMakeCurrent(context_,
56 surface->GetHandle(),
57 GL_UNSIGNED_BYTE,
58 size.width(),
59 size.height())) {
60 LOG(ERROR) << "OSMesaMakeCurrent failed.";
61 Destroy();
62 return false;
65 // Row 0 is at the top.
66 OSMesaPixelStore(OSMESA_Y_UP, 0);
68 SetCurrent(this, surface);
69 if (!InitializeExtensionBindings()) {
70 ReleaseCurrent(surface);
71 return false;
74 if (!surface->OnMakeCurrent(this)) {
75 LOG(ERROR) << "Could not make current.";
76 return false;
79 SetRealGLApi();
80 return true;
83 void GLContextOSMesa::ReleaseCurrent(GLSurface* surface) {
84 if (!IsCurrent(surface))
85 return;
87 SetCurrent(NULL, NULL);
88 OSMesaMakeCurrent(NULL, NULL, GL_UNSIGNED_BYTE, 0, 0);
91 bool GLContextOSMesa::IsCurrent(GLSurface* surface) {
92 DCHECK(context_);
94 bool native_context_is_current =
95 context_ == OSMesaGetCurrentContext();
97 // If our context is current then our notion of which GLContext is
98 // current must be correct. On the other hand, third-party code
99 // using OpenGL might change the current context.
100 DCHECK(!native_context_is_current || (GetCurrent() == this));
102 if (!native_context_is_current)
103 return false;
105 if (surface) {
106 GLint width;
107 GLint height;
108 GLint format;
109 void* buffer = NULL;
110 OSMesaGetColorBuffer(context_, &width, &height, &format, &buffer);
111 if (buffer != surface->GetHandle())
112 return false;
115 return true;
118 void* GLContextOSMesa::GetHandle() {
119 return context_;
122 void GLContextOSMesa::SetSwapInterval(int interval) {
123 DCHECK(IsCurrent(NULL));
124 DLOG(INFO) << "GLContextOSMesa::SetSwapInterval is ignored.";
127 GLContextOSMesa::~GLContextOSMesa() {
128 Destroy();
131 } // namespace gfx