| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <GL/osmesa.h> | 5 #include <GL/osmesa.h> |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include "ui/gfx/gl/gl_context_osmesa.h" |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "ui/gfx/gl/gl_bindings.h" | 10 #include "ui/gfx/gl/gl_bindings.h" |
| 11 #include "ui/gfx/gl/gl_context_osmesa.h" | 11 #include "ui/gfx/gl/gl_surface_osmesa.h" |
| 12 | 12 |
| 13 namespace gfx { | 13 namespace gfx { |
| 14 | 14 |
| 15 GLContextOSMesa::GLContextOSMesa(GLSurfaceOSMesa* surface) | 15 GLContextOSMesa::GLContextOSMesa() |
| 16 : surface_(surface), | 16 : context_(NULL) { |
| 17 context_(NULL) | |
| 18 { | |
| 19 } | 17 } |
| 20 | 18 |
| 21 GLContextOSMesa::~GLContextOSMesa() { | 19 GLContextOSMesa::~GLContextOSMesa() { |
| 22 Destroy(); | 20 Destroy(); |
| 23 } | 21 } |
| 24 | 22 |
| 25 bool GLContextOSMesa::Initialize(GLuint format, GLContext* shared_context) { | 23 bool GLContextOSMesa::Initialize(GLContext* shared_context, |
| 24 GLSurface* compatible_surface) { |
| 26 DCHECK(!context_); | 25 DCHECK(!context_); |
| 27 | 26 |
| 28 OSMesaContext shared_handle = NULL; | 27 OSMesaContext shared_handle = NULL; |
| 29 if (shared_context) | 28 if (shared_context) |
| 30 shared_handle = static_cast<OSMesaContext>(shared_context->GetHandle()); | 29 shared_handle = static_cast<OSMesaContext>(shared_context->GetHandle()); |
| 31 | 30 |
| 31 GLuint format = |
| 32 static_cast<GLSurfaceOSMesa*>(compatible_surface)->GetFormat(); |
| 32 context_ = OSMesaCreateContextExt(format, | 33 context_ = OSMesaCreateContextExt(format, |
| 33 24, // depth bits | 34 24, // depth bits |
| 34 8, // stencil bits | 35 8, // stencil bits |
| 35 0, // accum bits | 36 0, // accum bits |
| 36 shared_handle); | 37 shared_handle); |
| 37 if (!context_) { | 38 if (!context_) { |
| 38 LOG(ERROR) << "OSMesaCreateContextExt failed."; | 39 LOG(ERROR) << "OSMesaCreateContextExt failed."; |
| 39 return false; | 40 return false; |
| 40 } | 41 } |
| 41 | 42 |
| 42 return true; | 43 return true; |
| 43 } | 44 } |
| 44 | 45 |
| 45 void GLContextOSMesa::Destroy() { | 46 void GLContextOSMesa::Destroy() { |
| 46 if (context_) { | 47 if (context_) { |
| 47 OSMesaDestroyContext(static_cast<OSMesaContext>(context_)); | 48 OSMesaDestroyContext(static_cast<OSMesaContext>(context_)); |
| 48 context_ = NULL; | 49 context_ = NULL; |
| 49 } | 50 } |
| 50 | |
| 51 if (surface_.get()) { | |
| 52 surface_->Destroy(); | |
| 53 surface_.reset(); | |
| 54 } | |
| 55 } | 51 } |
| 56 | 52 |
| 57 bool GLContextOSMesa::MakeCurrent() { | 53 bool GLContextOSMesa::MakeCurrent(GLSurface* surface) { |
| 58 DCHECK(context_); | 54 DCHECK(context_); |
| 59 | 55 |
| 60 gfx::Size size = surface_->GetSize(); | 56 gfx::Size size = surface->GetSize(); |
| 61 | 57 |
| 62 if (!OSMesaMakeCurrent(static_cast<OSMesaContext>(context_), | 58 if (!OSMesaMakeCurrent(static_cast<OSMesaContext>(context_), |
| 63 surface_->GetHandle(), | 59 surface->GetHandle(), |
| 64 GL_UNSIGNED_BYTE, | 60 GL_UNSIGNED_BYTE, |
| 65 size.width(), size.height())) { | 61 size.width(), |
| 62 size.height())) { |
| 63 LOG(ERROR) << "OSMesaMakeCurrent failed."; |
| 64 Destroy(); |
| 66 return false; | 65 return false; |
| 67 } | 66 } |
| 68 | 67 |
| 69 // Row 0 is at the top. | 68 // Row 0 is at the top. |
| 70 OSMesaPixelStore(OSMESA_Y_UP, 0); | 69 OSMesaPixelStore(OSMESA_Y_UP, 0); |
| 71 | 70 |
| 72 return true; | 71 return true; |
| 73 } | 72 } |
| 74 | 73 |
| 75 bool GLContextOSMesa::IsCurrent() { | 74 void GLContextOSMesa::ReleaseCurrent(GLSurface* surface) { |
| 76 DCHECK(context_); | 75 if (!IsCurrent(surface)) |
| 77 return context_ == OSMesaGetCurrentContext(); | 76 return; |
| 77 |
| 78 OSMesaMakeCurrent(NULL, NULL, GL_UNSIGNED_BYTE, 0, 0); |
| 78 } | 79 } |
| 79 | 80 |
| 80 bool GLContextOSMesa::IsOffscreen() { | 81 bool GLContextOSMesa::IsCurrent(GLSurface* surface) { |
| 81 // TODO(apatrick): remove this from GLContext interface. | 82 DCHECK(context_); |
| 82 return surface_->IsOffscreen(); | 83 if (context_ != OSMesaGetCurrentContext()) |
| 83 } | 84 return false; |
| 84 | 85 |
| 85 bool GLContextOSMesa::SwapBuffers() { | 86 if (surface) { |
| 86 // TODO(apatrick): remove this from GLContext interface. | 87 GLint width; |
| 87 return surface_->SwapBuffers(); | 88 GLint height; |
| 88 } | 89 GLint format; |
| 90 void* buffer = NULL; |
| 91 OSMesaGetColorBuffer(context_, &width, &height, &format, &buffer); |
| 92 if (buffer != surface->GetHandle()) |
| 93 return false; |
| 94 } |
| 89 | 95 |
| 90 gfx::Size GLContextOSMesa::GetSize() { | 96 return true; |
| 91 // TODO(apatrick): remove this from GLContext interface. | |
| 92 return surface_->GetSize(); | |
| 93 } | 97 } |
| 94 | 98 |
| 95 void* GLContextOSMesa::GetHandle() { | 99 void* GLContextOSMesa::GetHandle() { |
| 96 return context_; | 100 return context_; |
| 97 } | 101 } |
| 98 | 102 |
| 99 void GLContextOSMesa::SetSwapInterval(int interval) { | 103 void GLContextOSMesa::SetSwapInterval(int interval) { |
| 100 DCHECK(IsCurrent()); | 104 DCHECK(IsCurrent(NULL)); |
| 101 NOTREACHED() << "Attempt to call SetSwapInterval on an GLContextOSMesa."; | 105 NOTREACHED() << "Attempt to call SetSwapInterval on an GLContextOSMesa."; |
| 102 } | 106 } |
| 103 | 107 |
| 104 } // namespace gfx | 108 } // namespace gfx |
| OLD | NEW |