blob: 146d671a72a1ec8f897f4ce2bfc40c433b5de905 [file] [log] [blame]
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -08001/*
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 <utils/Trace.h>
25#include <overlayWriteback.h>
26#include "hwc_utils.h"
27#include "hwc_fbupdate.h"
28#include "hwc_mdpcomp.h"
29#include "hwc_dump_layers.h"
30#include "hwc_copybit.h"
31#include "hwc_virtual.h"
Tatenda Chipeperekwa523eac52014-05-29 14:58:39 -070032#include "sync/sync.h"
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080033
34#define HWCVIRTUAL_LOG 0
35
36using namespace qhwc;
37using namespace overlay;
38
Manoj Kumar AVM9591a5e2014-08-21 22:50:21 -070039bool HWCVirtualVDS::sVDDumpEnabled = false;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080040
41void HWCVirtualVDS::init(hwc_context_t *ctx) {
42 const int dpy = HWC_DISPLAY_VIRTUAL;
Tatenda Chipeperekwacb2a2432014-08-06 17:45:58 -070043 mScalingWidth = 0, mScalingHeight = 0;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080044 ctx->mFBUpdate[dpy] =
45 IFBUpdate::getObject(ctx, dpy);
46 ctx->mMDPComp[dpy] = MDPComp::getObject(ctx, dpy);
47
48 if(ctx->mFBUpdate[dpy])
49 ctx->mFBUpdate[dpy]->reset();
50 if(ctx->mMDPComp[dpy])
51 ctx->mMDPComp[dpy]->reset();
52}
53
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -080054void HWCVirtualVDS::destroy(hwc_context_t *ctx, size_t /*numDisplays*/,
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080055 hwc_display_contents_1_t** displays) {
56 int dpy = HWC_DISPLAY_VIRTUAL;
57
58 //Cleanup virtual display objs, since there is no explicit disconnect
Raj Kamal52b4fdb2014-01-27 19:35:13 +053059 if(ctx->dpyAttr[dpy].connected && (displays[dpy] == NULL)) {
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080060 ctx->dpyAttr[dpy].connected = false;
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -080061 ctx->dpyAttr[dpy].isPause = false;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080062
63 if(ctx->mFBUpdate[dpy]) {
64 delete ctx->mFBUpdate[dpy];
65 ctx->mFBUpdate[dpy] = NULL;
66 }
67 if(ctx->mMDPComp[dpy]) {
68 delete ctx->mMDPComp[dpy];
69 ctx->mMDPComp[dpy] = NULL;
70 }
Manoj Kumar AVMfb472b02014-08-21 15:13:37 -070071 // signal synclock to indicate successful wfd teardown
Raj kamal59fea562014-04-01 16:52:19 +053072 ctx->mWfdSyncLock.lock();
73 ctx->mWfdSyncLock.signal();
74 ctx->mWfdSyncLock.unlock();
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080075 }
76}
77
78int HWCVirtualVDS::prepare(hwc_composer_device_1 *dev,
79 hwc_display_contents_1_t *list) {
80 ATRACE_CALL();
81 //XXX: Fix when framework support is added
82 hwc_context_t* ctx = (hwc_context_t*)(dev);
83 const int dpy = HWC_DISPLAY_VIRTUAL;
84
Tatenda Chipeperekwab0a9f9d2014-01-20 09:23:21 -080085 if (list && list->outbuf && list->numHwLayers > 0) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +053086 reset_layer_prop(ctx, dpy, (int)list->numHwLayers - 1);
87 uint32_t last = (uint32_t)list->numHwLayers - 1;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080088 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
89 int fbWidth = 0, fbHeight = 0;
90 getLayerResolution(fbLayer, fbWidth, fbHeight);
91 ctx->dpyAttr[dpy].xres = fbWidth;
92 ctx->dpyAttr[dpy].yres = fbHeight;
93
94 if(ctx->dpyAttr[dpy].connected == false) {
95 ctx->dpyAttr[dpy].connected = true;
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -080096 ctx->dpyAttr[dpy].isPause = false;
Tatenda Chipeperekwad6a8d4b2014-01-14 18:31:20 -080097 // We set the vsync period to the primary refresh rate, leaving
98 // it up to the consumer to decide how fast to consume frames.
99 ctx->dpyAttr[dpy].vsync_period
100 = ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800101 init(ctx);
Tatenda Chipeperekwad6a8d4b2014-01-14 18:31:20 -0800102 // XXX: for architectures with limited resources we would normally
103 // allow one padding round to free up resources but this breaks
104 // certain use cases.
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800105 }
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800106 if(!ctx->dpyAttr[dpy].isPause) {
107 ctx->dpyAttr[dpy].isConfiguring = false;
108 ctx->dpyAttr[dpy].fd = Writeback::getInstance()->getFbFd();
109 private_handle_t *ohnd = (private_handle_t *)list->outbuf;
Tatenda Chipeperekwacb2a2432014-08-06 17:45:58 -0700110
111 setMDPScalingMode(ctx, ohnd, dpy);
112
113 mScalingWidth = getWidth(ohnd);
114 mScalingHeight = getHeight(ohnd);
115
116 Writeback::getInstance()->configureDpyInfo(mScalingWidth,
117 mScalingHeight);
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800118 setListStats(ctx, list, dpy);
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800119
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800120 if(ctx->mMDPComp[dpy]->prepare(ctx, list) < 0) {
121 const int fbZ = 0;
Saurabh Shahef19fe32014-04-22 15:31:58 -0700122 if(not ctx->mFBUpdate[dpy]->prepareAndValidate(ctx, list, fbZ))
123 {
124 ctx->mOverlay->clear(dpy);
125 ctx->mLayerRotMap[dpy]->clear();
126 }
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800127 }
128 } else {
129 /* Virtual Display is in Pause state.
130 * Mark all application layers as OVERLAY so that
131 * GPU will not compose.
132 */
Raj Kamale012e0e2014-05-28 23:45:19 +0530133 Writeback::getInstance(); //Ensure that WB is active during pause
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800134 for(size_t i = 0 ;i < (size_t)(list->numHwLayers - 1); i++) {
135 hwc_layer_1_t *layer = &list->hwLayers[i];
136 layer->compositionType = HWC_OVERLAY;
137 }
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800138 }
139 }
140 return 0;
141}
142
143int HWCVirtualVDS::set(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
144 ATRACE_CALL();
145 int ret = 0;
146 const int dpy = HWC_DISPLAY_VIRTUAL;
147
Tatenda Chipeperekwaf19f84d2014-01-17 12:43:29 -0800148 if (list && list->outbuf && list->numHwLayers > 0) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530149 uint32_t last = (uint32_t)list->numHwLayers - 1;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800150 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
151
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800152 if(ctx->dpyAttr[dpy].connected
153 && (!ctx->dpyAttr[dpy].isPause))
154 {
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800155 private_handle_t *ohnd = (private_handle_t *)list->outbuf;
Tatenda Chipeperekwaa2fdebe2014-01-20 12:29:50 -0800156 int format = ohnd->format;
157 if (format == HAL_PIXEL_FORMAT_RGBA_8888)
158 format = HAL_PIXEL_FORMAT_RGBX_8888;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800159 Writeback::getInstance()->setOutputFormat(
Tatenda Chipeperekwaa2fdebe2014-01-20 12:29:50 -0800160 utils::getMdpFormat(format));
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800161
Manoj Kumar AVMfb472b02014-08-21 15:13:37 -0700162 // Configure WB secure mode based on output buffer handle
163 if(! Writeback::getInstance()->setSecure(isSecureBuffer(ohnd)))
164 {
165 ALOGE("Failed to set WB secure mode: %d for virtual display",
166 isSecureBuffer(ohnd));
167 return false;
Tatenda Chipeperekwa92961f82014-01-17 13:04:28 -0800168 }
169
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800170 int fd = -1; //FenceFD from the Copybit
171 hwc_sync(ctx, list, dpy, fd);
172
Tatenda Chipeperekwa523eac52014-05-29 14:58:39 -0700173 // Dump the layers for virtual
174 if(ctx->mHwcDebug[dpy])
175 ctx->mHwcDebug[dpy]->dumpLayers(list);
176
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800177 if (!ctx->mMDPComp[dpy]->draw(ctx, list)) {
178 ALOGE("%s: MDPComp draw failed", __FUNCTION__);
179 ret = -1;
180 }
Tatenda Chipeperekwad6a8d4b2014-01-14 18:31:20 -0800181 // We need an FB layer handle check to cater for this usecase:
182 // Video is playing in landscape on primary, then launch
183 // ScreenRecord app.
184 // In this scenario, the first VDS draw call will have HWC
185 // composition and VDS does nit involve GPU to get eglSwapBuffer
186 // to get valid fb handle.
187 if (fbLayer->handle && !ctx->mFBUpdate[dpy]->draw(ctx,
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800188 (private_handle_t *)fbLayer->handle)) {
189 ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__);
190 ret = -1;
191 }
192
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530193 Writeback::getInstance()->queueBuffer(ohnd->fd,
194 (uint32_t)ohnd->offset);
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800195 if(!Overlay::displayCommit(ctx->dpyAttr[dpy].fd)) {
196 ALOGE("%s: display commit fail!", __FUNCTION__);
197 ret = -1;
198 }
199
Tatenda Chipeperekwa06af9cb2014-08-26 14:51:05 -0700200 if(sVDDumpEnabled) {
Tatenda Chipeperekwa523eac52014-05-29 14:58:39 -0700201 char bufferName[128];
202 // Dumping frame buffer
203 sync_wait(fbLayer->acquireFenceFd, 1000);
204 snprintf(bufferName, sizeof(bufferName), "vds.fb");
205 dumpBuffer((private_handle_t *)fbLayer->handle, bufferName);
206 // Dumping WB output for non-secure session
207 if(!isSecureBuffer(ohnd)) {
208 sync_wait(list->retireFenceFd, 1000);
209 snprintf(bufferName, sizeof(bufferName), "vds.wb");
210 dumpBuffer(ohnd, bufferName);
211 }
212 }
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800213 } else if(list->outbufAcquireFenceFd >= 0) {
214 //If we dont handle the frame, set retireFenceFd to outbufFenceFd,
215 //which will make sure, the framework waits on it and closes it.
216 //The other way is to wait on outbufFenceFd ourselves, close it and
217 //set retireFenceFd to -1. Since we want hwc to be async, choosing
218 //the former.
219 //Also dup because, the closeAcquireFds() will close the outbufFence
220 list->retireFenceFd = dup(list->outbufAcquireFenceFd);
221 }
222 }
223
224 closeAcquireFds(list);
225 return ret;
226}
227
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800228void HWCVirtualVDS::pause(hwc_context_t* ctx, int dpy) {
229 {
230 Locker::Autolock _l(ctx->mDrawLock);
231 ctx->dpyAttr[dpy].isActive = true;
232 ctx->dpyAttr[dpy].isPause = true;
233 ctx->proc->invalidate(ctx->proc);
234 }
235 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
236 * 2 / 1000);
Raj Kamale012e0e2014-05-28 23:45:19 +0530237 // At this point all the pipes used by External have been
238 // marked as UNSET.
239 {
240 Locker::Autolock _l(ctx->mDrawLock);
241 // Perform commit to unstage the pipes.
242 if (!Overlay::displayCommit(ctx->dpyAttr[dpy].fd)) {
243 ALOGE("%s: display commit fail! for %d dpy",
244 __FUNCTION__, dpy);
245 }
Raj Kamaldf3d1d22014-05-30 21:23:27 +0530246 ctx->proc->invalidate(ctx->proc);
Raj Kamale012e0e2014-05-28 23:45:19 +0530247 }
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800248 return;
249}
250
251void HWCVirtualVDS::resume(hwc_context_t* ctx, int dpy) {
252 {
253 Locker::Autolock _l(ctx->mDrawLock);
254 ctx->dpyAttr[dpy].isConfiguring = true;
255 ctx->dpyAttr[dpy].isActive = true;
256 ctx->proc->invalidate(ctx->proc);
257 }
258 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
259 * 2 / 1000);
260 //At this point external has all the pipes it would need.
261 {
262 Locker::Autolock _l(ctx->mDrawLock);
263 ctx->dpyAttr[dpy].isPause = false;
264 ctx->proc->invalidate(ctx->proc);
265 }
266 return;
267}
Tatenda Chipeperekwacb2a2432014-08-06 17:45:58 -0700268
269/* We set scaling mode on the VD if the output handle width and height
270 differs from the virtual frame buffer width and height. */
271void HWCVirtualVDS::setMDPScalingMode(hwc_context_t* ctx,
272 private_handle_t* ohnd, int dpy) {
273 bool scalingMode = false;
274 int fbWidth = ctx->dpyAttr[dpy].xres;
275 int fbHeight = ctx->dpyAttr[dpy].yres;
276 if((getWidth(ohnd) != fbWidth) || (getHeight(ohnd) != fbHeight)) {
277 scalingMode = true;
278 }
279 ctx->dpyAttr[dpy].mMDPScalingMode = scalingMode;
280
281 ALOGD_IF(HWCVIRTUAL_LOG, "%s fb(%dx%d) outputBuffer(%dx%d) scalingMode=%d",
282 __FUNCTION__, fbWidth, fbHeight,
283 getWidth(ohnd), getHeight(ohnd), scalingMode);
284}