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 <vector> 24 25 #include <hardware/hardware.h> 26 #include <hardware/hwcomposer.h> 27 #include "autofd.h" 28 #include "drmhwcgralloc.h" 29 30 struct hwc_import_context; 31 32 int hwc_import_init(struct hwc_import_context **ctx); 33 int hwc_import_destroy(struct hwc_import_context *ctx); 34 35 int hwc_import_bo_create(int fd, struct hwc_import_context *ctx, 36 buffer_handle_t buf, struct hwc_drm_bo *bo); 37 bool hwc_import_bo_release(int fd, struct hwc_import_context *ctx, 38 struct hwc_drm_bo *bo); 39 40 namespace android { 41 42 class Importer; 43 44 class DrmHwcBuffer { 45 public: 46 DrmHwcBuffer() = default; DrmHwcBuffer(const hwc_drm_bo & bo,Importer * importer)47 DrmHwcBuffer(const hwc_drm_bo &bo, Importer *importer) 48 : bo_(bo), importer_(importer) { 49 } DrmHwcBuffer(DrmHwcBuffer && rhs)50 DrmHwcBuffer(DrmHwcBuffer &&rhs) : bo_(rhs.bo_), importer_(rhs.importer_) { 51 rhs.importer_ = NULL; 52 } 53 ~DrmHwcBuffer()54 ~DrmHwcBuffer() { 55 Clear(); 56 } 57 58 DrmHwcBuffer &operator=(DrmHwcBuffer &&rhs) { 59 Clear(); 60 importer_ = rhs.importer_; 61 rhs.importer_ = NULL; 62 bo_ = rhs.bo_; 63 return *this; 64 } 65 66 operator bool() const { 67 return importer_ != NULL; 68 } 69 70 const hwc_drm_bo *operator->() const; 71 72 void Clear(); 73 74 int ImportBuffer(buffer_handle_t handle, Importer *importer); 75 76 private: 77 hwc_drm_bo bo_; 78 Importer *importer_ = NULL; 79 }; 80 81 class DrmHwcNativeHandle { 82 public: 83 DrmHwcNativeHandle() = default; 84 DrmHwcNativeHandle(native_handle_t * handle)85 DrmHwcNativeHandle(native_handle_t *handle) : handle_(handle) { 86 } 87 DrmHwcNativeHandle(DrmHwcNativeHandle && rhs)88 DrmHwcNativeHandle(DrmHwcNativeHandle &&rhs) { 89 handle_ = rhs.handle_; 90 rhs.handle_ = NULL; 91 } 92 93 ~DrmHwcNativeHandle(); 94 95 DrmHwcNativeHandle &operator=(DrmHwcNativeHandle &&rhs) { 96 Clear(); 97 handle_ = rhs.handle_; 98 rhs.handle_ = NULL; 99 return *this; 100 } 101 102 int CopyBufferHandle(buffer_handle_t handle); 103 104 void Clear(); 105 get()106 buffer_handle_t get() const { 107 return handle_; 108 } 109 110 private: 111 native_handle_t *handle_ = NULL; 112 }; 113 114 enum DrmHwcTransform { 115 kIdentity = 0, 116 kFlipH = 1 << 0, 117 kFlipV = 1 << 1, 118 kRotate90 = 1 << 2, 119 kRotate180 = 1 << 3, 120 kRotate270 = 1 << 4, 121 }; 122 123 enum class DrmHwcBlending : int32_t { 124 kNone = HWC_BLENDING_NONE, 125 kPreMult = HWC_BLENDING_PREMULT, 126 kCoverage = HWC_BLENDING_COVERAGE, 127 }; 128 129 struct DrmHwcLayer { 130 buffer_handle_t sf_handle = NULL; 131 int gralloc_buffer_usage = 0; 132 DrmHwcBuffer buffer; 133 DrmHwcNativeHandle handle; 134 uint32_t transform; 135 DrmHwcBlending blending = DrmHwcBlending::kNone; 136 uint16_t alpha = 0xffff; 137 hwc_frect_t source_crop; 138 hwc_rect_t display_frame; 139 140 UniqueFd acquire_fence; 141 OutputFd release_fence; 142 143 int ImportBuffer(Importer *importer); 144 int InitFromDrmHwcLayer(DrmHwcLayer *layer, Importer *importer); 145 146 void SetTransform(int32_t sf_transform); 147 void SetSourceCrop(hwc_frect_t const &crop); 148 void SetDisplayFrame(hwc_rect_t const &frame); 149 get_usable_handleDrmHwcLayer150 buffer_handle_t get_usable_handle() const { 151 return handle.get() != NULL ? handle.get() : sf_handle; 152 } 153 protected_usageDrmHwcLayer154 bool protected_usage() const { 155 return (gralloc_buffer_usage & GRALLOC_USAGE_PROTECTED) == 156 GRALLOC_USAGE_PROTECTED; 157 } 158 }; 159 160 } // namespace android 161 162 #endif 163