summaryrefslogtreecommitdiff
path: root/hwc2/common/devices/PhysicalDevice.cpp (plain)
blob: fda377304c66fa53016d247cc6fafb1e1292c225
1/*
2// Copyright (c) 2014 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// This file is modified by Amlogic, Inc. 2017.01.17.
17*/
18
19#include <fcntl.h>
20#include <inttypes.h>
21#include <HwcTrace.h>
22#include <PhysicalDevice.h>
23#include <Hwcomposer.h>
24#include <sys/ioctl.h>
25#include <sync/sync.h>
26#include <Utils.h>
27#include <HwcFenceControl.h>
28#include <cutils/properties.h>
29#include <tvp/OmxUtil.h>
30
31static int Amvideo_Handle = 0;
32
33namespace android {
34namespace amlogic {
35
36PhysicalDevice::PhysicalDevice(hwc2_display_t id, Hwcomposer& hwc, DeviceControlFactory* controlFactory)
37 : mId(id),
38 mHwc(hwc),
39 mControlFactory(controlFactory),
40 mVsyncObserver(NULL),
41 mIsConnected(false),
42 mSecure(false),
43 mFramebufferHnd(NULL),
44 mSystemControl(NULL),
45 mFbSlot(0),
46 mComposer(NULL),
47 mPriorFrameRetireFence(-1),
48 mClientTargetHnd(NULL),
49 mTargetAcquireFence(-1),
50 mRenderMode(GLES_COMPOSE_MODE),
51 mIsValidated(false),
52 mIsContinuousBuf(true),
53 mDirectRenderLayerId(0),
54 mVideoOverlayLayerId(0),
55 mGE2DClearVideoRegionCount(0),
56 mGE2DComposeFrameCount(0),
57 mDirectComposeFrameCount(0),
58 mInitialized(false)
59{
60 CTRACE();
61
62 switch (id) {
63 case DEVICE_PRIMARY:
64 mName = "Primary";
65 break;
66 case DEVICE_EXTERNAL:
67 mName = "External";
68 break;
69 default:
70 mName = "Unknown";
71 }
72
73 mHdrCapabilities.init = false;
74 // init Display here.
75 initDisplay();
76
77 // set capacity of layers, layer's changed type, layer's changed request.
78 mHwcLayersChangeType.setCapacity(LAYER_MAX_NUM_CHANGE_TYPE);
79 mHwcLayersChangeRequest.setCapacity(LAYER_MAX_NUM_CHANGE_REQUEST);
80 mHwcGlesLayers.setCapacity(LAYER_MAX_NUM_CHANGE_TYPE);
81 mHwcLayers.setCapacity(LAYER_MAX_NUM_SUPPORT);
82#ifdef HWC_ENABLE_SECURE_LAYER
83 mHwcSecureLayers.setCapacity(LAYER_MAX_NUM_SECURE_PROTECTED);
84 mHwcSecureLayers.clear();
85#endif
86
87 // clear layers vectors.
88 mHwcLayersChangeType.clear();
89 mHwcLayersChangeRequest.clear();
90 mHwcGlesLayers.clear();
91 mHwcLayers.clear();
92
93 mGE2DRenderSortedLayerIds.setCapacity(HWC2_MAX_LAYERS);
94 mGE2DRenderSortedLayerIds.clear();
95
96 mHwcCurReleaseFences = mHwcPriorReleaseFences = NULL;
97}
98
99PhysicalDevice::~PhysicalDevice()
100{
101 WARN_IF_NOT_DEINIT();
102 clearFenceList(mHwcCurReleaseFences);
103 clearFenceList(mHwcPriorReleaseFences);
104}
105
106bool PhysicalDevice::initialize() {
107 CTRACE();
108
109 if (mId != DEVICE_PRIMARY && mId != DEVICE_EXTERNAL) {
110 ETRACE("invalid device type");
111 return false;
112 }
113
114 // create vsync event observer, we only have soft vsync now...
115 mVsyncObserver = new SoftVsyncObserver(*this);
116 if (!mVsyncObserver || !mVsyncObserver->initialize()) {
117 DEINIT_AND_RETURN_FALSE("failed to create vsync observer");
118 }
119
120 mDisplayHdmi = new DisplayHdmi(mId);
121 mDisplayHdmi->initialize();
122 UeventObserver *observer = Hwcomposer::getInstance().getUeventObserver();
123 if (observer) {
124 observer->registerListener(
125 Utils::getHdcpUeventEnvelope(),
126 hdcpEventListener,
127 this);
128 } else {
129 ETRACE("PhysicalDevice::Uevent observer is NULL");
130 }
131
132 mInitialized = true;
133 return true;
134}
135
136void PhysicalDevice::hdcpEventListener(void *data, bool status)
137{
138 PhysicalDevice *pThis = (PhysicalDevice*)data;
139 if (pThis) {
140 pThis->setSecureStatus(status);
141 }
142}
143
144void PhysicalDevice::setSecureStatus(bool status)
145{
146 DTRACE("hdcp event: %d", status);
147 mSecure = status;
148}
149
150void PhysicalDevice::deinitialize() {
151 Mutex::Autolock _l(mLock);
152
153 DEINIT_AND_DELETE_OBJ(mVsyncObserver);
154 DEINIT_AND_DELETE_OBJ(mDisplayHdmi);
155 DEINIT_AND_DELETE_OBJ(mComposer);
156
157 if (mFramebufferContext != NULL) {
158 delete mFramebufferContext;
159 mFramebufferContext = NULL;
160 }
161
162 if (mCursorContext != NULL) {
163 delete mCursorContext;
164 mCursorContext = NULL;
165 }
166
167 mInitialized = false;
168}
169
170HwcLayer* PhysicalDevice::getLayerById(hwc2_layer_t layerId) {
171 HwcLayer* layer = NULL;
172 ssize_t index = mHwcLayers.indexOfKey(layerId);
173
174 if (index >= 0) {
175 layer = mHwcLayers.valueFor(layerId);
176 }
177
178 if (!layer) {
179 DTRACE("getLayerById %lld error!", layerId);
180 }
181 return layer;
182}
183
184int32_t PhysicalDevice::acceptDisplayChanges() {
185 HwcLayer* layer = NULL;
186
187 for (uint32_t i=0; i<mHwcLayersChangeType.size(); i++) {
188 hwc2_layer_t layerId = mHwcLayersChangeType.keyAt(i);
189 layer = mHwcLayersChangeType.valueAt(i);
190 if (layer) {
191#ifdef HWC_ENABLE_SECURE_LAYER
192 // deal non secure display.
193 if (!mSecure && !mHwcSecureLayers.isEmpty()) {
194 for (uint32_t j=0; j<mHwcSecureLayers.size(); j++) {
195 hwc2_layer_t secureLayerId = mHwcSecureLayers.keyAt(j);
196 HwcLayer* secureLayer = mHwcSecureLayers.valueAt(j);
197 // deal secure layers release fence and composition type on non secure display.
198 addReleaseFence(secureLayerId, secureLayer->getDuppedAcquireFence());
199 if (layerId == secureLayerId) {
200 if (layer->getCompositionType() != HWC2_COMPOSITION_DEVICE) {
201 layer->setCompositionType(HWC2_COMPOSITION_DEVICE);
202 continue;
203 }
204 }
205 }
206 }
207#endif
208 if (layer->getCompositionType() == HWC2_COMPOSITION_DEVICE
209 || layer->getCompositionType() == HWC2_COMPOSITION_SOLID_COLOR) {
210 layer->setCompositionType(HWC2_COMPOSITION_CLIENT);
211 } else if (layer->getCompositionType() == HWC2_COMPOSITION_SIDEBAND) {
212 layer->setCompositionType(HWC2_COMPOSITION_DEVICE);
213 }
214 }
215 }
216 // reset layer changed or requested size to zero.
217 mHwcLayersChangeType.clear();
218 mHwcLayersChangeRequest.clear();
219
220#ifdef HWC_ENABLE_SECURE_LAYER
221 // deal non secure display device.
222 if (!mHwcSecureLayers.isEmpty()) {
223 mHwcSecureLayers.clear();
224 }
225#endif
226
227 return HWC2_ERROR_NONE;
228}
229
230bool PhysicalDevice::createLayer(hwc2_layer_t* outLayer) {
231 HwcLayer* layer = new HwcLayer(mId);
232
233 if (layer == NULL || !layer->initialize()) {
234 ETRACE("createLayer: failed !");
235 return false;
236 }
237
238 hwc2_layer_t layerId = reinterpret_cast<hwc2_layer_t>(layer);
239 mHwcLayers.add(layerId, layer);
240 *outLayer = layerId;
241 ETRACE("layerId %lld.\n", layerId);
242
243 return true;
244}
245
246bool PhysicalDevice::destroyLayer(hwc2_layer_t layerId) {
247 HwcLayer* layer = mHwcLayers.valueFor(layerId);
248
249 if (layer == NULL) {
250 ETRACE("destroyLayer: no Hwclayer found (%d)", layerId);
251 return false;
252 }
253
254 for (int i = 0; i < 2; i++) {
255 ssize_t idx = mLayerReleaseFences[i].indexOfKey(layerId);
256 if (idx >= 0) {
257 HwcFenceControl::closeFd(mLayerReleaseFences[i].valueAt(idx));
258 mLayerReleaseFences[i].removeItemsAt(idx);
259 DTRACE("destroyLayer remove layer %lld from cur release list %p\n", layerId, &(mLayerReleaseFences[i]));
260 }
261 }
262
263 mHwcLayers.removeItem(layerId);
264 DEINIT_AND_DELETE_OBJ(layer);
265 return true;
266}
267
268int32_t PhysicalDevice::getActiveConfig(
269 hwc2_config_t* outConfig) {
270 Mutex::Autolock _l(mLock);
271
272 return mDisplayHdmi->getActiveConfig(outConfig);
273}
274
275int32_t PhysicalDevice::getChangedCompositionTypes(
276 uint32_t* outNumElements,
277 hwc2_layer_t* outLayers,
278 int32_t* /*hwc2_composition_t*/ outTypes) {
279 HwcLayer* layer = NULL;
280
281 // if outLayers or outTypes were NULL, the number of layers and types which would have been returned.
282 if (NULL == outLayers || NULL == outTypes) {
283 *outNumElements = mHwcLayersChangeType.size();
284 } else {
285 for (uint32_t i=0; i<mHwcLayersChangeType.size(); i++) {
286 hwc2_layer_t layerId = mHwcLayersChangeType.keyAt(i);
287 layer = mHwcLayersChangeType.valueAt(i);
288 if (layer) {
289#ifdef HWC_ENABLE_SECURE_LAYER
290 // deal non secure display.
291 if (!mSecure && !mHwcSecureLayers.isEmpty()) {
292 for (uint32_t j=0; j<mHwcSecureLayers.size(); j++) {
293 hwc2_layer_t secureLayerId = mHwcSecureLayers.keyAt(j);
294 if (layerId == secureLayerId) {
295 if (layer->getCompositionType() != HWC2_COMPOSITION_DEVICE) {
296 outLayers[i] = layerId;
297 outTypes[i] = HWC2_COMPOSITION_DEVICE;
298 continue;
299 }
300 }
301 }
302 }
303#endif
304
305 if (layer->getCompositionType() == HWC2_COMPOSITION_DEVICE
306 || layer->getCompositionType() == HWC2_COMPOSITION_SOLID_COLOR) {
307 // change all other device type to client.
308 outLayers[i] = layerId;
309 outTypes[i] = HWC2_COMPOSITION_CLIENT;
310 continue;
311 }
312
313 // sideband stream.
314 if (layer->getCompositionType() == HWC2_COMPOSITION_SIDEBAND
315 && layer->getSidebandStream()) {
316 // TODO: we just transact SIDEBAND to OVERLAY for now;
317 DTRACE("get HWC_SIDEBAND layer, just change to overlay");
318 outLayers[i] = layerId;
319 outTypes[i] = HWC2_COMPOSITION_DEVICE;
320 continue;
321 }
322 }
323 }
324
325 if (mHwcLayersChangeType.size() > 0) {
326 DTRACE("There are %d layers type has changed.", mHwcLayersChangeType.size());
327 *outNumElements = mHwcLayersChangeType.size();
328 } else {
329 DTRACE("No layers compositon type changed.");
330 }
331 }
332
333 return HWC2_ERROR_NONE;
334}
335
336int32_t PhysicalDevice::getClientTargetSupport(
337 uint32_t width,
338 uint32_t height,
339 int32_t /*android_pixel_format_t*/ format,
340 int32_t /*android_dataspace_t*/ dataspace) {
341 framebuffer_info_t* fbInfo = mFramebufferContext->getInfo();
342
343 if (width == fbInfo->info.xres
344 && height == fbInfo->info.yres
345 && format == HAL_PIXEL_FORMAT_RGBA_8888
346 && dataspace == HAL_DATASPACE_UNKNOWN) {
347 return HWC2_ERROR_NONE;
348 }
349
350 DTRACE("fbinfo: [%d x %d], client: [%d x %d]"
351 "format: %d, dataspace: %d",
352 fbInfo->info.xres,
353 fbInfo->info.yres,
354 width, height, format, dataspace);
355
356 // TODO: ?
357 return HWC2_ERROR_UNSUPPORTED;
358}
359
360int32_t PhysicalDevice::getColorModes(
361 uint32_t* outNumModes,
362 int32_t* /*android_color_mode_t*/ outModes) {
363
364 if (NULL == outModes) {
365 *outNumModes = 1;
366 } else {
367 *outModes = HAL_COLOR_MODE_NATIVE;
368 }
369
370 return HWC2_ERROR_NONE;
371}
372
373int32_t PhysicalDevice::getDisplayAttribute(
374 hwc2_config_t config,
375 int32_t /*hwc2_attribute_t*/ attribute,
376 int32_t* outValue) {
377 Mutex::Autolock _l(mLock);
378
379 if (!mIsConnected) {
380 ETRACE("display %d is not connected.", mId);
381 }
382
383 int ret = mDisplayHdmi->getDisplayAttribute(config, attribute, outValue);
384 if (ret < 0)
385 return HWC2_ERROR_BAD_CONFIG;
386
387 return HWC2_ERROR_NONE;
388}
389
390int32_t PhysicalDevice::getDisplayConfigs(
391 uint32_t* outNumConfigs,
392 hwc2_config_t* outConfigs) {
393 Mutex::Autolock _l(mLock);
394
395 return mDisplayHdmi->getDisplayConfigs(outNumConfigs, outConfigs);
396}
397
398int32_t PhysicalDevice::getDisplayName(
399 uint32_t* outSize,
400 char* outName) {
401 return HWC2_ERROR_NONE;
402}
403
404int32_t PhysicalDevice::getDisplayRequests(
405 int32_t* /*hwc2_display_request_t*/ outDisplayRequests,
406 uint32_t* outNumElements,
407 hwc2_layer_t* outLayers,
408 int32_t* /*hwc2_layer_request_t*/ outLayerRequests) {
409
410 // if outLayers or outTypes were NULL, the number of layers and types which would have been returned.
411 if (NULL == outLayers || NULL == outLayerRequests) {
412 *outNumElements = mHwcLayersChangeRequest.size();
413 } else {
414 for (uint32_t i=0; i<mHwcLayersChangeRequest.size(); i++) {
415 hwc2_layer_t layerId = mHwcLayersChangeRequest.keyAt(i);
416 HwcLayer *layer = mHwcLayersChangeRequest.valueAt(i);
417 if (layer->getCompositionType() == HWC2_COMPOSITION_DEVICE) {
418 // video overlay.
419 if (layerId == mVideoOverlayLayerId) {
420 outLayers[i] = layerId;
421 outLayerRequests[i] = HWC2_LAYER_REQUEST_CLEAR_CLIENT_TARGET;
422 }
423 /* if (layer->getBufferHandle()) {
424 private_handle_t const* hnd =
425 reinterpret_cast<private_handle_t const*>(layer->getBufferHandle());
426 if (hnd->flags & private_handle_t::PRIV_FLAGS_VIDEO_OVERLAY) {
427 outLayers[i] = layerId;
428 outLayerRequests[i] = HWC2_LAYER_REQUEST_CLEAR_CLIENT_TARGET;
429 continue;
430 }
431 } */
432 }
433
434 // sideband stream.
435 if ((layer->getCompositionType() == HWC2_COMPOSITION_SIDEBAND && layer->getSidebandStream())
436 //|| layer->getCompositionType() == HWC2_COMPOSITION_SOLID_COLOR
437 || layer->getCompositionType() == HWC2_COMPOSITION_CURSOR) {
438 // TODO: we just transact SIDEBAND to OVERLAY for now;
439 DTRACE("get HWC_SIDEBAND layer, just change to overlay");
440 outLayers[i] = layerId;
441 outLayerRequests[i] = HWC2_LAYER_REQUEST_CLEAR_CLIENT_TARGET;
442 continue;
443 }
444 }
445
446 if (mHwcLayersChangeRequest.size() > 0) {
447 DTRACE("There are %d layer requests.", mHwcLayersChangeRequest.size());
448 *outNumElements = mHwcLayersChangeRequest.size();
449 } else {
450 DTRACE("No layer requests.");
451 }
452 }
453
454 return HWC2_ERROR_NONE;
455}
456
457int32_t PhysicalDevice::getDisplayType(
458 int32_t* /*hwc2_display_type_t*/ outType) {
459
460 *outType = HWC2_DISPLAY_TYPE_PHYSICAL;
461 return HWC2_ERROR_NONE;
462}
463
464int32_t PhysicalDevice::getDozeSupport(
465 int32_t* outSupport) {
466 return HWC2_ERROR_NONE;
467}
468
469int32_t PhysicalDevice::getHdrCapabilities(
470 uint32_t* outNumTypes,
471 int32_t* /*android_hdr_t*/ outTypes,
472 float* outMaxLuminance,
473 float* outMaxAverageLuminance,
474 float* outMinLuminance) {
475
476 Mutex::Autolock _l(mLock);
477 if (!mIsConnected) {
478 ETRACE("disp: %llu is not connected", mId);
479 return HWC2_ERROR_BAD_DISPLAY;
480 }
481
482 if (!mHdrCapabilities.init) {
483 parseHdrCapabilities();
484 mHdrCapabilities.init = true;
485 }
486
487 if (NULL == outTypes) {
488 int num = 0;
489 if (mHdrCapabilities.dvSupport) num++;
490 if (mHdrCapabilities.hdrSupport) num++;
491
492 *outNumTypes = num;
493 } else {
494 if (mHdrCapabilities.dvSupport) *outTypes++ = HAL_HDR_DOLBY_VISION;
495 if (mHdrCapabilities.hdrSupport) *outTypes++ = HAL_HDR_HDR10;
496
497 *outMaxLuminance = mHdrCapabilities.maxLuminance;
498 *outMaxAverageLuminance = mHdrCapabilities.avgLuminance;
499 *outMinLuminance = mHdrCapabilities.minLuminance;
500 }
501
502 return HWC2_ERROR_NONE;
503}
504
505void PhysicalDevice::swapReleaseFence() {
506 //dumpFenceList(mHwcCurReleaseFences);
507
508 if (mHwcCurReleaseFences == NULL || mHwcPriorReleaseFences == NULL) {
509 if (mHwcCurReleaseFences) {
510 clearFenceList(mHwcPriorReleaseFences);
511 }
512
513 if (mHwcPriorReleaseFences) {
514 clearFenceList(mHwcPriorReleaseFences);
515 }
516
517 mHwcCurReleaseFences = &(mLayerReleaseFences[0]);
518 mHwcPriorReleaseFences = &(mLayerReleaseFences[1]);
519 } else {
520 KeyedVector<hwc2_layer_t, int32_t> * tmp = mHwcCurReleaseFences;
521 clearFenceList(mHwcPriorReleaseFences);
522 mHwcCurReleaseFences = mHwcPriorReleaseFences;
523 mHwcPriorReleaseFences = tmp;
524 }
525}
526
527
528void PhysicalDevice::addReleaseFence(hwc2_layer_t layerId, int32_t fenceFd) {
529 ssize_t idx = mHwcCurReleaseFences->indexOfKey(layerId);
530 if (idx >= 0 && idx < mHwcCurReleaseFences->size()) {
531 int32_t oldFence = mHwcCurReleaseFences->valueAt(idx);
532 String8 mergeName("hwc-release");
533 int32_t newFence = HwcFenceControl::merge(mergeName, oldFence, fenceFd);
534 mHwcCurReleaseFences->replaceValueAt(idx, newFence);
535 HwcFenceControl::closeFd(oldFence);
536 HwcFenceControl::closeFd(fenceFd);
537 ETRACE("addReleaseFence:(%d, %d) + %d -> (%d,%d)\n", idx, oldFence, fenceFd, idx, newFence);
538 dumpFenceList(mHwcCurReleaseFences);
539 } else {
540 mHwcCurReleaseFences->add(layerId, fenceFd);
541 }
542}
543
544void PhysicalDevice::clearFenceList(KeyedVector<hwc2_layer_t, int32_t> * fenceList) {
545 if (!fenceList || !fenceList->size())
546 return;
547
548 for (int i = 0; i < fenceList->size(); i++) {
549 int32_t fenceFd = fenceList->valueAt(i);
550 HwcFenceControl::closeFd(fenceFd);
551 DTRACE("clearFenceList close fd %d\n", fenceFd);
552 fenceList->replaceValueAt(i, -1);
553 }
554 fenceList->clear();
555}
556
557void PhysicalDevice::dumpFenceList(KeyedVector<hwc2_layer_t, int32_t> * fenceList) {
558 if (!fenceList || fenceList->isEmpty())
559 return;
560
561 String8 resultStr("dumpFenceList: ");
562 for (int i = 0; i < fenceList->size(); i++) {
563 hwc2_layer_t layerId = fenceList->keyAt(i);
564 int32_t fenceFd = fenceList->valueAt(i);
565 resultStr.appendFormat("(%lld, %d), ", layerId, fenceFd);
566 }
567
568 ETRACE("%s", resultStr.string());
569}
570
571int32_t PhysicalDevice::getReleaseFences(
572 uint32_t* outNumElements,
573 hwc2_layer_t* outLayers,
574 int32_t* outFences) {
575 *outNumElements = mHwcPriorReleaseFences->size();
576
577 if (outLayers && outFences) {
578 for (uint32_t i=0; i<mHwcPriorReleaseFences->size(); i++) {
579 outLayers[i] = mHwcPriorReleaseFences->keyAt(i);
580 outFences[i] = HwcFenceControl::dupFence(mHwcPriorReleaseFences->valueAt(i));
581 }
582 }
583
584 return HWC2_ERROR_NONE;
585}
586
587void PhysicalDevice::directCompose(framebuffer_info_t * fbInfo) {
588 HwcLayer* layer = NULL;
589 ssize_t idx = mHwcLayers.indexOfKey(mDirectRenderLayerId);
590 if (idx >= 0) {
591 layer = mHwcLayers.valueAt(idx);
592 if (mTargetAcquireFence > -1) {
593 ETRACE("ERROR:directCompose with mTargetAcquireFence %d\n", mTargetAcquireFence);
594 HwcFenceControl::closeFd(mTargetAcquireFence);
595 }
596
597 mTargetAcquireFence = layer->getDuppedAcquireFence();
598 mClientTargetHnd = layer->getBufferHandle();
599 DTRACE("Hit only one non video overlay layer, handle: %08" PRIxPTR ", fence: %d",
600 intptr_t(mClientTargetHnd), mTargetAcquireFence);
601
602 /* hwc_rect_t displayframe = layer->getDisplayFrame();
603 fbInfo->info.xoffset = displayframe.left;
604 fbInfo->info.yoffset = displayframe.top; */
605 return;
606 }
607
608 ETRACE("Didn't find direct compose layer!");
609}
610
611#ifdef ENABLE_AML_GE2D_COMPOSER
612void PhysicalDevice::ge2dCompose(framebuffer_info_t * fbInfo, bool hasVideoOverlay) {
613 if (mGE2DRenderSortedLayerIds.size() > 0) {
614 DTRACE("GE2D compose mFbSlot: %d", mFbSlot);
615 if (hasVideoOverlay) {
616 if (mGE2DClearVideoRegionCount < 3) {
617 mComposer->setVideoOverlayLayerId(mVideoOverlayLayerId);
618 }
619 }
620 if (mTargetAcquireFence > -1) {
621 ETRACE("ERROR:GE2D compose with mTargetAcquireFence %d\n", mTargetAcquireFence);
622 HwcFenceControl::closeFd(mTargetAcquireFence);
623 }
624 mTargetAcquireFence = mComposer->startCompose(mGE2DRenderSortedLayerIds, &mFbSlot, mGE2DComposeFrameCount);
625 for (uint32_t i=0; i<mGE2DRenderSortedLayerIds.size(); i++) {
626 addReleaseFence(mGE2DRenderSortedLayerIds.itemAt(i), HwcFenceControl::dupFence(mTargetAcquireFence));
627 }
628 // HwcFenceControl::traceFenceInfo(mTargetAcquireFence);
629 // dumpLayers(mGE2DRenderSortedLayerIds);
630 if (mGE2DComposeFrameCount < 3) {
631 mGE2DComposeFrameCount++;
632 }
633 mClientTargetHnd = mComposer->getBufHnd();
634 fbInfo->yOffset = mFbSlot;
635 return;
636 }
637
638 ETRACE("Didn't find ge2d compose layers!");
639}
640#endif
641
642int32_t PhysicalDevice::postFramebuffer(int32_t* outRetireFence, bool hasVideoOverlay) {
643 HwcLayer* layer = NULL;
644 void *cbuffer;
645
646 // deal physical display's client target layer
647 framebuffer_info_t fbInfo = *(mFramebufferContext->getInfo());
648 framebuffer_info_t* cbInfo = mCursorContext->getInfo();
649 bool cursorShow = false;
650 bool haveCursorLayer = false;
651 for (uint32_t i=0; i<mHwcLayers.size(); i++) {
652 hwc2_layer_t layerId = mHwcLayers.keyAt(i);
653 layer = mHwcLayers.valueAt(i);
654 if (layer && layer->getCompositionType()== HWC2_COMPOSITION_CURSOR) {
655 private_handle_t *hnd = (private_handle_t *)(layer->getBufferHandle());
656 if (private_handle_t::validate(hnd) < 0) {
657 ETRACE("invalid cursor layer handle.");
658 break;
659 }
660 haveCursorLayer = true;
661 DTRACE("This is a Sprite, hnd->stride is %d, hnd->height is %d", hnd->stride, hnd->height);
662 if (cbInfo->info.xres != (uint32_t)hnd->stride || cbInfo->info.yres != (uint32_t)hnd->height) {
663 ETRACE("disp: %d cursor need to redrew", mId);
664 update_cursor_buffer_locked(cbInfo, hnd->stride, hnd->height);
665 cbuffer = mmap(NULL, hnd->size, PROT_READ|PROT_WRITE, MAP_SHARED, cbInfo->fd, 0);
666 if (cbuffer != MAP_FAILED) {
667 memcpy(cbuffer, hnd->base, hnd->size);
668 munmap(cbuffer, hnd->size);
669 DTRACE("setCursor ok");
670 } else {
671 ETRACE("buffer mmap fail");
672 }
673 }
674 cursorShow = true;
675 break;
676 }
677 }
678
679 if (mRenderMode == GLES_COMPOSE_MODE) {
680 /*private_handle_t const* buffer = reinterpret_cast<private_handle_t const*>(mClientTargetHnd);
681 if (buffer && private_handle_t::validate(buffer) == 0) {
682 uint32_t slot = buffer->offset / fbInfo.finfo.line_length;
683 mComposer->setCurGlesFbSlot(slot);
684 DTRACE("GLES compose Slot: %d", slot);
685 }*/
686 } else if (mRenderMode == DIRECT_COMPOSE_MODE) { // if only one layer exists, let hwc do her work.
687 directCompose(&fbInfo);
688 }
689#ifdef ENABLE_AML_GE2D_COMPOSER
690 else if (mRenderMode == GE2D_COMPOSE_MODE) {
691 ge2dCompose(&fbInfo, hasVideoOverlay);
692 }
693#endif
694 fbInfo.renderMode = mRenderMode;
695
696 bool needBlankFb0 = false;
697 uint32_t layerNum = mHwcLayers.size();
698 if (hasVideoOverlay && (layerNum == 1 || (layerNum == 2 && haveCursorLayer))) needBlankFb0 = true;
699
700 if (mIsContinuousBuf) {
701 // bit 0 is osd blank flag.
702 fbInfo.op &= ~0x00000001;
703 if (needBlankFb0) {
704 fbInfo.op |= 0x00000001;
705 }
706 mFramebufferContext->setStatus(needBlankFb0);
707 } else {
708 setOSD0Blank(needBlankFb0);
709 }
710
711 if (!mClientTargetHnd || private_handle_t::validate(mClientTargetHnd) < 0 || mPowerMode == HWC2_POWER_MODE_OFF) {
712 DTRACE("mClientTargetHnd is null or Enter suspend state, mTargetAcquireFence: %d", mTargetAcquireFence);
713 *outRetireFence = HwcFenceControl::merge(String8("Layer"), mPriorFrameRetireFence, mTargetAcquireFence);
714 HwcFenceControl::closeFd(mTargetAcquireFence);
715 mTargetAcquireFence = -1;
716 HwcFenceControl::closeFd(mPriorFrameRetireFence);
717 mPriorFrameRetireFence = -1;
718 if (private_handle_t::validate(mClientTargetHnd) < 0) {
719 ETRACE("mClientTargetHnd is not validate!");
720 }
721 } else {
722 *outRetireFence = HwcFenceControl::dupFence(mPriorFrameRetireFence);
723 if (*outRetireFence >= 0) {
724 DTRACE("Get prior frame's retire fence %d", *outRetireFence);
725 } else {
726 ETRACE("No valid prior frame's retire returned. %d ", *outRetireFence);
727 // -1 means no fence, less than -1 is some error
728 *outRetireFence = -1;
729 }
730 HwcFenceControl::closeFd(mPriorFrameRetireFence);
731 mPriorFrameRetireFence = -1;
732
733 // real post framebuffer here.
734 DTRACE("fbInfo->renderMode: %d", fbInfo.renderMode);
735 if (!mIsContinuousBuf) {
736 mPriorFrameRetireFence = fb_post_with_fence_locked(&fbInfo, mClientTargetHnd, mTargetAcquireFence);
737 } else {
738 mPriorFrameRetireFence = hwc_fb_post_with_fence_locked(&fbInfo, mClientTargetHnd, mTargetAcquireFence);
739 }
740 mTargetAcquireFence = -1;
741
742 if (mRenderMode == GE2D_COMPOSE_MODE) {
743 mComposer->mergeRetireFence(mFbSlot, HwcFenceControl::dupFence(mPriorFrameRetireFence));
744 } else {
745 if (mComposer && mGE2DComposeFrameCount != 0) {
746 mComposer->removeRetireFence(mFbSlot);
747 }
748
749 if (mRenderMode == DIRECT_COMPOSE_MODE) {
750 addReleaseFence(mDirectRenderLayerId, HwcFenceControl::dupFence(mPriorFrameRetireFence));
751 }
752 }
753
754 // finally we need to update cursor's blank status.
755 if (cbInfo->fd > 0 && cursorShow != mCursorContext->getStatus()) {
756 mCursorContext->setStatus(cursorShow);
757 DTRACE("UPDATE FB1 status to %d", !cursorShow);
758 ioctl(cbInfo->fd, FBIOBLANK, !cursorShow);
759 }
760 }
761
762 if (mRenderMode != GE2D_COMPOSE_MODE) {
763 mGE2DComposeFrameCount = 0;
764 }
765
766 return HWC2_ERROR_NONE;
767}
768
769// TODO: need add fence wait.
770int32_t PhysicalDevice::setOSD0Blank(bool blank) {
771 framebuffer_info_t* fbInfo = mFramebufferContext->getInfo();
772
773 if (fbInfo->fd > 0 && blank != mFramebufferContext->getStatus()) {
774 mFramebufferContext->setStatus(blank);
775 DTRACE("UPDATE FB0 status to %d", blank);
776 ioctl(fbInfo->fd, FBIOBLANK, blank);
777 }
778
779 return HWC2_ERROR_NONE;
780}
781
782int32_t PhysicalDevice::presentDisplay(
783 int32_t* outRetireFence) {
784 int32_t err = HWC2_ERROR_NONE;
785 HwcLayer* layer = NULL;
786 bool hasVideoOverlay = false;
787
788 if (mIsValidated) {
789 // TODO: need improve the way to set video axis.
790#if WITH_LIBPLAYER_MODULE
791 ssize_t index = mHwcLayers.indexOfKey(mVideoOverlayLayerId);
792 if (index >= 0) {
793 layer = mHwcLayers.valueFor(mVideoOverlayLayerId);
794 if (layer != NULL) {
795 layer->presentOverlay();
796 hasVideoOverlay = true;
797 if (mGE2DClearVideoRegionCount < 3) {
798 mGE2DClearVideoRegionCount++;
799 }
800 }
801 } else {
802 mGE2DClearVideoRegionCount = 0;
803 }
804#endif
805 err = postFramebuffer(outRetireFence, hasVideoOverlay);
806
807 mIsValidated = false;
808 } else { // display not validate yet.
809 err = HWC2_ERROR_NOT_VALIDATED;
810 }
811
812 // reset layers' acquire fence.
813 for (uint32_t i=0; i<mHwcLayers.size(); i++) {
814 hwc2_layer_t layerId = mHwcLayers.keyAt(i);
815 layer = mHwcLayers.valueAt(i);
816 if (layer != NULL) {
817 layer->resetAcquireFence();
818 }
819 }
820
821 return err;
822}
823
824int32_t PhysicalDevice::setActiveConfig(
825 hwc2_config_t config) {
826 Mutex::Autolock _l(mLock);
827
828 int32_t err = mDisplayHdmi->setActiveConfig(config);
829 if (err == HWC2_ERROR_NONE)
830 mVsyncObserver->setRefreshRate(mDisplayHdmi->getActiveRefreshRate());
831
832 return err;
833}
834
835int32_t PhysicalDevice::setClientTarget(
836 buffer_handle_t target,
837 int32_t acquireFence,
838 int32_t /*android_dataspace_t*/ dataspace,
839 hwc_region_t damage) {
840
841 if (target && private_handle_t::validate(target) < 0) {
842 return HWC2_ERROR_BAD_PARAMETER;
843 }
844
845 if (NULL != target) {
846 mClientTargetHnd = target;
847 mClientTargetDamageRegion = damage;
848 mTargetAcquireFence = acquireFence;
849 DTRACE("setClientTarget %p, %d", target, acquireFence);
850 // TODO: HWC2_ERROR_BAD_PARAMETER && dataspace && damage.
851 } else {
852 DTRACE("client target is null!, no need to update this frame.");
853 }
854
855 return HWC2_ERROR_NONE;
856}
857
858int32_t PhysicalDevice::setColorMode(
859 int32_t /*android_color_mode_t*/ mode) {
860 return HWC2_ERROR_NONE;
861}
862
863int32_t PhysicalDevice::setColorTransform(
864 const float* matrix,
865 int32_t /*android_color_transform_t*/ hint) {
866 return HWC2_ERROR_NONE;
867}
868
869int32_t PhysicalDevice::setPowerMode(
870 int32_t /*hwc2_power_mode_t*/ mode){
871
872 mPowerMode = mode;
873 return HWC2_ERROR_NONE;
874}
875
876bool PhysicalDevice::vsyncControl(bool enabled) {
877 RETURN_FALSE_IF_NOT_INIT();
878
879 ATRACE("disp = %d, enabled = %d", mId, enabled);
880 return mVsyncObserver->control(enabled);
881}
882
883void PhysicalDevice::dumpLayers(Vector < hwc2_layer_t > layerIds) {
884 for (uint32_t x=0; x<layerIds.size(); x++) {
885 HwcLayer* layer = getLayerById(layerIds.itemAt(x));
886 static char const* compositionTypeName[] = {
887 "UNKNOWN",
888 "GLES",
889 "HWC",
890 "SOLID",
891 "HWC_CURSOR",
892 "SIDEBAND"};
893 ETRACE(" %11s | %12" PRIxPTR " | %10d | %02x | %1.2f | %02x | %04x |%7.1f,%7.1f,%7.1f,%7.1f |%5d,%5d,%5d,%5d \n",
894 compositionTypeName[layer->getCompositionType()],
895 intptr_t(layer->getBufferHandle()), layer->getZ(), layer->getDataspace(),
896 layer->getPlaneAlpha(), layer->getTransform(), layer->getBlendMode(),
897 layer->getSourceCrop().left, layer->getSourceCrop().top, layer->getSourceCrop().right, layer->getSourceCrop().bottom,
898 layer->getDisplayFrame().left, layer->getDisplayFrame().top, layer->getDisplayFrame().right, layer->getDisplayFrame().bottom);
899 }
900}
901
902void PhysicalDevice::dumpLayers(KeyedVector<hwc2_layer_t, HwcLayer*> layers) {
903 for (uint32_t x=0; x<layers.size(); x++) {
904 HwcLayer* layer = layers.valueAt(x);
905 static char const* compositionTypeName[] = {
906 "UNKNOWN",
907 "GLES",
908 "HWC",
909 "SOLID",
910 "HWC_CURSOR",
911 "SIDEBAND"};
912 ETRACE(" %11s | %12" PRIxPTR " | %10d | %02x | %1.2f | %02x | %04x |%7.1f,%7.1f,%7.1f,%7.1f |%5d,%5d,%5d,%5d \n",
913 compositionTypeName[layer->getCompositionType()],
914 intptr_t(layer->getBufferHandle()), layer->getZ(), layer->getDataspace(),
915 layer->getPlaneAlpha(), layer->getTransform(), layer->getBlendMode(),
916 layer->getSourceCrop().left, layer->getSourceCrop().top, layer->getSourceCrop().right, layer->getSourceCrop().bottom,
917 layer->getDisplayFrame().left, layer->getDisplayFrame().top, layer->getDisplayFrame().right, layer->getDisplayFrame().bottom);
918 }
919}
920
921bool PhysicalDevice::layersStateCheck(int32_t renderMode,
922 KeyedVector<hwc2_layer_t, HwcLayer*> & composeLayers) {
923 bool ret = false;
924 uint32_t layerNum = composeLayers.size();
925 hwc_frect_t sourceCrop[HWC2_MAX_LAYERS];
926 HwcLayer* layer[HWC2_MAX_LAYERS] = { NULL };
927 private_handle_t const* hnd[HWC2_MAX_LAYERS] = { NULL };
928 hwc_rect_t displayFrame[HWC2_MAX_LAYERS];
929
930 for (int32_t i=0; i<layerNum; i++) {
931 layer[i] = composeLayers.valueAt(i);
932 sourceCrop[i] = layer[i]->getSourceCrop();
933 displayFrame[i] = layer[i]->getDisplayFrame();
934 hnd[i] = reinterpret_cast<private_handle_t const*>(layer[i]->getBufferHandle());
935 if (hnd[i] == NULL) return false; // no buffer to process.
936 if (hnd[i]->share_fd == -1) return false; // no buffer to process.
937 DTRACE("layer[%d] zorder: %d, blend: %d, PlaneAlpha: %f, "
938 "mColor: [%d, %d, %d, %d], mDataSpace: %d, format hnd[%d]: %x",
939 i, layer[i]->getZ(), layer[i]->getBlendMode(), layer[i]->getPlaneAlpha(),
940 layer[i]->getColor().r, layer[i]->getColor().g, layer[i]->getColor().b,
941 layer[i]->getColor().a, layer[i]->getDataspace(), i, hnd[i]->format);
942 }
943
944 if (renderMode == DIRECT_COMPOSE_MODE) {
945 switch (hnd[0]->format) {
946 case HAL_PIXEL_FORMAT_RGBA_8888:
947 case HAL_PIXEL_FORMAT_RGBX_8888:
948 case HAL_PIXEL_FORMAT_RGB_888:
949 case HAL_PIXEL_FORMAT_RGB_565:
950 case HAL_PIXEL_FORMAT_BGRA_8888:
951 DTRACE("Layer format match direct composer.");
952 ret = true;
953 break;
954 default:
955 DTRACE("Layer format not support by direct compose");
956 return false;
957 break;
958 }
959 if (layer[0]->isCropped()
960 || layer[0]->isScaled()
961 || layer[0]->isOffset()) {
962 DTRACE("direct compose can not process!");
963 return false;
964 }
965 }
966#ifdef ENABLE_AML_GE2D_COMPOSER
967 else if (renderMode == GE2D_COMPOSE_MODE) {
968 bool yuv420Sp = false;
969 for (int32_t i=0; i<layerNum; i++) {
970 switch (hnd[i]->format) {
971 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
972 yuv420Sp = true;
973 case HAL_PIXEL_FORMAT_RGBA_8888:
974 case HAL_PIXEL_FORMAT_RGBX_8888:
975 case HAL_PIXEL_FORMAT_RGB_888:
976 case HAL_PIXEL_FORMAT_RGB_565:
977 case HAL_PIXEL_FORMAT_BGRA_8888:
978 case HAL_PIXEL_FORMAT_YV12:
979 case HAL_PIXEL_FORMAT_Y8:
980 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
981 case HAL_PIXEL_FORMAT_YCbCr_422_I:
982 DTRACE("Layer format match ge2d composer.");
983 ret = true;
984 break;
985 default:
986 DTRACE("Layer format not support by ge2d");
987 return false;
988 break;
989 }
990 if (layer[i]->havePlaneAlpha()
991 || layer[i]->haveColor()
992 || layer[i]->haveDataspace()
993 || (layer[i]->isBlended()
994 && layer[i]->isScaled())) {
995 DTRACE("ge2d compose can not process!");
996 return false;
997 }
998 }
999#if 0
1000 if (yuv420Sp && HWC2_TWO_LAYERS == layerNum) {
1001 if (Utils::compareRect(sourceCrop[0], sourceCrop[1])
1002 && Utils::compareRect(sourceCrop[0], displayFrame[0])
1003 && Utils::compareRect(sourceCrop[1], displayFrame[1])) {
1004 DTRACE("2 layers is same size and have yuv420sp format ge2d compose can not process!");
1005 return false;
1006 }
1007 }
1008#endif
1009 }
1010#endif
1011
1012 return ret;
1013}
1014
1015/*************************************************************
1016 * For direct framebuffer composer:
1017 * 1) only support one layer.
1018 * 2) layer format: rgba, rgbx,rgb565,bgra;
1019 * 3) layer no need scale to display;
1020 * 4) layer has no offset to display;
1021
1022 * For ge2d composer:
1023 * 1) support layer format that direct composer can't support.
1024 * 2) support 2 layers blending.
1025 * 3) support scale and rotation etc.
1026**************************************************************/
1027int32_t PhysicalDevice::composersFilter(
1028 KeyedVector<hwc2_layer_t, HwcLayer*> & composeLayers) {
1029
1030 // direct Composer.
1031 if (composeLayers.size() == HWC2_ONE_LAYER) {
1032 // if only one layer exists, do direct framebuffer composer.
1033 bool directCompose = layersStateCheck(DIRECT_COMPOSE_MODE, composeLayers);
1034 if (directCompose) {
1035 if (mDirectComposeFrameCount >= 3) {
1036 hwc2_layer_t layerGlesLayerId = composeLayers.keyAt(0);
1037 composeLayers.clear();
1038 mDirectRenderLayerId = layerGlesLayerId;
1039 return DIRECT_COMPOSE_MODE;
1040 }
1041 mDirectComposeFrameCount++;
1042 return GLES_COMPOSE_MODE;
1043 }
1044 }
1045
1046#ifdef ENABLE_AML_GE2D_COMPOSER
1047 // if direct composer can't work, try this.
1048 if (composeLayers.size() > HWC2_NO_LAYER
1049 && composeLayers.size() < HWC2_MAX_LAYERS) {
1050 bool ge2dCompose = layersStateCheck(GE2D_COMPOSE_MODE, composeLayers);
1051 if (!ge2dCompose) return GLES_COMPOSE_MODE;
1052 mGE2DRenderSortedLayerIds.clear();
1053 for (uint32_t i=0; i<composeLayers.size(); i++) {
1054 hwc2_layer_t layerGlesLayerId = composeLayers.keyAt(i);
1055 HwcLayer* layer = getLayerById(layerGlesLayerId);
1056 if (0 == i) {
1057 mGE2DRenderSortedLayerIds.push_front(layerGlesLayerId);
1058 continue;
1059 }
1060 for (uint32_t j=0; j<i; j++) {
1061 HwcLayer* layer1 = getLayerById(mGE2DRenderSortedLayerIds.itemAt(j));
1062 HwcLayer* layer2 = getLayerById(layerGlesLayerId);
1063 if (layer1 != NULL && layer2 != NULL) {
1064 uint32_t z1 = layer1->getZ();
1065 uint32_t z2 = layer2->getZ();
1066 if (layer1->getZ() > layer2->getZ()) {
1067 mGE2DRenderSortedLayerIds.insertAt(layerGlesLayerId, j, 1);
1068 break;
1069 }
1070 if (j == i-1) mGE2DRenderSortedLayerIds.push_back(layerGlesLayerId);
1071 } else {
1072 ETRACE("Layer1 or Layer2 is NULL!!!");
1073 }
1074 }
1075 }
1076
1077 // Vector < hwc2_layer_t > layerIds;
1078 if (!mComposer) {
1079 // create ge2d composer...
1080 mComposer = mControlFactory->createComposer(*this);
1081 if (!mComposer || !mComposer->initialize(mFramebufferContext->getInfo())) {
1082 DEINIT_AND_DELETE_OBJ(mComposer);
1083 return GLES_COMPOSE_MODE;
1084 }
1085 }
1086 composeLayers.clear();
1087 return GE2D_COMPOSE_MODE;
1088 }
1089#endif
1090
1091 return GLES_COMPOSE_MODE;
1092}
1093
1094void PhysicalDevice::clearFramebuffer() {
1095 hwc_rect_t clipRect;
1096 framebuffer_info_t* fbInfo = mFramebufferContext->getInfo();
1097 uint32_t offset = 0;
1098 clipRect.left = 0;
1099 clipRect.top = 0;
1100 clipRect.right = fbInfo->info.xres;
1101 clipRect.bottom = fbInfo->info.yres_virtual;
1102 mComposer->fillRectangle(clipRect, 0, offset, mFramebufferHnd->share_fd);
1103
1104 DTRACE("Composer mode: %d, %d layers", mRenderMode, mHwcLayers.size());
1105}
1106
1107int32_t PhysicalDevice::validateDisplay(uint32_t* outNumTypes,
1108 uint32_t* outNumRequests) {
1109 HwcLayer* layer = NULL;
1110 bool istvp = false;
1111 bool glesCompose = false;
1112 bool zeroLayer = false;
1113
1114 mRenderMode = GLES_COMPOSE_MODE;
1115 mVideoOverlayLayerId = 0;
1116 mIsContinuousBuf = true;
1117 swapReleaseFence();
1118
1119 for (uint32_t i=0; i<mHwcLayers.size(); i++) {
1120 hwc2_layer_t layerId = mHwcLayers.keyAt(i);
1121 layer = mHwcLayers.valueAt(i);
1122 if (layer != NULL) {
1123 // Physical Display.
1124 private_handle_t const* hnd =
1125 reinterpret_cast<private_handle_t const*>(layer->getBufferHandle());
1126 if (hnd) {
1127 // continous buffer.
1128 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_CONTINUOUS_BUF
1129 || hnd->flags & private_handle_t::PRIV_FLAGS_VIDEO_OVERLAY)) {
1130 DTRACE("continous buffer flag is not set, bufhnd: 0x%" PRIxPTR "", intptr_t(layer->getBufferHandle()));
1131 mIsContinuousBuf = false;
1132 }
1133#ifdef GRALLOC_ENABLE_SECURE_LAYER
1134 // secure or protected layer.
1135 if (!mSecure && (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_PROTECTED)) {
1136 ETRACE("layer's secure or protected buffer flag is set!");
1137 if (layer->getCompositionType() != HWC2_COMPOSITION_DEVICE) {
1138 mHwcLayersChangeType.add(layerId, layer);
1139 }
1140 mHwcSecureLayers.add(layerId, layer);
1141 continue;
1142 }
1143#endif
1144 if (layer->getCompositionType() == HWC2_COMPOSITION_DEVICE) {
1145 // video overlay.
1146 if (hnd->flags & private_handle_t::PRIV_FLAGS_VIDEO_OMX) {
1147 set_omx_pts((char*)hnd->base, &Amvideo_Handle);
1148 istvp = true;
1149 }
1150 if (hnd->flags & private_handle_t::PRIV_FLAGS_VIDEO_OVERLAY) {
1151 DTRACE("PRIV_FLAGS_VIDEO_OVERLAY!!!!");
1152 mVideoOverlayLayerId = layerId;
1153 mHwcLayersChangeRequest.add(layerId, layer);
1154 continue;
1155 }
1156
1157 mHwcGlesLayers.add(layerId, layer);
1158 continue;
1159 }
1160 }
1161
1162 // cursor layer.
1163 if (layer->getCompositionType() == HWC2_COMPOSITION_CURSOR) {
1164 DTRACE("This is a Cursor layer!");
1165 mHwcLayersChangeRequest.add(layerId, layer);
1166 continue;
1167 }
1168
1169 // sideband stream.
1170 if (layer->getCompositionType() == HWC2_COMPOSITION_SIDEBAND
1171 && layer->getSidebandStream()) {
1172 // TODO: we just transact SIDEBAND to OVERLAY for now;
1173 DTRACE("get HWC_SIDEBAND layer, just change to overlay");
1174 mHwcLayersChangeRequest.add(layerId, layer);
1175 mHwcLayersChangeType.add(layerId, layer);
1176 mVideoOverlayLayerId = layerId;
1177 continue;
1178 }
1179
1180 // TODO: solid color.
1181 if (layer->getCompositionType() == HWC2_COMPOSITION_SOLID_COLOR) {
1182 DTRACE("This is a Solid Color layer!");
1183 // mHwcLayersChangeRequest.add(layerId, layer);
1184 // mHwcLayersChangeType.add(layerId, layer);
1185 mHwcGlesLayers.add(layerId, layer);
1186 continue;
1187 }
1188
1189 if (layer->getCompositionType() == HWC2_COMPOSITION_CLIENT) {
1190 DTRACE("Meet a client layer!");
1191 glesCompose = true;
1192 }
1193 }
1194 }
1195
1196 bool noDevComp = Utils::checkBoolProp("sys.sf.debug.nohwc");
1197#ifndef USE_CONTINOUS_BUFFER_COMPOSER
1198 DTRACE("No continous buffer composer!");
1199 noDevComp = true;
1200 mIsContinuousBuf = false;
1201#endif
1202
1203 if (mHwcLayers.size() == 0) {
1204 zeroLayer = true;
1205 mIsContinuousBuf = false;
1206 }
1207
1208 // dumpLayers(mHwcLayers);
1209 if (mIsContinuousBuf && !glesCompose && !noDevComp) {
1210 mRenderMode = composersFilter(mHwcGlesLayers);
1211 } else {
1212 mDirectComposeFrameCount = 0;
1213 }
1214
1215 if (mComposer && zeroLayer) {
1216 clearFramebuffer();
1217 }
1218
1219 if (mHwcGlesLayers.size() > 0) {
1220 for (int i=0;i<mHwcGlesLayers.size(); i++) {
1221 mHwcLayersChangeType.add(mHwcGlesLayers.keyAt(i), mHwcGlesLayers.valueAt(i));
1222 }
1223 mHwcGlesLayers.clear();
1224 }
1225
1226 if (istvp == false && Amvideo_Handle!=0) {
1227 closeamvideo();
1228 Amvideo_Handle = 0;
1229 }
1230
1231 if (mHwcLayersChangeRequest.size() > 0) {
1232 DTRACE("There are %d layer requests.", mHwcLayersChangeRequest.size());
1233 *outNumRequests = mHwcLayersChangeRequest.size();
1234 }
1235
1236 // mark the validate function is called.(???)
1237 if (zeroLayer) {
1238 mIsValidated = false;
1239 } else {
1240 mIsValidated = true;
1241 }
1242
1243 if (mHwcLayersChangeType.size() > 0) {
1244 DTRACE("there are %d layer types has changed.", mHwcLayersChangeType.size());
1245 *outNumTypes = mHwcLayersChangeType.size();
1246 return HWC2_ERROR_HAS_CHANGES;
1247 }
1248
1249 return HWC2_ERROR_NONE;
1250}
1251
1252int32_t PhysicalDevice::setCursorPosition(hwc2_layer_t layerId, int32_t x, int32_t y) {
1253 HwcLayer* layer = getLayerById(layerId);
1254 if (layer && HWC2_COMPOSITION_CURSOR == layer->getCompositionType()) {
1255 framebuffer_info_t* cbInfo = mCursorContext->getInfo();
1256 fb_cursor cinfo;
1257 if (cbInfo->fd < 0) {
1258 ETRACE("setCursorPosition fd=%d", cbInfo->fd );
1259 }else {
1260 cinfo.hot.x = x;
1261 cinfo.hot.y = y;
1262 DTRACE("setCursorPosition x_pos=%d, y_pos=%d", cinfo.hot.x, cinfo.hot.y);
1263 ioctl(cbInfo->fd, FBIO_CURSOR, &cinfo);
1264 }
1265 } else {
1266 ETRACE("setCursorPosition bad layer.");
1267 return HWC2_ERROR_BAD_LAYER;
1268 }
1269
1270 return HWC2_ERROR_NONE;
1271}
1272
1273
1274/*
1275Operater of framebuffer
1276*/
1277int32_t PhysicalDevice::initDisplay() {
1278 if (mIsConnected) return 0;
1279
1280 Mutex::Autolock _l(mLock);
1281
1282 if (!mFramebufferHnd) {
1283 // init framebuffer context.
1284 mFramebufferContext = new FBContext();
1285 framebuffer_info_t* fbInfo = mFramebufferContext->getInfo();
1286 // init information from osd.
1287 fbInfo->displayType = mId;
1288 fbInfo->fbIdx = getOsdIdx(mId);
1289 int32_t err = init_frame_buffer_locked(fbInfo);
1290 int32_t bufferSize = fbInfo->finfo.line_length
1291 * fbInfo->info.yres;
1292 DTRACE("init_frame_buffer get fbinfo->fbIdx (%d) "
1293 "fbinfo->info.xres (%d) fbinfo->info.yres (%d)",
1294 fbInfo->fbIdx, fbInfo->info.xres,
1295 fbInfo->info.yres);
1296 int32_t usage = 0;
1297 private_module_t *grallocModule = Hwcomposer::getInstance().getGrallocModule();
1298 if (mId == HWC_DISPLAY_PRIMARY) {
1299 grallocModule->fb_primary.fb_info = *(fbInfo);
1300 } else if (mId == HWC_DISPLAY_EXTERNAL) {
1301 grallocModule->fb_external.fb_info = *(fbInfo);
1302 usage |= GRALLOC_USAGE_EXTERNAL_DISP;
1303 }
1304 fbInfo->grallocModule = grallocModule;
1305
1306 //Register the framebuffer to gralloc module
1307 mFramebufferHnd = new private_handle_t(
1308 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
1309 usage, fbInfo->fbSize, 0,
1310 0, fbInfo->fd, bufferSize, 0);
1311 grallocModule->base.registerBuffer(&(grallocModule->base), mFramebufferHnd);
1312 DTRACE("init_frame_buffer get frame size %d usage %d",
1313 bufferSize, usage);
1314 }
1315
1316 mIsConnected = true;
1317
1318 // init cursor framebuffer
1319 mCursorContext = new FBContext();
1320 framebuffer_info_t* cbInfo = mCursorContext->getInfo();
1321 cbInfo->fd = -1;
1322
1323 //init information from cursor framebuffer.
1324 cbInfo->fbIdx = mId*2+1;
1325 if (1 != cbInfo->fbIdx && 3 != cbInfo->fbIdx) {
1326 ETRACE("invalid fb index: %d, need to check!",
1327 cbInfo->fbIdx);
1328 return 0;
1329 }
1330 int32_t err = init_cursor_buffer_locked(cbInfo);
1331 if (err != 0) {
1332 ETRACE("init_cursor_buffer_locked failed, need to check!");
1333 return 0;
1334 }
1335 ITRACE("init_cursor_buffer get cbinfo->fbIdx (%d) "
1336 "cbinfo->info.xres (%d) cbinfo->info.yres (%d)",
1337 cbInfo->fbIdx,
1338 cbInfo->info.xres,
1339 cbInfo->info.yres);
1340
1341 if ( cbInfo->fd >= 0) {
1342 DTRACE("init_cursor_buffer success!");
1343 }else{
1344 DTRACE("init_cursor_buffer fail!");
1345 }
1346
1347 return 0;
1348}
1349
1350bool PhysicalDevice::updateDisplayConfigs() {
1351 Mutex::Autolock _l(mLock);
1352 bool ret;
1353
1354 if (!mIsConnected) {
1355 ETRACE("disp: %llu is not connected", mId);
1356 return false;
1357 }
1358
1359 framebuffer_info_t* fbinfo = mFramebufferContext->getInfo();
1360 ret = Utils::checkVinfo(fbinfo);
1361 if (!ret) {
1362 ETRACE("checkVinfo fail");
1363 return false;
1364 }
1365
1366 mDisplayHdmi->updateHotplug(mIsConnected, fbinfo, mFramebufferHnd);
1367 if (mIsConnected)
1368 mVsyncObserver->setRefreshRate(mDisplayHdmi->getActiveRefreshRate());
1369 //ETRACE("updateDisplayConfigs rate:%d", mDisplayHdmi->getActiveRefreshRate());
1370
1371 // check hdcp authentication status when hotplug is happen.
1372 if (mSystemControl == NULL) {
1373 mSystemControl = getSystemControlService();
1374 } else {
1375 DTRACE("already have system control instance.");
1376 }
1377 if (mSystemControl != NULL) {
1378 // mSecure = Utils::checkHdcp();
1379 int status = 0;
1380 mSystemControl->isHDCPTxAuthSuccess(status);
1381 DTRACE("hdcp status: %d", status);
1382 mSecure = (status == 1) ? true : false;
1383 } else {
1384 ETRACE("can't get system control.");
1385 }
1386
1387 return true;
1388}
1389
1390sp<ISystemControlService> PhysicalDevice::getSystemControlService()
1391{
1392 sp<IServiceManager> sm = defaultServiceManager();
1393 if (sm == NULL) {
1394 ETRACE("Couldn't get default ServiceManager\n");
1395 return NULL;
1396 }
1397 sp<IBinder> binder = sm->getService(String16("system_control"));
1398 sp<ISystemControlService> sc = interface_cast<ISystemControlService>(binder);
1399
1400 return sc;
1401}
1402
1403void PhysicalDevice::onVsync(int64_t timestamp) {
1404 RETURN_VOID_IF_NOT_INIT();
1405 ATRACE("timestamp = %lld", timestamp);
1406
1407 if (!mIsConnected)
1408 return;
1409
1410 // notify hwc
1411 mHwc.vsync(mId, timestamp);
1412}
1413
1414int32_t PhysicalDevice::createVirtualDisplay(
1415 uint32_t width,
1416 uint32_t height,
1417 int32_t* /*android_pixel_format_t*/ format,
1418 hwc2_display_t* outDisplay) {
1419
1420 return HWC2_ERROR_NONE;
1421}
1422
1423int32_t PhysicalDevice::destroyVirtualDisplay(
1424 hwc2_display_t display) {
1425
1426 return HWC2_ERROR_NONE;
1427}
1428
1429int32_t PhysicalDevice::setOutputBuffer(
1430 buffer_handle_t buffer, int32_t releaseFence) {
1431 // Virtual Display Only.
1432 return HWC2_ERROR_NONE;
1433}
1434
1435void PhysicalDevice::updateHotplugState(bool connected) {
1436 Mutex::Autolock _l(mLock);
1437
1438 mIsConnected = connected;
1439 //if plug out, need reinit
1440 if (!connected)
1441 mHdrCapabilities.init = false;
1442}
1443
1444int32_t PhysicalDevice::getLineValue(const char *lineStr, const char *magicStr) {
1445 int len = 0;
1446 char value[100] = {0};
1447 char *pos = NULL;
1448
1449 if ((NULL == lineStr) || (NULL == magicStr)) {
1450 ETRACE("line string: %s, magic string: %s\n", lineStr, magicStr);
1451 return 0;
1452 }
1453
1454 if (NULL != (pos = strstr(lineStr, magicStr))) {
1455 pos = pos + strlen(magicStr);
1456 char* start = pos;
1457 while (*start != '\n' && (strlen(start) > 0))
1458 start++;
1459
1460 len = start - pos;
1461 strncpy(value, pos, len);
1462 value[len] = '\0';
1463 return atoi(value);
1464 }
1465
1466 return 0;
1467}
1468
1469/*
1470cat /sys/class/amhdmitx/amhdmitx0/hdr_cap
1471Supported EOTF:
1472 Traditional SDR: 1
1473 Traditional HDR: 0
1474 SMPTE ST 2084: 1
1475 Future EOTF: 0
1476Supported SMD type1: 1
1477Luminance Data
1478 Max: 0
1479 Avg: 0
1480 Min: 0
1481cat /sys/class/amhdmitx/amhdmitx0/dv_cap
1482DolbyVision1 RX support list:
1483 2160p30hz: 1
1484 global dimming
1485 colorimetry
1486 IEEEOUI: 0x00d046
1487 DM Ver: 1
1488*/
1489int32_t PhysicalDevice::parseHdrCapabilities() {
1490 //DolbyVision1
1491 const char *DV_PATH = "/sys/class/amhdmitx/amhdmitx0/dv_cap";
1492 //HDR
1493 const char *HDR_PATH = "/sys/class/amhdmitx/amhdmitx0/hdr_cap";
1494
1495 char buf[1024+1] = {0};
1496 char* pos = buf;
1497 int fd, len;
1498
1499 memset(&mHdrCapabilities, 0, sizeof(hdr_capabilities_t));
1500 if ((fd = open(DV_PATH, O_RDONLY)) < 0) {
1501 ETRACE("open %s fail.", DV_PATH);
1502 goto exit;
1503 }
1504
1505 len = read(fd, buf, 1024);
1506 if (len < 0) {
1507 ETRACE("read error: %s, %s\n", DV_PATH, strerror(errno));
1508 goto exit;
1509 }
1510 close(fd);
1511
1512 if ((NULL != strstr(pos, "2160p30hz")) || (NULL != strstr(pos, "2160p60hz")))
1513 mHdrCapabilities.dvSupport = true;
1514 //dobly version parse end
1515
1516 memset(buf, 0, 1024);
1517 if ((fd = open(HDR_PATH, O_RDONLY)) < 0) {
1518 ETRACE("open %s fail.", HDR_PATH);
1519 goto exit;
1520 }
1521
1522 len = read(fd, buf, 1024);
1523 if (len < 0) {
1524 ETRACE("read error: %s, %s\n", HDR_PATH, strerror(errno));
1525 goto exit;
1526 }
1527
1528 pos = strstr(pos, "SMPTE ST 2084: ");
1529 if ((NULL != pos) && ('1' == *(pos + strlen("SMPTE ST 2084: ")))) {
1530 mHdrCapabilities.hdrSupport = true;
1531
1532 mHdrCapabilities.maxLuminance = getLineValue(pos, "Max: ");
1533 mHdrCapabilities.avgLuminance = getLineValue(pos, "Avg: ");
1534 mHdrCapabilities.minLuminance = getLineValue(pos, "Min: ");
1535 }
1536
1537 ITRACE("dolby version support:%d, hdr support:%d max:%d, avg:%d, min:%d\n",
1538 mHdrCapabilities.dvSupport?1:0, mHdrCapabilities.hdrSupport?1:0, mHdrCapabilities.maxLuminance, mHdrCapabilities.avgLuminance, mHdrCapabilities.minLuminance);
1539exit:
1540 close(fd);
1541 return HWC2_ERROR_NONE;
1542}
1543
1544void PhysicalDevice::dump(Dump& d) {
1545 Mutex::Autolock _l(mLock);
1546 d.append("-------------------------------------------------------------"
1547 "----------------------------------------------------------------\n");
1548 d.append("Device Name: %s (%s)\n", mName,
1549 mIsConnected ? "connected" : "disconnected");
1550 mDisplayHdmi->dump(d);
1551
1552 // dump layer list
1553 d.append(" Layers state:\n");
1554 d.append(" numLayers=%zu\n", mHwcLayers.size());
1555 // d.append(" numChangedTypeLayers=%zu\n", mHwcLayersChangeType.size());
1556 // d.append(" numChangedRequestLayers=%zu\n", mHwcLayersChangeRequest.size());
1557
1558 if (mHwcLayers.size() > 0) {
1559 d.append(
1560 " type | handle | zorder | ds | alpa | tr | blnd |"
1561 " source crop (l,t,r,b) | frame \n"
1562 " -------------+--------------+------------+----+------+----+------+"
1563 "--------------------------------+------------------------\n");
1564 for (uint32_t i=0; i<mHwcLayers.size(); i++) {
1565 hwc2_layer_t layerId = mHwcLayers.keyAt(i);
1566 HwcLayer *layer = mHwcLayers.valueAt(i);
1567 if (layer) layer->dump(d);
1568 }
1569 }
1570
1571 // HDR info
1572 d.append(" HDR Capabilities:\n");
1573 d.append(" DolbyVision1=%zu\n", mHdrCapabilities.dvSupport?1:0);
1574 d.append(" HDR10=%zu, maxLuminance=%zu, avgLuminance=%zu, minLuminance=%zu\n",
1575 mHdrCapabilities.hdrSupport?1:0, mHdrCapabilities.maxLuminance, mHdrCapabilities.avgLuminance, mHdrCapabilities.minLuminance);
1576}
1577
1578} // namespace amlogic
1579} // namespace android
1580