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

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

Issue 712343003: Infrastructure for temportarily relinquishing GPU resources. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: changes suggested by dcheng Created 6 years 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_surface_egl.h ('k') | ui/ozone/platform/dri/dri_gpu_platform_support.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_surface_egl.h" 5 #include "ui/gl/gl_surface_egl.h"
6 6
7 #if defined(OS_ANDROID) 7 #if defined(OS_ANDROID)
8 #include <android/native_window_jni.h> 8 #include <android/native_window_jni.h>
9 #endif 9 #endif
10 10
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 using ui::GetLastEGLErrorString; 53 using ui::GetLastEGLErrorString;
54 54
55 namespace gfx { 55 namespace gfx {
56 56
57 unsigned int NativeViewGLSurfaceEGL::current_swap_generation_ = 0; 57 unsigned int NativeViewGLSurfaceEGL::current_swap_generation_ = 0;
58 58
59 namespace { 59 namespace {
60 60
61 EGLConfig g_config; 61 EGLConfig g_config;
62 EGLDisplay g_display; 62 EGLDisplay g_display;
63 EGLNativeDisplayType g_native_display; 63 EGLNativeDisplayType g_native_display_type;
64
65 // In the Cast environment, we need to destroy the EGLNativeDisplayType and
66 // EGLDisplay returned by the GPU platform when we switch to an external app
67 // which will temporarily own all screen and GPU resources.
68 // Even though Chromium is still in the background.
69 // As such, it must be reinitialized each time we come back to the foreground.
70 bool g_initialized = false;
71 int g_num_surfaces = 0;
72 bool g_terminate_pending = false;
64 73
65 const char* g_egl_extensions = NULL; 74 const char* g_egl_extensions = NULL;
66 bool g_egl_create_context_robustness_supported = false; 75 bool g_egl_create_context_robustness_supported = false;
67 bool g_egl_sync_control_supported = false; 76 bool g_egl_sync_control_supported = false;
68 bool g_egl_window_fixed_size_supported = false; 77 bool g_egl_window_fixed_size_supported = false;
69 bool g_egl_surfaceless_context_supported = false; 78 bool g_egl_surfaceless_context_supported = false;
70 79
71 class EGLSyncControlVSyncProvider 80 class EGLSyncControlVSyncProvider
72 : public gfx::SyncControlVSyncProvider { 81 : public gfx::SyncControlVSyncProvider {
73 public: 82 public:
(...skipping 23 matching lines...) Expand all
97 bool GetMscRate(int32* numerator, int32* denominator) override { 106 bool GetMscRate(int32* numerator, int32* denominator) override {
98 return false; 107 return false;
99 } 108 }
100 109
101 private: 110 private:
102 EGLSurface surface_; 111 EGLSurface surface_;
103 112
104 DISALLOW_COPY_AND_ASSIGN(EGLSyncControlVSyncProvider); 113 DISALLOW_COPY_AND_ASSIGN(EGLSyncControlVSyncProvider);
105 }; 114 };
106 115
116 void DeinitializeEgl() {
117 if (g_initialized) {
118 g_initialized = false;
119 eglTerminate(g_display);
120 }
121 }
122
107 } // namespace 123 } // namespace
108 124
109 GLSurfaceEGL::GLSurfaceEGL() {} 125 GLSurfaceEGL::GLSurfaceEGL() {
126 ++g_num_surfaces;
127 if (!g_initialized) {
128 bool result = GLSurfaceEGL::InitializeOneOff();
129 DCHECK(result);
130 DCHECK(g_initialized);
131 }
132 }
110 133
111 bool GLSurfaceEGL::InitializeOneOff() { 134 bool GLSurfaceEGL::InitializeOneOff() {
112 static bool initialized = false; 135 if (g_initialized)
113 if (initialized)
114 return true; 136 return true;
115 137
116 g_native_display = GetPlatformDefaultEGLNativeDisplay(); 138 g_native_display_type = GetPlatformDefaultEGLNativeDisplay();
117 139
118 #if defined(OS_WIN) 140 #if defined(OS_WIN)
119 g_display = GetPlatformDisplay(g_native_display); 141 g_display = GetPlatformDisplay(g_native_display_type);
120 #else 142 #else
121 g_display = eglGetDisplay(g_native_display); 143 g_display = eglGetDisplay(g_native_display_type);
122 #endif 144 #endif
123 145
124 if (!g_display) { 146 if (!g_display) {
125 LOG(ERROR) << "eglGetDisplay failed with error " << GetLastEGLErrorString(); 147 LOG(ERROR) << "eglGetDisplay failed with error " << GetLastEGLErrorString();
126 return false; 148 return false;
127 } 149 }
128 150
129 if (!eglInitialize(g_display, NULL, NULL)) { 151 if (!eglInitialize(g_display, NULL, NULL)) {
130 LOG(ERROR) << "eglInitialize failed with error " << GetLastEGLErrorString(); 152 LOG(ERROR) << "eglInitialize failed with error " << GetLastEGLErrorString();
131 return false; 153 return false;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 } 201 }
180 202
181 g_egl_extensions = eglQueryString(g_display, EGL_EXTENSIONS); 203 g_egl_extensions = eglQueryString(g_display, EGL_EXTENSIONS);
182 g_egl_create_context_robustness_supported = 204 g_egl_create_context_robustness_supported =
183 HasEGLExtension("EGL_EXT_create_context_robustness"); 205 HasEGLExtension("EGL_EXT_create_context_robustness");
184 g_egl_sync_control_supported = 206 g_egl_sync_control_supported =
185 HasEGLExtension("EGL_CHROMIUM_sync_control"); 207 HasEGLExtension("EGL_CHROMIUM_sync_control");
186 g_egl_window_fixed_size_supported = 208 g_egl_window_fixed_size_supported =
187 HasEGLExtension("EGL_ANGLE_window_fixed_size"); 209 HasEGLExtension("EGL_ANGLE_window_fixed_size");
188 210
211 // We always succeed beyond this point so set g_initialized here to avoid
212 // infinite recursion through CreateGLContext and GetDisplay
213 // if g_egl_surfaceless_context_supported.
214 g_initialized = true;
215 g_terminate_pending = false;
216
189 // TODO(oetuaho@nvidia.com): Surfaceless is disabled on Android as a temporary 217 // TODO(oetuaho@nvidia.com): Surfaceless is disabled on Android as a temporary
190 // workaround, since code written for Android WebView takes different paths 218 // workaround, since code written for Android WebView takes different paths
191 // based on whether GL surface objects have underlying EGL surface handles, 219 // based on whether GL surface objects have underlying EGL surface handles,
192 // conflicting with the use of surfaceless. See https://crbug.com/382349 220 // conflicting with the use of surfaceless. See https://crbug.com/382349
193 #if defined(OS_ANDROID) 221 #if defined(OS_ANDROID)
194 DCHECK(!g_egl_surfaceless_context_supported); 222 DCHECK(!g_egl_surfaceless_context_supported);
195 #else 223 #else
196 // Check if SurfacelessEGL is supported. 224 // Check if SurfacelessEGL is supported.
197 g_egl_surfaceless_context_supported = 225 g_egl_surfaceless_context_supported =
198 HasEGLExtension("EGL_KHR_surfaceless_context"); 226 HasEGLExtension("EGL_KHR_surfaceless_context");
199 if (g_egl_surfaceless_context_supported) { 227 if (g_egl_surfaceless_context_supported) {
200 // EGL_KHR_surfaceless_context is supported but ensure 228 // EGL_KHR_surfaceless_context is supported but ensure
201 // GL_OES_surfaceless_context is also supported. We need a current context 229 // GL_OES_surfaceless_context is also supported. We need a current context
202 // to query for supported GL extensions. 230 // to query for supported GL extensions.
203 scoped_refptr<GLSurface> surface = new SurfacelessEGL(Size(1, 1)); 231 scoped_refptr<GLSurface> surface = new SurfacelessEGL(Size(1, 1));
204 scoped_refptr<GLContext> context = GLContext::CreateGLContext( 232 scoped_refptr<GLContext> context = GLContext::CreateGLContext(
205 NULL, surface.get(), PreferIntegratedGpu); 233 NULL, surface.get(), PreferIntegratedGpu);
206 if (!context->MakeCurrent(surface.get())) 234 if (!context->MakeCurrent(surface.get()))
207 g_egl_surfaceless_context_supported = false; 235 g_egl_surfaceless_context_supported = false;
208 236
209 // Ensure context supports GL_OES_surfaceless_context. 237 // Ensure context supports GL_OES_surfaceless_context.
210 if (g_egl_surfaceless_context_supported) { 238 if (g_egl_surfaceless_context_supported) {
211 g_egl_surfaceless_context_supported = context->HasExtension( 239 g_egl_surfaceless_context_supported = context->HasExtension(
212 "GL_OES_surfaceless_context"); 240 "GL_OES_surfaceless_context");
213 context->ReleaseCurrent(surface.get()); 241 context->ReleaseCurrent(surface.get());
214 } 242 }
215 } 243 }
216 #endif 244 #endif
217 245
218 initialized = true;
219
220 return true; 246 return true;
221 } 247 }
222 248
223 EGLDisplay GLSurfaceEGL::GetDisplay() { 249 EGLDisplay GLSurfaceEGL::GetDisplay() {
250 DCHECK(g_initialized);
224 return g_display; 251 return g_display;
225 } 252 }
226 253
254 // static
227 EGLDisplay GLSurfaceEGL::GetHardwareDisplay() { 255 EGLDisplay GLSurfaceEGL::GetHardwareDisplay() {
256 if (!g_initialized) {
257 bool result = GLSurfaceEGL::InitializeOneOff();
258 DCHECK(result);
259 }
228 return g_display; 260 return g_display;
229 } 261 }
230 262
263 // static
231 EGLNativeDisplayType GLSurfaceEGL::GetNativeDisplay() { 264 EGLNativeDisplayType GLSurfaceEGL::GetNativeDisplay() {
232 return g_native_display; 265 if (!g_initialized) {
266 bool result = GLSurfaceEGL::InitializeOneOff();
267 DCHECK(result);
268 }
269 return g_native_display_type;
233 } 270 }
234 271
235 const char* GLSurfaceEGL::GetEGLExtensions() { 272 const char* GLSurfaceEGL::GetEGLExtensions() {
273 // No need for InitializeOneOff. Assume that extensions will not change
274 // after the first initialization.
236 return g_egl_extensions; 275 return g_egl_extensions;
237 } 276 }
238 277
239 bool GLSurfaceEGL::HasEGLExtension(const char* name) { 278 bool GLSurfaceEGL::HasEGLExtension(const char* name) {
240 return ExtensionsContain(GetEGLExtensions(), name); 279 return ExtensionsContain(GetEGLExtensions(), name);
241 } 280 }
242 281
243 bool GLSurfaceEGL::IsCreateContextRobustnessSupported() { 282 bool GLSurfaceEGL::IsCreateContextRobustnessSupported() {
244 return g_egl_create_context_robustness_supported; 283 return g_egl_create_context_robustness_supported;
245 } 284 }
246 285
247 bool GLSurfaceEGL::IsEGLSurfacelessContextSupported() { 286 bool GLSurfaceEGL::IsEGLSurfacelessContextSupported() {
248 return g_egl_surfaceless_context_supported; 287 return g_egl_surfaceless_context_supported;
249 } 288 }
250 289
251 GLSurfaceEGL::~GLSurfaceEGL() {} 290 void GLSurfaceEGL::DestroyAndTerminateDisplay() {
291 DCHECK(g_initialized);
292 DCHECK_EQ(g_num_surfaces, 1);
293 Destroy();
294 g_terminate_pending = true;
295 }
296
297 GLSurfaceEGL::~GLSurfaceEGL() {
298 DCHECK_GT(g_num_surfaces, 0) << "Bad surface count";
299 if (--g_num_surfaces == 0 && g_terminate_pending) {
300 DeinitializeEgl();
301 g_terminate_pending = false;
302 }
303 }
252 304
253 #if defined(OS_WIN) 305 #if defined(OS_WIN)
254 static const EGLint kDisplayAttribsWarp[] { 306 static const EGLint kDisplayAttribsWarp[] {
255 EGL_PLATFORM_ANGLE_TYPE_ANGLE, 307 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
256 EGL_PLATFORM_ANGLE_TYPE_D3D11_WARP_ANGLE, 308 EGL_PLATFORM_ANGLE_TYPE_D3D11_WARP_ANGLE,
257 EGL_NONE 309 EGL_NONE
258 }; 310 };
259 311
260 // static 312 // static
261 EGLDisplay GLSurfaceEGL::GetPlatformDisplay( 313 EGLDisplay GLSurfaceEGL::GetPlatformDisplay(
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 } 790 }
739 791
740 void* SurfacelessEGL::GetShareHandle() { 792 void* SurfacelessEGL::GetShareHandle() {
741 return NULL; 793 return NULL;
742 } 794 }
743 795
744 SurfacelessEGL::~SurfacelessEGL() { 796 SurfacelessEGL::~SurfacelessEGL() {
745 } 797 }
746 798
747 } // namespace gfx 799 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gl/gl_surface_egl.h ('k') | ui/ozone/platform/dri/dri_gpu_platform_support.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698