1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  * Copyright (C) 2012-2014, The Linux Foundation. All rights reserved.
4  *
5  * Not a Contribution, Apache license notifications and license are retained
6  * for attribution purposes only.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #include <fcntl.h>
21 #include <errno.h>
22 
23 #include <cutils/log.h>
24 #include <overlayWriteback.h>
25 #include "hwc_utils.h"
26 #include "hwc_fbupdate.h"
27 #include "hwc_mdpcomp.h"
28 #include "hwc_dump_layers.h"
29 #include "hwc_copybit.h"
30 #include "hwc_virtual.h"
31 #include "sync/sync.h"
32 #include <utils/Trace.h>
33 
34 #define HWCVIRTUAL_LOG 0
35 
36 using namespace qhwc;
37 using namespace overlay;
38 
39 bool HWCVirtualVDS::sVDDumpEnabled = false;
40 
init(hwc_context_t * ctx)41 void HWCVirtualVDS::init(hwc_context_t *ctx) {
42     const int dpy = HWC_DISPLAY_VIRTUAL;
43     mScalingWidth = 0, mScalingHeight = 0;
44     initCompositionResources(ctx, dpy);
45 
46     if(ctx->mFBUpdate[dpy])
47         ctx->mFBUpdate[dpy]->reset();
48     if(ctx->mMDPComp[dpy])
49         ctx->mMDPComp[dpy]->reset();
50 }
51 
destroy(hwc_context_t * ctx,size_t,hwc_display_contents_1_t ** displays)52 void HWCVirtualVDS::destroy(hwc_context_t *ctx, size_t /*numDisplays*/,
53                        hwc_display_contents_1_t** displays) {
54     int dpy = HWC_DISPLAY_VIRTUAL;
55 
56     //Cleanup virtual display objs, since there is no explicit disconnect
57     if(ctx->dpyAttr[dpy].connected && (displays[dpy] == NULL)) {
58         ctx->dpyAttr[dpy].connected = false;
59         ctx->dpyAttr[dpy].isPause = false;
60 
61         destroyCompositionResources(ctx, dpy);
62 
63         // signal synclock to indicate successful wfd teardown
64         ctx->mWfdSyncLock.lock();
65         ctx->mWfdSyncLock.signal();
66         ctx->mWfdSyncLock.unlock();
67     }
68 }
69 
prepare(hwc_composer_device_1 * dev,hwc_display_contents_1_t * list)70 int HWCVirtualVDS::prepare(hwc_composer_device_1 *dev,
71         hwc_display_contents_1_t *list) {
72     ATRACE_CALL();
73     //XXX: Fix when framework support is added
74     hwc_context_t* ctx = (hwc_context_t*)(dev);
75     const int dpy = HWC_DISPLAY_VIRTUAL;
76 
77     if (list && list->outbuf && list->numHwLayers > 0) {
78         reset_layer_prop(ctx, dpy, (int)list->numHwLayers - 1);
79         uint32_t last = (uint32_t)list->numHwLayers - 1;
80         hwc_layer_1_t *fbLayer = &list->hwLayers[last];
81         int fbWidth = 0, fbHeight = 0;
82         getLayerResolution(fbLayer, fbWidth, fbHeight);
83         ctx->dpyAttr[dpy].xres = fbWidth;
84         ctx->dpyAttr[dpy].yres = fbHeight;
85 
86         if(ctx->dpyAttr[dpy].connected == false) {
87             ctx->dpyAttr[dpy].connected = true;
88             ctx->dpyAttr[dpy].isPause = false;
89             // We set the vsync period to the primary refresh rate, leaving
90             // it up to the consumer to decide how fast to consume frames.
91             ctx->dpyAttr[dpy].vsync_period
92                               = ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period;
93             init(ctx);
94             // Do one padding round for cases where primary has all pipes
95             // The virtual composition falls back to GPU in such cases.
96             ctx->isPaddingRound = true;
97         }
98         if(!ctx->dpyAttr[dpy].isPause) {
99             ctx->dpyAttr[dpy].isConfiguring = false;
100             ctx->dpyAttr[dpy].fd = Writeback::getInstance()->getFbFd();
101             private_handle_t *ohnd = (private_handle_t *)list->outbuf;
102 
103             setMDPScalingMode(ctx, ohnd, dpy);
104 
105             mScalingWidth = getWidth(ohnd);
106             mScalingHeight = getHeight(ohnd);
107 
108             Writeback::getInstance()->configureDpyInfo(mScalingWidth,
109                                                         mScalingHeight);
110             setListStats(ctx, list, dpy);
111 
112             if(ctx->mMDPComp[dpy]->prepare(ctx, list) < 0) {
113                 const int fbZ = 0;
114                 if(not ctx->mFBUpdate[dpy]->prepareAndValidate(ctx, list, fbZ))
115                 {
116                     ctx->mOverlay->clear(dpy);
117                     ctx->mLayerRotMap[dpy]->clear();
118                 }
119             }
120         } else {
121             /* Virtual Display is in Pause state.
122              * Mark all application layers as OVERLAY so that
123              * GPU will not compose.
124              */
125             Writeback::getInstance(); //Ensure that WB is active during pause
126             for(size_t i = 0 ;i < (size_t)(list->numHwLayers - 1); i++) {
127                 hwc_layer_1_t *layer = &list->hwLayers[i];
128                 layer->compositionType = HWC_OVERLAY;
129             }
130         }
131     }
132     return 0;
133 }
134 
set(hwc_context_t * ctx,hwc_display_contents_1_t * list)135 int HWCVirtualVDS::set(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
136     ATRACE_CALL();
137     int ret = 0;
138     const int dpy = HWC_DISPLAY_VIRTUAL;
139 
140     if (list && list->outbuf && list->numHwLayers > 0) {
141         uint32_t last = (uint32_t)list->numHwLayers - 1;
142         hwc_layer_1_t *fbLayer = &list->hwLayers[last];
143 
144         if(ctx->dpyAttr[dpy].connected
145                 && (!ctx->dpyAttr[dpy].isPause))
146         {
147             private_handle_t *ohnd = (private_handle_t *)list->outbuf;
148             int format = ohnd->format;
149             if (format == HAL_PIXEL_FORMAT_RGBA_8888)
150                 format = HAL_PIXEL_FORMAT_RGBX_8888;
151             Writeback::getInstance()->setOutputFormat(
152                                     utils::getMdpFormat(format));
153 
154             // Configure WB secure mode based on output buffer handle
155             if(! Writeback::getInstance()->setSecure(isSecureBuffer(ohnd)))
156             {
157                 ALOGE("Failed to set WB secure mode: %d for virtual display",
158                     isSecureBuffer(ohnd));
159                 return false;
160             }
161 
162             int fd = -1; //FenceFD from the Copybit
163             hwc_sync(ctx, list, dpy, fd);
164 
165             // Dump the layers for virtual
166             if(ctx->mHwcDebug[dpy])
167                 ctx->mHwcDebug[dpy]->dumpLayers(list);
168 
169             if (!ctx->mMDPComp[dpy]->draw(ctx, list)) {
170                 ALOGE("%s: MDPComp draw failed", __FUNCTION__);
171                 ret = -1;
172             }
173             // We need an FB layer handle check to cater for this usecase:
174             // Video is playing in landscape on primary, then launch
175             // ScreenRecord app.
176             // In this scenario, the first VDS draw call will have HWC
177             // composition and VDS does nit involve GPU to get eglSwapBuffer
178             // to get valid fb handle.
179             if (fbLayer->handle && !ctx->mFBUpdate[dpy]->draw(ctx,
180                         (private_handle_t *)fbLayer->handle)) {
181                 ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__);
182                 ret = -1;
183             }
184 
185             Writeback::getInstance()->queueBuffer(ohnd->fd,
186                                         (uint32_t)ohnd->offset);
187             if(!Overlay::displayCommit(ctx->dpyAttr[dpy].fd)) {
188                 ALOGE("%s: display commit fail!", __FUNCTION__);
189                 ret = -1;
190             }
191 
192             if(sVDDumpEnabled) {
193                 char bufferName[128];
194                 // Dumping frame buffer
195                 sync_wait(fbLayer->acquireFenceFd, 1000);
196                 snprintf(bufferName, sizeof(bufferName), "vds.fb");
197                 dumpBuffer((private_handle_t *)fbLayer->handle, bufferName);
198                 // Dumping WB output for non-secure session
199                 if(!isSecureBuffer(ohnd)) {
200                     sync_wait(list->retireFenceFd, 1000);
201                     snprintf(bufferName, sizeof(bufferName), "vds.wb");
202                     dumpBuffer(ohnd, bufferName);
203                 }
204             }
205         } else if(list->outbufAcquireFenceFd >= 0) {
206             //If we dont handle the frame, set retireFenceFd to outbufFenceFd,
207             //which will make sure, the framework waits on it and closes it.
208             //The other way is to wait on outbufFenceFd ourselves, close it and
209             //set retireFenceFd to -1. Since we want hwc to be async, choosing
210             //the former.
211             //Also dup because, the closeAcquireFds() will close the outbufFence
212             list->retireFenceFd = dup(list->outbufAcquireFenceFd);
213         }
214     }
215 
216     closeAcquireFds(list);
217     return ret;
218 }
219 
220 /* We set scaling mode on the VD if the output handle width and height
221    differs from the virtual frame buffer width and height. */
setMDPScalingMode(hwc_context_t * ctx,private_handle_t * ohnd,int dpy)222 void HWCVirtualVDS::setMDPScalingMode(hwc_context_t* ctx,
223         private_handle_t* ohnd, int dpy) {
224     bool scalingMode = false;
225     int fbWidth = ctx->dpyAttr[dpy].xres;
226     int fbHeight = ctx->dpyAttr[dpy].yres;
227     int alW = 0, alH = 0;
228     getBufferSizeAndDimensions(fbWidth, fbHeight, ohnd->format, alW, alH);
229     if((getWidth(ohnd) != alW) || (getHeight(ohnd) != alH)) {
230         scalingMode = true;
231     }
232     ctx->dpyAttr[dpy].mMDPScalingMode = scalingMode;
233 
234     ALOGD_IF(HWCVIRTUAL_LOG, "%s fb(%dx%d) outputBuffer(%dx%d) scalingMode=%d",
235             __FUNCTION__, alW, alH,
236             getWidth(ohnd), getHeight(ohnd), scalingMode);
237 }
238