summaryrefslogtreecommitdiff
path: root/sdm/libs/hwc2/hwc_display_virtual.cpp
blob: 693039978e827ea6d9bf3a0ebbd35d47cd9687c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (c) 2014-2019, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above
*       copyright notice, this list of conditions and the following
*       disclaimer in the documentation and/or other materials provided
*       with the distribution.
*     * Neither the name of The Linux Foundation nor the names of its
*       contributors may be used to endorse or promote products derived
*       from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <utils/constants.h>
#include <utils/debug.h>
#include <sync/sync.h>
#include <stdarg.h>

#include "hwc_display_virtual.h"
#include "hwc_debugger.h"

#define __CLASS__ "HWCDisplayVirtual"

namespace sdm {

int HWCDisplayVirtual::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) {
  int status = 0;
  HWCDisplayVirtual *hwc_display_virtual = new HWCDisplayVirtual(core_intf, buffer_allocator,
                                                                 callbacks, id, sdm_id);

  // TODO(user): Populate format correctly
  DLOGI("Creating virtual display: w: %d h:%d format:0x%x", width, height, *format);

  status = hwc_display_virtual->Init();
  if (status) {
    DLOGW("Failed to initialize virtual display");
    delete hwc_display_virtual;
    return status;
  }

  if (max_lum != -1.0 || min_lum != -1.0) {
    hwc_display_virtual->SetPanelLuminanceAttributes(min_lum, max_lum);
  }

  status = hwc_display_virtual->SetConfig(width, height);
  if (status) {
    Destroy(hwc_display_virtual);
    return status;
  }

  status = INT32(hwc_display_virtual->SetPowerMode(HWC2::PowerMode::On, false /* teardown */));
  if (status) {
    DLOGW("Failed to set power mode on virtual display");
    Destroy(hwc_display_virtual);
    return status;
  }

  // TODO(user): Validate that we support this width/height
  status = hwc_display_virtual->SetFrameBufferResolution(width, height);

  if (status) {
    DLOGW("Failed to set virtual display FB resolution");
    Destroy(hwc_display_virtual);
    return status;
  }

  *hwc_display = static_cast<HWCDisplay *>(hwc_display_virtual);

  return 0;
}

void HWCDisplayVirtual::Destroy(HWCDisplay *hwc_display) {
  hwc_display->Deinit();
  delete hwc_display;
}

HWCDisplayVirtual::HWCDisplayVirtual(CoreInterface *core_intf, HWCBufferAllocator *buffer_allocator,
                                     HWCCallbacks *callbacks, hwc2_display_t id, int32_t sdm_id) :
      HWCDisplay(core_intf, buffer_allocator, callbacks, nullptr, nullptr, kVirtual, id, sdm_id,
                 false, DISPLAY_CLASS_VIRTUAL) {
}

int HWCDisplayVirtual::Init() {
  output_buffer_ = new LayerBuffer();
  flush_on_error_ = true;
  return HWCDisplay::Init();
}

int HWCDisplayVirtual::Deinit() {
  int status = 0;
  if (output_buffer_) {
    if (output_buffer_->acquire_fence_fd >= 0) {
      close(output_buffer_->acquire_fence_fd);
    }
    delete output_buffer_;
    output_buffer_ = nullptr;
  }
  status = HWCDisplay::Deinit();

  return status;
}

HWC2::Error HWCDisplayVirtual::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
  auto status = HWC2::Error::None;

  if (display_paused_ || active_secure_sessions_.any()) {
    MarkLayersForGPUBypass();
    return status;
  }

  BuildLayerStack();
  layer_stack_.output_buffer = output_buffer_;
  // If Output buffer of Virtual Display is not secure, set SKIP flag on the secure layers.
  if (output_buffer_ && !output_buffer_->flags.secure && layer_stack_.flags.secure_present) {
    for (auto hwc_layer : layer_set_) {
      Layer *layer = hwc_layer->GetSDMLayer();
      if (layer->input_buffer.flags.secure) {
        layer_stack_.flags.skip_present = true;
        layer->flags.skip = true;
      }
    }
  }

  if (layer_set_.empty()) {
    DLOGI("Skipping Validate and Commit");
    return status;
  }
  status = PrepareLayerStack(out_num_types, out_num_requests);
  return status;
}

HWC2::Error HWCDisplayVirtual::Present(int32_t *out_retire_fence) {
  auto status = HWC2::Error::None;

  if (!output_buffer_->buffer_id) {
    return HWC2::Error::NoResources;
  }

  if (active_secure_sessions_.any()) {
    return status;
  }

  layer_stack_.output_buffer = output_buffer_;
  if (display_paused_) {
    validated_ = false;
    flush_ = true;
  }

  status = HWCDisplay::CommitLayerStack();
  if (status != HWC2::Error::None) {
    return status;
  }

  if (dump_frame_count_ && !flush_ && dump_output_layer_) {
    if (output_handle_) {
      BufferInfo buffer_info;
      const private_handle_t *output_handle =
        reinterpret_cast<const private_handle_t *>(output_buffer_->buffer_id);
      DisplayError error = kErrorNone;
      if (!output_handle->base) {
        error = buffer_allocator_->MapBuffer(output_handle, -1);
        if (error != kErrorNone) {
          DLOGE("Failed to map output buffer, error = %d", error);
          return HWC2::Error::BadParameter;
        }
      }
      buffer_info.buffer_config.width = static_cast<uint32_t>(output_handle->width);
      buffer_info.buffer_config.height = static_cast<uint32_t>(output_handle->height);
      buffer_info.buffer_config.format =
      HWCLayer::GetSDMFormat(output_handle->format, output_handle->flags);
      buffer_info.alloc_buffer_info.size = static_cast<uint32_t>(output_handle->size);
      DumpOutputBuffer(buffer_info, reinterpret_cast<void *>(output_handle->base),
                       layer_stack_.retire_fence_fd);

      int release_fence = -1;
      error = buffer_allocator_->UnmapBuffer(output_handle, &release_fence);
      if (error != kErrorNone) {
        DLOGE("Failed to unmap buffer, error = %d", error);
        return HWC2::Error::BadParameter;
      }
    }
  }

  status = HWCDisplay::PostCommitLayerStack(out_retire_fence);

  return status;
}

int HWCDisplayVirtual::SetConfig(uint32_t width, uint32_t height) {
  DisplayConfigVariableInfo variable_info;
  variable_info.x_pixels = width;
  variable_info.y_pixels = height;
  // TODO(user): Need to get the framerate of primary display and update it.
  variable_info.fps = 60;
  DisplayError err = display_intf_->SetActiveConfig(&variable_info);
  if (err != kErrorNone) {
    return -EINVAL;
  }
  return 0;
}


HWC2::Error HWCDisplayVirtual::SetPanelLuminanceAttributes(float min_lum, float max_lum) {
  DisplayError err = display_intf_->SetPanelLuminanceAttributes(min_lum, max_lum);
  if (err != kErrorNone) {
    return HWC2::Error::BadParameter;
  }
  return HWC2::Error::None;
}

HWC2::Error HWCDisplayVirtual::SetOutputBuffer(buffer_handle_t buf, int32_t release_fence) {
  if (buf == nullptr || release_fence == 0) {
    return HWC2::Error::BadParameter;
  }
  const private_handle_t *output_handle = static_cast<const private_handle_t *>(buf);

  // Close the previous acquire fence and update with the latest release fence to avoid fence leak
  // in case if this function gets invoked multiple times from the client.
  if (output_buffer_->acquire_fence_fd >= 0) {
    close(output_buffer_->acquire_fence_fd);
  }
  // Fill output buffer parameters (width, height, format, plane information, fence)
  output_buffer_->acquire_fence_fd = dup(release_fence);

  if (output_handle) {
    int output_handle_format = output_handle->format;
    int active_aligned_w, active_aligned_h;
    int new_width, new_height;
    int new_aligned_w, new_aligned_h;
    uint32_t active_width, active_height;
    ColorMetaData color_metadata = {};

    if (output_handle_format == HAL_PIXEL_FORMAT_RGBA_8888) {
      output_handle_format = HAL_PIXEL_FORMAT_RGBX_8888;
    }

    LayerBufferFormat new_sdm_format =
        HWCLayer::GetSDMFormat(output_handle_format, output_handle->flags);
    if (new_sdm_format == kFormatInvalid) {
      return HWC2::Error::BadParameter;
    }

    if (sdm::SetCSC(output_handle, &color_metadata) != kErrorNone) {
      return HWC2::Error::BadParameter;
    }

    GetMixerResolution(&active_width, &active_height);
    buffer_allocator_->GetCustomWidthAndHeight(output_handle, &new_width, &new_height);
    buffer_allocator_->GetAlignedWidthAndHeight(INT(new_width), INT(new_height),
                                                output_handle_format, 0, &new_aligned_w,
                                                &new_aligned_h);
    buffer_allocator_->GetAlignedWidthAndHeight(INT(active_width), INT(active_height),
                                                output_handle_format, 0, &active_aligned_w,
                                                &active_aligned_h);
    if (new_aligned_w != active_aligned_w  || new_aligned_h != active_aligned_h) {
      int status = SetConfig(UINT32(new_width), UINT32(new_height));
      if (status) {
        DLOGE("SetConfig failed custom WxH %dx%d", new_width, new_height);
        return HWC2::Error::BadParameter;
      }
      validated_ = false;
    }

    output_buffer_->width = UINT32(new_aligned_w);
    output_buffer_->height = UINT32(new_aligned_h);
    output_buffer_->unaligned_width = UINT32(new_width);
    output_buffer_->unaligned_height = UINT32(new_height);
    output_buffer_->flags.secure = 0;
    output_buffer_->flags.video = 0;
    output_buffer_->buffer_id = reinterpret_cast<uint64_t>(output_handle);
    output_buffer_->format = new_sdm_format;
    output_buffer_->color_metadata = color_metadata;
    output_handle_ = output_handle;

    // TZ Protected Buffer - L1
    if (output_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
      output_buffer_->flags.secure = 1;
    }

    // ToDo: Need to extend for non-RGB formats
    output_buffer_->planes[0].fd = output_handle->fd;
    output_buffer_->planes[0].offset = output_handle->offset;
    output_buffer_->planes[0].stride = UINT32(output_handle->width);
  }

  return HWC2::Error::None;
}

HWC2::Error HWCDisplayVirtual::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type,
                                                  int32_t format, bool post_processed) {
  HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type, format, post_processed);
  dump_output_layer_ = ((bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP)) != 0);

  DLOGI("output_layer_dump_enable %d", dump_output_layer_);
  return HWC2::Error::None;
}

HWC2::Error HWCDisplayVirtual::GetDisplayType(int32_t *out_type) {
  if (out_type == nullptr) {
    return HWC2::Error::BadParameter;
  }

  *out_type = HWC2_DISPLAY_TYPE_VIRTUAL;

  return HWC2::Error::None;
}

HWC2::Error HWCDisplayVirtual::SetColorMode(ColorMode mode) {
  return HWC2::Error::None;
}

}  // namespace sdm