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