blob: 7ba382a962d81228c00d21cebf97fac96d96fd96 [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
20#include "drmhwcomposer.h"
21#include "platform.h"
22
John Stultz9057a6f2018-04-26 12:05:55 -070023#include <log/log.h>
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020024#include <ui/GraphicBufferMapper.h>
Sean Paul80b1a5d2016-03-10 15:35:13 -050025
26namespace android {
27
28const hwc_drm_bo *DrmHwcBuffer::operator->() const {
29 if (importer_ == NULL) {
30 ALOGE("Access of non-existent BO");
31 exit(1);
32 return NULL;
33 }
34 return &bo_;
35}
36
37void DrmHwcBuffer::Clear() {
38 if (importer_ != NULL) {
39 importer_->ReleaseBuffer(&bo_);
40 importer_ = NULL;
41 }
42}
43
44int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
45 hwc_drm_bo tmp_bo;
46
47 int ret = importer->ImportBuffer(handle, &tmp_bo);
48 if (ret)
49 return ret;
50
51 if (importer_ != NULL) {
52 importer_->ReleaseBuffer(&bo_);
53 }
54
55 importer_ = importer;
56
57 bo_ = tmp_bo;
58
59 return 0;
60}
61
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020062int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle) {
63 native_handle_t *handle_copy;
64 GraphicBufferMapper &gm(GraphicBufferMapper::get());
65 int ret =
66 gm.importBuffer(handle, const_cast<buffer_handle_t *>(&handle_copy));
Sean Paul80b1a5d2016-03-10 15:35:13 -050067 if (ret) {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020068 ALOGE("Failed to import buffer handle %d", ret);
Sean Paul80b1a5d2016-03-10 15:35:13 -050069 return ret;
70 }
71
72 Clear();
73
Sean Paul80b1a5d2016-03-10 15:35:13 -050074 handle_ = handle_copy;
75
76 return 0;
77}
78
79DrmHwcNativeHandle::~DrmHwcNativeHandle() {
80 Clear();
81}
82
83void DrmHwcNativeHandle::Clear() {
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020084 if (handle_ != NULL) {
85 GraphicBufferMapper &gm(GraphicBufferMapper::get());
86 int ret = gm.freeBuffer(handle_);
87 if (ret) {
88 ALOGE("Failed to free buffer handle %d", ret);
89 }
Sean Paul80b1a5d2016-03-10 15:35:13 -050090 handle_ = NULL;
91 }
92}
93
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020094int DrmHwcLayer::ImportBuffer(Importer *importer) {
Sean Paul80b1a5d2016-03-10 15:35:13 -050095 int ret = buffer.ImportBuffer(sf_handle, importer);
96 if (ret)
97 return ret;
98
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +020099 ret = handle.CopyBufferHandle(sf_handle);
Sean Paul80b1a5d2016-03-10 15:35:13 -0500100 if (ret)
101 return ret;
102
Rob Herringaeccd892017-10-06 17:20:05 -0500103 gralloc_buffer_usage = buffer.operator->()->usage;
104
Sean Paul80b1a5d2016-03-10 15:35:13 -0500105 return 0;
106}
107
108void DrmHwcLayer::SetSourceCrop(hwc_frect_t const &crop) {
109 source_crop = DrmHwcRect<float>(crop.left, crop.top, crop.right, crop.bottom);
110}
111
112void DrmHwcLayer::SetDisplayFrame(hwc_rect_t const &frame) {
113 display_frame =
114 DrmHwcRect<int>(frame.left, frame.top, frame.right, frame.bottom);
115}
116
117void DrmHwcLayer::SetTransform(int32_t sf_transform) {
118 transform = 0;
119 // 270* and 180* cannot be combined with flips. More specifically, they
120 // already contain both horizontal and vertical flips, so those fields are
121 // redundant in this case. 90* rotation can be combined with either horizontal
122 // flip or vertical flip, so treat it differently
123 if (sf_transform == HWC_TRANSFORM_ROT_270) {
124 transform = DrmHwcTransform::kRotate270;
125 } else if (sf_transform == HWC_TRANSFORM_ROT_180) {
126 transform = DrmHwcTransform::kRotate180;
127 } else {
128 if (sf_transform & HWC_TRANSFORM_FLIP_H)
129 transform |= DrmHwcTransform::kFlipH;
130 if (sf_transform & HWC_TRANSFORM_FLIP_V)
131 transform |= DrmHwcTransform::kFlipV;
132 if (sf_transform & HWC_TRANSFORM_ROT_90)
133 transform |= DrmHwcTransform::kRotate90;
134 }
135}
136}