Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(504)

Side by Side Diff: ui/gl/gl_context_cgl.cc

Issue 2627323007: Migrate WebGL contexts on backgrounded tabs to the integrated GPU.
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gl/gl_context_cgl.h ('k') | ui/gl/gpu_preference.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "ui/gl/gl_context_cgl.h" 5 #include "ui/gl/gl_context_cgl.h"
6 6
7 #include <OpenGL/CGLRenderers.h> 7 #include <OpenGL/CGLRenderers.h>
8 #include <OpenGL/CGLTypes.h> 8 #include <OpenGL/CGLTypes.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 static_cast<GLContextCGL*>(share_group()->GetContext()) : nullptr; 107 static_cast<GLContextCGL*>(share_group()->GetContext()) : nullptr;
108 108
109 CGLPixelFormatObj format = GetPixelFormat(); 109 CGLPixelFormatObj format = GetPixelFormat();
110 if (!format) 110 if (!format)
111 return false; 111 return false;
112 112
113 // If using the discrete gpu, create a pixel format requiring it before we 113 // If using the discrete gpu, create a pixel format requiring it before we
114 // create the context. 114 // create the context.
115 if (!ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus() || 115 if (!ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus() ||
116 gpu_preference == PreferDiscreteGpu) { 116 gpu_preference == PreferDiscreteGpu) {
117 std::vector<CGLPixelFormatAttribute> discrete_attribs; 117 if (!AllocateDiscretePixelFormat())
118 discrete_attribs.push_back((CGLPixelFormatAttribute) 0);
119 GLint num_pixel_formats;
120 if (CGLChoosePixelFormat(&discrete_attribs.front(),
121 &discrete_pixelformat_,
122 &num_pixel_formats) != kCGLNoError) {
123 LOG(ERROR) << "Error choosing pixel format.";
124 return false; 118 return false;
125 }
126 // The renderer might be switched after this, so ignore the saved ID.
127 share_group()->SetRendererID(-1);
128 } 119 }
129 120
130 CGLError res = CGLCreateContext( 121 CGLError res = CGLCreateContext(
131 format, 122 format,
132 share_context ? 123 share_context ?
133 static_cast<CGLContextObj>(share_context->GetHandle()) : nullptr, 124 static_cast<CGLContextObj>(share_context->GetHandle()) : nullptr,
134 reinterpret_cast<CGLContextObj*>(&context_)); 125 reinterpret_cast<CGLContextObj*>(&context_));
135 if (res != kCGLNoError) { 126 if (res != kCGLNoError) {
136 LOG(ERROR) << "Error creating context."; 127 LOG(ERROR) << "Error creating context.";
137 Destroy(); 128 Destroy();
138 return false; 129 return false;
139 } 130 }
140 131
141 gpu_preference_ = gpu_preference; 132 gpu_preference_ = gpu_preference;
142 // Contexts that prefer integrated gpu are known to use only the subset of GL 133 // Contexts that prefer integrated gpu are known to use only the subset of GL
143 // that can be safely migrated between the iGPU and the dGPU. Mark those 134 // that can be safely migrated between the iGPU and the dGPU. Mark those
144 // contexts as safe to forcibly transition between the GPUs by default. 135 // contexts as safe to forcibly transition between the GPUs by default.
145 // http://crbug.com/180876, http://crbug.com/227228 136 // http://crbug.com/180876, http://crbug.com/227228
146 safe_to_force_gpu_switch_ = gpu_preference == PreferIntegratedGpu; 137 safe_to_force_gpu_switch_ = gpu_preference == PreferIntegratedGpu;
147 return true; 138 return true;
148 } 139 }
149 140
141 bool GLContextCGL::AllocateDiscretePixelFormat() {
142 std::vector<CGLPixelFormatAttribute> discrete_attribs;
143 discrete_attribs.push_back((CGLPixelFormatAttribute) 0);
144 GLint num_pixel_formats;
145 if (CGLChoosePixelFormat(&discrete_attribs.front(),
146 &discrete_pixelformat_,
147 &num_pixel_formats) != kCGLNoError) {
148 LOG(ERROR) << "Error choosing discrete pixel format.";
149 return false;
150 }
151 // The renderer might be switched after this, so ignore the saved ID.
152 share_group()->SetRendererID(-1);
153 return true;
154 }
155
156 void GLContextCGL::ReleaseDiscretePixelFormat() {
157 CGLReleasePixelFormat(discrete_pixelformat_);
158 discrete_pixelformat_ = nullptr;
159 }
160
150 void GLContextCGL::Destroy() { 161 void GLContextCGL::Destroy() {
151 if (yuv_to_rgb_converter_) { 162 if (yuv_to_rgb_converter_) {
152 ScopedCGLSetCurrentContext(static_cast<CGLContextObj>(context_)); 163 ScopedCGLSetCurrentContext(static_cast<CGLContextObj>(context_));
153 yuv_to_rgb_converter_.reset(); 164 yuv_to_rgb_converter_.reset();
154 } 165 }
155 if (discrete_pixelformat_) { 166 if (discrete_pixelformat_) {
156 if (base::MessageLoop::current() != nullptr) { 167 if (base::MessageLoop::current() != nullptr) {
157 // Delay releasing the pixel format for 10 seconds to reduce the number of 168 // Delay releasing the pixel format for 10 seconds to reduce the number of
158 // unnecessary GPU switches. 169 // unnecessary GPU switches.
159 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 170 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 297
287 298
288 GLContextCGL::~GLContextCGL() { 299 GLContextCGL::~GLContextCGL() {
289 Destroy(); 300 Destroy();
290 } 301 }
291 302
292 GpuPreference GLContextCGL::GetGpuPreference() { 303 GpuPreference GLContextCGL::GetGpuPreference() {
293 return gpu_preference_; 304 return gpu_preference_;
294 } 305 }
295 306
307 void GLContextCGL::SetPerformancePreference(GpuPerformancePreference pref) {
308 if (pref == PreferLowPower) {
309 if (discrete_pixelformat_)
310 ReleaseDiscretePixelFormat();
311 } else {
312 if (gpu_preference_ == PreferDiscreteGpu && !discrete_pixelformat_)
313 AllocateDiscretePixelFormat();
jbauman 2017/01/19 01:13:14 I'm not sure this would actually switch the contex
Ken Russell (switch to Gerrit) 2017/01/20 04:34:52 Thanks for tracking down that change. I'd remember
314 }
315 }
316
296 } // namespace gl 317 } // namespace gl
OLDNEW
« no previous file with comments | « ui/gl/gl_context_cgl.h ('k') | ui/gl/gpu_preference.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698