Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / compositing_iosurface_context_mac.mm
blob6394fe370cfc7652b34b0cde892a089ef51c1a87
1 // Copyright (c) 2013 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 "content/browser/renderer_host/compositing_iosurface_context_mac.h"
7 #include <OpenGL/gl.h>
8 #include <OpenGL/OpenGL.h>
9 #include <vector>
11 #include "base/command_line.h"
12 #include "base/debug/trace_event.h"
13 #include "base/logging.h"
14 #include "content/browser/renderer_host/compositing_iosurface_shader_programs_mac.h"
15 #include "ui/gl/gl_switches.h"
16 #include "ui/gl/gpu_switching_manager.h"
18 namespace content {
20 // static
21 scoped_refptr<CompositingIOSurfaceContext>
22 CompositingIOSurfaceContext::Get(int window_number) {
23   TRACE_EVENT0("browser", "CompositingIOSurfaceContext::Get");
25   // Return the context for this window_number, if it exists.
26   WindowMap::iterator found = window_map()->find(window_number);
27   if (found != window_map()->end()) {
28     DCHECK(found->second->can_be_shared_);
29     return found->second;
30   }
32   std::vector<NSOpenGLPixelFormatAttribute> attributes;
33   attributes.push_back(NSOpenGLPFADoubleBuffer);
34   // We don't need a depth buffer - try setting its size to 0...
35   attributes.push_back(NSOpenGLPFADepthSize); attributes.push_back(0);
36   if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus())
37     attributes.push_back(NSOpenGLPFAAllowOfflineRenderers);
38   attributes.push_back(0);
40   base::scoped_nsobject<NSOpenGLPixelFormat> glPixelFormat(
41       [[NSOpenGLPixelFormat alloc] initWithAttributes:&attributes.front()]);
42   if (!glPixelFormat) {
43     LOG(ERROR) << "NSOpenGLPixelFormat initWithAttributes failed";
44     return NULL;
45   }
47   // Create all contexts in the same share group so that the textures don't
48   // need to be recreated when transitioning contexts.
49   NSOpenGLContext* share_context = nil;
50   if (!window_map()->empty())
51     share_context = window_map()->begin()->second->nsgl_context();
52   base::scoped_nsobject<NSOpenGLContext> nsgl_context(
53       [[NSOpenGLContext alloc] initWithFormat:glPixelFormat
54                                  shareContext:share_context]);
55   if (!nsgl_context) {
56     LOG(ERROR) << "NSOpenGLContext initWithFormat failed";
57     return NULL;
58   }
60   CGLContextObj cgl_context = (CGLContextObj)[nsgl_context CGLContextObj];
61   if (!cgl_context) {
62     LOG(ERROR) << "CGLContextObj failed";
63     return NULL;
64   }
66   // Draw at beam vsync.
67   bool is_vsync_disabled =
68       CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableGpuVsync);
69   GLint swapInterval = is_vsync_disabled ? 0 : 1;
70   [nsgl_context setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
72   // Prepare the shader program cache.  Precompile only the shader programs
73   // needed to draw the IO Surface.
74   CGLSetCurrentContext(cgl_context);
75   scoped_ptr<CompositingIOSurfaceShaderPrograms> shader_program_cache(
76       new CompositingIOSurfaceShaderPrograms());
77   const bool prepared = (
78       shader_program_cache->UseBlitProgram() &&
79       shader_program_cache->UseSolidWhiteProgram());
80   glUseProgram(0u);
81   CGLSetCurrentContext(0);
82   if (!prepared) {
83     LOG(ERROR) << "IOSurface failed to compile/link required shader programs.";
84     return NULL;
85   }
87   return new CompositingIOSurfaceContext(
88       window_number,
89       nsgl_context.release(),
90       cgl_context,
91       is_vsync_disabled,
92       shader_program_cache.Pass());
95 // static
96 void CompositingIOSurfaceContext::MarkExistingContextsAsNotShareable() {
97   for (WindowMap::iterator it = window_map()->begin();
98        it != window_map()->end();
99        ++it) {
100     it->second->can_be_shared_ = false;
101   }
102   window_map()->clear();
105 CompositingIOSurfaceContext::CompositingIOSurfaceContext(
106     int window_number,
107     NSOpenGLContext* nsgl_context,
108     CGLContextObj cgl_context,
109     bool is_vsync_disabled,
110     scoped_ptr<CompositingIOSurfaceShaderPrograms> shader_program_cache)
111     : window_number_(window_number),
112       nsgl_context_(nsgl_context),
113       cgl_context_(cgl_context),
114       is_vsync_disabled_(is_vsync_disabled),
115       shader_program_cache_(shader_program_cache.Pass()),
116       can_be_shared_(true) {
117   DCHECK(window_map()->find(window_number_) == window_map()->end());
118   window_map()->insert(std::make_pair(window_number_, this));
121 CompositingIOSurfaceContext::~CompositingIOSurfaceContext() {
122   CGLSetCurrentContext(cgl_context_);
123   shader_program_cache_->Reset();
124   CGLSetCurrentContext(0);
125   if (can_be_shared_) {
126     DCHECK(window_map()->find(window_number_) != window_map()->end());
127     DCHECK(window_map()->find(window_number_)->second == this);
128     window_map()->erase(window_number_);
129   } else {
130     WindowMap::const_iterator found = window_map()->find(window_number_);
131     if (found != window_map()->end())
132       DCHECK(found->second != this);
133   }
136 // static
137 CompositingIOSurfaceContext::WindowMap*
138     CompositingIOSurfaceContext::window_map() {
139   return window_map_.Pointer();
142 // static
143 base::LazyInstance<CompositingIOSurfaceContext::WindowMap>
144     CompositingIOSurfaceContext::window_map_;
146 }  // namespace content