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 #include <HwcTrace.h>
17 #include <Drm.h>
18 #include <HwcLayerList.h>
19 #include <Hwcomposer.h>
20 #include <GraphicBuffer.h>
21 #include <IDisplayDevice.h>
22 #include <PlaneCapabilities.h>
23 #include <DisplayQuery.h>
24
25 namespace android {
26 namespace intel {
27
HwcLayerList(hwc_display_contents_1_t * list,int disp)28 HwcLayerList::HwcLayerList(hwc_display_contents_1_t *list, int disp)
29 : mList(list),
30 mLayerCount(0),
31 mLayers(),
32 mFBLayers(),
33 mStaticLayersIndex(),
34 mSpriteCandidates(),
35 mOverlayCandidates(),
36 mZOrderConfig(),
37 mFrameBufferTarget(NULL),
38 mDisplayIndex(disp),
39 mLayerSize(0)
40 {
41 initialize();
42 }
43
~HwcLayerList()44 HwcLayerList::~HwcLayerList()
45 {
46 deinitialize();
47 }
48
checkSupported(int planeType,HwcLayer * hwcLayer)49 bool HwcLayerList::checkSupported(int planeType, HwcLayer *hwcLayer)
50 {
51 bool valid = false;
52 hwc_layer_1_t& layer = *(hwcLayer->getLayer());
53
54 // if layer was forced to use FB
55 if (hwcLayer->getType() == HwcLayer::LAYER_FORCE_FB) {
56 VTRACE("layer was forced to use HWC_FRAMEBUFFER");
57 return false;
58 }
59
60 // check layer flags
61 if (layer.flags & HWC_SKIP_LAYER) {
62 VTRACE("plane type %d: (skip layer flag was set)", planeType);
63 return false;
64 }
65
66 if (layer.handle == 0) {
67 WTRACE("invalid buffer handle");
68 return false;
69 }
70
71 // check usage
72 if (!hwcLayer->getUsage() & GRALLOC_USAGE_HW_COMPOSER) {
73 WTRACE("not a composer layer");
74 return false;
75 }
76
77 // check layer transform
78 valid = PlaneCapabilities::isTransformSupported(planeType, hwcLayer);
79 if (!valid) {
80 VTRACE("plane type %d: (bad transform)", planeType);
81 return false;
82 }
83
84 // check buffer format
85 valid = PlaneCapabilities::isFormatSupported(planeType, hwcLayer);
86 if (!valid) {
87 VTRACE("plane type %d: (bad buffer format)", planeType);
88 return false;
89 }
90
91 // check buffer size
92 valid = PlaneCapabilities::isSizeSupported(planeType, hwcLayer);
93 if (!valid) {
94 VTRACE("plane type %d: (bad buffer size)", planeType);
95 return false;
96 }
97
98 // check layer blending
99 valid = PlaneCapabilities::isBlendingSupported(planeType, hwcLayer);
100 if (!valid) {
101 VTRACE("plane type %d: (bad blending)", planeType);
102 return false;
103 }
104
105 // check layer scaling
106 valid = PlaneCapabilities::isScalingSupported(planeType, hwcLayer);
107 if (!valid) {
108 VTRACE("plane type %d: (bad scaling)", planeType);
109 return false;
110 }
111
112 // TODO: check visible region?
113 return true;
114 }
115
checkCursorSupported(HwcLayer * hwcLayer)116 bool HwcLayerList::checkCursorSupported(HwcLayer *hwcLayer)
117 {
118 hwc_layer_1_t& layer = *(hwcLayer->getLayer());
119
120 // if layer was forced to use FB
121 if (hwcLayer->getType() == HwcLayer::LAYER_FORCE_FB) {
122 VTRACE("layer was forced to use HWC_FRAMEBUFFER");
123 return false;
124 }
125
126 // check layer flags
127 if (layer.flags & HWC_SKIP_LAYER) {
128 VTRACE("skip layer flag was set");
129 return false;
130 }
131
132 if (!(layer.flags & HWC_IS_CURSOR_LAYER)) {
133 VTRACE("not a cursor layer");
134 return false;
135 }
136
137 if (hwcLayer->getIndex() != mLayerCount - 2) {
138 WTRACE("cursor layer is not on top of zorder");
139 return false;
140 }
141
142 if (layer.handle == 0) {
143 WTRACE("invalid buffer handle");
144 return false;
145 }
146
147 // check usage
148 if (!(hwcLayer->getUsage() & GRALLOC_USAGE_HW_COMPOSER)) {
149 WTRACE("not a composer layer");
150 return false;
151 }
152
153 uint32_t format = hwcLayer->getFormat();
154 if (format != HAL_PIXEL_FORMAT_BGRA_8888 &&
155 format != HAL_PIXEL_FORMAT_RGBA_8888) {
156 WTRACE("unexpected color format %u for cursor", format);
157 return false;
158 }
159
160 uint32_t trans = hwcLayer->getLayer()->transform;
161 if (trans != 0) {
162 WTRACE("unexpected transform %u for cursor", trans);
163 return false;
164 }
165
166 hwc_frect_t& src = hwcLayer->getLayer()->sourceCropf;
167 hwc_rect_t& dest = hwcLayer->getLayer()->displayFrame;
168 int srcW = (int)src.right - (int)src.left;
169 int srcH = (int)src.bottom - (int)src.top;
170 int dstW = dest.right - dest.left;
171 int dstH = dest.bottom - dest.top;
172 if (srcW != dstW || srcH != dstH) {
173 WTRACE("unexpected scaling for cursor: %dx%d => %dx%d",
174 srcW, srcH, dstW, dstH);
175 //return false;
176 }
177
178 if (srcW > 256 || srcH > 256) {
179 WTRACE("unexpected size %dx%d for cursor", srcW, srcH);
180 return false;
181 }
182
183 BufferManager *bm = Hwcomposer::getInstance().getBufferManager();
184 if (bm) {
185 DataBuffer *buffer = bm->lockDataBuffer(hwcLayer->getHandle());
186 if (buffer) {
187 uint32_t w = buffer->getWidth();
188 uint32_t h = buffer->getHeight();
189
190 if ((w != 64 || h != 64) &&
191 (w != 128 || h != 128) &&
192 (w != 256 || h != 256)) {
193 bm->unlockDataBuffer(buffer);
194 return false;
195 }
196 }
197 bm->unlockDataBuffer(buffer);
198 }
199
200 return true;
201 }
202
initialize()203 bool HwcLayerList::initialize()
204 {
205 if (!mList || mList->numHwLayers == 0) {
206 ETRACE("invalid hwc list");
207 return false;
208 }
209
210 mLayerCount = (int)mList->numHwLayers;
211 mLayers.setCapacity(mLayerCount);
212 mFBLayers.setCapacity(mLayerCount);
213 mSpriteCandidates.setCapacity(mLayerCount);
214 mOverlayCandidates.setCapacity(mLayerCount);
215 mCursorCandidates.setCapacity(mLayerCount);
216 mZOrderConfig.setCapacity(mLayerCount);
217 Hwcomposer& hwc = Hwcomposer::getInstance();
218
219 for (int i = 0; i < mLayerCount; i++) {
220 hwc_layer_1_t *layer = &mList->hwLayers[i];
221 if (!layer) {
222 DEINIT_AND_RETURN_FALSE("layer %d is null", i);
223 }
224
225 HwcLayer *hwcLayer = new HwcLayer(i, layer);
226 if (!hwcLayer) {
227 DEINIT_AND_RETURN_FALSE("failed to allocate hwc layer %d", i);
228 }
229
230 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET) {
231 hwcLayer->setType(HwcLayer::LAYER_FRAMEBUFFER_TARGET);
232 mFrameBufferTarget = hwcLayer;
233 } else if (layer->compositionType == HWC_OVERLAY){
234 // skipped layer, filtered by Display Analyzer
235 hwcLayer->setType(HwcLayer::LAYER_SKIPPED);
236 } else if (layer->compositionType == HWC_FORCE_FRAMEBUFFER) {
237 layer->compositionType = HWC_FRAMEBUFFER;
238 hwcLayer->setType(HwcLayer::LAYER_FORCE_FB);
239 // add layer to FB layer list for zorder check during plane assignment
240 mFBLayers.add(hwcLayer);
241 } else if (layer->compositionType == HWC_FRAMEBUFFER) {
242 // by default use GPU composition
243 hwcLayer->setType(HwcLayer::LAYER_FB);
244 mFBLayers.add(hwcLayer);
245 if (checkCursorSupported(hwcLayer)) {
246 mCursorCandidates.add(hwcLayer);
247 } else if (checkSupported(DisplayPlane::PLANE_SPRITE, hwcLayer)) {
248 mSpriteCandidates.add(hwcLayer);
249 } else if (hwc.getDisplayAnalyzer()->isOverlayAllowed() &&
250 checkSupported(DisplayPlane::PLANE_OVERLAY, hwcLayer)) {
251 mOverlayCandidates.add(hwcLayer);
252 } else {
253 // noncandidate layer
254 }
255 } else if (layer->compositionType == HWC_SIDEBAND){
256 hwcLayer->setType(HwcLayer::LAYER_SIDEBAND);
257 } else {
258 DEINIT_AND_RETURN_FALSE("invalid composition type %d", layer->compositionType);
259 }
260 // add layer to layer list
261 mLayers.add(hwcLayer);
262 }
263
264 if (mFrameBufferTarget == NULL) {
265 ETRACE("no frame buffer target?");
266 return false;
267 }
268
269 // If has layer besides of FB_Target, but no FBLayers, skip plane allocation
270 // Note: There is case that SF passes down a layerlist with only FB_Target
271 // layer; we need to have this FB_Target to be flipped as well, otherwise it
272 // will have the buffer queue blocked. (The buffer hold by driver cannot be
273 // released if new buffers' flip is skipped).
274 if ((mFBLayers.size() == 0) && (mLayers.size() > 1)) {
275 VTRACE("no FB layers, skip plane allocation");
276 return true;
277 }
278
279 allocatePlanes();
280
281 //dump();
282 return true;
283 }
284
deinitialize()285 void HwcLayerList::deinitialize()
286 {
287 if (mLayerCount == 0) {
288 return;
289 }
290
291 DisplayPlaneManager *planeManager = Hwcomposer::getInstance().getPlaneManager();
292 for (int i = 0; i < mLayerCount; i++) {
293 HwcLayer *hwcLayer = mLayers.itemAt(i);
294 if (hwcLayer) {
295 DisplayPlane *plane = hwcLayer->detachPlane();
296 if (plane) {
297 planeManager->reclaimPlane(mDisplayIndex, *plane);
298 }
299 }
300 delete hwcLayer;
301 }
302
303 mLayers.clear();
304 mFBLayers.clear();
305 mOverlayCandidates.clear();
306 mSpriteCandidates.clear();
307 mCursorCandidates.clear();
308 mZOrderConfig.clear();
309 mFrameBufferTarget = NULL;
310 mLayerCount = 0;
311 }
312
313
allocatePlanes()314 bool HwcLayerList::allocatePlanes()
315 {
316 return assignCursorPlanes();
317 }
318
assignCursorPlanes()319 bool HwcLayerList::assignCursorPlanes()
320 {
321 int cursorCandidates = (int)mCursorCandidates.size();
322 if (cursorCandidates == 0) {
323 return assignOverlayPlanes();
324 }
325
326 DisplayPlaneManager *planeManager = Hwcomposer::getInstance().getPlaneManager();
327 int planeNumber = planeManager->getFreePlanes(mDisplayIndex, DisplayPlane::PLANE_CURSOR);
328 if (planeNumber == 0) {
329 DTRACE("no cursor plane available. candidates %d", cursorCandidates);
330 return assignOverlayPlanes();
331 }
332
333 if (planeNumber > cursorCandidates) {
334 // assuming all cursor planes have the same capabilities, just
335 // need up to number of candidates for plane assignment
336 planeNumber = cursorCandidates;
337 }
338
339 for (int i = planeNumber; i >= 0; i--) {
340 // assign as many cursor planes as possible
341 if (assignCursorPlanes(0, i)) {
342 return true;
343 }
344 if (mZOrderConfig.size() != 0) {
345 ETRACE("ZOrder config is not cleaned up!");
346 }
347 }
348 return false;
349 }
350
assignCursorPlanes(int index,int planeNumber)351 bool HwcLayerList::assignCursorPlanes(int index, int planeNumber)
352 {
353 // index indicates position in mCursorCandidates to start plane assignment
354 if (planeNumber == 0) {
355 return assignOverlayPlanes();
356 }
357
358 int cursorCandidates = (int)mCursorCandidates.size();
359 for (int i = index; i <= cursorCandidates - planeNumber; i++) {
360 ZOrderLayer *zlayer = addZOrderLayer(DisplayPlane::PLANE_CURSOR, mCursorCandidates[i]);
361 if (assignCursorPlanes(i + 1, planeNumber - 1)) {
362 return true;
363 }
364 removeZOrderLayer(zlayer);
365 }
366 return false;
367 }
368
assignOverlayPlanes()369 bool HwcLayerList::assignOverlayPlanes()
370 {
371 int overlayCandidates = (int)mOverlayCandidates.size();
372 if (overlayCandidates == 0) {
373 return assignSpritePlanes();
374 }
375
376 DisplayPlaneManager *planeManager = Hwcomposer::getInstance().getPlaneManager();
377 int planeNumber = planeManager->getFreePlanes(mDisplayIndex, DisplayPlane::PLANE_OVERLAY);
378 if (planeNumber == 0) {
379 DTRACE("no overlay plane available. candidates %d", overlayCandidates);
380 return assignSpritePlanes();
381 }
382
383 if (planeNumber > overlayCandidates) {
384 // assuming all overlay planes have the same capabilities, just
385 // need up to number of candidates for plane assignment
386 planeNumber = overlayCandidates;
387 }
388
389 for (int i = planeNumber; i >= 0; i--) {
390 // assign as many overlay planes as possible
391 if (assignOverlayPlanes(0, i)) {
392 return true;
393 }
394 if (mZOrderConfig.size() != 0) {
395 ETRACE("ZOrder config is not cleaned up!");
396 }
397 }
398 return false;
399 }
400
401
assignOverlayPlanes(int index,int planeNumber)402 bool HwcLayerList::assignOverlayPlanes(int index, int planeNumber)
403 {
404 // index indicates position in mOverlayCandidates to start plane assignment
405 if (planeNumber == 0) {
406 return assignSpritePlanes();
407 }
408
409 int overlayCandidates = (int)mOverlayCandidates.size();
410 for (int i = index; i <= overlayCandidates - planeNumber; i++) {
411 ZOrderLayer *zlayer = addZOrderLayer(DisplayPlane::PLANE_OVERLAY, mOverlayCandidates[i]);
412 if (assignOverlayPlanes(i + 1, planeNumber - 1)) {
413 return true;
414 }
415 removeZOrderLayer(zlayer);
416 }
417 return false;
418 }
419
assignSpritePlanes()420 bool HwcLayerList::assignSpritePlanes()
421 {
422 int spriteCandidates = (int)mSpriteCandidates.size();
423 if (spriteCandidates == 0) {
424 return assignPrimaryPlane();
425 }
426
427 // number does not include primary plane
428 DisplayPlaneManager *planeManager = Hwcomposer::getInstance().getPlaneManager();
429 int planeNumber = planeManager->getFreePlanes(mDisplayIndex, DisplayPlane::PLANE_SPRITE);
430 if (planeNumber == 0) {
431 VTRACE("no sprite plane available, candidates %d", spriteCandidates);
432 return assignPrimaryPlane();
433 }
434
435 if (planeNumber > spriteCandidates) {
436 // assuming all sprite planes have the same capabilities, just
437 // need up to number of candidates for plane assignment
438 planeNumber = spriteCandidates;
439 }
440
441 for (int i = planeNumber; i >= 0; i--) {
442 // assign as many sprite planes as possible
443 if (assignSpritePlanes(0, i)) {
444 return true;
445 }
446
447 if (mOverlayCandidates.size() == 0 && mZOrderConfig.size() != 0) {
448 ETRACE("ZOrder config is not cleaned up!");
449 }
450 }
451 return false;
452 }
453
454
assignSpritePlanes(int index,int planeNumber)455 bool HwcLayerList::assignSpritePlanes(int index, int planeNumber)
456 {
457 if (planeNumber == 0) {
458 return assignPrimaryPlane();
459 }
460
461 int spriteCandidates = (int)mSpriteCandidates.size();
462 for (int i = index; i <= spriteCandidates - planeNumber; i++) {
463 ZOrderLayer *zlayer = addZOrderLayer(DisplayPlane::PLANE_SPRITE, mSpriteCandidates[i]);
464 if (assignSpritePlanes(i + 1, planeNumber - 1)) {
465 return true;
466 }
467 removeZOrderLayer(zlayer);
468 }
469 return false;
470 }
471
assignPrimaryPlane()472 bool HwcLayerList::assignPrimaryPlane()
473 {
474 // find a sprit layer that is not candidate but has lower priority than candidates.
475 HwcLayer *spriteLayer = NULL;
476 for (int i = (int)mSpriteCandidates.size() - 1; i >= 0; i--) {
477 if (mSpriteCandidates[i]->mPlaneCandidate)
478 break;
479
480 spriteLayer = mSpriteCandidates[i];
481 }
482
483 int candidates = (int)mZOrderConfig.size();
484 int layers = (int)mFBLayers.size();
485 bool ok = false;
486
487 if (candidates == layers - 1 && spriteLayer != NULL) {
488 // primary plane is configured as sprite, all sprite candidates are offloaded to display planes
489 ok = assignPrimaryPlaneHelper(spriteLayer);
490 if (!ok) {
491 VTRACE("failed to use primary as sprite plane");
492 }
493 } else if (candidates == 0) {
494 // none assigned, use primary plane for frame buffer target and set zorder to 0
495 ok = assignPrimaryPlaneHelper(mFrameBufferTarget, 0);
496 if (!ok) {
497 ETRACE("failed to compose all layers to primary plane, should never happen");
498 }
499 } else if (candidates == layers) {
500 // all assigned, primary plane may be used during ZOrder config.
501 ok = attachPlanes();
502 if (!ok) {
503 VTRACE("failed to assign layers without primary");
504 }
505 } else {
506 // check if the remaining planes can be composed to frame buffer target (FBT)
507 // look up a legitimate Z order position to place FBT.
508 for (int i = 0; i < layers && !ok; i++) {
509 if (mFBLayers[i]->mPlaneCandidate) {
510 continue;
511 }
512 if (useAsFrameBufferTarget(mFBLayers[i])) {
513 ok = assignPrimaryPlaneHelper(mFrameBufferTarget, mFBLayers[i]->getZOrder());
514 if (!ok) {
515 VTRACE("failed to use zorder %d for frame buffer target",
516 mFBLayers[i]->getZOrder());
517 }
518 }
519 }
520 if (!ok) {
521 VTRACE("no possible zorder for frame buffer target");
522 }
523
524 }
525 return ok;
526 }
527
assignPrimaryPlaneHelper(HwcLayer * hwcLayer,int zorder)528 bool HwcLayerList::assignPrimaryPlaneHelper(HwcLayer *hwcLayer, int zorder)
529 {
530 ZOrderLayer *zlayer = addZOrderLayer(DisplayPlane::PLANE_PRIMARY, hwcLayer, zorder);
531 bool ok = attachPlanes();
532 if (!ok) {
533 removeZOrderLayer(zlayer);
534 }
535 return ok;
536 }
537
attachPlanes()538 bool HwcLayerList::attachPlanes()
539 {
540 DisplayPlaneManager *planeManager = Hwcomposer::getInstance().getPlaneManager();
541 if (!planeManager->isValidZOrder(mDisplayIndex, mZOrderConfig)) {
542 VTRACE("invalid z order, size of config %d", mZOrderConfig.size());
543 return false;
544 }
545
546 if (!planeManager->assignPlanes(mDisplayIndex, mZOrderConfig)) {
547 WTRACE("failed to assign planes");
548 return false;
549 }
550
551 VTRACE("============= plane assignment===================");
552 for (int i = 0; i < (int)mZOrderConfig.size(); i++) {
553 ZOrderLayer *zlayer = mZOrderConfig.itemAt(i);
554 if (zlayer->plane == NULL || zlayer->hwcLayer == NULL) {
555 ETRACE("invalid ZOrderLayer, should never happen!!");
556 return false;
557 }
558
559 zlayer->plane->setZOrder(i);
560
561 if (zlayer->plane->getType() == DisplayPlane::PLANE_CURSOR) {
562 zlayer->hwcLayer->setType(HwcLayer::LAYER_CURSOR_OVERLAY);
563 mFBLayers.remove(zlayer->hwcLayer);
564 } else if (zlayer->hwcLayer != mFrameBufferTarget) {
565 zlayer->hwcLayer->setType(HwcLayer::LAYER_OVERLAY);
566 // update FB layers for smart composition
567 mFBLayers.remove(zlayer->hwcLayer);
568 }
569
570 zlayer->hwcLayer->attachPlane(zlayer->plane, mDisplayIndex);
571
572 VTRACE("total %d, layer %d, type %d, index %d, zorder %d",
573 mLayerCount - 1,
574 zlayer->hwcLayer->getIndex(),
575 zlayer->plane->getType(),
576 zlayer->plane->getIndex(),
577 zlayer->zorder);
578
579 delete zlayer;
580 }
581
582 mZOrderConfig.clear();
583 return true;
584 }
585
useAsFrameBufferTarget(HwcLayer * target)586 bool HwcLayerList::useAsFrameBufferTarget(HwcLayer *target)
587 {
588 // check if zorder of target can be used as zorder of frame buffer target
589 // eligible only when all noncandidate layers can be merged to the target layer:
590 // 1) noncandidate layer and candidate layer below the target layer can't overlap
591 // if candidate layer is on top of non candidate layer, as "noncandidate layer" needs
592 // to be moved up to target layer in z order;
593 // 2) noncandidate layer and candidate layers above the target layer can't overlap
594 // if candidate layer is below noncandidate layer, as "noncandidate layer" needs
595 // to be moved down to target layer in z order.
596
597 int targetLayerIndex = target->getIndex();
598
599 // check candidate and noncandidate layers below this candidate does not overlap
600 for (int below = 0; below < targetLayerIndex; below++) {
601 if (mFBLayers[below]->mPlaneCandidate) {
602 continue;
603 } else {
604 // check candidate layer above this noncandidate layer does not overlap
605 for (int above = below + 1; above < targetLayerIndex; above++) {
606 if (mFBLayers[above]->mPlaneCandidate == false) {
607 continue;
608 }
609 if (hasIntersection(mFBLayers[above], mFBLayers[below])) {
610 return false;
611 }
612 }
613 }
614 }
615
616 // check candidate and noncandidate layers above this candidate does not overlap
617 for (int above = targetLayerIndex + 1; above < mFBLayers.size(); above++) {
618 if (mFBLayers[above]->mPlaneCandidate) {
619 continue;
620 } else {
621 // check candidate layer below this noncandidate layer does not overlap
622 for (int below = targetLayerIndex + 1; below < above; below++) {
623 if (mFBLayers[below]->mPlaneCandidate == false) {
624 continue;
625 }
626 if (hasIntersection(mFBLayers[above], mFBLayers[below])) {
627 return false;
628 }
629 }
630 }
631 }
632
633 return true;
634 }
635
hasIntersection(HwcLayer * la,HwcLayer * lb)636 bool HwcLayerList::hasIntersection(HwcLayer *la, HwcLayer *lb)
637 {
638 hwc_layer_1_t *a = la->getLayer();
639 hwc_layer_1_t *b = lb->getLayer();
640 hwc_rect_t *aRect = &a->displayFrame;
641 hwc_rect_t *bRect = &b->displayFrame;
642
643 if (bRect->right <= aRect->left ||
644 bRect->left >= aRect->right ||
645 bRect->top >= aRect->bottom ||
646 bRect->bottom <= aRect->top)
647 return false;
648
649 return true;
650 }
651
addZOrderLayer(int type,HwcLayer * hwcLayer,int zorder)652 ZOrderLayer* HwcLayerList::addZOrderLayer(int type, HwcLayer *hwcLayer, int zorder)
653 {
654 ZOrderLayer *layer = new ZOrderLayer;
655 layer->planeType = type;
656 layer->hwcLayer = hwcLayer;
657 layer->zorder = (zorder != -1) ? zorder : hwcLayer->getZOrder();
658 layer->plane = NULL;
659
660 if (hwcLayer->mPlaneCandidate) {
661 ETRACE("plane is candidate!, order = %d", zorder);
662 }
663
664 hwcLayer->mPlaneCandidate = true;
665
666 if ((int)mZOrderConfig.indexOf(layer) >= 0) {
667 ETRACE("layer exists!");
668 }
669
670 mZOrderConfig.add(layer);
671 return layer;
672 }
673
removeZOrderLayer(ZOrderLayer * layer)674 void HwcLayerList::removeZOrderLayer(ZOrderLayer *layer)
675 {
676 if ((int)mZOrderConfig.indexOf(layer) < 0) {
677 ETRACE("layer does not exist!");
678 }
679
680 mZOrderConfig.remove(layer);
681
682 if (layer->hwcLayer->mPlaneCandidate == false) {
683 ETRACE("plane is not candidate!, order %d", layer->zorder);
684 }
685 layer->hwcLayer->mPlaneCandidate = false;
686 delete layer;
687 }
688
addStaticLayerSize(HwcLayer * hwcLayer)689 void HwcLayerList::addStaticLayerSize(HwcLayer *hwcLayer)
690 {
691 // Calculate static layer size to avoid only composition navigation bar
692 // and status bar etc.
693 hwc_layer_1_t *a = hwcLayer->getLayer();
694 hwc_rect_t *Rect = &a->displayFrame;
695
696 mLayerSize = mLayerSize + ((Rect->right - Rect->left) * (Rect->bottom - Rect->top));
697 }
698
checkStaticLayerSize()699 bool HwcLayerList::checkStaticLayerSize()
700 {
701 // Check static layer size if over threshold: half display size
702 bool ret = false;
703 int width = 0;
704 int height = 0;
705 drmModeModeInfo mode;
706 Drm *drm = Hwcomposer::getInstance().getDrm();
707 drm->getModeInfo(mDisplayIndex, mode);
708 width = mode.hdisplay;
709 height = mode.vdisplay;
710
711 if (mLayerSize > (width * height/2))
712 ret = true;
713
714 return ret;
715 }
716
setupSmartComposition()717 void HwcLayerList::setupSmartComposition()
718 {
719 uint32_t compositionType = HWC_OVERLAY;
720 HwcLayer *hwcLayer = NULL;
721
722 // setup smart composition only there's no update on all FB layers
723 for (size_t i = 0; i < mFBLayers.size(); i++) {
724 hwcLayer = mFBLayers.itemAt(i);
725 if (hwcLayer->isUpdated() ||
726 hwcLayer->getStaticCount() == LAYER_STATIC_THRESHOLD) {
727 compositionType = HWC_FRAMEBUFFER;
728 }
729 }
730
731 VTRACE("smart composition enabled %s",
732 (compositionType == HWC_OVERLAY) ? "TRUE" : "FALSE");
733 for (size_t i = 0; i < mFBLayers.size(); i++) {
734 hwcLayer = mFBLayers.itemAt(i);
735 switch (hwcLayer->getType()) {
736 case HwcLayer::LAYER_FB:
737 case HwcLayer::LAYER_FORCE_FB:
738 hwcLayer->setCompositionType(compositionType);
739 break;
740 default:
741 ETRACE("Invalid layer type %d", hwcLayer->getType());
742 break;
743 }
744 }
745 }
746
setupSmartComposition2()747 bool HwcLayerList::setupSmartComposition2()
748 {
749 bool ret = false;
750 HwcLayer *hwcLayer = NULL;
751 int layerIndex = 0;
752 int i = 0;
753
754 if (mList->flags & HWC_GEOMETRY_CHANGED) {
755 // clear static layers vector once geometry changed
756 mStaticLayersIndex.setCapacity(mLayerCount);
757 mStaticLayersIndex.clear();
758 return ret;
759 }
760
761 if (mStaticLayersIndex.size() > 0) {
762 // exit criteria: once either static layer has update
763 for (i = 0; i < mStaticLayersIndex.size(); i++) {
764 layerIndex = mStaticLayersIndex.itemAt(i);
765 hwcLayer = mLayers.itemAt(layerIndex);
766
767 if (hwcLayer->isUpdated()) {
768 ret = true;
769 }
770 }
771
772 if (ret == true) {
773 for (i = 0; i < mStaticLayersIndex.size(); i++) {
774 layerIndex = mStaticLayersIndex.itemAt(i);
775 hwcLayer = mLayers.itemAt(layerIndex);
776
777 hwcLayer->setCompositionType(HWC_FRAMEBUFFER);
778 }
779
780 DTRACE("Exit Smart Composition2 !");
781 mLayerSize = 0;
782 mStaticLayersIndex.clear();
783 }
784 } else {
785 // entry criteria: hwc layers has no update
786 if (mFBLayers.size() == 0) {
787 for (i = 0; i < mLayerCount - 1; i++) {
788 hwcLayer = mLayers.itemAt(i);
789 if (hwcLayer->getPlane() &&
790 hwcLayer->getCompositionType() == HWC_OVERLAY &&
791 hwcLayer->getStaticCount() >= LAYER_STATIC_THRESHOLD) {
792 mStaticLayersIndex.add(i);
793 }
794 }
795
796 // check if all static layers in sequence
797 // if all in sequence, set FORCE_FB for static layers
798 // TODO: optimization here
799 // 1. If two connected, can trigger smart composition2
800 // 2. Caculate layer size to see if it saves more bandwidth
801 // 3. Dynamically check and add new static layers
802 int staticLayerCount = mStaticLayersIndex.size();
803
804 if (staticLayerCount > 1 && staticLayerCount < mLayerCount-1) {
805 layerIndex = mStaticLayersIndex.itemAt(0);
806 hwcLayer = mLayers.itemAt(layerIndex);
807 mLayerSize = 0;
808 addStaticLayerSize(hwcLayer);
809 int preIndex = hwcLayer->getIndex();
810
811 for (i = 1; i < staticLayerCount; i++) {
812 layerIndex = mStaticLayersIndex.itemAt(i);
813 hwcLayer = mLayers.itemAt(layerIndex);
814 int index = hwcLayer->getIndex();
815
816 if (index == preIndex + 1) {
817 addStaticLayerSize(hwcLayer);
818 preIndex = index;
819 } else
820 break;
821 }
822
823 if ((i == staticLayerCount) && checkStaticLayerSize()) {
824 for (i =0; i < staticLayerCount; i++) {
825 layerIndex = mStaticLayersIndex.itemAt(i);
826 hwcLayer = mLayers.itemAt(layerIndex);
827 hwcLayer->setCompositionType(HWC_FORCE_FRAMEBUFFER);
828 }
829 DTRACE("In Smart Composition2 !");
830 ret = true;
831 } else {
832 mLayerSize = 0;
833 }
834 }
835
836 if (!ret)
837 mStaticLayersIndex.clear();
838 }
839 }
840
841 // return ture to trigger remap layers with HW plane
842 return ret;
843 }
844
845 #if 1 // support overlay fallback to GLES
846
update(hwc_display_contents_1_t * list)847 bool HwcLayerList::update(hwc_display_contents_1_t *list)
848 {
849 bool ret;
850
851 // basic check to make sure the consistance
852 if (!list) {
853 ETRACE("null layer list");
854 return false;
855 }
856
857 if ((int)list->numHwLayers != mLayerCount) {
858 ETRACE("layer count doesn't match (%zd, %d)", list->numHwLayers, mLayerCount);
859 return false;
860 }
861
862 // update list
863 mList = list;
864
865 bool ok = true;
866 // update all layers, call each layer's update()
867 for (int i = 0; i < mLayerCount; i++) {
868 HwcLayer *hwcLayer = mLayers.itemAt(i);
869 if (!hwcLayer) {
870 ETRACE("no HWC layer for layer %d", i);
871 continue;
872 }
873
874 if (!hwcLayer->update(&list->hwLayers[i])) {
875 ok = false;
876 hwcLayer->setCompositionType(HWC_FORCE_FRAMEBUFFER);
877 }
878 }
879
880 if (!ok || setupSmartComposition2()) {
881 ITRACE("overlay fallback to GLES. flags: %#x", list->flags);
882 for (int i = 0; i < mLayerCount - 1; i++) {
883 HwcLayer *hwcLayer = mLayers.itemAt(i);
884 if (hwcLayer->getPlane() &&
885 (hwcLayer->getCompositionType() == HWC_OVERLAY ||
886 hwcLayer->getCompositionType() == HWC_CURSOR_OVERLAY)) {
887 hwcLayer->setCompositionType(HWC_FRAMEBUFFER);
888 }
889 }
890 mLayers.itemAt(mLayerCount - 1)->setCompositionType(HWC_FRAMEBUFFER_TARGET);
891 deinitialize();
892 mList = list;
893 initialize();
894
895 // update all layers again after plane re-allocation
896 for (int i = 0; i < mLayerCount; i++) {
897 HwcLayer *hwcLayer = mLayers.itemAt(i);
898 if (!hwcLayer) {
899 ETRACE("no HWC layer for layer %d", i);
900 continue;
901 }
902
903 if (!hwcLayer->update(&list->hwLayers[i])) {
904 DTRACE("fallback to GLES update failed on layer[%d]!\n", i);
905 }
906 }
907 }
908
909 setupSmartComposition();
910 return true;
911 }
912
913 #else
914
update(hwc_display_contents_1_t * list)915 bool HwcLayerList::update(hwc_display_contents_1_t *list)
916 {
917 bool ret;
918
919 // basic check to make sure the consistance
920 if (!list) {
921 ETRACE("null layer list");
922 return false;
923 }
924
925 if ((int)list->numHwLayers != mLayerCount) {
926 ETRACE("layer count doesn't match (%d, %d)", list->numHwLayers, mLayerCount);
927 return false;
928 }
929
930 // update list
931 mList = list;
932
933 // update all layers, call each layer's update()
934 for (int i = 0; i < mLayerCount; i++) {
935 HwcLayer *hwcLayer = mLayers.itemAt(i);
936 if (!hwcLayer) {
937 ETRACE("no HWC layer for layer %d", i);
938 continue;
939 }
940
941 hwcLayer->update(&list->hwLayers[i]);
942 }
943
944 setupSmartComposition();
945 return true;
946 }
947
948 #endif
949
getPlane(uint32_t index) const950 DisplayPlane* HwcLayerList::getPlane(uint32_t index) const
951 {
952 HwcLayer *hwcLayer;
953
954 if (index >= mLayers.size()) {
955 ETRACE("invalid layer index %d", index);
956 return 0;
957 }
958
959 hwcLayer = mLayers.itemAt(index);
960 if ((hwcLayer->getType() == HwcLayer::LAYER_FB) ||
961 (hwcLayer->getType() == HwcLayer::LAYER_FORCE_FB) ||
962 (hwcLayer->getType() == HwcLayer::LAYER_SKIPPED)) {
963 return 0;
964 }
965
966 if (hwcLayer->getHandle() == 0) {
967 DTRACE("plane is attached with invalid handle");
968 return 0;
969 }
970
971 return hwcLayer->getPlane();
972 }
973
postFlip()974 void HwcLayerList::postFlip()
975 {
976 for (size_t i = 0; i < mLayers.size(); i++) {
977 HwcLayer *hwcLayer = mLayers.itemAt(i);
978 hwcLayer->postFlip();
979 }
980 }
981
dump(Dump & d)982 void HwcLayerList::dump(Dump& d)
983 {
984 d.append("Layer list: (number of layers %d):\n", mLayers.size());
985 d.append(" LAYER | TYPE | PLANE | INDEX | Z Order \n");
986 d.append("-------+------------------------+----------------------------\n");
987 for (size_t i = 0; i < mLayers.size(); i++) {
988 HwcLayer *hwcLayer = mLayers.itemAt(i);
989 DisplayPlane *plane;
990 long int planeIndex = -1;
991 long int zorder = -1;
992 const char *type = "HWC_FB";
993 const char *planeType = "N/A";
994
995 if (hwcLayer) {
996 switch (hwcLayer->getType()) {
997 case HwcLayer::LAYER_FB:
998 case HwcLayer::LAYER_FORCE_FB:
999 type = "HWC_FB";
1000 break;
1001 case HwcLayer::LAYER_OVERLAY:
1002 case HwcLayer::LAYER_SKIPPED:
1003 type = "HWC_OVERLAY";
1004 break;
1005 case HwcLayer::LAYER_FRAMEBUFFER_TARGET:
1006 type = "HWC_FRAMEBUFFER_TARGET";
1007 break;
1008 case HwcLayer::LAYER_SIDEBAND:
1009 type = "HWC_SIDEBAND";
1010 break;
1011 case HwcLayer::LAYER_CURSOR_OVERLAY:
1012 type = "HWC_CURSOR_OVERLAY";
1013 break;
1014 default:
1015 type = "Unknown";
1016 }
1017
1018 plane = hwcLayer->getPlane();
1019 if (plane) {
1020 planeIndex = plane->getIndex();
1021 zorder = plane->getZOrder();
1022 switch (plane->getType()) {
1023 case DisplayPlane::PLANE_OVERLAY:
1024 planeType = "OVERLAY";
1025 break;
1026 case DisplayPlane::PLANE_SPRITE:
1027 planeType = "SPRITE";
1028 break;
1029 case DisplayPlane::PLANE_PRIMARY:
1030 planeType = "PRIMARY";
1031 break;
1032 case DisplayPlane::PLANE_CURSOR:
1033 planeType = "CURSOR";
1034 break;
1035 default:
1036 planeType = "Unknown";
1037 }
1038 }
1039
1040 d.append(" %2d | %22s | %8s | %3ld | %3ld \n",
1041 i, type, planeType, planeIndex, zorder);
1042 }
1043 }
1044 }
1045
1046
dump()1047 void HwcLayerList::dump()
1048 {
1049 static char const* compositionTypeName[] = {
1050 "GLES",
1051 "HWC",
1052 "BG",
1053 "FBT",
1054 "SB",
1055 "CUR",
1056 "N/A"};
1057
1058 static char const* planeTypeName[] = {
1059 "SPRITE",
1060 "OVERLAY",
1061 "PRIMARY",
1062 "CURSOR",
1063 "UNKNOWN"};
1064
1065 DTRACE(" numHwLayers = %zu, flags = %08x", mList->numHwLayers, mList->flags);
1066
1067 DTRACE(" type | handle | hints | flags | tr | blend | alpha | format | source crop | frame | index | zorder | plane ");
1068 DTRACE("------+----------+-------+-------+----+-------+-------+----------+-----------------------------------+---------------------------+-------+--------+---------");
1069
1070
1071 for (int i = 0 ; i < mLayerCount ; i++) {
1072 const hwc_layer_1_t&l = mList->hwLayers[i];
1073 DisplayPlane *plane = mLayers[i]->getPlane();
1074 int planeIndex = -1;
1075 int zorder = -1;
1076 const char *planeType = "N/A";
1077 if (plane) {
1078 planeIndex = plane->getIndex();
1079 zorder = plane->getZOrder();
1080 planeType = planeTypeName[plane->getType()];
1081 }
1082
1083 DTRACE(
1084 " %4s | %p | %5x | %5x | %2x | %5x | %5x | %8x | [%7.1f,%7.1f,%7.1f,%7.1f] | [%5d,%5d,%5d,%5d] | %5d | %6d | %7s ",
1085 compositionTypeName[l.compositionType],
1086 mLayers[i]->getHandle(), l.hints, l.flags, l.transform, l.blending, l.planeAlpha, mLayers[i]->getFormat(),
1087 l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom,
1088 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1089 planeIndex, zorder, planeType);
1090 }
1091
1092 }
1093
1094
1095 } // namespace intel
1096 } // namespace android
1097