Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ui / gl / gl_surface_osmesa.cc
blob8018fe83824dcc20ed7adf6ad93f8251b7914979
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"
12 namespace gfx {
14 GLSurfaceOSMesa::GLSurfaceOSMesa(unsigned format, const gfx::Size& size)
15 : format_(format),
16 size_(size) {
19 bool GLSurfaceOSMesa::Initialize() {
20 return Resize(size_);
23 void GLSurfaceOSMesa::Destroy() {
24 buffer_.reset();
27 bool GLSurfaceOSMesa::Resize(const gfx::Size& new_size) {
28 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current;
29 GLContext* current_context = GLContext::GetCurrent();
30 bool was_current =
31 current_context && current_context->IsCurrent(this);
32 if (was_current) {
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];
56 size_ = new_size;
58 return true;
61 bool GLSurfaceOSMesa::IsOffscreen() {
62 return true;
65 bool GLSurfaceOSMesa::SwapBuffers() {
66 NOTREACHED() << "Should not call SwapBuffers on an GLSurfaceOSMesa.";
67 return false;
70 gfx::Size GLSurfaceOSMesa::GetSize() {
71 return size_;
74 void* GLSurfaceOSMesa::GetHandle() {
75 return buffer_.get();
78 unsigned GLSurfaceOSMesa::GetFormat() {
79 return format_;
82 GLSurfaceOSMesa::~GLSurfaceOSMesa() {
83 Destroy();
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(); }
95 } // namespace gfx