blob: fa4037085b57eeab18448743b01b1e3b5cd271c9 [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>
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080024#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"
Tatenda Chipeperekwa523eac52014-05-29 14:58:39 -070031#include "sync/sync.h"
Naseer Ahmed9eb5e092014-09-25 13:24:44 -040032#include <utils/Trace.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;
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -070044 initCompositionResources(ctx, dpy);
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080045
46 if(ctx->mFBUpdate[dpy])
47 ctx->mFBUpdate[dpy]->reset();
48 if(ctx->mMDPComp[dpy])
49 ctx->mMDPComp[dpy]->reset();
50}
51
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -080052void HWCVirtualVDS::destroy(hwc_context_t *ctx, size_t /*numDisplays*/,
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080053 hwc_display_contents_1_t** displays) {
54 int dpy = HWC_DISPLAY_VIRTUAL;
55
56 //Cleanup virtual display objs, since there is no explicit disconnect
Raj Kamal52b4fdb2014-01-27 19:35:13 +053057 if(ctx->dpyAttr[dpy].connected && (displays[dpy] == NULL)) {
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080058 ctx->dpyAttr[dpy].connected = false;
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -080059 ctx->dpyAttr[dpy].isPause = false;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080060
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -070061 destroyCompositionResources(ctx, dpy);
62
Manoj Kumar AVMfb472b02014-08-21 15:13:37 -070063 // signal synclock to indicate successful wfd teardown
Raj kamal59fea562014-04-01 16:52:19 +053064 ctx->mWfdSyncLock.lock();
65 ctx->mWfdSyncLock.signal();
66 ctx->mWfdSyncLock.unlock();
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080067 }
68}
69
70int 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
Tatenda Chipeperekwab0a9f9d2014-01-20 09:23:21 -080077 if (list && list->outbuf && list->numHwLayers > 0) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +053078 reset_layer_prop(ctx, dpy, (int)list->numHwLayers - 1);
79 uint32_t last = (uint32_t)list->numHwLayers - 1;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080080 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;
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -080088 ctx->dpyAttr[dpy].isPause = false;
Tatenda Chipeperekwad6a8d4b2014-01-14 18:31:20 -080089 // 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;
radhakrishnac3198ff2015-03-10 17:10:02 +053093 ctx->dpyAttr[dpy].fbformat = HAL_PIXEL_FORMAT_RGBA_8888;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080094 init(ctx);
Naseer Ahmed73d15792014-11-25 20:05:30 -050095 // Do one padding round for cases where primary has all pipes
96 // The virtual composition falls back to GPU in such cases.
97 ctx->isPaddingRound = true;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080098 }
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -080099 if(!ctx->dpyAttr[dpy].isPause) {
100 ctx->dpyAttr[dpy].isConfiguring = false;
101 ctx->dpyAttr[dpy].fd = Writeback::getInstance()->getFbFd();
102 private_handle_t *ohnd = (private_handle_t *)list->outbuf;
Tatenda Chipeperekwacb2a2432014-08-06 17:45:58 -0700103
104 setMDPScalingMode(ctx, ohnd, dpy);
105
106 mScalingWidth = getWidth(ohnd);
107 mScalingHeight = getHeight(ohnd);
108
109 Writeback::getInstance()->configureDpyInfo(mScalingWidth,
110 mScalingHeight);
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800111 setListStats(ctx, list, dpy);
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800112
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800113 if(ctx->mMDPComp[dpy]->prepare(ctx, list) < 0) {
114 const int fbZ = 0;
Saurabh Shahef19fe32014-04-22 15:31:58 -0700115 if(not ctx->mFBUpdate[dpy]->prepareAndValidate(ctx, list, fbZ))
116 {
117 ctx->mOverlay->clear(dpy);
118 ctx->mLayerRotMap[dpy]->clear();
119 }
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800120 }
121 } else {
122 /* Virtual Display is in Pause state.
123 * Mark all application layers as OVERLAY so that
124 * GPU will not compose.
125 */
Raj Kamale012e0e2014-05-28 23:45:19 +0530126 Writeback::getInstance(); //Ensure that WB is active during pause
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800127 for(size_t i = 0 ;i < (size_t)(list->numHwLayers - 1); i++) {
128 hwc_layer_1_t *layer = &list->hwLayers[i];
129 layer->compositionType = HWC_OVERLAY;
130 }
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800131 }
132 }
133 return 0;
134}
135
136int HWCVirtualVDS::set(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
137 ATRACE_CALL();
138 int ret = 0;
139 const int dpy = HWC_DISPLAY_VIRTUAL;
140
Tatenda Chipeperekwaf19f84d2014-01-17 12:43:29 -0800141 if (list && list->outbuf && list->numHwLayers > 0) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530142 uint32_t last = (uint32_t)list->numHwLayers - 1;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800143 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
144
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800145 if(ctx->dpyAttr[dpy].connected
146 && (!ctx->dpyAttr[dpy].isPause))
147 {
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800148 private_handle_t *ohnd = (private_handle_t *)list->outbuf;
Tatenda Chipeperekwaa2fdebe2014-01-20 12:29:50 -0800149 int format = ohnd->format;
150 if (format == HAL_PIXEL_FORMAT_RGBA_8888)
151 format = HAL_PIXEL_FORMAT_RGBX_8888;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800152 Writeback::getInstance()->setOutputFormat(
Tatenda Chipeperekwaa2fdebe2014-01-20 12:29:50 -0800153 utils::getMdpFormat(format));
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800154
Manoj Kumar AVMfb472b02014-08-21 15:13:37 -0700155 // Configure WB secure mode based on output buffer handle
156 if(! Writeback::getInstance()->setSecure(isSecureBuffer(ohnd)))
157 {
158 ALOGE("Failed to set WB secure mode: %d for virtual display",
159 isSecureBuffer(ohnd));
160 return false;
Tatenda Chipeperekwa92961f82014-01-17 13:04:28 -0800161 }
162
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800163 int fd = -1; //FenceFD from the Copybit
164 hwc_sync(ctx, list, dpy, fd);
165
Tatenda Chipeperekwa523eac52014-05-29 14:58:39 -0700166 // Dump the layers for virtual
167 if(ctx->mHwcDebug[dpy])
168 ctx->mHwcDebug[dpy]->dumpLayers(list);
169
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800170 if (!ctx->mMDPComp[dpy]->draw(ctx, list)) {
171 ALOGE("%s: MDPComp draw failed", __FUNCTION__);
172 ret = -1;
173 }
Tatenda Chipeperekwad6a8d4b2014-01-14 18:31:20 -0800174 // We need an FB layer handle check to cater for this usecase:
175 // Video is playing in landscape on primary, then launch
176 // ScreenRecord app.
177 // In this scenario, the first VDS draw call will have HWC
178 // composition and VDS does nit involve GPU to get eglSwapBuffer
179 // to get valid fb handle.
180 if (fbLayer->handle && !ctx->mFBUpdate[dpy]->draw(ctx,
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800181 (private_handle_t *)fbLayer->handle)) {
182 ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__);
183 ret = -1;
184 }
185
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530186 Writeback::getInstance()->queueBuffer(ohnd->fd,
187 (uint32_t)ohnd->offset);
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800188 if(!Overlay::displayCommit(ctx->dpyAttr[dpy].fd)) {
189 ALOGE("%s: display commit fail!", __FUNCTION__);
190 ret = -1;
191 }
192
Tatenda Chipeperekwa06af9cb2014-08-26 14:51:05 -0700193 if(sVDDumpEnabled) {
Tatenda Chipeperekwa523eac52014-05-29 14:58:39 -0700194 char bufferName[128];
195 // Dumping frame buffer
196 sync_wait(fbLayer->acquireFenceFd, 1000);
197 snprintf(bufferName, sizeof(bufferName), "vds.fb");
198 dumpBuffer((private_handle_t *)fbLayer->handle, bufferName);
199 // Dumping WB output for non-secure session
200 if(!isSecureBuffer(ohnd)) {
201 sync_wait(list->retireFenceFd, 1000);
202 snprintf(bufferName, sizeof(bufferName), "vds.wb");
203 dumpBuffer(ohnd, bufferName);
204 }
205 }
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800206 } else if(list->outbufAcquireFenceFd >= 0) {
207 //If we dont handle the frame, set retireFenceFd to outbufFenceFd,
208 //which will make sure, the framework waits on it and closes it.
209 //The other way is to wait on outbufFenceFd ourselves, close it and
210 //set retireFenceFd to -1. Since we want hwc to be async, choosing
211 //the former.
212 //Also dup because, the closeAcquireFds() will close the outbufFence
213 list->retireFenceFd = dup(list->outbufAcquireFenceFd);
214 }
215 }
216
217 closeAcquireFds(list);
218 return ret;
219}
220
Tatenda Chipeperekwacb2a2432014-08-06 17:45:58 -0700221/* We set scaling mode on the VD if the output handle width and height
222 differs from the virtual frame buffer width and height. */
223void HWCVirtualVDS::setMDPScalingMode(hwc_context_t* ctx,
224 private_handle_t* ohnd, int dpy) {
225 bool scalingMode = false;
226 int fbWidth = ctx->dpyAttr[dpy].xres;
Manoj Kumar AVMbffa0692014-12-08 11:16:16 -0800227 int fbHeight = ctx->dpyAttr[dpy].yres;
228 int alW = 0, alH = 0;
229 getBufferSizeAndDimensions(fbWidth, fbHeight, ohnd->format, alW, alH);
230 if((getWidth(ohnd) != alW) || (getHeight(ohnd) != alH)) {
Tatenda Chipeperekwacb2a2432014-08-06 17:45:58 -0700231 scalingMode = true;
232 }
233 ctx->dpyAttr[dpy].mMDPScalingMode = scalingMode;
234
235 ALOGD_IF(HWCVIRTUAL_LOG, "%s fb(%dx%d) outputBuffer(%dx%d) scalingMode=%d",
Manoj Kumar AVMbffa0692014-12-08 11:16:16 -0800236 __FUNCTION__, alW, alH,
Tatenda Chipeperekwacb2a2432014-08-06 17:45:58 -0700237 getWidth(ohnd), getHeight(ohnd), scalingMode);
238}