blob: 31b781a8525f372f96a20fd93f95f17750e7cd9b [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
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100108int DrmHwcLayer::InitFromDrmHwcLayer(DrmHwcLayer *src_layer,
109 Importer *importer) {
110 blending = src_layer->blending;
111 sf_handle = src_layer->sf_handle;
112 acquire_fence = -1;
113 display_frame = src_layer->display_frame;
114 alpha = src_layer->alpha;
115 source_crop = src_layer->source_crop;
116 transform = src_layer->transform;
117 return ImportBuffer(importer);
118}
119
Sean Paul80b1a5d2016-03-10 15:35:13 -0500120void DrmHwcLayer::SetSourceCrop(hwc_frect_t const &crop) {
Rob Herringcff7b1e2018-05-09 15:18:36 -0500121 source_crop = crop;
Sean Paul80b1a5d2016-03-10 15:35:13 -0500122}
123
124void DrmHwcLayer::SetDisplayFrame(hwc_rect_t const &frame) {
Rob Herringcff7b1e2018-05-09 15:18:36 -0500125 display_frame = frame;
Sean Paul80b1a5d2016-03-10 15:35:13 -0500126}
127
128void DrmHwcLayer::SetTransform(int32_t sf_transform) {
129 transform = 0;
130 // 270* and 180* cannot be combined with flips. More specifically, they
131 // already contain both horizontal and vertical flips, so those fields are
132 // redundant in this case. 90* rotation can be combined with either horizontal
133 // flip or vertical flip, so treat it differently
134 if (sf_transform == HWC_TRANSFORM_ROT_270) {
135 transform = DrmHwcTransform::kRotate270;
136 } else if (sf_transform == HWC_TRANSFORM_ROT_180) {
137 transform = DrmHwcTransform::kRotate180;
138 } else {
139 if (sf_transform & HWC_TRANSFORM_FLIP_H)
140 transform |= DrmHwcTransform::kFlipH;
141 if (sf_transform & HWC_TRANSFORM_FLIP_V)
142 transform |= DrmHwcTransform::kFlipV;
143 if (sf_transform & HWC_TRANSFORM_ROT_90)
144 transform |= DrmHwcTransform::kRotate90;
145 }
146}
147}