1/*
2* Copyright (c) 2014 - 2016, 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 <dlfcn.h>
26#include <utils/locker.h>
27#include <utils/constants.h>
28#include <utils/debug.h>
29
30#include "core_impl.h"
31#include "display_primary.h"
32#include "display_hdmi.h"
33#include "display_virtual.h"
34#include "hw_info_interface.h"
35#include "color_manager.h"
36
37#define __CLASS__ "CoreImpl"
38
39namespace sdm {
40
41CoreImpl::CoreImpl(BufferAllocator *buffer_allocator,
42                   BufferSyncHandler *buffer_sync_handler,
43                   SocketHandler *socket_handler)
44  : buffer_allocator_(buffer_allocator), buffer_sync_handler_(buffer_sync_handler),
45    socket_handler_(socket_handler) {
46}
47
48DisplayError CoreImpl::Init() {
49  SCOPE_LOCK(locker_);
50  DisplayError error = kErrorNone;
51
52  // Try to load extension library & get handle to its interface.
53  if (extension_lib_.Open(EXTENSION_LIBRARY_NAME)) {
54    if (!extension_lib_.Sym(CREATE_EXTENSION_INTERFACE_NAME,
55                            reinterpret_cast<void **>(&create_extension_intf_)) ||
56        !extension_lib_.Sym(DESTROY_EXTENSION_INTERFACE_NAME,
57                            reinterpret_cast<void **>(&destroy_extension_intf_))) {
58      DLOGE("Unable to load symbols, error = %s", extension_lib_.Error());
59      return kErrorUndefined;
60    }
61
62    error = create_extension_intf_(EXTENSION_VERSION_TAG, &extension_intf_);
63    if (error != kErrorNone) {
64      DLOGE("Unable to create interface");
65      return error;
66    }
67  } else {
68    DLOGW("Unable to load = %s, error = %s", EXTENSION_LIBRARY_NAME, extension_lib_.Error());
69  }
70
71  error = HWInfoInterface::Create(&hw_info_intf_);
72  if (error != kErrorNone) {
73    goto CleanupOnError;
74  }
75
76  error = hw_info_intf_->GetHWResourceInfo(&hw_resource_);
77  if (error != kErrorNone) {
78    goto CleanupOnError;
79  }
80
81  error = comp_mgr_.Init(hw_resource_, extension_intf_, buffer_allocator_,
82                         buffer_sync_handler_, socket_handler_);
83
84  if (error != kErrorNone) {
85    goto CleanupOnError;
86  }
87
88  error = ColorManagerProxy::Init(hw_resource_);
89  // if failed, doesn't affect display core functionalities.
90  if (error != kErrorNone) {
91    DLOGW("Unable creating color manager and continue without it.");
92  }
93
94  return kErrorNone;
95
96CleanupOnError:
97  if (hw_info_intf_) {
98    HWInfoInterface::Destroy(hw_info_intf_);
99  }
100
101  return error;
102}
103
104DisplayError CoreImpl::Deinit() {
105  SCOPE_LOCK(locker_);
106
107  ColorManagerProxy::Deinit();
108
109  comp_mgr_.Deinit();
110  HWInfoInterface::Destroy(hw_info_intf_);
111
112  return kErrorNone;
113}
114
115DisplayError CoreImpl::CreateDisplay(DisplayType type, DisplayEventHandler *event_handler,
116                                     DisplayInterface **intf) {
117  SCOPE_LOCK(locker_);
118
119  if (!event_handler || !intf) {
120    return kErrorParameters;
121  }
122
123  DisplayBase *display_base = NULL;
124
125  switch (type) {
126  case kPrimary:
127    display_base = new DisplayPrimary(event_handler, hw_info_intf_, buffer_sync_handler_,
128                                      &comp_mgr_);
129    break;
130  case kHDMI:
131    display_base = new DisplayHDMI(event_handler, hw_info_intf_, buffer_sync_handler_,
132                                   &comp_mgr_);
133    break;
134  case kVirtual:
135    display_base = new DisplayVirtual(event_handler, hw_info_intf_, buffer_sync_handler_,
136                                      &comp_mgr_);
137    break;
138  default:
139    DLOGE("Spurious display type %d", type);
140    return kErrorParameters;
141  }
142
143  if (!display_base) {
144    return kErrorMemory;
145  }
146
147  DisplayError error = display_base->Init();
148  if (error != kErrorNone) {
149    delete display_base;
150    return error;
151  }
152
153  *intf = display_base;
154  return kErrorNone;
155}
156
157DisplayError CoreImpl::DestroyDisplay(DisplayInterface *intf) {
158  SCOPE_LOCK(locker_);
159
160  if (!intf) {
161    return kErrorParameters;
162  }
163
164  DisplayBase *display_base = static_cast<DisplayBase *>(intf);
165  display_base->Deinit();
166  delete display_base;
167
168  return kErrorNone;
169}
170
171DisplayError CoreImpl::SetMaxBandwidthMode(HWBwModes mode) {
172  SCOPE_LOCK(locker_);
173
174  return comp_mgr_.SetMaxBandwidthMode(mode);
175}
176
177DisplayError CoreImpl::GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo *hw_disp_info) {
178  return hw_info_intf_->GetFirstDisplayInterfaceType(hw_disp_info);
179}
180
181}  // namespace sdm
182
183