Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | #define LOG_TAG "hwc-drm-utils" |
| 19 | |
| 20 | #include "drmhwcomposer.h" |
| 21 | #include "platform.h" |
| 22 | |
John Stultz | 9057a6f | 2018-04-26 12:05:55 -0700 | [diff] [blame] | 23 | #include <log/log.h> |
Andrii Chepurnyi | dc1278c | 2018-03-20 19:41:18 +0200 | [diff] [blame] | 24 | #include <ui/GraphicBufferMapper.h> |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 25 | |
John Stultz | fb3599c | 2018-08-21 11:21:56 -0700 | [diff] [blame] | 26 | #define UNUSED(x) (void)(x) |
| 27 | |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 28 | namespace android { |
| 29 | |
| 30 | const hwc_drm_bo *DrmHwcBuffer::operator->() const { |
| 31 | if (importer_ == NULL) { |
| 32 | ALOGE("Access of non-existent BO"); |
| 33 | exit(1); |
| 34 | return NULL; |
| 35 | } |
| 36 | return &bo_; |
| 37 | } |
| 38 | |
| 39 | void DrmHwcBuffer::Clear() { |
| 40 | if (importer_ != NULL) { |
| 41 | importer_->ReleaseBuffer(&bo_); |
| 42 | importer_ = NULL; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) { |
| 47 | hwc_drm_bo tmp_bo; |
| 48 | |
| 49 | int ret = importer->ImportBuffer(handle, &tmp_bo); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | |
| 53 | if (importer_ != NULL) { |
| 54 | importer_->ReleaseBuffer(&bo_); |
| 55 | } |
| 56 | |
| 57 | importer_ = importer; |
| 58 | |
| 59 | bo_ = tmp_bo; |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
John Stultz | fb3599c | 2018-08-21 11:21:56 -0700 | [diff] [blame] | 64 | int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle, int width, |
| 65 | int height, int layerCount, int format, |
| 66 | int usage, int stride) { |
Andrii Chepurnyi | dc1278c | 2018-03-20 19:41:18 +0200 | [diff] [blame] | 67 | native_handle_t *handle_copy; |
| 68 | GraphicBufferMapper &gm(GraphicBufferMapper::get()); |
John Stultz | fb3599c | 2018-08-21 11:21:56 -0700 | [diff] [blame] | 69 | int ret; |
| 70 | |
| 71 | #ifdef HWC2_USE_OLD_GB_IMPORT |
| 72 | UNUSED(width); |
| 73 | UNUSED(height); |
| 74 | UNUSED(layerCount); |
| 75 | UNUSED(format); |
| 76 | UNUSED(usage); |
| 77 | UNUSED(stride); |
| 78 | ret = gm.importBuffer(handle, const_cast<buffer_handle_t *>(&handle_copy)); |
| 79 | #else |
| 80 | ret = gm.importBuffer(handle, width, height, layerCount, format, usage, |
| 81 | stride, const_cast<buffer_handle_t *>(&handle_copy)); |
| 82 | #endif |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 83 | if (ret) { |
Andrii Chepurnyi | dc1278c | 2018-03-20 19:41:18 +0200 | [diff] [blame] | 84 | ALOGE("Failed to import buffer handle %d", ret); |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | Clear(); |
| 89 | |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 90 | handle_ = handle_copy; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | DrmHwcNativeHandle::~DrmHwcNativeHandle() { |
| 96 | Clear(); |
| 97 | } |
| 98 | |
| 99 | void DrmHwcNativeHandle::Clear() { |
Andrii Chepurnyi | dc1278c | 2018-03-20 19:41:18 +0200 | [diff] [blame] | 100 | if (handle_ != NULL) { |
| 101 | GraphicBufferMapper &gm(GraphicBufferMapper::get()); |
| 102 | int ret = gm.freeBuffer(handle_); |
| 103 | if (ret) { |
| 104 | ALOGE("Failed to free buffer handle %d", ret); |
| 105 | } |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 106 | handle_ = NULL; |
| 107 | } |
| 108 | } |
| 109 | |
Andrii Chepurnyi | dc1278c | 2018-03-20 19:41:18 +0200 | [diff] [blame] | 110 | int DrmHwcLayer::ImportBuffer(Importer *importer) { |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 111 | int ret = buffer.ImportBuffer(sf_handle, importer); |
| 112 | if (ret) |
| 113 | return ret; |
| 114 | |
John Stultz | fb3599c | 2018-08-21 11:21:56 -0700 | [diff] [blame] | 115 | const hwc_drm_bo *bo = buffer.operator->(); |
| 116 | |
Roman Stratiienko | cfc2b2e | 2020-09-01 13:39:17 +0300 | [diff] [blame^] | 117 | ret = handle.CopyBufferHandle(sf_handle, bo->width, bo->height, |
| 118 | 1 /*layer_count*/, bo->hal_format, bo->usage, |
| 119 | bo->pixel_stride); |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 120 | if (ret) |
| 121 | return ret; |
| 122 | |
John Stultz | fb3599c | 2018-08-21 11:21:56 -0700 | [diff] [blame] | 123 | gralloc_buffer_usage = bo->usage; |
Rob Herring | aeccd89 | 2017-10-06 17:20:05 -0500 | [diff] [blame] | 124 | |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 128 | int DrmHwcLayer::InitFromDrmHwcLayer(DrmHwcLayer *src_layer, |
| 129 | Importer *importer) { |
| 130 | blending = src_layer->blending; |
| 131 | sf_handle = src_layer->sf_handle; |
| 132 | acquire_fence = -1; |
| 133 | display_frame = src_layer->display_frame; |
| 134 | alpha = src_layer->alpha; |
| 135 | source_crop = src_layer->source_crop; |
| 136 | transform = src_layer->transform; |
| 137 | return ImportBuffer(importer); |
| 138 | } |
| 139 | |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 140 | void DrmHwcLayer::SetSourceCrop(hwc_frect_t const &crop) { |
Rob Herring | cff7b1e | 2018-05-09 15:18:36 -0500 | [diff] [blame] | 141 | source_crop = crop; |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void DrmHwcLayer::SetDisplayFrame(hwc_rect_t const &frame) { |
Rob Herring | cff7b1e | 2018-05-09 15:18:36 -0500 | [diff] [blame] | 145 | display_frame = frame; |
Sean Paul | 80b1a5d | 2016-03-10 15:35:13 -0500 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void DrmHwcLayer::SetTransform(int32_t sf_transform) { |
| 149 | transform = 0; |
| 150 | // 270* and 180* cannot be combined with flips. More specifically, they |
| 151 | // already contain both horizontal and vertical flips, so those fields are |
| 152 | // redundant in this case. 90* rotation can be combined with either horizontal |
| 153 | // flip or vertical flip, so treat it differently |
| 154 | if (sf_transform == HWC_TRANSFORM_ROT_270) { |
| 155 | transform = DrmHwcTransform::kRotate270; |
| 156 | } else if (sf_transform == HWC_TRANSFORM_ROT_180) { |
| 157 | transform = DrmHwcTransform::kRotate180; |
| 158 | } else { |
| 159 | if (sf_transform & HWC_TRANSFORM_FLIP_H) |
| 160 | transform |= DrmHwcTransform::kFlipH; |
| 161 | if (sf_transform & HWC_TRANSFORM_FLIP_V) |
| 162 | transform |= DrmHwcTransform::kFlipV; |
| 163 | if (sf_transform & HWC_TRANSFORM_ROT_90) |
| 164 | transform |= DrmHwcTransform::kRotate90; |
| 165 | } |
| 166 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 167 | } // namespace android |