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