1/*
2* Copyright (c) 2014 - 2017, The Linux Foundation. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without modification, are permitted
5* provided that the following conditions are met:
6*    * Redistributions of source code must retain the above copyright notice, this list of
7*      conditions and the following disclaimer.
8*    * Redistributions in binary form must reproduce the above copyright notice, this list of
9*      conditions and the following disclaimer in the documentation and/or other materials provided
10*      with the distribution.
11*    * Neither the name of The Linux Foundation nor the names of its contributors may be used to
12*      endorse or promote products derived from this software without specific prior written
13*      permission.
14*
15* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17* NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*/
24
25#include <utils/constants.h>
26#include <utils/debug.h>
27
28#include "display_virtual.h"
29#include "hw_interface.h"
30#include "hw_info_interface.h"
31
32#define __CLASS__ "DisplayVirtual"
33
34namespace sdm {
35
36DisplayVirtual::DisplayVirtual(DisplayEventHandler *event_handler, HWInfoInterface *hw_info_intf,
37                               BufferSyncHandler *buffer_sync_handler, CompManager *comp_manager)
38  : DisplayBase(kVirtual, event_handler, kDeviceVirtual, buffer_sync_handler, comp_manager,
39                hw_info_intf) {
40}
41
42DisplayError DisplayVirtual::Init() {
43  lock_guard<recursive_mutex> obj(recursive_mutex_);
44
45  DisplayError error = HWInterface::Create(kVirtual, hw_info_intf_, buffer_sync_handler_,
46                                           &hw_intf_);
47  if (error != kErrorNone) {
48    return error;
49  }
50
51  hw_intf_->GetDisplayAttributes(0 /* active_index */, &display_attributes_);
52
53  error = DisplayBase::Init();
54  if (error != kErrorNone) {
55    HWInterface::Destroy(hw_intf_);
56  }
57
58  return error;
59}
60
61DisplayError DisplayVirtual::GetNumVariableInfoConfigs(uint32_t *count) {
62  lock_guard<recursive_mutex> obj(recursive_mutex_);
63  *count = 1;
64  return kErrorNone;
65}
66
67DisplayError DisplayVirtual::GetConfig(uint32_t index, DisplayConfigVariableInfo *variable_info) {
68  lock_guard<recursive_mutex> obj(recursive_mutex_);
69  *variable_info = display_attributes_;
70  return kErrorNone;
71}
72
73DisplayError DisplayVirtual::GetActiveConfig(uint32_t *index) {
74  lock_guard<recursive_mutex> obj(recursive_mutex_);
75  *index = 0;
76  return kErrorNone;
77}
78
79DisplayError DisplayVirtual::SetActiveConfig(DisplayConfigVariableInfo *variable_info) {
80  lock_guard<recursive_mutex> obj(recursive_mutex_);
81
82  if (!variable_info) {
83    return kErrorParameters;
84  }
85
86  DisplayError error = kErrorNone;
87  HWDisplayAttributes display_attributes;
88  HWMixerAttributes mixer_attributes;
89
90  display_attributes.x_pixels = variable_info->x_pixels;
91  display_attributes.y_pixels = variable_info->y_pixels;
92  display_attributes.fps = variable_info->fps;
93
94  if (display_attributes == display_attributes_) {
95    return kErrorNone;
96  }
97
98  error = hw_intf_->SetDisplayAttributes(display_attributes);
99  if (error != kErrorNone) {
100    return error;
101  }
102
103  error = hw_intf_->GetMixerAttributes(&mixer_attributes);
104  if (error != kErrorNone) {
105    return error;
106  }
107
108  // if display is already connected, unregister display from composition manager and register
109  // the display with new configuration.
110  if (display_comp_ctx_) {
111    comp_manager_->UnregisterDisplay(display_comp_ctx_);
112  }
113
114  error = comp_manager_->RegisterDisplay(display_type_, display_attributes, hw_panel_info_,
115                                         mixer_attributes, fb_config_, &display_comp_ctx_);
116  if (error != kErrorNone) {
117    return error;
118  }
119
120  display_attributes_ = display_attributes;
121  mixer_attributes_ = mixer_attributes;
122
123  DLOGI("Virtual display resolution changed to[%dx%d]", display_attributes_.x_pixels,
124        display_attributes_.y_pixels);
125
126  return kErrorNone;
127}
128
129DisplayError DisplayVirtual::Prepare(LayerStack *layer_stack) {
130  lock_guard<recursive_mutex> obj(recursive_mutex_);
131
132  // Clean hw layers for reuse.
133  hw_layers_ = HWLayers();
134
135  return DisplayBase::Prepare(layer_stack);
136}
137
138
139}  // namespace sdm
140
141