blob: 2cd46fa93b400eb8541e4d4317584e10573d160d [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>
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020021#include <ui/GraphicBufferMapper.h>
Sean Paul80b1a5d2016-03-10 15:35:13 -050022
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030023#include "bufferinfo/BufferInfoGetter.h"
24#include "drm/DrmGenericImporter.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030025#include "drmhwcomposer.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030026
John Stultzfb3599c2018-08-21 11:21:56 -070027#define UNUSED(x) (void)(x)
28
Sean Paul80b1a5d2016-03-10 15:35:13 -050029namespace android {
30
31const 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
40void DrmHwcBuffer::Clear() {
41 if (importer_ != NULL) {
42 importer_->ReleaseBuffer(&bo_);
43 importer_ = NULL;
44 }
45}
46
47int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030048 hwc_drm_bo tmp_bo{};
Sean Paul80b1a5d2016-03-10 15:35:13 -050049
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030050 BufferInfoGetter::GetInstance()->ConvertBoInfo(handle, &tmp_bo);
51
52 int ret = importer->ImportBuffer(&tmp_bo);
Sean Paul80b1a5d2016-03-10 15:35:13 -050053 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 Stultzfb3599c2018-08-21 11:21:56 -070067int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle, int width,
68 int height, int layerCount, int format,
69 int usage, int stride) {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020070 native_handle_t *handle_copy;
71 GraphicBufferMapper &gm(GraphicBufferMapper::get());
John Stultzfb3599c2018-08-21 11:21:56 -070072 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 Paul80b1a5d2016-03-10 15:35:13 -050086 if (ret) {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020087 ALOGE("Failed to import buffer handle %d", ret);
Sean Paul80b1a5d2016-03-10 15:35:13 -050088 return ret;
89 }
90
91 Clear();
92
Sean Paul80b1a5d2016-03-10 15:35:13 -050093 handle_ = handle_copy;
94
95 return 0;
96}
97
98DrmHwcNativeHandle::~DrmHwcNativeHandle() {
99 Clear();
100}
101
102void DrmHwcNativeHandle::Clear() {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200103 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 Paul80b1a5d2016-03-10 15:35:13 -0500109 handle_ = NULL;
110 }
111}
112
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200113int DrmHwcLayer::ImportBuffer(Importer *importer) {
Sean Paul80b1a5d2016-03-10 15:35:13 -0500114 int ret = buffer.ImportBuffer(sf_handle, importer);
115 if (ret)
116 return ret;
117
John Stultzfb3599c2018-08-21 11:21:56 -0700118 const hwc_drm_bo *bo = buffer.operator->();
119
Roman Stratiienkocfc2b2e2020-09-01 13:39:17 +0300120 ret = handle.CopyBufferHandle(sf_handle, bo->width, bo->height,
121 1 /*layer_count*/, bo->hal_format, bo->usage,
122 bo->pixel_stride);
Sean Paul80b1a5d2016-03-10 15:35:13 -0500123 if (ret)
124 return ret;
125
John Stultzfb3599c2018-08-21 11:21:56 -0700126 gralloc_buffer_usage = bo->usage;
Rob Herringaeccd892017-10-06 17:20:05 -0500127
Sean Paul80b1a5d2016-03-10 15:35:13 -0500128 return 0;
129}
130
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100131int 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 Paul80b1a5d2016-03-10 15:35:13 -0500143void DrmHwcLayer::SetSourceCrop(hwc_frect_t const &crop) {
Rob Herringcff7b1e2018-05-09 15:18:36 -0500144 source_crop = crop;
Sean Paul80b1a5d2016-03-10 15:35:13 -0500145}
146
147void DrmHwcLayer::SetDisplayFrame(hwc_rect_t const &frame) {
Rob Herringcff7b1e2018-05-09 15:18:36 -0500148 display_frame = frame;
Sean Paul80b1a5d2016-03-10 15:35:13 -0500149}
150
151void 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 Paulf72cccd2018-08-27 13:59:08 -0400170} // namespace android