OLD | NEW |
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 // This include must be here so that the includes provided transitively | 5 // This include must be here so that the includes provided transitively |
6 // by gl_surface_egl.h don't make it impossible to compile this code. | 6 // by gl_surface_egl.h don't make it impossible to compile this code. |
7 #include "third_party/mesa/src/include/GL/osmesa.h" | 7 #include "third_party/mesa/src/include/GL/osmesa.h" |
8 | 8 |
9 #include "ui/gl/gl_surface_egl.h" | 9 #include "ui/gl/gl_surface_egl.h" |
10 | 10 |
11 #if defined(OS_ANDROID) | 11 #if defined(OS_ANDROID) |
12 #include <android/native_window_jni.h> | 12 #include <android/native_window_jni.h> |
13 #include "base/android/sys_utils.h" | 13 #include "base/android/sys_utils.h" |
14 #endif | 14 #endif |
15 | 15 |
16 #include "base/command_line.h" | |
17 #include "base/debug/trace_event.h" | 16 #include "base/debug/trace_event.h" |
18 #include "base/logging.h" | 17 #include "base/logging.h" |
19 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
20 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
21 #include "build/build_config.h" | 20 #include "build/build_config.h" |
22 #include "ui/gl/egl_util.h" | 21 #include "ui/gl/egl_util.h" |
23 #include "ui/gl/gl_context.h" | 22 #include "ui/gl/gl_context.h" |
24 #include "ui/gl/gl_implementation.h" | 23 #include "ui/gl/gl_implementation.h" |
25 #include "ui/gl/gl_surface_osmesa.h" | 24 #include "ui/gl/gl_surface_osmesa.h" |
26 #include "ui/gl/gl_surface_stub.h" | 25 #include "ui/gl/gl_surface_stub.h" |
27 #include "ui/gl/gl_switches.h" | 26 #include "ui/gl/gl_switches.h" |
28 #include "ui/gl/scoped_make_current.h" | 27 #include "ui/gl/scoped_make_current.h" |
29 #include "ui/gl/sync_control_vsync_provider.h" | 28 #include "ui/gl/sync_control_vsync_provider.h" |
30 | 29 |
31 #if defined(USE_X11) | 30 #if defined(USE_X11) |
32 extern "C" { | 31 extern "C" { |
33 #include <X11/Xlib.h> | 32 #include <X11/Xlib.h> |
34 } | 33 } |
35 #endif | 34 #endif |
36 | 35 |
37 #if defined (USE_OZONE) | 36 #if defined (USE_OZONE) |
38 #include "ui/gfx/ozone/surface_factory_ozone.h" | 37 #include "ui/gfx/ozone/surface_factory_ozone.h" |
39 #endif | 38 #endif |
40 | 39 |
41 // From ANGLE's egl/eglext.h. | |
42 #if !defined(EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE) | |
43 #define EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE \ | |
44 reinterpret_cast<EGLNativeDisplayType>(-2) | |
45 #endif | |
46 #if !defined(EGL_FIXED_SIZE_ANGLE) | 40 #if !defined(EGL_FIXED_SIZE_ANGLE) |
47 #define EGL_FIXED_SIZE_ANGLE 0x3201 | 41 #define EGL_FIXED_SIZE_ANGLE 0x3201 |
48 #endif | 42 #endif |
49 | 43 |
50 using ui::GetLastEGLErrorString; | 44 using ui::GetLastEGLErrorString; |
51 | 45 |
52 namespace gfx { | 46 namespace gfx { |
53 | 47 |
54 namespace { | 48 namespace { |
55 | 49 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 114 |
121 } // namespace | 115 } // namespace |
122 | 116 |
123 GLSurfaceEGL::GLSurfaceEGL() {} | 117 GLSurfaceEGL::GLSurfaceEGL() {} |
124 | 118 |
125 bool GLSurfaceEGL::InitializeOneOff() { | 119 bool GLSurfaceEGL::InitializeOneOff() { |
126 static bool initialized = false; | 120 static bool initialized = false; |
127 if (initialized) | 121 if (initialized) |
128 return true; | 122 return true; |
129 | 123 |
130 #if defined(USE_X11) | 124 g_native_display = GetPlatformDefaultEGLNativeDisplay(); |
131 g_native_display = base::MessagePumpForUI::GetDefaultXDisplay(); | |
132 #elif defined(OS_WIN) | |
133 g_native_display = EGL_DEFAULT_DISPLAY; | |
134 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableD3D11)) { | |
135 g_native_display = EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE; | |
136 } | |
137 #elif defined(USE_OZONE) | |
138 gfx::SurfaceFactoryOzone* surface_factory = | |
139 gfx::SurfaceFactoryOzone::GetInstance(); | |
140 if (surface_factory->InitializeHardware() != | |
141 gfx::SurfaceFactoryOzone::INITIALIZED) { | |
142 LOG(ERROR) << "OZONE failed to initialize hardware"; | |
143 return false; | |
144 } | |
145 g_native_display = surface_factory->GetNativeDisplay(); | |
146 #else | |
147 g_native_display = EGL_DEFAULT_DISPLAY; | |
148 #endif | |
149 g_display = eglGetDisplay(g_native_display); | 125 g_display = eglGetDisplay(g_native_display); |
150 if (!g_display) { | 126 if (!g_display) { |
151 LOG(ERROR) << "eglGetDisplay failed with error " << GetLastEGLErrorString(); | 127 LOG(ERROR) << "eglGetDisplay failed with error " << GetLastEGLErrorString(); |
152 return false; | 128 return false; |
153 } | 129 } |
154 | 130 |
155 if (!eglInitialize(g_display, NULL, NULL)) { | 131 if (!eglInitialize(g_display, NULL, NULL)) { |
156 LOG(ERROR) << "eglInitialize failed with error " << GetLastEGLErrorString(); | 132 LOG(ERROR) << "eglInitialize failed with error " << GetLastEGLErrorString(); |
157 return false; | 133 return false; |
158 } | 134 } |
(...skipping 25 matching lines...) Expand all Loading... |
184 EGLint* choose_attributes = config_attribs_8888; | 160 EGLint* choose_attributes = config_attribs_8888; |
185 | 161 |
186 #if defined(OS_ANDROID) | 162 #if defined(OS_ANDROID) |
187 if (base::android::SysUtils::IsLowEndDevice()) { | 163 if (base::android::SysUtils::IsLowEndDevice()) { |
188 choose_attributes = config_attribs_565; | 164 choose_attributes = config_attribs_565; |
189 } | 165 } |
190 #endif | 166 #endif |
191 | 167 |
192 #if defined(USE_OZONE) | 168 #if defined(USE_OZONE) |
193 const EGLint* config_attribs = | 169 const EGLint* config_attribs = |
194 surface_factory->GetEGLSurfaceProperties(choose_attributes); | 170 SurfaceFactoryOzone::GetInstance()->GetEGLSurfaceProperties( |
| 171 choose_attributes); |
195 #else | 172 #else |
196 const EGLint* config_attribs = choose_attributes; | 173 const EGLint* config_attribs = choose_attributes; |
197 #endif | 174 #endif |
198 | 175 |
199 EGLint num_configs; | 176 EGLint num_configs; |
200 EGLint config_size = 1; | 177 EGLint config_size = 1; |
201 EGLConfig* config_data = &g_config; | 178 EGLConfig* config_data = &g_config; |
202 // Validate if there are any configs for given atrribs. | 179 // Validate if there are any configs for given atrribs. |
203 if (!ValidateEglConfig(g_display, | 180 if (!ValidateEglConfig(g_display, |
204 config_attribs, | 181 config_attribs, |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 } | 297 } |
321 | 298 |
322 bool GLSurfaceEGL::HasEGLExtension(const char* name) { | 299 bool GLSurfaceEGL::HasEGLExtension(const char* name) { |
323 return ExtensionsContain(GetEGLExtensions(), name); | 300 return ExtensionsContain(GetEGLExtensions(), name); |
324 } | 301 } |
325 | 302 |
326 bool GLSurfaceEGL::IsCreateContextRobustnessSupported() { | 303 bool GLSurfaceEGL::IsCreateContextRobustnessSupported() { |
327 return g_egl_create_context_robustness_supported; | 304 return g_egl_create_context_robustness_supported; |
328 } | 305 } |
329 | 306 |
| 307 bool GLSurfaceEGL::IsEGLSurfacelessContextSupported() { |
| 308 return g_egl_surfaceless_context_supported; |
| 309 } |
| 310 |
330 GLSurfaceEGL::~GLSurfaceEGL() {} | 311 GLSurfaceEGL::~GLSurfaceEGL() {} |
331 | 312 |
332 NativeViewGLSurfaceEGL::NativeViewGLSurfaceEGL(EGLNativeWindowType window) | 313 NativeViewGLSurfaceEGL::NativeViewGLSurfaceEGL(EGLNativeWindowType window) |
333 : window_(window), | 314 : window_(window), |
334 surface_(NULL), | 315 surface_(NULL), |
335 supports_post_sub_buffer_(false), | 316 supports_post_sub_buffer_(false), |
336 config_(NULL), | 317 config_(NULL), |
337 size_(1, 1) { | 318 size_(1, 1) { |
338 #if defined(OS_ANDROID) | 319 #if defined(OS_ANDROID) |
339 if (window) | 320 if (window) |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 return EGL_NO_SURFACE; | 729 return EGL_NO_SURFACE; |
749 } | 730 } |
750 | 731 |
751 void* SurfacelessEGL::GetShareHandle() { | 732 void* SurfacelessEGL::GetShareHandle() { |
752 return NULL; | 733 return NULL; |
753 } | 734 } |
754 | 735 |
755 SurfacelessEGL::~SurfacelessEGL() { | 736 SurfacelessEGL::~SurfacelessEGL() { |
756 } | 737 } |
757 | 738 |
758 #if defined(ANDROID) || defined(USE_OZONE) | |
759 | |
760 // A thin subclass of |GLSurfaceOSMesa| that can be used in place | |
761 // of a native hardware-provided surface when a native surface | |
762 // provider is not available. | |
763 class GLSurfaceOSMesaHeadless : public GLSurfaceOSMesa { | |
764 public: | |
765 explicit GLSurfaceOSMesaHeadless(); | |
766 | |
767 virtual bool IsOffscreen() OVERRIDE; | |
768 virtual bool SwapBuffers() OVERRIDE; | |
769 | |
770 protected: | |
771 virtual ~GLSurfaceOSMesaHeadless(); | |
772 | |
773 private: | |
774 | |
775 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOSMesaHeadless); | |
776 }; | |
777 | |
778 bool GLSurfaceOSMesaHeadless::IsOffscreen() { return false; } | |
779 | |
780 bool GLSurfaceOSMesaHeadless::SwapBuffers() { return true; } | |
781 | |
782 GLSurfaceOSMesaHeadless::GLSurfaceOSMesaHeadless() | |
783 : GLSurfaceOSMesa(OSMESA_BGRA, gfx::Size(1, 1)) {} | |
784 | |
785 GLSurfaceOSMesaHeadless::~GLSurfaceOSMesaHeadless() { Destroy(); } | |
786 | |
787 // static | |
788 bool GLSurface::InitializeOneOffInternal() { | |
789 switch (GetGLImplementation()) { | |
790 case kGLImplementationEGLGLES2: | |
791 if (!GLSurfaceEGL::InitializeOneOff()) { | |
792 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; | |
793 return false; | |
794 } | |
795 default: | |
796 break; | |
797 } | |
798 return true; | |
799 } | |
800 | |
801 // static | |
802 scoped_refptr<GLSurface> | |
803 GLSurface::CreateViewGLSurface(gfx::AcceleratedWidget window) { | |
804 | |
805 if (GetGLImplementation() == kGLImplementationOSMesaGL) { | |
806 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless()); | |
807 if (!surface->Initialize()) | |
808 return NULL; | |
809 return surface; | |
810 } | |
811 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2); | |
812 if (window != kNullAcceleratedWidget) { | |
813 EGLNativeWindowType egl_window; | |
814 scoped_refptr<NativeViewGLSurfaceEGL> surface; | |
815 scoped_ptr<VSyncProvider> sync_provider; | |
816 #if defined(USE_OZONE) | |
817 egl_window = | |
818 gfx::SurfaceFactoryOzone::GetInstance()->RealizeAcceleratedWidget( | |
819 window); | |
820 sync_provider = | |
821 gfx::SurfaceFactoryOzone::GetInstance()->CreateVSyncProvider( | |
822 egl_window); | |
823 #else | |
824 egl_window = window; | |
825 #endif | |
826 surface = new NativeViewGLSurfaceEGL(egl_window); | |
827 if(surface->Initialize(sync_provider.Pass())) | |
828 return surface; | |
829 } else { | |
830 scoped_refptr<GLSurface> surface = new GLSurfaceStub(); | |
831 if (surface->Initialize()) | |
832 return surface; | |
833 } | |
834 return NULL; | |
835 } | |
836 | |
837 // static | |
838 scoped_refptr<GLSurface> | |
839 GLSurface::CreateOffscreenGLSurface(const gfx::Size& size) { | |
840 switch (GetGLImplementation()) { | |
841 case kGLImplementationOSMesaGL: { | |
842 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesa(1, size)); | |
843 if (!surface->Initialize()) | |
844 return NULL; | |
845 | |
846 return surface; | |
847 } | |
848 case kGLImplementationEGLGLES2: { | |
849 scoped_refptr<GLSurface> surface; | |
850 if (g_egl_surfaceless_context_supported && | |
851 (size.width() == 0 && size.height() == 0)) { | |
852 surface = new SurfacelessEGL(size); | |
853 } else | |
854 surface = new PbufferGLSurfaceEGL(size); | |
855 | |
856 if (!surface->Initialize()) | |
857 return NULL; | |
858 return surface; | |
859 } | |
860 default: | |
861 NOTREACHED(); | |
862 return NULL; | |
863 } | |
864 } | |
865 | |
866 #endif | |
867 | |
868 } // namespace gfx | 739 } // namespace gfx |
OLD | NEW |