blob: d9b8a8762dc83daac5d6c9364dd6984f69a181c7 [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"
Roman Stratiienko8666dc92021-02-09 17:49:55 +020025#include "drm/DrmFbImporter.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 Stratiienko8666dc92021-02-09 17:49:55 +020033 if (mDrmDevice == 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 Stratiienko8666dc92021-02-09 17:49:55 +020042 FbIdHandle.reset();
43 mDrmDevice = nullptr;
Sean Paul80b1a5d2016-03-10 15:35:13 -050044}
45
Roman Stratiienko8666dc92021-02-09 17:49:55 +020046int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, DrmDevice *drmDevice) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030047 hwc_drm_bo tmp_bo{};
Sean Paul80b1a5d2016-03-10 15:35:13 -050048
Jason Macnakf2e03b72020-11-02 07:04:11 -080049 int ret = BufferInfoGetter::GetInstance()->ConvertBoInfo(handle, &tmp_bo);
50 if (ret) {
51 ALOGE("Failed to convert buffer info %d", ret);
Sean Paul80b1a5d2016-03-10 15:35:13 -050052 return ret;
Jason Macnakf2e03b72020-11-02 07:04:11 -080053 }
54
Roman Stratiienko8666dc92021-02-09 17:49:55 +020055 FbIdHandle = drmDevice->GetDrmFbImporter().GetOrCreateFbId(&tmp_bo);
56 if (!FbIdHandle) {
57 ALOGE("Failed to import buffer");
58 return -EINVAL;
Jason Macnakf2e03b72020-11-02 07:04:11 -080059 }
Sean Paul80b1a5d2016-03-10 15:35:13 -050060
Roman Stratiienko8666dc92021-02-09 17:49:55 +020061 mDrmDevice = drmDevice;
Sean Paul80b1a5d2016-03-10 15:35:13 -050062 bo_ = tmp_bo;
63
64 return 0;
65}
66
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +030067int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020068 native_handle_t *handle_copy = nullptr;
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020069 GraphicBufferMapper &gm(GraphicBufferMapper::get());
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020070 int ret = 0;
John Stultzfb3599c2018-08-21 11:21:56 -070071
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +030072 ret = gm.getGrallocMapper().importBuffer(handle,
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020073 (buffer_handle_t *)&handle_copy);
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +030074
Sean Paul80b1a5d2016-03-10 15:35:13 -050075 if (ret) {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020076 ALOGE("Failed to import buffer handle %d", ret);
Sean Paul80b1a5d2016-03-10 15:35:13 -050077 return ret;
78 }
79
80 Clear();
81
Sean Paul80b1a5d2016-03-10 15:35:13 -050082 handle_ = handle_copy;
83
84 return 0;
85}
86
87DrmHwcNativeHandle::~DrmHwcNativeHandle() {
88 Clear();
89}
90
91void DrmHwcNativeHandle::Clear() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020092 if (handle_ != nullptr) {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020093 GraphicBufferMapper &gm(GraphicBufferMapper::get());
94 int ret = gm.freeBuffer(handle_);
95 if (ret) {
96 ALOGE("Failed to free buffer handle %d", ret);
97 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020098 handle_ = nullptr;
Sean Paul80b1a5d2016-03-10 15:35:13 -050099 }
100}
101
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200102int DrmHwcLayer::ImportBuffer(DrmDevice *drmDevice) {
103 int ret = buffer.ImportBuffer(sf_handle, drmDevice);
Sean Paul80b1a5d2016-03-10 15:35:13 -0500104 if (ret)
105 return ret;
106
John Stultzfb3599c2018-08-21 11:21:56 -0700107 const hwc_drm_bo *bo = buffer.operator->();
108
Roman Stratiienkoadd24cb2020-10-23 22:28:38 +0300109 ret = handle.CopyBufferHandle(sf_handle);
Sean Paul80b1a5d2016-03-10 15:35:13 -0500110 if (ret)
111 return ret;
112
John Stultzfb3599c2018-08-21 11:21:56 -0700113 gralloc_buffer_usage = bo->usage;
Rob Herringaeccd892017-10-06 17:20:05 -0500114
Sean Paul80b1a5d2016-03-10 15:35:13 -0500115 return 0;
116}
117
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100118int DrmHwcLayer::InitFromDrmHwcLayer(DrmHwcLayer *src_layer,
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200119 DrmDevice *drmDevice) {
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100120 blending = src_layer->blending;
121 sf_handle = src_layer->sf_handle;
122 acquire_fence = -1;
123 display_frame = src_layer->display_frame;
124 alpha = src_layer->alpha;
125 source_crop = src_layer->source_crop;
126 transform = src_layer->transform;
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200127 return ImportBuffer(drmDevice);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100128}
129
Sean Paul80b1a5d2016-03-10 15:35:13 -0500130void DrmHwcLayer::SetTransform(int32_t sf_transform) {
131 transform = 0;
132 // 270* and 180* cannot be combined with flips. More specifically, they
133 // already contain both horizontal and vertical flips, so those fields are
134 // redundant in this case. 90* rotation can be combined with either horizontal
135 // flip or vertical flip, so treat it differently
136 if (sf_transform == HWC_TRANSFORM_ROT_270) {
137 transform = DrmHwcTransform::kRotate270;
138 } else if (sf_transform == HWC_TRANSFORM_ROT_180) {
139 transform = DrmHwcTransform::kRotate180;
140 } else {
141 if (sf_transform & HWC_TRANSFORM_FLIP_H)
142 transform |= DrmHwcTransform::kFlipH;
143 if (sf_transform & HWC_TRANSFORM_FLIP_V)
144 transform |= DrmHwcTransform::kFlipV;
145 if (sf_transform & HWC_TRANSFORM_ROT_90)
146 transform |= DrmHwcTransform::kRotate90;
147 }
148}
Sean Paulf72cccd2018-08-27 13:59:08 -0400149} // namespace android