- add sources.
[platform/framework/web/crosswalk.git] / src / ui / gfx / ozone / impl / hardware_display_controller_ozone.cc
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/gfx/ozone/impl/hardware_display_controller_ozone.h"
6
7 #include <errno.h>
8 #include <string.h>
9
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "ui/gfx/ozone/impl/drm_skbitmap_ozone.h"
13 #include "ui/gfx/ozone/impl/drm_wrapper_ozone.h"
14 #include "ui/gfx/ozone/impl/software_surface_ozone.h"
15
16 namespace gfx {
17
18 HardwareDisplayControllerOzone::HardwareDisplayControllerOzone()
19   : drm_(NULL),
20     connector_id_(0),
21     crtc_id_(0),
22     mode_(),
23     saved_crtc_(NULL),
24     state_(UNASSOCIATED),
25     surface_() {
26 }
27
28 void HardwareDisplayControllerOzone::SetControllerInfo(
29     DrmWrapperOzone* drm,
30     uint32_t connector_id,
31     uint32_t crtc_id,
32     uint32_t dpms_property_id,
33     drmModeModeInfo mode) {
34   drm_ = drm;
35   connector_id_ = connector_id;
36   crtc_id_ = crtc_id;
37   dpms_property_id_ = dpms_property_id;
38   mode_ = mode;
39   saved_crtc_ = drm_->GetCrtc(crtc_id_);
40   state_ = UNINITIALIZED;
41 }
42
43 HardwareDisplayControllerOzone::~HardwareDisplayControllerOzone() {
44   if (saved_crtc_) {
45     if (!drm_->SetCrtc(saved_crtc_, &connector_id_))
46       DLOG(ERROR) << "Failed to restore CRTC state: " << strerror(errno);
47     drm_->FreeCrtc(saved_crtc_);
48   }
49
50   if (surface_.get()) {
51     // Unregister the buffers.
52     for (int i = 0; i < 2; ++i) {
53       if (!drm_->RemoveFramebuffer(surface_->bitmaps_[i]->get_framebuffer()))
54         DLOG(ERROR) << "Failed to remove FB: " << strerror(errno);
55     }
56   }
57 }
58
59 bool
60 HardwareDisplayControllerOzone::BindSurfaceToController(
61     scoped_ptr<SoftwareSurfaceOzone> surface) {
62   CHECK(state_ == UNINITIALIZED);
63
64   // Register the buffers.
65   for (int i = 0; i < 2; ++i) {
66     uint32_t fb_id;
67     if (!drm_->AddFramebuffer(mode_,
68                               surface->bitmaps_[i]->GetColorDepth(),
69                               surface->bitmaps_[i]->bytesPerPixel() << 3,
70                               surface->bitmaps_[i]->rowBytes(),
71                               surface->bitmaps_[i]->get_handle(),
72                               &fb_id)) {
73       DLOG(ERROR) << "Failed to register framebuffer: " << strerror(errno);
74       state_ = FAILED;
75       return false;
76     }
77     surface->bitmaps_[i]->set_framebuffer(fb_id);
78   }
79
80   surface_.reset(surface.release());
81   state_ = SURFACE_INITIALIZED;
82   return true;
83 }
84
85 bool HardwareDisplayControllerOzone::SchedulePageFlip() {
86   CHECK(state_ == SURFACE_INITIALIZED || state_ == INITIALIZED);
87
88   if (state_ == SURFACE_INITIALIZED) {
89     // Perform the initial modeset.
90     if (!drm_->SetCrtc(crtc_id_,
91                        surface_->GetFramebufferId(),
92                        &connector_id_,
93                        &mode_)) {
94       DLOG(ERROR) << "Cannot set CRTC: " << strerror(errno);
95       state_ = FAILED;
96       return false;
97     } else {
98       state_ = INITIALIZED;
99     }
100
101     if (dpms_property_id_)
102       drm_->ConnectorSetProperty(connector_id_,
103                                  dpms_property_id_,
104                                  DRM_MODE_DPMS_ON);
105   }
106
107   if (!drm_->PageFlip(crtc_id_,
108                       surface_->GetFramebufferId(),
109                       this)) {
110     state_ = FAILED;
111     LOG(ERROR) << "Cannot page flip: " << strerror(errno);
112     return false;
113   }
114
115   return true;
116 }
117
118 }  // namespace gfx