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