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 "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"
14 GLSurfaceOSMesa::GLSurfaceOSMesa(unsigned format
, const gfx::Size
& size
)
19 bool GLSurfaceOSMesa::Initialize() {
23 void GLSurfaceOSMesa::Destroy() {
27 bool GLSurfaceOSMesa::Resize(const gfx::Size
& new_size
) {
28 scoped_ptr
<ui::ScopedMakeCurrent
> scoped_make_current
;
29 GLContext
* current_context
= GLContext::GetCurrent();
31 current_context
&& current_context
->IsCurrent(this);
33 scoped_make_current
.reset(
34 new ui::ScopedMakeCurrent(current_context
, this));
35 current_context
->ReleaseCurrent(this);
38 // Preserve the old buffer.
39 scoped_ptr
<int32
[]> old_buffer(buffer_
.release());
41 // Allocate a new one.
42 buffer_
.reset(new int32
[new_size
.GetArea()]);
43 memset(buffer_
.get(), 0, new_size
.GetArea() * sizeof(buffer_
[0]));
45 // Copy the old back buffer into the new buffer.
46 if (old_buffer
.get()) {
47 int copy_width
= std::min(size_
.width(), new_size
.width());
48 int copy_height
= std::min(size_
.height(), new_size
.height());
49 for (int y
= 0; y
< copy_height
; ++y
) {
50 for (int x
= 0; x
< copy_width
; ++x
) {
51 buffer_
[y
* new_size
.width() + x
] = old_buffer
[y
* size_
.width() + x
];
61 bool GLSurfaceOSMesa::IsOffscreen() {
65 bool GLSurfaceOSMesa::SwapBuffers() {
66 NOTREACHED() << "Should not call SwapBuffers on an GLSurfaceOSMesa.";
70 gfx::Size
GLSurfaceOSMesa::GetSize() {
74 void* GLSurfaceOSMesa::GetHandle() {
78 unsigned GLSurfaceOSMesa::GetFormat() {
82 GLSurfaceOSMesa::~GLSurfaceOSMesa() {
86 bool GLSurfaceOSMesaHeadless::IsOffscreen() { return false; }
88 bool GLSurfaceOSMesaHeadless::SwapBuffers() { return true; }
90 GLSurfaceOSMesaHeadless::GLSurfaceOSMesaHeadless()
91 : GLSurfaceOSMesa(OSMESA_BGRA
, gfx::Size(1, 1)) {}
93 GLSurfaceOSMesaHeadless::~GLSurfaceOSMesaHeadless() { Destroy(); }