OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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/dri/gbm_surface.h" |
| 6 |
| 7 #include <gbm.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "third_party/skia/include/core/SkImageInfo.h" |
| 11 #include "ui/ozone/platform/dri/dri_buffer.h" |
| 12 #include "ui/ozone/platform/dri/dri_wrapper.h" |
| 13 #include "ui/ozone/platform/dri/gbm_surface.h" |
| 14 #include "ui/ozone/platform/dri/hardware_display_controller.h" |
| 15 |
| 16 namespace ui { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Pixel configuration for the current buffer format. |
| 21 // TODO(dnicoara) These will need to change once we'll query the hardware for |
| 22 // supported configurations. |
| 23 const uint8_t kColorDepth = 24; |
| 24 const uint8_t kPixelDepth = 32; |
| 25 |
| 26 class BufferData { |
| 27 public: |
| 28 // When we create the BufferData we need to register the buffer. Once |
| 29 // successfully registered, the |framebuffer_| field will hold the ID of the |
| 30 // buffer. The controller will use this ID when scanning out the buffer. On |
| 31 // creation we will also associate the BufferData with the buffer. |
| 32 static BufferData* CreateData(DriWrapper* dri, gbm_bo* buffer); |
| 33 |
| 34 // Callback used by GBM to destory the BufferData associated with a buffer. |
| 35 static void Destroy(gbm_bo* buffer, void* data); |
| 36 |
| 37 // Returns the BufferData associated with |buffer|. NULL if no data is |
| 38 // associated. |
| 39 static BufferData* GetData(gbm_bo* buffer); |
| 40 |
| 41 uint32_t framebuffer() const { return framebuffer_; } |
| 42 uint32_t handle() const { return handle_; } |
| 43 |
| 44 private: |
| 45 BufferData(DriWrapper* dri, gbm_bo* buffer); |
| 46 ~BufferData(); |
| 47 |
| 48 DriWrapper* dri_; |
| 49 |
| 50 uint32_t handle_; |
| 51 |
| 52 // ID provided by the controller when the buffer is registered. This ID is |
| 53 // used when scanning out the buffer. |
| 54 uint32_t framebuffer_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(BufferData); |
| 57 }; |
| 58 |
| 59 BufferData::BufferData(DriWrapper* dri, gbm_bo* buffer) |
| 60 : dri_(dri), |
| 61 handle_(gbm_bo_get_handle(buffer).u32), |
| 62 framebuffer_(0) { |
| 63 // Register the buffer with the controller. This will allow us to scan out the |
| 64 // buffer once we're done drawing into it. If we can't register the buffer |
| 65 // then there's no point in having BufferData associated with it. |
| 66 if (!dri_->AddFramebuffer(gbm_bo_get_width(buffer), |
| 67 gbm_bo_get_height(buffer), |
| 68 kColorDepth, |
| 69 kPixelDepth, |
| 70 gbm_bo_get_stride(buffer), |
| 71 handle_, |
| 72 &framebuffer_)) { |
| 73 LOG(ERROR) << "Failed to register buffer"; |
| 74 } |
| 75 } |
| 76 |
| 77 BufferData::~BufferData() { |
| 78 if (framebuffer_) |
| 79 dri_->RemoveFramebuffer(framebuffer_); |
| 80 } |
| 81 |
| 82 // static |
| 83 BufferData* BufferData::CreateData(DriWrapper* dri, |
| 84 gbm_bo* buffer) { |
| 85 BufferData* data = new BufferData(dri, buffer); |
| 86 if (!data->framebuffer()) { |
| 87 delete data; |
| 88 return NULL; |
| 89 } |
| 90 |
| 91 // GBM can destroy the buffers at any time as long as they aren't locked. This |
| 92 // sets a callback such that we can clean up all our state when GBM destroys |
| 93 // the buffer. |
| 94 gbm_bo_set_user_data(buffer, data, BufferData::Destroy); |
| 95 |
| 96 return data; |
| 97 } |
| 98 |
| 99 // static |
| 100 void BufferData::Destroy(gbm_bo* buffer, void* data) { |
| 101 BufferData* bd = static_cast<BufferData*>(data); |
| 102 delete bd; |
| 103 } |
| 104 |
| 105 // static |
| 106 BufferData* BufferData::GetData(gbm_bo* buffer) { |
| 107 return static_cast<BufferData*>(gbm_bo_get_user_data(buffer)); |
| 108 } |
| 109 |
| 110 } // namespace |
| 111 |
| 112 GbmSurface::GbmSurface(gbm_device* device, |
| 113 DriWrapper* dri, |
| 114 const gfx::Size& size) |
| 115 : gbm_device_(device), |
| 116 dri_(dri), |
| 117 size_(size), |
| 118 native_surface_(NULL), |
| 119 buffers_(), |
| 120 front_buffer_(0) { |
| 121 for (size_t i = 0; i < arraysize(buffers_); ++i) |
| 122 buffers_[i] = NULL; |
| 123 } |
| 124 |
| 125 GbmSurface::~GbmSurface() { |
| 126 for (size_t i = 0; i < arraysize(buffers_); ++i) { |
| 127 if (buffers_[i]) { |
| 128 gbm_surface_release_buffer(native_surface_, buffers_[i]); |
| 129 } |
| 130 } |
| 131 |
| 132 if (native_surface_) |
| 133 gbm_surface_destroy(native_surface_); |
| 134 } |
| 135 |
| 136 bool GbmSurface::Initialize() { |
| 137 // TODO(dnicoara) Check underlying system support for pixel format. |
| 138 native_surface_ = gbm_surface_create( |
| 139 gbm_device_, |
| 140 size_.width(), |
| 141 size_.height(), |
| 142 GBM_BO_FORMAT_XRGB8888, |
| 143 GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); |
| 144 |
| 145 if (!native_surface_) |
| 146 return false; |
| 147 |
| 148 dumb_buffer_.reset(new DriBuffer(dri_)); |
| 149 if (!dumb_buffer_->Initialize(SkImageInfo::MakeN32Premul(size_.width(), |
| 150 size_.height()))) |
| 151 return false; |
| 152 |
| 153 return true; |
| 154 } |
| 155 |
| 156 uint32_t GbmSurface::GetFramebufferId() const { |
| 157 if (!buffers_[front_buffer_ ^ 1]) |
| 158 return dumb_buffer_->framebuffer(); |
| 159 |
| 160 BufferData* data = BufferData::GetData(buffers_[front_buffer_ ^ 1]); |
| 161 CHECK(data); |
| 162 return data->framebuffer(); |
| 163 } |
| 164 |
| 165 uint32_t GbmSurface::GetHandle() const { |
| 166 if (!buffers_[front_buffer_ ^ 1]) |
| 167 return dumb_buffer_->handle(); |
| 168 |
| 169 BufferData* data = BufferData::GetData(buffers_[front_buffer_ ^ 1]); |
| 170 CHECK(data); |
| 171 return data->handle(); |
| 172 } |
| 173 |
| 174 gfx::Size GbmSurface::Size() const { |
| 175 return size_; |
| 176 } |
| 177 |
| 178 void GbmSurface::SwapBuffers() { |
| 179 // If there was a frontbuffer, is no longer active. Release it back to GBM. |
| 180 if (buffers_[front_buffer_]) |
| 181 gbm_surface_release_buffer(native_surface_, buffers_[front_buffer_]); |
| 182 |
| 183 // Update the index to the frontbuffer. |
| 184 front_buffer_ ^= 1; |
| 185 // We've just released it. Since GBM doesn't guarantee we'll get the same |
| 186 // buffer back, we set it to NULL so we don't keep track of objects that may |
| 187 // have been destroyed. |
| 188 buffers_[front_buffer_ ^ 1] = NULL; |
| 189 } |
| 190 |
| 191 void GbmSurface::LockCurrentDrawable() { |
| 192 CHECK(native_surface_); |
| 193 // Lock the buffer we want to display. |
| 194 buffers_[front_buffer_ ^ 1] = gbm_surface_lock_front_buffer(native_surface_); |
| 195 |
| 196 BufferData* data = BufferData::GetData(buffers_[front_buffer_ ^ 1]); |
| 197 // If it is a new buffer, it won't have any data associated with it. So we |
| 198 // create it. On creation it will associate itself with the buffer and |
| 199 // register the buffer. |
| 200 if (!data) { |
| 201 data = BufferData::CreateData(dri_, buffers_[front_buffer_ ^ 1]); |
| 202 DCHECK(data) << "Failed to associate the buffer with the controller"; |
| 203 } |
| 204 } |
| 205 |
| 206 } // namespace ui |
OLD | NEW |