summaryrefslogtreecommitdiffstats
path: root/ui/ozone/platform/drm/gpu/hardware_display_controller.cc
blob: f9f08ea1926f5fb6691f12eb7dc5c2b82205053a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/ozone/platform/drm/gpu/hardware_display_controller.h"

#include <drm.h>
#include <string.h>
#include <xf86drm.h>

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/trace_event/trace_event.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/size.h"
#include "ui/ozone/platform/drm/gpu/crtc_controller.h"
#include "ui/ozone/platform/drm/gpu/drm_buffer.h"
#include "ui/ozone/platform/drm/gpu/drm_device.h"
#include "ui/ozone/public/native_pixmap.h"

namespace ui {

HardwareDisplayController::PageFlipRequest::PageFlipRequest(
    const OverlayPlaneList& planes,
    bool is_sync,
    const base::Closure& callback)
    : planes(planes), is_sync(is_sync), callback(callback) {
}

HardwareDisplayController::PageFlipRequest::~PageFlipRequest() {
}

HardwareDisplayController::HardwareDisplayController(
    scoped_ptr<CrtcController> controller)
    : is_disabled_(true) {
  memset(&mode_, 0, sizeof(mode_));
  AddCrtc(controller.Pass());
}

HardwareDisplayController::~HardwareDisplayController() {
  // Reset the cursor.
  UnsetCursor();
  ClearPendingRequests();
}

bool HardwareDisplayController::Modeset(const OverlayPlane& primary,
                                        drmModeModeInfo mode) {
  TRACE_EVENT0("drm", "HDC::Modeset");
  DCHECK(primary.buffer.get());
  bool status = true;
  for (size_t i = 0; i < crtc_controllers_.size(); ++i)
    status &= crtc_controllers_[i]->Modeset(primary, mode);

  is_disabled_ = false;
  mode_ = mode;

  current_planes_ = std::vector<OverlayPlane>(1, primary);
  ClearPendingRequests();

  // Because a page flip is pending we need to leave some state for the
  // callback. We use the modeset state since it is the only valid state.
  if (HasPendingPageFlips())
    requests_.push_back(
        PageFlipRequest(current_planes_, false, base::Bind(&base::DoNothing)));

  return status;
}

bool HardwareDisplayController::Enable() {
  TRACE_EVENT0("drm", "HDC::Enable");
  DCHECK(!current_planes_.empty());

  const OverlayPlane* primary = nullptr;
  // Use the last scheduled buffer to modeset to preserve request order.
  if (!requests_.empty())
    primary = OverlayPlane::GetPrimaryPlane(requests_.back().planes);
  else
    primary = OverlayPlane::GetPrimaryPlane(current_planes_);

  return Modeset(*primary, mode_);
}

void HardwareDisplayController::Disable() {
  TRACE_EVENT0("drm", "HDC::Disable");
  for (size_t i = 0; i < crtc_controllers_.size(); ++i)
    crtc_controllers_[i]->Disable();

  is_disabled_ = true;
}

void HardwareDisplayController::QueueOverlayPlane(const OverlayPlane& plane) {
  pending_planes_.push_back(plane);
}

bool HardwareDisplayController::SchedulePageFlip(
    bool is_sync,
    const base::Closure& callback) {
  TRACE_EVENT0("drm", "HDC::SchedulePageFlip");

  // Ignore requests with no planes to schedule.
  if (pending_planes_.empty()) {
    callback.Run();
    return true;
  }

  requests_.push_back(PageFlipRequest(pending_planes_, is_sync, callback));
  pending_planes_.clear();

  // A request is being serviced right now.
  if (HasPendingPageFlips())
    return true;

  bool status = ActualSchedulePageFlip();

  // No page flip event on failure so discard failed request.
  if (!status)
    requests_.pop_front();

  return status;
}

bool HardwareDisplayController::SetCursor(
    const scoped_refptr<ScanoutBuffer>& buffer) {
  bool status = true;

  if (is_disabled_)
    return true;

  for (size_t i = 0; i < crtc_controllers_.size(); ++i)
    status &= crtc_controllers_[i]->SetCursor(buffer);

  return status;
}

bool HardwareDisplayController::UnsetCursor() {
  bool status = true;
  for (size_t i = 0; i < crtc_controllers_.size(); ++i)
    status &= crtc_controllers_[i]->SetCursor(nullptr);

  return status;
}

bool HardwareDisplayController::MoveCursor(const gfx::Point& location) {
  if (is_disabled_)
    return true;

  bool status = true;
  for (size_t i = 0; i < crtc_controllers_.size(); ++i)
    status &= crtc_controllers_[i]->MoveCursor(location);

  return status;
}

void HardwareDisplayController::AddCrtc(scoped_ptr<CrtcController> controller) {
  owned_hardware_planes_.add(
      controller->drm().get(),
      scoped_ptr<HardwareDisplayPlaneList>(new HardwareDisplayPlaneList()));
  controller->AddObserver(this);
  crtc_controllers_.push_back(controller.release());
}

scoped_ptr<CrtcController> HardwareDisplayController::RemoveCrtc(
    const scoped_refptr<DrmDevice>& drm,
    uint32_t crtc) {
  for (ScopedVector<CrtcController>::iterator it = crtc_controllers_.begin();
       it != crtc_controllers_.end(); ++it) {
    if ((*it)->drm() == drm && (*it)->crtc() == crtc) {
      scoped_ptr<CrtcController> controller(*it);
      crtc_controllers_.weak_erase(it);
      // Remove entry from |owned_hardware_planes_| iff no other crtcs share it.
      bool found = false;
      for (ScopedVector<CrtcController>::iterator it =
               crtc_controllers_.begin();
           it != crtc_controllers_.end(); ++it) {
        if ((*it)->drm() == controller->drm()) {
          found = true;
          break;
        }
      }
      if (!found)
        owned_hardware_planes_.erase(controller->drm().get());

      controller->RemoveObserver(this);
      // If a display configuration happens mid page flip we want to make sure
      // the HDC won't wait for an event from a CRTC that is no longer
      // associated with it.
      if (controller->page_flip_pending())
        OnPageFlipEvent();

      return controller.Pass();
    }
  }

  return nullptr;
}

bool HardwareDisplayController::HasCrtc(const scoped_refptr<DrmDevice>& drm,
                                        uint32_t crtc) const {
  for (size_t i = 0; i < crtc_controllers_.size(); ++i)
    if (crtc_controllers_[i]->drm() == drm &&
        crtc_controllers_[i]->crtc() == crtc)
      return true;

  return false;
}

bool HardwareDisplayController::IsMirrored() const {
  return crtc_controllers_.size() > 1;
}

bool HardwareDisplayController::IsDisabled() const {
  return is_disabled_;
}

gfx::Size HardwareDisplayController::GetModeSize() const {
  return gfx::Size(mode_.hdisplay, mode_.vdisplay);
}

uint64_t HardwareDisplayController::GetTimeOfLastFlip() const {
  uint64_t time = 0;
  for (size_t i = 0; i < crtc_controllers_.size(); ++i)
    if (time < crtc_controllers_[i]->time_of_last_flip())
      time = crtc_controllers_[i]->time_of_last_flip();

  return time;
}

void HardwareDisplayController::OnPageFlipEvent() {
  TRACE_EVENT0("drm", "HDC::OnPageFlipEvent");
  // OnPageFlipEvent() needs to handle 2 cases:
  //  1) Normal page flips in which case:
  //    a) HasPendingPageFlips() may return false if we're in mirror mode and
  //       one of the CRTCs hasn't finished page flipping. In this case we want
  //       to wait for all the CRTCs.
  //    b) HasPendingPageFlips() returns true in which case all CRTCs are ready
  //       for the next request. In this case we expect that |requests_| isn't
  //       empty.
  //  2) A CRTC was added while it was page flipping. In this case a modeset
  //     must be performed. Modesetting clears all pending requests, however the
  //     CRTCs will honor the scheduled page flip. Thus we need to handle page
  //     flip events with no requests.

  if (HasPendingPageFlips())
    return;

  if (!requests_.empty())
    ProcessPageFlipRequest();

  // ProcessPageFlipRequest() consumes a request.
  if (requests_.empty())
    return;

  // At this point we still have requests pending, so schedule the next request.
  bool status = ActualSchedulePageFlip();
  if (!status) {
    PageFlipRequest request = requests_.front();
    requests_.pop_front();

    // Normally the caller would handle the error call, but because we're in a
    // delayed schedule the initial SchedulePageFlip() already returned true,
    // thus we need to run the callback.
    request.callback.Run();
  }
}

scoped_refptr<DrmDevice> HardwareDisplayController::GetAllocationDrmDevice()
    const {
  DCHECK(!crtc_controllers_.empty());
  // TODO(dnicoara) When we support mirroring across DRM devices, figure out
  // which device should be used for allocations.
  return crtc_controllers_[0]->drm();
}

bool HardwareDisplayController::HasPendingPageFlips() const {
  for (size_t i = 0; i < crtc_controllers_.size(); ++i)
    if (crtc_controllers_[i]->page_flip_pending())
      return true;

  return false;
}

bool HardwareDisplayController::ActualSchedulePageFlip() {
  TRACE_EVENT0("drm", "HDC::ActualSchedulePageFlip");
  DCHECK(!requests_.empty());

  if (is_disabled_) {
    ProcessPageFlipRequest();
    return true;
  }

  OverlayPlaneList pending_planes = requests_.front().planes;
  std::sort(pending_planes.begin(), pending_planes.end(),
            [](const OverlayPlane& l, const OverlayPlane& r) {
              return l.z_order < r.z_order;
            });

  bool status = true;
  for (size_t i = 0; i < crtc_controllers_.size(); ++i) {
    status &= crtc_controllers_[i]->SchedulePageFlip(
        owned_hardware_planes_.get(crtc_controllers_[i]->drm().get()),
        pending_planes);
  }

  bool is_sync = requests_.front().is_sync;
  for (const auto& planes : owned_hardware_planes_) {
    if (!planes.first->plane_manager()->Commit(planes.second, is_sync)) {
      status = false;
    }
  }

  return status;
}

void HardwareDisplayController::ProcessPageFlipRequest() {
  DCHECK(!requests_.empty());
  PageFlipRequest request = requests_.front();
  requests_.pop_front();

  current_planes_.swap(request.planes);
  request.callback.Run();
}

void HardwareDisplayController::ClearPendingRequests() {
  while (!requests_.empty()) {
    PageFlipRequest request = requests_.front();
    requests_.pop_front();
    request.callback.Run();
  }
}

}  // namespace ui