Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_surface_mac.cc
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.h"
6
7 #include <OpenGL/CGLRenderers.h>
8
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"
20
21 namespace gfx {
22 namespace {
23
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) {}
31
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;
39   }
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; }
44
45  protected:
46   ~NoOpGLSurface() override {}
47
48  private:
49   gfx::Size size_;
50
51   DISALLOW_COPY_AND_ASSIGN(NoOpGLSurface);
52 };
53
54 // static
55 bool InitializeOneOffForSandbox() {
56   static bool initialized = false;
57   if (initialized)
58     return true;
59
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);
68   }
69   if (GetGLImplementation() == kGLImplementationAppleGL) {
70     attribs.push_back(kCGLPFARendererID);
71     attribs.push_back(static_cast<CGLPixelFormatAttribute>(
72       kCGLRendererGenericFloatID));
73   }
74   attribs.push_back(static_cast<CGLPixelFormatAttribute>(0));
75
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;
83   }
84   if (!format) {
85     LOG(ERROR) << "format == 0.";
86     return false;
87   }
88   CGLReleasePixelFormat(format);
89   DCHECK_NE(num_pixel_formats, 0);
90   initialized = true;
91   return true;
92 }
93
94 }  // namespace
95
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;
103       }
104       break;
105     default:
106       break;
107   }
108   return true;
109 }
110
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;
119     }
120     case kGLImplementationOSMesaGL: {
121       scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless());
122       if (!surface->Initialize())
123         return NULL;
124       return surface;
125     }
126     case kGLImplementationMockGL:
127       return new GLSurfaceStub;
128     default:
129       NOTREACHED();
130       return NULL;
131   }
132 }
133
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;
143
144       return surface;
145     }
146     case kGLImplementationDesktopGL:
147     case kGLImplementationAppleGL: {
148       scoped_refptr<GLSurface> surface(new NoOpGLSurface(size));
149       if (!surface->Initialize())
150         return NULL;
151
152       return surface;
153     }
154     case kGLImplementationMockGL:
155       return new GLSurfaceStub;
156     default:
157       NOTREACHED();
158       return NULL;
159   }
160 }
161
162 }  // namespace gfx