summaryrefslogtreecommitdiffstats
path: root/app/gfx/gl/gl_context_osmesa.cc
blob: 9b7319588aeb797787680cf36ff9ad759c559507 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <GL/osmesa.h>

#include <algorithm>

#include "app/gfx/gl/gl_bindings.h"
#include "app/gfx/gl/gl_context_osmesa.h"
#include "base/logging.h"

namespace gfx {

OSMesaGLContext::OSMesaGLContext() : context_(NULL)
{
}

OSMesaGLContext::~OSMesaGLContext() {
}

bool OSMesaGLContext::Initialize(GLuint format, GLContext* shared_context) {
  DCHECK(!context_);

  size_ = gfx::Size(1, 1);
  buffer_.reset(new int32[1]);

  OSMesaContext shared_handle = NULL;
  if (shared_context)
    shared_handle = static_cast<OSMesaContext>(shared_context->GetHandle());

  context_ = OSMesaCreateContextExt(format,
                                    24,  // depth bits
                                    8,  // stencil bits
                                    0,  // accum bits
                                    shared_handle);
  if (!context_)
    return false;

  if (!MakeCurrent()) {
    Destroy();
    return false;
  }

  // Row 0 is at the top.
  OSMesaPixelStore(OSMESA_Y_UP, 0);

  if (!InitializeCommon()) {
    Destroy();
    return false;
  }

  return true;
}

void OSMesaGLContext::Destroy() {
  if (context_) {
    OSMesaDestroyContext(static_cast<OSMesaContext>(context_));
    context_ = NULL;
  }
  buffer_.reset();
  size_ = gfx::Size();
}

bool OSMesaGLContext::MakeCurrent() {
  DCHECK(context_);
  return OSMesaMakeCurrent(static_cast<OSMesaContext>(context_),
                           buffer_.get(),
                           GL_UNSIGNED_BYTE,
                           size_.width(), size_.height()) == GL_TRUE;
  return true;
}

bool OSMesaGLContext::IsCurrent() {
  DCHECK(context_);
  return context_ == OSMesaGetCurrentContext();
}

bool OSMesaGLContext::IsOffscreen() {
  return true;
}

bool OSMesaGLContext::SwapBuffers() {
  NOTREACHED() << "Should not call SwapBuffers on an OSMesaGLContext.";
  return false;
}

gfx::Size OSMesaGLContext::GetSize() {
  return size_;
}

void* OSMesaGLContext::GetHandle() {
  return context_;
}

void OSMesaGLContext::Resize(const gfx::Size& new_size) {
  if (new_size == size_)
    return;

  // Allocate a new back buffer.
  scoped_array<int32> new_buffer(new int32[new_size.GetArea()]);
  memset(new_buffer.get(), 0, new_size.GetArea() * sizeof(new_buffer[0]));

  // Copy the current back buffer into the new buffer.
  int copy_width = std::min(size_.width(), new_size.width());
  int copy_height = std::min(size_.height(), new_size.height());
  for (int y = 0; y < copy_height; ++y) {
    for (int x = 0; x < copy_width; ++x) {
      new_buffer[y * new_size.width() + x] = buffer_[y * size_.width() + x];
    }
  }

  buffer_.reset(new_buffer.release());
  size_ = new_size;

  // If this context is current, need to call MakeCurrent again so OSMesa uses
  // the new buffer.
  if (IsCurrent())
    MakeCurrent();
}

}  // namespace gfx