OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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/gfx/ozone/impl/software_surface_factory_ozone.h" |
| 6 |
| 7 #include <drm.h> |
| 8 #include <errno.h> |
| 9 #include <xf86drm.h> |
| 10 |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 #include "third_party/skia/include/core/SkDevice.h" |
| 14 #include "ui/gfx/native_widget_types.h" |
| 15 #include "ui/gfx/ozone/impl/drm_skbitmap_ozone.h" |
| 16 #include "ui/gfx/ozone/impl/drm_wrapper_ozone.h" |
| 17 #include "ui/gfx/ozone/impl/hardware_display_controller_ozone.h" |
| 18 #include "ui/gfx/ozone/impl/software_surface_ozone.h" |
| 19 |
| 20 namespace gfx { |
| 21 |
| 22 namespace { |
| 23 |
| 24 const char kDefaultGraphicsCardPath[] = "/dev/dri/card0"; |
| 25 |
| 26 const gfx::AcceleratedWidget kDefaultWidgetHandle = 1; |
| 27 |
| 28 // DRM callback on page flip events. This callback is triggered after the |
| 29 // page flip has happened and the backbuffer is now the new frontbuffer |
| 30 // The old frontbuffer is no longer used by the hardware and can be used for |
| 31 // future draw operations. |
| 32 // |
| 33 // |device| will contain a reference to the |SoftwareSurfaceOzone| object which |
| 34 // the event belongs to. |
| 35 // |
| 36 // TODO(dnicoara) When we have a FD handler for the DRM calls in the message |
| 37 // loop, we can move this function in the handler. |
| 38 void HandlePageFlipEvent(int fd, |
| 39 unsigned int frame, |
| 40 unsigned int seconds, |
| 41 unsigned int useconds, |
| 42 void* controller) { |
| 43 static_cast<HardwareDisplayControllerOzone*>(controller)->get_surface() |
| 44 ->SwapBuffers(); |
| 45 } |
| 46 |
| 47 uint32_t GetCrtc(int fd, drmModeRes* resources, drmModeConnector* connector) { |
| 48 // If the connector already has an encoder try to re-use. |
| 49 if (connector->encoder_id) { |
| 50 drmModeEncoder* encoder = drmModeGetEncoder(fd, connector->encoder_id); |
| 51 if (encoder) { |
| 52 if (encoder->crtc_id) { |
| 53 uint32_t crtc = encoder->crtc_id; |
| 54 drmModeFreeEncoder(encoder); |
| 55 return crtc; |
| 56 } |
| 57 drmModeFreeEncoder(encoder); |
| 58 } |
| 59 } |
| 60 |
| 61 // Try to find an encoder for the connector. |
| 62 for (int i = 0; i < connector->count_encoders; ++i) { |
| 63 drmModeEncoder* encoder = drmModeGetEncoder(fd, connector->encoders[i]); |
| 64 if (!encoder) |
| 65 continue; |
| 66 |
| 67 for (int j = 0; j < resources->count_crtcs; ++j) { |
| 68 // Check if the encoder is compatible with this CRTC |
| 69 if (!(encoder->possible_crtcs & (1 << j))) |
| 70 continue; |
| 71 |
| 72 drmModeFreeEncoder(encoder); |
| 73 return resources->crtcs[j]; |
| 74 } |
| 75 } |
| 76 |
| 77 return 0; |
| 78 } |
| 79 |
| 80 } // namespace |
| 81 |
| 82 SoftwareSurfaceFactoryOzone::SoftwareSurfaceFactoryOzone() |
| 83 : drm_(), |
| 84 state_(UNINITIALIZED), |
| 85 controller_() { |
| 86 } |
| 87 |
| 88 SoftwareSurfaceFactoryOzone::~SoftwareSurfaceFactoryOzone() { |
| 89 if (state_ == INITIALIZED) |
| 90 ShutdownHardware(); |
| 91 } |
| 92 |
| 93 SurfaceFactoryOzone::HardwareState |
| 94 SoftwareSurfaceFactoryOzone::InitializeHardware() { |
| 95 CHECK(state_ == UNINITIALIZED); |
| 96 |
| 97 // TODO(dnicoara): Short-cut right now. What we want is to look at all the |
| 98 // graphics devices available and select the primary one. |
| 99 drm_.reset(CreateWrapper()); |
| 100 if (drm_->get_fd() < 0) { |
| 101 LOG(ERROR) << "Cannot open graphics card '" |
| 102 << kDefaultGraphicsCardPath << "': " << strerror(errno); |
| 103 state_ = FAILED; |
| 104 return state_; |
| 105 } |
| 106 |
| 107 state_ = INITIALIZED; |
| 108 return state_; |
| 109 } |
| 110 |
| 111 void SoftwareSurfaceFactoryOzone::ShutdownHardware() { |
| 112 CHECK(state_ == INITIALIZED); |
| 113 |
| 114 controller_.reset(); |
| 115 drm_.reset(); |
| 116 |
| 117 state_ = UNINITIALIZED; |
| 118 } |
| 119 |
| 120 gfx::AcceleratedWidget SoftwareSurfaceFactoryOzone::GetAcceleratedWidget() { |
| 121 CHECK(state_ != FAILED); |
| 122 |
| 123 // TODO(dnicoara) When there's more information on which display we want, |
| 124 // then we can return the widget associated with the display. |
| 125 // For now just assume we have 1 display device and return it. |
| 126 if (!controller_.get()) |
| 127 controller_.reset(new HardwareDisplayControllerOzone()); |
| 128 |
| 129 // TODO(dnicoara) We only have 1 display for now, so only 1 AcceleratedWidget. |
| 130 // When we'll support multiple displays this needs to be changed to return a |
| 131 // different handle for every display. |
| 132 return kDefaultWidgetHandle; |
| 133 } |
| 134 |
| 135 gfx::AcceleratedWidget SoftwareSurfaceFactoryOzone::RealizeAcceleratedWidget( |
| 136 gfx::AcceleratedWidget w) { |
| 137 CHECK(state_ == INITIALIZED); |
| 138 // TODO(dnicoara) Once we can handle multiple displays this needs to be |
| 139 // changed. |
| 140 CHECK(w == kDefaultWidgetHandle); |
| 141 |
| 142 CHECK(controller_->get_state() == |
| 143 HardwareDisplayControllerOzone::UNASSOCIATED); |
| 144 |
| 145 // Until now the controller is just a stub. Initializing it will link it to a |
| 146 // hardware display. |
| 147 if (!InitializeControllerForPrimaryDisplay(drm_.get(), controller_.get())) { |
| 148 LOG(ERROR) << "Failed to initialize controller"; |
| 149 return gfx::kNullAcceleratedWidget; |
| 150 } |
| 151 |
| 152 // Create a surface suitable for the current controller. |
| 153 scoped_ptr<SoftwareSurfaceOzone> surface(CreateSurface(controller_.get())); |
| 154 |
| 155 if (!surface->Initialize()) { |
| 156 LOG(ERROR) << "Failed to initialize surface"; |
| 157 return gfx::kNullAcceleratedWidget; |
| 158 } |
| 159 |
| 160 // Bind the surface to the controller. This will register the backing buffers |
| 161 // with the hardware CRTC such that we can show the buffers. The controller |
| 162 // takes ownership of the surface. |
| 163 if (!controller_->BindSurfaceToController(surface.Pass())) { |
| 164 LOG(ERROR) << "Failed to bind surface to controller"; |
| 165 return gfx::kNullAcceleratedWidget; |
| 166 } |
| 167 |
| 168 return reinterpret_cast<gfx::AcceleratedWidget>( |
| 169 controller_->get_surface()->GetDrawableForWidget()); |
| 170 } |
| 171 |
| 172 bool SoftwareSurfaceFactoryOzone::LoadEGLGLES2Bindings() { |
| 173 return false; |
| 174 } |
| 175 |
| 176 bool SoftwareSurfaceFactoryOzone::AttemptToResizeAcceleratedWidget( |
| 177 gfx::AcceleratedWidget w, |
| 178 const gfx::Rect& bounds) { |
| 179 return false; |
| 180 } |
| 181 |
| 182 bool SoftwareSurfaceFactoryOzone::SchedulePageFlip(gfx::AcceleratedWidget w) { |
| 183 CHECK(state_ == INITIALIZED); |
| 184 // TODO(dnicoara) Change this CHECK once we're running with the threaded |
| 185 // compositor. |
| 186 CHECK(base::MessageLoop::current()->type() == base::MessageLoop::TYPE_UI); |
| 187 |
| 188 // TODO(dnicoara) Once we can handle multiple displays this needs to be |
| 189 // changed. |
| 190 CHECK(w == kDefaultWidgetHandle); |
| 191 |
| 192 if (!controller_->SchedulePageFlip()) |
| 193 return false; |
| 194 |
| 195 // Only wait for the page flip event to finish if it was properly scheduled. |
| 196 // |
| 197 // TODO(dnicoara) The following call will wait for the page flip event to |
| 198 // complete. This means that it will block until the next VSync. Ideally the |
| 199 // wait should happen in the message loop. The message loop would then |
| 200 // schedule the next draw event. Alternatively, the VSyncProvider could be |
| 201 // used to schedule the next draw. Unfortunately, at this point, |
| 202 // SoftwareOutputDevice does not provide any means to use any of the above |
| 203 // solutions. Note that if the DRM callback does not schedule the next draw, |
| 204 // then some sort of synchronization needs to take place since starting a new |
| 205 // draw before the page flip happened is considered an error. However we can |
| 206 // not use any lock constructs unless we're using the threaded compositor. |
| 207 // Note that the following call does not use any locks, so it is safe to be |
| 208 // made on the UI thread (thought not ideal). |
| 209 WaitForPageFlipEvent(drm_->get_fd()); |
| 210 |
| 211 return true; |
| 212 } |
| 213 |
| 214 gfx::VSyncProvider* SoftwareSurfaceFactoryOzone::GetVSyncProvider( |
| 215 gfx::AcceleratedWidget w) { |
| 216 return NULL; |
| 217 } |
| 218 |
| 219 //////////////////////////////////////////////////////////////////////////////// |
| 220 // SoftwareSurfaceFactoryOzone private |
| 221 |
| 222 SoftwareSurfaceOzone* SoftwareSurfaceFactoryOzone::CreateSurface( |
| 223 HardwareDisplayControllerOzone* controller) { |
| 224 return new SoftwareSurfaceOzone(controller); |
| 225 } |
| 226 |
| 227 DrmWrapperOzone* SoftwareSurfaceFactoryOzone::CreateWrapper() { |
| 228 return new DrmWrapperOzone(kDefaultGraphicsCardPath); |
| 229 } |
| 230 |
| 231 bool SoftwareSurfaceFactoryOzone::InitializeControllerForPrimaryDisplay( |
| 232 DrmWrapperOzone* drm, |
| 233 HardwareDisplayControllerOzone* controller) { |
| 234 CHECK(state_ == SurfaceFactoryOzone::INITIALIZED); |
| 235 |
| 236 drmModeRes* resources = drmModeGetResources(drm->get_fd()); |
| 237 |
| 238 // Search for an active connector. |
| 239 for (int i = 0; i < resources->count_connectors; ++i) { |
| 240 drmModeConnector* connector = drmModeGetConnector( |
| 241 drm->get_fd(), |
| 242 resources->connectors[i]); |
| 243 |
| 244 if (!connector) |
| 245 continue; |
| 246 |
| 247 if (connector->connection != DRM_MODE_CONNECTED || |
| 248 connector->count_modes == 0) { |
| 249 drmModeFreeConnector(connector); |
| 250 continue; |
| 251 } |
| 252 |
| 253 uint32_t crtc = GetCrtc(drm->get_fd(), resources, connector); |
| 254 |
| 255 if (!crtc) |
| 256 continue; |
| 257 |
| 258 // TODO(dnicoara) Select one mode for now. In the future we may need to |
| 259 // save all the modes and allow the user to choose a specific mode. Or |
| 260 // even some fullscreen applications may need to change the mode. |
| 261 controller->SetControllerInfo( |
| 262 drm, |
| 263 connector->connector_id, |
| 264 crtc, |
| 265 connector->modes[0]); |
| 266 |
| 267 drmModeFreeConnector(connector); |
| 268 |
| 269 return true; |
| 270 } |
| 271 |
| 272 return false; |
| 273 } |
| 274 |
| 275 void SoftwareSurfaceFactoryOzone::WaitForPageFlipEvent(int fd) { |
| 276 drmEventContext drm_event; |
| 277 drm_event.version = DRM_EVENT_CONTEXT_VERSION; |
| 278 drm_event.page_flip_handler = HandlePageFlipEvent; |
| 279 drm_event.vblank_handler = NULL; |
| 280 |
| 281 // Wait for the page-flip to complete. |
| 282 drmHandleEvent(fd, &drm_event); |
| 283 } |
| 284 |
| 285 } // namespace gfx |
OLD | NEW |