Stop implicit conversion from Rect to RectF.
[chromium-blink-merge.git] / ui / ozone / platform / drm / gpu / hardware_display_plane_manager.cc
blob3b06861a94ead750b90d7b4a9dd9968ae698c7ce
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.
5 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager.h"
7 #include <set>
9 #include "base/logging.h"
10 #include "ui/gfx/geometry/rect.h"
11 #include "ui/ozone/platform/drm/gpu/drm_device.h"
12 #include "ui/ozone/platform/drm/gpu/scanout_buffer.h"
14 namespace ui {
15 namespace {
17 const float kFixedPointScaleValue = 65536.0f;
19 } // namespace
21 HardwareDisplayPlaneList::HardwareDisplayPlaneList() {
22 #if defined(USE_DRM_ATOMIC)
23 atomic_property_set.reset(drmModePropertySetAlloc());
24 #endif // defined(USE_DRM_ATOMIC)
27 HardwareDisplayPlaneList::~HardwareDisplayPlaneList() {
30 HardwareDisplayPlaneList::PageFlipInfo::PageFlipInfo(uint32_t crtc_id,
31 uint32_t framebuffer,
32 CrtcController* crtc)
33 : crtc_id(crtc_id), framebuffer(framebuffer), crtc(crtc) {
36 HardwareDisplayPlaneList::PageFlipInfo::~PageFlipInfo() {
39 HardwareDisplayPlaneList::PageFlipInfo::Plane::Plane(int plane,
40 int framebuffer,
41 const gfx::Rect& bounds,
42 const gfx::Rect& src_rect)
43 : plane(plane),
44 framebuffer(framebuffer),
45 bounds(bounds),
46 src_rect(src_rect) {
49 HardwareDisplayPlaneList::PageFlipInfo::Plane::~Plane() {
52 HardwareDisplayPlaneManager::HardwareDisplayPlaneManager() : drm_(nullptr) {
55 HardwareDisplayPlaneManager::~HardwareDisplayPlaneManager() {
58 bool HardwareDisplayPlaneManager::Initialize(DrmDevice* drm) {
59 drm_ = drm;
61 // Try to get all of the planes if possible, so we don't have to try to
62 // discover hidden primary planes.
63 bool has_universal_planes = false;
64 #if defined(DRM_CLIENT_CAP_UNIVERSAL_PLANES)
65 has_universal_planes = drm->SetCapability(DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
66 #endif // defined(DRM_CLIENT_CAP_UNIVERSAL_PLANES)
68 ScopedDrmResourcesPtr resources(drmModeGetResources(drm->get_fd()));
69 if (!resources) {
70 PLOG(ERROR) << "Failed to get resources";
71 return false;
74 ScopedDrmPlaneResPtr plane_resources(drmModeGetPlaneResources(drm->get_fd()));
75 if (!plane_resources) {
76 PLOG(ERROR) << "Failed to get plane resources";
77 return false;
80 crtcs_.clear();
81 for (int i = 0; i < resources->count_crtcs; ++i) {
82 crtcs_.push_back(resources->crtcs[i]);
85 uint32_t num_planes = plane_resources->count_planes;
86 std::set<uint32_t> plane_ids;
87 for (uint32_t i = 0; i < num_planes; ++i) {
88 ScopedDrmPlanePtr drm_plane(
89 drmModeGetPlane(drm->get_fd(), plane_resources->planes[i]));
90 if (!drm_plane) {
91 PLOG(ERROR) << "Failed to get plane " << i;
92 return false;
95 uint32_t formats_size = drm_plane->count_formats;
96 plane_ids.insert(drm_plane->plane_id);
97 scoped_ptr<HardwareDisplayPlane> plane(
98 CreatePlane(drm_plane->plane_id, drm_plane->possible_crtcs));
100 std::vector<uint32_t> supported_formats(formats_size);
101 for (uint32_t j = 0; j < formats_size; j++)
102 supported_formats.push_back(drm_plane->formats[j]);
104 if (plane->Initialize(drm, supported_formats, false, false)) {
105 // CRTC controllers always assume they have a cursor plane and the cursor
106 // plane is updated via cursor specific DRM API. Hence, we dont keep
107 // track of Cursor plane here to avoid re-using it for any other purpose.
108 if (plane->type() != HardwareDisplayPlane::kCursor)
109 planes_.push_back(plane.Pass());
113 // crbug.com/464085: if driver reports no primary planes for a crtc, create a
114 // dummy plane for which we can assign exactly one overlay.
115 // TODO(dnicoara): refactor this to simplify AssignOverlayPlanes and move
116 // this workaround into HardwareDisplayPlaneLegacy.
117 if (!has_universal_planes) {
118 for (int i = 0; i < resources->count_crtcs; ++i) {
119 if (plane_ids.find(resources->crtcs[i] - 1) == plane_ids.end()) {
120 scoped_ptr<HardwareDisplayPlane> dummy_plane(
121 CreatePlane(resources->crtcs[i] - 1, (1 << i)));
122 if (dummy_plane->Initialize(drm, std::vector<uint32_t>(), true,
123 false)) {
124 planes_.push_back(dummy_plane.Pass());
130 std::sort(planes_.begin(), planes_.end(),
131 [](HardwareDisplayPlane* l, HardwareDisplayPlane* r) {
132 return l->plane_id() < r->plane_id();
134 return true;
137 scoped_ptr<HardwareDisplayPlane> HardwareDisplayPlaneManager::CreatePlane(
138 uint32_t plane_id,
139 uint32_t possible_crtcs) {
140 return scoped_ptr<HardwareDisplayPlane>(
141 new HardwareDisplayPlane(plane_id, possible_crtcs));
144 HardwareDisplayPlane* HardwareDisplayPlaneManager::FindNextUnusedPlane(
145 size_t* index,
146 uint32_t crtc_index,
147 uint32_t format) {
148 for (size_t i = *index; i < planes_.size(); ++i) {
149 auto plane = planes_[i];
150 if (!plane->in_use() && plane->CanUseForCrtc(crtc_index) &&
151 plane->IsSupportedFormat(format)) {
152 *index = i + 1;
153 return plane;
156 return nullptr;
159 int HardwareDisplayPlaneManager::LookupCrtcIndex(uint32_t crtc_id) {
160 for (size_t i = 0; i < crtcs_.size(); ++i)
161 if (crtcs_[i] == crtc_id)
162 return i;
163 return -1;
166 void HardwareDisplayPlaneManager::BeginFrame(
167 HardwareDisplayPlaneList* plane_list) {
168 for (auto* plane : plane_list->old_plane_list) {
169 plane->set_in_use(false);
173 bool HardwareDisplayPlaneManager::AssignOverlayPlanes(
174 HardwareDisplayPlaneList* plane_list,
175 const OverlayPlaneList& overlay_list,
176 uint32_t crtc_id,
177 CrtcController* crtc) {
178 int crtc_index = LookupCrtcIndex(crtc_id);
179 if (crtc_index < 0) {
180 LOG(ERROR) << "Cannot find crtc " << crtc_id;
181 return false;
184 size_t plane_idx = 0;
185 for (const auto& plane : overlay_list) {
186 HardwareDisplayPlane* hw_plane = FindNextUnusedPlane(
187 &plane_idx, crtc_index, plane.buffer->GetFramebufferPixelFormat());
188 if (!hw_plane) {
189 LOG(ERROR) << "Failed to find a free plane for crtc " << crtc_id;
190 return false;
193 gfx::Rect fixed_point_rect;
194 if (hw_plane->type() != HardwareDisplayPlane::kDummy) {
195 const gfx::Size& size = plane.buffer->GetSize();
196 gfx::RectF crop_rect = gfx::RectF(plane.crop_rect);
197 crop_rect.Scale(size.width(), size.height());
199 // This returns a number in 16.16 fixed point, required by the DRM overlay
200 // APIs.
201 auto to_fixed_point =
202 [](double v) -> uint32_t { return v * kFixedPointScaleValue; };
203 fixed_point_rect = gfx::Rect(to_fixed_point(crop_rect.x()),
204 to_fixed_point(crop_rect.y()),
205 to_fixed_point(crop_rect.width()),
206 to_fixed_point(crop_rect.height()));
209 plane_list->plane_list.push_back(hw_plane);
210 hw_plane->set_owning_crtc(crtc_id);
211 if (SetPlaneData(plane_list, hw_plane, plane, crtc_id, fixed_point_rect,
212 crtc)) {
213 hw_plane->set_in_use(true);
214 } else {
215 return false;
218 return true;
221 } // namespace ui