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 17 #ifndef ANDROID_DRM_HWCOMPOSER_H_ 18 #define ANDROID_DRM_HWCOMPOSER_H_ 19 20 #include <stdbool.h> 21 #include <stdint.h> 22 23 #include <hardware/hardware.h> 24 #include <hardware/hwcomposer.h> 25 #include "seperate_rects.h" 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); 37 38 namespace android { 39 40 class Importer; 41 42 class UniqueFd { 43 public: 44 UniqueFd() = default; UniqueFd(int fd)45 UniqueFd(int fd) : fd_(fd) { 46 } UniqueFd(UniqueFd && rhs)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 ~UniqueFd()57 ~UniqueFd() { 58 if (fd_ >= 0) 59 close(fd_); 60 } 61 Release()62 int Release() { 63 int old_fd = fd_; 64 fd_ = -1; 65 return old_fd; 66 } 67 Set(int fd)68 int Set(int fd) { 69 if (fd_ >= 0) 70 close(fd_); 71 fd_ = fd; 72 return fd_; 73 } 74 Close()75 void Close() { 76 if (fd_ >= 0) 77 close(fd_); 78 fd_ = -1; 79 } 80 get()81 int get() { 82 return fd_; 83 } 84 85 private: 86 int fd_ = -1; 87 }; 88 89 struct OutputFd { 90 OutputFd() = default; OutputFdOutputFd91 OutputFd(int *fd) : fd_(fd) { 92 } OutputFdOutputFd93 OutputFd(OutputFd &&rhs) { 94 fd_ = rhs.fd_; 95 rhs.fd_ = NULL; 96 } 97 98 OutputFd &operator=(OutputFd &&rhs); 99 SetOutputFd100 int Set(int fd) { 101 if (*fd_ >= 0) 102 close(*fd_); 103 *fd_ = fd; 104 return fd; 105 } 106 getOutputFd107 int get() { 108 return *fd_; 109 } 110 111 private: 112 int *fd_ = NULL; 113 }; 114 115 class DrmHwcBuffer { 116 public: 117 DrmHwcBuffer() = default; DrmHwcBuffer(const hwc_drm_bo & bo,Importer * importer)118 DrmHwcBuffer(const hwc_drm_bo &bo, Importer *importer) 119 : bo_(bo), importer_(importer) { 120 } DrmHwcBuffer(DrmHwcBuffer && rhs)121 DrmHwcBuffer(DrmHwcBuffer &&rhs) : bo_(rhs.bo_), importer_(rhs.importer_) { 122 rhs.importer_ = NULL; 123 } 124 ~DrmHwcBuffer()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 137 operator bool() const { 138 return importer_ != NULL; 139 } 140 141 const hwc_drm_bo *operator->() const; 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 DrmHwcNativeHandle(const gralloc_module_t * gralloc,native_handle_t * handle)156 DrmHwcNativeHandle(const gralloc_module_t *gralloc, native_handle_t *handle) 157 : gralloc_(gralloc), handle_(handle) { 158 } 159 DrmHwcNativeHandle(DrmHwcNativeHandle && rhs)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 get()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; 211 int gralloc_buffer_usage = 0; 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 224 int InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer, 225 const gralloc_module_t *gralloc); 226 get_usable_handleDrmHwcLayer227 buffer_handle_t get_usable_handle() const { 228 return handle.get() != NULL ? handle.get() : sf_handle; 229 } 230 }; 231 232 struct DrmHwcDisplayContents { 233 OutputFd retire_fence; 234 std::vector<DrmHwcLayer> layers; 235 }; 236 } 237 238 #endif 239