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