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

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

Issue 1168993002: Update the native_viewport interface to allow specification of the surface configuration, currently… (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 6 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
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 29 matching lines...) Expand all
40 #if !defined(EGL_OPENGL_ES3_BIT) 40 #if !defined(EGL_OPENGL_ES3_BIT)
41 #define EGL_OPENGL_ES3_BIT 0x00000040 41 #define EGL_OPENGL_ES3_BIT 0x00000040
42 #endif 42 #endif
43 43
44 using ui::GetLastEGLErrorString; 44 using ui::GetLastEGLErrorString;
45 45
46 namespace gfx { 46 namespace gfx {
47 47
48 namespace { 48 namespace {
49 49
50 EGLConfig g_config;
51 EGLDisplay g_display; 50 EGLDisplay g_display;
52 EGLNativeDisplayType g_native_display_type; 51 EGLNativeDisplayType g_native_display_type;
53 52
54 // In the Cast environment, we need to destroy the EGLNativeDisplayType and 53 // In the Cast environment, we need to destroy the EGLNativeDisplayType and
55 // EGLDisplay returned by the GPU platform when we switch to an external app 54 // EGLDisplay returned by the GPU platform when we switch to an external app
56 // which will temporarily own all screen and GPU resources. 55 // which will temporarily own all screen and GPU resources.
57 // Even though Chromium is still in the background. 56 // Even though Chromium is still in the background.
58 // As such, it must be reinitialized each time we come back to the foreground. 57 // As such, it must be reinitialized each time we come back to the foreground.
59 bool g_initialized = false; 58 bool g_initialized = false;
60 int g_num_surfaces = 0; 59 int g_num_surfaces = 0;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 103
105 void DeinitializeEgl() { 104 void DeinitializeEgl() {
106 if (g_initialized) { 105 if (g_initialized) {
107 g_initialized = false; 106 g_initialized = false;
108 eglTerminate(g_display); 107 eglTerminate(g_display);
109 } 108 }
110 } 109 }
111 110
112 } // namespace 111 } // namespace
113 112
114 GLSurfaceEGL::GLSurfaceEGL() { 113 GLSurfaceEGL::GLSurfaceEGL(SurfaceConfiguration requested_configuration)
114 : GLSurface(requested_configuration) {
115 ++g_num_surfaces; 115 ++g_num_surfaces;
116 if (!g_initialized) { 116 if (!g_initialized) {
117 bool result = GLSurfaceEGL::InitializeOneOff(); 117 bool result = GLSurfaceEGL::InitializeOneOff();
118 DCHECK(result); 118 DCHECK(result);
119 DCHECK(g_initialized); 119 DCHECK(g_initialized);
120 } 120 }
121 } 121 }
122 122
123 bool GLSurfaceEGL::InitializeOneOff() { 123 void* GetEGLConfig(SurfaceConfiguration configuration, bool allow_window_bit) {
124 if (g_initialized)
125 return true;
126
127 g_native_display_type = GetPlatformDefaultEGLNativeDisplay();
128
129 g_display = eglGetDisplay(g_native_display_type);
130
131 if (!g_display) {
132 LOG(ERROR) << "eglGetDisplay failed with error " << GetLastEGLErrorString();
133 return false;
134 }
135
136 if (!eglInitialize(g_display, NULL, NULL)) {
137 LOG(ERROR) << "eglInitialize failed with error " << GetLastEGLErrorString();
138 return false;
139 }
140
141 // Choose an EGL configuration. 124 // Choose an EGL configuration.
142 // On X this is only used for PBuffer surfaces. 125 // On X this is only used for PBuffer surfaces.
126 EGLConfig config = {0};
127
128 #if defined(USE_X11)
129 XWindowAttributes win_attribs;
130 if (!XGetWindowAttributes(GetNativeDisplay(), window_, &win_attribs)) {
131 return NULL;
132 }
133 #endif
134
143 EGLint renderable_type = EGL_OPENGL_ES2_BIT; 135 EGLint renderable_type = EGL_OPENGL_ES2_BIT;
144 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 136 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
145 switches::kEnableUnsafeES3APIs)) { 137 switches::kEnableUnsafeES3APIs)) {
146 renderable_type = EGL_OPENGL_ES3_BIT; 138 renderable_type = EGL_OPENGL_ES3_BIT;
147 } 139 }
148 const EGLint kConfigAttribs[] = { 140 const EGLint kConfigAttribs[] = {
149 EGL_BUFFER_SIZE, 32, 141 EGL_BUFFER_SIZE, configuration.alpha_bits +
150 EGL_ALPHA_SIZE, 8, 142 configuration.red_bits +
151 EGL_BLUE_SIZE, 8, 143 configuration.green_bits +
152 EGL_GREEN_SIZE, 8, 144 configuration.blue_bits,
153 EGL_RED_SIZE, 8, 145 EGL_ALPHA_SIZE, configuration.alpha_bits,
146 EGL_BLUE_SIZE, configuration.blue_bits,
147 EGL_GREEN_SIZE, configuration.green_bits,
148 EGL_RED_SIZE, configuration.red_bits,
149 EGL_DEPTH_SIZE, configuration.depth_bits,
150 EGL_STENCIL_SIZE, configuration.stencil_bits,
154 EGL_RENDERABLE_TYPE, renderable_type, 151 EGL_RENDERABLE_TYPE, renderable_type,
155 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT, 152 EGL_SURFACE_TYPE, (allow_window_bit ?
153 (EGL_WINDOW_BIT | EGL_PBUFFER_BIT) :
154 EGL_PBUFFER_BIT),
156 EGL_NONE 155 EGL_NONE
157 }; 156 };
158 157
158 #if defined(USE_X11)
159 // Try matching the window depth with an alpha channel,
160 // because we're worried the destination alpha width could
161 // constrain blending precision.
162 const int kBufferSizeOffset = 1;
163 const int kAlphaSizeOffset = 3;
164 config_attribs[kBufferSizeOffset] = win_attribs.depth;
165 #endif
166
159 #if defined(USE_OZONE) 167 #if defined(USE_OZONE)
160 const EGLint* config_attribs = 168 const EGLint* config_attribs =
161 ui::SurfaceFactoryOzone::GetInstance()->GetEGLSurfaceProperties( 169 ui::SurfaceFactoryOzone::GetInstance()->GetEGLSurfaceProperties(
162 kConfigAttribs); 170 kConfigAttribs);
163 #else 171 #else
164 const EGLint* config_attribs = kConfigAttribs; 172 const EGLint* config_attribs = kConfigAttribs;
165 #endif 173 #endif
166 174
167 EGLint num_configs; 175 EGLint num_configs;
168 if (!eglChooseConfig(g_display, 176 if (!eglChooseConfig(g_display,
169 config_attribs, 177 config_attribs,
170 NULL, 178 NULL,
171 0, 179 0,
172 &num_configs)) { 180 &num_configs)) {
173 LOG(ERROR) << "eglChooseConfig failed with error " 181 LOG(ERROR) << "eglChooseConfig failed with error "
174 << GetLastEGLErrorString(); 182 << GetLastEGLErrorString();
175 return false; 183 return NULL;
abarth-chromium 2015/06/09 00:30:22 nullptr
iansf 2015/06/09 01:52:05 Done.
176 }
177
178 if (num_configs == 0) {
179 LOG(ERROR) << "No suitable EGL configs found.";
180 return false;
181 } 184 }
182 185
183 if (!eglChooseConfig(g_display, 186 if (!eglChooseConfig(g_display,
184 config_attribs, 187 config_attribs,
185 &g_config, 188 &config,
186 1, 189 1,
187 &num_configs)) { 190 &num_configs)) {
188 LOG(ERROR) << "eglChooseConfig failed with error " 191 LOG(ERROR) << "eglChooseConfig failed with error "
189 << GetLastEGLErrorString(); 192 << GetLastEGLErrorString();
193 return NULL;
abarth-chromium 2015/06/09 00:30:22 ditto
iansf 2015/06/09 01:52:05 Done.
194 }
195
196 #if defined(USE_X11)
197 if (num_configs) {
198 EGLint config_depth;
199 if (!eglGetConfigAttrib(g_display,
200 config,
201 EGL_BUFFER_SIZE,
202 &config_depth)) {
203 LOG(ERROR) << "eglGetConfigAttrib failed with error "
204 << GetLastEGLErrorString();
205 return NULL;
206 }
207
208 if (config_depth == win_attribs.depth) {
209 return config;
210 }
211 }
212
213 // Try without an alpha channel.
214 config_attribs[kAlphaSizeOffset] = 0;
215 if (!eglChooseConfig(g_display,
216 config_attribs,
217 &config,
218 1,
219 &num_configs)) {
220 LOG(ERROR) << "eglChooseConfig failed with error "
221 << GetLastEGLErrorString();
222 return NULL;
223 }
224 #endif
225
226 if (num_configs == 0) {
227 LOG(ERROR) << "No suitable EGL configs found.";
228 return NULL;
229 }
230
231 return config;
232 }
233
234 bool GLSurfaceEGL::InitializeOneOff() {
235 if (g_initialized)
236 return true;
237
238 g_native_display_type = GetPlatformDefaultEGLNativeDisplay();
239
240 g_display = eglGetDisplay(g_native_display_type);
241
242 if (!g_display) {
243 LOG(ERROR) << "eglGetDisplay failed with error " << GetLastEGLErrorString();
190 return false; 244 return false;
191 } 245 }
192 246
247 if (!eglInitialize(g_display, NULL, NULL)) {
248 LOG(ERROR) << "eglInitialize failed with error " << GetLastEGLErrorString();
249 return false;
250 }
251
193 g_egl_extensions = eglQueryString(g_display, EGL_EXTENSIONS); 252 g_egl_extensions = eglQueryString(g_display, EGL_EXTENSIONS);
194 g_egl_create_context_robustness_supported = 253 g_egl_create_context_robustness_supported =
195 HasEGLExtension("EGL_EXT_create_context_robustness"); 254 HasEGLExtension("EGL_EXT_create_context_robustness");
196 g_egl_sync_control_supported = 255 g_egl_sync_control_supported =
197 HasEGLExtension("EGL_CHROMIUM_sync_control"); 256 HasEGLExtension("EGL_CHROMIUM_sync_control");
198 g_egl_window_fixed_size_supported = 257 g_egl_window_fixed_size_supported =
199 HasEGLExtension("EGL_ANGLE_window_fixed_size"); 258 HasEGLExtension("EGL_ANGLE_window_fixed_size");
200 259
201 // We always succeed beyond this point so set g_initialized here to avoid 260 // We always succeed beyond this point so set g_initialized here to avoid
202 // infinite recursion through CreateGLContext and GetDisplay 261 // infinite recursion through CreateGLContext and GetDisplay
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 } 344 }
286 345
287 GLSurfaceEGL::~GLSurfaceEGL() { 346 GLSurfaceEGL::~GLSurfaceEGL() {
288 DCHECK_GT(g_num_surfaces, 0) << "Bad surface count"; 347 DCHECK_GT(g_num_surfaces, 0) << "Bad surface count";
289 if (--g_num_surfaces == 0 && g_terminate_pending) { 348 if (--g_num_surfaces == 0 && g_terminate_pending) {
290 DeinitializeEgl(); 349 DeinitializeEgl();
291 g_terminate_pending = false; 350 g_terminate_pending = false;
292 } 351 }
293 } 352 }
294 353
295 NativeViewGLSurfaceEGL::NativeViewGLSurfaceEGL(EGLNativeWindowType window) 354 NativeViewGLSurfaceEGL::NativeViewGLSurfaceEGL(
296 : window_(window), 355 EGLNativeWindowType window,
356 SurfaceConfiguration requested_configuration)
357 : GLSurfaceEGL(requested_configuration),
358 window_(window),
297 surface_(NULL), 359 surface_(NULL),
298 supports_post_sub_buffer_(false), 360 supports_post_sub_buffer_(false),
299 config_(NULL), 361 config_(NULL),
300 size_(1, 1), 362 size_(1, 1),
301 swap_interval_(1) { 363 swap_interval_(1) {
302 #if defined(OS_ANDROID) 364 #if defined(OS_ANDROID)
303 if (window) 365 if (window)
304 ANativeWindow_acquire(window); 366 ANativeWindow_acquire(window);
305 #endif 367 #endif
306 } 368 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 if (surface_) { 426 if (surface_) {
365 if (!eglDestroySurface(GetDisplay(), surface_)) { 427 if (!eglDestroySurface(GetDisplay(), surface_)) {
366 LOG(ERROR) << "eglDestroySurface failed with error " 428 LOG(ERROR) << "eglDestroySurface failed with error "
367 << GetLastEGLErrorString(); 429 << GetLastEGLErrorString();
368 } 430 }
369 surface_ = NULL; 431 surface_ = NULL;
370 } 432 }
371 } 433 }
372 434
373 EGLConfig NativeViewGLSurfaceEGL::GetConfig() { 435 EGLConfig NativeViewGLSurfaceEGL::GetConfig() {
374 #if !defined(USE_X11)
375 return g_config;
376 #else
377 if (!config_) { 436 if (!config_) {
378 // Get a config compatible with the window
379 DCHECK(window_); 437 DCHECK(window_);
380 XWindowAttributes win_attribs; 438 config_ = GetEGLConfig(this->GetSurfaceConfiguration(), true);
381 if (!XGetWindowAttributes(GetNativeDisplay(), window_, &win_attribs)) {
382 return NULL;
383 }
384
385 // Try matching the window depth with an alpha channel,
386 // because we're worried the destination alpha width could
387 // constrain blending precision.
388 const int kBufferSizeOffset = 1;
389 const int kAlphaSizeOffset = 3;
390 EGLint config_attribs[] = {
391 EGL_BUFFER_SIZE, ~0,
392 EGL_ALPHA_SIZE, 8,
393 EGL_BLUE_SIZE, 8,
394 EGL_GREEN_SIZE, 8,
395 EGL_RED_SIZE, 8,
396 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
397 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
398 EGL_NONE
399 };
400 config_attribs[kBufferSizeOffset] = win_attribs.depth;
401
402 EGLint num_configs;
403 if (!eglChooseConfig(g_display,
404 config_attribs,
405 &config_,
406 1,
407 &num_configs)) {
408 LOG(ERROR) << "eglChooseConfig failed with error "
409 << GetLastEGLErrorString();
410 return NULL;
411 }
412
413 if (num_configs) {
414 EGLint config_depth;
415 if (!eglGetConfigAttrib(g_display,
416 config_,
417 EGL_BUFFER_SIZE,
418 &config_depth)) {
419 LOG(ERROR) << "eglGetConfigAttrib failed with error "
420 << GetLastEGLErrorString();
421 return NULL;
422 }
423
424 if (config_depth == win_attribs.depth) {
425 return config_;
426 }
427 }
428
429 // Try without an alpha channel.
430 config_attribs[kAlphaSizeOffset] = 0;
431 if (!eglChooseConfig(g_display,
432 config_attribs,
433 &config_,
434 1,
435 &num_configs)) {
436 LOG(ERROR) << "eglChooseConfig failed with error "
437 << GetLastEGLErrorString();
438 return NULL;
439 }
440
441 if (num_configs == 0) {
442 LOG(ERROR) << "No suitable EGL configs found.";
443 return NULL;
444 }
445 } 439 }
446 return config_; 440 return config_;
447 #endif
448 } 441 }
449 442
450 bool NativeViewGLSurfaceEGL::IsOffscreen() { 443 bool NativeViewGLSurfaceEGL::IsOffscreen() {
451 return false; 444 return false;
452 } 445 }
453 446
454 bool NativeViewGLSurfaceEGL::SwapBuffers() { 447 bool NativeViewGLSurfaceEGL::SwapBuffers() {
455 TRACE_EVENT2("gpu", "NativeViewGLSurfaceEGL:RealSwapBuffers", 448 TRACE_EVENT2("gpu", "NativeViewGLSurfaceEGL:RealSwapBuffers",
456 "width", GetSize().width(), 449 "width", GetSize().width(),
457 "height", GetSize().height()); 450 "height", GetSize().height());
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 } 534 }
542 535
543 NativeViewGLSurfaceEGL::~NativeViewGLSurfaceEGL() { 536 NativeViewGLSurfaceEGL::~NativeViewGLSurfaceEGL() {
544 Destroy(); 537 Destroy();
545 #if defined(OS_ANDROID) 538 #if defined(OS_ANDROID)
546 if (window_) 539 if (window_)
547 ANativeWindow_release(window_); 540 ANativeWindow_release(window_);
548 #endif 541 #endif
549 } 542 }
550 543
551 PbufferGLSurfaceEGL::PbufferGLSurfaceEGL(const gfx::Size& size) 544 PbufferGLSurfaceEGL::PbufferGLSurfaceEGL(
552 : size_(size), 545 const gfx::Size& size,
553 surface_(NULL) { 546 SurfaceConfiguration requested_configuration)
547 : GLSurfaceEGL(requested_configuration),
548 size_(size),
549 surface_(NULL),
550 config_(NULL) {
abarth-chromium 2015/06/09 00:30:22 nullptr
iansf 2015/06/09 01:52:05 Done.
554 // Some implementations of Pbuffer do not support having a 0 size. For such 551 // Some implementations of Pbuffer do not support having a 0 size. For such
555 // cases use a (1, 1) surface. 552 // cases use a (1, 1) surface.
556 if (size_.GetArea() == 0) 553 if (size_.GetArea() == 0)
557 size_.SetSize(1, 1); 554 size_.SetSize(1, 1);
558 } 555 }
559 556
560 bool PbufferGLSurfaceEGL::Initialize() { 557 bool PbufferGLSurfaceEGL::Initialize() {
561 EGLSurface old_surface = surface_; 558 EGLSurface old_surface = surface_;
562 559
563 EGLDisplay display = GetDisplay(); 560 EGLDisplay display = GetDisplay();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
596 if (surface_) { 593 if (surface_) {
597 if (!eglDestroySurface(GetDisplay(), surface_)) { 594 if (!eglDestroySurface(GetDisplay(), surface_)) {
598 LOG(ERROR) << "eglDestroySurface failed with error " 595 LOG(ERROR) << "eglDestroySurface failed with error "
599 << GetLastEGLErrorString(); 596 << GetLastEGLErrorString();
600 } 597 }
601 surface_ = NULL; 598 surface_ = NULL;
602 } 599 }
603 } 600 }
604 601
605 EGLConfig PbufferGLSurfaceEGL::GetConfig() { 602 EGLConfig PbufferGLSurfaceEGL::GetConfig() {
606 return g_config; 603 if (!config_) {
604 config_ = GetEGLConfig(this->GetSurfaceConfiguration(), false);
605 }
606 return config_;
607 } 607 }
608 608
609 bool PbufferGLSurfaceEGL::IsOffscreen() { 609 bool PbufferGLSurfaceEGL::IsOffscreen() {
610 return true; 610 return true;
611 } 611 }
612 612
613 bool PbufferGLSurfaceEGL::SwapBuffers() { 613 bool PbufferGLSurfaceEGL::SwapBuffers() {
614 NOTREACHED() << "Attempted to call SwapBuffers on a PbufferGLSurfaceEGL."; 614 NOTREACHED() << "Attempted to call SwapBuffers on a PbufferGLSurfaceEGL.";
615 return false; 615 return false;
616 } 616 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 } 666 }
667 667
668 return handle; 668 return handle;
669 #endif 669 #endif
670 } 670 }
671 671
672 PbufferGLSurfaceEGL::~PbufferGLSurfaceEGL() { 672 PbufferGLSurfaceEGL::~PbufferGLSurfaceEGL() {
673 Destroy(); 673 Destroy();
674 } 674 }
675 675
676 SurfacelessEGL::SurfacelessEGL(const gfx::Size& size) 676 SurfacelessEGL::SurfacelessEGL(const gfx::Size& size,
677 : size_(size) { 677 SurfaceConfiguration requested_configuration)
678 : GLSurfaceEGL(requested_configuration), size_(size) {
678 } 679 }
679 680
680 bool SurfacelessEGL::Initialize() { 681 bool SurfacelessEGL::Initialize() {
681 return true; 682 return true;
682 } 683 }
683 684
684 void SurfacelessEGL::Destroy() { 685 void SurfacelessEGL::Destroy() {
685 } 686 }
686 687
687 EGLConfig SurfacelessEGL::GetConfig() { 688 EGLConfig SurfacelessEGL::GetConfig() {
688 return g_config; 689 return NULL;
689 } 690 }
690 691
691 bool SurfacelessEGL::IsOffscreen() { 692 bool SurfacelessEGL::IsOffscreen() {
692 return true; 693 return true;
693 } 694 }
694 695
695 bool SurfacelessEGL::IsSurfaceless() const { 696 bool SurfacelessEGL::IsSurfaceless() const {
696 return true; 697 return true;
697 } 698 }
698 699
(...skipping 16 matching lines...) Expand all
715 } 716 }
716 717
717 void* SurfacelessEGL::GetShareHandle() { 718 void* SurfacelessEGL::GetShareHandle() {
718 return NULL; 719 return NULL;
719 } 720 }
720 721
721 SurfacelessEGL::~SurfacelessEGL() { 722 SurfacelessEGL::~SurfacelessEGL() {
722 } 723 }
723 724
724 } // namespace gfx 725 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698