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. 4 5 #include "base/logging.h" 6 #include "third_party/mesa/src/include/GL/osmesa.h" 7 #include "ui/gl/gl_bindings.h" 8 #include "ui/gl/gl_context.h" 9 #include "ui/gl/gl_surface_osmesa.h" 10 #include "ui/gl/scoped_make_current.h" 11 12 namespace gfx { 13 14 GLSurfaceOSMesa::GLSurfaceOSMesa(unsigned format, const gfx::Size& size) 15 : format_(format), 16 size_(size) { 17 // Implementations of OSMesa surface do not support having a 0 size. In such 18 // cases use a (1, 1) surface. 19 if (size_.GetArea() == 0) 20 size_.SetSize(1, 1); 21 } 22 23 bool GLSurfaceOSMesa::Initialize() { 24 return Resize(size_); 25 } 26 27 void GLSurfaceOSMesa::Destroy() { 28 buffer_.reset(); 29 } 30 31 bool GLSurfaceOSMesa::Resize(const gfx::Size& new_size) { 32 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current; 33 GLContext* current_context = GLContext::GetCurrent(); 34 bool was_current = 35 current_context && current_context->IsCurrent(this); 36 if (was_current) { 37 scoped_make_current.reset( 38 new ui::ScopedMakeCurrent(current_context, this)); 39 current_context->ReleaseCurrent(this); 40 } 41 42 // Preserve the old buffer. 43 scoped_ptr<int32[]> old_buffer(buffer_.release()); 44 45 // Allocate a new one. 46 buffer_.reset(new int32[new_size.GetArea()]); 47 memset(buffer_.get(), 0, new_size.GetArea() * sizeof(buffer_[0])); 48 49 // Copy the old back buffer into the new buffer. 50 if (old_buffer.get()) { 51 int copy_width = std::min(size_.width(), new_size.width()); 52 int copy_height = std::min(size_.height(), new_size.height()); 53 for (int y = 0; y < copy_height; ++y) { 54 for (int x = 0; x < copy_width; ++x) { 55 buffer_[y * new_size.width() + x] = old_buffer[y * size_.width() + x]; 56 } 57 } 58 } 59 60 size_ = new_size; 61 62 return true; 63 } 64 65 bool GLSurfaceOSMesa::IsOffscreen() { 66 return true; 67 } 68 69 bool GLSurfaceOSMesa::SwapBuffers() { 70 NOTREACHED() << "Should not call SwapBuffers on an GLSurfaceOSMesa."; 71 return false; 72 } 73 74 gfx::Size GLSurfaceOSMesa::GetSize() { 75 return size_; 76 } 77 78 void* GLSurfaceOSMesa::GetHandle() { 79 return buffer_.get(); 80 } 81 82 unsigned GLSurfaceOSMesa::GetFormat() { 83 return format_; 84 } 85 86 GLSurfaceOSMesa::~GLSurfaceOSMesa() { 87 Destroy(); 88 } 89 90 bool GLSurfaceOSMesaHeadless::IsOffscreen() { return false; } 91 92 bool GLSurfaceOSMesaHeadless::SwapBuffers() { return true; } 93 94 GLSurfaceOSMesaHeadless::GLSurfaceOSMesaHeadless() 95 : GLSurfaceOSMesa(OSMESA_BGRA, gfx::Size(1, 1)) {} 96 97 GLSurfaceOSMesaHeadless::~GLSurfaceOSMesaHeadless() { Destroy(); } 98 99 } // namespace gfx 100