Chromium Code Reviews| 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 SoftwareSurfaceOzone* surface = CreateSurface(controller_.get()); | |
|
rjkroege
2013/10/16 19:03:36
who tracks ownership of surface? it would seem to
dnicoara
2013/10/16 20:27:29
You are right, there is one case where it would le
| |
| 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. | |
| 162 if (!controller_->BindSurfaceToController(surface)) { | |
| 163 delete surface; | |
| 164 LOG(ERROR) << "Failed to bind surface to controller"; | |
| 165 return gfx::kNullAcceleratedWidget; | |
| 166 } | |
| 167 | |
| 168 return reinterpret_cast<gfx::AcceleratedWidget>( | |
| 169 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()) | |
|
rjkroege
2013/10/16 19:03:36
The comments imply that we must always return fals
dnicoara
2013/10/16 20:27:29
I have updated the comments. Hopefully they are cl
| |
| 193 return false; | |
| 194 | |
| 195 // TODO(dnicoara) This requires the threaded compositor. | |
| 196 // This should happen in the message loop such that we don't | |
| 197 // block. However we should be able to signal the drawing thread when the page | |
| 198 // flip finished such that it can start a new frame. This would typically be | |
| 199 // done using the VSyncProvider, however at this point the | |
| 200 // SoftwareOutputDevice doesn't provide a way to use a custom VSyncProvider to | |
| 201 // update its timer or state. | |
| 202 WaitForPageFlipEvent(drm_->get_fd()); | |
| 203 | |
| 204 return true; | |
| 205 } | |
| 206 | |
| 207 gfx::VSyncProvider* SoftwareSurfaceFactoryOzone::GetVSyncProvider( | |
| 208 gfx::AcceleratedWidget w) { | |
| 209 return NULL; | |
| 210 } | |
| 211 | |
| 212 //////////////////////////////////////////////////////////////////////////////// | |
| 213 // SoftwareSurfaceFactoryOzone private | |
| 214 | |
| 215 SoftwareSurfaceOzone* SoftwareSurfaceFactoryOzone::CreateSurface( | |
| 216 HardwareDisplayControllerOzone* controller) { | |
| 217 return new SoftwareSurfaceOzone(controller); | |
| 218 } | |
| 219 | |
| 220 DrmWrapperOzone* SoftwareSurfaceFactoryOzone::CreateWrapper() { | |
| 221 return new DrmWrapperOzone(kDefaultGraphicsCardPath); | |
| 222 } | |
| 223 | |
| 224 bool SoftwareSurfaceFactoryOzone::InitializeControllerForPrimaryDisplay( | |
| 225 DrmWrapperOzone* drm, | |
| 226 HardwareDisplayControllerOzone* controller) { | |
| 227 CHECK(state_ == SurfaceFactoryOzone::INITIALIZED); | |
| 228 | |
| 229 drmModeRes* resources = drmModeGetResources(drm->get_fd()); | |
| 230 | |
| 231 // Search for an active connector. | |
| 232 for (int i = 0; i < resources->count_connectors; ++i) { | |
| 233 drmModeConnector* connector = drmModeGetConnector( | |
| 234 drm->get_fd(), | |
| 235 resources->connectors[i]); | |
| 236 | |
| 237 if (!connector) | |
| 238 continue; | |
| 239 | |
| 240 if (connector->connection != DRM_MODE_CONNECTED || | |
| 241 connector->count_modes == 0) { | |
| 242 drmModeFreeConnector(connector); | |
| 243 continue; | |
| 244 } | |
| 245 | |
| 246 uint32_t crtc = GetCrtc(drm->get_fd(), resources, connector); | |
| 247 | |
| 248 if (!crtc) | |
| 249 continue; | |
| 250 | |
| 251 // TODO(dnicoara) Select one mode for now. In the future we may need to | |
| 252 // save all the modes and allow the user to choose a specific mode. Or | |
| 253 // even some fullscreen applications may need to change the mode. | |
| 254 controller->SetControllerInfo( | |
| 255 drm, | |
| 256 connector->connector_id, | |
| 257 crtc, | |
| 258 connector->modes[0]); | |
| 259 | |
| 260 drmModeFreeConnector(connector); | |
| 261 | |
| 262 return true; | |
| 263 } | |
| 264 | |
| 265 return false; | |
| 266 } | |
| 267 | |
| 268 void SoftwareSurfaceFactoryOzone::WaitForPageFlipEvent(int fd) { | |
| 269 drmEventContext drm_event; | |
| 270 drm_event.version = DRM_EVENT_CONTEXT_VERSION; | |
| 271 drm_event.page_flip_handler = HandlePageFlipEvent; | |
| 272 drm_event.vblank_handler = NULL; | |
| 273 | |
| 274 // Wait for the page-flip to complete. | |
| 275 drmHandleEvent(fd, &drm_event); | |
| 276 } | |
| 277 | |
| 278 } // namespace gfx | |
| OLD | NEW |