Home | History | Annotate | Download | only in gl
      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.
      4 
      5 #include "ui/gl/gl_surface_cgl.h"
      6 
      7 #include <OpenGL/CGLRenderers.h>
      8 
      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"
     16 
     17 namespace gfx {
     18 
     19 namespace {
     20 CGLPixelFormatObj g_pixel_format;
     21 }
     22 
     23 GLSurfaceCGL::GLSurfaceCGL() {}
     24 
     25 bool GLSurfaceCGL::InitializeOneOff() {
     26   static bool initialized = false;
     27   if (initialized)
     28     return true;
     29 
     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
     36     // format selection.
     37     attribs.push_back(kCGLPFAAllowOfflineRenderers);
     38   }
     39   if (GetGLImplementation() == kGLImplementationAppleGL) {
     40     attribs.push_back(kCGLPFARendererID);
     41     attribs.push_back(static_cast<CGLPixelFormatAttribute>(
     42       kCGLRendererGenericFloatID));
     43   }
     44   attribs.push_back(static_cast<CGLPixelFormatAttribute>(0));
     45 
     46   CGLPixelFormatObj format;
     47   GLint num_pixel_formats;
     48   if (CGLChoosePixelFormat(&attribs.front(),
     49                            &format,
     50                            &num_pixel_formats) != kCGLNoError) {
     51     LOG(ERROR) << "Error choosing pixel format.";
     52     return false;
     53   }
     54   if (!format) {
     55     LOG(ERROR) << "format == 0.";
     56     return false;
     57   }
     58   CGLReleasePixelFormat(format);
     59   DCHECK_NE(num_pixel_formats, 0);
     60   initialized = true;
     61   return true;
     62 }
     63 
     64 void* GLSurfaceCGL::GetPixelFormat() {
     65   return g_pixel_format;
     66 }
     67 
     68 GLSurfaceCGL::~GLSurfaceCGL() {}
     69 
     70 NoOpGLSurfaceCGL::NoOpGLSurfaceCGL(const gfx::Size& size)
     71   : size_(size) {
     72 }
     73 
     74 bool NoOpGLSurfaceCGL::Initialize() {
     75   return true;
     76 }
     77 
     78 void NoOpGLSurfaceCGL::Destroy() {
     79 }
     80 
     81 bool NoOpGLSurfaceCGL::IsOffscreen() {
     82   return true;
     83 }
     84 
     85 bool NoOpGLSurfaceCGL::SwapBuffers() {
     86   NOTREACHED() << "Cannot call SwapBuffers on a NoOpGLSurfaceCGL.";
     87   return false;
     88 }
     89 
     90 gfx::Size NoOpGLSurfaceCGL::GetSize() {
     91   return size_;
     92 }
     93 
     94 void* NoOpGLSurfaceCGL::GetHandle() {
     95   return NULL;
     96 }
     97 
     98 void* NoOpGLSurfaceCGL::GetDisplay() {
     99   return NULL;
    100 }
    101 
    102 NoOpGLSurfaceCGL::~NoOpGLSurfaceCGL() {
    103   Destroy();
    104 }
    105 
    106 }  // namespace gfx
    107