1 /*
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 <log/log.h>
21 #include <ui/Gralloc.h>
22 #include <ui/GraphicBufferMapper.h>
23
24 #include "bufferinfo/BufferInfoGetter.h"
25 #include "drm/DrmGenericImporter.h"
26 #include "drmhwcomposer.h"
27
28 #define UNUSED(x) (void)(x)
29
30 namespace android {
31
operator ->() const32 const 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
Clear()41 void DrmHwcBuffer::Clear() {
42 if (importer_ != NULL) {
43 importer_->ReleaseBuffer(&bo_);
44 importer_ = NULL;
45 }
46 }
47
ImportBuffer(buffer_handle_t handle,Importer * importer)48 int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
49 hwc_drm_bo tmp_bo{};
50
51 int ret = BufferInfoGetter::GetInstance()->ConvertBoInfo(handle, &tmp_bo);
52 if (ret) {
53 ALOGE("Failed to convert buffer info %d", ret);
54 return ret;
55 }
56
57 ret = importer->ImportBuffer(&tmp_bo);
58 if (ret) {
59 ALOGE("Failed to import buffer %d", ret);
60 return ret;
61 }
62
63 if (importer_ != NULL) {
64 importer_->ReleaseBuffer(&bo_);
65 }
66
67 importer_ = importer;
68
69 bo_ = tmp_bo;
70
71 return 0;
72 }
73
CopyBufferHandle(buffer_handle_t handle)74 int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle) {
75 native_handle_t *handle_copy;
76 GraphicBufferMapper &gm(GraphicBufferMapper::get());
77 int ret;
78
79 ret = gm.getGrallocMapper().importBuffer(handle,
80 const_cast<buffer_handle_t *>(
81 &handle_copy));
82
83 if (ret) {
84 ALOGE("Failed to import buffer handle %d", ret);
85 return ret;
86 }
87
88 Clear();
89
90 handle_ = handle_copy;
91
92 return 0;
93 }
94
~DrmHwcNativeHandle()95 DrmHwcNativeHandle::~DrmHwcNativeHandle() {
96 Clear();
97 }
98
Clear()99 void DrmHwcNativeHandle::Clear() {
100 if (handle_ != NULL) {
101 GraphicBufferMapper &gm(GraphicBufferMapper::get());
102 int ret = gm.freeBuffer(handle_);
103 if (ret) {
104 ALOGE("Failed to free buffer handle %d", ret);
105 }
106 handle_ = NULL;
107 }
108 }
109
ImportBuffer(Importer * importer)110 int DrmHwcLayer::ImportBuffer(Importer *importer) {
111 int ret = buffer.ImportBuffer(sf_handle, importer);
112 if (ret)
113 return ret;
114
115 const hwc_drm_bo *bo = buffer.operator->();
116
117 ret = handle.CopyBufferHandle(sf_handle);
118 if (ret)
119 return ret;
120
121 gralloc_buffer_usage = bo->usage;
122
123 return 0;
124 }
125
InitFromDrmHwcLayer(DrmHwcLayer * src_layer,Importer * importer)126 int DrmHwcLayer::InitFromDrmHwcLayer(DrmHwcLayer *src_layer,
127 Importer *importer) {
128 blending = src_layer->blending;
129 sf_handle = src_layer->sf_handle;
130 acquire_fence = -1;
131 display_frame = src_layer->display_frame;
132 alpha = src_layer->alpha;
133 source_crop = src_layer->source_crop;
134 transform = src_layer->transform;
135 return ImportBuffer(importer);
136 }
137
SetSourceCrop(hwc_frect_t const & crop)138 void DrmHwcLayer::SetSourceCrop(hwc_frect_t const &crop) {
139 source_crop = crop;
140 }
141
SetDisplayFrame(hwc_rect_t const & frame)142 void DrmHwcLayer::SetDisplayFrame(hwc_rect_t const &frame) {
143 display_frame = frame;
144 }
145
SetTransform(int32_t sf_transform)146 void DrmHwcLayer::SetTransform(int32_t sf_transform) {
147 transform = 0;
148 // 270* and 180* cannot be combined with flips. More specifically, they
149 // already contain both horizontal and vertical flips, so those fields are
150 // redundant in this case. 90* rotation can be combined with either horizontal
151 // flip or vertical flip, so treat it differently
152 if (sf_transform == HWC_TRANSFORM_ROT_270) {
153 transform = DrmHwcTransform::kRotate270;
154 } else if (sf_transform == HWC_TRANSFORM_ROT_180) {
155 transform = DrmHwcTransform::kRotate180;
156 } else {
157 if (sf_transform & HWC_TRANSFORM_FLIP_H)
158 transform |= DrmHwcTransform::kFlipH;
159 if (sf_transform & HWC_TRANSFORM_FLIP_V)
160 transform |= DrmHwcTransform::kFlipV;
161 if (sf_transform & HWC_TRANSFORM_ROT_90)
162 transform |= DrmHwcTransform::kRotate90;
163 }
164 }
165 } // namespace android
166