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" |
Zach Reizner | 766518e | 2015-10-22 16:34:49 -0700 | [diff] [blame] | 26 | #include "drmhwcgralloc.h" |
| 27 | |
| 28 | struct hwc_import_context; |
| 29 | |
| 30 | int hwc_import_init(struct hwc_import_context **ctx); |
| 31 | int hwc_import_destroy(struct hwc_import_context *ctx); |
| 32 | |
| 33 | int hwc_import_bo_create(int fd, struct hwc_import_context *ctx, |
| 34 | buffer_handle_t buf, struct hwc_drm_bo *bo); |
| 35 | bool hwc_import_bo_release(int fd, struct hwc_import_context *ctx, |
| 36 | struct hwc_drm_bo *bo); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 37 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 38 | namespace android { |
| 39 | |
| 40 | class Importer; |
| 41 | |
| 42 | class UniqueFd { |
| 43 | public: |
| 44 | UniqueFd() = default; |
| 45 | UniqueFd(int fd) : fd_(fd) { |
| 46 | } |
| 47 | UniqueFd(UniqueFd &&rhs) { |
| 48 | fd_ = rhs.fd_; |
| 49 | rhs.fd_ = -1; |
| 50 | } |
| 51 | |
| 52 | UniqueFd &operator=(UniqueFd &&rhs) { |
| 53 | Set(rhs.Release()); |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | ~UniqueFd() { |
| 58 | if (fd_ >= 0) |
| 59 | close(fd_); |
| 60 | } |
| 61 | |
| 62 | int Release() { |
| 63 | int old_fd = fd_; |
| 64 | fd_ = -1; |
| 65 | return old_fd; |
| 66 | } |
| 67 | |
| 68 | int Set(int fd) { |
| 69 | if (fd_ >= 0) |
| 70 | close(fd_); |
| 71 | fd_ = fd; |
| 72 | return fd_; |
| 73 | } |
| 74 | |
| 75 | void Close() { |
| 76 | if (fd_ >= 0) |
| 77 | close(fd_); |
| 78 | fd_ = -1; |
| 79 | } |
| 80 | |
| 81 | int get() { |
| 82 | return fd_; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | int fd_ = -1; |
| 87 | }; |
| 88 | |
| 89 | struct OutputFd { |
| 90 | OutputFd() = default; |
| 91 | OutputFd(int *fd) : fd_(fd) { |
| 92 | } |
| 93 | OutputFd(OutputFd &&rhs) { |
| 94 | fd_ = rhs.fd_; |
| 95 | rhs.fd_ = NULL; |
| 96 | } |
| 97 | |
| 98 | OutputFd &operator=(OutputFd &&rhs); |
| 99 | |
| 100 | int Set(int fd) { |
| 101 | if (*fd_ >= 0) |
| 102 | close(*fd_); |
| 103 | *fd_ = fd; |
| 104 | return fd; |
| 105 | } |
| 106 | |
| 107 | int get() { |
| 108 | return *fd_; |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | int *fd_ = NULL; |
| 113 | }; |
| 114 | |
| 115 | class DrmHwcBuffer { |
| 116 | public: |
| 117 | DrmHwcBuffer() = default; |
| 118 | DrmHwcBuffer(const hwc_drm_bo &bo, Importer *importer) |
| 119 | : bo_(bo), importer_(importer) { |
| 120 | } |
| 121 | DrmHwcBuffer(DrmHwcBuffer &&rhs) : bo_(rhs.bo_), importer_(rhs.importer_) { |
| 122 | rhs.importer_ = NULL; |
| 123 | } |
| 124 | |
| 125 | ~DrmHwcBuffer() { |
| 126 | Clear(); |
| 127 | } |
| 128 | |
| 129 | DrmHwcBuffer &operator=(DrmHwcBuffer &&rhs) { |
| 130 | Clear(); |
| 131 | importer_ = rhs.importer_; |
| 132 | rhs.importer_ = NULL; |
| 133 | bo_ = rhs.bo_; |
| 134 | return *this; |
| 135 | } |
| 136 | |
Zach Reizner | fd6dc33 | 2015-10-13 21:12:48 -0700 | [diff] [blame] | 137 | operator bool() const { |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 138 | return importer_ != NULL; |
| 139 | } |
| 140 | |
Zach Reizner | f99d53f | 2015-10-09 13:02:55 -0700 | [diff] [blame] | 141 | const hwc_drm_bo *operator->() const; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 142 | |
| 143 | void Clear(); |
| 144 | |
| 145 | int ImportBuffer(buffer_handle_t handle, Importer *importer); |
| 146 | |
| 147 | private: |
| 148 | hwc_drm_bo bo_; |
| 149 | Importer *importer_ = NULL; |
| 150 | }; |
| 151 | |
| 152 | class DrmHwcNativeHandle { |
| 153 | public: |
| 154 | DrmHwcNativeHandle() = default; |
| 155 | |
| 156 | DrmHwcNativeHandle(const gralloc_module_t *gralloc, native_handle_t *handle) |
| 157 | : gralloc_(gralloc), handle_(handle) { |
| 158 | } |
| 159 | |
| 160 | DrmHwcNativeHandle(DrmHwcNativeHandle &&rhs) { |
| 161 | gralloc_ = rhs.gralloc_; |
| 162 | rhs.gralloc_ = NULL; |
| 163 | handle_ = rhs.handle_; |
| 164 | rhs.handle_ = NULL; |
| 165 | } |
| 166 | |
| 167 | ~DrmHwcNativeHandle(); |
| 168 | |
| 169 | DrmHwcNativeHandle &operator=(DrmHwcNativeHandle &&rhs) { |
| 170 | Clear(); |
| 171 | gralloc_ = rhs.gralloc_; |
| 172 | rhs.gralloc_ = NULL; |
| 173 | handle_ = rhs.handle_; |
| 174 | rhs.handle_ = NULL; |
| 175 | return *this; |
| 176 | } |
| 177 | |
| 178 | int CopyBufferHandle(buffer_handle_t handle, const gralloc_module_t *gralloc); |
| 179 | |
| 180 | void Clear(); |
| 181 | |
| 182 | buffer_handle_t get() const { |
| 183 | return handle_; |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | const gralloc_module_t *gralloc_ = NULL; |
| 188 | native_handle_t *handle_ = NULL; |
| 189 | }; |
| 190 | |
| 191 | template <typename T> |
| 192 | using DrmHwcRect = seperate_rects::Rect<T>; |
| 193 | |
| 194 | enum class DrmHwcTransform : uint32_t { |
| 195 | kIdentity = 0, |
| 196 | kFlipH = HWC_TRANSFORM_FLIP_H, |
| 197 | kFlipV = HWC_TRANSFORM_FLIP_V, |
| 198 | kRotate90 = HWC_TRANSFORM_ROT_90, |
| 199 | kRotate180 = HWC_TRANSFORM_ROT_180, |
| 200 | kRotate270 = HWC_TRANSFORM_ROT_270, |
| 201 | }; |
| 202 | |
| 203 | enum class DrmHwcBlending : int32_t { |
| 204 | kNone = HWC_BLENDING_NONE, |
| 205 | kPreMult = HWC_BLENDING_PREMULT, |
| 206 | kCoverage = HWC_BLENDING_COVERAGE, |
| 207 | }; |
| 208 | |
| 209 | struct DrmHwcLayer { |
| 210 | buffer_handle_t sf_handle = NULL; |
Zach Reizner | 36d7c6e | 2015-10-20 10:58:19 -0700 | [diff] [blame] | 211 | int gralloc_buffer_usage = 0; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 212 | DrmHwcBuffer buffer; |
| 213 | DrmHwcNativeHandle handle; |
| 214 | DrmHwcTransform transform = DrmHwcTransform::kIdentity; |
| 215 | DrmHwcBlending blending = DrmHwcBlending::kNone; |
| 216 | uint8_t alpha = 0xff; |
| 217 | DrmHwcRect<float> source_crop; |
| 218 | DrmHwcRect<int> display_frame; |
| 219 | std::vector<DrmHwcRect<int>> source_damage; |
| 220 | |
| 221 | UniqueFd acquire_fence; |
| 222 | OutputFd release_fence; |
| 223 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 224 | int InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer, |
| 225 | const gralloc_module_t *gralloc); |
Zach Reizner | f99d53f | 2015-10-09 13:02:55 -0700 | [diff] [blame] | 226 | |
| 227 | buffer_handle_t get_usable_handle() const { |
| 228 | return handle.get() != NULL ? handle.get() : sf_handle; |
| 229 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | struct DrmHwcDisplayContents { |
| 233 | OutputFd retire_fence; |
| 234 | std::vector<DrmHwcLayer> layers; |
| 235 | }; |
| 236 | } |
| 237 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 238 | #endif |