blob: dbe6bb8339b1a8837b0e42b03cd009ab6ffeb46a [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"
32
33#define HWCVIRTUAL_LOG 0
34
35using namespace qhwc;
36using namespace overlay;
37
38HWCVirtualBase* HWCVirtualBase::getObject() {
39 char property[PROPERTY_VALUE_MAX];
40
41 if((property_get("persist.hwc.enable_vds", property, NULL) > 0)) {
42 if(atoi(property) != 0) {
43 ALOGD_IF(HWCVIRTUAL_LOG, "%s: VDS is enabled for Virtual display",
44 __FUNCTION__);
45 return new HWCVirtualVDS();
46 }
47 }
48 ALOGD_IF(HWCVIRTUAL_LOG, "%s: V4L2 is enabled for Virtual display",
49 __FUNCTION__);
50 return new HWCVirtualV4L2();
51}
52
53void HWCVirtualVDS::init(hwc_context_t *ctx) {
54 const int dpy = HWC_DISPLAY_VIRTUAL;
55 ctx->mFBUpdate[dpy] =
56 IFBUpdate::getObject(ctx, dpy);
57 ctx->mMDPComp[dpy] = MDPComp::getObject(ctx, dpy);
58
59 if(ctx->mFBUpdate[dpy])
60 ctx->mFBUpdate[dpy]->reset();
61 if(ctx->mMDPComp[dpy])
62 ctx->mMDPComp[dpy]->reset();
63}
64
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -080065void HWCVirtualVDS::destroy(hwc_context_t *ctx, size_t /*numDisplays*/,
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080066 hwc_display_contents_1_t** displays) {
67 int dpy = HWC_DISPLAY_VIRTUAL;
68
69 //Cleanup virtual display objs, since there is no explicit disconnect
Raj Kamal52b4fdb2014-01-27 19:35:13 +053070 if(ctx->dpyAttr[dpy].connected && (displays[dpy] == NULL)) {
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080071 ctx->dpyAttr[dpy].connected = false;
72
73 if(ctx->mFBUpdate[dpy]) {
74 delete ctx->mFBUpdate[dpy];
75 ctx->mFBUpdate[dpy] = NULL;
76 }
77 if(ctx->mMDPComp[dpy]) {
78 delete ctx->mMDPComp[dpy];
79 ctx->mMDPComp[dpy] = NULL;
80 }
81 }
82}
83
84int HWCVirtualVDS::prepare(hwc_composer_device_1 *dev,
85 hwc_display_contents_1_t *list) {
86 ATRACE_CALL();
87 //XXX: Fix when framework support is added
88 hwc_context_t* ctx = (hwc_context_t*)(dev);
89 const int dpy = HWC_DISPLAY_VIRTUAL;
90
Tatenda Chipeperekwab0a9f9d2014-01-20 09:23:21 -080091 if (list && list->outbuf && list->numHwLayers > 0) {
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -080092 reset_layer_prop(ctx, dpy, list->numHwLayers - 1);
93 uint32_t last = list->numHwLayers - 1;
94 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
95 int fbWidth = 0, fbHeight = 0;
96 getLayerResolution(fbLayer, fbWidth, fbHeight);
97 ctx->dpyAttr[dpy].xres = fbWidth;
98 ctx->dpyAttr[dpy].yres = fbHeight;
99
100 if(ctx->dpyAttr[dpy].connected == false) {
101 ctx->dpyAttr[dpy].connected = true;
102 init(ctx);
103 //First round, just setup and return so primary can free pipes
104 return 0;
105 }
106
107 ctx->dpyAttr[dpy].isConfiguring = false;
108 ctx->dpyAttr[dpy].fd = Writeback::getInstance()->getFbFd();
Tatenda Chipeperekwab0a9f9d2014-01-20 09:23:21 -0800109 private_handle_t *ohnd = (private_handle_t *)list->outbuf;
110 Writeback::getInstance()->configureDpyInfo(ohnd->width, ohnd->height);
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800111 setListStats(ctx, list, dpy);
112
113 if(ctx->mMDPComp[dpy]->prepare(ctx, list) < 0) {
114 const int fbZ = 0;
115 ctx->mFBUpdate[dpy]->prepare(ctx, list, fbZ);
116 }
117 }
118 return 0;
119}
120
121int HWCVirtualVDS::set(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
122 ATRACE_CALL();
123 int ret = 0;
124 const int dpy = HWC_DISPLAY_VIRTUAL;
125
Tatenda Chipeperekwaf19f84d2014-01-17 12:43:29 -0800126 if (list && list->outbuf && list->numHwLayers > 0) {
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800127 uint32_t last = list->numHwLayers - 1;
128 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
129
130 if(fbLayer->handle && !isSecondaryConfiguring(ctx) &&
131 !ctx->mMDPComp[dpy]->isGLESOnlyComp()) {
132 private_handle_t *ohnd = (private_handle_t *)list->outbuf;
Tatenda Chipeperekwaa2fdebe2014-01-20 12:29:50 -0800133 int format = ohnd->format;
134 if (format == HAL_PIXEL_FORMAT_RGBA_8888)
135 format = HAL_PIXEL_FORMAT_RGBX_8888;
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800136 Writeback::getInstance()->setOutputFormat(
Tatenda Chipeperekwaa2fdebe2014-01-20 12:29:50 -0800137 utils::getMdpFormat(format));
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800138
139 int fd = -1; //FenceFD from the Copybit
140 hwc_sync(ctx, list, dpy, fd);
141
142 if (!ctx->mMDPComp[dpy]->draw(ctx, list)) {
143 ALOGE("%s: MDPComp draw failed", __FUNCTION__);
144 ret = -1;
145 }
146 if (!ctx->mFBUpdate[dpy]->draw(ctx,
147 (private_handle_t *)fbLayer->handle)) {
148 ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__);
149 ret = -1;
150 }
151
152 Writeback::getInstance()->queueBuffer(ohnd->fd, ohnd->offset);
153 if(!Overlay::displayCommit(ctx->dpyAttr[dpy].fd)) {
154 ALOGE("%s: display commit fail!", __FUNCTION__);
155 ret = -1;
156 }
157
158 } else if(list->outbufAcquireFenceFd >= 0) {
159 //If we dont handle the frame, set retireFenceFd to outbufFenceFd,
160 //which will make sure, the framework waits on it and closes it.
161 //The other way is to wait on outbufFenceFd ourselves, close it and
162 //set retireFenceFd to -1. Since we want hwc to be async, choosing
163 //the former.
164 //Also dup because, the closeAcquireFds() will close the outbufFence
165 list->retireFenceFd = dup(list->outbufAcquireFenceFd);
166 }
167 }
168
169 closeAcquireFds(list);
170 return ret;
171}
172
173/* Implementation for HWCVirtualV4L2 class */
174
175int HWCVirtualV4L2::prepare(hwc_composer_device_1 *dev,
176 hwc_display_contents_1_t *list) {
177 ATRACE_CALL();
178
179 hwc_context_t* ctx = (hwc_context_t*)(dev);
180 const int dpy = HWC_DISPLAY_VIRTUAL;
181
182 if (LIKELY(list && list->numHwLayers > 1) &&
183 ctx->dpyAttr[dpy].isActive &&
Raj Kamal52b4fdb2014-01-27 19:35:13 +0530184 ctx->dpyAttr[dpy].connected &&
185 canUseMDPforVirtualDisplay(ctx,list)) {
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800186 reset_layer_prop(ctx, dpy, list->numHwLayers - 1);
187 if(!ctx->dpyAttr[dpy].isPause) {
188 ctx->dpyAttr[dpy].isConfiguring = false;
189 setListStats(ctx, list, dpy);
190 if(ctx->mMDPComp[dpy]->prepare(ctx, list) < 0) {
191 const int fbZ = 0;
192 ctx->mFBUpdate[dpy]->prepare(ctx, list, fbZ);
193 }
194 } else {
195 /* Virtual Display is in Pause state.
196 * Mark all application layers as OVERLAY so that
197 * GPU will not compose.
198 */
199 for(size_t i = 0 ;i < (size_t)(list->numHwLayers - 1); i++) {
200 hwc_layer_1_t *layer = &list->hwLayers[i];
201 layer->compositionType = HWC_OVERLAY;
202 }
203 }
204 }
205 return 0;
206}
207
208int HWCVirtualV4L2::set(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
209 ATRACE_CALL();
210 int ret = 0;
211
212 const int dpy = HWC_DISPLAY_VIRTUAL;
213
214 if (LIKELY(list) && ctx->dpyAttr[dpy].isActive &&
215 ctx->dpyAttr[dpy].connected &&
Raj Kamal52b4fdb2014-01-27 19:35:13 +0530216 (!ctx->dpyAttr[dpy].isPause) &&
217 canUseMDPforVirtualDisplay(ctx,list)) {
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800218 uint32_t last = list->numHwLayers - 1;
219 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
220 int fd = -1; //FenceFD from the Copybit(valid in async mode)
221 bool copybitDone = false;
222 if(ctx->mCopyBit[dpy])
223 copybitDone = ctx->mCopyBit[dpy]->draw(ctx, list, dpy, &fd);
224
225 if(list->numHwLayers > 1)
226 hwc_sync(ctx, list, dpy, fd);
227
228 // Dump the layers for virtual
229 if(ctx->mHwcDebug[dpy])
230 ctx->mHwcDebug[dpy]->dumpLayers(list);
231
232 if (!ctx->mMDPComp[dpy]->draw(ctx, list)) {
233 ALOGE("%s: MDPComp draw failed", __FUNCTION__);
234 ret = -1;
235 }
236
237 int extOnlyLayerIndex =
238 ctx->listStats[dpy].extOnlyLayerIndex;
239
240 private_handle_t *hnd = (private_handle_t *)fbLayer->handle;
241 if(extOnlyLayerIndex!= -1) {
242 hwc_layer_1_t *extLayer = &list->hwLayers[extOnlyLayerIndex];
243 hnd = (private_handle_t *)extLayer->handle;
244 } else if(copybitDone) {
245 hnd = ctx->mCopyBit[dpy]->getCurrentRenderBuffer();
246 }
247
248 if(hnd && !isYuvBuffer(hnd)) {
249 if (!ctx->mFBUpdate[dpy]->draw(ctx, hnd)) {
250 ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__);
251 ret = -1;
252 }
253 }
254
255 if(!Overlay::displayCommit(ctx->dpyAttr[dpy].fd)) {
256 ALOGE("%s: display commit fail for %d dpy!", __FUNCTION__, dpy);
257 ret = -1;
258 }
259 }
260
261 closeAcquireFds(list);
262
263 if (list && !ctx->mVirtualonExtActive && (list->retireFenceFd < 0) ) {
264 // SF assumes HWC waits for the acquire fence and returns a new fence
265 // that signals when we're done. Since we don't wait, and also don't
266 // touch the buffer, we can just handle the acquire fence back to SF
267 // as the retire fence.
268 list->retireFenceFd = list->outbufAcquireFenceFd;
269 }
270
271 return ret;
272}