1 /*
2 * Copyright (c) 2014-2018, 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,uint32_t width,uint32_t height,int32_t * format,HWCDisplay ** hwc_display)42 int HWCDisplayVirtual::Create(CoreInterface *core_intf, HWCBufferAllocator *buffer_allocator,
43 HWCCallbacks *callbacks, uint32_t width,
44 uint32_t height, int32_t *format, HWCDisplay **hwc_display) {
45 int status = 0;
46 HWCDisplayVirtual *hwc_display_virtual = new HWCDisplayVirtual(core_intf, buffer_allocator,
47 callbacks);
48
49 // TODO(user): Populate format correctly
50 DLOGI("Creating virtual display: w: %d h:%d format:0x%x", width, height, *format);
51
52 status = hwc_display_virtual->Init();
53 if (status) {
54 DLOGW("Failed to initialize virtual display");
55 delete hwc_display_virtual;
56 return status;
57 }
58
59 status = hwc_display_virtual->SetConfig(width, height);
60 if (status) {
61 Destroy(hwc_display_virtual);
62 return status;
63 }
64
65 status = INT32(hwc_display_virtual->SetPowerMode(HWC2::PowerMode::On));
66 if (status) {
67 DLOGW("Failed to set power mode on virtual display");
68 Destroy(hwc_display_virtual);
69 return status;
70 }
71
72 // TODO(user): Validate that we support this width/height
73 status = hwc_display_virtual->SetFrameBufferResolution(width, height);
74
75 if (status) {
76 DLOGW("Failed to set virtual display FB resolution");
77 Destroy(hwc_display_virtual);
78 return status;
79 }
80
81 *hwc_display = static_cast<HWCDisplay *>(hwc_display_virtual);
82
83 return 0;
84 }
85
Destroy(HWCDisplay * hwc_display)86 void HWCDisplayVirtual::Destroy(HWCDisplay *hwc_display) {
87 hwc_display->Deinit();
88 delete hwc_display;
89 }
90
HWCDisplayVirtual(CoreInterface * core_intf,HWCBufferAllocator * buffer_allocator,HWCCallbacks * callbacks)91 HWCDisplayVirtual::HWCDisplayVirtual(CoreInterface *core_intf, HWCBufferAllocator *buffer_allocator,
92 HWCCallbacks *callbacks)
93 : HWCDisplay(core_intf, callbacks, kVirtual, HWC_DISPLAY_VIRTUAL, false, NULL,
94 DISPLAY_CLASS_VIRTUAL, buffer_allocator) {
95 }
96
Init()97 int HWCDisplayVirtual::Init() {
98 output_buffer_ = new LayerBuffer();
99 return HWCDisplay::Init();
100 }
101
Deinit()102 int HWCDisplayVirtual::Deinit() {
103 int status = 0;
104 if (output_buffer_) {
105 delete output_buffer_;
106 output_buffer_ = nullptr;
107 }
108 status = HWCDisplay::Deinit();
109
110 return status;
111 }
112
Validate(uint32_t * out_num_types,uint32_t * out_num_requests)113 HWC2::Error HWCDisplayVirtual::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
114 auto status = HWC2::Error::None;
115
116 if (display_paused_) {
117 MarkLayersForGPUBypass();
118 return status;
119 }
120
121 BuildLayerStack();
122 layer_stack_.output_buffer = output_buffer_;
123 // If Output buffer of Virtual Display is not secure, set SKIP flag on the secure layers.
124 if (output_buffer_ && !output_buffer_->flags.secure && layer_stack_.flags.secure_present) {
125 for (auto hwc_layer : layer_set_) {
126 Layer *layer = hwc_layer->GetSDMLayer();
127 if (layer->input_buffer.flags.secure) {
128 layer_stack_.flags.skip_present = true;
129 layer->flags.skip = true;
130 }
131 }
132 }
133
134 if (layer_set_.empty()) {
135 DLOGI("Skipping Validate and Commit");
136 return status;
137 }
138 status = PrepareLayerStack(out_num_types, out_num_requests);
139 return status;
140 }
141
Present(int32_t * out_retire_fence)142 HWC2::Error HWCDisplayVirtual::Present(int32_t *out_retire_fence) {
143 auto status = HWC2::Error::None;
144
145 if (!output_buffer_->buffer_id) {
146 return HWC2::Error::NoResources;
147 }
148
149 if (display_paused_) {
150 DisplayError error = display_intf_->Flush();
151 validated_ = false;
152 if (error != kErrorNone) {
153 DLOGE("Flush failed. Error = %d", error);
154 }
155 } else {
156 status = HWCDisplay::CommitLayerStack();
157 if (status == HWC2::Error::None) {
158 if (dump_frame_count_ && !flush_ && dump_output_layer_) {
159 if (output_handle_) {
160 BufferInfo buffer_info;
161 const private_handle_t *output_handle =
162 reinterpret_cast<const private_handle_t *>(output_buffer_->buffer_id);
163 DisplayError error = kErrorNone;
164 if (!output_handle->base) {
165 error = buffer_allocator_->MapBuffer(output_handle, -1);
166 if (error != kErrorNone) {
167 DLOGE("Failed to map output buffer, error = %d", error);
168 return HWC2::Error::BadParameter;
169 }
170 }
171 buffer_info.buffer_config.width = static_cast<uint32_t>(output_handle->width);
172 buffer_info.buffer_config.height = static_cast<uint32_t>(output_handle->height);
173 buffer_info.buffer_config.format =
174 GetSDMFormat(output_handle->format, output_handle->flags);
175 buffer_info.alloc_buffer_info.size = static_cast<uint32_t>(output_handle->size);
176 DumpOutputBuffer(buffer_info, reinterpret_cast<void *>(output_handle->base),
177 layer_stack_.retire_fence_fd);
178
179 int release_fence = -1;
180 error = buffer_allocator_->UnmapBuffer(output_handle, &release_fence);
181 if (error != kErrorNone) {
182 DLOGE("Failed to unmap buffer, error = %d", error);
183 return HWC2::Error::BadParameter;
184 }
185 }
186 }
187
188 status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
189 }
190 }
191 if (output_buffer_->acquire_fence_fd >= 0) {
192 close(output_buffer_->acquire_fence_fd);
193 output_buffer_->acquire_fence_fd = -1;
194 }
195 return status;
196 }
197
SetConfig(uint32_t width,uint32_t height)198 int HWCDisplayVirtual::SetConfig(uint32_t width, uint32_t height) {
199 DisplayConfigVariableInfo variable_info;
200 variable_info.x_pixels = width;
201 variable_info.y_pixels = height;
202 // TODO(user): Need to get the framerate of primary display and update it.
203 variable_info.fps = 60;
204 DisplayError err = display_intf_->SetActiveConfig(&variable_info);
205 if (err != kErrorNone) {
206 return -EINVAL;
207 }
208 return 0;
209 }
210
SetOutputBuffer(buffer_handle_t buf,int32_t release_fence)211 HWC2::Error HWCDisplayVirtual::SetOutputBuffer(buffer_handle_t buf, int32_t release_fence) {
212 if (buf == nullptr || release_fence == 0) {
213 return HWC2::Error::BadParameter;
214 }
215 const private_handle_t *output_handle = static_cast<const private_handle_t *>(buf);
216
217 // Fill output buffer parameters (width, height, format, plane information, fence)
218 output_buffer_->acquire_fence_fd = dup(release_fence);
219
220 if (output_handle) {
221 int output_handle_format = output_handle->format;
222 int active_aligned_w, active_aligned_h;
223 int new_width, new_height;
224 int new_aligned_w, new_aligned_h;
225 uint32_t active_width, active_height;
226 ColorMetaData color_metadata = {};
227
228 if (output_handle_format == HAL_PIXEL_FORMAT_RGBA_8888) {
229 output_handle_format = HAL_PIXEL_FORMAT_RGBX_8888;
230 }
231
232 LayerBufferFormat new_sdm_format = GetSDMFormat(output_handle_format, output_handle->flags);
233 if (new_sdm_format == kFormatInvalid) {
234 return HWC2::Error::BadParameter;
235 }
236
237 if (sdm::SetCSC(output_handle, &color_metadata) != kErrorNone) {
238 return HWC2::Error::BadParameter;
239 }
240
241 GetMixerResolution(&active_width, &active_height);
242 buffer_allocator_->GetCustomWidthAndHeight(output_handle, &new_width, &new_height);
243 buffer_allocator_->GetAlignedWidthAndHeight(INT(new_width), INT(new_height),
244 output_handle_format, 0, &new_aligned_w,
245 &new_aligned_h);
246 buffer_allocator_->GetAlignedWidthAndHeight(INT(active_width), INT(active_height),
247 output_handle_format, 0, &active_aligned_w,
248 &active_aligned_h);
249 if (new_aligned_w != active_aligned_w || new_aligned_h != active_aligned_h) {
250 int status = SetConfig(UINT32(new_width), UINT32(new_height));
251 if (status) {
252 DLOGE("SetConfig failed custom WxH %dx%d", new_width, new_height);
253 return HWC2::Error::BadParameter;
254 }
255 validated_ = false;
256 }
257
258 output_buffer_->width = UINT32(new_aligned_w);
259 output_buffer_->height = UINT32(new_aligned_h);
260 output_buffer_->unaligned_width = UINT32(new_width);
261 output_buffer_->unaligned_height = UINT32(new_height);
262 output_buffer_->flags.secure = 0;
263 output_buffer_->flags.video = 0;
264 output_buffer_->buffer_id = reinterpret_cast<uint64_t>(output_handle);
265 output_buffer_->format = new_sdm_format;
266 output_buffer_->color_metadata = color_metadata;
267 output_handle_ = output_handle;
268
269 // TZ Protected Buffer - L1
270 if (output_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
271 output_buffer_->flags.secure = 1;
272 }
273
274 // ToDo: Need to extend for non-RGB formats
275 output_buffer_->planes[0].fd = output_handle->fd;
276 output_buffer_->planes[0].offset = output_handle->offset;
277 output_buffer_->planes[0].stride = UINT32(output_handle->width);
278 }
279
280 return HWC2::Error::None;
281 }
282
SetFrameDumpConfig(uint32_t count,uint32_t bit_mask_layer_type,int32_t format,bool post_processed)283 HWC2::Error HWCDisplayVirtual::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type,
284 int32_t format, bool post_processed) {
285 HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type, format, post_processed);
286 dump_output_layer_ = ((bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP)) != 0);
287
288 DLOGI("output_layer_dump_enable %d", dump_output_layer_);
289 return HWC2::Error::None;
290 }
291
292 } // namespace sdm
293