[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ui/gl/gl_surface.h" |
| 6 | |
avi | 739878c | 2015-12-24 18:06:17 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 9 | #include "base/bind.h" |
alexst | 165be97 | 2015-03-11 15:37:09 | [diff] [blame] | 10 | #include "base/callback.h" |
| 11 | #include "base/location.h" |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 12 | #include "base/logging.h" |
avi | 739878c | 2015-12-24 18:06:17 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
achaulk | 77f08ab | 2015-04-07 16:22:34 | [diff] [blame] | 15 | #include "base/memory/scoped_vector.h" |
alexst | 165be97 | 2015-03-11 15:37:09 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
| 17 | #include "base/threading/worker_pool.h" |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 18 | #include "ui/gfx/native_widget_types.h" |
kylechar | 53c91f2 | 2016-02-11 20:57:44 | [diff] [blame] | 19 | #include "ui/gl/egl_util.h" |
[email protected] | aab13e2f | 2014-07-29 03:01:45 | [diff] [blame] | 20 | #include "ui/gl/gl_context.h" |
[email protected] | f46c2bd | 2014-08-16 07:47:09 | [diff] [blame] | 21 | #include "ui/gl/gl_image.h" |
dongseong.hwang | 161c7a2 | 2015-07-29 18:28:17 | [diff] [blame] | 22 | #include "ui/gl/gl_image_ozone_native_pixmap.h" |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 23 | #include "ui/gl/gl_implementation.h" |
| 24 | #include "ui/gl/gl_surface_egl.h" |
| 25 | #include "ui/gl/gl_surface_osmesa.h" |
watk | e6e68b1e | 2015-12-01 04:04:03 | [diff] [blame] | 26 | #include "ui/gl/gl_surface_overlay.h" |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 27 | #include "ui/gl/gl_surface_stub.h" |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 28 | #include "ui/gl/scoped_binders.h" |
[email protected] | aab13e2f | 2014-07-29 03:01:45 | [diff] [blame] | 29 | #include "ui/gl/scoped_make_current.h" |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 30 | #include "ui/ozone/public/native_pixmap.h" |
dongseong.hwang | 9162b5fa | 2015-06-05 16:52:30 | [diff] [blame] | 31 | #include "ui/ozone/public/ozone_platform.h" |
[email protected] | 0e37fc20 | 2014-06-20 13:24:02 | [diff] [blame] | 32 | #include "ui/ozone/public/surface_factory_ozone.h" |
| 33 | #include "ui/ozone/public/surface_ozone_egl.h" |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 34 | |
tfarina | df856e02 | 2015-10-29 12:50:45 | [diff] [blame] | 35 | using gl::GLImage; |
| 36 | |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 37 | namespace gfx { |
| 38 | |
[email protected] | 502371d | 2014-03-31 17:16:42 | [diff] [blame] | 39 | namespace { |
| 40 | |
kylechar | 53c91f2 | 2016-02-11 20:57:44 | [diff] [blame] | 41 | // Helper function for base::Bind to create callback to eglChooseConfig. |
| 42 | bool EglChooseConfig(EGLDisplay display, |
| 43 | const int32_t* attribs, |
| 44 | EGLConfig* configs, |
| 45 | int32_t config_size, |
| 46 | int32_t* num_configs) { |
| 47 | return eglChooseConfig(display, attribs, configs, config_size, num_configs); |
| 48 | } |
| 49 | |
| 50 | // Helper function for base::Bind to create callback to eglGetConfigAttrib. |
| 51 | bool EglGetConfigAttribute(EGLDisplay display, |
| 52 | EGLConfig config, |
| 53 | int32_t attribute, |
| 54 | int32_t* value) { |
| 55 | return eglGetConfigAttrib(display, config, attribute, value); |
| 56 | } |
| 57 | |
kylechar | 445089ac | 2016-03-09 19:23:44 | [diff] [blame] | 58 | // Populates EglConfigCallbacks with appropriate callbacks. |
| 59 | ui::EglConfigCallbacks GetEglConfigCallbacks(EGLDisplay display) { |
| 60 | ui::EglConfigCallbacks callbacks; |
| 61 | callbacks.choose_config = base::Bind(EglChooseConfig, display); |
| 62 | callbacks.get_config_attribute = base::Bind(EglGetConfigAttribute, display); |
| 63 | callbacks.get_last_error_string = base::Bind(&ui::GetLastEGLErrorString); |
| 64 | return callbacks; |
| 65 | } |
| 66 | |
alexst | 165be97 | 2015-03-11 15:37:09 | [diff] [blame] | 67 | void WaitForFence(EGLDisplay display, EGLSyncKHR fence) { |
| 68 | eglClientWaitSyncKHR(display, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, |
| 69 | EGL_FOREVER_KHR); |
| 70 | } |
| 71 | |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 72 | // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow. |
[email protected] | 502371d | 2014-03-31 17:16:42 | [diff] [blame] | 73 | class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL { |
| 74 | public: |
[email protected] | aab13e2f | 2014-07-29 03:01:45 | [diff] [blame] | 75 | GLSurfaceOzoneEGL(scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface, |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 76 | AcceleratedWidget widget); |
[email protected] | 502371d | 2014-03-31 17:16:42 | [diff] [blame] | 77 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 78 | // GLSurface: |
jinsukkim | 2521961 | 2016-01-20 23:24:07 | [diff] [blame] | 79 | bool Initialize(gfx::GLSurface::Format format) override; |
jbauman | 1620587 | 2015-12-15 21:27:27 | [diff] [blame] | 80 | bool Resize(const gfx::Size& size, |
| 81 | float scale_factor, |
| 82 | bool has_alpha) override; |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 83 | gfx::SwapResult SwapBuffers() override; |
alexst | fa14b671 | 2015-01-05 19:29:48 | [diff] [blame] | 84 | bool ScheduleOverlayPlane(int z_order, |
| 85 | OverlayTransform transform, |
| 86 | GLImage* image, |
| 87 | const Rect& bounds_rect, |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 88 | const RectF& crop_rect) override; |
kylechar | 53c91f2 | 2016-02-11 20:57:44 | [diff] [blame] | 89 | EGLConfig GetConfig() override; |
[email protected] | 5780da09 | 2014-04-08 12:04:41 | [diff] [blame] | 90 | |
[email protected] | b8211e3 | 2014-04-01 22:58:45 | [diff] [blame] | 91 | private: |
achaulk | e79d5d85 | 2014-08-27 00:16:42 | [diff] [blame] | 92 | using NativeViewGLSurfaceEGL::Initialize; |
| 93 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 94 | ~GLSurfaceOzoneEGL() override; |
[email protected] | 502371d | 2014-03-31 17:16:42 | [diff] [blame] | 95 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 96 | bool ReinitializeNativeSurface(); |
[email protected] | aab13e2f | 2014-07-29 03:01:45 | [diff] [blame] | 97 | |
[email protected] | 502371d | 2014-03-31 17:16:42 | [diff] [blame] | 98 | // The native surface. Deleting this is allowed to free the EGLNativeWindow. |
[email protected] | 0e37fc20 | 2014-06-20 13:24:02 | [diff] [blame] | 99 | scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_; |
[email protected] | aab13e2f | 2014-07-29 03:01:45 | [diff] [blame] | 100 | AcceleratedWidget widget_; |
[email protected] | 502371d | 2014-03-31 17:16:42 | [diff] [blame] | 101 | |
| 102 | DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneEGL); |
| 103 | }; |
| 104 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 105 | GLSurfaceOzoneEGL::GLSurfaceOzoneEGL( |
| 106 | scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface, |
| 107 | AcceleratedWidget widget) |
| 108 | : NativeViewGLSurfaceEGL(ozone_surface->GetNativeWindow()), |
martina.kollarova | 66bdb2b | 2015-12-17 08:35:46 | [diff] [blame] | 109 | ozone_surface_(std::move(ozone_surface)), |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 110 | widget_(widget) {} |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 111 | |
jinsukkim | 2521961 | 2016-01-20 23:24:07 | [diff] [blame] | 112 | bool GLSurfaceOzoneEGL::Initialize(gfx::GLSurface::Format format) { |
| 113 | format_ = format; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 114 | return Initialize(ozone_surface_->CreateVSyncProvider()); |
| 115 | } |
| 116 | |
jbauman | 1620587 | 2015-12-15 21:27:27 | [diff] [blame] | 117 | bool GLSurfaceOzoneEGL::Resize(const gfx::Size& size, |
| 118 | float scale_factor, |
| 119 | bool has_alpha) { |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 120 | if (!ozone_surface_->ResizeNativeWindow(size)) { |
| 121 | if (!ReinitializeNativeSurface() || |
| 122 | !ozone_surface_->ResizeNativeWindow(size)) |
| 123 | return false; |
| 124 | } |
| 125 | |
jbauman | 1620587 | 2015-12-15 21:27:27 | [diff] [blame] | 126 | return NativeViewGLSurfaceEGL::Resize(size, scale_factor, has_alpha); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 127 | } |
| 128 | |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 129 | gfx::SwapResult GLSurfaceOzoneEGL::SwapBuffers() { |
| 130 | gfx::SwapResult result = NativeViewGLSurfaceEGL::SwapBuffers(); |
| 131 | if (result != gfx::SwapResult::SWAP_ACK) |
| 132 | return result; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 133 | |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 134 | return ozone_surface_->OnSwapBuffers() ? gfx::SwapResult::SWAP_ACK |
| 135 | : gfx::SwapResult::SWAP_FAILED; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | bool GLSurfaceOzoneEGL::ScheduleOverlayPlane(int z_order, |
| 139 | OverlayTransform transform, |
| 140 | GLImage* image, |
| 141 | const Rect& bounds_rect, |
| 142 | const RectF& crop_rect) { |
| 143 | return image->ScheduleOverlayPlane(widget_, z_order, transform, bounds_rect, |
| 144 | crop_rect); |
| 145 | } |
| 146 | |
kylechar | 53c91f2 | 2016-02-11 20:57:44 | [diff] [blame] | 147 | EGLConfig GLSurfaceOzoneEGL::GetConfig() { |
| 148 | if (!config_) { |
kylechar | 445089ac | 2016-03-09 19:23:44 | [diff] [blame] | 149 | ui::EglConfigCallbacks callbacks = GetEglConfigCallbacks(GetDisplay()); |
| 150 | config_ = ozone_surface_->GetEGLSurfaceConfig(callbacks); |
kylechar | 53c91f2 | 2016-02-11 20:57:44 | [diff] [blame] | 151 | } |
| 152 | if (config_) |
| 153 | return config_; |
| 154 | return NativeViewGLSurfaceEGL::GetConfig(); |
| 155 | } |
| 156 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 157 | GLSurfaceOzoneEGL::~GLSurfaceOzoneEGL() { |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 158 | Destroy(); // The EGL surface must be destroyed before SurfaceOzone. |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | bool GLSurfaceOzoneEGL::ReinitializeNativeSurface() { |
| 162 | scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current; |
| 163 | GLContext* current_context = GLContext::GetCurrent(); |
| 164 | bool was_current = current_context && current_context->IsCurrent(this); |
| 165 | if (was_current) { |
| 166 | scoped_make_current.reset(new ui::ScopedMakeCurrent(current_context, this)); |
| 167 | } |
| 168 | |
| 169 | Destroy(); |
dongseong.hwang | 9162b5fa | 2015-06-05 16:52:30 | [diff] [blame] | 170 | ozone_surface_ = ui::OzonePlatform::GetInstance() |
| 171 | ->GetSurfaceFactoryOzone() |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 172 | ->CreateEGLSurfaceForWidget(widget_); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 173 | if (!ozone_surface_) { |
| 174 | LOG(ERROR) << "Failed to create native surface."; |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | window_ = ozone_surface_->GetNativeWindow(); |
jinsukkim | 2521961 | 2016-01-20 23:24:07 | [diff] [blame] | 179 | if (!Initialize(format_)) { |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 180 | LOG(ERROR) << "Failed to initialize."; |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
achaulk | e79d5d85 | 2014-08-27 00:16:42 | [diff] [blame] | 187 | class GL_EXPORT GLSurfaceOzoneSurfaceless : public SurfacelessEGL { |
| 188 | public: |
| 189 | GLSurfaceOzoneSurfaceless(scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface, |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 190 | AcceleratedWidget widget); |
achaulk | e79d5d85 | 2014-08-27 00:16:42 | [diff] [blame] | 191 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 192 | // GLSurface: |
jinsukkim | 2521961 | 2016-01-20 23:24:07 | [diff] [blame] | 193 | bool Initialize(gfx::GLSurface::Format format) override; |
jbauman | 1620587 | 2015-12-15 21:27:27 | [diff] [blame] | 194 | bool Resize(const gfx::Size& size, |
| 195 | float scale_factor, |
| 196 | bool has_alpha) override; |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 197 | gfx::SwapResult SwapBuffers() override; |
alexst | fa14b671 | 2015-01-05 19:29:48 | [diff] [blame] | 198 | bool ScheduleOverlayPlane(int z_order, |
| 199 | OverlayTransform transform, |
| 200 | GLImage* image, |
| 201 | const Rect& bounds_rect, |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 202 | const RectF& crop_rect) override; |
| 203 | bool IsOffscreen() override; |
| 204 | VSyncProvider* GetVSyncProvider() override; |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 205 | bool SupportsAsyncSwap() override; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 206 | bool SupportsPostSubBuffer() override; |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 207 | gfx::SwapResult PostSubBuffer(int x, int y, int width, int height) override; |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 208 | void SwapBuffersAsync(const SwapCompletionCallback& callback) override; |
| 209 | void PostSubBufferAsync(int x, |
alexst | 409e3e0 | 2015-01-05 22:20:24 | [diff] [blame] | 210 | int y, |
| 211 | int width, |
| 212 | int height, |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 213 | const SwapCompletionCallback& callback) override; |
kylechar | 445089ac | 2016-03-09 19:23:44 | [diff] [blame] | 214 | EGLConfig GetConfig() override; |
achaulk | e79d5d85 | 2014-08-27 00:16:42 | [diff] [blame] | 215 | |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 216 | protected: |
achaulk | 77f08ab | 2015-04-07 16:22:34 | [diff] [blame] | 217 | struct PendingFrame { |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 218 | PendingFrame(); |
achaulk | 77f08ab | 2015-04-07 16:22:34 | [diff] [blame] | 219 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 220 | bool ScheduleOverlayPlanes(gfx::AcceleratedWidget widget); |
achaulk | 77f08ab | 2015-04-07 16:22:34 | [diff] [blame] | 221 | |
achaulk | 77f08ab | 2015-04-07 16:22:34 | [diff] [blame] | 222 | bool ready; |
watk | e6e68b1e | 2015-12-01 04:04:03 | [diff] [blame] | 223 | std::vector<GLSurfaceOverlay> overlays; |
achaulk | 77f08ab | 2015-04-07 16:22:34 | [diff] [blame] | 224 | SwapCompletionCallback callback; |
| 225 | }; |
| 226 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 227 | ~GLSurfaceOzoneSurfaceless() override; |
achaulk | e79d5d85 | 2014-08-27 00:16:42 | [diff] [blame] | 228 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 229 | void SubmitFrame(); |
alexst | 9c97321 | 2015-04-24 14:28:45 | [diff] [blame] | 230 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 231 | EGLSyncKHR InsertFence(); |
| 232 | void FenceRetired(EGLSyncKHR fence, PendingFrame* frame); |
alexst | 9c97321 | 2015-04-24 14:28:45 | [diff] [blame] | 233 | |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 234 | void SwapCompleted(const SwapCompletionCallback& callback, |
| 235 | gfx::SwapResult result); |
dbehr | 5addbd5 | 2015-02-07 03:18:05 | [diff] [blame] | 236 | |
achaulk | e79d5d85 | 2014-08-27 00:16:42 | [diff] [blame] | 237 | // The native surface. Deleting this is allowed to free the EGLNativeWindow. |
| 238 | scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_; |
| 239 | AcceleratedWidget widget_; |
| 240 | scoped_ptr<VSyncProvider> vsync_provider_; |
achaulk | 77f08ab | 2015-04-07 16:22:34 | [diff] [blame] | 241 | ScopedVector<PendingFrame> unsubmitted_frames_; |
dbehr | 5addbd5 | 2015-02-07 03:18:05 | [diff] [blame] | 242 | bool has_implicit_external_sync_; |
alexst | 165be97 | 2015-03-11 15:37:09 | [diff] [blame] | 243 | bool last_swap_buffers_result_; |
alexst | 9c97321 | 2015-04-24 14:28:45 | [diff] [blame] | 244 | bool swap_buffers_pending_; |
alexst | 165be97 | 2015-03-11 15:37:09 | [diff] [blame] | 245 | |
| 246 | base::WeakPtrFactory<GLSurfaceOzoneSurfaceless> weak_factory_; |
| 247 | |
kylechar | 53c91f2 | 2016-02-11 20:57:44 | [diff] [blame] | 248 | private: |
achaulk | e79d5d85 | 2014-08-27 00:16:42 | [diff] [blame] | 249 | DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfaceless); |
| 250 | }; |
| 251 | |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 252 | GLSurfaceOzoneSurfaceless::PendingFrame::PendingFrame() : ready(false) {} |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 253 | |
| 254 | bool GLSurfaceOzoneSurfaceless::PendingFrame::ScheduleOverlayPlanes( |
| 255 | gfx::AcceleratedWidget widget) { |
| 256 | for (const auto& overlay : overlays) |
| 257 | if (!overlay.ScheduleOverlayPlane(widget)) |
| 258 | return false; |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | GLSurfaceOzoneSurfaceless::GLSurfaceOzoneSurfaceless( |
| 263 | scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface, |
| 264 | AcceleratedWidget widget) |
| 265 | : SurfacelessEGL(gfx::Size()), |
martina.kollarova | 66bdb2b | 2015-12-17 08:35:46 | [diff] [blame] | 266 | ozone_surface_(std::move(ozone_surface)), |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 267 | widget_(widget), |
| 268 | has_implicit_external_sync_( |
| 269 | HasEGLExtension("EGL_ARM_implicit_external_sync")), |
| 270 | last_swap_buffers_result_(true), |
| 271 | swap_buffers_pending_(false), |
| 272 | weak_factory_(this) { |
| 273 | unsubmitted_frames_.push_back(new PendingFrame()); |
| 274 | } |
| 275 | |
jinsukkim | 2521961 | 2016-01-20 23:24:07 | [diff] [blame] | 276 | bool GLSurfaceOzoneSurfaceless::Initialize(gfx::GLSurface::Format format) { |
| 277 | if (!SurfacelessEGL::Initialize(format)) |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 278 | return false; |
| 279 | vsync_provider_ = ozone_surface_->CreateVSyncProvider(); |
| 280 | if (!vsync_provider_) |
| 281 | return false; |
| 282 | return true; |
| 283 | } |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 284 | |
jbauman | ce45e11 | 2015-10-30 02:33:57 | [diff] [blame] | 285 | bool GLSurfaceOzoneSurfaceless::Resize(const gfx::Size& size, |
jbauman | 1620587 | 2015-12-15 21:27:27 | [diff] [blame] | 286 | float scale_factor, |
| 287 | bool has_alpha) { |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 288 | if (!ozone_surface_->ResizeNativeWindow(size)) |
| 289 | return false; |
| 290 | |
jbauman | 1620587 | 2015-12-15 21:27:27 | [diff] [blame] | 291 | return SurfacelessEGL::Resize(size, scale_factor, has_alpha); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 292 | } |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 293 | |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 294 | gfx::SwapResult GLSurfaceOzoneSurfaceless::SwapBuffers() { |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 295 | glFlush(); |
| 296 | // TODO: the following should be replaced by a per surface flush as it gets |
| 297 | // implemented in GL drivers. |
| 298 | if (has_implicit_external_sync_) { |
| 299 | EGLSyncKHR fence = InsertFence(); |
| 300 | if (!fence) |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 301 | return SwapResult::SWAP_FAILED; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 302 | |
| 303 | EGLDisplay display = GetDisplay(); |
| 304 | WaitForFence(display, fence); |
| 305 | eglDestroySyncKHR(display, fence); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | unsubmitted_frames_.back()->ScheduleOverlayPlanes(widget_); |
| 309 | unsubmitted_frames_.back()->overlays.clear(); |
| 310 | |
hshi | 66cf41e2 | 2016-01-07 21:10:23 | [diff] [blame] | 311 | if (ozone_surface_->IsUniversalDisplayLinkDevice()) |
| 312 | glFinish(); |
| 313 | |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 314 | return ozone_surface_->OnSwapBuffers() ? gfx::SwapResult::SWAP_ACK |
| 315 | : gfx::SwapResult::SWAP_FAILED; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 316 | } |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 317 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 318 | bool GLSurfaceOzoneSurfaceless::ScheduleOverlayPlane(int z_order, |
| 319 | OverlayTransform transform, |
| 320 | GLImage* image, |
| 321 | const Rect& bounds_rect, |
| 322 | const RectF& crop_rect) { |
| 323 | unsubmitted_frames_.back()->overlays.push_back( |
watk | e6e68b1e | 2015-12-01 04:04:03 | [diff] [blame] | 324 | GLSurfaceOverlay(z_order, transform, image, bounds_rect, crop_rect)); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 325 | return true; |
| 326 | } |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 327 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 328 | bool GLSurfaceOzoneSurfaceless::IsOffscreen() { |
| 329 | return false; |
| 330 | } |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 331 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 332 | VSyncProvider* GLSurfaceOzoneSurfaceless::GetVSyncProvider() { |
| 333 | return vsync_provider_.get(); |
| 334 | } |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 335 | |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 336 | bool GLSurfaceOzoneSurfaceless::SupportsAsyncSwap() { |
| 337 | return true; |
| 338 | } |
| 339 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 340 | bool GLSurfaceOzoneSurfaceless::SupportsPostSubBuffer() { |
| 341 | return true; |
| 342 | } |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 343 | |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 344 | gfx::SwapResult GLSurfaceOzoneSurfaceless::PostSubBuffer(int x, |
| 345 | int y, |
| 346 | int width, |
| 347 | int height) { |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 348 | // The actual sub buffer handling is handled at higher layers. |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 349 | NOTREACHED(); |
| 350 | return gfx::SwapResult::SWAP_FAILED; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 351 | } |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 352 | |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 353 | void GLSurfaceOzoneSurfaceless::SwapBuffersAsync( |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 354 | const SwapCompletionCallback& callback) { |
| 355 | // If last swap failed, don't try to schedule new ones. |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 356 | if (!last_swap_buffers_result_) { |
| 357 | callback.Run(gfx::SwapResult::SWAP_FAILED); |
| 358 | return; |
| 359 | } |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 360 | |
| 361 | glFlush(); |
| 362 | |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 363 | SwapCompletionCallback surface_swap_callback = |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 364 | base::Bind(&GLSurfaceOzoneSurfaceless::SwapCompleted, |
| 365 | weak_factory_.GetWeakPtr(), callback); |
| 366 | |
| 367 | PendingFrame* frame = unsubmitted_frames_.back(); |
| 368 | frame->callback = surface_swap_callback; |
| 369 | unsubmitted_frames_.push_back(new PendingFrame()); |
| 370 | |
| 371 | // TODO: the following should be replaced by a per surface flush as it gets |
| 372 | // implemented in GL drivers. |
| 373 | if (has_implicit_external_sync_) { |
| 374 | EGLSyncKHR fence = InsertFence(); |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 375 | if (!fence) { |
| 376 | callback.Run(gfx::SwapResult::SWAP_FAILED); |
| 377 | return; |
| 378 | } |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 379 | |
| 380 | base::Closure fence_wait_task = |
| 381 | base::Bind(&WaitForFence, GetDisplay(), fence); |
| 382 | |
| 383 | base::Closure fence_retired_callback = |
| 384 | base::Bind(&GLSurfaceOzoneSurfaceless::FenceRetired, |
| 385 | weak_factory_.GetWeakPtr(), fence, frame); |
| 386 | |
| 387 | base::WorkerPool::PostTaskAndReply(FROM_HERE, fence_wait_task, |
| 388 | fence_retired_callback, false); |
spang | c63975d | 2015-11-24 16:08:11 | [diff] [blame] | 389 | return; // Defer frame submission until fence signals. |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | frame->ready = true; |
| 393 | SubmitFrame(); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 394 | } |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 395 | |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 396 | void GLSurfaceOzoneSurfaceless::PostSubBufferAsync( |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 397 | int x, |
| 398 | int y, |
| 399 | int width, |
| 400 | int height, |
| 401 | const SwapCompletionCallback& callback) { |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 402 | // The actual sub buffer handling is handled at higher layers. |
| 403 | SwapBuffersAsync(callback); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 404 | } |
| 405 | |
kylechar | 445089ac | 2016-03-09 19:23:44 | [diff] [blame] | 406 | EGLConfig GLSurfaceOzoneSurfaceless::GetConfig() { |
| 407 | if (!config_) { |
| 408 | ui::EglConfigCallbacks callbacks = GetEglConfigCallbacks(GetDisplay()); |
| 409 | config_ = ozone_surface_->GetEGLSurfaceConfig(callbacks); |
| 410 | } |
| 411 | if (config_) |
| 412 | return config_; |
| 413 | return SurfacelessEGL::GetConfig(); |
| 414 | } |
| 415 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 416 | GLSurfaceOzoneSurfaceless::~GLSurfaceOzoneSurfaceless() { |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 417 | Destroy(); // The EGL surface must be destroyed before SurfaceOzone. |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void GLSurfaceOzoneSurfaceless::SubmitFrame() { |
| 421 | DCHECK(!unsubmitted_frames_.empty()); |
| 422 | |
| 423 | if (unsubmitted_frames_.front()->ready && !swap_buffers_pending_) { |
| 424 | scoped_ptr<PendingFrame> frame(unsubmitted_frames_.front()); |
| 425 | unsubmitted_frames_.weak_erase(unsubmitted_frames_.begin()); |
| 426 | swap_buffers_pending_ = true; |
| 427 | |
dnicoara | 02681f4 | 2015-11-12 15:34:38 | [diff] [blame] | 428 | if (!frame->ScheduleOverlayPlanes(widget_)) { |
| 429 | // |callback| is a wrapper for SwapCompleted(). Call it to properly |
| 430 | // propagate the failed state. |
| 431 | frame->callback.Run(gfx::SwapResult::SWAP_FAILED); |
| 432 | return; |
| 433 | } |
| 434 | |
hshi | 66cf41e2 | 2016-01-07 21:10:23 | [diff] [blame] | 435 | if (ozone_surface_->IsUniversalDisplayLinkDevice()) |
| 436 | glFinish(); |
| 437 | |
dnicoara | 02681f4 | 2015-11-12 15:34:38 | [diff] [blame] | 438 | ozone_surface_->OnSwapBuffersAsync(frame->callback); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | EGLSyncKHR GLSurfaceOzoneSurfaceless::InsertFence() { |
| 443 | const EGLint attrib_list[] = {EGL_SYNC_CONDITION_KHR, |
| 444 | EGL_SYNC_PRIOR_COMMANDS_IMPLICIT_EXTERNAL_ARM, |
| 445 | EGL_NONE}; |
| 446 | return eglCreateSyncKHR(GetDisplay(), EGL_SYNC_FENCE_KHR, attrib_list); |
| 447 | } |
| 448 | |
| 449 | void GLSurfaceOzoneSurfaceless::FenceRetired(EGLSyncKHR fence, |
| 450 | PendingFrame* frame) { |
| 451 | eglDestroySyncKHR(GetDisplay(), fence); |
| 452 | frame->ready = true; |
| 453 | SubmitFrame(); |
| 454 | } |
| 455 | |
| 456 | void GLSurfaceOzoneSurfaceless::SwapCompleted( |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 457 | const SwapCompletionCallback& callback, |
| 458 | gfx::SwapResult result) { |
| 459 | callback.Run(result); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 460 | swap_buffers_pending_ = false; |
dnicoara | 02681f4 | 2015-11-12 15:34:38 | [diff] [blame] | 461 | if (result == gfx::SwapResult::SWAP_FAILED) { |
| 462 | last_swap_buffers_result_ = false; |
| 463 | return; |
| 464 | } |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 465 | |
| 466 | SubmitFrame(); |
| 467 | } |
| 468 | |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 469 | // This provides surface-like semantics implemented through surfaceless. |
| 470 | // A framebuffer is bound automatically. |
| 471 | class GL_EXPORT GLSurfaceOzoneSurfacelessSurfaceImpl |
| 472 | : public GLSurfaceOzoneSurfaceless { |
| 473 | public: |
| 474 | GLSurfaceOzoneSurfacelessSurfaceImpl( |
| 475 | scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface, |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 476 | AcceleratedWidget widget); |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 477 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 478 | // GLSurface: |
| 479 | unsigned int GetBackingFrameBufferObject() override; |
| 480 | bool OnMakeCurrent(GLContext* context) override; |
jbauman | 1620587 | 2015-12-15 21:27:27 | [diff] [blame] | 481 | bool Resize(const gfx::Size& size, |
| 482 | float scale_factor, |
| 483 | bool has_alpha) override; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 484 | bool SupportsPostSubBuffer() override; |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 485 | gfx::SwapResult SwapBuffers() override; |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 486 | void SwapBuffersAsync(const SwapCompletionCallback& callback) override; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 487 | void Destroy() override; |
spang | b55571ca | 2015-09-16 22:09:35 | [diff] [blame] | 488 | bool IsSurfaceless() const override; |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 489 | |
| 490 | private: |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 491 | ~GLSurfaceOzoneSurfacelessSurfaceImpl() override; |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 492 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 493 | void BindFramebuffer(); |
| 494 | bool CreatePixmaps(); |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 495 | |
achaulk | 8d645d29 | 2015-08-04 14:09:27 | [diff] [blame] | 496 | scoped_refptr<GLContext> context_; |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 497 | GLuint fbo_; |
| 498 | GLuint textures_[2]; |
| 499 | scoped_refptr<GLImage> images_[2]; |
| 500 | int current_surface_; |
| 501 | DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfacelessSurfaceImpl); |
| 502 | }; |
| 503 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 504 | GLSurfaceOzoneSurfacelessSurfaceImpl::GLSurfaceOzoneSurfacelessSurfaceImpl( |
| 505 | scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface, |
| 506 | AcceleratedWidget widget) |
martina.kollarova | 66bdb2b | 2015-12-17 08:35:46 | [diff] [blame] | 507 | : GLSurfaceOzoneSurfaceless(std::move(ozone_surface), widget), |
achaulk | 8d645d29 | 2015-08-04 14:09:27 | [diff] [blame] | 508 | context_(nullptr), |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 509 | fbo_(0), |
| 510 | current_surface_(0) { |
| 511 | for (auto& texture : textures_) |
| 512 | texture = 0; |
| 513 | } |
| 514 | |
| 515 | unsigned int |
| 516 | GLSurfaceOzoneSurfacelessSurfaceImpl::GetBackingFrameBufferObject() { |
| 517 | return fbo_; |
| 518 | } |
| 519 | |
| 520 | bool GLSurfaceOzoneSurfacelessSurfaceImpl::OnMakeCurrent(GLContext* context) { |
achaulk | 8d645d29 | 2015-08-04 14:09:27 | [diff] [blame] | 521 | DCHECK(!context_ || context == context_); |
| 522 | context_ = context; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 523 | if (!fbo_) { |
| 524 | glGenFramebuffersEXT(1, &fbo_); |
| 525 | if (!fbo_) |
| 526 | return false; |
| 527 | glGenTextures(arraysize(textures_), textures_); |
| 528 | if (!CreatePixmaps()) |
| 529 | return false; |
| 530 | } |
| 531 | BindFramebuffer(); |
| 532 | glBindFramebufferEXT(GL_FRAMEBUFFER, fbo_); |
| 533 | return SurfacelessEGL::OnMakeCurrent(context); |
| 534 | } |
| 535 | |
jbauman | ce45e11 | 2015-10-30 02:33:57 | [diff] [blame] | 536 | bool GLSurfaceOzoneSurfacelessSurfaceImpl::Resize(const gfx::Size& size, |
jbauman | 1620587 | 2015-12-15 21:27:27 | [diff] [blame] | 537 | float scale_factor, |
| 538 | bool has_alpha) { |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 539 | if (size == GetSize()) |
| 540 | return true; |
jbauman | 1620587 | 2015-12-15 21:27:27 | [diff] [blame] | 541 | // Alpha value isn't actually used in allocating buffers yet, so always use |
| 542 | // true instead. |
| 543 | return GLSurfaceOzoneSurfaceless::Resize(size, scale_factor, true) && |
jbauman | ce45e11 | 2015-10-30 02:33:57 | [diff] [blame] | 544 | CreatePixmaps(); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | bool GLSurfaceOzoneSurfacelessSurfaceImpl::SupportsPostSubBuffer() { |
| 548 | return false; |
| 549 | } |
| 550 | |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 551 | gfx::SwapResult GLSurfaceOzoneSurfacelessSurfaceImpl::SwapBuffers() { |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 552 | if (!images_[current_surface_]->ScheduleOverlayPlane( |
| 553 | widget_, 0, OverlayTransform::OVERLAY_TRANSFORM_NONE, |
| 554 | gfx::Rect(GetSize()), gfx::RectF(1, 1))) |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 555 | return gfx::SwapResult::SWAP_FAILED; |
| 556 | gfx::SwapResult result = GLSurfaceOzoneSurfaceless::SwapBuffers(); |
| 557 | if (result != gfx::SwapResult::SWAP_ACK) |
| 558 | return result; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 559 | current_surface_ ^= 1; |
| 560 | BindFramebuffer(); |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 561 | return gfx::SwapResult::SWAP_ACK; |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 562 | } |
| 563 | |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 564 | void GLSurfaceOzoneSurfacelessSurfaceImpl::SwapBuffersAsync( |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 565 | const SwapCompletionCallback& callback) { |
| 566 | if (!images_[current_surface_]->ScheduleOverlayPlane( |
| 567 | widget_, 0, OverlayTransform::OVERLAY_TRANSFORM_NONE, |
spang | f960a950 | 2015-11-18 21:04:26 | [diff] [blame] | 568 | gfx::Rect(GetSize()), gfx::RectF(1, 1))) { |
| 569 | callback.Run(gfx::SwapResult::SWAP_FAILED); |
| 570 | return; |
| 571 | } |
| 572 | GLSurfaceOzoneSurfaceless::SwapBuffersAsync(callback); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 573 | current_surface_ ^= 1; |
| 574 | BindFramebuffer(); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | void GLSurfaceOzoneSurfacelessSurfaceImpl::Destroy() { |
achaulk | 8d645d29 | 2015-08-04 14:09:27 | [diff] [blame] | 578 | if (!context_) |
| 579 | return; |
wuchengli | 20824f95 | 2015-10-22 05:05:27 | [diff] [blame] | 580 | scoped_refptr<gfx::GLContext> previous_context = gfx::GLContext::GetCurrent(); |
| 581 | scoped_refptr<gfx::GLSurface> previous_surface = gfx::GLSurface::GetCurrent(); |
| 582 | context_->MakeCurrent(this); |
| 583 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 584 | glBindFramebufferEXT(GL_FRAMEBUFFER, 0); |
| 585 | if (fbo_) { |
| 586 | glDeleteTextures(arraysize(textures_), textures_); |
| 587 | for (auto& texture : textures_) |
| 588 | texture = 0; |
| 589 | glDeleteFramebuffersEXT(1, &fbo_); |
| 590 | fbo_ = 0; |
| 591 | } |
| 592 | for (auto image : images_) { |
| 593 | if (image) |
| 594 | image->Destroy(true); |
| 595 | } |
wuchengli | 20824f95 | 2015-10-22 05:05:27 | [diff] [blame] | 596 | |
| 597 | if (previous_context.get()) { |
| 598 | previous_context->MakeCurrent(previous_surface.get()); |
| 599 | } else { |
| 600 | context_->ReleaseCurrent(this); |
| 601 | } |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 602 | } |
| 603 | |
spang | b55571ca | 2015-09-16 22:09:35 | [diff] [blame] | 604 | bool GLSurfaceOzoneSurfacelessSurfaceImpl::IsSurfaceless() const { |
| 605 | return false; |
| 606 | } |
| 607 | |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 608 | GLSurfaceOzoneSurfacelessSurfaceImpl::~GLSurfaceOzoneSurfacelessSurfaceImpl() { |
wuchengli | 20824f95 | 2015-10-22 05:05:27 | [diff] [blame] | 609 | Destroy(); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | void GLSurfaceOzoneSurfacelessSurfaceImpl::BindFramebuffer() { |
| 613 | ScopedFrameBufferBinder fb(fbo_); |
| 614 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 615 | textures_[current_surface_], 0); |
| 616 | } |
| 617 | |
| 618 | bool GLSurfaceOzoneSurfacelessSurfaceImpl::CreatePixmaps() { |
| 619 | if (!fbo_) |
| 620 | return true; |
| 621 | for (size_t i = 0; i < arraysize(textures_); i++) { |
| 622 | scoped_refptr<ui::NativePixmap> pixmap = |
dongseong.hwang | 9162b5fa | 2015-06-05 16:52:30 | [diff] [blame] | 623 | ui::OzonePlatform::GetInstance() |
| 624 | ->GetSurfaceFactoryOzone() |
| 625 | ->CreateNativePixmap(widget_, GetSize(), |
spang | fcf5fab4 | 2015-08-04 19:25:35 | [diff] [blame] | 626 | gfx::BufferFormat::BGRA_8888, |
reveman | 1108e0b7 | 2015-10-30 17:05:44 | [diff] [blame] | 627 | gfx::BufferUsage::SCANOUT); |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 628 | if (!pixmap) |
| 629 | return false; |
dongseong.hwang | 161c7a2 | 2015-07-29 18:28:17 | [diff] [blame] | 630 | scoped_refptr<GLImageOzoneNativePixmap> image = |
| 631 | new GLImageOzoneNativePixmap(GetSize(), GL_BGRA_EXT); |
reveman | 0bb50d7 | 2015-11-16 18:04:52 | [diff] [blame] | 632 | if (!image->Initialize(pixmap.get(), gfx::BufferFormat::BGRA_8888)) |
alexst | e3e4605 | 2015-05-04 19:40:56 | [diff] [blame] | 633 | return false; |
| 634 | images_[i] = image; |
| 635 | // Bind image to texture. |
| 636 | ScopedTextureBinder binder(GL_TEXTURE_2D, textures_[i]); |
| 637 | if (!images_[i]->BindTexImage(GL_TEXTURE_2D)) |
| 638 | return false; |
| 639 | } |
| 640 | return true; |
| 641 | } |
| 642 | |
spang | e82006b | 2015-09-16 18:07:29 | [diff] [blame] | 643 | scoped_refptr<GLSurface> CreateViewGLSurfaceOzone( |
| 644 | gfx::AcceleratedWidget window) { |
| 645 | scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = |
| 646 | ui::OzonePlatform::GetInstance() |
| 647 | ->GetSurfaceFactoryOzone() |
| 648 | ->CreateEGLSurfaceForWidget(window); |
| 649 | if (!surface_ozone) |
| 650 | return nullptr; |
| 651 | scoped_refptr<GLSurface> surface = |
martina.kollarova | 66bdb2b | 2015-12-17 08:35:46 | [diff] [blame] | 652 | new GLSurfaceOzoneEGL(std::move(surface_ozone), window); |
spang | e82006b | 2015-09-16 18:07:29 | [diff] [blame] | 653 | if (!surface->Initialize()) |
| 654 | return nullptr; |
| 655 | return surface; |
| 656 | } |
| 657 | |
| 658 | scoped_refptr<GLSurface> CreateViewGLSurfaceOzoneSurfacelessSurfaceImpl( |
| 659 | gfx::AcceleratedWidget window) { |
| 660 | scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = |
| 661 | ui::OzonePlatform::GetInstance() |
| 662 | ->GetSurfaceFactoryOzone() |
| 663 | ->CreateSurfacelessEGLSurfaceForWidget(window); |
| 664 | if (!surface_ozone) |
| 665 | return nullptr; |
martina.kollarova | 66bdb2b | 2015-12-17 08:35:46 | [diff] [blame] | 666 | scoped_refptr<GLSurface> surface = new GLSurfaceOzoneSurfacelessSurfaceImpl( |
| 667 | std::move(surface_ozone), window); |
spang | e82006b | 2015-09-16 18:07:29 | [diff] [blame] | 668 | if (!surface->Initialize()) |
| 669 | return nullptr; |
| 670 | return surface; |
| 671 | } |
| 672 | |
[email protected] | 502371d | 2014-03-31 17:16:42 | [diff] [blame] | 673 | } // namespace |
| 674 | |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 675 | // static |
| 676 | bool GLSurface::InitializeOneOffInternal() { |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 677 | switch (GetGLImplementation()) { |
| 678 | case kGLImplementationEGLGLES2: |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 679 | if (!GLSurfaceEGL::InitializeOneOff()) { |
| 680 | LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; |
| 681 | return false; |
| 682 | } |
[email protected] | 1fe75057 | 2014-03-29 01:22:34 | [diff] [blame] | 683 | |
| 684 | return true; |
| 685 | case kGLImplementationOSMesaGL: |
| 686 | case kGLImplementationMockGL: |
| 687 | return true; |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 688 | default: |
[email protected] | 1fe75057 | 2014-03-29 01:22:34 | [diff] [blame] | 689 | return false; |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 690 | } |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | // static |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 694 | scoped_refptr<GLSurface> GLSurface::CreateSurfacelessViewGLSurface( |
| 695 | gfx::AcceleratedWidget window) { |
| 696 | if (GetGLImplementation() == kGLImplementationEGLGLES2 && |
| 697 | window != kNullAcceleratedWidget && |
spang | e82006b | 2015-09-16 18:07:29 | [diff] [blame] | 698 | GLSurfaceEGL::IsEGLSurfacelessContextSupported()) { |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 699 | scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = |
dongseong.hwang | 9162b5fa | 2015-06-05 16:52:30 | [diff] [blame] | 700 | ui::OzonePlatform::GetInstance() |
| 701 | ->GetSurfaceFactoryOzone() |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 702 | ->CreateSurfacelessEGLSurfaceForWidget(window); |
| 703 | if (!surface_ozone) |
| 704 | return nullptr; |
| 705 | scoped_refptr<GLSurface> surface; |
martina.kollarova | 66bdb2b | 2015-12-17 08:35:46 | [diff] [blame] | 706 | surface = new GLSurfaceOzoneSurfaceless(std::move(surface_ozone), window); |
achaulk | e86e411c | 2015-03-02 22:45:36 | [diff] [blame] | 707 | if (surface->Initialize()) |
| 708 | return surface; |
| 709 | } |
| 710 | |
| 711 | return nullptr; |
| 712 | } |
| 713 | |
| 714 | // static |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 715 | scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface( |
| 716 | gfx::AcceleratedWidget window) { |
| 717 | if (GetGLImplementation() == kGLImplementationOSMesaGL) { |
| 718 | scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless()); |
| 719 | if (!surface->Initialize()) |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 720 | return nullptr; |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 721 | return surface; |
| 722 | } |
| 723 | DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2); |
| 724 | if (window != kNullAcceleratedWidget) { |
achaulk | e79d5d85 | 2014-08-27 00:16:42 | [diff] [blame] | 725 | scoped_refptr<GLSurface> surface; |
spang | e82006b | 2015-09-16 18:07:29 | [diff] [blame] | 726 | if (GLSurfaceEGL::IsEGLSurfacelessContextSupported()) |
| 727 | surface = CreateViewGLSurfaceOzoneSurfacelessSurfaceImpl(window); |
| 728 | if (!surface) |
| 729 | surface = CreateViewGLSurfaceOzone(window); |
[email protected] | 502371d | 2014-03-31 17:16:42 | [diff] [blame] | 730 | return surface; |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 731 | } else { |
| 732 | scoped_refptr<GLSurface> surface = new GLSurfaceStub(); |
| 733 | if (surface->Initialize()) |
| 734 | return surface; |
| 735 | } |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 736 | return nullptr; |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // static |
| 740 | scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface( |
jinsukkim | 556e80df | 2016-02-24 01:01:32 | [diff] [blame] | 741 | const gfx::Size& size) { |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 742 | switch (GetGLImplementation()) { |
| 743 | case kGLImplementationOSMesaGL: { |
[email protected] | dab8662 | 2014-08-16 14:34:59 | [diff] [blame] | 744 | scoped_refptr<GLSurface> surface( |
jinsukkim | 556e80df | 2016-02-24 01:01:32 | [diff] [blame] | 745 | new GLSurfaceOSMesa(SURFACE_OSMESA_BGRA, size)); |
| 746 | if (!surface->Initialize()) |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 747 | return nullptr; |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 748 | |
| 749 | return surface; |
| 750 | } |
| 751 | case kGLImplementationEGLGLES2: { |
| 752 | scoped_refptr<GLSurface> surface; |
| 753 | if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() && |
| 754 | (size.width() == 0 && size.height() == 0)) { |
| 755 | surface = new SurfacelessEGL(size); |
kylechar | 53c91f2 | 2016-02-11 20:57:44 | [diff] [blame] | 756 | } else { |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 757 | surface = new PbufferGLSurfaceEGL(size); |
kylechar | 53c91f2 | 2016-02-11 20:57:44 | [diff] [blame] | 758 | } |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 759 | |
jinsukkim | 556e80df | 2016-02-24 01:01:32 | [diff] [blame] | 760 | if (!surface->Initialize()) |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 761 | return nullptr; |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 762 | return surface; |
| 763 | } |
zmo | eecacfe | 2015-05-07 05:42:53 | [diff] [blame] | 764 | case kGLImplementationMockGL: |
| 765 | return new GLSurfaceStub; |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 766 | default: |
| 767 | NOTREACHED(); |
spang | 0ac9fc3 | 2015-09-16 23:11:03 | [diff] [blame] | 768 | return nullptr; |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
| 772 | EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() { |
dongseong.hwang | 9162b5fa | 2015-06-05 16:52:30 | [diff] [blame] | 773 | return ui::OzonePlatform::GetInstance() |
| 774 | ->GetSurfaceFactoryOzone() |
| 775 | ->GetNativeDisplay(); |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | } // namespace gfx |