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