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

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: Requested fixes and a few other minor corrections 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(
114 const gfx::SurfaceConfiguration requested_configuration)
115 : GLSurface(requested_configuration) {
115 ++g_num_surfaces; 116 ++g_num_surfaces;
116 if (!g_initialized) { 117 if (!g_initialized) {
117 bool result = GLSurfaceEGL::InitializeOneOff(); 118 bool result = GLSurfaceEGL::InitializeOneOff();
118 DCHECK(result); 119 DCHECK(result);
119 DCHECK(g_initialized); 120 DCHECK(g_initialized);
120 } 121 }
121 } 122 }
122 123
123 bool GLSurfaceEGL::InitializeOneOff() { 124 void* GetEGLConfig(const gfx::SurfaceConfiguration configuration,
124 if (g_initialized) 125 bool allow_window_bit) {
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. 126 // Choose an EGL configuration.
142 // On X this is only used for PBuffer surfaces. 127 // On X this is only used for PBuffer surfaces.
128 EGLConfig config = {0};
129
130 #if defined(USE_X11)
131 XWindowAttributes win_attribs;
132 if (!XGetWindowAttributes(GetNativeDisplay(), window_, &win_attribs)) {
133 return nullptr;
134 }
135 #endif
136
143 EGLint renderable_type = EGL_OPENGL_ES2_BIT; 137 EGLint renderable_type = EGL_OPENGL_ES2_BIT;
144 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 138 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
145 switches::kEnableUnsafeES3APIs)) { 139 switches::kEnableUnsafeES3APIs)) {
146 renderable_type = EGL_OPENGL_ES3_BIT; 140 renderable_type = EGL_OPENGL_ES3_BIT;
147 } 141 }
148 const EGLint kConfigAttribs[] = { 142 const EGLint kConfigAttribs[] = {
149 EGL_BUFFER_SIZE, 32, 143 EGL_BUFFER_SIZE, configuration.alpha_bits +
150 EGL_ALPHA_SIZE, 8, 144 configuration.red_bits +
151 EGL_BLUE_SIZE, 8, 145 configuration.green_bits +
152 EGL_GREEN_SIZE, 8, 146 configuration.blue_bits,
153 EGL_RED_SIZE, 8, 147 EGL_ALPHA_SIZE, configuration.alpha_bits,
148 EGL_BLUE_SIZE, configuration.blue_bits,
149 EGL_GREEN_SIZE, configuration.green_bits,
150 EGL_RED_SIZE, configuration.red_bits,
151 EGL_DEPTH_SIZE, configuration.depth_bits,
152 EGL_STENCIL_SIZE, configuration.stencil_bits,
154 EGL_RENDERABLE_TYPE, renderable_type, 153 EGL_RENDERABLE_TYPE, renderable_type,
155 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT, 154 EGL_SURFACE_TYPE, (allow_window_bit ?
155 (EGL_WINDOW_BIT | EGL_PBUFFER_BIT) :
156 EGL_PBUFFER_BIT),
156 EGL_NONE 157 EGL_NONE
157 }; 158 };
158 159
160 #if defined(USE_X11)
161 // Try matching the window depth with an alpha channel,
162 // because we're worried the destination alpha width could
163 // constrain blending precision.
164 const int kBufferSizeOffset = 1;
165 const int kAlphaSizeOffset = 3;
166 config_attribs[kBufferSizeOffset] = win_attribs.depth;
167 #endif
168
159 #if defined(USE_OZONE) 169 #if defined(USE_OZONE)
160 const EGLint* config_attribs = 170 const EGLint* config_attribs =
161 ui::SurfaceFactoryOzone::GetInstance()->GetEGLSurfaceProperties( 171 ui::SurfaceFactoryOzone::GetInstance()->GetEGLSurfaceProperties(
162 kConfigAttribs); 172 kConfigAttribs);
163 #else 173 #else
164 const EGLint* config_attribs = kConfigAttribs; 174 const EGLint* config_attribs = kConfigAttribs;
165 #endif 175 #endif
166 176
167 EGLint num_configs; 177 EGLint num_configs;
168 if (!eglChooseConfig(g_display, 178 if (!eglChooseConfig(g_display,
169 config_attribs, 179 config_attribs,
170 NULL, 180 NULL,
171 0, 181 0,
172 &num_configs)) { 182 &num_configs)) {
173 LOG(ERROR) << "eglChooseConfig failed with error " 183 LOG(ERROR) << "eglChooseConfig failed with error "
174 << GetLastEGLErrorString(); 184 << GetLastEGLErrorString();
175 return false; 185 return nullptr;
176 }
177
178 if (num_configs == 0) {
179 LOG(ERROR) << "No suitable EGL configs found.";
180 return false;
181 } 186 }
182 187
183 if (!eglChooseConfig(g_display, 188 if (!eglChooseConfig(g_display,
184 config_attribs, 189 config_attribs,
185 &g_config, 190 &config,
186 1, 191 1,
187 &num_configs)) { 192 &num_configs)) {
188 LOG(ERROR) << "eglChooseConfig failed with error " 193 LOG(ERROR) << "eglChooseConfig failed with error "
189 << GetLastEGLErrorString(); 194 << GetLastEGLErrorString();
195 return nullptr;
196 }
197
198 #if defined(USE_X11)
199 if (num_configs) {
200 EGLint config_depth;
201 if (!eglGetConfigAttrib(g_display,
202 config,
203 EGL_BUFFER_SIZE,
204 &config_depth)) {
205 LOG(ERROR) << "eglGetConfigAttrib failed with error "
206 << GetLastEGLErrorString();
207 return nullptr;
208 }
209
210 if (config_depth == win_attribs.depth) {
211 return config;
212 }
213 }
214
215 // Try without an alpha channel.
216 config_attribs[kAlphaSizeOffset] = 0;
217 if (!eglChooseConfig(g_display,
218 config_attribs,
219 &config,
220 1,
221 &num_configs)) {
222 LOG(ERROR) << "eglChooseConfig failed with error "
223 << GetLastEGLErrorString();
224 return nullptr;
225 }
226 #endif
227
228 if (num_configs == 0) {
229 LOG(ERROR) << "No suitable EGL configs found.";
230 return nullptr;
231 }
232
233 return config;
234 }
235
236 bool GLSurfaceEGL::InitializeOneOff() {
237 if (g_initialized)
238 return true;
239
240 g_native_display_type = GetPlatformDefaultEGLNativeDisplay();
241
242 g_display = eglGetDisplay(g_native_display_type);
243
244 if (!g_display) {
245 LOG(ERROR) << "eglGetDisplay failed with error " << GetLastEGLErrorString();
190 return false; 246 return false;
191 } 247 }
192 248
249 if (!eglInitialize(g_display, NULL, NULL)) {
250 LOG(ERROR) << "eglInitialize failed with error " << GetLastEGLErrorString();
251 return false;
252 }
253
193 g_egl_extensions = eglQueryString(g_display, EGL_EXTENSIONS); 254 g_egl_extensions = eglQueryString(g_display, EGL_EXTENSIONS);
194 g_egl_create_context_robustness_supported = 255 g_egl_create_context_robustness_supported =
195 HasEGLExtension("EGL_EXT_create_context_robustness"); 256 HasEGLExtension("EGL_EXT_create_context_robustness");
196 g_egl_sync_control_supported = 257 g_egl_sync_control_supported =
197 HasEGLExtension("EGL_CHROMIUM_sync_control"); 258 HasEGLExtension("EGL_CHROMIUM_sync_control");
198 g_egl_window_fixed_size_supported = 259 g_egl_window_fixed_size_supported =
199 HasEGLExtension("EGL_ANGLE_window_fixed_size"); 260 HasEGLExtension("EGL_ANGLE_window_fixed_size");
200 261
201 // We always succeed beyond this point so set g_initialized here to avoid 262 // We always succeed beyond this point so set g_initialized here to avoid
202 // infinite recursion through CreateGLContext and GetDisplay 263 // infinite recursion through CreateGLContext and GetDisplay
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 } 346 }
286 347
287 GLSurfaceEGL::~GLSurfaceEGL() { 348 GLSurfaceEGL::~GLSurfaceEGL() {
288 DCHECK_GT(g_num_surfaces, 0) << "Bad surface count"; 349 DCHECK_GT(g_num_surfaces, 0) << "Bad surface count";
289 if (--g_num_surfaces == 0 && g_terminate_pending) { 350 if (--g_num_surfaces == 0 && g_terminate_pending) {
290 DeinitializeEgl(); 351 DeinitializeEgl();
291 g_terminate_pending = false; 352 g_terminate_pending = false;
292 } 353 }
293 } 354 }
294 355
295 NativeViewGLSurfaceEGL::NativeViewGLSurfaceEGL(EGLNativeWindowType window) 356 NativeViewGLSurfaceEGL::NativeViewGLSurfaceEGL(
296 : window_(window), 357 EGLNativeWindowType window,
358 const gfx::SurfaceConfiguration requested_configuration)
359 : GLSurfaceEGL(requested_configuration),
360 window_(window),
297 surface_(NULL), 361 surface_(NULL),
298 supports_post_sub_buffer_(false), 362 supports_post_sub_buffer_(false),
299 config_(NULL), 363 config_(NULL),
300 size_(1, 1), 364 size_(1, 1),
301 swap_interval_(1) { 365 swap_interval_(1) {
302 #if defined(OS_ANDROID) 366 #if defined(OS_ANDROID)
303 if (window) 367 if (window)
304 ANativeWindow_acquire(window); 368 ANativeWindow_acquire(window);
305 #endif 369 #endif
306 } 370 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 if (surface_) { 428 if (surface_) {
365 if (!eglDestroySurface(GetDisplay(), surface_)) { 429 if (!eglDestroySurface(GetDisplay(), surface_)) {
366 LOG(ERROR) << "eglDestroySurface failed with error " 430 LOG(ERROR) << "eglDestroySurface failed with error "
367 << GetLastEGLErrorString(); 431 << GetLastEGLErrorString();
368 } 432 }
369 surface_ = NULL; 433 surface_ = NULL;
370 } 434 }
371 } 435 }
372 436
373 EGLConfig NativeViewGLSurfaceEGL::GetConfig() { 437 EGLConfig NativeViewGLSurfaceEGL::GetConfig() {
374 #if !defined(USE_X11)
375 return g_config;
376 #else
377 if (!config_) { 438 if (!config_) {
378 // Get a config compatible with the window
379 DCHECK(window_); 439 DCHECK(window_);
380 XWindowAttributes win_attribs; 440 config_ = GetEGLConfig(this->get_surface_configuration(), 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 } 441 }
446 return config_; 442 return config_;
447 #endif
448 } 443 }
449 444
450 bool NativeViewGLSurfaceEGL::IsOffscreen() { 445 bool NativeViewGLSurfaceEGL::IsOffscreen() {
451 return false; 446 return false;
452 } 447 }
453 448
454 bool NativeViewGLSurfaceEGL::SwapBuffers() { 449 bool NativeViewGLSurfaceEGL::SwapBuffers() {
455 TRACE_EVENT2("gpu", "NativeViewGLSurfaceEGL:RealSwapBuffers", 450 TRACE_EVENT2("gpu", "NativeViewGLSurfaceEGL:RealSwapBuffers",
456 "width", GetSize().width(), 451 "width", GetSize().width(),
457 "height", GetSize().height()); 452 "height", GetSize().height());
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 } 536 }
542 537
543 NativeViewGLSurfaceEGL::~NativeViewGLSurfaceEGL() { 538 NativeViewGLSurfaceEGL::~NativeViewGLSurfaceEGL() {
544 Destroy(); 539 Destroy();
545 #if defined(OS_ANDROID) 540 #if defined(OS_ANDROID)
546 if (window_) 541 if (window_)
547 ANativeWindow_release(window_); 542 ANativeWindow_release(window_);
548 #endif 543 #endif
549 } 544 }
550 545
551 PbufferGLSurfaceEGL::PbufferGLSurfaceEGL(const gfx::Size& size) 546 PbufferGLSurfaceEGL::PbufferGLSurfaceEGL(
552 : size_(size), 547 const gfx::Size& size,
553 surface_(NULL) { 548 const gfx::SurfaceConfiguration requested_configuration)
549 : GLSurfaceEGL(requested_configuration),
550 size_(size),
551 surface_(nullptr),
552 config_(nullptr) {
554 // Some implementations of Pbuffer do not support having a 0 size. For such 553 // Some implementations of Pbuffer do not support having a 0 size. For such
555 // cases use a (1, 1) surface. 554 // cases use a (1, 1) surface.
556 if (size_.GetArea() == 0) 555 if (size_.GetArea() == 0)
557 size_.SetSize(1, 1); 556 size_.SetSize(1, 1);
558 } 557 }
559 558
560 bool PbufferGLSurfaceEGL::Initialize() { 559 bool PbufferGLSurfaceEGL::Initialize() {
561 EGLSurface old_surface = surface_; 560 EGLSurface old_surface = surface_;
562 561
563 EGLDisplay display = GetDisplay(); 562 EGLDisplay display = GetDisplay();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
596 if (surface_) { 595 if (surface_) {
597 if (!eglDestroySurface(GetDisplay(), surface_)) { 596 if (!eglDestroySurface(GetDisplay(), surface_)) {
598 LOG(ERROR) << "eglDestroySurface failed with error " 597 LOG(ERROR) << "eglDestroySurface failed with error "
599 << GetLastEGLErrorString(); 598 << GetLastEGLErrorString();
600 } 599 }
601 surface_ = NULL; 600 surface_ = NULL;
602 } 601 }
603 } 602 }
604 603
605 EGLConfig PbufferGLSurfaceEGL::GetConfig() { 604 EGLConfig PbufferGLSurfaceEGL::GetConfig() {
606 return g_config; 605 if (!config_) {
606 config_ = GetEGLConfig(this->get_surface_configuration(), false);
607 }
608 return config_;
607 } 609 }
608 610
609 bool PbufferGLSurfaceEGL::IsOffscreen() { 611 bool PbufferGLSurfaceEGL::IsOffscreen() {
610 return true; 612 return true;
611 } 613 }
612 614
613 bool PbufferGLSurfaceEGL::SwapBuffers() { 615 bool PbufferGLSurfaceEGL::SwapBuffers() {
614 NOTREACHED() << "Attempted to call SwapBuffers on a PbufferGLSurfaceEGL."; 616 NOTREACHED() << "Attempted to call SwapBuffers on a PbufferGLSurfaceEGL.";
615 return false; 617 return false;
616 } 618 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 } 668 }
667 669
668 return handle; 670 return handle;
669 #endif 671 #endif
670 } 672 }
671 673
672 PbufferGLSurfaceEGL::~PbufferGLSurfaceEGL() { 674 PbufferGLSurfaceEGL::~PbufferGLSurfaceEGL() {
673 Destroy(); 675 Destroy();
674 } 676 }
675 677
676 SurfacelessEGL::SurfacelessEGL(const gfx::Size& size) 678 SurfacelessEGL::SurfacelessEGL(
677 : size_(size) { 679 const gfx::Size& size,
680 const gfx::SurfaceConfiguration requested_configuration)
681 : GLSurfaceEGL(requested_configuration), size_(size) {
678 } 682 }
679 683
680 bool SurfacelessEGL::Initialize() { 684 bool SurfacelessEGL::Initialize() {
681 return true; 685 return true;
682 } 686 }
683 687
684 void SurfacelessEGL::Destroy() { 688 void SurfacelessEGL::Destroy() {
685 } 689 }
686 690
687 EGLConfig SurfacelessEGL::GetConfig() { 691 EGLConfig SurfacelessEGL::GetConfig() {
688 return g_config; 692 return NULL;
689 } 693 }
690 694
691 bool SurfacelessEGL::IsOffscreen() { 695 bool SurfacelessEGL::IsOffscreen() {
692 return true; 696 return true;
693 } 697 }
694 698
695 bool SurfacelessEGL::IsSurfaceless() const { 699 bool SurfacelessEGL::IsSurfaceless() const {
696 return true; 700 return true;
697 } 701 }
698 702
(...skipping 16 matching lines...) Expand all
715 } 719 }
716 720
717 void* SurfacelessEGL::GetShareHandle() { 721 void* SurfacelessEGL::GetShareHandle() {
718 return NULL; 722 return NULL;
719 } 723 }
720 724
721 SurfacelessEGL::~SurfacelessEGL() { 725 SurfacelessEGL::~SurfacelessEGL() {
722 } 726 }
723 727
724 } // namespace gfx 728 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698