summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/gl_context.h
blob: 832624db9af3a556d28a0a74d883967e3f5c3b90 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) 2010 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.

#ifndef GPU_COMMAND_BUFFER_SERVICE_GL_CONTEXT_H_
#define GPU_COMMAND_BUFFER_SERVICE_GL_CONTEXT_H_

#include <build/build_config.h>

#include "base/basictypes.h"
#include "gfx/native_widget_types.h"
#include "gfx/size.h"
#include "gpu/command_buffer/common/logging.h"
#include "gpu/command_buffer/service/gl_utils.h"

class AcceleratedSurface;

namespace gpu {

#if defined(UNIT_TEST)
typedef void* HDC;
struct Display;
typedef void* GLContextHandle;
typedef void* PbufferHandle;
#elif defined(OS_WIN)
typedef HGLRC GLContextHandle;
typedef HPBUFFERARB PbufferHandle;
#elif defined(OS_LINUX)
typedef GLXContext GLContextHandle;
typedef GLXPbuffer PbufferHandle;
#elif defined(OS_MACOSX)
typedef CGLContextObj GLContextHandle;
typedef CGLPBufferObj PbufferHandle;
#endif

bool InitializeGLEW();

// Encapsulates an OpenGL context, hiding platform specific management.
class GLContext {
 public:
  GLContext();
  virtual ~GLContext();

  // Destroys the GL context.
  virtual void Destroy() = 0;

  // Makes the GL context current on the current thread.
  virtual bool MakeCurrent() = 0;

  // Returns true if this context is offscreen.
  virtual bool IsOffscreen() = 0;

  // Swaps front and back buffers. This has no effect for off-screen
  // contexts.
  virtual void SwapBuffers() = 0;

  // Get the size of the back buffer.
  virtual gfx::Size GetSize() = 0;

  // Get the underlying platform specific GL context "handle".
  virtual GLContextHandle GetHandle() = 0;

 private:
  DISALLOW_COPY_AND_ASSIGN(GLContext);
};

// This class is a wrapper around a GL context that renders directly to a
// window.
class ViewGLContext : public GLContext {
 public:
#if defined(OS_WIN)
  explicit ViewGLContext(gfx::PluginWindowHandle window)
      : window_(window),
        device_context_(NULL),
        context_(NULL) {
    DCHECK(window);
  }
#elif defined(OS_LINUX)
  ViewGLContext(Display* display, gfx::PluginWindowHandle window)
      : display_(display),
        window_(window),
        context_(NULL) {
    DCHECK(display);
    DCHECK(window);
  }
#elif defined(OS_MACOSX)
  explicit ViewGLContext(AcceleratedSurface* surface) : surface_(surface) {
    DCHECK(surface);
  }
#endif

  // Initializes the GL context.
  bool Initialize(bool multisampled);

  virtual void Destroy();
  virtual bool MakeCurrent();
  virtual bool IsOffscreen();
  virtual void SwapBuffers();
  virtual gfx::Size GetSize();
  virtual GLContextHandle GetHandle();

 private:
#if defined(OS_WIN)
  gfx::PluginWindowHandle window_;
  HDC device_context_;
  GLContextHandle context_;
#elif defined(OS_LINUX)
  Display* display_;
  gfx::PluginWindowHandle window_;
  GLContextHandle context_;
#elif defined(OS_MACOSX)
  AcceleratedSurface* surface_;
#endif

  DISALLOW_COPY_AND_ASSIGN(ViewGLContext);
};

// This class is a wrapper around a GL context used for offscreen rendering.
// It is initially backed by a 1x1 pbuffer. Use it to create an FBO to do useful
// rendering.
class PbufferGLContext : public GLContext {
 public:
#if defined(OS_WIN)
  PbufferGLContext()
      : context_(NULL),
        device_context_(NULL),
        pbuffer_(NULL) {
  }
#elif defined(OS_LINUX)
  explicit PbufferGLContext(Display* display)
      : context_(NULL),
        display_(display),
        pbuffer_(NULL) {
    DCHECK(display_);
  }
#elif defined(OS_MACOSX)
  PbufferGLContext()
      : context_(NULL),
        pbuffer_(NULL) {
  }
#endif

  // Initializes the GL context.
  bool Initialize(GLContext* shared_context);

  virtual void Destroy();
  virtual bool MakeCurrent();
  virtual bool IsOffscreen();
  virtual void SwapBuffers();
  virtual gfx::Size GetSize();
  virtual GLContextHandle GetHandle();

 private:
  GLContextHandle context_;
#if defined(OS_WIN)
  HDC device_context_;
  PbufferHandle pbuffer_;
#elif defined(OS_LINUX)
  Display* display_;
  PbufferHandle pbuffer_;
#elif defined(OS_MACOSX)
  PbufferHandle pbuffer_;
#endif
  DISALLOW_COPY_AND_ASSIGN(PbufferGLContext);
};

}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_SERVICE_GL_CONTEXT_H_