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_cgl.h"
7 #include <OpenGL/CGLRenderers.h>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/mac/mac_util.h"
12 #include "ui/gl/gl_bindings.h"
13 #include "ui/gl/gl_context.h"
14 #include "ui/gl/gl_implementation.h"
15 #include "ui/gl/gpu_switching_manager.h"
20 CGLPixelFormatObj g_pixel_format
;
23 GLSurfaceCGL::GLSurfaceCGL() {}
25 bool GLSurfaceCGL::InitializeOneOff() {
26 static bool initialized
= false;
30 // This is called from the sandbox warmup code on Mac OS X.
31 // GPU-related stuff is very slow without this, probably because
32 // the sandbox prevents loading graphics drivers or some such.
33 std::vector
<CGLPixelFormatAttribute
> attribs
;
34 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) {
35 // Avoid switching to the discrete GPU just for this pixel
37 attribs
.push_back(kCGLPFAAllowOfflineRenderers
);
39 if (GetGLImplementation() == kGLImplementationAppleGL
) {
40 attribs
.push_back(kCGLPFARendererID
);
41 attribs
.push_back(static_cast<CGLPixelFormatAttribute
>(
42 kCGLRendererGenericFloatID
));
44 attribs
.push_back(static_cast<CGLPixelFormatAttribute
>(0));
46 CGLPixelFormatObj format
;
47 GLint num_pixel_formats
;
48 if (CGLChoosePixelFormat(&attribs
.front(),
50 &num_pixel_formats
) != kCGLNoError
) {
51 LOG(ERROR
) << "Error choosing pixel format.";
55 LOG(ERROR
) << "format == 0.";
58 CGLReleasePixelFormat(format
);
59 DCHECK_NE(num_pixel_formats
, 0);
64 void* GLSurfaceCGL::GetPixelFormat() {
65 return g_pixel_format
;
68 GLSurfaceCGL::~GLSurfaceCGL() {}
70 NoOpGLSurfaceCGL::NoOpGLSurfaceCGL(const gfx::Size
& size
)
74 bool NoOpGLSurfaceCGL::Initialize() {
78 void NoOpGLSurfaceCGL::Destroy() {
81 bool NoOpGLSurfaceCGL::IsOffscreen() {
85 bool NoOpGLSurfaceCGL::SwapBuffers() {
86 NOTREACHED() << "Cannot call SwapBuffers on a NoOpGLSurfaceCGL.";
90 gfx::Size
NoOpGLSurfaceCGL::GetSize() {
94 void* NoOpGLSurfaceCGL::GetHandle() {
98 void* NoOpGLSurfaceCGL::GetDisplay() {
102 NoOpGLSurfaceCGL::~NoOpGLSurfaceCGL() {