gl_context_cgl.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include "ui/gl/gl_context_cgl.h"
  5. #include <OpenGL/CGLRenderers.h>
  6. #include <OpenGL/CGLTypes.h>
  7. #include <memory>
  8. #include <sstream>
  9. #include <vector>
  10. #include "base/bind.h"
  11. #include "base/location.h"
  12. #include "base/logging.h"
  13. #include "base/task/single_thread_task_runner.h"
  14. #include "base/threading/thread_task_runner_handle.h"
  15. #include "base/trace_event/trace_event.h"
  16. #include "ui/gl/dual_gpu_state_mac.h"
  17. #include "ui/gl/gl_bindings.h"
  18. #include "ui/gl/gl_gl_api_implementation.h"
  19. #include "ui/gl/gl_implementation.h"
  20. #include "ui/gl/gl_surface.h"
  21. #include "ui/gl/gpu_switching_manager.h"
  22. #include "ui/gl/scoped_cgl.h"
  23. #include "ui/gl/yuv_to_rgb_converter.h"
  24. namespace gl {
  25. namespace {
  26. bool g_support_renderer_switching;
  27. } // namespace
  28. static CGLPixelFormatObj GetPixelFormat() {
  29. static CGLPixelFormatObj format;
  30. if (format)
  31. return format;
  32. std::vector<CGLPixelFormatAttribute> attribs;
  33. // If the system supports dual gpus then allow offline renderers for every
  34. // context, so that they can all be in the same share group.
  35. if (GLContext::SwitchableGPUsSupported()) {
  36. attribs.push_back(kCGLPFAAllowOfflineRenderers);
  37. g_support_renderer_switching = true;
  38. }
  39. if (GetGLImplementation() == kGLImplementationDesktopGLCoreProfile) {
  40. attribs.push_back(kCGLPFAOpenGLProfile);
  41. attribs.push_back((CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core);
  42. }
  43. attribs.push_back((CGLPixelFormatAttribute) 0);
  44. GLint num_virtual_screens;
  45. if (CGLChoosePixelFormat(&attribs.front(),
  46. &format,
  47. &num_virtual_screens) != kCGLNoError) {
  48. LOG(ERROR) << "Error choosing pixel format.";
  49. return nullptr;
  50. }
  51. if (!format) {
  52. LOG(ERROR) << "format == 0.";
  53. return nullptr;
  54. }
  55. DCHECK_NE(num_virtual_screens, 0);
  56. return format;
  57. }
  58. GLContextCGL::GLContextCGL(GLShareGroup* share_group)
  59. : GLContextReal(share_group) {}
  60. bool GLContextCGL::Initialize(GLSurface* compatible_surface,
  61. const GLContextAttribs& attribs) {
  62. DCHECK(compatible_surface);
  63. DCHECK(share_group());
  64. // webgl_compatibility_context and disabling bind_generates_resource are not
  65. // supported.
  66. DCHECK(!attribs.webgl_compatibility_context &&
  67. attribs.bind_generates_resource);
  68. GpuPreference gpu_preference =
  69. GLSurface::AdjustGpuPreference(attribs.gpu_preference);
  70. CGLPixelFormatObj format = GetPixelFormat();
  71. if (!format)
  72. return false;
  73. // If using the discrete gpu, create a pixel format requiring it before we
  74. // create the context. If switchable GPUs are unsupported, we should bias
  75. // toward the discrete gpu.
  76. if (!GLContext::SwitchableGPUsSupported() ||
  77. gpu_preference == GpuPreference::kHighPerformance) {
  78. DualGPUStateMac::GetInstance()->RegisterHighPerformanceContext(this);
  79. is_high_performance_context_ = true;
  80. // The renderer might be switched after this, so ignore the saved ID.
  81. share_group()->SetRendererID(-1);
  82. }
  83. CGLError res = CGLCreateContext(
  84. format, static_cast<CGLContextObj>(share_group()->GetHandle()),
  85. reinterpret_cast<CGLContextObj*>(&context_));
  86. if (res != kCGLNoError) {
  87. LOG(ERROR) << "Error creating context.";
  88. Destroy();
  89. return false;
  90. }
  91. gpu_preference_ = gpu_preference;
  92. // Contexts that prefer low power gpu are known to use only the subset of GL
  93. // that can be safely migrated between the iGPU and the dGPU. Mark those
  94. // contexts as safe to forcibly transition between the GPUs by default.
  95. // http://crbug.com/180876, http://crbug.com/227228
  96. safe_to_force_gpu_switch_ = gpu_preference == GpuPreference::kLowPower;
  97. return true;
  98. }
  99. void GLContextCGL::Destroy() {
  100. if (!yuv_to_rgb_converters_.empty() || HasBackpressureFences()) {
  101. // If this context is not current, bind this context's API so that the YUV
  102. // converter and GLFences can safely destruct
  103. GLContext* current_context = GetRealCurrent();
  104. if (current_context != this) {
  105. SetCurrentGL(GetCurrentGL());
  106. }
  107. ScopedCGLSetCurrentContext scoped_set_current(
  108. static_cast<CGLContextObj>(context_));
  109. yuv_to_rgb_converters_.clear();
  110. DestroyBackpressureFences();
  111. // Rebind the current context's API if needed.
  112. if (current_context && current_context != this) {
  113. SetCurrentGL(current_context->GetCurrentGL());
  114. }
  115. }
  116. if (is_high_performance_context_) {
  117. DualGPUStateMac::GetInstance()->RemoveHighPerformanceContext(this);
  118. }
  119. if (context_) {
  120. CGLDestroyContext(static_cast<CGLContextObj>(context_));
  121. context_ = nullptr;
  122. }
  123. }
  124. bool GLContextCGL::ForceGpuSwitchIfNeeded() {
  125. DCHECK(context_);
  126. // The call to CGLSetVirtualScreen can hang on some AMD drivers
  127. // http://crbug.com/227228
  128. if (safe_to_force_gpu_switch_) {
  129. int renderer_id = share_group()->GetRendererID();
  130. int screen;
  131. CGLGetVirtualScreen(static_cast<CGLContextObj>(context_), &screen);
  132. if (g_support_renderer_switching && !is_high_performance_context_ &&
  133. renderer_id != -1 &&
  134. (screen != screen_ || renderer_id != renderer_id_)) {
  135. // Attempt to find a virtual screen that's using the requested renderer,
  136. // and switch the context to use that screen. Don't attempt to switch if
  137. // the context requires the discrete GPU.
  138. CGLPixelFormatObj format = GetPixelFormat();
  139. int virtual_screen_count;
  140. if (CGLDescribePixelFormat(format, 0, kCGLPFAVirtualScreenCount,
  141. &virtual_screen_count) != kCGLNoError)
  142. return false;
  143. for (int i = 0; i < virtual_screen_count; ++i) {
  144. int screen_renderer_id;
  145. if (CGLDescribePixelFormat(format, i, kCGLPFARendererID,
  146. &screen_renderer_id) != kCGLNoError)
  147. return false;
  148. screen_renderer_id &= kCGLRendererIDMatchingMask;
  149. if (screen_renderer_id == renderer_id) {
  150. CGLSetVirtualScreen(static_cast<CGLContextObj>(context_), i);
  151. screen_ = i;
  152. break;
  153. }
  154. }
  155. renderer_id_ = renderer_id;
  156. has_switched_gpus_ = true;
  157. }
  158. }
  159. return true;
  160. }
  161. YUVToRGBConverter* GLContextCGL::GetYUVToRGBConverter(
  162. const gfx::ColorSpace& color_space) {
  163. std::unique_ptr<YUVToRGBConverter>& yuv_to_rgb_converter =
  164. yuv_to_rgb_converters_[color_space];
  165. if (!yuv_to_rgb_converter)
  166. yuv_to_rgb_converter =
  167. std::make_unique<YUVToRGBConverter>(*GetVersionInfo(), color_space);
  168. return yuv_to_rgb_converter.get();
  169. }
  170. bool GLContextCGL::MakeCurrentImpl(GLSurface* surface) {
  171. DCHECK(context_);
  172. if (!ForceGpuSwitchIfNeeded())
  173. return false;
  174. if (IsCurrent(surface))
  175. return true;
  176. // It's likely we're going to switch OpenGL contexts at this point.
  177. // Before doing so, if there is a current context, flush it. There
  178. // are many implicit assumptions of flush ordering between contexts
  179. // at higher levels, and if a flush isn't performed, OpenGL commands
  180. // may be issued in unexpected orders, causing flickering and other
  181. // artifacts.
  182. if (CGLGetCurrentContext() != nullptr) {
  183. glFlush();
  184. }
  185. ScopedReleaseCurrent release_current;
  186. TRACE_EVENT0("gpu", "GLContextCGL::MakeCurrent");
  187. if (CGLSetCurrentContext(
  188. static_cast<CGLContextObj>(context_)) != kCGLNoError) {
  189. LOG(ERROR) << "Unable to make gl context current.";
  190. return false;
  191. }
  192. // Set this as soon as the context is current, since we might call into GL.
  193. BindGLApi();
  194. SetCurrent(surface);
  195. InitializeDynamicBindings();
  196. if (!surface->OnMakeCurrent(this)) {
  197. LOG(ERROR) << "Unable to make gl context current.";
  198. return false;
  199. }
  200. release_current.Cancel();
  201. return true;
  202. }
  203. void GLContextCGL::SetVisibility(bool visibility) {
  204. if (!is_high_performance_context_ || !g_support_renderer_switching)
  205. return;
  206. if (visibility)
  207. DualGPUStateMac::GetInstance()->RegisterHighPerformanceContext(this);
  208. else
  209. DualGPUStateMac::GetInstance()->RemoveHighPerformanceContext(this);
  210. }
  211. void GLContextCGL::ReleaseCurrent(GLSurface* surface) {
  212. if (!IsCurrent(surface))
  213. return;
  214. // Before releasing the current context, flush it. This ensures that
  215. // all commands issued by higher levels will be seen by the OpenGL
  216. // implementation, which is assumed throughout the code. See comment
  217. // in MakeCurrent, above.
  218. glFlush();
  219. SetCurrent(nullptr);
  220. CGLSetCurrentContext(nullptr);
  221. }
  222. bool GLContextCGL::IsCurrent(GLSurface* surface) {
  223. bool native_context_is_current = CGLGetCurrentContext() == context_;
  224. // If our context is current then our notion of which GLContext is
  225. // current must be correct. On the other hand, third-party code
  226. // using OpenGL might change the current context.
  227. DCHECK(!native_context_is_current || (GetRealCurrent() == this));
  228. if (!native_context_is_current)
  229. return false;
  230. return true;
  231. }
  232. void* GLContextCGL::GetHandle() {
  233. return context_;
  234. }
  235. void GLContextCGL::SetSafeToForceGpuSwitch() {
  236. safe_to_force_gpu_switch_ = true;
  237. }
  238. GLContextCGL::~GLContextCGL() {
  239. Destroy();
  240. }
  241. GpuPreference GLContextCGL::GetGpuPreference() {
  242. return gpu_preference_;
  243. }
  244. } // namespace gl