OLD | NEW |
(Empty) | |
| 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/ozone/platform/drm/gpu/gbm_surface.h" |
| 6 |
| 7 #include <gbm.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "ui/ozone/platform/drm/gpu/drm_buffer.h" |
| 12 #include "ui/ozone/platform/drm/gpu/drm_window.h" |
| 13 #include "ui/ozone/platform/drm/gpu/gbm_buffer_base.h" |
| 14 #include "ui/ozone/platform/drm/gpu/gbm_device.h" |
| 15 #include "ui/ozone/platform/drm/gpu/hardware_display_controller.h" |
| 16 #include "ui/ozone/platform/drm/gpu/scanout_buffer.h" |
| 17 |
| 18 namespace ui { |
| 19 |
| 20 namespace { |
| 21 |
| 22 void DoNothing(gfx::SwapResult) { |
| 23 } |
| 24 |
| 25 class GbmSurfaceBuffer : public GbmBufferBase { |
| 26 public: |
| 27 static scoped_refptr<GbmSurfaceBuffer> CreateBuffer( |
| 28 const scoped_refptr<DrmDevice>& drm, |
| 29 gbm_bo* buffer); |
| 30 static scoped_refptr<GbmSurfaceBuffer> GetBuffer(gbm_bo* buffer); |
| 31 |
| 32 private: |
| 33 GbmSurfaceBuffer(const scoped_refptr<DrmDevice>& drm, gbm_bo* bo); |
| 34 ~GbmSurfaceBuffer() override; |
| 35 |
| 36 static void Destroy(gbm_bo* buffer, void* data); |
| 37 |
| 38 // This buffer is special and is released by GBM at any point in time (as |
| 39 // long as it isn't being used). Since GBM should be the only one to |
| 40 // release this buffer, keep a self-reference in order to keep this alive. |
| 41 // When GBM calls Destroy(..) the self-reference will dissapear and this will |
| 42 // be destroyed. |
| 43 scoped_refptr<GbmSurfaceBuffer> self_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(GbmSurfaceBuffer); |
| 46 }; |
| 47 |
| 48 GbmSurfaceBuffer::GbmSurfaceBuffer(const scoped_refptr<DrmDevice>& drm, |
| 49 gbm_bo* bo) |
| 50 : GbmBufferBase(drm, bo, true) { |
| 51 if (GetFramebufferId()) { |
| 52 self_ = this; |
| 53 gbm_bo_set_user_data(bo, this, GbmSurfaceBuffer::Destroy); |
| 54 } |
| 55 } |
| 56 |
| 57 GbmSurfaceBuffer::~GbmSurfaceBuffer() { |
| 58 } |
| 59 |
| 60 // static |
| 61 scoped_refptr<GbmSurfaceBuffer> GbmSurfaceBuffer::CreateBuffer( |
| 62 const scoped_refptr<DrmDevice>& drm, |
| 63 gbm_bo* buffer) { |
| 64 scoped_refptr<GbmSurfaceBuffer> scoped_buffer( |
| 65 new GbmSurfaceBuffer(drm, buffer)); |
| 66 if (!scoped_buffer->GetFramebufferId()) |
| 67 return NULL; |
| 68 |
| 69 return scoped_buffer; |
| 70 } |
| 71 |
| 72 // static |
| 73 scoped_refptr<GbmSurfaceBuffer> GbmSurfaceBuffer::GetBuffer(gbm_bo* buffer) { |
| 74 return scoped_refptr<GbmSurfaceBuffer>( |
| 75 static_cast<GbmSurfaceBuffer*>(gbm_bo_get_user_data(buffer))); |
| 76 } |
| 77 |
| 78 // static |
| 79 void GbmSurfaceBuffer::Destroy(gbm_bo* buffer, void* data) { |
| 80 GbmSurfaceBuffer* scoped_buffer = static_cast<GbmSurfaceBuffer*>(data); |
| 81 scoped_buffer->self_ = NULL; |
| 82 } |
| 83 |
| 84 } // namespace |
| 85 |
| 86 GbmSurface::GbmSurface(DrmWindow* window_delegate, |
| 87 const scoped_refptr<GbmDevice>& gbm) |
| 88 : GbmSurfaceless(window_delegate, NULL), |
| 89 gbm_(gbm), |
| 90 native_surface_(NULL), |
| 91 current_buffer_(NULL), |
| 92 weak_factory_(this) { |
| 93 } |
| 94 |
| 95 GbmSurface::~GbmSurface() { |
| 96 if (current_buffer_) |
| 97 gbm_surface_release_buffer(native_surface_, current_buffer_); |
| 98 |
| 99 if (native_surface_) |
| 100 gbm_surface_destroy(native_surface_); |
| 101 } |
| 102 |
| 103 bool GbmSurface::Initialize() { |
| 104 // If we're initializing the surface without a controller (possible on startup |
| 105 // where the surface creation can happen before the native window delegate |
| 106 // IPCs arrive), initialize the size to a valid value such that surface |
| 107 // creation doesn't fail. |
| 108 gfx::Size size(1, 1); |
| 109 if (window_delegate_->GetController()) { |
| 110 size = window_delegate_->GetController()->GetModeSize(); |
| 111 } |
| 112 // TODO(dnicoara) Check underlying system support for pixel format. |
| 113 native_surface_ = gbm_surface_create( |
| 114 gbm_->device(), size.width(), size.height(), GBM_BO_FORMAT_XRGB8888, |
| 115 GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); |
| 116 |
| 117 if (!native_surface_) |
| 118 return false; |
| 119 |
| 120 size_ = size; |
| 121 return true; |
| 122 } |
| 123 |
| 124 intptr_t GbmSurface::GetNativeWindow() { |
| 125 DCHECK(native_surface_); |
| 126 return reinterpret_cast<intptr_t>(native_surface_); |
| 127 } |
| 128 |
| 129 bool GbmSurface::ResizeNativeWindow(const gfx::Size& viewport_size) { |
| 130 if (size_ == viewport_size) |
| 131 return true; |
| 132 |
| 133 return false; |
| 134 } |
| 135 |
| 136 bool GbmSurface::OnSwapBuffers() { |
| 137 return OnSwapBuffersAsync(base::Bind(&DoNothing)); |
| 138 } |
| 139 |
| 140 bool GbmSurface::OnSwapBuffersAsync(const SwapCompletionCallback& callback) { |
| 141 DCHECK(native_surface_); |
| 142 |
| 143 gbm_bo* pending_buffer = gbm_surface_lock_front_buffer(native_surface_); |
| 144 scoped_refptr<GbmSurfaceBuffer> primary = |
| 145 GbmSurfaceBuffer::GetBuffer(pending_buffer); |
| 146 if (!primary.get()) { |
| 147 primary = GbmSurfaceBuffer::CreateBuffer(gbm_, pending_buffer); |
| 148 if (!primary.get()) { |
| 149 LOG(ERROR) << "Failed to associate the buffer with the controller"; |
| 150 callback.Run(gfx::SwapResult::SWAP_FAILED); |
| 151 return false; |
| 152 } |
| 153 } |
| 154 |
| 155 // The primary buffer is a special case. |
| 156 window_delegate_->QueueOverlayPlane(OverlayPlane(primary)); |
| 157 |
| 158 if (!GbmSurfaceless::OnSwapBuffersAsync( |
| 159 base::Bind(&GbmSurface::OnSwapBuffersCallback, |
| 160 weak_factory_.GetWeakPtr(), callback, pending_buffer))) { |
| 161 callback.Run(gfx::SwapResult::SWAP_FAILED); |
| 162 return false; |
| 163 } |
| 164 |
| 165 return true; |
| 166 } |
| 167 |
| 168 void GbmSurface::OnSwapBuffersCallback(const SwapCompletionCallback& callback, |
| 169 gbm_bo* pending_buffer, |
| 170 gfx::SwapResult result) { |
| 171 // If there was a frontbuffer, it is no longer active. Release it back to GBM. |
| 172 if (current_buffer_) |
| 173 gbm_surface_release_buffer(native_surface_, current_buffer_); |
| 174 |
| 175 current_buffer_ = pending_buffer; |
| 176 callback.Run(result); |
| 177 } |
| 178 |
| 179 } // namespace ui |
OLD | NEW |