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"
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
{
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.";
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; }
46 ~NoOpGLSurface() override
{}
51 DISALLOW_COPY_AND_ASSIGN(NoOpGLSurface
);
55 bool InitializeOneOffForSandbox() {
56 static bool initialized
= false;
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
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(),
80 &num_pixel_formats
) != kCGLNoError
) {
81 LOG(ERROR
) << "Error choosing pixel format.";
85 LOG(ERROR
) << "format == 0.";
88 CGLReleasePixelFormat(format
);
89 DCHECK_NE(num_pixel_formats
, 0);
96 bool GLSurface::InitializeOneOffInternal() {
97 switch (GetGLImplementation()) {
98 case kGLImplementationDesktopGL
:
99 case kGLImplementationAppleGL
:
100 if (!InitializeOneOffForSandbox()) {
101 LOG(ERROR
) << "GLSurfaceCGL::InitializeOneOff failed.";
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.";
120 case kGLImplementationOSMesaGL
: {
121 scoped_refptr
<GLSurface
> surface(new GLSurfaceOSMesaHeadless());
122 if (!surface
->Initialize())
126 case kGLImplementationMockGL
:
127 return new GLSurfaceStub
;
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())
146 case kGLImplementationDesktopGL
:
147 case kGLImplementationAppleGL
: {
148 scoped_refptr
<GLSurface
> surface(new NoOpGLSurface(size
));
149 if (!surface
->Initialize())
154 case kGLImplementationMockGL
:
155 return new GLSurfaceStub
;