Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / ozone / platform / dri / chromeos / native_display_delegate_dri.cc
1 // Copyright 2014 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/chromeos/native_display_delegate_dri.h"
6
7 #include "base/bind.h"
8 #include "ui/display/types/chromeos/native_display_observer.h"
9 #include "ui/events/ozone/device/device_event.h"
10 #include "ui/events/ozone/device/device_manager.h"
11 #include "ui/ozone/platform/dri/chromeos/display_mode_dri.h"
12 #include "ui/ozone/platform/dri/chromeos/display_snapshot_dri.h"
13 #include "ui/ozone/platform/dri/dri_surface_factory.h"
14 #include "ui/ozone/platform/dri/dri_util.h"
15 #include "ui/ozone/platform/dri/dri_wrapper.h"
16
17 namespace ui {
18
19 namespace {
20 const size_t kMaxDisplayCount = 2;
21 }  // namespace
22
23 NativeDisplayDelegateDri::NativeDisplayDelegateDri(
24     DriSurfaceFactory* surface_factory, DeviceManager* device_manager)
25     : surface_factory_(surface_factory),
26       device_manager_(device_manager) {}
27
28 NativeDisplayDelegateDri::~NativeDisplayDelegateDri() {
29   device_manager_->RemoveObserver(this);
30 }
31
32 void NativeDisplayDelegateDri::Initialize() {
33   gfx::SurfaceFactoryOzone::HardwareState state =
34       surface_factory_->InitializeHardware();
35
36   CHECK_EQ(gfx::SurfaceFactoryOzone::INITIALIZED, state)
37       << "Failed to initialize hardware";
38
39   device_manager_->AddObserver(this);
40 }
41
42 void NativeDisplayDelegateDri::GrabServer() {}
43
44 void NativeDisplayDelegateDri::UngrabServer() {}
45
46 void NativeDisplayDelegateDri::SyncWithServer() {}
47
48 void NativeDisplayDelegateDri::SetBackgroundColor(uint32_t color_argb) {
49   NOTIMPLEMENTED();
50 }
51
52 void NativeDisplayDelegateDri::ForceDPMSOn() {
53   for (size_t i = 0; i < cached_displays_.size(); ++i) {
54     DisplaySnapshotDri* dri_output = cached_displays_[i];
55     if (dri_output->dpms_property())
56       surface_factory_->drm()->SetProperty(
57           dri_output->connector(),
58           dri_output->dpms_property()->prop_id,
59           DRM_MODE_DPMS_ON);
60   }
61 }
62
63 std::vector<DisplaySnapshot*> NativeDisplayDelegateDri::GetDisplays() {
64   ScopedVector<DisplaySnapshotDri> old_displays(cached_displays_.Pass());
65   cached_modes_.clear();
66
67   drmModeRes* resources = drmModeGetResources(
68       surface_factory_->drm()->get_fd());
69   DCHECK(resources) << "Failed to get DRM resources";
70   ScopedVector<HardwareDisplayControllerInfo> displays =
71       GetAvailableDisplayControllerInfos(
72           surface_factory_->drm()->get_fd(),
73           resources);
74   for (size_t i = 0;
75        i < displays.size() && cached_displays_.size() < kMaxDisplayCount; ++i) {
76     DisplaySnapshotDri* display = new DisplaySnapshotDri(
77         surface_factory_->drm(),
78         displays[i]->connector(),
79         displays[i]->crtc(),
80         i);
81     cached_displays_.push_back(display);
82     // Modes can be shared between different displays, so we need to keep track
83     // of them independently for cleanup.
84     cached_modes_.insert(cached_modes_.end(),
85                          display->modes().begin(),
86                          display->modes().end());
87   }
88
89   drmModeFreeResources(resources);
90
91   for (size_t i = 0; i < old_displays.size(); ++i) {
92     bool found = false;
93     for (size_t j = 0; j < cached_displays_.size(); ++j) {
94       if (old_displays[i]->connector() == cached_displays_[j]->connector() &&
95           old_displays[i]->crtc() == cached_displays_[j]->crtc()) {
96         found = true;
97         break;
98       }
99     }
100
101     if (!found) {
102       surface_factory_->DestroyHardwareDisplayController(
103           old_displays[i]->connector(), old_displays[i]->crtc());
104     }
105   }
106
107   std::vector<DisplaySnapshot*> generic_displays(cached_displays_.begin(),
108                                                  cached_displays_.end());
109   return generic_displays;
110 }
111
112 void NativeDisplayDelegateDri::AddMode(const DisplaySnapshot& output,
113                                        const DisplayMode* mode) {}
114
115 bool NativeDisplayDelegateDri::Configure(const DisplaySnapshot& output,
116                                          const DisplayMode* mode,
117                                          const gfx::Point& origin) {
118   const DisplaySnapshotDri& dri_output =
119       static_cast<const DisplaySnapshotDri&>(output);
120
121   VLOG(1) << "DRM configuring: crtc=" << dri_output.crtc()
122           << " connector=" << dri_output.connector()
123           << " origin=" << origin.ToString()
124           << " size=" << mode->size().ToString();
125
126   if (mode) {
127     if (!surface_factory_->CreateHardwareDisplayController(
128         dri_output.connector(),
129         dri_output.crtc(),
130         static_cast<const DisplayModeDri*>(mode)->mode_info())) {
131       VLOG(1) << "Failed to configure: crtc=" << dri_output.crtc()
132               << " connector=" << dri_output.connector();
133       return false;
134     }
135   } else {
136     if (!surface_factory_->DisableHardwareDisplayController(
137         dri_output.crtc())) {
138       VLOG(1) << "Failed to disable crtc=" << dri_output.crtc();
139       return false;
140     }
141   }
142
143   return true;
144 }
145
146 void NativeDisplayDelegateDri::CreateFrameBuffer(const gfx::Size& size) {}
147
148 bool NativeDisplayDelegateDri::GetHDCPState(const DisplaySnapshot& output,
149                                             HDCPState* state) {
150   NOTIMPLEMENTED();
151   return false;
152 }
153
154 bool NativeDisplayDelegateDri::SetHDCPState(const DisplaySnapshot& output,
155                                             HDCPState state) {
156   NOTIMPLEMENTED();
157   return false;
158 }
159
160 std::vector<ui::ColorCalibrationProfile>
161 NativeDisplayDelegateDri::GetAvailableColorCalibrationProfiles(
162     const ui::DisplaySnapshot& output) {
163   NOTIMPLEMENTED();
164   return std::vector<ui::ColorCalibrationProfile>();
165 }
166
167 bool NativeDisplayDelegateDri::SetColorCalibrationProfile(
168     const ui::DisplaySnapshot& output,
169     ui::ColorCalibrationProfile new_profile) {
170   NOTIMPLEMENTED();
171   return false;
172 }
173
174 void NativeDisplayDelegateDri::AddObserver(NativeDisplayObserver* observer) {
175   observers_.AddObserver(observer);
176 }
177
178 void NativeDisplayDelegateDri::RemoveObserver(NativeDisplayObserver* observer) {
179   observers_.RemoveObserver(observer);
180 }
181
182 void NativeDisplayDelegateDri::OnDeviceEvent(const DeviceEvent& event) {
183   if (event.device_type() != DeviceEvent::DISPLAY)
184     return;
185
186   if (event.action_type() == DeviceEvent::CHANGE) {
187     VLOG(1) << "Got display changed event";
188     FOR_EACH_OBSERVER(
189         NativeDisplayObserver, observers_, OnConfigurationChanged());
190   }
191 }
192
193 }  // namespace ui