summaryrefslogtreecommitdiffstats
path: root/ui/gfx/gl/gl_context_osmesa.cc
blob: 6da01d1d0369a7f46c20aa9c3c41ef040f2c6bd1 (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
// Copyright (c) 2011 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 "base/logging.h"
#include "ui/gfx/gl/gl_bindings.h"
#include "ui/gfx/gl/gl_context_osmesa.h"

namespace gfx {

GLContextOSMesa::GLContextOSMesa(GLSurfaceOSMesa* surface)
    : surface_(surface),
      context_(NULL)
{
}

GLContextOSMesa::~GLContextOSMesa() {
}

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

  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_) {
    LOG(ERROR) << "OSMesaCreateContextExt failed.";
    return false;
  }

  return true;
}

void GLContextOSMesa::Destroy() {
  if (context_) {
    OSMesaDestroyContext(static_cast<OSMesaContext>(context_));
    context_ = NULL;
  }

  surface_->Destroy();
  surface_.reset();
}

bool GLContextOSMesa::MakeCurrent() {
  DCHECK(context_);

  gfx::Size size = surface_->GetSize();

  if (!OSMesaMakeCurrent(static_cast<OSMesaContext>(context_),
                         surface_->GetHandle(),
                         GL_UNSIGNED_BYTE,
                         size.width(), size.height())) {
    return false;
  }

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

  return true;
}

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

bool GLContextOSMesa::IsOffscreen() {
  // TODO(apatrick): remove this from GLContext interface.
  return surface_->IsOffscreen();
}

bool GLContextOSMesa::SwapBuffers() {
  // TODO(apatrick): remove this from GLContext interface.
  return surface_->SwapBuffers();
}

gfx::Size GLContextOSMesa::GetSize() {
  // TODO(apatrick): remove this from GLContext interface.
  return surface_->GetSize();
}

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

void GLContextOSMesa::SetSwapInterval(int interval) {
  DCHECK(IsCurrent());
  NOTREACHED() << "Attempt to call SetSwapInterval on an GLContextOSMesa.";
}

}  // namespace gfx