[Android] Add a reset_usb utility script (RELAND).
[chromium-blink-merge.git] / ui / gl / gl_surface_mac.cc
blob0aea62f385aebd7a18adb9b37498cd0a04d0814f
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/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/trace_event/trace_event.h"
13 #include "ui/gl/gl_bindings.h"
14 #include "ui/gl/gl_context.h"
15 #include "ui/gl/gl_implementation.h"
16 #include "ui/gl/gl_surface_osmesa.h"
17 #include "ui/gl/gl_surface_stub.h"
18 #include "ui/gl/gpu_switching_manager.h"
20 namespace gfx {
21 namespace {
23 // A "no-op" surface. It is not required that a CGLContextObj have an
24 // associated drawable (pbuffer or fullscreen context) in order to be
25 // made current. Everywhere this surface type is used, we allocate an
26 // FBO at the user level as the drawable of the associated context.
27 class GL_EXPORT NoOpGLSurface : public GLSurface {
28 public:
29 explicit NoOpGLSurface(const gfx::Size& size) : size_(size) {}
31 // Implement GLSurface.
32 bool Initialize() override { return true; }
33 void Destroy() override {}
34 bool IsOffscreen() override { return true; }
35 gfx::SwapResult SwapBuffers() override {
36 NOTREACHED() << "Cannot call SwapBuffers on a NoOpGLSurface.";
37 return gfx::SwapResult::SWAP_FAILED;
39 gfx::Size GetSize() override { return size_; }
40 void* GetHandle() override { return NULL; }
41 void* GetDisplay() override { return NULL; }
42 bool IsSurfaceless() const override { return true; }
44 protected:
45 ~NoOpGLSurface() override {}
47 private:
48 gfx::Size size_;
50 DISALLOW_COPY_AND_ASSIGN(NoOpGLSurface);
53 // static
54 bool InitializeOneOffForSandbox() {
55 static bool initialized = false;
56 if (initialized)
57 return true;
59 // This is called from the sandbox warmup code on Mac OS X.
60 // GPU-related stuff is very slow without this, probably because
61 // the sandbox prevents loading graphics drivers or some such.
62 std::vector<CGLPixelFormatAttribute> attribs;
63 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) {
64 // Avoid switching to the discrete GPU just for this pixel
65 // format selection.
66 attribs.push_back(kCGLPFAAllowOfflineRenderers);
68 if (GetGLImplementation() == kGLImplementationAppleGL) {
69 attribs.push_back(kCGLPFARendererID);
70 attribs.push_back(static_cast<CGLPixelFormatAttribute>(
71 kCGLRendererGenericFloatID));
73 attribs.push_back(static_cast<CGLPixelFormatAttribute>(0));
75 CGLPixelFormatObj format;
76 GLint num_pixel_formats;
77 if (CGLChoosePixelFormat(&attribs.front(),
78 &format,
79 &num_pixel_formats) != kCGLNoError) {
80 LOG(ERROR) << "Error choosing pixel format.";
81 return false;
83 if (!format) {
84 LOG(ERROR) << "format == 0.";
85 return false;
87 CGLReleasePixelFormat(format);
88 DCHECK_NE(num_pixel_formats, 0);
89 initialized = true;
90 return true;
93 } // namespace
95 bool GLSurface::InitializeOneOffInternal() {
96 switch (GetGLImplementation()) {
97 case kGLImplementationDesktopGL:
98 case kGLImplementationDesktopGLCoreProfile:
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 kGLImplementationDesktopGLCoreProfile:
117 case kGLImplementationAppleGL: {
118 NOTIMPLEMENTED() << "No onscreen support on Mac.";
119 return NULL;
121 case kGLImplementationOSMesaGL: {
122 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless());
123 if (!surface->Initialize())
124 return NULL;
125 return surface;
127 case kGLImplementationMockGL:
128 return new GLSurfaceStub;
129 default:
130 NOTREACHED();
131 return NULL;
135 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface(
136 const gfx::Size& size) {
137 TRACE_EVENT0("gpu", "GLSurface::CreateOffscreenGLSurface");
138 switch (GetGLImplementation()) {
139 case kGLImplementationOSMesaGL: {
140 scoped_refptr<GLSurface> surface(
141 new GLSurfaceOSMesa(OSMesaSurfaceFormatRGBA, size));
142 if (!surface->Initialize())
143 return NULL;
145 return surface;
147 case kGLImplementationDesktopGL:
148 case kGLImplementationDesktopGLCoreProfile:
149 case kGLImplementationAppleGL: {
150 scoped_refptr<GLSurface> surface(new NoOpGLSurface(size));
151 if (!surface->Initialize())
152 return NULL;
154 return surface;
156 case kGLImplementationMockGL:
157 return new GLSurfaceStub;
158 default:
159 NOTREACHED();
160 return NULL;
164 } // namespace gfx