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 
39 namespace sdm {
40 
CoreImpl(BufferAllocator * buffer_allocator,BufferSyncHandler * buffer_sync_handler)41 CoreImpl::CoreImpl(BufferAllocator *buffer_allocator,
42                    BufferSyncHandler *buffer_sync_handler)
43   : buffer_allocator_(buffer_allocator), buffer_sync_handler_(buffer_sync_handler) {
44 }
45 
Init()46 DisplayError CoreImpl::Init() {
47   SCOPE_LOCK(locker_);
48   DisplayError error = kErrorNone;
49 
50   // Try to load extension library & get handle to its interface.
51   extension_lib_ = ::dlopen(EXTENSION_LIBRARY_NAME, RTLD_NOW);
52   if (extension_lib_) {
53     void **create_sym = reinterpret_cast<void **>(&create_extension_intf_);
54     void **destroy_sym = reinterpret_cast<void **>(&destroy_extension_intf_);
55 
56     *create_sym = ::dlsym(extension_lib_, CREATE_EXTENSION_INTERFACE_NAME);
57     *destroy_sym = ::dlsym(extension_lib_, DESTROY_EXTENSION_INTERFACE_NAME);
58 
59     if (!create_extension_intf_ || !destroy_extension_intf_) {
60       DLOGE("Unable to load symbols, error = %s", ::dlerror());
61       ::dlclose(extension_lib_);
62       return kErrorUndefined;
63     }
64 
65     error = create_extension_intf_(EXTENSION_VERSION_TAG, &extension_intf_);
66     if (error != kErrorNone) {
67       DLOGE("Unable to create interface, error = %s", ::dlerror());
68       ::dlclose(extension_lib_);
69       return error;
70     }
71   } else {
72     DLOGW("Unable to load = %s, error = %s", EXTENSION_LIBRARY_NAME, ::dlerror());
73   }
74 
75   error = HWInfoInterface::Create(&hw_info_intf_);
76   if (error != kErrorNone) {
77     goto CleanupOnError;
78   }
79 
80   hw_resource_ = new HWResourceInfo();
81   if (!hw_resource_) {
82     error = kErrorMemory;
83     goto CleanupOnError;
84   }
85 
86   error = hw_info_intf_->GetHWResourceInfo(hw_resource_);
87   if (error != kErrorNone) {
88     goto CleanupOnError;
89   }
90 
91   error = comp_mgr_.Init(*hw_resource_, extension_intf_, buffer_sync_handler_);
92   if (error != kErrorNone) {
93     goto CleanupOnError;
94   }
95 
96   if (extension_intf_ && hw_resource_->hw_rot_info.num_rotator) {
97     error = extension_intf_->CreateRotator(hw_resource_->hw_rot_info, buffer_allocator_,
98                                            buffer_sync_handler_, &rotator_intf_);
99     if (error != kErrorNone) {
100       DLOGW("rotation is not supported");
101     }
102   }
103 
104   error = ColorManagerProxy::Init(*hw_resource_);
105   // if failed, doesn't affect display core functionalities.
106   if (error != kErrorNone) {
107     DLOGW("Unable creating color manager and continue without it.");
108   }
109 
110   return kErrorNone;
111 
112 CleanupOnError:
113   if (hw_info_intf_) {
114     HWInfoInterface::Destroy(hw_info_intf_);
115   }
116 
117   if (hw_resource_) {
118     delete hw_resource_;
119   }
120 
121   if (extension_lib_) {
122     destroy_extension_intf_(extension_intf_);
123     ::dlclose(extension_lib_);
124   }
125 
126   return error;
127 }
128 
Deinit()129 DisplayError CoreImpl::Deinit() {
130   SCOPE_LOCK(locker_);
131 
132   if (extension_intf_ && hw_resource_->hw_rot_info.num_rotator) {
133     extension_intf_->DestroyRotator(rotator_intf_);
134   }
135 
136   ColorManagerProxy::Deinit();
137 
138   comp_mgr_.Deinit();
139   HWInfoInterface::Destroy(hw_info_intf_);
140 
141   if (hw_resource_) {
142     delete hw_resource_;
143   }
144 
145   if (extension_lib_) {
146     destroy_extension_intf_(extension_intf_);
147     ::dlclose(extension_lib_);
148   }
149 
150   return kErrorNone;
151 }
152 
CreateDisplay(DisplayType type,DisplayEventHandler * event_handler,DisplayInterface ** intf)153 DisplayError CoreImpl::CreateDisplay(DisplayType type, DisplayEventHandler *event_handler,
154                                      DisplayInterface **intf) {
155   SCOPE_LOCK(locker_);
156 
157   if (!event_handler || !intf) {
158     return kErrorParameters;
159   }
160 
161   DisplayBase *display_base = NULL;
162 
163   switch (type) {
164   case kPrimary:
165     display_base = new DisplayPrimary(event_handler, hw_info_intf_, buffer_sync_handler_,
166                                       &comp_mgr_, rotator_intf_);
167     break;
168   case kHDMI:
169     display_base = new DisplayHDMI(event_handler, hw_info_intf_, buffer_sync_handler_,
170                                    &comp_mgr_, rotator_intf_);
171     break;
172   case kVirtual:
173     display_base = new DisplayVirtual(event_handler, hw_info_intf_, buffer_sync_handler_,
174                                       &comp_mgr_, rotator_intf_);
175     break;
176   default:
177     DLOGE("Spurious display type %d", type);
178     return kErrorParameters;
179   }
180 
181   if (!display_base) {
182     return kErrorMemory;
183   }
184 
185   DisplayError error = display_base->Init();
186   if (error != kErrorNone) {
187     delete display_base;
188     return error;
189   }
190 
191   *intf = display_base;
192   return kErrorNone;
193 }
194 
DestroyDisplay(DisplayInterface * intf)195 DisplayError CoreImpl::DestroyDisplay(DisplayInterface *intf) {
196   SCOPE_LOCK(locker_);
197 
198   if (!intf) {
199     return kErrorParameters;
200   }
201 
202   DisplayBase *display_base = static_cast<DisplayBase *>(intf);
203   display_base->Deinit();
204   delete display_base;
205 
206   return kErrorNone;
207 }
208 
SetMaxBandwidthMode(HWBwModes mode)209 DisplayError CoreImpl::SetMaxBandwidthMode(HWBwModes mode) {
210   SCOPE_LOCK(locker_);
211 
212   return comp_mgr_.SetMaxBandwidthMode(mode);
213 }
214 
215 }  // namespace sdm
216 
217