1 /*
2 * Copyright (c) 2014-2019, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <utils/constants.h>
31 #include <utils/debug.h>
32 #include <sync/sync.h>
33 #include <stdarg.h>
34
35 #include "hwc_display_virtual.h"
36 #include "hwc_debugger.h"
37
38 #define __CLASS__ "HWCDisplayVirtual"
39
40 namespace sdm {
41
Create(CoreInterface * core_intf,HWCBufferAllocator * buffer_allocator,HWCCallbacks * callbacks,hwc2_display_t id,int32_t sdm_id,uint32_t width,uint32_t height,int32_t * format,HWCDisplay ** hwc_display,float min_lum,float max_lum)42 int HWCDisplayVirtual::Create(CoreInterface *core_intf, HWCBufferAllocator *buffer_allocator,
43 HWCCallbacks *callbacks, hwc2_display_t id, int32_t sdm_id,
44 uint32_t width, uint32_t height, int32_t *format,
45 HWCDisplay **hwc_display, float min_lum, float max_lum) {
46 int status = 0;
47 HWCDisplayVirtual *hwc_display_virtual = new HWCDisplayVirtual(core_intf, buffer_allocator,
48 callbacks, id, sdm_id);
49
50 // TODO(user): Populate format correctly
51 DLOGI("Creating virtual display: w: %d h:%d format:0x%x", width, height, *format);
52
53 status = hwc_display_virtual->Init();
54 if (status) {
55 DLOGW("Failed to initialize virtual display");
56 delete hwc_display_virtual;
57 return status;
58 }
59
60 if (max_lum != -1.0 || min_lum != -1.0) {
61 hwc_display_virtual->SetPanelLuminanceAttributes(min_lum, max_lum);
62 }
63
64 status = hwc_display_virtual->SetConfig(width, height);
65 if (status) {
66 Destroy(hwc_display_virtual);
67 return status;
68 }
69
70 status = INT32(hwc_display_virtual->SetPowerMode(HWC2::PowerMode::On, false /* teardown */));
71 if (status) {
72 DLOGW("Failed to set power mode on virtual display");
73 Destroy(hwc_display_virtual);
74 return status;
75 }
76
77 // TODO(user): Validate that we support this width/height
78 status = hwc_display_virtual->SetFrameBufferResolution(width, height);
79
80 if (status) {
81 DLOGW("Failed to set virtual display FB resolution");
82 Destroy(hwc_display_virtual);
83 return status;
84 }
85
86 *hwc_display = static_cast<HWCDisplay *>(hwc_display_virtual);
87
88 return 0;
89 }
90
Destroy(HWCDisplay * hwc_display)91 void HWCDisplayVirtual::Destroy(HWCDisplay *hwc_display) {
92 hwc_display->Deinit();
93 delete hwc_display;
94 }
95
HWCDisplayVirtual(CoreInterface * core_intf,HWCBufferAllocator * buffer_allocator,HWCCallbacks * callbacks,hwc2_display_t id,int32_t sdm_id)96 HWCDisplayVirtual::HWCDisplayVirtual(CoreInterface *core_intf, HWCBufferAllocator *buffer_allocator,
97 HWCCallbacks *callbacks, hwc2_display_t id, int32_t sdm_id) :
98 HWCDisplay(core_intf, buffer_allocator, callbacks, nullptr, nullptr, kVirtual, id, sdm_id,
99 false, DISPLAY_CLASS_VIRTUAL) {
100 }
101
Init()102 int HWCDisplayVirtual::Init() {
103 output_buffer_ = new LayerBuffer();
104 flush_on_error_ = true;
105 return HWCDisplay::Init();
106 }
107
Deinit()108 int HWCDisplayVirtual::Deinit() {
109 int status = 0;
110 if (output_buffer_) {
111 if (output_buffer_->acquire_fence_fd >= 0) {
112 close(output_buffer_->acquire_fence_fd);
113 }
114 delete output_buffer_;
115 output_buffer_ = nullptr;
116 }
117 status = HWCDisplay::Deinit();
118
119 return status;
120 }
121
Validate(uint32_t * out_num_types,uint32_t * out_num_requests)122 HWC2::Error HWCDisplayVirtual::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
123 auto status = HWC2::Error::None;
124
125 if (display_paused_ || active_secure_sessions_.any()) {
126 MarkLayersForGPUBypass();
127 return status;
128 }
129
130 BuildLayerStack();
131 layer_stack_.output_buffer = output_buffer_;
132 // If Output buffer of Virtual Display is not secure, set SKIP flag on the secure layers.
133 if (output_buffer_ && !output_buffer_->flags.secure && layer_stack_.flags.secure_present) {
134 for (auto hwc_layer : layer_set_) {
135 Layer *layer = hwc_layer->GetSDMLayer();
136 if (layer->input_buffer.flags.secure) {
137 layer_stack_.flags.skip_present = true;
138 layer->flags.skip = true;
139 }
140 }
141 }
142
143 if (layer_set_.empty()) {
144 DLOGI("Skipping Validate and Commit");
145 return status;
146 }
147 status = PrepareLayerStack(out_num_types, out_num_requests);
148 return status;
149 }
150
Present(int32_t * out_retire_fence)151 HWC2::Error HWCDisplayVirtual::Present(int32_t *out_retire_fence) {
152 auto status = HWC2::Error::None;
153
154 if (!output_buffer_->buffer_id) {
155 return HWC2::Error::NoResources;
156 }
157
158 if (active_secure_sessions_.any()) {
159 return status;
160 }
161
162 layer_stack_.output_buffer = output_buffer_;
163 if (display_paused_) {
164 validated_ = false;
165 flush_ = true;
166 }
167
168 status = HWCDisplay::CommitLayerStack();
169 if (status != HWC2::Error::None) {
170 return status;
171 }
172
173 if (dump_frame_count_ && !flush_ && dump_output_layer_) {
174 if (output_handle_) {
175 BufferInfo buffer_info;
176 const private_handle_t *output_handle =
177 reinterpret_cast<const private_handle_t *>(output_buffer_->buffer_id);
178 DisplayError error = kErrorNone;
179 if (!output_handle->base) {
180 error = buffer_allocator_->MapBuffer(output_handle, -1);
181 if (error != kErrorNone) {
182 DLOGE("Failed to map output buffer, error = %d", error);
183 return HWC2::Error::BadParameter;
184 }
185 }
186 buffer_info.buffer_config.width = static_cast<uint32_t>(output_handle->width);
187 buffer_info.buffer_config.height = static_cast<uint32_t>(output_handle->height);
188 buffer_info.buffer_config.format =
189 HWCLayer::GetSDMFormat(output_handle->format, output_handle->flags);
190 buffer_info.alloc_buffer_info.size = static_cast<uint32_t>(output_handle->size);
191 DumpOutputBuffer(buffer_info, reinterpret_cast<void *>(output_handle->base),
192 layer_stack_.retire_fence_fd);
193
194 int release_fence = -1;
195 error = buffer_allocator_->UnmapBuffer(output_handle, &release_fence);
196 if (error != kErrorNone) {
197 DLOGE("Failed to unmap buffer, error = %d", error);
198 return HWC2::Error::BadParameter;
199 }
200 }
201 }
202
203 status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
204
205 return status;
206 }
207
SetConfig(uint32_t width,uint32_t height)208 int HWCDisplayVirtual::SetConfig(uint32_t width, uint32_t height) {
209 DisplayConfigVariableInfo variable_info;
210 variable_info.x_pixels = width;
211 variable_info.y_pixels = height;
212 // TODO(user): Need to get the framerate of primary display and update it.
213 variable_info.fps = 60;
214 DisplayError err = display_intf_->SetActiveConfig(&variable_info);
215 if (err != kErrorNone) {
216 return -EINVAL;
217 }
218 return 0;
219 }
220
221
SetPanelLuminanceAttributes(float min_lum,float max_lum)222 HWC2::Error HWCDisplayVirtual::SetPanelLuminanceAttributes(float min_lum, float max_lum) {
223 DisplayError err = display_intf_->SetPanelLuminanceAttributes(min_lum, max_lum);
224 if (err != kErrorNone) {
225 return HWC2::Error::BadParameter;
226 }
227 return HWC2::Error::None;
228 }
229
SetOutputBuffer(buffer_handle_t buf,int32_t release_fence)230 HWC2::Error HWCDisplayVirtual::SetOutputBuffer(buffer_handle_t buf, int32_t release_fence) {
231 if (buf == nullptr || release_fence == 0) {
232 return HWC2::Error::BadParameter;
233 }
234 const private_handle_t *output_handle = static_cast<const private_handle_t *>(buf);
235
236 // Close the previous acquire fence and update with the latest release fence to avoid fence leak
237 // in case if this function gets invoked multiple times from the client.
238 if (output_buffer_->acquire_fence_fd >= 0) {
239 close(output_buffer_->acquire_fence_fd);
240 }
241 // Fill output buffer parameters (width, height, format, plane information, fence)
242 output_buffer_->acquire_fence_fd = dup(release_fence);
243
244 if (output_handle) {
245 int output_handle_format = output_handle->format;
246 int active_aligned_w, active_aligned_h;
247 int new_width, new_height;
248 int new_aligned_w, new_aligned_h;
249 uint32_t active_width, active_height;
250 ColorMetaData color_metadata = {};
251
252 if (output_handle_format == HAL_PIXEL_FORMAT_RGBA_8888) {
253 output_handle_format = HAL_PIXEL_FORMAT_RGBX_8888;
254 }
255
256 LayerBufferFormat new_sdm_format =
257 HWCLayer::GetSDMFormat(output_handle_format, output_handle->flags);
258 if (new_sdm_format == kFormatInvalid) {
259 return HWC2::Error::BadParameter;
260 }
261
262 if (sdm::SetCSC(output_handle, &color_metadata) != kErrorNone) {
263 return HWC2::Error::BadParameter;
264 }
265
266 GetMixerResolution(&active_width, &active_height);
267 buffer_allocator_->GetCustomWidthAndHeight(output_handle, &new_width, &new_height);
268 buffer_allocator_->GetAlignedWidthAndHeight(INT(new_width), INT(new_height),
269 output_handle_format, 0, &new_aligned_w,
270 &new_aligned_h);
271 buffer_allocator_->GetAlignedWidthAndHeight(INT(active_width), INT(active_height),
272 output_handle_format, 0, &active_aligned_w,
273 &active_aligned_h);
274 if (new_aligned_w != active_aligned_w || new_aligned_h != active_aligned_h) {
275 int status = SetConfig(UINT32(new_width), UINT32(new_height));
276 if (status) {
277 DLOGE("SetConfig failed custom WxH %dx%d", new_width, new_height);
278 return HWC2::Error::BadParameter;
279 }
280 validated_ = false;
281 }
282
283 output_buffer_->width = UINT32(new_aligned_w);
284 output_buffer_->height = UINT32(new_aligned_h);
285 output_buffer_->unaligned_width = UINT32(new_width);
286 output_buffer_->unaligned_height = UINT32(new_height);
287 output_buffer_->flags.secure = 0;
288 output_buffer_->flags.video = 0;
289 output_buffer_->buffer_id = reinterpret_cast<uint64_t>(output_handle);
290 output_buffer_->format = new_sdm_format;
291 output_buffer_->color_metadata = color_metadata;
292 output_handle_ = output_handle;
293
294 // TZ Protected Buffer - L1
295 if (output_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
296 output_buffer_->flags.secure = 1;
297 }
298
299 // ToDo: Need to extend for non-RGB formats
300 output_buffer_->planes[0].fd = output_handle->fd;
301 output_buffer_->planes[0].offset = output_handle->offset;
302 output_buffer_->planes[0].stride = UINT32(output_handle->width);
303 }
304
305 return HWC2::Error::None;
306 }
307
SetFrameDumpConfig(uint32_t count,uint32_t bit_mask_layer_type,int32_t format,bool post_processed)308 HWC2::Error HWCDisplayVirtual::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type,
309 int32_t format, bool post_processed) {
310 HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type, format, post_processed);
311 dump_output_layer_ = ((bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP)) != 0);
312
313 DLOGI("output_layer_dump_enable %d", dump_output_layer_);
314 return HWC2::Error::None;
315 }
316
GetDisplayType(int32_t * out_type)317 HWC2::Error HWCDisplayVirtual::GetDisplayType(int32_t *out_type) {
318 if (out_type == nullptr) {
319 return HWC2::Error::BadParameter;
320 }
321
322 *out_type = HWC2_DISPLAY_TYPE_VIRTUAL;
323
324 return HWC2::Error::None;
325 }
326
SetColorMode(ColorMode mode)327 HWC2::Error HWCDisplayVirtual::SetColorMode(ColorMode mode) {
328 return HWC2::Error::None;
329 }
330
331 } // namespace sdm
332