blob: f09aa35924daf21f1ab19a5eb57ba463eb8e011c [file] [log] [blame]
[email protected]9fc44162012-01-23 22:56:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]24edbd02011-04-14 00:11:592// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]c9e2cbbb2012-05-12 21:17:275#include "ui/gl/gl_surface.h"
[email protected]24edbd02011-04-14 00:11:596
[email protected]0f5e8882011-11-08 22:29:387#include <algorithm>
8#include <vector>
9
10#include "base/command_line.h"
[email protected]d13f35d2012-05-18 02:28:1511#include "base/debug/trace_event.h"
[email protected]b6a9c042011-11-04 18:56:0512#include "base/lazy_instance.h"
[email protected]1b5eee12011-10-26 21:44:1213#include "base/logging.h"
[email protected]c777de52011-09-09 23:08:5614#include "base/threading/thread_local.h"
[email protected]c9e2cbbb2012-05-12 21:17:2715#include "ui/gl/gl_context.h"
16#include "ui/gl/gl_implementation.h"
[email protected]43390252011-07-28 12:54:2017
[email protected]24edbd02011-04-14 00:11:5918namespace gfx {
19
[email protected]b6a9c042011-11-04 18:56:0520namespace {
[email protected]9fc44162012-01-23 22:56:4121base::LazyInstance<base::ThreadLocalPointer<GLSurface> >::Leaky
22 current_surface_ = LAZY_INSTANCE_INITIALIZER;
[email protected]b6a9c042011-11-04 18:56:0523} // namespace
[email protected]c777de52011-09-09 23:08:5624
[email protected]0f5e8882011-11-08 22:29:3825// static
26bool GLSurface::InitializeOneOff() {
27 static bool initialized = false;
28 if (initialized)
29 return true;
30
[email protected]d13f35d2012-05-18 02:28:1531 TRACE_EVENT0("gpu", "GLSurface::InitializeOneOff");
32
[email protected]0f5e8882011-11-08 22:29:3833 std::vector<GLImplementation> allowed_impls;
34 GetAllowedGLImplementations(&allowed_impls);
35 DCHECK(!allowed_impls.empty());
36
37 // The default implementation is always the first one in list.
38 GLImplementation impl = allowed_impls[0];
39 bool fallback_to_osmesa = false;
40 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseGL)) {
41 std::string requested_implementation_name =
42 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switches::kUseGL);
43 if (requested_implementation_name == "any") {
44 fallback_to_osmesa = true;
45 } else if (requested_implementation_name == "swiftshader") {
46 impl = kGLImplementationEGLGLES2;
47 } else {
48 impl = GetNamedGLImplementation(requested_implementation_name);
49 if (std::find(allowed_impls.begin(),
50 allowed_impls.end(),
51 impl) == allowed_impls.end()) {
52 LOG(ERROR) << "Requested GL implementation is not available.";
53 return false;
54 }
55 }
56 }
57
58 initialized = InitializeGLBindings(impl) && InitializeOneOffInternal();
59 if (!initialized && fallback_to_osmesa) {
60 ClearGLBindings();
61 initialized = InitializeGLBindings(kGLImplementationOSMesaGL) &&
62 InitializeOneOffInternal();
63 }
64
65 if (initialized) {
66 DVLOG(1) << "Using "
67 << GetGLImplementationName(GetGLImplementation())
68 << " GL implementation.";
69 if (CommandLine::ForCurrentProcess()->HasSwitch(
70 switches::kEnableGPUServiceLogging))
71 InitializeDebugGLBindings();
72 }
73 return initialized;
74}
75
[email protected]ab9327c2012-05-02 02:38:0876GLSurface::GLSurface() {}
[email protected]ffae4022011-05-12 22:54:2977
[email protected]6f5fac9d12012-06-26 21:02:4578bool GLSurface::Initialize() {
[email protected]7b1a3da22011-04-27 23:56:3379 return true;
80}
81
[email protected]1b5eee12011-10-26 21:44:1282bool GLSurface::Resize(const gfx::Size& size) {
83 NOTIMPLEMENTED();
84 return false;
85}
86
[email protected]85cb4682013-04-20 00:54:2487bool GLSurface::Recreate() {
88 NOTIMPLEMENTED();
89 return false;
90}
91
[email protected]a7266a92012-06-28 02:11:0892bool GLSurface::DeferDraws() {
93 return false;
94}
95
[email protected]aa258822011-11-22 13:52:2596std::string GLSurface::GetExtensions() {
[email protected]007b3f82013-04-09 08:46:4597 return std::string();
[email protected]24edbd02011-04-14 00:11:5998}
99
[email protected]6f5fac9d12012-06-26 21:02:45100bool GLSurface::HasExtension(const char* name) {
101 std::string extensions = GetExtensions();
102 extensions += " ";
103
104 std::string delimited_name(name);
105 delimited_name += " ";
106
107 return extensions.find(delimited_name) != std::string::npos;
108}
109
[email protected]aa258822011-11-22 13:52:25110unsigned int GLSurface::GetBackingFrameBufferObject() {
111 return 0;
[email protected]1c75a3702011-11-11 14:15:28112}
113
114bool GLSurface::PostSubBuffer(int x, int y, int width, int height) {
115 return false;
116}
117
[email protected]e4f9f9b2011-10-17 13:22:01118bool GLSurface::OnMakeCurrent(GLContext* context) {
119 return true;
[email protected]2e7bbf22011-07-22 18:41:29120}
121
[email protected]a9327cf2013-01-30 00:01:30122bool GLSurface::SetBackbufferAllocation(bool allocated) {
123 return true;
[email protected]3ac71bf2012-05-16 15:55:53124}
125
126void GLSurface::SetFrontbufferAllocation(bool allocated) {
[email protected]8cc980c52011-10-14 20:35:51127}
128
[email protected]1b5eee12011-10-26 21:44:12129void* GLSurface::GetShareHandle() {
130 NOTIMPLEMENTED();
131 return NULL;
132}
133
134void* GLSurface::GetDisplay() {
135 NOTIMPLEMENTED();
136 return NULL;
137}
138
139void* GLSurface::GetConfig() {
140 NOTIMPLEMENTED();
141 return NULL;
142}
143
144unsigned GLSurface::GetFormat() {
145 NOTIMPLEMENTED();
146 return 0;
147}
148
[email protected]83f48ae2013-01-14 14:12:53149VSyncProvider* GLSurface::GetVSyncProvider() {
150 return NULL;
[email protected]a0c3fbb42012-11-01 15:40:36151}
152
[email protected]c777de52011-09-09 23:08:56153GLSurface* GLSurface::GetCurrent() {
[email protected]b6a9c042011-11-04 18:56:05154 return current_surface_.Pointer()->Get();
[email protected]c777de52011-09-09 23:08:56155}
156
[email protected]ab9327c2012-05-02 02:38:08157GLSurface::~GLSurface() {
158 if (GetCurrent() == this)
159 SetCurrent(NULL);
160}
161
[email protected]c777de52011-09-09 23:08:56162void GLSurface::SetCurrent(GLSurface* surface) {
[email protected]b6a9c042011-11-04 18:56:05163 current_surface_.Pointer()->Set(surface);
[email protected]c777de52011-09-09 23:08:56164}
165
[email protected]706b69f2012-07-27 04:59:30166bool GLSurface::ExtensionsContain(const char* c_extensions, const char* name) {
167 DCHECK(name);
168 if (!c_extensions)
169 return false;
170 std::string extensions(c_extensions);
171 extensions += " ";
172
173 std::string delimited_name(name);
174 delimited_name += " ";
175
176 return extensions.find(delimited_name) != std::string::npos;
177}
178
[email protected]ab9327c2012-05-02 02:38:08179GLSurfaceAdapter::GLSurfaceAdapter(GLSurface* surface) : surface_(surface) {}
[email protected]1b5eee12011-10-26 21:44:12180
181bool GLSurfaceAdapter::Initialize() {
182 return surface_->Initialize();
183}
184
185void GLSurfaceAdapter::Destroy() {
186 surface_->Destroy();
187}
188
189bool GLSurfaceAdapter::Resize(const gfx::Size& size) {
190 return surface_->Resize(size);
191}
192
[email protected]85cb4682013-04-20 00:54:24193bool GLSurfaceAdapter::Recreate() {
194 return surface_->Recreate();
195}
196
[email protected]a7266a92012-06-28 02:11:08197bool GLSurfaceAdapter::DeferDraws() {
198 return surface_->DeferDraws();
199}
200
[email protected]1b5eee12011-10-26 21:44:12201bool GLSurfaceAdapter::IsOffscreen() {
202 return surface_->IsOffscreen();
203}
204
205bool GLSurfaceAdapter::SwapBuffers() {
206 return surface_->SwapBuffers();
207}
208
[email protected]2368ad12011-11-23 19:49:27209bool GLSurfaceAdapter::PostSubBuffer(int x, int y, int width, int height) {
210 return surface_->PostSubBuffer(x, y, width, height);
211}
212
213std::string GLSurfaceAdapter::GetExtensions() {
214 return surface_->GetExtensions();
215}
216
[email protected]1b5eee12011-10-26 21:44:12217gfx::Size GLSurfaceAdapter::GetSize() {
218 return surface_->GetSize();
219}
220
221void* GLSurfaceAdapter::GetHandle() {
222 return surface_->GetHandle();
223}
224
225unsigned int GLSurfaceAdapter::GetBackingFrameBufferObject() {
226 return surface_->GetBackingFrameBufferObject();
227}
228
229bool GLSurfaceAdapter::OnMakeCurrent(GLContext* context) {
230 return surface_->OnMakeCurrent(context);
231}
232
[email protected]a9327cf2013-01-30 00:01:30233bool GLSurfaceAdapter::SetBackbufferAllocation(bool allocated) {
234 return surface_->SetBackbufferAllocation(allocated);
[email protected]3ac71bf2012-05-16 15:55:53235}
236
237void GLSurfaceAdapter::SetFrontbufferAllocation(bool allocated) {
238 surface_->SetFrontbufferAllocation(allocated);
[email protected]91f30db2011-12-08 03:18:38239}
240
[email protected]1b5eee12011-10-26 21:44:12241void* GLSurfaceAdapter::GetShareHandle() {
242 return surface_->GetShareHandle();
243}
244
245void* GLSurfaceAdapter::GetDisplay() {
246 return surface_->GetDisplay();
247}
248
249void* GLSurfaceAdapter::GetConfig() {
250 return surface_->GetConfig();
251}
252
253unsigned GLSurfaceAdapter::GetFormat() {
254 return surface_->GetFormat();
255}
256
[email protected]83f48ae2013-01-14 14:12:53257VSyncProvider* GLSurfaceAdapter::GetVSyncProvider() {
258 return surface_->GetVSyncProvider();
[email protected]a0c3fbb42012-11-01 15:40:36259}
260
[email protected]ab9327c2012-05-02 02:38:08261GLSurfaceAdapter::~GLSurfaceAdapter() {}
262
[email protected]24edbd02011-04-14 00:11:59263} // namespace gfx