Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_surface_osmesa.cc
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 }
18
19 bool GLSurfaceOSMesa::Initialize() {
20   return Resize(size_);
21 }
22
23 void GLSurfaceOSMesa::Destroy() {
24   buffer_.reset();
25 }
26
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);
36   }
37
38   // Preserve the old buffer.
39   scoped_ptr<int32[]> old_buffer(buffer_.release());
40
41   // Allocate a new one.
42   buffer_.reset(new int32[new_size.GetArea()]);
43   memset(buffer_.get(), 0, new_size.GetArea() * sizeof(buffer_[0]));
44
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];
52       }
53     }
54   }
55
56   size_ = new_size;
57
58   return true;
59 }
60
61 bool GLSurfaceOSMesa::IsOffscreen() {
62   return true;
63 }
64
65 bool GLSurfaceOSMesa::SwapBuffers() {
66   NOTREACHED() << "Should not call SwapBuffers on an GLSurfaceOSMesa.";
67   return false;
68 }
69
70 gfx::Size GLSurfaceOSMesa::GetSize() {
71   return size_;
72 }
73
74 void* GLSurfaceOSMesa::GetHandle() {
75   return buffer_.get();
76 }
77
78 unsigned GLSurfaceOSMesa::GetFormat() {
79   return format_;
80 }
81
82 GLSurfaceOSMesa::~GLSurfaceOSMesa() {
83   Destroy();
84 }
85
86 bool GLSurfaceOSMesaHeadless::IsOffscreen() { return false; }
87
88 bool GLSurfaceOSMesaHeadless::SwapBuffers() { return true; }
89
90 GLSurfaceOSMesaHeadless::GLSurfaceOSMesaHeadless()
91     : GLSurfaceOSMesa(OSMESA_BGRA, gfx::Size(1, 1)) {}
92
93 GLSurfaceOSMesaHeadless::~GLSurfaceOSMesaHeadless() { Destroy(); }
94
95 }  // namespace gfx