1 /* Copyright (c) 2015, The Linux Foundataion. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 
30 #ifndef __HWC_COLOR_MANAGER_H__
31 #define __HWC_COLOR_MANAGER_H__
32 
33 #include <stdlib.h>
34 #include <binder/Parcel.h>
35 #include <powermanager/IPowerManager.h>
36 #include <binder/BinderService.h>
37 #include <core/sdm_types.h>
38 #include <utils/locker.h>
39 
40 namespace sdm {
41 
42 // This macro defines name for display APIs interface wrapper library.
43 // This macro shall be used to load library using dlopen().
44 #define DISPLAY_API_INTERFACE_LIBRARY_NAME "libsdm-disp-apis.so"
45 
46 // This macro defines variable name of display color APIs function tables
47 // This macro shall be used to specify name of the variable in dlsym().
48 #define DISPLAY_API_FUNC_TABLES "display_color_apis_ftables"
49 #define QDCM_DIAG_CLIENT_LIBRARY_NAME "libsdm-diag.so"
50 #define INIT_QDCM_DIAG_CLIENT_NAME "QDCMDiagInit"
51 #define DEINIT_QDCM_DIAG_CLIENT_NAME "QDCMDiagDeInit"
52 
53 typedef int (*QDCMDiagInit)(void *ftables);
54 
55 typedef int (*QDCMDiagDeInit)(void);
56 
57 // Class to encapsulte all details of managing QDCM operating mode.
58 class HWCQDCMModeManager {
59  public:
60   static const uint32_t kSocketCMDMaxLength = 4096;
61   static const uint32_t kFullWakeLock = 0x0000001a;
62   static const uint32_t kAcquireCauseWakeup = 0x10000000;
63   static const uint32_t kONAfterRelease = 0x20000000;
64   enum ActiveFeatureID {
65     kCABLFeature,
66     kADFeature,
67     kSVIFeature,
68     kMaxNumActiveFeature,
69   };
70 
71   struct ActiveFeatureCMD {
72     const char *cmd_on = NULL;
73     const char *cmd_off = NULL;
74     const char *cmd_query_status = NULL;
75     const char *running = NULL;
ActiveFeatureCMDActiveFeatureCMD76     ActiveFeatureCMD(const char *arg1, const char *arg2, const char *arg3, const char *arg4)
77         : cmd_on(arg1), cmd_off(arg2), cmd_query_status(arg3), running(arg4) {}
78   };
79 
80   static const ActiveFeatureCMD kActiveFeatureCMD[kMaxNumActiveFeature];
81 
82  public:
83   static HWCQDCMModeManager *CreateQDCMModeMgr();
84   ~HWCQDCMModeManager();
85   int EnableQDCMMode(bool enable, HWCDisplay *hwc_display);
86 
87  protected:
88   bool SendSocketCmd();
89   int AcquireAndroidWakeLock(bool enable);
90   int EnableActiveFeatures(bool enable);
91   int EnableActiveFeatures(bool enable, const ActiveFeatureCMD &cmds, bool *was_running);
92 
93  private:
94   bool cabl_was_running_ = false;
95   int socket_fd_ = -1;
96   android::sp<android::IBinder> wakelock_token_ = NULL;
97   android::sp<android::IPowerManager> power_mgr_ = NULL;
98   uint32_t entry_timeout_ = 0;
99   static const char *const kSocketName;
100   static const char *const kTagName;
101   static const char *const kPackageName;
102 };
103 
104 // Class to encapsulte all HWC/OS specific behaviours for ColorManager.
105 class HWCColorManager {
106  public:
107   static const int kNumSolidFillLayers = 2;
108   static HWCColorManager *CreateColorManager();
109   static int CreatePayloadFromParcel(const android::Parcel &in, uint32_t *disp_id,
110                                      PPDisplayAPIPayload *sink);
111   static void MarshallStructIntoParcel(const PPDisplayAPIPayload &data,
112                                        android::Parcel *out_parcel);
113 
114   ~HWCColorManager();
115   void DestroyColorManager();
116   int EnableQDCMMode(bool enable, HWCDisplay *hwc_display);
117   int SetSolidFill(const void *params, bool enable, HWCDisplay *hwc_display);
118   bool SolidFillLayersPrepare(hwc_display_contents_1_t **displays, HWCDisplay *hwc_display);
119   bool SolidFillLayersSet(hwc_display_contents_1_t **displays, HWCDisplay *hwc_display);
120 
121  protected:
122   int CreateSolidFillLayers(HWCDisplay *hwc_display);
123   void DestroySolidFillLayers();
124   static uint32_t Get8BitsARGBColorValue(const PPColorFillParams &params);
125 
126  private:
127   void *color_apis_lib_ = NULL;
128   void *diag_client_lib_ = NULL;
129   void *color_apis_ = NULL;
130   QDCMDiagInit qdcm_diag_init_ = NULL;
131   QDCMDiagDeInit qdcm_diag_deinit_ = NULL;
132   HWCQDCMModeManager *qdcm_mode_mgr_ = NULL;
133 
134   bool solid_fill_enable_ = false;
135   PPColorFillParams solid_fill_params_;
136   hwc_display_contents_1_t *solid_fill_layers_ = NULL;
137   Locker locker_;
138 };
139 
140 }  // namespace sdm
141 
142 #endif  // __HWC_COLOR_MANAGER_H__
143