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.
5 #include "ui/gfx/ozone/impl/software_surface_factory_ozone.h"
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"
24 const char kDefaultGraphicsCardPath[] = "/dev/dri/card0";
25 const char kDPMSProperty[] = "DPMS";
27 const gfx::AcceleratedWidget kDefaultWidgetHandle = 1;
29 // DRM callback on page flip events. This callback is triggered after the
30 // page flip has happened and the backbuffer is now the new frontbuffer
31 // The old frontbuffer is no longer used by the hardware and can be used for
32 // future draw operations.
34 // |device| will contain a reference to the |SoftwareSurfaceOzone| object which
35 // the event belongs to.
37 // TODO(dnicoara) When we have a FD handler for the DRM calls in the message
38 // loop, we can move this function in the handler.
39 void HandlePageFlipEvent(int fd,
42 unsigned int useconds,
44 static_cast<HardwareDisplayControllerOzone*>(controller)->get_surface()
48 uint32_t GetDrmProperty(int fd, drmModeConnector* connector, const char* name) {
49 for (int i = 0; i < connector->count_props; ++i) {
50 drmModePropertyPtr property = drmModeGetProperty(fd, connector->props[i]);
54 if (strcmp(property->name, name) == 0) {
55 uint32_t id = property->prop_id;
56 drmModeFreeProperty(property);
60 drmModeFreeProperty(property);
65 uint32_t GetCrtc(int fd, drmModeRes* resources, drmModeConnector* connector) {
66 // If the connector already has an encoder try to re-use.
67 if (connector->encoder_id) {
68 drmModeEncoder* encoder = drmModeGetEncoder(fd, connector->encoder_id);
70 if (encoder->crtc_id) {
71 uint32_t crtc = encoder->crtc_id;
72 drmModeFreeEncoder(encoder);
75 drmModeFreeEncoder(encoder);
79 // Try to find an encoder for the connector.
80 for (int i = 0; i < connector->count_encoders; ++i) {
81 drmModeEncoder* encoder = drmModeGetEncoder(fd, connector->encoders[i]);
85 for (int j = 0; j < resources->count_crtcs; ++j) {
86 // Check if the encoder is compatible with this CRTC
87 if (!(encoder->possible_crtcs & (1 << j)))
90 drmModeFreeEncoder(encoder);
91 return resources->crtcs[j];
100 SoftwareSurfaceFactoryOzone::SoftwareSurfaceFactoryOzone()
102 state_(UNINITIALIZED),
106 SoftwareSurfaceFactoryOzone::~SoftwareSurfaceFactoryOzone() {
107 if (state_ == INITIALIZED)
111 SurfaceFactoryOzone::HardwareState
112 SoftwareSurfaceFactoryOzone::InitializeHardware() {
113 CHECK(state_ == UNINITIALIZED);
115 // TODO(dnicoara): Short-cut right now. What we want is to look at all the
116 // graphics devices available and select the primary one.
117 drm_.reset(CreateWrapper());
118 if (drm_->get_fd() < 0) {
119 LOG(ERROR) << "Cannot open graphics card '"
120 << kDefaultGraphicsCardPath << "': " << strerror(errno);
125 state_ = INITIALIZED;
129 void SoftwareSurfaceFactoryOzone::ShutdownHardware() {
130 CHECK(state_ == INITIALIZED);
135 state_ = UNINITIALIZED;
138 gfx::AcceleratedWidget SoftwareSurfaceFactoryOzone::GetAcceleratedWidget() {
139 CHECK(state_ != FAILED);
141 // TODO(dnicoara) When there's more information on which display we want,
142 // then we can return the widget associated with the display.
143 // For now just assume we have 1 display device and return it.
144 if (!controller_.get())
145 controller_.reset(new HardwareDisplayControllerOzone());
147 // TODO(dnicoara) We only have 1 display for now, so only 1 AcceleratedWidget.
148 // When we'll support multiple displays this needs to be changed to return a
149 // different handle for every display.
150 return kDefaultWidgetHandle;
153 gfx::AcceleratedWidget SoftwareSurfaceFactoryOzone::RealizeAcceleratedWidget(
154 gfx::AcceleratedWidget w) {
155 CHECK(state_ == INITIALIZED);
156 // TODO(dnicoara) Once we can handle multiple displays this needs to be
158 CHECK(w == kDefaultWidgetHandle);
160 CHECK(controller_->get_state() ==
161 HardwareDisplayControllerOzone::UNASSOCIATED);
163 // Until now the controller is just a stub. Initializing it will link it to a
165 if (!InitializeControllerForPrimaryDisplay(drm_.get(), controller_.get())) {
166 LOG(ERROR) << "Failed to initialize controller";
167 return gfx::kNullAcceleratedWidget;
170 // Create a surface suitable for the current controller.
171 scoped_ptr<SoftwareSurfaceOzone> surface(CreateSurface(controller_.get()));
173 if (!surface->Initialize()) {
174 LOG(ERROR) << "Failed to initialize surface";
175 return gfx::kNullAcceleratedWidget;
178 // Bind the surface to the controller. This will register the backing buffers
179 // with the hardware CRTC such that we can show the buffers. The controller
180 // takes ownership of the surface.
181 if (!controller_->BindSurfaceToController(surface.Pass())) {
182 LOG(ERROR) << "Failed to bind surface to controller";
183 return gfx::kNullAcceleratedWidget;
186 return reinterpret_cast<gfx::AcceleratedWidget>(controller_->get_surface());
189 bool SoftwareSurfaceFactoryOzone::LoadEGLGLES2Bindings(
190 AddGLLibraryCallback add_gl_library,
191 SetGLGetProcAddressProcCallback set_gl_get_proc_address) {
195 bool SoftwareSurfaceFactoryOzone::AttemptToResizeAcceleratedWidget(
196 gfx::AcceleratedWidget w,
197 const gfx::Rect& bounds) {
201 bool SoftwareSurfaceFactoryOzone::SchedulePageFlip(gfx::AcceleratedWidget w) {
202 CHECK(state_ == INITIALIZED);
203 // TODO(dnicoara) Change this CHECK once we're running with the threaded
205 CHECK(base::MessageLoop::current()->type() == base::MessageLoop::TYPE_UI);
207 // TODO(dnicoara) Once we can handle multiple displays this needs to be
209 CHECK(w == kDefaultWidgetHandle);
211 if (!controller_->SchedulePageFlip())
214 // Only wait for the page flip event to finish if it was properly scheduled.
216 // TODO(dnicoara) The following call will wait for the page flip event to
217 // complete. This means that it will block until the next VSync. Ideally the
218 // wait should happen in the message loop. The message loop would then
219 // schedule the next draw event. Alternatively, the VSyncProvider could be
220 // used to schedule the next draw. Unfortunately, at this point,
221 // SoftwareOutputDevice does not provide any means to use any of the above
222 // solutions. Note that if the DRM callback does not schedule the next draw,
223 // then some sort of synchronization needs to take place since starting a new
224 // draw before the page flip happened is considered an error. However we can
225 // not use any lock constructs unless we're using the threaded compositor.
226 // Note that the following call does not use any locks, so it is safe to be
227 // made on the UI thread (thought not ideal).
228 WaitForPageFlipEvent(drm_->get_fd());
233 SkCanvas* SoftwareSurfaceFactoryOzone::GetCanvasForWidget(
234 gfx::AcceleratedWidget w) {
235 CHECK(state_ == INITIALIZED);
236 return reinterpret_cast<SoftwareSurfaceOzone*>(w)->GetDrawableForWidget();
239 gfx::VSyncProvider* SoftwareSurfaceFactoryOzone::GetVSyncProvider(
240 gfx::AcceleratedWidget w) {
244 ////////////////////////////////////////////////////////////////////////////////
245 // SoftwareSurfaceFactoryOzone private
247 SoftwareSurfaceOzone* SoftwareSurfaceFactoryOzone::CreateSurface(
248 HardwareDisplayControllerOzone* controller) {
249 return new SoftwareSurfaceOzone(controller);
252 DrmWrapperOzone* SoftwareSurfaceFactoryOzone::CreateWrapper() {
253 return new DrmWrapperOzone(kDefaultGraphicsCardPath);
256 bool SoftwareSurfaceFactoryOzone::InitializeControllerForPrimaryDisplay(
257 DrmWrapperOzone* drm,
258 HardwareDisplayControllerOzone* controller) {
259 CHECK(state_ == SurfaceFactoryOzone::INITIALIZED);
261 drmModeRes* resources = drmModeGetResources(drm->get_fd());
263 // Search for an active connector.
264 for (int i = 0; i < resources->count_connectors; ++i) {
265 drmModeConnector* connector = drmModeGetConnector(
267 resources->connectors[i]);
272 if (connector->connection != DRM_MODE_CONNECTED ||
273 connector->count_modes == 0) {
274 drmModeFreeConnector(connector);
278 uint32_t crtc = GetCrtc(drm->get_fd(), resources, connector);
283 uint32_t dpms_property_id = GetDrmProperty(drm->get_fd(),
287 // TODO(dnicoara) Select one mode for now. In the future we may need to
288 // save all the modes and allow the user to choose a specific mode. Or
289 // even some fullscreen applications may need to change the mode.
290 controller->SetControllerInfo(
292 connector->connector_id,
295 connector->modes[0]);
297 drmModeFreeConnector(connector);
305 void SoftwareSurfaceFactoryOzone::WaitForPageFlipEvent(int fd) {
306 drmEventContext drm_event;
307 drm_event.version = DRM_EVENT_CONTEXT_VERSION;
308 drm_event.page_flip_handler = HandlePageFlipEvent;
309 drm_event.vblank_handler = NULL;
311 // Wait for the page-flip to complete.
312 drmHandleEvent(fd, &drm_event);