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(OSMesaSurfaceFormat format
,
15 const gfx::Size
& size
)
18 case OSMesaSurfaceFormatBGRA
:
19 format_
= OSMESA_BGRA
;
21 case OSMesaSurfaceFormatRGBA
:
22 format_
= OSMESA_RGBA
;
25 // Implementations of OSMesa surface do not support having a 0 size. In such
26 // cases use a (1, 1) surface.
27 if (size_
.GetArea() == 0)
31 bool GLSurfaceOSMesa::Initialize() {
35 void GLSurfaceOSMesa::Destroy() {
39 bool GLSurfaceOSMesa::Resize(const gfx::Size
& new_size
) {
40 scoped_ptr
<ui::ScopedMakeCurrent
> scoped_make_current
;
41 GLContext
* current_context
= GLContext::GetCurrent();
43 current_context
&& current_context
->IsCurrent(this);
45 scoped_make_current
.reset(
46 new ui::ScopedMakeCurrent(current_context
, this));
47 current_context
->ReleaseCurrent(this);
50 // Preserve the old buffer.
51 scoped_ptr
<int32
[]> old_buffer(buffer_
.release());
53 // Allocate a new one.
54 buffer_
.reset(new int32
[new_size
.GetArea()]);
55 memset(buffer_
.get(), 0, new_size
.GetArea() * sizeof(buffer_
[0]));
57 // Copy the old back buffer into the new buffer.
58 if (old_buffer
.get()) {
59 int copy_width
= std::min(size_
.width(), new_size
.width());
60 int copy_height
= std::min(size_
.height(), new_size
.height());
61 for (int y
= 0; y
< copy_height
; ++y
) {
62 for (int x
= 0; x
< copy_width
; ++x
) {
63 buffer_
[y
* new_size
.width() + x
] = old_buffer
[y
* size_
.width() + x
];
73 bool GLSurfaceOSMesa::IsOffscreen() {
77 bool GLSurfaceOSMesa::SwapBuffers() {
78 NOTREACHED() << "Should not call SwapBuffers on an GLSurfaceOSMesa.";
82 gfx::Size
GLSurfaceOSMesa::GetSize() {
86 void* GLSurfaceOSMesa::GetHandle() {
90 unsigned GLSurfaceOSMesa::GetFormat() {
94 GLSurfaceOSMesa::~GLSurfaceOSMesa() {
98 bool GLSurfaceOSMesaHeadless::IsOffscreen() { return false; }
100 bool GLSurfaceOSMesaHeadless::SwapBuffers() { return true; }
102 GLSurfaceOSMesaHeadless::GLSurfaceOSMesaHeadless()
103 : GLSurfaceOSMesa(OSMesaSurfaceFormatBGRA
, gfx::Size(1, 1)) {
106 GLSurfaceOSMesaHeadless::~GLSurfaceOSMesaHeadless() { Destroy(); }