Roll src/third_party/WebKit 6f84130:7353389 (svn 184386:184391)
[chromium-blink-merge.git] / ui / gl / gl_surface_mac.cc
blob7f1ab681707b99cd160d358baf0fd5fd7e8e099e
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.
5 #include "ui/gl/gl_surface.h"
7 #include <OpenGL/CGLRenderers.h>
9 #include "base/basictypes.h"
10 #include "base/debug/trace_event.h"
11 #include "base/logging.h"
12 #include "base/mac/mac_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "ui/gl/gl_bindings.h"
15 #include "ui/gl/gl_context.h"
16 #include "ui/gl/gl_implementation.h"
17 #include "ui/gl/gl_surface_osmesa.h"
18 #include "ui/gl/gl_surface_stub.h"
19 #include "ui/gl/gpu_switching_manager.h"
21 namespace gfx {
22 namespace {
24 // A "no-op" surface. It is not required that a CGLContextObj have an
25 // associated drawable (pbuffer or fullscreen context) in order to be
26 // made current. Everywhere this surface type is used, we allocate an
27 // FBO at the user level as the drawable of the associated context.
28 class GL_EXPORT NoOpGLSurface : public GLSurface {
29 public:
30 explicit NoOpGLSurface(const gfx::Size& size) : size_(size) {}
32 // Implement GLSurface.
33 bool Initialize() override { return true; }
34 void Destroy() override {}
35 bool IsOffscreen() override { return true; }
36 bool SwapBuffers() override {
37 NOTREACHED() << "Cannot call SwapBuffers on a NoOpGLSurface.";
38 return false;
40 gfx::Size GetSize() override { return size_; }
41 void* GetHandle() override { return NULL; }
42 void* GetDisplay() override { return NULL; }
43 bool IsSurfaceless() const override { return true; }
45 protected:
46 ~NoOpGLSurface() override {}
48 private:
49 gfx::Size size_;
51 DISALLOW_COPY_AND_ASSIGN(NoOpGLSurface);
54 // static
55 bool InitializeOneOffForSandbox() {
56 static bool initialized = false;
57 if (initialized)
58 return true;
60 // This is called from the sandbox warmup code on Mac OS X.
61 // GPU-related stuff is very slow without this, probably because
62 // the sandbox prevents loading graphics drivers or some such.
63 std::vector<CGLPixelFormatAttribute> attribs;
64 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) {
65 // Avoid switching to the discrete GPU just for this pixel
66 // format selection.
67 attribs.push_back(kCGLPFAAllowOfflineRenderers);
69 if (GetGLImplementation() == kGLImplementationAppleGL) {
70 attribs.push_back(kCGLPFARendererID);
71 attribs.push_back(static_cast<CGLPixelFormatAttribute>(
72 kCGLRendererGenericFloatID));
74 attribs.push_back(static_cast<CGLPixelFormatAttribute>(0));
76 CGLPixelFormatObj format;
77 GLint num_pixel_formats;
78 if (CGLChoosePixelFormat(&attribs.front(),
79 &format,
80 &num_pixel_formats) != kCGLNoError) {
81 LOG(ERROR) << "Error choosing pixel format.";
82 return false;
84 if (!format) {
85 LOG(ERROR) << "format == 0.";
86 return false;
88 CGLReleasePixelFormat(format);
89 DCHECK_NE(num_pixel_formats, 0);
90 initialized = true;
91 return true;
94 } // namespace
96 bool GLSurface::InitializeOneOffInternal() {
97 switch (GetGLImplementation()) {
98 case kGLImplementationDesktopGL:
99 case kGLImplementationAppleGL:
100 if (!InitializeOneOffForSandbox()) {
101 LOG(ERROR) << "GLSurfaceCGL::InitializeOneOff failed.";
102 return false;
104 break;
105 default:
106 break;
108 return true;
111 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface(
112 gfx::AcceleratedWidget window) {
113 TRACE_EVENT0("gpu", "GLSurface::CreateViewGLSurface");
114 switch (GetGLImplementation()) {
115 case kGLImplementationDesktopGL:
116 case kGLImplementationAppleGL: {
117 NOTIMPLEMENTED() << "No onscreen support on Mac.";
118 return NULL;
120 case kGLImplementationOSMesaGL: {
121 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless());
122 if (!surface->Initialize())
123 return NULL;
124 return surface;
126 case kGLImplementationMockGL:
127 return new GLSurfaceStub;
128 default:
129 NOTREACHED();
130 return NULL;
134 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface(
135 const gfx::Size& size) {
136 TRACE_EVENT0("gpu", "GLSurface::CreateOffscreenGLSurface");
137 switch (GetGLImplementation()) {
138 case kGLImplementationOSMesaGL: {
139 scoped_refptr<GLSurface> surface(
140 new GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA, size));
141 if (!surface->Initialize())
142 return NULL;
144 return surface;
146 case kGLImplementationDesktopGL:
147 case kGLImplementationAppleGL: {
148 scoped_refptr<GLSurface> surface(new NoOpGLSurface(size));
149 if (!surface->Initialize())
150 return NULL;
152 return surface;
154 case kGLImplementationMockGL:
155 return new GLSurfaceStub;
156 default:
157 NOTREACHED();
158 return NULL;
162 } // namespace gfx