| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/compositing_iosurface_context_mac.h" | 5 #include "content/browser/renderer_host/compositing_iosurface_context_mac.h" |
| 6 | 6 |
| 7 #include <OpenGL/gl.h> | 7 #include <OpenGL/gl.h> |
| 8 #include <OpenGL/OpenGL.h> | 8 #include <OpenGL/OpenGL.h> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "content/browser/renderer_host/compositing_iosurface_shader_programs_ma
c.h" |
| 14 #include "content/browser/gpu/gpu_data_manager_impl.h" | 15 #include "content/browser/gpu/gpu_data_manager_impl.h" |
| 15 #include "ui/base/ui_base_switches.h" | 16 #include "ui/base/ui_base_switches.h" |
| 16 #include "ui/gl/gl_switches.h" | 17 #include "ui/gl/gl_switches.h" |
| 17 #include "ui/gl/gpu_switching_manager.h" | 18 #include "ui/gl/gpu_switching_manager.h" |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 | 21 |
| 21 // static | 22 // static |
| 22 scoped_refptr<CompositingIOSurfaceContext> | 23 scoped_refptr<CompositingIOSurfaceContext> |
| 23 CompositingIOSurfaceContext::Get(int window_number) { | 24 CompositingIOSurfaceContext::Get(int window_number) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 pixel_format, share_context, cgl_context_strong.InitializeInto()); | 63 pixel_format, share_context, cgl_context_strong.InitializeInto()); |
| 63 if (error != kCGLNoError) { | 64 if (error != kCGLNoError) { |
| 64 LOG(ERROR) << "Failed to create context object."; | 65 LOG(ERROR) << "Failed to create context object."; |
| 65 return NULL; | 66 return NULL; |
| 66 } | 67 } |
| 67 cgl_context = cgl_context_strong; | 68 cgl_context = cgl_context_strong; |
| 68 | 69 |
| 69 // Note that VSync is ignored because CoreAnimation will automatically | 70 // Note that VSync is ignored because CoreAnimation will automatically |
| 70 // rate limit draws. | 71 // rate limit draws. |
| 71 | 72 |
| 73 // Prepare the shader program cache. Precompile the shader programs |
| 74 // needed to draw the IO Surface for non-offscreen contexts. |
| 75 bool prepared = false; |
| 76 scoped_ptr<CompositingIOSurfaceShaderPrograms> shader_program_cache; |
| 77 { |
| 78 gfx::ScopedCGLSetCurrentContext scoped_set_current_context(cgl_context); |
| 79 shader_program_cache.reset(new CompositingIOSurfaceShaderPrograms()); |
| 80 if (window_number == kOffscreenContextWindowNumber) { |
| 81 prepared = true; |
| 82 } else { |
| 83 prepared = ( |
| 84 shader_program_cache->UseBlitProgram() && |
| 85 shader_program_cache->UseSolidWhiteProgram()); |
| 86 } |
| 87 glUseProgram(0u); |
| 88 } |
| 89 if (!prepared) { |
| 90 LOG(ERROR) << "IOSurface failed to compile/link required shader programs."; |
| 91 return NULL; |
| 92 } |
| 93 |
| 72 return new CompositingIOSurfaceContext( | 94 return new CompositingIOSurfaceContext( |
| 73 window_number, | 95 window_number, |
| 74 cgl_context_strong, | 96 cgl_context_strong, |
| 75 cgl_context); | 97 cgl_context, |
| 98 shader_program_cache.Pass()); |
| 76 } | 99 } |
| 77 | 100 |
| 78 void CompositingIOSurfaceContext::PoisonContextAndSharegroup() { | 101 void CompositingIOSurfaceContext::PoisonContextAndSharegroup() { |
| 79 if (poisoned_) | 102 if (poisoned_) |
| 80 return; | 103 return; |
| 81 | 104 |
| 82 for (WindowMap::iterator it = window_map()->begin(); | 105 for (WindowMap::iterator it = window_map()->begin(); |
| 83 it != window_map()->end(); | 106 it != window_map()->end(); |
| 84 ++it) { | 107 ++it) { |
| 85 it->second->poisoned_ = true; | 108 it->second->poisoned_ = true; |
| 86 } | 109 } |
| 87 window_map()->clear(); | 110 window_map()->clear(); |
| 88 } | 111 } |
| 89 | 112 |
| 90 CompositingIOSurfaceContext::CompositingIOSurfaceContext( | 113 CompositingIOSurfaceContext::CompositingIOSurfaceContext( |
| 91 int window_number, | 114 int window_number, |
| 92 base::ScopedTypeRef<CGLContextObj> cgl_context_strong, | 115 base::ScopedTypeRef<CGLContextObj> cgl_context_strong, |
| 93 CGLContextObj cgl_context) | 116 CGLContextObj cgl_context, |
| 117 scoped_ptr<CompositingIOSurfaceShaderPrograms> shader_program_cache) |
| 94 : window_number_(window_number), | 118 : window_number_(window_number), |
| 95 cgl_context_strong_(cgl_context_strong), | 119 cgl_context_strong_(cgl_context_strong), |
| 96 cgl_context_(cgl_context), | 120 cgl_context_(cgl_context), |
| 121 shader_program_cache_(shader_program_cache.Pass()), |
| 97 poisoned_(false) { | 122 poisoned_(false) { |
| 98 DCHECK(window_map()->find(window_number_) == window_map()->end()); | 123 DCHECK(window_map()->find(window_number_) == window_map()->end()); |
| 99 window_map()->insert(std::make_pair(window_number_, this)); | 124 window_map()->insert(std::make_pair(window_number_, this)); |
| 100 | 125 |
| 101 GpuDataManager::GetInstance()->AddObserver(this); | 126 GpuDataManager::GetInstance()->AddObserver(this); |
| 102 } | 127 } |
| 103 | 128 |
| 104 CompositingIOSurfaceContext::~CompositingIOSurfaceContext() { | 129 CompositingIOSurfaceContext::~CompositingIOSurfaceContext() { |
| 105 GpuDataManager::GetInstance()->RemoveObserver(this); | 130 GpuDataManager::GetInstance()->RemoveObserver(this); |
| 106 | 131 |
| 132 { |
| 133 gfx::ScopedCGLSetCurrentContext scoped_set_current_context(cgl_context_); |
| 134 shader_program_cache_->Reset(); |
| 135 } |
| 107 if (!poisoned_) { | 136 if (!poisoned_) { |
| 108 DCHECK(window_map()->find(window_number_) != window_map()->end()); | 137 DCHECK(window_map()->find(window_number_) != window_map()->end()); |
| 109 DCHECK(window_map()->find(window_number_)->second == this); | 138 DCHECK(window_map()->find(window_number_)->second == this); |
| 110 window_map()->erase(window_number_); | 139 window_map()->erase(window_number_); |
| 111 } else { | 140 } else { |
| 112 WindowMap::const_iterator found = window_map()->find(window_number_); | 141 WindowMap::const_iterator found = window_map()->find(window_number_); |
| 113 if (found != window_map()->end()) | 142 if (found != window_map()->end()) |
| 114 DCHECK(found->second != this); | 143 DCHECK(found->second != this); |
| 115 } | 144 } |
| 116 } | 145 } |
| 117 | 146 |
| 118 void CompositingIOSurfaceContext::OnGpuSwitching() { | 147 void CompositingIOSurfaceContext::OnGpuSwitching() { |
| 119 // Recreate all browser-side GL contexts whenever the GPU switches. If this | 148 // Recreate all browser-side GL contexts whenever the GPU switches. If this |
| 120 // is not done, performance will suffer. | 149 // is not done, performance will suffer. |
| 121 // http://crbug.com/361493 | 150 // http://crbug.com/361493 |
| 122 PoisonContextAndSharegroup(); | 151 PoisonContextAndSharegroup(); |
| 123 } | 152 } |
| 124 | 153 |
| 125 // static | 154 // static |
| 126 CompositingIOSurfaceContext::WindowMap* | 155 CompositingIOSurfaceContext::WindowMap* |
| 127 CompositingIOSurfaceContext::window_map() { | 156 CompositingIOSurfaceContext::window_map() { |
| 128 return window_map_.Pointer(); | 157 return window_map_.Pointer(); |
| 129 } | 158 } |
| 130 | 159 |
| 131 // static | 160 // static |
| 132 base::LazyInstance<CompositingIOSurfaceContext::WindowMap> | 161 base::LazyInstance<CompositingIOSurfaceContext::WindowMap> |
| 133 CompositingIOSurfaceContext::window_map_; | 162 CompositingIOSurfaceContext::window_map_; |
| 134 | 163 |
| 135 } // namespace content | 164 } // namespace content |
| OLD | NEW |