Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "hwc-drm-composition" |
| 18 | |
| 19 | #include "drmcomposition.h" |
| 20 | #include "drmcrtc.h" |
| 21 | #include "drmplane.h" |
| 22 | #include "drmresources.h" |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | #include <cutils/log.h> |
| 27 | #include <sw_sync.h> |
| 28 | #include <sync/sync.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
Sean Paul | c51b0c5 | 2015-06-10 14:32:51 -0400 | [diff] [blame] | 32 | static const bool kUseOverlayPlanes = true; |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 33 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 34 | DrmComposition::DrmComposition(DrmResources *drm, Importer *importer) |
| 35 | : drm_(drm), importer_(importer) { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 36 | for (DrmResources::PlaneIter iter = drm_->begin_planes(); |
| 37 | iter != drm_->end_planes(); ++iter) { |
| 38 | if ((*iter)->type() == DRM_PLANE_TYPE_PRIMARY) |
| 39 | primary_planes_.push_back(*iter); |
| 40 | else if (kUseOverlayPlanes && (*iter)->type() == DRM_PLANE_TYPE_OVERLAY) |
| 41 | overlay_planes_.push_back(*iter); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | DrmComposition::~DrmComposition() { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | int DrmComposition::Init() { |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 49 | for (DrmResources::ConnectorIter iter = drm_->begin_connectors(); |
| 50 | iter != drm_->end_connectors(); ++iter) { |
| 51 | int display = (*iter)->display(); |
| 52 | composition_map_[display].reset(new DrmDisplayComposition()); |
| 53 | if (!composition_map_[display]) { |
| 54 | ALOGE("Failed to allocate new display composition\n"); |
| 55 | return -ENOMEM; |
| 56 | } |
| 57 | int ret = composition_map_[(*iter)->display()]->Init(drm_, importer_); |
| 58 | if (ret) { |
| 59 | ALOGE("Failed to init display composition for %d", (*iter)->display()); |
| 60 | return ret; |
| 61 | } |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 62 | } |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | unsigned DrmComposition::GetRemainingLayers(int display, |
| 67 | unsigned num_needed) const { |
| 68 | DrmCrtc *crtc = drm_->GetCrtcForDisplay(display); |
| 69 | if (!crtc) { |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 70 | ALOGE("Failed to find crtc for display %d", display); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | unsigned num_planes = 0; |
| 75 | for (std::vector<DrmPlane *>::const_iterator iter = primary_planes_.begin(); |
| 76 | iter != primary_planes_.end(); ++iter) { |
| 77 | if ((*iter)->GetCrtcSupported(*crtc)) |
| 78 | ++num_planes; |
| 79 | } |
Sean Paul | 9099aa5 | 2015-07-09 17:51:50 -0400 | [diff] [blame] | 80 | for (std::vector<DrmPlane *>::const_iterator iter = overlay_planes_.begin(); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 81 | iter != overlay_planes_.end(); ++iter) { |
| 82 | if ((*iter)->GetCrtcSupported(*crtc)) |
| 83 | ++num_planes; |
| 84 | } |
| 85 | return std::min(num_planes, num_needed); |
| 86 | } |
| 87 | |
| 88 | int DrmComposition::AddLayer(int display, hwc_layer_1_t *layer, |
| 89 | hwc_drm_bo *bo) { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 90 | DrmCrtc *crtc = drm_->GetCrtcForDisplay(display); |
| 91 | if (!crtc) { |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 92 | ALOGE("Failed to find crtc for display %d", display); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 93 | return -ENODEV; |
| 94 | } |
| 95 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 96 | // Find a plane for the layer |
| 97 | DrmPlane *plane = NULL; |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 98 | for (std::vector<DrmPlane *>::iterator iter = primary_planes_.begin(); |
| 99 | iter != primary_planes_.end(); ++iter) { |
| 100 | if ((*iter)->GetCrtcSupported(*crtc)) { |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 101 | plane = *iter; |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 102 | primary_planes_.erase(iter); |
| 103 | break; |
| 104 | } |
| 105 | } |
Sean Paul | 9099aa5 | 2015-07-09 17:51:50 -0400 | [diff] [blame] | 106 | for (std::vector<DrmPlane *>::iterator iter = overlay_planes_.begin(); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 107 | !plane && iter != overlay_planes_.end(); ++iter) { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 108 | if ((*iter)->GetCrtcSupported(*crtc)) { |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 109 | plane = *iter; |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 110 | overlay_planes_.erase(iter); |
| 111 | break; |
| 112 | } |
| 113 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 114 | if (!plane) { |
| 115 | ALOGE("Failed to find plane for display %d", display); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 116 | return -ENOENT; |
| 117 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 118 | return composition_map_[display]->AddLayer(layer, bo, crtc, plane); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 121 | int DrmComposition::AddDpmsMode(int display, uint32_t dpms_mode) { |
| 122 | return composition_map_[display]->AddDpmsMode(dpms_mode); |
| 123 | } |
| 124 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 125 | std::unique_ptr<DrmDisplayComposition> DrmComposition::TakeDisplayComposition( |
| 126 | int display) { |
| 127 | return std::move(composition_map_[display]); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 128 | } |
Sean Paul | 2e46fbd | 2015-07-09 17:22:22 -0400 | [diff] [blame^] | 129 | |
| 130 | int DrmComposition::DisableUnusedPlanes() { |
| 131 | for (DrmResources::ConnectorIter iter = drm_->begin_connectors(); |
| 132 | iter != drm_->end_connectors(); ++iter) { |
| 133 | int display = (*iter)->display(); |
| 134 | DrmDisplayComposition *comp = GetDisplayComposition(display); |
| 135 | |
| 136 | /* |
| 137 | * Leave empty compositions alone |
| 138 | * TODO: re-visit this and potentially disable leftover planes after the |
| 139 | * active compositions have gobbled up all they can |
| 140 | */ |
| 141 | if (comp->type() == DRM_COMPOSITION_TYPE_EMPTY) |
| 142 | continue; |
| 143 | |
| 144 | DrmCrtc *crtc = drm_->GetCrtcForDisplay(display); |
| 145 | if (!crtc) { |
| 146 | ALOGE("Failed to find crtc for display %d", display); |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | for (std::vector<DrmPlane *>::iterator iter = primary_planes_.begin(); |
| 151 | iter != primary_planes_.end(); ++iter) { |
| 152 | if ((*iter)->GetCrtcSupported(*crtc)) { |
| 153 | comp->AddPlaneDisable(*iter); |
| 154 | primary_planes_.erase(iter); |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | for (std::vector<DrmPlane *>::iterator iter = overlay_planes_.begin(); |
| 159 | iter != overlay_planes_.end();) { |
| 160 | if ((*iter)->GetCrtcSupported(*crtc)) { |
| 161 | comp->AddPlaneDisable(*iter); |
| 162 | iter = overlay_planes_.erase(iter); |
| 163 | } else { |
| 164 | iter++; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | DrmDisplayComposition *DrmComposition::GetDisplayComposition(int display) { |
| 172 | return composition_map_[display].get(); |
| 173 | } |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 174 | } |