blob: 8cd5c65b924ef1e684a09ac1c5c1cee3f4398ddd [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 {
33 if (importer_ == NULL) {
34 ALOGE("Access of non-existent BO");
35 exit(1);
36 return NULL;
37 }
38 return &bo_;
39}
40
41void DrmHwcBuffer::Clear() {
42 if (importer_ != NULL) {
43 importer_->ReleaseBuffer(&bo_);
44 importer_ = NULL;
45 }
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
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030051 BufferInfoGetter::GetInstance()->ConvertBoInfo(handle, &tmp_bo);
52
53 int ret = importer->ImportBuffer(&tmp_bo);
Sean Paul80b1a5d2016-03-10 15:35:13 -050054 if (ret)
55 return ret;
56
57 if (importer_ != NULL) {
58 importer_->ReleaseBuffer(&bo_);
59 }
60
61 importer_ = importer;
62
63 bo_ = tmp_bo;
64
65 return 0;
66}
67
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +030068int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle) {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020069 native_handle_t *handle_copy;
70 GraphicBufferMapper &gm(GraphicBufferMapper::get());
John Stultzfb3599c2018-08-21 11:21:56 -070071 int ret;
72
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +030073 ret = gm.getGrallocMapper().importBuffer(handle,
74 const_cast<buffer_handle_t *>(
75 &handle_copy));
76
Sean Paul80b1a5d2016-03-10 15:35:13 -050077 if (ret) {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020078 ALOGE("Failed to import buffer handle %d", ret);
Sean Paul80b1a5d2016-03-10 15:35:13 -050079 return ret;
80 }
81
82 Clear();
83
Sean Paul80b1a5d2016-03-10 15:35:13 -050084 handle_ = handle_copy;
85
86 return 0;
87}
88
89DrmHwcNativeHandle::~DrmHwcNativeHandle() {
90 Clear();
91}
92
93void DrmHwcNativeHandle::Clear() {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020094 if (handle_ != NULL) {
95 GraphicBufferMapper &gm(GraphicBufferMapper::get());
96 int ret = gm.freeBuffer(handle_);
97 if (ret) {
98 ALOGE("Failed to free buffer handle %d", ret);
99 }
Sean Paul80b1a5d2016-03-10 15:35:13 -0500100 handle_ = NULL;
101 }
102}
103
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200104int DrmHwcLayer::ImportBuffer(Importer *importer) {
Sean Paul80b1a5d2016-03-10 15:35:13 -0500105 int ret = buffer.ImportBuffer(sf_handle, importer);
106 if (ret)
107 return ret;
108
John Stultzfb3599c2018-08-21 11:21:56 -0700109 const hwc_drm_bo *bo = buffer.operator->();
110
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +0300111 ret = handle.CopyBufferHandle(sf_handle);
Sean Paul80b1a5d2016-03-10 15:35:13 -0500112 if (ret)
113 return ret;
114
John Stultzfb3599c2018-08-21 11:21:56 -0700115 gralloc_buffer_usage = bo->usage;
Rob Herringaeccd892017-10-06 17:20:05 -0500116
Sean Paul80b1a5d2016-03-10 15:35:13 -0500117 return 0;
118}
119
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100120int DrmHwcLayer::InitFromDrmHwcLayer(DrmHwcLayer *src_layer,
121 Importer *importer) {
122 blending = src_layer->blending;
123 sf_handle = src_layer->sf_handle;
124 acquire_fence = -1;
125 display_frame = src_layer->display_frame;
126 alpha = src_layer->alpha;
127 source_crop = src_layer->source_crop;
128 transform = src_layer->transform;
129 return ImportBuffer(importer);
130}
131
Sean Paul80b1a5d2016-03-10 15:35:13 -0500132void DrmHwcLayer::SetSourceCrop(hwc_frect_t const &crop) {
Rob Herringcff7b1e2018-05-09 15:18:36 -0500133 source_crop = crop;
Sean Paul80b1a5d2016-03-10 15:35:13 -0500134}
135
136void DrmHwcLayer::SetDisplayFrame(hwc_rect_t const &frame) {
Rob Herringcff7b1e2018-05-09 15:18:36 -0500137 display_frame = frame;
Sean Paul80b1a5d2016-03-10 15:35:13 -0500138}
139
140void DrmHwcLayer::SetTransform(int32_t sf_transform) {
141 transform = 0;
142 // 270* and 180* cannot be combined with flips. More specifically, they
143 // already contain both horizontal and vertical flips, so those fields are
144 // redundant in this case. 90* rotation can be combined with either horizontal
145 // flip or vertical flip, so treat it differently
146 if (sf_transform == HWC_TRANSFORM_ROT_270) {
147 transform = DrmHwcTransform::kRotate270;
148 } else if (sf_transform == HWC_TRANSFORM_ROT_180) {
149 transform = DrmHwcTransform::kRotate180;
150 } else {
151 if (sf_transform & HWC_TRANSFORM_FLIP_H)
152 transform |= DrmHwcTransform::kFlipH;
153 if (sf_transform & HWC_TRANSFORM_FLIP_V)
154 transform |= DrmHwcTransform::kFlipV;
155 if (sf_transform & HWC_TRANSFORM_ROT_90)
156 transform |= DrmHwcTransform::kRotate90;
157 }
158}
Sean Paulf72cccd2018-08-27 13:59:08 -0400159} // namespace android