1 /*
2  *  Copyright (c) 2013-15, The Linux Foundation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above
10  *       copyright notice, this list of conditions and the following
11  *       disclaimer in the documentation and/or other materials provided
12  *       with the distribution.
13  *     * Neither the name of The Linux Foundation nor the names of its
14  *       contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR CLIENTS; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <hwc_qclient.h>
31 #include <IQService.h>
32 #include <hwc_utils.h>
33 #include <mdp_version.h>
34 #include <hwc_mdpcomp.h>
35 #include <hwc_virtual.h>
36 #include <overlay.h>
37 #include <display_config.h>
38 #include <dlfcn.h>
39 
40 #define QCLIENT_DEBUG 0
41 
42 using namespace android;
43 using namespace qService;
44 using namespace qhwc;
45 using namespace overlay;
46 using namespace qdutils;
47 
48 namespace qClient {
49 
50 // ----------------------------------------------------------------------------
QClient(hwc_context_t * ctx)51 QClient::QClient(hwc_context_t *ctx) : mHwcContext(ctx),
52         mMPDeathNotifier(new MPDeathNotifier(ctx))
53 {
54     ALOGD_IF(QCLIENT_DEBUG, "QClient Constructor invoked");
55 }
56 
~QClient()57 QClient::~QClient()
58 {
59     ALOGD_IF(QCLIENT_DEBUG,"QClient Destructor invoked");
60 }
61 
securing(hwc_context_t * ctx,uint32_t startEnd)62 static void securing(hwc_context_t *ctx, uint32_t startEnd) {
63     //The only way to make this class in this process subscribe to media
64     //player's death.
65     IMediaDeathNotifier::getMediaPlayerService();
66 
67     ctx->mDrawLock.lock();
68     ctx->mSecuring = startEnd;
69     //We're done securing
70     if(startEnd == IQService::END)
71         ctx->mSecureMode = true;
72     ctx->mDrawLock.unlock();
73 
74     if(ctx->proc)
75         ctx->proc->invalidate(ctx->proc);
76 }
77 
unsecuring(hwc_context_t * ctx,uint32_t startEnd)78 static void unsecuring(hwc_context_t *ctx, uint32_t startEnd) {
79     ctx->mDrawLock.lock();
80     ctx->mSecuring = startEnd;
81     //We're done unsecuring
82     if(startEnd == IQService::END)
83         ctx->mSecureMode = false;
84     ctx->mDrawLock.unlock();
85 
86     if(ctx->proc)
87         ctx->proc->invalidate(ctx->proc);
88 }
89 
died()90 void QClient::MPDeathNotifier::died() {
91     mHwcContext->mDrawLock.lock();
92     ALOGD_IF(QCLIENT_DEBUG, "Media Player died");
93     mHwcContext->mSecuring = false;
94     mHwcContext->mSecureMode = false;
95     mHwcContext->mDrawLock.unlock();
96     if(mHwcContext->proc)
97         mHwcContext->proc->invalidate(mHwcContext->proc);
98 }
99 
screenRefresh(hwc_context_t * ctx)100 static android::status_t screenRefresh(hwc_context_t *ctx) {
101     status_t result = NO_INIT;
102     if(ctx->proc) {
103         ctx->proc->invalidate(ctx->proc);
104         result = NO_ERROR;
105     }
106     return result;
107 }
108 
setExtOrientation(hwc_context_t * ctx,uint32_t orientation)109 static void setExtOrientation(hwc_context_t *ctx, uint32_t orientation) {
110     ctx->mExtOrientation = orientation;
111 }
112 
isExternalConnected(hwc_context_t * ctx,Parcel * outParcel)113 static void isExternalConnected(hwc_context_t* ctx, Parcel* outParcel) {
114     int connected;
115     connected = ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].connected ? 1 : 0;
116     outParcel->writeInt32(connected);
117 }
118 
getDisplayAttributes(hwc_context_t * ctx,const Parcel * inParcel,Parcel * outParcel)119 static void getDisplayAttributes(hwc_context_t* ctx, const Parcel* inParcel,
120         Parcel* outParcel) {
121     int dpy = inParcel->readInt32();
122     outParcel->writeInt32(ctx->dpyAttr[dpy].vsync_period);
123     if (ctx->dpyAttr[dpy].customFBSize) {
124         outParcel->writeInt32(ctx->dpyAttr[dpy].xres_new);
125         outParcel->writeInt32(ctx->dpyAttr[dpy].yres_new);
126     } else {
127         outParcel->writeInt32(ctx->dpyAttr[dpy].xres);
128         outParcel->writeInt32(ctx->dpyAttr[dpy].yres);
129     }
130     outParcel->writeFloat(ctx->dpyAttr[dpy].xdpi);
131     outParcel->writeFloat(ctx->dpyAttr[dpy].ydpi);
132     //XXX: Need to check what to return for HDMI
133     outParcel->writeInt32(ctx->mMDP.panel);
134 }
setHSIC(const Parcel * inParcel)135 static void setHSIC(const Parcel* inParcel) {
136     int dpy = inParcel->readInt32();
137     ALOGD_IF(0, "In %s: dpy = %d", __FUNCTION__, dpy);
138     HSICData_t hsic_data;
139     hsic_data.hue = inParcel->readInt32();
140     hsic_data.saturation = inParcel->readFloat();
141     hsic_data.intensity = inParcel->readInt32();
142     hsic_data.contrast = inParcel->readFloat();
143     //XXX: Actually set the HSIC data through ABL lib
144 }
145 
146 
setBufferMirrorMode(hwc_context_t * ctx,uint32_t enable)147 static void setBufferMirrorMode(hwc_context_t *ctx, uint32_t enable) {
148     ctx->mBufferMirrorMode = enable;
149 }
150 
getDisplayVisibleRegion(hwc_context_t * ctx,int dpy,Parcel * outParcel)151 static status_t getDisplayVisibleRegion(hwc_context_t* ctx, int dpy,
152                                 Parcel* outParcel) {
153     // Get the info only if the dpy is valid
154     if(dpy >= HWC_DISPLAY_PRIMARY && dpy <= HWC_DISPLAY_VIRTUAL) {
155         Locker::Autolock _sl(ctx->mDrawLock);
156         if(dpy && (ctx->mExtOrientation || ctx->mBufferMirrorMode)) {
157             // Return the destRect on external, if external orienation
158             // is enabled
159             outParcel->writeInt32(ctx->dpyAttr[dpy].mDstRect.left);
160             outParcel->writeInt32(ctx->dpyAttr[dpy].mDstRect.top);
161             outParcel->writeInt32(ctx->dpyAttr[dpy].mDstRect.right);
162             outParcel->writeInt32(ctx->dpyAttr[dpy].mDstRect.bottom);
163         } else {
164             outParcel->writeInt32(ctx->mViewFrame[dpy].left);
165             outParcel->writeInt32(ctx->mViewFrame[dpy].top);
166             outParcel->writeInt32(ctx->mViewFrame[dpy].right);
167             outParcel->writeInt32(ctx->mViewFrame[dpy].bottom);
168         }
169         return NO_ERROR;
170     } else {
171         ALOGE("In %s: invalid dpy index %d", __FUNCTION__, dpy);
172         return BAD_VALUE;
173     }
174 }
175 
176 // USed for setting the secondary(hdmi/wfd) status
setSecondaryDisplayStatus(hwc_context_t * ctx,const Parcel * inParcel)177 static void setSecondaryDisplayStatus(hwc_context_t *ctx,
178                                       const Parcel* inParcel) {
179     uint32_t dpy = inParcel->readInt32();
180     uint32_t status = inParcel->readInt32();
181     ALOGD_IF(QCLIENT_DEBUG, "%s: dpy = %d status = %s", __FUNCTION__,
182                                         dpy, getExternalDisplayState(status));
183 
184     if(dpy > HWC_DISPLAY_PRIMARY && dpy <= HWC_DISPLAY_VIRTUAL) {
185         if(dpy == HWC_DISPLAY_VIRTUAL && status == qdutils::EXTERNAL_OFFLINE) {
186             ctx->mWfdSyncLock.lock();
187             ctx->mWfdSyncLock.signal();
188             ctx->mWfdSyncLock.unlock();
189         } else if(status == qdutils::EXTERNAL_PAUSE) {
190             handle_pause(ctx, dpy);
191         } else if(status == qdutils::EXTERNAL_RESUME) {
192             handle_resume(ctx, dpy);
193         }
194     } else {
195         ALOGE("%s: Invalid dpy %d", __FUNCTION__, dpy);
196         return;
197     }
198 }
199 
200 
setViewFrame(hwc_context_t * ctx,const Parcel * inParcel)201 static status_t setViewFrame(hwc_context_t* ctx, const Parcel* inParcel) {
202     int dpy = inParcel->readInt32();
203     if(dpy >= HWC_DISPLAY_PRIMARY && dpy <= HWC_DISPLAY_VIRTUAL) {
204         Locker::Autolock _sl(ctx->mDrawLock);
205         ctx->mViewFrame[dpy].left   = inParcel->readInt32();
206         ctx->mViewFrame[dpy].top    = inParcel->readInt32();
207         ctx->mViewFrame[dpy].right  = inParcel->readInt32();
208         ctx->mViewFrame[dpy].bottom = inParcel->readInt32();
209         ALOGD_IF(QCLIENT_DEBUG, "%s: mViewFrame[%d] = [%d %d %d %d]",
210             __FUNCTION__, dpy,
211             ctx->mViewFrame[dpy].left, ctx->mViewFrame[dpy].top,
212             ctx->mViewFrame[dpy].right, ctx->mViewFrame[dpy].bottom);
213         return NO_ERROR;
214     } else {
215         ALOGE("In %s: invalid dpy index %d", __FUNCTION__, dpy);
216         return BAD_VALUE;
217     }
218 }
219 
toggleDynamicDebug(hwc_context_t * ctx,const Parcel * inParcel)220 static void toggleDynamicDebug(hwc_context_t* ctx, const Parcel* inParcel) {
221     int debug_type = inParcel->readInt32();
222     bool enable = !!inParcel->readInt32();
223     ALOGD("%s: debug_type: %d enable:%d",
224             __FUNCTION__, debug_type, enable);
225     Locker::Autolock _sl(ctx->mDrawLock);
226     switch (debug_type) {
227         //break is ignored for DEBUG_ALL to toggle all of them at once
228         case IQService::DEBUG_ALL:
229         case IQService::DEBUG_MDPCOMP:
230             qhwc::MDPComp::dynamicDebug(enable);
231             if (debug_type != IQService::DEBUG_ALL)
232                 break;
233         case IQService::DEBUG_VSYNC:
234             ctx->vstate.debug = enable;
235             if (debug_type != IQService::DEBUG_ALL)
236                 break;
237         case IQService::DEBUG_VD:
238             HWCVirtualVDS::dynamicDebug(enable);
239             if (debug_type != IQService::DEBUG_ALL)
240                 break;
241         case IQService::DEBUG_PIPE_LIFECYCLE:
242             Overlay::debugPipeLifecycle(enable);
243             if (debug_type != IQService::DEBUG_ALL)
244                 break;
245     }
246 }
247 
setIdleTimeout(hwc_context_t * ctx,const Parcel * inParcel)248 static void setIdleTimeout(hwc_context_t* ctx, const Parcel* inParcel) {
249     uint32_t timeout = (uint32_t)inParcel->readInt32();
250     ALOGD("%s :%u ms", __FUNCTION__, timeout);
251     Locker::Autolock _sl(ctx->mDrawLock);
252     MDPComp::setIdleTimeout(timeout);
253 }
254 
setMaxPipesPerMixer(hwc_context_t * ctx,const Parcel * inParcel)255 static void setMaxPipesPerMixer(hwc_context_t* ctx, const Parcel* inParcel) {
256     uint32_t value = (uint32_t)inParcel->readInt32();
257     ALOGD("%s : setting MaxPipesPerMixer: %d ", __FUNCTION__, value);
258     Locker::Autolock _sl(ctx->mDrawLock);
259     MDPComp::setMaxPipesPerMixer(value);
260 }
261 
toggleBWC(hwc_context_t * ctx,const Parcel * inParcel)262 static void toggleBWC(hwc_context_t* ctx, const Parcel* inParcel) {
263     uint32_t enable = (uint32_t)inParcel->readInt32();
264     if(MDPVersion::getInstance().supportsBWC()) {
265         Locker::Autolock _sl(ctx->mDrawLock);
266         ctx->mBWCEnabled = (bool) enable;
267         ALOGI("%s: Set BWC to %d", __FUNCTION__, enable);
268     } else {
269         ALOGI("%s: Target doesn't support BWC", __FUNCTION__);
270     }
271 }
272 
configureDynRefreshRate(hwc_context_t * ctx,const Parcel * inParcel)273 static void configureDynRefreshRate(hwc_context_t* ctx,
274                                     const Parcel* inParcel) {
275     uint32_t op = (uint32_t)inParcel->readInt32();
276     uint32_t refresh_rate = (uint32_t)inParcel->readInt32();
277     MDPVersion& mdpHw = MDPVersion::getInstance();
278     uint32_t dpy = HWC_DISPLAY_PRIMARY;
279 
280     if(mdpHw.isDynFpsSupported()) {
281         Locker::Autolock _sl(ctx->mDrawLock);
282 
283         switch (op) {
284         case DISABLE_METADATA_DYN_REFRESH_RATE:
285             ctx->mUseMetaDataRefreshRate = false;
286             setRefreshRate(ctx, dpy, ctx->dpyAttr[dpy].refreshRate);
287             break;
288         case ENABLE_METADATA_DYN_REFRESH_RATE:
289             ctx->mUseMetaDataRefreshRate = true;
290             setRefreshRate(ctx, dpy, ctx->dpyAttr[dpy].refreshRate);
291             break;
292         case SET_BINDER_DYN_REFRESH_RATE:
293             if(ctx->mUseMetaDataRefreshRate)
294                 ALOGW("%s: Ignoring binder request to change refresh-rate",
295                       __FUNCTION__);
296             else {
297                 uint32_t rate = roundOff(refresh_rate);
298                 if((rate >= mdpHw.getMinFpsSupported() &&
299                     rate <= mdpHw.getMaxFpsSupported())) {
300                     setRefreshRate(ctx, dpy, rate);
301                 } else {
302                     ALOGE("%s: Requested refresh-rate should be between \
303                           (%d) and (%d). Given (%d)", __FUNCTION__,
304                           mdpHw.getMinFpsSupported(),
305                           mdpHw.getMaxFpsSupported(), rate);
306                 }
307             }
308             break;
309         default:
310             ALOGE("%s: Invalid op %d",__FUNCTION__,op);
311         }
312     }
313 }
314 
setPartialUpdatePref(hwc_context_t * ctx,uint32_t enable)315 static status_t setPartialUpdatePref(hwc_context_t *ctx, uint32_t enable) {
316     ALOGD("%s: enable: %d", __FUNCTION__, enable);
317     if(qhwc::MDPComp::setPartialUpdatePref(ctx, (bool)enable) < 0)
318         return NO_INIT;
319     return NO_ERROR;
320 }
321 
toggleScreenUpdate(hwc_context_t * ctx,uint32_t on)322 static void toggleScreenUpdate(hwc_context_t* ctx, uint32_t on) {
323     ALOGD("%s: toggle update: %d", __FUNCTION__, on);
324     if (on == 0) {
325         ctx->mDrawLock.lock();
326         ctx->dpyAttr[HWC_DISPLAY_PRIMARY].isPause = true;
327         ctx->mOverlay->configBegin();
328         ctx->mOverlay->configDone();
329         ctx->mRotMgr->clear();
330         if(!Overlay::displayCommit(ctx->dpyAttr[0].fd)) {
331             ALOGE("%s: Display commit failed", __FUNCTION__);
332         }
333         ctx->mDrawLock.unlock();
334     } else {
335         ctx->mDrawLock.lock();
336         ctx->dpyAttr[HWC_DISPLAY_PRIMARY].isPause = false;
337         ctx->mDrawLock.unlock();
338         ctx->proc->invalidate(ctx->proc);
339     }
340 }
341 
applyModeById(hwc_context_t * ctx,int32_t modeId)342 static void applyModeById(hwc_context_t* ctx, int32_t modeId) {
343     int err = ctx->mColorMode->applyModeByID(modeId);
344     if (err)
345         ALOGD("%s: Not able to apply mode: %d", __FUNCTION__, modeId);
346     else
347         ctx->proc->invalidate(ctx->proc);
348 }
349 
notifyCallback(uint32_t command,const Parcel * inParcel,Parcel * outParcel)350 status_t QClient::notifyCallback(uint32_t command, const Parcel* inParcel,
351         Parcel* outParcel) {
352     status_t ret = NO_ERROR;
353 
354     switch(command) {
355         case IQService::SECURING:
356             securing(mHwcContext, inParcel->readInt32());
357             break;
358         case IQService::UNSECURING:
359             unsecuring(mHwcContext, inParcel->readInt32());
360             break;
361         case IQService::SCREEN_REFRESH:
362             qhwc::MDPComp::setSingleFullScreenUpdate();
363             return screenRefresh(mHwcContext);
364             break;
365         case IQService::EXTERNAL_ORIENTATION:
366             setExtOrientation(mHwcContext, inParcel->readInt32());
367             break;
368         case IQService::BUFFER_MIRRORMODE:
369             setBufferMirrorMode(mHwcContext, inParcel->readInt32());
370             break;
371         case IQService::GET_DISPLAY_VISIBLE_REGION:
372             ret = getDisplayVisibleRegion(mHwcContext, inParcel->readInt32(),
373                                     outParcel);
374             break;
375         case IQService::CHECK_EXTERNAL_STATUS:
376             isExternalConnected(mHwcContext, outParcel);
377             break;
378         case IQService::GET_DISPLAY_ATTRIBUTES:
379             getDisplayAttributes(mHwcContext, inParcel, outParcel);
380             break;
381         case IQService::SET_HSIC_DATA:
382             setHSIC(inParcel);
383             break;
384         case IQService::SET_SECONDARY_DISPLAY_STATUS:
385             setSecondaryDisplayStatus(mHwcContext, inParcel);
386             break;
387         case IQService::SET_VIEW_FRAME:
388             setViewFrame(mHwcContext, inParcel);
389             break;
390         case IQService::DYNAMIC_DEBUG:
391             toggleDynamicDebug(mHwcContext, inParcel);
392             break;
393         case IQService::SET_IDLE_TIMEOUT:
394             setIdleTimeout(mHwcContext, inParcel);
395             break;
396         case IQService::SET_MAX_PIPES_PER_MIXER:
397             setMaxPipesPerMixer(mHwcContext, inParcel);
398             break;
399         case IQService::SET_PARTIAL_UPDATE:
400             ret = setPartialUpdatePref(mHwcContext, inParcel->readInt32());
401             break;
402         case IQService::TOGGLE_BWC:
403             toggleBWC(mHwcContext, inParcel);
404             break;
405         case IQService::CONFIGURE_DYN_REFRESH_RATE:
406             configureDynRefreshRate(mHwcContext, inParcel);
407             break;
408         case IQService::TOGGLE_SCREEN_UPDATE:
409             toggleScreenUpdate(mHwcContext, inParcel->readInt32());
410             break;
411         case IQService::APPLY_MODE_BY_ID:
412             applyModeById(mHwcContext, inParcel->readInt32());
413             break;
414         default:
415             ret = NO_ERROR;
416     }
417     return ret;
418 }
419 
420 }
421