blob: 14904386625ba67dac3aa87099a9c2306ef94719 [file] [log] [blame]
Sean Paulcd36a9e2015-01-22 18:01:18 -05001/*
2 * Copyright (C) 2015 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
Sean Paulef8f1f92015-04-29 16:05:23 -040017#ifndef ANDROID_DRM_HWCOMPOSER_H_
18#define ANDROID_DRM_HWCOMPOSER_H_
19
Lauri Peltonen902c8942015-03-02 19:10:04 +020020#include <stdbool.h>
21#include <stdint.h>
22
Sean Paulef8f1f92015-04-29 16:05:23 -040023#include <hardware/hardware.h>
24#include <hardware/hwcomposer.h>
Zach Reizner4a253652015-09-10 18:30:54 -070025#include "seperate_rects.h"
Sean Paulef8f1f92015-04-29 16:05:23 -040026
Sean Paulcd36a9e2015-01-22 18:01:18 -050027struct hwc_import_context;
28
Lauri Peltonenceaadea2015-02-02 14:45:13 +020029enum {
Sean Paulef8f1f92015-04-29 16:05:23 -040030 /* perform(const struct gralloc_module_t *mod,
31 * int op,
32 * int drm_fd,
33 * buffer_handle_t buffer,
34 * struct hwc_drm_bo *bo);
35 */
36 GRALLOC_MODULE_PERFORM_DRM_IMPORT = 0xffeeff00,
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +020037
Sean Paulef8f1f92015-04-29 16:05:23 -040038 /* perform(const struct gralloc_module_t *mod,
39 * int op,
40 * buffer_handle_t buffer,
41 * void (*free_callback)(void *),
42 * void *priv);
43 */
44 GRALLOC_MODULE_PERFORM_SET_IMPORTER_PRIVATE = 0xffeeff01,
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +020045
Sean Paulef8f1f92015-04-29 16:05:23 -040046 /* perform(const struct gralloc_module_t *mod,
47 * int op,
48 * buffer_handle_t buffer,
49 * void (*free_callback)(void *),
50 * void **priv);
51 */
52 GRALLOC_MODULE_PERFORM_GET_IMPORTER_PRIVATE = 0xffeeff02,
Lauri Peltonenceaadea2015-02-02 14:45:13 +020053};
54
Sean Paulef8f1f92015-04-29 16:05:23 -040055typedef struct hwc_drm_bo {
56 uint32_t width;
57 uint32_t height;
58 uint32_t format; /* DRM_FORMAT_* from drm_fourcc.h */
59 uint32_t pitches[4];
60 uint32_t offsets[4];
61 uint32_t gem_handles[4];
62 uint32_t fb_id;
63 int acquire_fence_fd;
Sean Paul419b5e02015-06-10 14:30:47 -040064 void *priv;
Sean Paulef8f1f92015-04-29 16:05:23 -040065} hwc_drm_bo_t;
Sean Paulcd36a9e2015-01-22 18:01:18 -050066
67int hwc_import_init(struct hwc_import_context **ctx);
68int hwc_import_destroy(struct hwc_import_context *ctx);
69
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +020070int hwc_import_bo_create(int fd, struct hwc_import_context *ctx,
Sean Paulef8f1f92015-04-29 16:05:23 -040071 buffer_handle_t buf, struct hwc_drm_bo *bo);
Lauri Peltonen902c8942015-03-02 19:10:04 +020072bool hwc_import_bo_release(int fd, struct hwc_import_context *ctx,
Sean Paulef8f1f92015-04-29 16:05:23 -040073 struct hwc_drm_bo *bo);
74
Zach Reizner4a253652015-09-10 18:30:54 -070075namespace android {
76
77class Importer;
78
79class UniqueFd {
80 public:
81 UniqueFd() = default;
82 UniqueFd(int fd) : fd_(fd) {
83 }
84 UniqueFd(UniqueFd &&rhs) {
85 fd_ = rhs.fd_;
86 rhs.fd_ = -1;
87 }
88
89 UniqueFd &operator=(UniqueFd &&rhs) {
90 Set(rhs.Release());
91 return *this;
92 }
93
94 ~UniqueFd() {
95 if (fd_ >= 0)
96 close(fd_);
97 }
98
99 int Release() {
100 int old_fd = fd_;
101 fd_ = -1;
102 return old_fd;
103 }
104
105 int Set(int fd) {
106 if (fd_ >= 0)
107 close(fd_);
108 fd_ = fd;
109 return fd_;
110 }
111
112 void Close() {
113 if (fd_ >= 0)
114 close(fd_);
115 fd_ = -1;
116 }
117
118 int get() {
119 return fd_;
120 }
121
122 private:
123 int fd_ = -1;
124};
125
126struct OutputFd {
127 OutputFd() = default;
128 OutputFd(int *fd) : fd_(fd) {
129 }
130 OutputFd(OutputFd &&rhs) {
131 fd_ = rhs.fd_;
132 rhs.fd_ = NULL;
133 }
134
135 OutputFd &operator=(OutputFd &&rhs);
136
137 int Set(int fd) {
138 if (*fd_ >= 0)
139 close(*fd_);
140 *fd_ = fd;
141 return fd;
142 }
143
144 int get() {
145 return *fd_;
146 }
147
148 private:
149 int *fd_ = NULL;
150};
151
152class DrmHwcBuffer {
153 public:
154 DrmHwcBuffer() = default;
155 DrmHwcBuffer(const hwc_drm_bo &bo, Importer *importer)
156 : bo_(bo), importer_(importer) {
157 }
158 DrmHwcBuffer(DrmHwcBuffer &&rhs) : bo_(rhs.bo_), importer_(rhs.importer_) {
159 rhs.importer_ = NULL;
160 }
161
162 ~DrmHwcBuffer() {
163 Clear();
164 }
165
166 DrmHwcBuffer &operator=(DrmHwcBuffer &&rhs) {
167 Clear();
168 importer_ = rhs.importer_;
169 rhs.importer_ = NULL;
170 bo_ = rhs.bo_;
171 return *this;
172 }
173
174 operator bool() {
175 return importer_ != NULL;
176 }
177
Zach Reiznerf99d53f2015-10-09 13:02:55 -0700178 const hwc_drm_bo *operator->() const;
Zach Reizner4a253652015-09-10 18:30:54 -0700179
180 void Clear();
181
182 int ImportBuffer(buffer_handle_t handle, Importer *importer);
183
184 private:
185 hwc_drm_bo bo_;
186 Importer *importer_ = NULL;
187};
188
189class DrmHwcNativeHandle {
190 public:
191 DrmHwcNativeHandle() = default;
192
193 DrmHwcNativeHandle(const gralloc_module_t *gralloc, native_handle_t *handle)
194 : gralloc_(gralloc), handle_(handle) {
195 }
196
197 DrmHwcNativeHandle(DrmHwcNativeHandle &&rhs) {
198 gralloc_ = rhs.gralloc_;
199 rhs.gralloc_ = NULL;
200 handle_ = rhs.handle_;
201 rhs.handle_ = NULL;
202 }
203
204 ~DrmHwcNativeHandle();
205
206 DrmHwcNativeHandle &operator=(DrmHwcNativeHandle &&rhs) {
207 Clear();
208 gralloc_ = rhs.gralloc_;
209 rhs.gralloc_ = NULL;
210 handle_ = rhs.handle_;
211 rhs.handle_ = NULL;
212 return *this;
213 }
214
215 int CopyBufferHandle(buffer_handle_t handle, const gralloc_module_t *gralloc);
216
217 void Clear();
218
219 buffer_handle_t get() const {
220 return handle_;
221 }
222
223 private:
224 const gralloc_module_t *gralloc_ = NULL;
225 native_handle_t *handle_ = NULL;
226};
227
228template <typename T>
229using DrmHwcRect = seperate_rects::Rect<T>;
230
231enum class DrmHwcTransform : uint32_t {
232 kIdentity = 0,
233 kFlipH = HWC_TRANSFORM_FLIP_H,
234 kFlipV = HWC_TRANSFORM_FLIP_V,
235 kRotate90 = HWC_TRANSFORM_ROT_90,
236 kRotate180 = HWC_TRANSFORM_ROT_180,
237 kRotate270 = HWC_TRANSFORM_ROT_270,
238};
239
240enum class DrmHwcBlending : int32_t {
241 kNone = HWC_BLENDING_NONE,
242 kPreMult = HWC_BLENDING_PREMULT,
243 kCoverage = HWC_BLENDING_COVERAGE,
244};
245
246struct DrmHwcLayer {
247 buffer_handle_t sf_handle = NULL;
248 DrmHwcBuffer buffer;
249 DrmHwcNativeHandle handle;
250 DrmHwcTransform transform = DrmHwcTransform::kIdentity;
251 DrmHwcBlending blending = DrmHwcBlending::kNone;
252 uint8_t alpha = 0xff;
253 DrmHwcRect<float> source_crop;
254 DrmHwcRect<int> display_frame;
255 std::vector<DrmHwcRect<int>> source_damage;
256
257 UniqueFd acquire_fence;
258 OutputFd release_fence;
259
Zach Reizner4a253652015-09-10 18:30:54 -0700260 int InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer,
261 const gralloc_module_t *gralloc);
Zach Reiznerf99d53f2015-10-09 13:02:55 -0700262
263 buffer_handle_t get_usable_handle() const {
264 return handle.get() != NULL ? handle.get() : sf_handle;
265 }
Zach Reizner4a253652015-09-10 18:30:54 -0700266};
267
268struct DrmHwcDisplayContents {
269 OutputFd retire_fence;
270 std::vector<DrmHwcLayer> layers;
271};
272}
273
Sean Paulef8f1f92015-04-29 16:05:23 -0400274#endif