summaryrefslogtreecommitdiff
path: root/libhwc2.1/libresource/ExynosResourceManagerModule.cpp
blob: faf52af60d32bbd858830561817db8ca6fb30a05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ExynosResourceManagerModule.h"

#include <cutils/properties.h>

#include <list>
#include <utility>

#include "ExynosLayer.h"

using namespace zuma;

constexpr uint32_t TDM_OVERLAP_MARGIN = 68;

constexpr uint32_t kSramSBWCWidthAlign = 32;
constexpr uint32_t kSramSBWCWidthMargin = kSramSBWCWidthAlign - 1;
constexpr uint32_t kSramSBWCRotWidthAlign = 4;
constexpr uint32_t kSramAFBC8B4BAlign = 8;
constexpr uint32_t kSramAFBC8B4BMargin = kSramAFBC8B4BAlign - 1;
constexpr uint32_t kSramAFBC2BAlign = 16;
constexpr uint32_t kSramAFBC2BMargin = kSramAFBC2BAlign - 1;

ExynosResourceManagerModule::ExynosResourceManagerModule(ExynosDevice *device)
: gs201::ExynosResourceManagerModule(device)
{
    // HW Resource Table for TDM based allocation
    mHWResourceTables = &HWResourceTables;

    char value[PROPERTY_VALUE_MAX];
    property_get("ro.boot.hw.soc.rev", value, "2");
    const int socRev = atoi(value);
    mConstraintRev = socRev < 2 ? CONSTRAINT_A0 : CONSTRAINT_B0;
    HWAttrs.at(TDM_ATTR_WCG).loadSharing =
            (mConstraintRev == CONSTRAINT_A0) ? LS_DPUF : LS_DPUF_AXI;
    ALOGD("%s(): ro.boot.hw.soc.rev=%s ConstraintRev=%d", __func__, value, mConstraintRev);
}

ExynosResourceManagerModule::~ExynosResourceManagerModule() {}

bool ExynosResourceManagerModule::checkTDMResource(ExynosDisplay *display, ExynosMPP *currentMPP,
                                                   ExynosMPPSource *mppSrc) {
    std::array<uint32_t, TDM_ATTR_MAX> accumulatedDPUFAmount{};
    std::array<uint32_t, TDM_ATTR_MAX> accumulatedDPUFAXIAmount{};
    const uint32_t blkId = currentMPP->getHWBlockId();
    const uint32_t axiId = currentMPP->getAXIPortId();
    HDEBUGLOGD(eDebugTDM, "%s : %p trying to assign to %s, compare with layers", __func__,
               mppSrc->mSrcImg.bufferHandle, currentMPP->mName.c_str());
    ExynosLayer *layer = (mppSrc->mSourceType == MPP_SOURCE_LAYER) ? (ExynosLayer *)mppSrc : nullptr;

    for (auto compLayer : display->mLayers) {
        ExynosMPP *otfMPP = compLayer->mOtfMPP;
        if (!otfMPP || layer == compLayer) continue;
        getAmounts(display, blkId, axiId, otfMPP, mppSrc, compLayer,
                   accumulatedDPUFAmount, accumulatedDPUFAXIAmount);
    }

    if (display->mExynosCompositionInfo.mHasCompositionLayer) {
        HDEBUGLOGD(eDebugTDM,
                   "%s : %p trying to assign to %s, compare with ExynosComposition Target buffer",
                   __func__, mppSrc->mSrcImg.bufferHandle, currentMPP->mName.c_str());
        ExynosMPP *otfMPP = display->mExynosCompositionInfo.mOtfMPP;
        if (otfMPP)
            getAmounts(display, blkId, axiId, otfMPP, mppSrc, &display->mExynosCompositionInfo,
                       accumulatedDPUFAmount, accumulatedDPUFAXIAmount);
    }

    if (display->mClientCompositionInfo.mHasCompositionLayer) {
        HDEBUGLOGD(eDebugTDM,
                   "%s : %p trying to assign to %s, compare with ClientComposition Target buffer",
                   __func__, mppSrc->mSrcImg.bufferHandle, currentMPP->mName.c_str());
        ExynosMPP *otfMPP = display->mClientCompositionInfo.mOtfMPP;
        if (otfMPP)
            getAmounts(display, blkId, axiId, otfMPP, mppSrc, &display->mClientCompositionInfo,
                       accumulatedDPUFAmount, accumulatedDPUFAXIAmount);
    }

    for (auto attr = HWAttrs.begin(); attr != HWAttrs.end(); attr++) {
        const LoadSharing_t &loadSharing = attr->second.loadSharing;
        uint32_t currentAmount = mppSrc->getHWResourceAmount(attr->first);
        auto &accumulatedAmount =
                (loadSharing == LS_DPUF) ? accumulatedDPUFAmount : accumulatedDPUFAXIAmount;
        const auto &TDMInfoIdx =
                std::make_pair(blkId,
                               (loadSharing == LS_DPUF) ? AXI_DONT_CARE : axiId);
        int32_t totalAmount =
                display->mDisplayTDMInfo[TDMInfoIdx].getAvailableAmount(attr->first).totalAmount;
        HDEBUGLOGD(eDebugTDM,
                   "%s, layer[%p] -> %s attr[%s],ls=%d,accumulated:%d,current:%d,total: %d",
                   __func__, mppSrc->mSrcImg.bufferHandle, currentMPP->mName.c_str(),
                   attr->second.name.c_str(), loadSharing, accumulatedAmount[attr->first],
                   currentAmount, totalAmount);
        if (accumulatedAmount[attr->first] + currentAmount > totalAmount) {
            HDEBUGLOGD(eDebugTDM, "%s, %s could not assigned by attr[%s]", __func__,
                       currentMPP->mName.c_str(), attr->second.name.c_str());
            return false;
        }
    }

    HDEBUGLOGD(eDebugTDM, "%s : %p trying to assign to %s successfully", __func__,
               mppSrc->mSrcImg.bufferHandle, currentMPP->mName.c_str());
    return true;
}

bool ExynosResourceManagerModule::isHWResourceAvailable(ExynosDisplay *display,
                                                        ExynosMPP *currentMPP,
                                                        ExynosMPPSource *mppSrc) {
    if (!checkTDMResource(display, currentMPP, mppSrc)) {
        return false;
    }

    std::list<ExynosLayer *> overlappedLayers;
    uint32_t currentBlockId = currentMPP->getHWBlockId();
    for (auto layer : display->mLayers) {
        ExynosMPP *otfMPP = layer->mOtfMPP;
        if (!otfMPP || dynamic_cast<ExynosMPPSource *>(layer) == mppSrc) continue;

        if ((currentBlockId == otfMPP->getHWBlockId()) && isOverlapped(display, mppSrc, layer))
            overlappedLayers.push_back(layer);
    }

    if (overlappedLayers.size()) {
        HDEBUGLOGD(eDebugTDM,
                   "%s : %p trying to assign to %s, check its overlapped layers(%zu) status",
                   __func__, mppSrc->mSrcImg.bufferHandle, currentMPP->mName.c_str(),
                   overlappedLayers.size());

        for (auto &overlappedLayer : overlappedLayers) {
            HDEBUGLOGD(eDebugTDM, "%s : %p overlapped %p", __func__, mppSrc->mSrcImg.bufferHandle,
                       overlappedLayer->mLayerBuffer);
            if (!checkTDMResource(display, overlappedLayer->mOtfMPP, overlappedLayer)) {
                return false;
            }
        }
    }
    return true;
}

void ExynosResourceManagerModule::setupHWResource(const tdm_attr_t &tdmAttrId, const String8 &name,
                                                  const DPUblockId_t &blkId,
                                                  const AXIPortId_t &axiId, ExynosDisplay *display,
                                                  ExynosDisplay *addedDisplay,
                                                  const ConstraintRev_t &constraintsRev) {
    const int32_t dispType = display->mType;
    const auto &resourceIdx = HWResourceIndexes(tdmAttrId, blkId, axiId, dispType, constraintsRev);
    const auto &iter = mHWResourceTables->find(resourceIdx);
    if (iter != mHWResourceTables->end()) {
        auto &hwResource = iter->second;
        const auto &TDMInfoIdx = (HWAttrs.at(tdmAttrId).loadSharing == LS_DPUF)
                ? std::make_pair(blkId, AXI_DONT_CARE)
                : std::make_pair(blkId, axiId);
        uint32_t amount = (addedDisplay == nullptr) ? hwResource.maxAssignedAmount
                                                    : hwResource.totalAmount -
                        addedDisplay->mDisplayTDMInfo[TDMInfoIdx]
                                .getAvailableAmount(tdmAttrId)
                                .totalAmount;
        display->mDisplayTDMInfo[TDMInfoIdx].initTDMInfo(DisplayTDMInfo::ResourceAmount_t{amount},
                                                         tdmAttrId);
        if (addedDisplay == nullptr) {
            HDEBUGLOGD(eDebugTDM, "(%s=>%s) : %s amount is updated to %d",
                       resourceIdx.toString8().c_str(), iter->first.toString8().c_str(),
                       name.c_str(), amount);
        } else {
            HDEBUGLOGD(eDebugTDM, "(%s=>%s) : hwResource.totalAmount=%d %s amount is updated to %d",
                       resourceIdx.toString8().c_str(), iter->first.toString8().c_str(),
                       hwResource.totalAmount, name.c_str(), amount);
        }
    } else {
        ALOGW("(%s): cannot find resource for %s", resourceIdx.toString8().c_str(), name.c_str());
    }
}

uint32_t ExynosResourceManagerModule::setDisplaysTDMInfo()
{
    ExynosDisplay *addedDisplay = nullptr;

    /*
     * Checking display connections,
     * Assume that WFD and External are not connected at the same time
     * If non-primary display is connected, primary display's HW resource is looted
     */
    for (auto &display : mDisplays) {
        if (display->mType == HWC_DISPLAY_PRIMARY) continue;
        if (display->isEnabled()) {
            addedDisplay = display;
            break;
        }
    }

    /*
     * Update Primary's resource amount. primary = total - loot(other display's HW resource)
     * Other's aready defined at initDisplaysTDMInfo()
     */
    ExynosDisplay *primaryDisplay = getDisplay(getDisplayId(HWC_DISPLAY_PRIMARY, 0));
    for (auto attr = HWAttrs.begin(); attr != HWAttrs.end(); attr++) {
        for (auto blockId = DPUBlocks.begin(); blockId != DPUBlocks.end(); blockId++) {
            if (attr->second.loadSharing == LS_DPUF) {
                setupHWResource(attr->first, attr->second.name, blockId->first, AXI_DONT_CARE,
                                primaryDisplay, addedDisplay, mConstraintRev);
            } else if (attr->second.loadSharing == LS_DPUF_AXI) {
                for (auto axi = AXIPorts.begin(); axi != AXIPorts.end(); ++axi) {
                    setupHWResource(attr->first, attr->second.name, blockId->first, axi->first,
                                    primaryDisplay, addedDisplay, mConstraintRev);
                }
            }
        }
    }

    if (hwcCheckDebugMessages(eDebugTDM)) {
        for (auto &display : mDisplays) {
            for (auto attr = HWAttrs.begin(); attr != HWAttrs.end(); attr++) {
                for (auto blockId = DPUBlocks.begin(); blockId != DPUBlocks.end(); blockId++) {
                    if (attr->second.loadSharing == LS_DPUF) {
                        const auto &TDMInfoId = std::make_pair(blockId->first, AXI_DONT_CARE);
                        int32_t amount = display->mDisplayTDMInfo[TDMInfoId]
                                                 .getAvailableAmount(attr->first)
                                                 .totalAmount;
                        HDEBUGLOGD(eDebugTDM, "%s : [%s] display:%d,block:%d, amount : %d(%s)",
                                   __func__, attr->second.name.c_str(), display->mType,
                                   blockId->first, amount,
                                   display->isEnabled() ? "used" : "not used");
                    } else {
                        for (auto axi = AXIPorts.begin(); axi != AXIPorts.end(); ++axi) {
                            const auto &TDMInfoId = std::make_pair(blockId->first, axi->first);
                            int32_t amount = display->mDisplayTDMInfo[TDMInfoId]
                                                     .getAvailableAmount(attr->first)
                                                     .totalAmount;
                            HDEBUGLOGD(eDebugTDM,
                                       "%s : [%s] display:%d,block:%d,axi:%d, amount:%d(%s)",
                                       __func__, attr->second.name.c_str(), display->mType,
                                       blockId->first, axi->first, amount,
                                       display->isEnabled() ? "used" : "not used");
                        }
                    }
                }
            }
        }
    }

    return 0;
}

uint32_t ExynosResourceManagerModule::initDisplaysTDMInfo()
{
    /*
     * Initialize as predefined value at table
     * Primary's resource will be changed at setDisplaysTDMInfo() function
     */
    for (auto &display : mDisplays) {
        for (auto attr = HWAttrs.begin(); attr != HWAttrs.end(); attr++) {
            for (auto blockId = DPUBlocks.begin(); blockId != DPUBlocks.end(); blockId++) {
                if (attr->second.loadSharing == LS_DPUF) {
                    setupHWResource(attr->first, attr->second.name, blockId->first, AXI_DONT_CARE,
                                    display, nullptr, mConstraintRev);
                } else if (attr->second.loadSharing == LS_DPUF_AXI) {
                    for (auto axi = AXIPorts.begin(); axi != AXIPorts.end(); ++axi) {
                        setupHWResource(attr->first, attr->second.name, blockId->first, axi->first,
                                        display, nullptr, mConstraintRev);
                    }
                }
            }
        }
    }

    return 0;
}

uint32_t getSramAmount(tdm_attr_t attr, uint32_t formatProperty, lbWidthIndex_t widthIndex) {
    auto it = sramAmountMap.find(sramAmountParams(attr, formatProperty, widthIndex));
    return (it != sramAmountMap.end()) ? it->second : 0;
}

uint32_t ExynosResourceManagerModule::calculateHWResourceAmount(ExynosDisplay *display,
                                                                ExynosMPPSource *mppSrc)
{
    uint32_t SRAMtotal = 0;

    if (mppSrc == nullptr) return SRAMtotal;

    if (mppSrc->mSourceType == MPP_SOURCE_LAYER) {
        ExynosLayer *layer = static_cast<ExynosLayer *>(mppSrc->mSource);
        if (layer == nullptr) {
            ALOGE("%s: cannot cast ExynosLayer", __func__);
            return SRAMtotal;
        }
        exynos_image src_img;
        exynos_image dst_img;
        layer->setSrcExynosImage(&src_img);
        layer->setDstExynosImage(&dst_img);
        layer->setExynosImage(src_img, dst_img);
    }

    int32_t transform = mppSrc->mSrcImg.transform;
    int32_t compressType = mppSrc->mSrcImg.compressionInfo.type;
    bool rotation = (transform & HAL_TRANSFORM_ROT_90) ? true : false;

    int32_t width = mppSrc->mSrcImg.w;
    int32_t height = mppSrc->mSrcImg.h;
    uint32_t format = mppSrc->mSrcImg.format;
    uint32_t formatBPP = 0;
    if (isFormat10Bit(format))
        formatBPP = BIT10;
    else if (isFormat8Bit(format))
        formatBPP = BIT8;

    /** To find index **/
    uint32_t formatIndex = 0;

    lbWidthIndex_t widthIndex = LB_W_3073_INF;

    auto findWidthIndex = [&](int32_t w) -> lbWidthIndex_t {
        for (auto it = LB_WIDTH_INDEX_MAP.begin(); it != LB_WIDTH_INDEX_MAP.end(); it++) {
            if (w >= it->second.widthDownto && w <= it->second.widthUpto) {
                return it->first;
            }
        }
        return LB_W_3073_INF;
    };

    /* Caluclate SRAM amount */
    if (rotation) {
        width = height;
        /* Rotation amount, Only YUV rotation is supported */
        if (compressType == COMP_TYPE_SBWC) {
            /* Y and UV width should be aligned and should get sram for each Y and UV */
            int32_t width_y = pixel_align(width + kSramSBWCRotWidthAlign, kSramSBWCRotWidthAlign);
            int32_t width_c =
                    pixel_align(width / 2 + kSramSBWCRotWidthAlign, kSramSBWCRotWidthAlign);
            SRAMtotal += getSramAmount(TDM_ATTR_ROT_90, SBWC_Y, findWidthIndex(width_y));
            SRAMtotal += getSramAmount(TDM_ATTR_ROT_90, SBWC_UV, findWidthIndex(width_c * 2));
        } else {
            /* sramAmountMap has SRAM for both Y and UV */
            widthIndex = findWidthIndex(width);
            SRAMtotal += getSramAmount(TDM_ATTR_ROT_90, NON_SBWC_Y | formatBPP, widthIndex);
            SRAMtotal += getSramAmount(TDM_ATTR_ROT_90, NON_SBWC_UV | formatBPP, widthIndex);
        }
        HDEBUGLOGD(eDebugTDM, "+ rotation : %d", SRAMtotal);
    } else {
        if (compressType == COMP_TYPE_SBWC) {
            width = pixel_align(width + kSramSBWCWidthMargin, kSramSBWCWidthAlign);
        } else if (compressType == COMP_TYPE_AFBC) {
            /* Align for 8,4Byte/pixel formats */
            if (formatToBpp(format) > 16) {
                width = pixel_align(width + kSramAFBC8B4BMargin, kSramAFBC8B4BAlign);
            } else {
                /* Align for 2Byte/pixel formats */
                width = pixel_align(width + kSramAFBC2BMargin, kSramAFBC2BAlign);
            }
        }
        widthIndex = findWidthIndex(width);

        /* AFBC amount */
        if (compressType == COMP_TYPE_AFBC) {
            formatIndex = (isFormatRgb(format) ? RGB : 0) | formatBPP;
            SRAMtotal += getSramAmount(TDM_ATTR_AFBC, formatIndex, widthIndex);
            HDEBUGLOGD(eDebugTDM, "+ AFBC : %d", SRAMtotal);
        }

        /* SBWC amount */
        if (compressType == COMP_TYPE_SBWC) {
            SRAMtotal += getSramAmount(TDM_ATTR_SBWC, SBWC_Y, widthIndex);
            SRAMtotal += getSramAmount(TDM_ATTR_SBWC, SBWC_UV, widthIndex);
            HDEBUGLOGD(eDebugTDM, "+ SBWC : %d", SRAMtotal);
        }
    }

    /* ITP (CSC) amount */
    if (isFormatYUV(format)) {
        /** ITP has no size difference, Use width index as LB_W_3073_INF **/
        SRAMtotal += getSramAmount(TDM_ATTR_ITP, formatBPP, LB_W_3073_INF);
        HDEBUGLOGD(eDebugTDM, "+ YUV : %d", SRAMtotal);
    }

    /* Scale amount */
    int srcW = mppSrc->mSrcImg.w;
    int srcH = mppSrc->mSrcImg.h;
    int dstW = mppSrc->mDstImg.w;
    int dstH = mppSrc->mDstImg.h;

    if (!!(transform & HAL_TRANSFORM_ROT_90)) {
        int tmp = dstW;
        dstW = dstH;
        dstH = tmp;
    }

    bool isScaled = ((srcW != dstW) || (srcH != dstH));

    if (isScaled) {
        if (formatHasAlphaChannel(format))
            formatIndex = FORMAT_RGB_MASK;
        else
            formatIndex = FORMAT_YUV_MASK;

        /** Scale has no size difference, Use width index as LB_W_3073_INF **/
        SRAMtotal += getSramAmount(TDM_ATTR_SCALE, formatIndex, LB_W_3073_INF);
        HDEBUGLOGD(eDebugTDM, "+ Scale : %d", SRAMtotal);
    }

    for (auto it = HWAttrs.begin(); it != HWAttrs.end(); it++) {
        uint32_t amount = 0;
        if (it->first == TDM_ATTR_SRAM_AMOUNT) {
            amount = SRAMtotal;
        } else {
            amount = needHWResource(display, mppSrc->mSrcImg, mppSrc->mDstImg, it->first);
        }
        mppSrc->setHWResourceAmount(it->first, amount);
    }

    HDEBUGLOGD(eDebugTDM,
               "mppSrc(%p) needed SRAM(%d), SCALE(%d), AFBC(%d), CSC(%d), SBWC(%d), WCG(%d), "
               "ROT(%d)",
               mppSrc->mSrcImg.bufferHandle, SRAMtotal,
               needHWResource(display, mppSrc->mSrcImg, mppSrc->mDstImg, TDM_ATTR_SCALE),
               needHWResource(display, mppSrc->mSrcImg, mppSrc->mDstImg, TDM_ATTR_AFBC),
               needHWResource(display, mppSrc->mSrcImg, mppSrc->mDstImg, TDM_ATTR_ITP),
               needHWResource(display, mppSrc->mSrcImg, mppSrc->mDstImg, TDM_ATTR_SBWC),
               needHWResource(display, mppSrc->mSrcImg, mppSrc->mDstImg, TDM_ATTR_WCG),
               needHWResource(display, mppSrc->mSrcImg, mppSrc->mDstImg, TDM_ATTR_ROT_90));

    return SRAMtotal;
}

int32_t ExynosResourceManagerModule::otfMppReordering(ExynosDisplay *display,
                                                      ExynosMPPVector &otfMPPs,
                                                      struct exynos_image &src,
                                                      struct exynos_image &dst)
{
    int orderingType = isAFBCCompressed(src.bufferHandle)
            ? ORDER_AFBC
            : (needHdrProcessing(display, src, dst) ? ORDER_WCG : ORDER_AXI);

    int usedAFBCCount[DPU_BLOCK_CNT] = {0};
    int usedWCGCount[DPU_BLOCK_CNT * AXI_PORT_MAX_CNT] = {0};
    int usedBlockCount[DPU_BLOCK_CNT] = {0};
    int usedAXIPortCount[AXI_PORT_MAX_CNT] = {0};

    auto orderPolicy = [&](const void *lhs, const void *rhs) -> bool {
        if (lhs == NULL || rhs == NULL) {
            return 0;
        }

        const ExynosMPPModule *l = (ExynosMPPModule *)lhs;
        const ExynosMPPModule *r = (ExynosMPPModule *)rhs;

        uint32_t assignedStateL = l->mAssignedState & MPP_ASSIGN_STATE_ASSIGNED;
        uint32_t assignedStateR = r->mAssignedState & MPP_ASSIGN_STATE_ASSIGNED;

        if (assignedStateL != assignedStateR) return assignedStateL < assignedStateR;

        if (l->mPhysicalType != r->mPhysicalType) return l->mPhysicalType < r->mPhysicalType;

        if (orderingType == ORDER_AFBC) {
            /* AFBC balancing */
            if ((l->mAttr & MPP_ATTR_AFBC) != (r->mAttr & MPP_ATTR_AFBC))
                return (l->mAttr & MPP_ATTR_AFBC) > (r->mAttr & MPP_ATTR_AFBC);
            if (l->mAttr & MPP_ATTR_AFBC) {
                /* If layer is AFBC, DPU block that AFBC HW block belongs
                 * which has not been used much should be placed in the front */
                if (usedAFBCCount[l->mHWBlockId] != usedAFBCCount[r->mHWBlockId])
                    return usedAFBCCount[l->mHWBlockId] < usedAFBCCount[r->mHWBlockId];
            }
        } else if (orderingType == ORDER_WCG) {
            /* WCG balancing */
            if ((l->mAttr & MPP_ATTR_WCG) != (r->mAttr & MPP_ATTR_WCG))
                return (l->mAttr & MPP_ATTR_WCG) > (r->mAttr & MPP_ATTR_WCG);
            if (l->mAttr & MPP_ATTR_WCG) {
                /* If layer is WCG, DPU block that WCG HW block belongs
                 * which has not been used much should be placed in the front */
                if (usedWCGCount[l->mHWBlockId * AXI_PORT_MAX_CNT + l->mAXIPortId] !=
                    usedWCGCount[r->mHWBlockId * AXI_PORT_MAX_CNT + r->mAXIPortId])
                    return usedWCGCount[l->mHWBlockId * AXI_PORT_MAX_CNT + l->mAXIPortId] <
                        usedWCGCount[r->mHWBlockId * AXI_PORT_MAX_CNT + r->mAXIPortId];
            }
        }

        /* AXI bus balancing */
        /* AXI port which has not been used much should be placed in the front */
        if (usedAXIPortCount[l->mAXIPortId] != usedAXIPortCount[r->mAXIPortId]) {
            return usedAXIPortCount[l->mAXIPortId] < usedAXIPortCount[r->mAXIPortId];
        }
        /* IF MPP connected same AXI port, Block balancing should be regarded after */
        if (usedBlockCount[l->mHWBlockId] != usedBlockCount[r->mHWBlockId])
            return usedBlockCount[l->mHWBlockId] < usedBlockCount[r->mHWBlockId];

        return l->mPhysicalIndex < r->mPhysicalIndex;
    };

    for (auto it : otfMPPs) {
        ExynosMPPModule *mpp = (ExynosMPPModule *)it;
        uint32_t bId = mpp->getHWBlockId();
        uint32_t aId = mpp->getAXIPortId();
        bool isAFBC = false;
        bool isWCG = false;

        if (mpp->mAssignedState & MPP_ASSIGN_STATE_ASSIGNED) {
            ExynosMPPSource *mppSrc = mpp->mAssignedSources[0];
            if ((mppSrc->mSourceType == MPP_SOURCE_LAYER) &&
                (mppSrc->mSrcImg.bufferHandle != nullptr)) {
                if ((mpp->mAttr & MPP_ATTR_AFBC) &&
                    (isAFBCCompressed(mppSrc->mSrcImg.bufferHandle))) {
                    isAFBC = true;
                    usedAFBCCount[bId]++;
                } else if ((mpp->mAttr & MPP_ATTR_WCG) &&
                           (needHdrProcessing(display, mppSrc->mSrcImg, mppSrc->mDstImg))) {
                    isWCG = true;
                    usedWCGCount[bId]++;
                }
            } else if (mppSrc->mSourceType == MPP_SOURCE_COMPOSITION_TARGET) {
                ExynosCompositionInfo *info = (ExynosCompositionInfo *)mppSrc;
                // ESTEVAN_TBD
                // if ((mpp->mAttr & MPP_ATTR_AFBC) && (info->mCompressionInfo.type ==
                // COMP_TYPE_AFBC)) {
                if ((mpp->mAttr & MPP_ATTR_AFBC) &&
                    (isAFBCCompressed(mppSrc->mSrcImg.bufferHandle))) {
                    isAFBC = true;
                    usedAFBCCount[bId]++;
                } else if ((mpp->mAttr & MPP_ATTR_WCG) &&
                           (needHdrProcessing(display, info->mSrcImg, info->mDstImg))) {
                    isWCG = true;
                    usedWCGCount[bId]++;
                }
            }

            HDEBUGLOGD(eDebugLoadBalancing, "%s: %s is assigned (AFBC:%d, WCG:%d), is %s", __func__,
                       mpp->mName.c_str(), isAFBC, isWCG,
                       (mppSrc->mSourceType == MPP_SOURCE_LAYER) ? "Layer" : "Client Target");
            usedBlockCount[bId]++;
            usedAXIPortCount[aId]++;
        }
    }

    HDEBUGLOGD(eDebugLoadBalancing,
               "Sorting by %s ordering, AFBC(used DPUF0:%d, DPUF1:%d), AXI(used AXI0:%d, AXI1:%d), "
               "BLOCK(used DPUF0:%d, DPUF1:%d)",
               (orderingType == ORDER_AFBC) ? "AFBC" : "_AXI", usedAFBCCount[DPUF0],
               usedAFBCCount[DPUF1], usedAXIPortCount[AXI0], usedAXIPortCount[AXI1],
               usedBlockCount[DPUF0], usedBlockCount[DPUF1]);

    std::sort(otfMPPs.begin(), otfMPPs.end(), orderPolicy);

    if (hwcCheckDebugMessages(eDebugLoadBalancing)) {
        String8 after;
        for (uint32_t i = 0; i < otfMPPs.size(); i++) {
            ExynosMPPModule *mpp = (ExynosMPPModule *)otfMPPs[i];
            after.appendFormat("(%s) -> ", mpp->mName.c_str());
        }

        ALOGD("%s %p, %s", __func__, src.bufferHandle, after.c_str());
    }

    return 0;
}

bool ExynosResourceManagerModule::isOverlapped(ExynosDisplay *display, ExynosMPPSource *current,
                                               ExynosMPPSource *compare) {
    int CT = current->mDstImg.y - TDM_OVERLAP_MARGIN;
    CT = (CT < 0) ? 0 : CT;
    int CB = current->mDstImg.y + current->mDstImg.h + TDM_OVERLAP_MARGIN;
    CB = (CB > display->mYres) ? display->mYres : CB;
    int LT = compare->mDstImg.y;
    int LB = compare->mDstImg.y + compare->mDstImg.h;

    if (((LT <= CT && CT <= LB) || (LT <= CB && CB <= LB)) ||
        ((CT <= LT && LT <= CB) || (CT < LB && LB <= CB))) {
        HDEBUGLOGD(eDebugTDM, "%s, current %p and compare %p is overlaped", __func__,
                   current->mSrcImg.bufferHandle, compare->mSrcImg.bufferHandle);
        return true;
    }

    return false;
}

uint32_t ExynosResourceManagerModule::getAmounts(ExynosDisplay* display, uint32_t currentBlockId,
                                                 uint32_t currentAXIId, ExynosMPP* compOtfMPP,
                                                 ExynosMPPSource* curSrc, ExynosMPPSource* compSrc,
                                                 std::array<uint32_t, TDM_ATTR_MAX>& DPUFAmounts,
                                                 std::array<uint32_t, TDM_ATTR_MAX>& AXIAmounts) {
    const uint32_t blockId = compOtfMPP->getHWBlockId();
    const uint32_t AXIId = compOtfMPP->getAXIPortId();
    if (currentBlockId == blockId && isOverlapped(display, curSrc, compSrc)) {
        String8 log;
        if (hwcCheckDebugMessages(eDebugTDM)) {
            log.appendFormat("%s", compOtfMPP->mName.c_str());
        }
        for (auto attr = HWAttrs.begin(); attr != HWAttrs.end(); attr++) {
            uint32_t compareAmount = compSrc->getHWResourceAmount(attr->first);
            if (hwcCheckDebugMessages(eDebugTDM)) {
                log.appendFormat(", attr %s DPUF-%d(+ %d)", attr->second.name.c_str(),
                                 DPUFAmounts[attr->first], compareAmount);
            }
            DPUFAmounts[attr->first] += compareAmount;
            if (attr->second.loadSharing == LS_DPUF_AXI && currentAXIId == AXIId) {
                if (hwcCheckDebugMessages(eDebugTDM)) {
                    log.appendFormat(",AXI-%d(+ %d)", AXIAmounts[attr->first], compareAmount);
                }
                AXIAmounts[attr->first] += compareAmount;
            }
        }
        HDEBUGLOGD(eDebugTDM, "%s %s", __func__, log.c_str());
    }

    return 0;
}