blob: 35c9fb6b03adb31157d49583f20146682d275759 [file] [log] [blame]
Sean Paul80b1a5d2016-03-10 15:35:13 -05001/*
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 Stultz9057a6f2018-04-26 12:05:55 -070020#include <log/log.h>
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +030021#include <ui/Gralloc.h>
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020022#include <ui/GraphicBufferMapper.h>
Sean Paul80b1a5d2016-03-10 15:35:13 -050023
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030024#include "bufferinfo/BufferInfoGetter.h"
25#include "drm/DrmGenericImporter.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030026#include "drmhwcomposer.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030027
John Stultzfb3599c2018-08-21 11:21:56 -070028#define UNUSED(x) (void)(x)
29
Sean Paul80b1a5d2016-03-10 15:35:13 -050030namespace android {
31
32const hwc_drm_bo *DrmHwcBuffer::operator->() const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020033 if (importer_ == nullptr) {
Sean Paul80b1a5d2016-03-10 15:35:13 -050034 ALOGE("Access of non-existent BO");
35 exit(1);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020036 return nullptr;
Sean Paul80b1a5d2016-03-10 15:35:13 -050037 }
38 return &bo_;
39}
40
41void DrmHwcBuffer::Clear() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020042 if (importer_ != nullptr) {
Sean Paul80b1a5d2016-03-10 15:35:13 -050043 importer_->ReleaseBuffer(&bo_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020044 importer_ = nullptr;
Sean Paul80b1a5d2016-03-10 15:35:13 -050045 }
46}
47
48int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030049 hwc_drm_bo tmp_bo{};
Sean Paul80b1a5d2016-03-10 15:35:13 -050050
Jason Macnakf2e03b72020-11-02 07:04:11 -080051 int ret = BufferInfoGetter::GetInstance()->ConvertBoInfo(handle, &tmp_bo);
52 if (ret) {
53 ALOGE("Failed to convert buffer info %d", ret);
Sean Paul80b1a5d2016-03-10 15:35:13 -050054 return ret;
Jason Macnakf2e03b72020-11-02 07:04:11 -080055 }
56
57 ret = importer->ImportBuffer(&tmp_bo);
58 if (ret) {
59 ALOGE("Failed to import buffer %d", ret);
60 return ret;
61 }
Sean Paul80b1a5d2016-03-10 15:35:13 -050062
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020063 if (importer_ != nullptr) {
Sean Paul80b1a5d2016-03-10 15:35:13 -050064 importer_->ReleaseBuffer(&bo_);
65 }
66
67 importer_ = importer;
68
69 bo_ = tmp_bo;
70
71 return 0;
72}
73
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +030074int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020075 native_handle_t *handle_copy = nullptr;
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020076 GraphicBufferMapper &gm(GraphicBufferMapper::get());
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020077 int ret = 0;
John Stultzfb3599c2018-08-21 11:21:56 -070078
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +030079 ret = gm.getGrallocMapper().importBuffer(handle,
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020080 (buffer_handle_t *)&handle_copy);
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +030081
Sean Paul80b1a5d2016-03-10 15:35:13 -050082 if (ret) {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020083 ALOGE("Failed to import buffer handle %d", ret);
Sean Paul80b1a5d2016-03-10 15:35:13 -050084 return ret;
85 }
86
87 Clear();
88
Sean Paul80b1a5d2016-03-10 15:35:13 -050089 handle_ = handle_copy;
90
91 return 0;
92}
93
94DrmHwcNativeHandle::~DrmHwcNativeHandle() {
95 Clear();
96}
97
98void DrmHwcNativeHandle::Clear() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020099 if (handle_ != nullptr) {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200100 GraphicBufferMapper &gm(GraphicBufferMapper::get());
101 int ret = gm.freeBuffer(handle_);
102 if (ret) {
103 ALOGE("Failed to free buffer handle %d", ret);
104 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200105 handle_ = nullptr;
Sean Paul80b1a5d2016-03-10 15:35:13 -0500106 }
107}
108
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200109int DrmHwcLayer::ImportBuffer(Importer *importer) {
Sean Paul80b1a5d2016-03-10 15:35:13 -0500110 int ret = buffer.ImportBuffer(sf_handle, importer);
111 if (ret)
112 return ret;
113
John Stultzfb3599c2018-08-21 11:21:56 -0700114 const hwc_drm_bo *bo = buffer.operator->();
115
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +0300116 ret = handle.CopyBufferHandle(sf_handle);
Sean Paul80b1a5d2016-03-10 15:35:13 -0500117 if (ret)
118 return ret;
119
John Stultzfb3599c2018-08-21 11:21:56 -0700120 gralloc_buffer_usage = bo->usage;
Rob Herringaeccd892017-10-06 17:20:05 -0500121
Sean Paul80b1a5d2016-03-10 15:35:13 -0500122 return 0;
123}
124
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100125int DrmHwcLayer::InitFromDrmHwcLayer(DrmHwcLayer *src_layer,
126 Importer *importer) {
127 blending = src_layer->blending;
128 sf_handle = src_layer->sf_handle;
129 acquire_fence = -1;
130 display_frame = src_layer->display_frame;
131 alpha = src_layer->alpha;
132 source_crop = src_layer->source_crop;
133 transform = src_layer->transform;
134 return ImportBuffer(importer);
135}
136
Sean Paul80b1a5d2016-03-10 15:35:13 -0500137void DrmHwcLayer::SetSourceCrop(hwc_frect_t const &crop) {
Rob Herringcff7b1e2018-05-09 15:18:36 -0500138 source_crop = crop;
Sean Paul80b1a5d2016-03-10 15:35:13 -0500139}
140
141void DrmHwcLayer::SetDisplayFrame(hwc_rect_t const &frame) {
Rob Herringcff7b1e2018-05-09 15:18:36 -0500142 display_frame = frame;
Sean Paul80b1a5d2016-03-10 15:35:13 -0500143}
144
145void DrmHwcLayer::SetTransform(int32_t sf_transform) {
146 transform = 0;
147 // 270* and 180* cannot be combined with flips. More specifically, they
148 // already contain both horizontal and vertical flips, so those fields are
149 // redundant in this case. 90* rotation can be combined with either horizontal
150 // flip or vertical flip, so treat it differently
151 if (sf_transform == HWC_TRANSFORM_ROT_270) {
152 transform = DrmHwcTransform::kRotate270;
153 } else if (sf_transform == HWC_TRANSFORM_ROT_180) {
154 transform = DrmHwcTransform::kRotate180;
155 } else {
156 if (sf_transform & HWC_TRANSFORM_FLIP_H)
157 transform |= DrmHwcTransform::kFlipH;
158 if (sf_transform & HWC_TRANSFORM_FLIP_V)
159 transform |= DrmHwcTransform::kFlipV;
160 if (sf_transform & HWC_TRANSFORM_ROT_90)
161 transform |= DrmHwcTransform::kRotate90;
162 }
163}
Sean Paulf72cccd2018-08-27 13:59:08 -0400164} // namespace android