OSDN Git Service

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