Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -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-display-composition" |
| 18 | |
| 19 | #include "drmdisplaycomposition.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> |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 29 | #include <xf86drmMode.h> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | DrmCompositionLayer::DrmCompositionLayer() : crtc(NULL), plane(NULL) { |
| 34 | memset(&layer, 0, sizeof(layer)); |
| 35 | layer.acquireFenceFd = -1; |
| 36 | memset(&bo, 0, sizeof(bo)); |
| 37 | } |
| 38 | |
| 39 | DrmCompositionLayer::~DrmCompositionLayer() { |
| 40 | } |
| 41 | |
| 42 | DrmDisplayComposition::DrmDisplayComposition() |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 43 | : drm_(NULL), |
| 44 | importer_(NULL), |
| 45 | type_(DRM_COMPOSITION_TYPE_EMPTY), |
| 46 | timeline_fd_(-1), |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 47 | timeline_(0), |
Zach Reizner | ece0489 | 2015-07-17 14:13:28 -0700 | [diff] [blame] | 48 | timeline_current_(0), |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 49 | dpms_mode_(DRM_MODE_DPMS_ON) { |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | DrmDisplayComposition::~DrmDisplayComposition() { |
| 53 | for (DrmCompositionLayerVector_t::iterator iter = layers_.begin(); |
| 54 | iter != layers_.end(); ++iter) { |
Sean Paul | 2e46fbd | 2015-07-09 17:22:22 -0400 | [diff] [blame] | 55 | if (importer_ && iter->bo.fb_id) |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 56 | importer_->ReleaseBuffer(&iter->bo); |
| 57 | |
| 58 | if (iter->layer.acquireFenceFd >= 0) |
| 59 | close(iter->layer.acquireFenceFd); |
| 60 | } |
| 61 | |
Zach Reizner | ece0489 | 2015-07-17 14:13:28 -0700 | [diff] [blame] | 62 | if (timeline_fd_ >= 0) { |
| 63 | FinishComposition(); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 64 | close(timeline_fd_); |
Zach Reizner | ece0489 | 2015-07-17 14:13:28 -0700 | [diff] [blame] | 65 | timeline_fd_ = -1; |
| 66 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | int DrmDisplayComposition::Init(DrmResources *drm, Importer *importer) { |
| 70 | drm_ = drm; |
| 71 | importer_ = importer; |
| 72 | |
| 73 | int ret = sw_sync_timeline_create(); |
| 74 | if (ret < 0) { |
| 75 | ALOGE("Failed to create sw sync timeline %d", ret); |
| 76 | return ret; |
| 77 | } |
| 78 | timeline_fd_ = ret; |
| 79 | return 0; |
| 80 | } |
| 81 | |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 82 | DrmCompositionType DrmDisplayComposition::type() const { |
| 83 | return type_; |
| 84 | } |
| 85 | |
| 86 | bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) { |
| 87 | return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des; |
| 88 | } |
| 89 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 90 | int DrmDisplayComposition::AddLayer(hwc_layer_1_t *layer, hwc_drm_bo_t *bo, |
| 91 | DrmCrtc *crtc, DrmPlane *plane) { |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 92 | if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME)) |
| 93 | return -EINVAL; |
| 94 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 95 | ++timeline_; |
| 96 | layer->releaseFenceFd = |
| 97 | sw_sync_fence_create(timeline_fd_, "drm_fence", timeline_); |
| 98 | if (layer->releaseFenceFd < 0) { |
| 99 | ALOGE("Could not create release fence %d", layer->releaseFenceFd); |
| 100 | return layer->releaseFenceFd; |
| 101 | } |
| 102 | |
| 103 | DrmCompositionLayer_t c_layer; |
| 104 | c_layer.layer = *layer; |
| 105 | c_layer.bo = *bo; |
| 106 | c_layer.crtc = crtc; |
| 107 | c_layer.plane = plane; |
| 108 | |
| 109 | layer->acquireFenceFd = -1; // We own this now |
| 110 | layers_.push_back(c_layer); |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 111 | type_ = DRM_COMPOSITION_TYPE_FRAME; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 115 | int DrmDisplayComposition::AddDpmsMode(uint32_t dpms_mode) { |
| 116 | if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS)) |
| 117 | return -EINVAL; |
| 118 | dpms_mode_ = dpms_mode; |
| 119 | type_ = DRM_COMPOSITION_TYPE_DPMS; |
| 120 | return 0; |
| 121 | } |
| 122 | |
Sean Paul | 2e46fbd | 2015-07-09 17:22:22 -0400 | [diff] [blame] | 123 | int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) { |
| 124 | DrmCompositionLayer_t c_layer; |
| 125 | c_layer.crtc = NULL; |
| 126 | c_layer.plane = plane; |
| 127 | layers_.push_back(c_layer); |
| 128 | return 0; |
| 129 | } |
| 130 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 131 | int DrmDisplayComposition::FinishComposition() { |
Zach Reizner | ece0489 | 2015-07-17 14:13:28 -0700 | [diff] [blame] | 132 | int timeline_increase = timeline_ - timeline_current_; |
| 133 | if (timeline_increase <= 0) |
| 134 | return 0; |
| 135 | |
| 136 | int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 137 | if (ret) |
| 138 | ALOGE("Failed to increment sync timeline %d", ret); |
Zach Reizner | ece0489 | 2015-07-17 14:13:28 -0700 | [diff] [blame] | 139 | else |
| 140 | timeline_current_ = timeline_; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | DrmCompositionLayerVector_t *DrmDisplayComposition::GetCompositionLayers() { |
| 146 | return &layers_; |
| 147 | } |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 148 | |
| 149 | uint32_t DrmDisplayComposition::dpms_mode() const { |
| 150 | return dpms_mode_; |
| 151 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 152 | } |