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 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 
39 namespace sdm {
40 
CoreImpl(BufferAllocator * buffer_allocator,BufferSyncHandler * buffer_sync_handler,SocketHandler * socket_handler)41 CoreImpl::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 
Init()48 DisplayError 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   if (hw_info_intf_ == NULL) {
77     return kErrorResources;
78   }
79   error = hw_info_intf_->GetHWResourceInfo(&hw_resource_);
80   if (error != kErrorNone) {
81     goto CleanupOnError;
82   }
83 
84   error = comp_mgr_.Init(hw_resource_, extension_intf_, buffer_allocator_,
85                          buffer_sync_handler_, socket_handler_);
86 
87   if (error != kErrorNone) {
88     goto CleanupOnError;
89   }
90 
91   error = ColorManagerProxy::Init(hw_resource_);
92   // if failed, doesn't affect display core functionalities.
93   if (error != kErrorNone) {
94     DLOGW("Unable creating color manager and continue without it.");
95   }
96 
97   return kErrorNone;
98 
99 CleanupOnError:
100   if (hw_info_intf_) {
101     HWInfoInterface::Destroy(hw_info_intf_);
102   }
103 
104   return error;
105 }
106 
Deinit()107 DisplayError CoreImpl::Deinit() {
108   SCOPE_LOCK(locker_);
109 
110   ColorManagerProxy::Deinit();
111 
112   comp_mgr_.Deinit();
113   HWInfoInterface::Destroy(hw_info_intf_);
114 
115   return kErrorNone;
116 }
117 
CreateDisplay(DisplayType type,DisplayEventHandler * event_handler,DisplayInterface ** intf)118 DisplayError CoreImpl::CreateDisplay(DisplayType type, DisplayEventHandler *event_handler,
119                                      DisplayInterface **intf) {
120   SCOPE_LOCK(locker_);
121 
122   if (!event_handler || !intf) {
123     return kErrorParameters;
124   }
125 
126   DisplayBase *display_base = NULL;
127 
128   switch (type) {
129   case kPrimary:
130     display_base = new DisplayPrimary(event_handler, hw_info_intf_, buffer_sync_handler_,
131                                       buffer_allocator_, &comp_mgr_);
132     break;
133   case kHDMI:
134     display_base = new DisplayHDMI(event_handler, hw_info_intf_, buffer_sync_handler_,
135                                    buffer_allocator_, &comp_mgr_);
136     break;
137   case kVirtual:
138     display_base = new DisplayVirtual(event_handler, hw_info_intf_, buffer_sync_handler_,
139                                       buffer_allocator_, &comp_mgr_);
140     break;
141   default:
142     DLOGE("Spurious display type %d", type);
143     return kErrorParameters;
144   }
145 
146   if (!display_base) {
147     return kErrorMemory;
148   }
149 
150   DisplayError error = display_base->Init();
151   if (error != kErrorNone) {
152     delete display_base;
153     return error;
154   }
155 
156   *intf = display_base;
157   return kErrorNone;
158 }
159 
DestroyDisplay(DisplayInterface * intf)160 DisplayError CoreImpl::DestroyDisplay(DisplayInterface *intf) {
161   SCOPE_LOCK(locker_);
162 
163   if (!intf) {
164     return kErrorParameters;
165   }
166 
167   DisplayBase *display_base = static_cast<DisplayBase *>(intf);
168   display_base->Deinit();
169   delete display_base;
170 
171   return kErrorNone;
172 }
173 
SetMaxBandwidthMode(HWBwModes mode)174 DisplayError CoreImpl::SetMaxBandwidthMode(HWBwModes mode) {
175   SCOPE_LOCK(locker_);
176 
177   return comp_mgr_.SetMaxBandwidthMode(mode);
178 }
179 
GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo * hw_disp_info)180 DisplayError CoreImpl::GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo *hw_disp_info) {
181   return hw_info_intf_->GetFirstDisplayInterfaceType(hw_disp_info);
182 }
183 
IsColorTransformSupported()184 bool CoreImpl::IsColorTransformSupported() {
185     return (hw_resource_.has_ppp) ? false : true;
186 }
187 }  // namespace sdm
188 
189