Sean Paul | cd36a9e | 2015-01-22 18:01:18 -0500 | [diff] [blame] | 1 | /* |
| 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 Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 17 | #ifndef ANDROID_DRM_HWCOMPOSER_H_ |
| 18 | #define ANDROID_DRM_HWCOMPOSER_H_ |
| 19 | |
Lauri Peltonen | 902c894 | 2015-03-02 19:10:04 +0200 | [diff] [blame] | 20 | #include <stdbool.h> |
| 21 | #include <stdint.h> |
| 22 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 23 | #include <hardware/hardware.h> |
| 24 | #include <hardware/hwcomposer.h> |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 25 | #include "seperate_rects.h" |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 26 | |
Sean Paul | cd36a9e | 2015-01-22 18:01:18 -0500 | [diff] [blame] | 27 | struct hwc_import_context; |
| 28 | |
Lauri Peltonen | ceaadea | 2015-02-02 14:45:13 +0200 | [diff] [blame] | 29 | enum { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 30 | /* 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 Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 37 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 38 | /* 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 Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 45 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 46 | /* 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 Peltonen | ceaadea | 2015-02-02 14:45:13 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 55 | typedef 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 Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 64 | void *priv; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 65 | } hwc_drm_bo_t; |
Sean Paul | cd36a9e | 2015-01-22 18:01:18 -0500 | [diff] [blame] | 66 | |
| 67 | int hwc_import_init(struct hwc_import_context **ctx); |
| 68 | int hwc_import_destroy(struct hwc_import_context *ctx); |
| 69 | |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 70 | int hwc_import_bo_create(int fd, struct hwc_import_context *ctx, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 71 | buffer_handle_t buf, struct hwc_drm_bo *bo); |
Lauri Peltonen | 902c894 | 2015-03-02 19:10:04 +0200 | [diff] [blame] | 72 | bool hwc_import_bo_release(int fd, struct hwc_import_context *ctx, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 73 | struct hwc_drm_bo *bo); |
| 74 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 75 | namespace android { |
| 76 | |
| 77 | class Importer; |
| 78 | |
| 79 | class 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 | |
| 126 | struct 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 | |
| 152 | class 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 Reizner | f99d53f | 2015-10-09 13:02:55 -0700 | [diff] [blame] | 178 | const hwc_drm_bo *operator->() const; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 179 | |
| 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 | |
| 189 | class 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 | |
| 228 | template <typename T> |
| 229 | using DrmHwcRect = seperate_rects::Rect<T>; |
| 230 | |
| 231 | enum 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 | |
| 240 | enum class DrmHwcBlending : int32_t { |
| 241 | kNone = HWC_BLENDING_NONE, |
| 242 | kPreMult = HWC_BLENDING_PREMULT, |
| 243 | kCoverage = HWC_BLENDING_COVERAGE, |
| 244 | }; |
| 245 | |
| 246 | struct 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 Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 260 | int InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer, |
| 261 | const gralloc_module_t *gralloc); |
Zach Reizner | f99d53f | 2015-10-09 13:02:55 -0700 | [diff] [blame] | 262 | |
| 263 | buffer_handle_t get_usable_handle() const { |
| 264 | return handle.get() != NULL ? handle.get() : sf_handle; |
| 265 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | struct DrmHwcDisplayContents { |
| 269 | OutputFd retire_fence; |
| 270 | std::vector<DrmHwcLayer> layers; |
| 271 | }; |
| 272 | } |
| 273 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 274 | #endif |