1 /*
2  * Copyright (C) 2012 The Android Open Source Project
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 
17 /**
18  * Project HWC 2.0 Design
19  */
20 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
21 #include <utils/Errors.h>
22 #include <sync/sync.h>
23 #include <sys/mman.h>
24 #include <cutils/properties.h>
25 #include "ExynosMPP.h"
26 #include "ExynosResourceRestriction.h"
27 #include <hardware/hwcomposer_defs.h>
28 #include <math.h>
29 #include "VendorGraphicBuffer.h"
30 #include "ExynosHWCDebug.h"
31 #include "ExynosDisplay.h"
32 #include "ExynosVirtualDisplay.h"
33 #include "ExynosLayer.h"
34 #include "ExynosHWCHelper.h"
35 #include "exynos_sync.h"
36 #include "ExynosResourceManager.h"
37 
38 /**
39  * ExynosMPP implementation
40  *
41  * Abstraction class for HW Resource
42  */
43 
44 using namespace android;
45 using namespace vendor::graphics;
46 using namespace SOC_VERSION;
47 
48 int ExynosMPP::mainDisplayWidth = 0;
49 int ExynosMPP::mainDisplayHeight = 0;
50 extern struct exynos_hwc_control exynosHWCControl;
51 
52 std::unordered_map<tdm_attr_t, TDMInfo_t> HWAttrs = {
53     {TDM_ATTR_SRAM_AMOUNT, {String8("SRAM"),  LS_DPUF}},
54     {TDM_ATTR_AFBC,        {String8("AFBC"),  LS_DPUF}},
55     {TDM_ATTR_SBWC,        {String8("SBWC"),  LS_DPUF}},
56     {TDM_ATTR_ITP,         {String8("CSC"),   LS_DPUF}},
57     {TDM_ATTR_ROT_90,      {String8("ROT"),   LS_DPUF}},
58     {TDM_ATTR_SCALE,       {String8("SCALE"), LS_DPUF}},
59     {TDM_ATTR_WCG,         {String8("WCG"),   LS_DPUF_AXI}},
60 };
61 
dumpExynosMPPImgInfo(uint32_t type,exynos_mpp_img_info & imgInfo)62 void dumpExynosMPPImgInfo(uint32_t type, exynos_mpp_img_info &imgInfo)
63 {
64     HDEBUGLOGD(type, "\tbuffer: %p, bufferType: %d",
65             imgInfo.bufferHandle, imgInfo.bufferType);
66 }
67 
exynosMPPSourceComp(const ExynosMPPSource * l,const ExynosMPPSource * r)68 bool exynosMPPSourceComp(const ExynosMPPSource* l, const ExynosMPPSource* r)
69 {
70     if (l == NULL || r == NULL) {
71         HWC_LOGE(NULL,"exynosMPP compare error");
72         return 0;
73     }
74     return (l->mSrcImg.zOrder < r->mSrcImg.zOrder);
75 }
76 
dump(const restriction_size_t & restrictionSize,String8 & result)77 void dump(const restriction_size_t &restrictionSize, String8 &result) {
78     result.appendFormat("    maxDownScale = %u, maxUpscale = %u\n", restrictionSize.maxDownScale,
79                         restrictionSize.maxUpScale);
80     result.appendFormat("    maxFullWidth = %u, maxFullHeight = %u\n", restrictionSize.maxFullWidth,
81                         restrictionSize.maxFullHeight);
82     result.appendFormat("    minFullWidth = %u, minFullHeight = %u\n", restrictionSize.minFullWidth,
83                         restrictionSize.minFullHeight);
84     result.appendFormat("    fullWidthAlign = %u, fullHeightAlign = %u\n",
85                         restrictionSize.fullWidthAlign, restrictionSize.fullHeightAlign);
86     result.appendFormat("    maxCropWidth = %u, maxCropHeight = %u\n", restrictionSize.maxCropWidth,
87                         restrictionSize.maxCropHeight);
88     result.appendFormat("    minCropWidth = %u, minCropHeight = %u\n", restrictionSize.minCropWidth,
89                         restrictionSize.minCropHeight);
90     result.appendFormat("    cropXAlign = %u, cropYAlign = %u\n", restrictionSize.cropXAlign,
91                         restrictionSize.cropYAlign);
92     result.appendFormat("    cropWidthAlign = %u, cropHeightAlign = %u\n",
93                         restrictionSize.cropWidthAlign, restrictionSize.cropHeightAlign);
94 }
95 
ExynosMPPSource()96 ExynosMPPSource::ExynosMPPSource()
97       : mSourceType(MPP_SOURCE_MAX), mSource(NULL), mOtfMPP(NULL), mM2mMPP(NULL) {
98     memset(&mSrcImg, 0, sizeof(mSrcImg));
99     mSrcImg.acquireFenceFd = -1;
100     mSrcImg.releaseFenceFd = -1;
101     memset(&mDstImg, 0, sizeof(mDstImg));
102     mDstImg.acquireFenceFd = -1;
103     mDstImg.releaseFenceFd = -1;
104     memset(&mMidImg, 0, sizeof(mMidImg));
105     mMidImg.acquireFenceFd = -1;
106     mMidImg.releaseFenceFd = -1;
107 
108     mHWResourceAmount.clear();
109 }
110 
ExynosMPPSource(uint32_t sourceType,void * source)111 ExynosMPPSource::ExynosMPPSource(uint32_t sourceType, void *source)
112     : mSourceType(sourceType),
113     mSource(source),
114     mOtfMPP(NULL),
115     mM2mMPP(NULL)
116 {
117     memset(&mSrcImg, 0, sizeof(mSrcImg));
118     mSrcImg.acquireFenceFd = -1;
119     mSrcImg.releaseFenceFd = -1;
120     memset(&mDstImg, 0, sizeof(mDstImg));
121     mDstImg.acquireFenceFd = -1;
122     mDstImg.releaseFenceFd = -1;
123     memset(&mMidImg, 0, sizeof(mMidImg));
124     mMidImg.acquireFenceFd = -1;
125     mMidImg.releaseFenceFd = -1;
126 }
127 
setExynosImage(const exynos_image & src_img,const exynos_image & dst_img)128 void ExynosMPPSource::setExynosImage(const exynos_image& src_img, const exynos_image& dst_img) {
129     mSrcImg = src_img;
130     mDstImg = dst_img;
131 }
132 
setExynosMidImage(const exynos_image & mid_img)133 void ExynosMPPSource::setExynosMidImage(const exynos_image& mid_img) {
134     mMidImg = mid_img;
135 }
136 
ExynosMPP(ExynosResourceManager * resourceManager,uint32_t physicalType,uint32_t logicalType,const char * name,uint32_t physicalIndex,uint32_t logicalIndex,uint32_t preAssignInfo)137 ExynosMPP::ExynosMPP(ExynosResourceManager* resourceManager,
138         uint32_t physicalType, uint32_t logicalType, const char *name,
139         uint32_t physicalIndex, uint32_t logicalIndex, uint32_t preAssignInfo)
140 : mResourceManager(resourceManager),
141     mMPPType(MPP_TYPE_NONE),
142     mPhysicalType(physicalType),
143     mLogicalType(logicalType),
144     mName(name),
145     mPhysicalIndex(physicalIndex),
146     mLogicalIndex(logicalIndex),
147     mPreAssignDisplayInfo(preAssignInfo),
148     mHWState(MPP_HW_STATE_IDLE),
149     mLastStateFenceFd(-1),
150     mAssignedState(MPP_ASSIGN_STATE_FREE),
151     mEnable(true),
152     mAssignedDisplay(NULL),
153     mMaxSrcLayerNum(1),
154     mPrevAssignedState(MPP_ASSIGN_STATE_FREE),
155     mPrevAssignedDisplayType(-1),
156     mReservedDisplay(-1),
157     mResourceManageThread(android::sp<ResourceManageThread>::make(this)),
158     mCapacity(-1),
159     mUsedCapacity(0),
160     mAllocOutBufFlag(true),
161     mFreeOutBufFlag(true),
162     mHWBusyFlag(false),
163     mCurrentDstBuf(0),
164     mPrivDstBuf(-1),
165     mNeedCompressedTarget(false),
166     mDstAllocatedSize(DST_SIZE_UNKNOWN),
167     mUseM2MSrcFence(false),
168     mAttr(0),
169     mAssignOrder(0),
170     mAXIPortId(0),
171     mHWBlockId(0),
172     mNeedSolidColorLayer(false)
173 {
174     if (mPhysicalType < MPP_DPP_NUM) {
175         mClockKhz = VPP_CLOCK;
176         mPPC = VPP_PIXEL_PER_CLOCK;
177     }
178 
179     if (mPhysicalType == MPP_G2D) {
180         mClockKhz = G2D_CLOCK;
181         if (mLogicalType == MPP_LOGICAL_G2D_RGB) {
182 
183             char value[256];
184             int afbc_prop;
185             property_get("ro.vendor.ddk.set.afbc", value, "0");
186             afbc_prop = atoi(value);
187             if (afbc_prop == 0)
188                 mNeedCompressedTarget = false;
189             else
190                 mNeedCompressedTarget = true;
191 
192             mMaxSrcLayerNum = G2D_MAX_SRC_NUM;
193         } else if (mLogicalType == MPP_LOGICAL_G2D_COMBO &&
194                 (mPreAssignDisplayInfo & HWC_DISPLAY_VIRTUAL_BIT)) {
195             mMaxSrcLayerNum = G2D_MAX_SRC_NUM - 1;
196             mAllocOutBufFlag = false;
197             mNeedCompressedTarget = false;
198             mUseM2MSrcFence = true;
199         }
200         /* Capacity means time(ms) that can be used for operation */
201         mCapacity = MPP_G2D_CAPACITY;
202         mAcrylicHandle = AcrylicFactory::createAcrylic("default_compositor");
203         if (mAcrylicHandle == NULL) {
204             MPP_LOGE("Fail to allocate acrylic handle");
205             abort();
206         } else {
207             MPP_LOGI("mAcrylicHandle is created: %p", mAcrylicHandle);
208         }
209     }
210 
211     /* Basic feature supported flags */
212     for (const auto &feature: feature_table) {
213         if (feature.hwType == mPhysicalType)
214             mAttr = feature.attr;
215     }
216 
217     if (mPhysicalType == MPP_MSC) {
218         mClockKhz = MSC_CLOCK;
219         /* To do
220         * Capacity should be set
221         */
222         mCapacity = MPP_MSC_CAPACITY;
223         mAcrylicHandle = AcrylicFactory::createAcrylic("default_scaler");
224         if (mAcrylicHandle == NULL) {
225             MPP_LOGE("Fail to allocate acrylic handle");
226             abort();
227         } else {
228             MPP_LOGI("mAcrylicHandle is created: %p", mAcrylicHandle);
229         }
230     }
231 
232     if (mMaxSrcLayerNum > 1) {
233         mNeedSolidColorLayer = true;
234         mAcrylicHandle->setDefaultColor(0, 0, 0, 0);
235     }
236 
237     mAssignedSources.clear();
238     resetUsedCapacity();
239 
240     mResourceManageThread->mRunning = true;
241     mResourceManageThread->run("MPPThread");
242 
243     memset(&mPrevFrameInfo, 0, sizeof(mPrevFrameInfo));
244     for (int i = 0; i < NUM_MPP_SRC_BUFS; i++) {
245         mPrevFrameInfo.srcInfo[i].acquireFenceFd = -1;
246         mPrevFrameInfo.srcInfo[i].releaseFenceFd = -1;
247         mPrevFrameInfo.dstInfo[i].acquireFenceFd = -1;
248         mPrevFrameInfo.dstInfo[i].releaseFenceFd = -1;
249     }
250 
251     for (uint32_t i = 0; i < NUM_MPP_SRC_BUFS; i++) {
252         memset(&mSrcImgs[i], 0, sizeof(mSrcImgs[i]));
253         mSrcImgs[i].acrylicAcquireFenceFd = -1;
254         mSrcImgs[i].acrylicReleaseFenceFd = -1;
255     }
256     for (uint32_t i = 0; i < NUM_MPP_DST_BUFS(mLogicalType); i++) {
257         memset(&mDstImgs[i], 0, sizeof(mDstImgs[i]));
258         mDstImgs[i].acrylicAcquireFenceFd = -1;
259         mDstImgs[i].acrylicReleaseFenceFd = -1;
260     }
261 
262     for (uint32_t i = 0; i < DISPLAY_MODE_NUM; i++)
263     {
264         mPreAssignDisplayList[i] = 0;
265     }
266 }
267 
~ExynosMPP()268 ExynosMPP::~ExynosMPP()
269 {
270     mResourceManageThread->mRunning = false;
271     mResourceManageThread->requestExitAndWait();
272 }
273 
274 
ResourceManageThread(ExynosMPP * exynosMPP)275 ExynosMPP::ResourceManageThread::ResourceManageThread(ExynosMPP *exynosMPP)
276 : mExynosMPP(exynosMPP),
277     mRunning(false)
278 {
279 }
280 
~ResourceManageThread()281 ExynosMPP::ResourceManageThread::~ResourceManageThread()
282 {
283 }
284 
isDataspaceSupportedByMPP(struct exynos_image & src,struct exynos_image & dst)285 bool ExynosMPP::isDataspaceSupportedByMPP(struct exynos_image &src, struct exynos_image &dst)
286 {
287     uint32_t srcStandard = (src.dataSpace & HAL_DATASPACE_STANDARD_MASK);
288     uint32_t dstStandard = (dst.dataSpace & HAL_DATASPACE_STANDARD_MASK);
289     uint32_t srcTransfer = (src.dataSpace & HAL_DATASPACE_TRANSFER_MASK);
290     uint32_t dstTransfer = (dst.dataSpace & HAL_DATASPACE_TRANSFER_MASK);
291 
292     /* No conversion case */
293     if ((srcStandard == dstStandard) && (srcTransfer == dstTransfer))
294         return true;
295 
296     /* Unspecified conversion case */
297     if (((srcStandard == HAL_DATASPACE_STANDARD_UNSPECIFIED) ||
298          (dstStandard == HAL_DATASPACE_STANDARD_UNSPECIFIED)) &&
299         ((srcTransfer == HAL_DATASPACE_TRANSFER_UNSPECIFIED) ||
300          (dstTransfer == HAL_DATASPACE_TRANSFER_UNSPECIFIED)))
301         return true;
302 
303     /* WCG support check */
304     /* 'Src is not HDR' and 'src,dst has differenct dataspace' means WCG case */
305     /* Some MPPs are only support HDR but WCG */
306     if (!hasHdrInfo(src) && ((mAttr & MPP_ATTR_WCG) == 0))
307         return false;
308 
309     /* Standard support check */
310     auto standard_it = dataspace_standard_map.find(srcStandard);
311     if ((standard_it == dataspace_standard_map.end()) ||
312         ((mAttr & standard_it->second) == 0))
313         return false;
314 
315     /* Transfer support check */
316     auto transfer_it = dataspace_transfer_map.find(srcTransfer);
317     if ((transfer_it == dataspace_transfer_map.end()) ||
318         ((mAttr & transfer_it->second) == 0))
319         return false;
320 
321     return checkCSCRestriction(src, dst);
322 }
323 
isSupportedHDR(struct exynos_image & src,struct exynos_image & dst)324 bool ExynosMPP::isSupportedHDR(struct exynos_image &src, struct exynos_image &dst)
325 {
326 
327     uint32_t srcStandard = (src.dataSpace & HAL_DATASPACE_STANDARD_MASK);
328     uint32_t dstStandard = (dst.dataSpace & HAL_DATASPACE_STANDARD_MASK);
329     uint32_t srcTransfer = (src.dataSpace & HAL_DATASPACE_TRANSFER_MASK);
330     uint32_t dstTransfer = (dst.dataSpace & HAL_DATASPACE_TRANSFER_MASK);
331 
332     if (hasHdr10Plus(src) || hasHdrInfo(src) ) {
333         if (mAttr & MPP_ATTR_HDR10PLUS)
334             return true;
335         else if ((srcStandard == dstStandard) && (srcTransfer == dstTransfer))
336             return true;
337         else if ((mLogicalType == MPP_LOGICAL_G2D_COMBO) && (mPreAssignDisplayInfo & HWC_DISPLAY_VIRTUAL_BIT))
338             return true;
339         else
340             return false;
341     }
342     return true;
343 }
344 
isSupportedHStrideCrop(struct exynos_image __unused & src)345 bool ExynosMPP::isSupportedHStrideCrop(struct exynos_image __unused &src)
346 {
347     return true;
348 }
349 
isSupportedBlend(struct exynos_image & src)350 bool ExynosMPP::isSupportedBlend(struct exynos_image &src)
351 {
352     switch(src.blending) {
353     case HWC2_BLEND_MODE_NONE:
354     case HWC2_BLEND_MODE_PREMULTIPLIED:
355     case HWC2_BLEND_MODE_COVERAGE:
356         return true;
357     default:
358         return false;
359     }
360 }
361 
checkRotationCondition(struct exynos_image & src)362 bool ExynosMPP::checkRotationCondition(struct exynos_image &src)
363 {
364     /* Check only DPP types */
365     if (mPhysicalType >= MPP_DPP_NUM)
366         return true;
367 
368     /* If DPP has their own restriction, implmemnt module codes */
369     if (mAttr & MPP_ATTR_ROT_90) {
370         if (isFormatYUV420(src.format) == true)
371             return true;
372     }
373 
374     /* Other DPPs */
375     if ((src.transform & HAL_TRANSFORM_ROT_90) == 0)
376     {
377         if ((src.compressionInfo.type == COMP_TYPE_AFBC) && (src.transform != 0)) return false;
378         return true;
379     } else {
380         return false;
381     }
382 
383     return true;
384 }
385 
isSupportedTransform(struct exynos_image & src)386 bool ExynosMPP::isSupportedTransform(struct exynos_image &src)
387 {
388     if (src.transform == 0) return true;
389 
390     /* If MPP need to check additional condition,
391      * implement checkRotationCondition function to check it */
392     /* For example, DPP need to check custom conditons */
393     if (!checkRotationCondition(src))
394         return false;
395 
396     for(auto transform_map : transform_map_table) {
397         if (src.transform & transform_map.hal_tr) {
398             if (!(mAttr & transform_map.hwc_tr))
399                 return false;
400         }
401     }
402 
403     return true;
404 }
405 
isSupportedCompression(struct exynos_image & src)406 bool ExynosMPP::isSupportedCompression(struct exynos_image &src)
407 {
408     if (src.compressionInfo.type == COMP_TYPE_AFBC) {
409         if (mAttr & MPP_ATTR_AFBC)
410             return true;
411         else
412             return false;
413     }
414 
415     return true;
416 }
417 
isSupportedCapability(ExynosDisplay & display,struct exynos_image & src)418 bool ExynosMPP::isSupportedCapability(ExynosDisplay &display, struct exynos_image &src)
419 {
420     if (display.mType != HWC_DISPLAY_EXTERNAL)
421         return true;
422 
423     if (!(mAttr & MPP_ATTR_USE_CAPA))
424         return true;
425 
426     if (mResourceManager->hasHdrLayer || mResourceManager->hasDrmLayer) {
427         if (getDrmMode(src.usageFlags) != NO_DRM)
428             return true;
429         else if (hasHdrInfo(src))
430             return true;
431         else
432             return false;
433     }
434 
435     return true;
436 }
437 
isSupportedDRM(struct exynos_image & src)438 bool ExynosMPP::isSupportedDRM(struct exynos_image &src)
439 {
440     if (getDrmMode(src.usageFlags) == NO_DRM)
441         return true;
442 
443     if (mLogicalType == MPP_LOGICAL_G2D_RGB)
444         return false;
445 
446     return true;
447 }
448 
checkCSCRestriction(struct exynos_image & src,struct exynos_image & dst)449 bool ExynosMPP::checkCSCRestriction(struct exynos_image &src, struct exynos_image &dst)
450 {
451     return true;
452 }
453 
isDimLayerSupported()454 bool ExynosMPP::isDimLayerSupported()
455 {
456     if (mAttr & MPP_ATTR_DIM)
457         return true;
458 
459     return false;
460 }
461 
isSrcFormatSupported(struct exynos_image & src)462 bool ExynosMPP::isSrcFormatSupported(struct exynos_image &src)
463 {
464     if (mLogicalType == MPP_LOGICAL_G2D_YUV) {
465         /* Support YUV layer and HDR RGB layer */
466         if (isFormatRgb(src.format) && (hasHdrInfo(src) == false))
467             return false;
468     }
469     if ((mLogicalType == MPP_LOGICAL_G2D_RGB) &&
470         isFormatYUV(src.format))
471         return false;
472     if ((mLogicalType == MPP_LOGICAL_MSC_YUV) &&
473         isFormatRgb(src.format)) {
474         return false;
475     }
476 
477     if (mResourceManager == NULL) return false;
478 
479     for (uint32_t i = 0 ; i < mResourceManager->mFormatRestrictionCnt; i++) {
480         if ((mResourceManager->mFormatRestrictions[i].hwType == mPhysicalType) &&
481                 ((mResourceManager->mFormatRestrictions[i].nodeType == NODE_NONE) ||
482                  (mResourceManager->mFormatRestrictions[i].nodeType == NODE_SRC)) &&
483                 (mResourceManager->mFormatRestrictions[i].format == src.format))
484             return true;
485     }
486 
487     return false;
488 }
489 
isDstFormatSupported(struct exynos_image & dst)490 bool ExynosMPP::isDstFormatSupported(struct exynos_image &dst)
491 {
492 
493     for (uint32_t i = 0 ; i < mResourceManager->mFormatRestrictionCnt; i++) {
494         if ((mResourceManager->mFormatRestrictions[i].hwType == mPhysicalType) &&
495                 ((mResourceManager->mFormatRestrictions[i].nodeType == NODE_NONE) ||
496                  (mResourceManager->mFormatRestrictions[i].nodeType == NODE_DST)) &&
497                 (mResourceManager->mFormatRestrictions[i].format == dst.format))
498             return true;
499     }
500 
501     return false;
502 }
503 
getMaxUpscale(const struct exynos_image & src,const struct exynos_image __unused & dst) const504 uint32_t ExynosMPP::getMaxUpscale(const struct exynos_image &src,
505                                   const struct exynos_image __unused &dst) const {
506     uint32_t idx = getRestrictionClassification(src);
507     return mSrcSizeRestrictions[idx].maxUpScale;
508 }
509 
checkDownscaleCap(const float resolution,const float displayRatio_V) const510 bool ExynosMPP::checkDownscaleCap(const float resolution, const float displayRatio_V) const {
511     if (mPhysicalType >= MPP_DPP_NUM) return true;
512 
513     return float(mClockKhz) >= ((resolution * VPP_RESOL_MARGIN) / (mPPC * displayRatio_V));
514 }
515 
getDownscaleRestriction(const struct exynos_image & src,const struct exynos_image &) const516 uint32_t ExynosMPP::getDownscaleRestriction(const struct exynos_image &src,
517                                             const struct exynos_image & /*dst*/) const {
518     auto idx = getRestrictionClassification(src);
519     return mDstSizeRestrictions[idx].maxDownScale;
520 }
521 
getMaxDownscale(const ExynosDisplay & display,const struct exynos_image & src,const struct exynos_image & dst) const522 uint32_t ExynosMPP::getMaxDownscale(const ExynosDisplay &display, const struct exynos_image &src,
523                                     const struct exynos_image &dst) const {
524     uint32_t maxDownscale = getDownscaleRestriction(src, dst);
525 
526     if (maxDownscale <= 1) {
527         return maxDownscale;
528     }
529 
530     if (mPhysicalType < MPP_DPP_NUM) {
531         float resolution = float(src.w) * float(src.h) * display.getBtsRefreshRate() / 1000;
532         if (!checkDownscaleCap(resolution, float(dst.h) / float(display.mYres))) {
533             return 1;
534         }
535     }
536 
537     return maxDownscale;
538 }
539 
getSrcXOffsetAlign(struct exynos_image & src)540 uint32_t ExynosMPP::getSrcXOffsetAlign(struct exynos_image &src)
541 {
542     /* Refer module(ExynosMPPModule) for chip specific restrictions */
543     uint32_t idx = getRestrictionClassification(src);
544     if ((mPhysicalType == MPP_MSC) &&
545             ((src.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B) ||
546             (src.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B))) {
547         return 16;
548     }
549     return mSrcSizeRestrictions[idx].cropXAlign;
550 }
getSrcXOffsetAlign(uint32_t idx)551 uint32_t ExynosMPP::getSrcXOffsetAlign(uint32_t idx)
552 {
553     if (idx >= RESTRICTION_MAX)
554     {
555         MPP_LOGE("invalid idx: %d", idx);
556         return 16;
557     }
558     return mSrcSizeRestrictions[idx].cropXAlign;
559 }
getSrcYOffsetAlign(struct exynos_image & src)560 uint32_t ExynosMPP::getSrcYOffsetAlign(struct exynos_image &src)
561 {
562     uint32_t idx = getRestrictionClassification(src);
563     return mSrcSizeRestrictions[idx].cropYAlign;
564 }
getSrcYOffsetAlign(uint32_t idx)565 uint32_t ExynosMPP::getSrcYOffsetAlign(uint32_t idx)
566 {
567     if (idx >= RESTRICTION_MAX)
568     {
569         MPP_LOGE("invalid idx: %d", idx);
570         return 16;
571     }
572     return mSrcSizeRestrictions[idx].cropYAlign;
573 }
getSrcWidthAlign(struct exynos_image & src)574 uint32_t ExynosMPP::getSrcWidthAlign(struct exynos_image &src)
575 {
576     uint32_t idx = getRestrictionClassification(src);
577     return mSrcSizeRestrictions[idx].fullWidthAlign;
578 }
getSrcHeightAlign(struct exynos_image & src)579 uint32_t ExynosMPP::getSrcHeightAlign(struct exynos_image &src)
580 {
581     uint32_t idx = getRestrictionClassification(src);
582     return mSrcSizeRestrictions[idx].fullHeightAlign;
583 }
getSrcMaxWidth(struct exynos_image & src)584 uint32_t ExynosMPP::getSrcMaxWidth(struct exynos_image &src)
585 {
586     if (isFormatYUV(src.format))
587         return 4096;
588 
589     uint32_t idx = getRestrictionClassification(src);
590     return mSrcSizeRestrictions[idx].maxFullWidth;
591 }
getSrcMaxHeight(struct exynos_image & src)592 uint32_t ExynosMPP::getSrcMaxHeight(struct exynos_image &src)
593 {
594     if (isFormatYUV(src.format))
595         return 4096;
596 
597     uint32_t idx = getRestrictionClassification(src);
598     return mSrcSizeRestrictions[idx].maxFullHeight;
599 }
getSrcMinWidth(struct exynos_image & src)600 uint32_t ExynosMPP::getSrcMinWidth(struct exynos_image &src)
601 {
602     uint32_t idx = getRestrictionClassification(src);
603     return mSrcSizeRestrictions[idx].minFullWidth;
604 }
getSrcMinWidth(uint32_t idx)605 uint32_t ExynosMPP::getSrcMinWidth(uint32_t idx)
606 {
607     if (idx >= RESTRICTION_MAX)
608     {
609         MPP_LOGE("invalid idx: %d", idx);
610         return 16;
611     }
612     return mSrcSizeRestrictions[idx].minFullWidth;
613 }
getSrcMinHeight(struct exynos_image & src)614 uint32_t ExynosMPP::getSrcMinHeight(struct exynos_image &src)
615 {
616     uint32_t idx = getRestrictionClassification(src);
617     return mSrcSizeRestrictions[idx].minFullHeight;
618 }
getSrcMinHeight(uint32_t idx)619 uint32_t ExynosMPP::getSrcMinHeight(uint32_t idx)
620 {
621     if (idx >= RESTRICTION_MAX)
622     {
623         MPP_LOGE("invalid idx: %d", idx);
624         return 16;
625     }
626     return mSrcSizeRestrictions[idx].minFullHeight;
627 }
getSrcMaxCropWidth(struct exynos_image & src)628 uint32_t ExynosMPP::getSrcMaxCropWidth(struct exynos_image &src)
629 {
630     uint32_t idx = getRestrictionClassification(src);
631     return mSrcSizeRestrictions[idx].maxCropWidth;
632 }
getSrcMaxCropHeight(struct exynos_image & src)633 uint32_t ExynosMPP::getSrcMaxCropHeight(struct exynos_image &src)
634 {
635     if ((mMPPType == MPP_TYPE_OTF) &&
636         (src.transform & HAL_TRANSFORM_ROT_90))
637         return 2160;
638 
639     uint32_t idx = getRestrictionClassification(src);
640     return mSrcSizeRestrictions[idx].maxCropHeight;
641 }
getSrcMaxCropSize(struct exynos_image & src)642 uint32_t ExynosMPP::getSrcMaxCropSize(struct exynos_image &src)
643 {
644     return (getSrcMaxCropWidth(src) * getSrcMaxCropHeight(src));
645 }
getSrcMinCropWidth(struct exynos_image & src)646 uint32_t ExynosMPP::getSrcMinCropWidth(struct exynos_image &src)
647 {
648     if (((src.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B) ||
649          (src.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B)) &&
650         (mPhysicalType == MPP_G2D))
651         return 2;
652     uint32_t idx = getRestrictionClassification(src);
653     return mSrcSizeRestrictions[idx].minCropWidth;
654 }
getSrcMinCropHeight(struct exynos_image & src)655 uint32_t ExynosMPP::getSrcMinCropHeight(struct exynos_image &src)
656 {
657     if (((src.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B) ||
658          (src.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B)) &&
659         (mPhysicalType == MPP_G2D))
660         return 2;
661     uint32_t idx = getRestrictionClassification(src);
662     return mSrcSizeRestrictions[idx].minCropHeight;
663 }
getSrcCropWidthAlign(const struct exynos_image & src) const664 uint32_t ExynosMPP::getSrcCropWidthAlign(const struct exynos_image &src) const {
665     if (((src.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B) ||
666          (src.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B)) &&
667         (mPhysicalType == MPP_G2D))
668         return 2;
669     uint32_t idx = getRestrictionClassification(src);
670     return mSrcSizeRestrictions[idx].cropWidthAlign;
671 }
672 
673 /* This is used for only otfMPP */
getSrcCropWidthAlign(uint32_t idx) const674 uint32_t ExynosMPP::getSrcCropWidthAlign(uint32_t idx) const {
675     if (idx >= RESTRICTION_MAX)
676     {
677         MPP_LOGE("invalid idx: %d", idx);
678         return 16;
679     }
680     return mSrcSizeRestrictions[idx].cropWidthAlign;
681 }
getSrcCropHeightAlign(const struct exynos_image & src) const682 uint32_t ExynosMPP::getSrcCropHeightAlign(const struct exynos_image &src) const {
683     if (((src.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B) ||
684          (src.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B)) &&
685         (mPhysicalType == MPP_G2D))
686         return 2;
687 
688     uint32_t idx = getRestrictionClassification(src);
689     return mSrcSizeRestrictions[idx].cropHeightAlign;
690 }
691 
692 /* This is used for only otfMPP */
getSrcCropHeightAlign(uint32_t idx) const693 uint32_t ExynosMPP::getSrcCropHeightAlign(uint32_t idx) const {
694     if (idx >= RESTRICTION_MAX)
695     {
696         MPP_LOGE("invalid idx: %d", idx);
697         return 16;
698     }
699     return mSrcSizeRestrictions[idx].cropHeightAlign;
700 }
getDstMaxWidth(struct exynos_image & dst)701 uint32_t ExynosMPP::getDstMaxWidth(struct exynos_image &dst)
702 {
703     uint32_t idx = getRestrictionClassification(dst);
704     return mDstSizeRestrictions[idx].maxCropWidth;
705 }
getDstMaxHeight(struct exynos_image & dst)706 uint32_t ExynosMPP::getDstMaxHeight(struct exynos_image &dst)
707 {
708     uint32_t idx = getRestrictionClassification(dst);
709     return mDstSizeRestrictions[idx].maxCropHeight;
710 }
getDstMinWidth(struct exynos_image & dst)711 uint32_t ExynosMPP::getDstMinWidth(struct exynos_image &dst)
712 {
713     if (((dst.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B) ||
714          (dst.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B)) &&
715         (mPhysicalType == MPP_G2D))
716         return 64;
717 
718     if ((mNeedSolidColorLayer == false) && mNeedCompressedTarget)
719         return 16;
720 
721     if ((mPhysicalType == MPP_G2D) && (mNeedSolidColorLayer == false) &&
722             isFormatSBWC(dst.format))
723         return 32;
724 
725     uint32_t idx = getRestrictionClassification(dst);
726     return mDstSizeRestrictions[idx].minCropWidth;
727 }
getDstMinHeight(struct exynos_image & dst)728 uint32_t ExynosMPP::getDstMinHeight(struct exynos_image &dst)
729 {
730     if ((mNeedSolidColorLayer == false) && mNeedCompressedTarget)
731         return 16;
732 
733     if ((mPhysicalType == MPP_G2D) && (mNeedSolidColorLayer == false) &&
734             isFormatSBWC(dst.format))
735         return 8;
736 
737     uint32_t idx = getRestrictionClassification(dst);
738     return mDstSizeRestrictions[idx].minCropHeight;
739 }
getDstWidthAlign(const struct exynos_image & dst) const740 uint32_t ExynosMPP::getDstWidthAlign(const struct exynos_image &dst) const {
741     if (((dst.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B) ||
742          (dst.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B)) &&
743         (mPhysicalType == MPP_G2D))
744         return 64;
745 
746     if ((mNeedSolidColorLayer == false) && mNeedCompressedTarget)
747         return 16;
748 
749     if ((mPhysicalType == MPP_G2D) && (mNeedSolidColorLayer == false) &&
750             isFormatSBWC(dst.format))
751         return 32;
752 
753     uint32_t idx = getRestrictionClassification(dst);
754     return mDstSizeRestrictions[idx].cropWidthAlign;
755 }
getDstHeightAlign(const struct exynos_image & dst) const756 uint32_t ExynosMPP::getDstHeightAlign(const struct exynos_image &dst) const {
757     if ((mNeedSolidColorLayer == false) && mNeedCompressedTarget)
758         return 16;
759 
760     if ((mPhysicalType == MPP_G2D) && (mNeedSolidColorLayer == false) &&
761             isFormatSBWC(dst.format))
762         return 8;
763 
764     uint32_t idx = getRestrictionClassification(dst);
765     return mDstSizeRestrictions[idx].cropHeightAlign;
766 }
getDstXOffsetAlign(struct exynos_image & dst)767 uint32_t ExynosMPP::getDstXOffsetAlign(struct exynos_image &dst)
768 {
769     if ((mNeedSolidColorLayer == false) && mNeedCompressedTarget)
770         return 16;
771 
772     if ((mPhysicalType == MPP_G2D) && (mNeedSolidColorLayer == false) &&
773             isFormatSBWC(dst.format))
774         return 32;
775 
776     uint32_t idx = getRestrictionClassification(dst);
777     return mDstSizeRestrictions[idx].cropXAlign;
778 }
getDstYOffsetAlign(struct exynos_image & dst)779 uint32_t ExynosMPP::getDstYOffsetAlign(struct exynos_image &dst)
780 {
781     if ((mNeedSolidColorLayer == false) && mNeedCompressedTarget)
782         return 16;
783 
784     if ((mPhysicalType == MPP_G2D) && (mNeedSolidColorLayer == false) &&
785             isFormatSBWC(dst.format))
786         return 8;
787 
788     uint32_t idx = getRestrictionClassification(dst);
789     return mDstSizeRestrictions[idx].cropYAlign;
790 }
getOutBufAlign()791 uint32_t ExynosMPP::getOutBufAlign()
792 {
793     if (mNeedCompressedTarget)
794         return 16;
795     else
796         return 1;
797 }
798 
isSupportLayerColorTransform(struct exynos_image & src,struct exynos_image __unused & dst)799 int32_t ExynosMPP::isSupportLayerColorTransform(
800         struct exynos_image &src, struct exynos_image __unused &dst)
801 {
802     if (src.needColorTransform == false)
803         return true;
804 
805     if (mAttr & MPP_ATTR_LAYER_TRANSFORM)
806         return true;
807 
808     return false;
809 }
810 
threadLoop()811 bool ExynosMPP::ResourceManageThread::threadLoop()
812 {
813     if (mExynosMPP == NULL)
814         return false;
815 
816     ALOGI("%s threadLoop is started", mExynosMPP->mName.c_str());
817     while(mRunning) {
818         Mutex::Autolock lock(mMutex);
819         while((mFreedBuffers.size() == 0) &&
820                 (mStateFences.size() == 0)) {
821             mCondition.wait(mMutex);
822         }
823 
824         if ((mExynosMPP->mHWState == MPP_HW_STATE_RUNNING) &&
825                 (mStateFences.size() != 0)) {
826             if (checkStateFences()) {
827                 mExynosMPP->mHWState = MPP_HW_STATE_IDLE;
828             }
829         } else {
830             if ((mStateFences.size() != 0) &&
831                     (mExynosMPP->mHWState != MPP_HW_STATE_RUNNING)) {
832                 ALOGW("%s, mHWState(%d) but mStateFences size(%zu)",
833                         mExynosMPP->mName.c_str(), mExynosMPP->mHWState,
834                         mStateFences.size());
835                 checkStateFences();
836             }
837         }
838 
839         if (mFreedBuffers.size() != 0) {
840             freeBuffers();
841         }
842     }
843     return true;
844 }
845 
freeBuffers()846 void ExynosMPP::ResourceManageThread::freeBuffers()
847 {
848     VendorGraphicBufferAllocator& gAllocator(VendorGraphicBufferAllocator::get());
849     android::List<exynos_mpp_img_info >::iterator it;
850     android::List<exynos_mpp_img_info >::iterator end;
851     it = mFreedBuffers.begin();
852     end = mFreedBuffers.end();
853 
854     uint32_t freebufNum = 0;
855     while (it != end) {
856         exynos_mpp_img_info freeBuffer = (exynos_mpp_img_info)(*it);
857         HDEBUGLOGD(eDebugMPP|eDebugFence|eDebugBuf, "freebufNum: %d, buffer: %p", freebufNum, freeBuffer.bufferHandle);
858         dumpExynosMPPImgInfo(eDebugMPP|eDebugFence|eDebugBuf, freeBuffer);
859         if (fence_valid(freeBuffer.acrylicAcquireFenceFd)) {
860             if (sync_wait(freeBuffer.acrylicAcquireFenceFd, 1000) < 0)
861                 HWC_LOGE(NULL, "%s:: acquire fence sync_wait error", mExynosMPP->mName.c_str());
862             freeBuffer.acrylicAcquireFenceFd =
863                 fence_close(freeBuffer.acrylicAcquireFenceFd, mExynosMPP->mAssignedDisplay,
864                         FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_ALL);
865         }
866         if (fence_valid(freeBuffer.acrylicReleaseFenceFd)) {
867             if (sync_wait(freeBuffer.acrylicReleaseFenceFd, 1000) < 0)
868                 HWC_LOGE(NULL, "%s:: release fence sync_wait error", mExynosMPP->mName.c_str());
869             freeBuffer.acrylicReleaseFenceFd =
870                 fence_close(freeBuffer.acrylicReleaseFenceFd, mExynosMPP->mAssignedDisplay,
871                         FENCE_TYPE_SRC_RELEASE, FENCE_IP_ALL);
872         }
873         gAllocator.free(freeBuffer.bufferHandle);
874         it = mFreedBuffers.erase(it);
875     }
876 }
877 
checkStateFences()878 bool ExynosMPP::ResourceManageThread::checkStateFences()
879 {
880     bool ret = true;
881     android::List<int >::iterator it;
882     android::List<int >::iterator end;
883 
884     it = mStateFences.begin();
885     end = mStateFences.end();
886     uint32_t waitFenceNum = 0;
887     while (it != end) {
888         int fence = (int)(*it);
889         HDEBUGLOGD(eDebugMPP|eDebugFence, "%d wait fence: %d", waitFenceNum, fence);
890         waitFenceNum++;
891         if (fence_valid(fence)) {
892             if (sync_wait(fence, 5000) < 0) {
893                 HWC_LOGE(NULL, "%s::[%s][%d] sync_wait(%d) error(%s)", __func__,
894                         mExynosMPP->mName.c_str(), mExynosMPP->mLogicalIndex, fence, strerror(errno));
895                 ret = false;
896             }
897             fence = fence_close(fence, mExynosMPP->mAssignedDisplay,
898                     FENCE_TYPE_ALL, FENCE_IP_ALL);
899         }
900         it = mStateFences.erase(it);
901     }
902     return ret;
903 }
904 
addFreedBuffer(exynos_mpp_img_info freedBuffer)905 void ExynosMPP::ResourceManageThread::addFreedBuffer(exynos_mpp_img_info freedBuffer)
906 {
907     android::Mutex::Autolock lock(mMutex);
908     mFreedBuffers.push_back(freedBuffer);
909     mCondition.signal();
910 }
911 
addStateFence(int fence)912 void ExynosMPP::ResourceManageThread::addStateFence(int fence)
913 {
914     Mutex::Autolock lock(mMutex);
915     HDEBUGLOGD(eDebugMPP|eDebugFence, "wait fence is added: %d", fence);
916     mStateFences.push_back(fence);
917     mCondition.signal();
918 }
919 
920 /**
921  * @param w
922  * @param h
923  * @param color
924  * @param usage
925  * @return int32_t
926  */
allocOutBuf(uint32_t w,uint32_t h,uint32_t format,uint64_t usage,uint32_t index)927 int32_t ExynosMPP::allocOutBuf(uint32_t w, uint32_t h, uint32_t format, uint64_t usage, uint32_t index) {
928     ATRACE_CALL();
929     uint32_t dstStride = 0;
930 
931     MPP_LOGD(eDebugMPP|eDebugBuf, "index: %d++++++++", index);
932 
933     if (index >= NUM_MPP_DST_BUFS(mLogicalType)) {
934         return -EINVAL;
935     }
936 
937     exynos_mpp_img_info freeDstBuf = mDstImgs[index];
938     MPP_LOGD(eDebugMPP|eDebugBuf, "mDstImg[%d] is reallocated", index);
939     dumpExynosMPPImgInfo(eDebugMPP, mDstImgs[index]);
940 
941     uint64_t allocUsage = getBufferUsage(usage);
942     if (!needCompressDstBuf()) {
943         allocUsage |= VendorGraphicBufferUsage::NO_AFBC;
944     }
945     buffer_handle_t dstBuffer;
946 
947     MPP_LOGD(eDebugMPP|eDebugBuf, "\tw: %d, h: %d, format: 0x%8x, previousBuffer: %p, allocUsage: 0x%" PRIx64 ", usage: 0x%" PRIx64 "",
948             w, h, format, freeDstBuf.bufferHandle, allocUsage, usage);
949 
950     status_t error = NO_ERROR;
951 
952     {
953         ATRACE_CALL();
954 
955         VendorGraphicBufferAllocator& gAllocator(VendorGraphicBufferAllocator::get());
956         error = gAllocator.allocate(w, h, format, 1, allocUsage, &dstBuffer, &dstStride, "HWC");
957     }
958 
959     if ((error != NO_ERROR) || (dstBuffer == NULL)) {
960         MPP_LOGE("failed to allocate destination buffer(%dx%d): %d", w, h, error);
961         return -EINVAL;
962     }
963 
964     memset(&mDstImgs[index], 0, sizeof(mDstImgs[index]));
965 
966     mDstImgs[index].acrylicAcquireFenceFd = -1;
967     mDstImgs[index].acrylicReleaseFenceFd = -1;
968     mDstImgs[index].bufferHandle = dstBuffer;
969     mDstImgs[index].bufferType = getBufferType(usage);
970     mDstImgs[index].format = format;
971 
972     MPP_LOGD(eDebugMPP|eDebugBuf, "free outbuf[%d] %p", index, freeDstBuf.bufferHandle);
973 
974     if (freeDstBuf.bufferHandle != NULL) {
975         freeOutBuf(freeDstBuf);
976     } else {
977         if (mAssignedDisplay != NULL) {
978             freeDstBuf.acrylicAcquireFenceFd = fence_close(freeDstBuf.acrylicAcquireFenceFd,
979                     mAssignedDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_G2D);
980             freeDstBuf.acrylicReleaseFenceFd = fence_close(freeDstBuf.acrylicReleaseFenceFd,
981                     mAssignedDisplay, FENCE_TYPE_SRC_RELEASE, FENCE_IP_G2D);
982         }
983     }
984 
985     MPP_LOGD(eDebugMPP|eDebugBuf, "dstBuffer(%p)-----------", dstBuffer);
986 
987     return NO_ERROR;
988 }
989 
990 /**
991  * @param outbuf
992  * @return int32_t
993  */
setOutBuf(buffer_handle_t outbuf,int32_t fence)994 int32_t ExynosMPP::setOutBuf(buffer_handle_t outbuf, int32_t fence) {
995     mDstImgs[mCurrentDstBuf].bufferHandle = NULL;
996     if (outbuf != NULL) {
997         mDstImgs[mCurrentDstBuf].bufferHandle = outbuf;
998         mDstImgs[mCurrentDstBuf].format =
999             VendorGraphicBufferMeta::get_format(mDstImgs[mCurrentDstBuf].bufferHandle);
1000     }
1001     setDstAcquireFence(fence);
1002     return NO_ERROR;
1003 }
1004 
1005 /**
1006  * @param dst
1007  * @return int32_t
1008  */
freeOutBuf(struct exynos_mpp_img_info dst)1009 int32_t ExynosMPP::freeOutBuf(struct exynos_mpp_img_info dst) {
1010     mResourceManageThread->addFreedBuffer(dst);
1011     dst.bufferHandle = NULL;
1012     return NO_ERROR;
1013 }
1014 
getBufferType(uint64_t usage)1015 uint32_t ExynosMPP::getBufferType(uint64_t usage)
1016 {
1017     if (getDrmMode(usage) == SECURE_DRM)
1018         return MPP_BUFFER_SECURE_DRM;
1019     else if (getDrmMode(usage) == NORMAL_DRM)
1020         return MPP_BUFFER_NORMAL_DRM;
1021     else {
1022         if (exynosHWCControl.dumpMidBuf)
1023             return MPP_BUFFER_DUMP;
1024         else
1025             return MPP_BUFFER_NORMAL;
1026     }
1027 }
1028 
getBufferType(const buffer_handle_t handle)1029 uint32_t ExynosMPP::getBufferType(const buffer_handle_t handle)
1030 {
1031     uint64_t usage = VendorGraphicBufferMeta::get_usage(handle);
1032 
1033     return getBufferType(usage);
1034 }
1035 
getBufferUsage(uint64_t usage)1036 uint64_t ExynosMPP::getBufferUsage(uint64_t usage)
1037 {
1038     uint64_t allocUsage = 0;
1039     if (getBufferType(usage) == MPP_BUFFER_DUMP) {
1040         allocUsage = BufferUsage::CPU_READ_OFTEN |
1041             BufferUsage::CPU_WRITE_OFTEN;
1042     } else {
1043         allocUsage = BufferUsage::CPU_READ_NEVER |
1044             BufferUsage::CPU_WRITE_NEVER |
1045             VendorGraphicBufferUsage::NOZEROED |
1046             BufferUsage::COMPOSER_OVERLAY;
1047     }
1048 
1049     if (getDrmMode(usage) == SECURE_DRM) {
1050         allocUsage |= BufferUsage::PROTECTED;
1051         allocUsage &= ~VendorGraphicBufferUsage::PRIVATE_NONSECURE;
1052     } else if (getDrmMode(usage) == NORMAL_DRM) {
1053         allocUsage |= BufferUsage::PROTECTED;
1054         allocUsage |= VendorGraphicBufferUsage::PRIVATE_NONSECURE;
1055     }
1056 
1057     return allocUsage;
1058 }
1059 
needCompressDstBuf() const1060 bool ExynosMPP::needCompressDstBuf() const {
1061     return (mMaxSrcLayerNum > 1) && mNeedCompressedTarget;
1062 }
1063 
getAlignedDstFullWidth(struct exynos_image & dst)1064 uint32_t ExynosMPP::getAlignedDstFullWidth(struct exynos_image& dst) {
1065     return pixel_align(dst.fullWidth, getDstStrideAlignment(dst.format));
1066 }
1067 
needDstBufRealloc(struct exynos_image & dst,uint32_t index)1068 bool ExynosMPP::needDstBufRealloc(struct exynos_image &dst, uint32_t index)
1069 {
1070     MPP_LOGD(eDebugMPP|eDebugBuf, "index: %d++++++++", index);
1071 
1072     if (index >= NUM_MPP_DST_BUFS(mLogicalType)) {
1073         MPP_LOGE("%s:: index(%d) is not valid", __func__, index);
1074         return false;
1075     }
1076     buffer_handle_t dst_handle = NULL;
1077     if (mDstImgs[index].bufferHandle != NULL)
1078         dst_handle = mDstImgs[index].bufferHandle;
1079 
1080     if (dst_handle == NULL) {
1081         MPP_LOGD(eDebugMPP|eDebugBuf, "\tDstImag[%d]  handle is NULL", index);
1082         return true;
1083     }
1084 
1085     int32_t assignedDisplayType = -1;
1086     if (mAssignedDisplay != NULL) {
1087         assignedDisplayType = mAssignedDisplay->mType;
1088     } else {
1089         MPP_LOGE("%s:: mpp is not assigned", __func__);
1090         return false;
1091     }
1092 
1093     ExynosDisplay *prevAssignedDisplay = mDstImgs[index].assignedDisplay;
1094     if(prevAssignedDisplay == NULL) {
1095         MPP_LOGD(eDebugMPP|eDebugBuf, "\tDstImag[%d] prevAssignedDisplay is NULL", index);
1096         return true;
1097     }
1098 
1099     VendorGraphicBufferMeta gmeta(dst_handle);
1100 
1101     MPP_LOGD(eDebugMPP | eDebugBuf, "\tdst_handle(%p) afbc (%u) sbwc (%u) lossy (%u)", dst_handle,
1102              isAFBCCompressed(dst_handle), isFormatSBWC(gmeta.format), isFormatLossy(gmeta.format));
1103     MPP_LOGD(eDebugMPP | eDebugBuf,
1104              "\tAssignedDisplay[%d, %d] format[0x%8x, 0x%8x], bufferType[%d, %d], "
1105              "usageFlags: 0x%" PRIx64 ", need comp_type 0x%x lossy %u",
1106              mPrevAssignedDisplayType, assignedDisplayType, gmeta.format, dst.format,
1107              mDstImgs[index].bufferType, getBufferType(dst.usageFlags), dst.usageFlags,
1108              dst.compressionInfo.type, isFormatLossy(dst.format));
1109 
1110     bool realloc = (mPrevAssignedDisplayType != assignedDisplayType) ||
1111             (prevAssignedDisplay != mAssignedDisplay) ||
1112             (formatToBpp(gmeta.format) < formatToBpp(dst.format)) ||
1113             ((gmeta.stride * gmeta.vstride) <
1114              (int)(getAlignedDstFullWidth(dst) * dst.fullHeight)) ||
1115             (mDstImgs[index].bufferType != getBufferType(dst.usageFlags)) ||
1116             (isAFBCCompressed(dst_handle) != (dst.compressionInfo.type == COMP_TYPE_AFBC)) ||
1117             (isFormatSBWC(gmeta.format) != isFormatSBWC(dst.format)) ||
1118             (isFormatLossy(gmeta.format) != isFormatLossy(dst.format));
1119 
1120     MPP_LOGD(eDebugMPP|eDebugBuf, "realloc: %d--------", realloc);
1121     return realloc;
1122 }
1123 
canUsePrevFrame()1124 bool ExynosMPP::canUsePrevFrame()
1125 {
1126     if ((mAssignedDisplay && !mAssignedDisplay->mDisplayControl.skipM2mProcessing) ||
1127         !exynosHWCControl.skipM2mProcessing)
1128         return false;
1129 
1130     /* virtual display always require composition */
1131     if (mAllocOutBufFlag == false)
1132         return false;
1133 
1134     if (mPrevFrameInfo.srcNum != mAssignedSources.size())
1135         return false;
1136 
1137     for (uint32_t i = 0; i < mPrevFrameInfo.srcNum; i++) {
1138         if ((mPrevFrameInfo.srcInfo[i].bufferHandle != mAssignedSources[i]->mSrcImg.bufferHandle) ||
1139             (mPrevFrameInfo.srcInfo[i].x != mAssignedSources[i]->mSrcImg.x) ||
1140             (mPrevFrameInfo.srcInfo[i].y != mAssignedSources[i]->mSrcImg.y) ||
1141             (mPrevFrameInfo.srcInfo[i].w != mAssignedSources[i]->mSrcImg.w) ||
1142             (mPrevFrameInfo.srcInfo[i].h != mAssignedSources[i]->mSrcImg.h) ||
1143             (mPrevFrameInfo.srcInfo[i].format != mAssignedSources[i]->mSrcImg.format) ||
1144             (mPrevFrameInfo.srcInfo[i].usageFlags != mAssignedSources[i]->mSrcImg.usageFlags) ||
1145             (mPrevFrameInfo.srcInfo[i].dataSpace != mAssignedSources[i]->mSrcImg.dataSpace) ||
1146             (mPrevFrameInfo.srcInfo[i].blending != mAssignedSources[i]->mSrcImg.blending) ||
1147             (mPrevFrameInfo.srcInfo[i].transform != mAssignedSources[i]->mSrcImg.transform) ||
1148             (mPrevFrameInfo.srcInfo[i].compressionInfo.type !=
1149              mAssignedSources[i]->mSrcImg.compressionInfo.type) ||
1150             (mPrevFrameInfo.srcInfo[i].planeAlpha != mAssignedSources[i]->mSrcImg.planeAlpha) ||
1151             (mPrevFrameInfo.dstInfo[i].x != mAssignedSources[i]->mMidImg.x) ||
1152             (mPrevFrameInfo.dstInfo[i].y != mAssignedSources[i]->mMidImg.y) ||
1153             (mPrevFrameInfo.dstInfo[i].w != mAssignedSources[i]->mMidImg.w) ||
1154             (mPrevFrameInfo.dstInfo[i].h != mAssignedSources[i]->mMidImg.h) ||
1155             (mPrevFrameInfo.dstInfo[i].format != mAssignedSources[i]->mMidImg.format))
1156             return false;
1157     }
1158 
1159    int32_t prevDstIndex  = (mCurrentDstBuf + NUM_MPP_DST_BUFS(mLogicalType) - 1)% NUM_MPP_DST_BUFS(mLogicalType);
1160    if (mDstImgs[prevDstIndex].bufferHandle == NULL)
1161        return false;
1162 
1163     return true;
1164 }
1165 
setupLayer(exynos_mpp_img_info * srcImgInfo,struct exynos_image & src,struct exynos_image & dst)1166 int32_t ExynosMPP::setupLayer(exynos_mpp_img_info *srcImgInfo, struct exynos_image &src, struct exynos_image &dst)
1167 {
1168     int ret = NO_ERROR;
1169 
1170     if (srcImgInfo->mppLayer == NULL) {
1171         if ((srcImgInfo->mppLayer = mAcrylicHandle->createLayer()) == NULL)
1172         {
1173             MPP_LOGE("%s:: Fail to create layer", __func__);
1174             return -EINVAL;
1175         }
1176     }
1177 
1178     if (src.bufferHandle == NULL) {
1179         MPP_LOGE("%s:: Invalid source handle", __func__);
1180         return -EINVAL;
1181     }
1182 
1183     buffer_handle_t srcHandle = NULL;
1184     if (src.bufferHandle != NULL)
1185         srcHandle = src.bufferHandle;
1186 
1187     VendorGraphicBufferMeta gmeta(srcHandle);
1188     int bufFds[MAX_HW2D_PLANES];
1189     size_t bufLength[MAX_HW2D_PLANES];
1190     uint32_t attribute = 0;
1191     uint32_t bufferNum = getBufferNumOfFormat(gmeta.format, getCompressionType(srcHandle));
1192     android_dataspace_t dataspace = src.dataSpace;
1193     if (dataspace == HAL_DATASPACE_UNKNOWN)
1194     {
1195         if (isFormatRgb(gmeta.format))
1196             dataspace = HAL_DATASPACE_V0_SRGB;
1197         else
1198             dataspace = HAL_DATASPACE_V0_BT601_625;
1199     }
1200 
1201     if (bufferNum == 0)
1202     {
1203         MPP_LOGE("%s:: Fail to get bufferNum(%d), format(0x%8x, afbc %d)", __func__, bufferNum,
1204                  gmeta.format, isAFBCCompressed(srcHandle));
1205         return -EINVAL;
1206     }
1207     bufFds[0] = gmeta.fd;
1208     bufFds[1] = gmeta.fd1;
1209     bufFds[2] = gmeta.fd2;
1210     if (getBufLength(srcHandle, MAX_HW2D_PLANES, bufLength, gmeta.format, src.fullWidth, src.fullHeight) != NO_ERROR) {
1211         MPP_LOGE("%s:: invalid bufferLength(%zu, %zu, %zu), format(0x%8x)", __func__,
1212                 bufLength[0], bufLength[1], bufLength[2], gmeta.format);
1213         return -EINVAL;
1214     }
1215 
1216     /* HDR process */
1217     if (hasHdrInfo(src)) {
1218         unsigned int max = (src.metaParcel.sHdrStaticInfo.sType1.mMaxDisplayLuminance/10000);
1219         unsigned int min = src.metaParcel.sHdrStaticInfo.sType1.mMinDisplayLuminance;
1220 
1221         srcImgInfo->mppLayer->setMasterDisplayLuminance(min,max);
1222         MPP_LOGD(eDebugMPP, "HWC2: G2D luminance min %d, max %d", min, max);
1223         MPP_LOGD(eDebugMPP|eDebugFence, "G2D getting HDR source!");
1224 
1225         srcImgInfo->mppLayer->setLayerHDR(true);
1226     } else
1227         srcImgInfo->mppLayer->setLayerHDR(false);
1228 
1229     /* Transfer MetaData */
1230     if (src.hasMetaParcel) {
1231         srcImgInfo->mppLayer->setLayerData(&src.metaParcel, sizeof(src.metaParcel));
1232     }
1233 
1234     srcImgInfo->bufferType = getBufferType(srcHandle);
1235     if (srcImgInfo->bufferType == MPP_BUFFER_SECURE_DRM)
1236         attribute |= AcrylicCanvas::ATTR_PROTECTED;
1237     /*Change AFBC attribute on the basis of the modifier*/
1238     if (src.compressionInfo.type == COMP_TYPE_AFBC) {
1239         if ((src.compressionInfo.modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) ==
1240             AFBC_FORMAT_MOD_BLOCK_SIZE_32x8) {
1241             attribute |= AcrylicCanvas::ATTR_COMPRESSED_WIDEBLK;
1242         } else {
1243             attribute |= AcrylicCanvas::ATTR_COMPRESSED;
1244         }
1245     }
1246 
1247     srcImgInfo->bufferHandle = srcHandle;
1248     srcImgInfo->acrylicAcquireFenceFd =
1249         hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_G2D, src.acquireFenceFd);
1250 
1251     MPP_LOGD(eDebugMPP|eDebugFence, "source configuration:");
1252     MPP_LOGD(eDebugMPP, "\tImageDimension[%d, %d], ImageType[0x%8x, 0x%8x]",
1253             src.fullWidth, src.fullHeight,
1254             gmeta.format, dataspace);
1255     MPP_LOGD(eDebugMPP|eDebugFence, "\tImageBuffer handle: %p, fds[%d, %d, %d], bufLength[%zu, %zu, %zu], bufferNum: %d, acquireFence: %d, attribute: %d",
1256             srcHandle, bufFds[0], bufFds[1], bufFds[2], bufLength[0], bufLength[1], bufLength[2],
1257             bufferNum, srcImgInfo->acrylicAcquireFenceFd, attribute);
1258     MPP_LOGD(eDebugMPP, "\tsrc_rect[%d, %d, %d, %d], dst_rect[%d, %d, %d, %d], transform(0x%4x)",
1259             (int)src.x, (int)src.y, (int)(src.x + src.w), (int)(src.y + src.h),
1260             (int)dst.x, (int)dst.y, (int)(dst.x + dst.w), (int)(dst.y + dst.h), src.transform);
1261 
1262     srcImgInfo->mppLayer->setImageDimension(src.fullWidth, src.fullHeight);
1263 
1264     if (gmeta.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV) {
1265         srcImgInfo->mppLayer->setImageType(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M, dataspace);
1266     } else {
1267         srcImgInfo->mppLayer->setImageType(gmeta.format, dataspace);
1268     }
1269 
1270     if (mPhysicalType == MPP_G2D) {
1271         setFenceName(srcImgInfo->acrylicAcquireFenceFd, FENCE_G2D_SRC_LAYER);
1272         setFenceInfo(srcImgInfo->acrylicAcquireFenceFd, mAssignedDisplay, FENCE_TYPE_SRC_ACQUIRE,
1273                      FENCE_IP_G2D, HwcFenceDirection::TO);
1274     } else if (mPhysicalType == MPP_MSC) {
1275         setFenceName(srcImgInfo->acrylicAcquireFenceFd, FENCE_MSC_SRC_LAYER);
1276         setFenceInfo(srcImgInfo->acrylicAcquireFenceFd, mAssignedDisplay, FENCE_TYPE_SRC_ACQUIRE,
1277                      FENCE_IP_MSC, HwcFenceDirection::TO);
1278     } else {
1279         MPP_LOGE("%s:: invalid mPhysicalType(%d)", __func__, mPhysicalType);
1280     }
1281 
1282     srcImgInfo->mppLayer->setImageBuffer(bufFds, bufLength, bufferNum,
1283             srcImgInfo->acrylicAcquireFenceFd, attribute);
1284 
1285     if (mMaxSrcLayerNum > 1) {
1286         srcImgInfo->mppLayer->setCompositMode(src.blending, (uint8_t)(255 * src.planeAlpha), src.zOrder);
1287     } else {
1288         srcImgInfo->mppLayer->setCompositMode(src.blending, 255, src.zOrder);
1289     }
1290 
1291     hwc_rect_t src_rect = {(int)src.x, (int)src.y, (int)(src.x + src.w), (int)(src.y + src.h)};
1292     hwc_rect_t dst_rect = {(int)dst.x, (int)dst.y, (int)(dst.x + dst.w), (int)(dst.y + dst.h)};
1293 
1294     if ((mAssignedDisplay != NULL) &&
1295         ((mAssignedDisplay->mType == HWC_DISPLAY_VIRTUAL) ||
1296          (mAssignedDisplay->mType == HWC_DISPLAY_EXTERNAL)))
1297         srcImgInfo->mppLayer->setCompositArea(src_rect, dst_rect, src.transform, AcrylicLayer::ATTR_NORESAMPLING);
1298     else {
1299         if(isFormatYUV(src.format))
1300             srcImgInfo->mppLayer->setCompositArea(src_rect, dst_rect, src.transform, AcrylicLayer::ATTR_NORESAMPLING);
1301         else
1302             srcImgInfo->mppLayer->setCompositArea(src_rect, dst_rect, src.transform);
1303     }
1304 
1305     srcImgInfo->acrylicAcquireFenceFd = -1;
1306     srcImgInfo->format = gmeta.format;
1307 
1308     if (gmeta.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV) {
1309         srcImgInfo->format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M;
1310     }
1311 
1312     return ret;
1313 }
1314 
getDstMetaInfo(android_dataspace_t dstDataspace)1315 dstMetaInfo_t ExynosMPP::getDstMetaInfo(android_dataspace_t dstDataspace)
1316 {
1317     dstMetaInfo_t metaInfo;
1318 
1319     if ((mAssignedSources.size() <= 1) &&
1320             (mAssignedSources[0]->mSrcImg.dataSpace == dstDataspace)) {
1321         metaInfo.minLuminance =
1322             (uint16_t)mAssignedSources[0]->mSrcImg.metaParcel.sHdrStaticInfo.sType1.mMinDisplayLuminance;
1323         metaInfo.maxLuminance =
1324             (uint16_t)(mAssignedSources[0]->mSrcImg.metaParcel.sHdrStaticInfo.sType1.mMaxDisplayLuminance/10000);
1325     } else {
1326         // minLuminance: 0.0001nit unit, maxLuminance: 1nit unit
1327         metaInfo.minLuminance = (uint16_t)(mAssignedDisplay->mMinLuminance * 10000);
1328         metaInfo.maxLuminance = (uint16_t)mAssignedDisplay->mMaxLuminance;
1329     }
1330 
1331     return metaInfo;
1332 }
1333 
getDstStrideAlignment(int format)1334 uint32_t ExynosMPP::getDstStrideAlignment(int format) {
1335     /* In cases of Single-FD format, stride alignment should be matched. */
1336     if (format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN)
1337         return 64;
1338     else if (format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_SPN)
1339         return 128;
1340     else
1341         return G2D_JUSTIFIED_DST_ALIGN;
1342 }
1343 
setupDst(exynos_mpp_img_info * dstImgInfo)1344 int32_t ExynosMPP::setupDst(exynos_mpp_img_info *dstImgInfo)
1345 {
1346     int ret = NO_ERROR;
1347     bool isComposition = (mMaxSrcLayerNum > 1);
1348     buffer_handle_t dstHandle = dstImgInfo->bufferHandle;
1349     int bufFds[MAX_HW2D_PLANES];
1350     size_t bufLength[MAX_HW2D_PLANES];
1351     uint32_t attribute = 0;
1352     uint32_t bufferNum = getBufferNumOfFormat(dstImgInfo->format, getCompressionType(dstHandle));
1353     if (bufferNum == 0)
1354     {
1355         MPP_LOGE("%s:: Fail to get bufferNum(%d), format(0x%8x, afbc %d)", __func__, bufferNum,
1356                  dstImgInfo->format, isAFBCCompressed(dstHandle));
1357         return -EINVAL;
1358     }
1359 
1360     android_dataspace_t dataspace = HAL_DATASPACE_UNKNOWN;
1361     VendorGraphicBufferMeta gmeta(dstHandle);
1362 
1363     if (isComposition) {
1364         if (isFormatRgb(dstImgInfo->format)) {
1365             if ((mAssignedDisplay != NULL) &&
1366                 (mAssignedDisplay->mColorMode != HAL_COLOR_MODE_NATIVE))
1367                 dataspace = colorModeToDataspace(mAssignedDisplay->mColorMode);
1368         } else {
1369             dataspace =
1370                 (android_dataspace)(HAL_DATASPACE_STANDARD_BT709 | HAL_DATASPACE_TRANSFER_GAMMA2_2 | HAL_DATASPACE_RANGE_LIMITED);
1371         }
1372     } else {
1373         dataspace = mAssignedSources[0]->mMidImg.dataSpace;
1374     }
1375 
1376     if (dataspace == HAL_DATASPACE_UNKNOWN)
1377     {
1378         if (isFormatRgb(dstImgInfo->format))
1379             dataspace = HAL_DATASPACE_V0_SRGB;
1380         else
1381             dataspace = HAL_DATASPACE_V0_BT601_625;
1382     }
1383 
1384     bufFds[0] = gmeta.fd;
1385     bufFds[1] = gmeta.fd1;
1386     bufFds[2] = gmeta.fd2;
1387     if (getBufLength(dstHandle, MAX_HW2D_PLANES, bufLength, dstImgInfo->format,
1388                 gmeta.stride, gmeta.vstride) != NO_ERROR) {
1389         MPP_LOGE("%s:: invalid bufferLength(%zu, %zu, %zu), format(0x%8x)", __func__,
1390                 bufLength[0], bufLength[1], bufLength[2], dstImgInfo->format);
1391         return -EINVAL;
1392     }
1393 
1394     dstImgInfo->bufferType = getBufferType(dstHandle);
1395     if (dstImgInfo->bufferType == MPP_BUFFER_SECURE_DRM)
1396         attribute |= AcrylicCanvas::ATTR_PROTECTED;
1397 
1398     dstImgInfo->assignedDisplay = mAssignedDisplay;
1399     if (mAssignedDisplay != NULL) {
1400         mAcrylicHandle->setCanvasDimension(pixel_align(mAssignedDisplay->mXres,
1401                                                        getDstStrideAlignment(dstImgInfo->format)),
1402                                            pixel_align(mAssignedDisplay->mYres,
1403                                                        G2D_JUSTIFIED_DST_ALIGN));
1404     }
1405 
1406     /* setup dst */
1407     if (needCompressDstBuf()) {
1408         attribute |= AcrylicCanvas::ATTR_COMPRESSED;
1409     }
1410 
1411     if (mPhysicalType == MPP_G2D) {
1412         setFenceName(dstImgInfo->acrylicAcquireFenceFd, FENCE_G2D_DST_DPP);
1413         /* Might be closed next frame */
1414         setFenceInfo(dstImgInfo->acrylicAcquireFenceFd, mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE,
1415                      FENCE_IP_G2D, HwcFenceDirection::TO);
1416     } else if (mPhysicalType == MPP_MSC) {
1417         setFenceName(dstImgInfo->acrylicAcquireFenceFd, FENCE_MSC_DST_DPP);
1418         /* Might be closed next frame */
1419         setFenceInfo(dstImgInfo->acrylicAcquireFenceFd, mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE,
1420                      FENCE_IP_MSC, HwcFenceDirection::TO);
1421     } else {
1422         MPP_LOGE("%s:: invalid mPhysicalType(%d)", __func__, mPhysicalType);
1423     }
1424 
1425     mAcrylicHandle->setCanvasImageType(dstImgInfo->format, dataspace);
1426 
1427     if ((mLogicalType == MPP_LOGICAL_G2D_COMBO) &&
1428             (mAssignedDisplay != NULL) &&
1429             (mAssignedDisplay->mType == HWC_DISPLAY_VIRTUAL) &&
1430             (((ExynosVirtualDisplay *)mAssignedDisplay)->mIsWFDState == (int)LLWFD)) {
1431         mAcrylicHandle->setCanvasImageType(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN, dataspace);
1432         dstImgInfo->acrylicAcquireFenceFd = fence_close(dstImgInfo->acrylicAcquireFenceFd,
1433                 mAssignedDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_G2D);
1434         mAcrylicHandle->setCanvasBuffer(bufFds, bufLength, bufferNum,
1435                 dstImgInfo->acrylicAcquireFenceFd, attribute);
1436         mAcrylicHandle->setCanvasOTF(attribute);
1437     }
1438     else {
1439         dstImgInfo->acrylicAcquireFenceFd =
1440             hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE, FENCE_IP_G2D, dstImgInfo->acrylicAcquireFenceFd);
1441         mAcrylicHandle->setCanvasBuffer(bufFds, bufLength, bufferNum,
1442                 dstImgInfo->acrylicAcquireFenceFd, attribute);
1443     }
1444 
1445     dstMetaInfo_t metaInfo = getDstMetaInfo(dataspace);
1446     if ((mAssignedDisplay != NULL) &&
1447         (mAssignedDisplay->mType != HWC_DISPLAY_VIRTUAL)) {
1448         mAcrylicHandle->setTargetDisplayLuminance(metaInfo.minLuminance, metaInfo.maxLuminance);
1449     }
1450 
1451     MPP_LOGD(eDebugMPP|eDebugFence, "destination configuration:");
1452     MPP_LOGD(eDebugMPP, "\tImageDimension[%d, %d], ImageType[0x%8x, %d], target luminance[%d, %d]",
1453             gmeta.stride, gmeta.vstride,
1454             dstImgInfo->format, dataspace, metaInfo.minLuminance, metaInfo.maxLuminance);
1455     MPP_LOGD(eDebugMPP|eDebugFence, "\tImageBuffer handle: %p, fds[%d, %d, %d], bufLength[%zu, %zu, %zu], bufferNum: %d, acquireFence: %d, attribute: %d",
1456             dstHandle, bufFds[0], bufFds[1], bufFds[2], bufLength[0], bufLength[1], bufLength[2],
1457             bufferNum, dstImgInfo->acrylicAcquireFenceFd, attribute);
1458 
1459 
1460     dstImgInfo->acrylicAcquireFenceFd = -1;
1461     dstImgInfo->dataspace = dataspace;
1462 
1463     return ret;
1464 }
1465 
doPostProcessingInternal()1466 int32_t ExynosMPP::doPostProcessingInternal()
1467 {
1468     ATRACE_CALL();
1469     int ret = NO_ERROR;
1470     size_t sourceNum = mAssignedSources.size();
1471 
1472     if (mAcrylicHandle == NULL) {
1473         MPP_LOGE("%s:: mAcrylicHandle is NULL", __func__);
1474         return -EINVAL;
1475     }
1476 
1477     /* setup source layers */
1478     for(size_t i = 0; i < sourceNum; i++) {
1479         MPP_LOGD(eDebugMPP|eDebugFence, "Setup [%zu] source: %p", i, mAssignedSources[i]);
1480         if ((ret = setupLayer(&mSrcImgs[i], mAssignedSources[i]->mSrcImg, mAssignedSources[i]->mMidImg)) != NO_ERROR) {
1481             MPP_LOGE("%s:: fail to setupLayer[%zu], ret %d",
1482                     __func__, i, ret);
1483             return ret;
1484         }
1485     }
1486 
1487     if ((ret = setColorConversionInfo()) != NO_ERROR) {
1488             MPP_LOGE("%s:: fail to setColorConversionInfo ret %d",
1489                     __func__, ret);
1490             return ret;
1491     }
1492 
1493     if (mPrevFrameInfo.srcNum > sourceNum) {
1494         MPP_LOGD(eDebugMPP, "prev sourceNum(%d), current sourceNum(%zu)",
1495                 mPrevFrameInfo.srcNum, sourceNum);
1496         for (size_t i = sourceNum; i < mPrevFrameInfo.srcNum; i++)
1497         {
1498             MPP_LOGD(eDebugMPP, "Remove mSrcImgs[%zu], %p", i, mSrcImgs[i].mppLayer);
1499             if (mSrcImgs[i].mppLayer != NULL) {
1500                 delete mSrcImgs[i].mppLayer;
1501                 mSrcImgs[i].mppLayer = NULL;
1502             }
1503         }
1504     }
1505 
1506     if (mAcrylicHandle->layerCount() != mAssignedSources.size()) {
1507         MPP_LOGE("Different layer number, acrylic layers(%d), assigned size(%zu)",
1508                 mAcrylicHandle->layerCount(), mAssignedSources.size());
1509         return -EINVAL;
1510     }
1511     MPP_LOGD(eDebugFence, "setupDst ++ mDstImgs[%d] acrylicAcquireFenceFd(%d)",
1512             mCurrentDstBuf, mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd);
1513 
1514     setupDst(&mDstImgs[mCurrentDstBuf]);
1515 
1516     MPP_LOGD(eDebugFence, "setupDst -- mDstImgs[%d] acrylicAcquireFenceFd(%d) closed",
1517             mCurrentDstBuf, mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd);
1518 
1519 
1520     int usingFenceCnt = 1;
1521     bool acrylicReturn = true;
1522 
1523 #ifndef DISABLE_FENCE
1524     if (mUseM2MSrcFence)
1525         usingFenceCnt = sourceNum + 1; // Get and Use src + dst fence
1526     else
1527         usingFenceCnt = 1;             // Get and Use only dst fence
1528     int *releaseFences = new int[usingFenceCnt];
1529     int dstBufIdx = usingFenceCnt - 1;
1530 #else
1531     usingFenceCnt = 0;                 // Get and Use no fences
1532     int dstBufIdx = 0;
1533     int *releaseFences = NULL;
1534 #endif
1535 
1536     acrylicReturn = mAcrylicHandle->execute(releaseFences, usingFenceCnt);
1537 
1538     if (acrylicReturn == false) {
1539         MPP_LOGE("%s:: fail to excute compositor", __func__);
1540         for(size_t i = 0; i < sourceNum; i++) {
1541             mSrcImgs[i].acrylicReleaseFenceFd = -1;
1542             MPP_LOGE("src[%zu]: ImageDimension[%d, %d], src_rect[%d, %d, %d, %d], dst_rect[%d, %d, %d, %d], transform(0x%4x)",
1543                     i,
1544                     mAssignedSources[i]->mSrcImg.fullWidth, mAssignedSources[i]->mSrcImg.fullHeight,
1545                     mAssignedSources[i]->mSrcImg.x, mAssignedSources[i]->mSrcImg.y,
1546                     mAssignedSources[i]->mSrcImg.x + mAssignedSources[i]->mSrcImg.w,
1547                     mAssignedSources[i]->mSrcImg.y + mAssignedSources[i]->mSrcImg.h,
1548                     mAssignedSources[i]->mMidImg.x, mAssignedSources[i]->mMidImg.y,
1549                     mAssignedSources[i]->mMidImg.x + mAssignedSources[i]->mMidImg.w,
1550                     mAssignedSources[i]->mMidImg.y + mAssignedSources[i]->mMidImg.h,
1551                     mAssignedSources[i]->mSrcImg.transform);
1552         }
1553         mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd = -1;
1554         ret = -EPERM;
1555     } else {
1556 
1557         // set fence informations from acryl
1558         if (mPhysicalType == MPP_G2D) {
1559             setFenceInfo(releaseFences[dstBufIdx], mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE,
1560                          FENCE_IP_G2D, HwcFenceDirection::FROM);
1561             if (usingFenceCnt > 1) {
1562                 for(size_t i = 0; i < sourceNum; i++) {
1563                     // TODO DPU release fence is tranferred to m2mMPP's source layer fence
1564                     setFenceInfo(releaseFences[i], mAssignedDisplay, FENCE_TYPE_SRC_RELEASE,
1565                                  FENCE_IP_G2D, HwcFenceDirection::FROM);
1566                 }
1567             }
1568         } else if (mPhysicalType == MPP_MSC) {
1569             setFenceInfo(releaseFences[dstBufIdx], mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE,
1570                          FENCE_IP_MSC, HwcFenceDirection::FROM);
1571             if (usingFenceCnt > 1) {
1572                 for(size_t i = 0; i < sourceNum; i++) {
1573                     // TODO DPU release fence is tranferred to m2mMPP's source layer fence
1574                     setFenceInfo(releaseFences[i], mAssignedDisplay, FENCE_TYPE_SRC_RELEASE,
1575                                  FENCE_IP_MSC, HwcFenceDirection::FROM);
1576                 }
1577             }
1578         } else {
1579             MPP_LOGE("%s:: invalid mPhysicalType(%d)", __func__, mPhysicalType);
1580         }
1581 
1582         if ((mLogicalType == MPP_LOGICAL_G2D_COMBO) &&
1583                 (mAssignedDisplay != NULL) &&
1584                 (mAssignedDisplay->mType == HWC_DISPLAY_VIRTUAL)) {
1585             if (((ExynosVirtualDisplay *)mAssignedDisplay)->mIsWFDState == (int)LLWFD) {
1586                 if (usingFenceCnt != 0) // Use no fences
1587                     releaseFences[dstBufIdx] = fence_close(releaseFences[dstBufIdx],
1588                             mAssignedDisplay, FENCE_TYPE_SRC_RELEASE, FENCE_IP_G2D); // Close dst buf's fence
1589             }
1590             if (mUseM2MSrcFence) {
1591                 if (((ExynosVirtualDisplay *)mAssignedDisplay)->mIsWFDState != (int)GOOGLEWFD) {
1592                     for (size_t i = 0; i < sourceNum; i++)
1593                         releaseFences[i] = fence_close(releaseFences[i],
1594                                 mAssignedDisplay, FENCE_TYPE_SRC_RELEASE, FENCE_IP_G2D);
1595                 }
1596             }
1597         }
1598 
1599         if (usingFenceCnt == 0) { // Use no fences
1600             for(size_t i = 0; i < sourceNum; i++) {
1601                 mSrcImgs[i].acrylicReleaseFenceFd = -1;
1602             }
1603             mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd = -1;
1604         } else {
1605             for(size_t i = 0; i < sourceNum; i++) {
1606                 if (mUseM2MSrcFence)
1607                     mSrcImgs[i].acrylicReleaseFenceFd =
1608                         hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_SRC_RELEASE, FENCE_IP_G2D, releaseFences[i]);
1609                 else
1610                     mSrcImgs[i].acrylicReleaseFenceFd = -1;
1611                 MPP_LOGD(eDebugFence, "mSrcImgs[%zu] acrylicReleaseFenceFd: %d",
1612                         i, mSrcImgs[i].acrylicReleaseFenceFd);
1613             }
1614 
1615             if (mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd >= 0) {
1616                 MPP_LOGE("mDstImgs[%d].acrylicReleaseFenceFd(%d) is not initialized",
1617                         mCurrentDstBuf,
1618                         mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd);
1619             }
1620 
1621             if (mPhysicalType == MPP_G2D)
1622                 mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd =
1623                     hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_DST_RELEASE, FENCE_IP_G2D, releaseFences[dstBufIdx]);
1624             else if (mPhysicalType == MPP_MSC)
1625                 mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd =
1626                     hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_DST_RELEASE, FENCE_IP_MSC, releaseFences[dstBufIdx]);
1627 
1628             MPP_LOGD(eDebugFence, "mDstImgs[%d] acrylicReleaseFenceFd: %d , releaseFences[%d]",
1629                     mCurrentDstBuf, mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd, dstBufIdx);
1630         }
1631 
1632         if (exynosHWCControl.dumpMidBuf) {
1633             ALOGI("dump image");
1634             exynosHWCControl.dumpMidBuf = false;
1635             if ((mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd > 0) &&
1636                 (sync_wait(mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd, 1000) < 0)) {
1637                 ALOGE("%s:: fence sync_wait error to dump image", __func__);
1638             } else {
1639                 buffer_handle_t dstHandle = mDstImgs[mCurrentDstBuf].bufferHandle;
1640                 VendorGraphicBufferMeta gmeta(dstHandle);
1641 
1642                 ALOGI("dump image fw: %d, fh:%d, size: %d", gmeta.stride, gmeta.vstride, gmeta.size);
1643                 FILE *fp;
1644                 fp = fopen(MPP_DUMP_PATH,"ab");
1645 
1646                 if (fp) {
1647                     void *temp = mmap(0, gmeta.size, PROT_READ|PROT_WRITE, MAP_SHARED, gmeta.fd, 0);
1648                     if (temp) {
1649                         ALOGI("write...%p", temp);
1650                         int write_size = fwrite(temp, gmeta.size, 1, fp);
1651                         if (write_size < 0) {
1652                             ALOGI("write error: %s", strerror(errno));
1653                         } else {
1654                             ALOGI("write size: %d", write_size);
1655                         }
1656                         munmap(temp, gmeta.size);
1657                     } else {
1658                         ALOGE("mmap is NULL %s", strerror(errno));
1659                     }
1660                     fclose(fp);
1661                 } else {
1662                     ALOGE("open fail %s", strerror(errno));
1663                 }
1664             }
1665         }
1666     }
1667 
1668 #ifndef DISABLE_FENCE
1669     delete [] releaseFences;
1670 #endif
1671 
1672     return ret;
1673 }
1674 
canSkipProcessing()1675 bool ExynosMPP::canSkipProcessing()
1676 {
1677     if ((mAssignedDisplay == NULL) || (mAssignedSources.size() == 0))
1678         return true;
1679     ExynosMPPSource *source = mAssignedSources[0];
1680     exynos_image dst = source->mMidImg;
1681     if ((mLogicalType == MPP_LOGICAL_G2D_RGB) ||
1682         (mLogicalType == MPP_LOGICAL_G2D_COMBO)) {
1683         dst = mAssignedDisplay->mExynosCompositionInfo.mDstImg;
1684     }
1685     return ((needDstBufRealloc(dst, mCurrentDstBuf) == false) && canUsePrevFrame());
1686 }
1687 
1688 /**
1689  * @param dst
1690  * @return int32_t 0 on success, or a negative error code on failure.
1691  */
doPostProcessing(struct exynos_image & dst)1692 int32_t ExynosMPP::doPostProcessing(struct exynos_image& dst) {
1693     ATRACE_CALL();
1694     MPP_LOGD(eDebugMPP, "total assigned sources (%zu)++++++++", mAssignedSources.size());
1695 
1696     int ret = NO_ERROR;
1697     bool realloc = false;
1698     if (mAssignedSources.size() == 0) {
1699         MPP_LOGE("Assigned source size(%zu) is not valid",
1700                 mAssignedSources.size());
1701         ret = -EINVAL;
1702         goto save_frame_info;
1703     }
1704 
1705     // Check whether destination buffer allocation is required
1706     if (mAllocOutBufFlag) {
1707         if ((realloc = needDstBufRealloc(dst, mCurrentDstBuf)) == true) {
1708             //  allocate mDstImgs[mCurrentDstBuf]
1709             uint32_t bufAlign = getOutBufAlign();
1710             bool isComposition = (mMaxSrcLayerNum > 1);
1711             if (isComposition)
1712                 dst.format = DEFAULT_MPP_DST_FORMAT;
1713 
1714             uint32_t allocFormat = dst.format;
1715             if (mFreeOutBufFlag == false)
1716                 allocFormat = DEFAULT_MPP_DST_FORMAT;
1717 
1718             if ((allocFormat == HAL_PIXEL_FORMAT_RGBA_1010102) ||
1719                 (allocFormat == HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B) ||
1720                 (allocFormat == HAL_PIXEL_FORMAT_YCBCR_P010))
1721                 allocFormat = DEFAULT_MPP_DST_FORMAT;
1722 
1723             ret = allocOutBuf(ALIGN_UP(mAssignedDisplay->mXres, bufAlign),
1724                     ALIGN_UP(mAssignedDisplay->mYres, bufAlign),
1725                     allocFormat, dst.usageFlags, mCurrentDstBuf);
1726         }
1727         if (ret < 0) {
1728             MPP_LOGE("%s:: fail to allocate dst buffer[%d]", __func__, mCurrentDstBuf);
1729             goto save_frame_info;
1730         }
1731         if (mDstImgs[mCurrentDstBuf].format != dst.format) {
1732             MPP_LOGD(eDebugMPP, "dst format is changed (%d -> %d)",
1733                     mDstImgs[mCurrentDstBuf].format, dst.format);
1734             mDstImgs[mCurrentDstBuf].format = dst.format;
1735         }
1736     }
1737 
1738     if ((realloc == false) && canUsePrevFrame()) {
1739         mCurrentDstBuf = (mCurrentDstBuf + NUM_MPP_DST_BUFS(mLogicalType) - 1)% NUM_MPP_DST_BUFS(mLogicalType);
1740         MPP_LOGD(eDebugMPP|eDebugFence, "Reuse previous frame, dstImg[%d]", mCurrentDstBuf);
1741         for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
1742             mAssignedSources[i]->mSrcImg.acquireFenceFd =
1743                 fence_close(mAssignedSources[i]->mSrcImg.acquireFenceFd,
1744                         mAssignedDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_G2D);
1745         }
1746         goto save_frame_info;
1747     }
1748 
1749     /* G2D or sclaer case */
1750     if ((ret = doPostProcessingInternal()) < 0) {
1751         MPP_LOGE("%s:: fail to post processing, ret %d",
1752                 __func__, ret);
1753         goto save_frame_info;
1754     }
1755 
1756 save_frame_info:
1757     /* Save current frame information for next frame*/
1758     mPrevAssignedDisplayType = mAssignedDisplay->mType;
1759     mPrevFrameInfo.srcNum = (uint32_t)mAssignedSources.size();
1760     for (uint32_t i = 0; i < mPrevFrameInfo.srcNum; i++) {
1761         mPrevFrameInfo.srcInfo[i] = mAssignedSources[i]->mSrcImg;
1762         mPrevFrameInfo.dstInfo[i] = mAssignedSources[i]->mMidImg;
1763     }
1764 
1765     MPP_LOGD(eDebugMPP, "mPrevAssignedState: %d, mPrevAssignedDisplayType: %d--------------",
1766             mAssignedState, mAssignedDisplay->mType);
1767 
1768     return ret;
1769 }
1770 
1771 /*
1772  * This function should be called after doPostProcessing()
1773  * because doPostProcessing() sets
1774  * mSrcImgs[].mppImg.releaseFenceFd
1775  */
getSrcReleaseFence(uint32_t srcIndex)1776 int32_t ExynosMPP::getSrcReleaseFence(uint32_t srcIndex)
1777 {
1778     if (srcIndex >= NUM_MPP_SRC_BUFS)
1779         return -EINVAL;
1780 
1781     return mSrcImgs[srcIndex].acrylicReleaseFenceFd;
1782 
1783     return -EINVAL;
1784 }
1785 
resetSrcReleaseFence()1786 int32_t ExynosMPP::resetSrcReleaseFence()
1787 {
1788     MPP_LOGD(eDebugFence, "");
1789     for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
1790         mSrcImgs[i].acrylicReleaseFenceFd = -1;
1791     }
1792     return NO_ERROR;
1793 }
1794 
getDstImageInfo(exynos_image * img)1795 int32_t ExynosMPP::getDstImageInfo(exynos_image *img)
1796 {
1797     if ((mCurrentDstBuf < 0) || (mCurrentDstBuf >= NUM_MPP_DST_BUFS(mLogicalType)) ||
1798         (mAssignedDisplay == NULL)) {
1799         MPP_LOGE("mCurrentDstBuf(%d), mAssignedDisplay(%p)", mCurrentDstBuf, mAssignedDisplay);
1800         return -EINVAL;
1801     }
1802 
1803     memset(img, 0, sizeof(exynos_image));
1804     img->acquireFenceFd = -1;
1805     img->releaseFenceFd = -1;
1806 
1807     if (needCompressDstBuf()) {
1808         img->compressionInfo.type = COMP_TYPE_AFBC;
1809         img->compressionInfo.modifier = AFBC_FORMAT_MOD_BLOCK_SIZE_16x16;
1810     }
1811 
1812     if (mDstImgs[mCurrentDstBuf].bufferHandle == NULL) {
1813         img->acquireFenceFd = -1;
1814         img->releaseFenceFd = -1;
1815         return -EFAULT;
1816     } else {
1817         img->bufferHandle = mDstImgs[mCurrentDstBuf].bufferHandle;
1818         img->compressionInfo = getCompressionInfo(img->bufferHandle);
1819         VendorGraphicBufferMeta gmeta(img->bufferHandle);
1820         img->fullWidth = gmeta.stride;
1821         img->fullHeight = gmeta.vstride;
1822         if ((mLogicalType == MPP_LOGICAL_G2D_RGB) ||
1823             (mLogicalType == MPP_LOGICAL_G2D_COMBO)) {
1824             if (mAssignedSources.size() == 1) {
1825                 img->x = mAssignedSources[0]->mDstImg.x;
1826                 img->y = mAssignedSources[0]->mDstImg.y;
1827                 img->w = mAssignedSources[0]->mDstImg.w;
1828                 img->h = mAssignedSources[0]->mDstImg.h;
1829             } else {
1830                 img->x = 0;
1831                 img->y = 0;
1832                 img->w = mAssignedDisplay->mXres;
1833                 img->h = mAssignedDisplay->mXres;
1834             }
1835         } else {
1836             img->x = mAssignedSources[0]->mMidImg.x;
1837             img->y = mAssignedSources[0]->mMidImg.y;
1838             img->w = mAssignedSources[0]->mMidImg.w;
1839             img->h = mAssignedSources[0]->mMidImg.h;
1840             img->needColorTransform =
1841                 mAssignedSources[0]->mMidImg.needColorTransform;
1842         }
1843 
1844         img->format = mDstImgs[mCurrentDstBuf].format;
1845         MPP_LOGD(eDebugFence, "get dstBuf[%d] accquireFence(%d)", mCurrentDstBuf,
1846                 mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd);
1847         img->acquireFenceFd = mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd;
1848         img->releaseFenceFd = mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd;
1849         img->dataSpace = mDstImgs[mCurrentDstBuf].dataspace;
1850     }
1851     return NO_ERROR;
1852 }
1853 
1854 /*
1855  * This function should be called after getDstReleaseFence()
1856  * by ExynosDisplay
1857  */
setDstAcquireFence(int acquireFence)1858 int32_t ExynosMPP::setDstAcquireFence(int acquireFence)
1859 {
1860 
1861     int dstBufIndex = 0;
1862 
1863     dstBufIndex = mPrivDstBuf;
1864 
1865     if (mPrivDstBuf == mCurrentDstBuf)
1866         MPP_LOGD(eDebugFence|eDebugMPP,
1867                 "M2MMPP : same buffer was reused idx %d, %d",mPrivDstBuf, mCurrentDstBuf);
1868 
1869     if (dstBufIndex < 0 || dstBufIndex >= NUM_MPP_DST_BUFS(mLogicalType)) {
1870         // TODO fence_close..
1871         acquireFence = fence_close(acquireFence, mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE, FENCE_IP_ALL);
1872         mPrivDstBuf = mCurrentDstBuf;
1873         return -EINVAL;
1874     }
1875 
1876     if (acquireFence < 0) {
1877         mPrivDstBuf = mCurrentDstBuf;
1878         return -EINVAL;
1879     }
1880 
1881     if (mDstImgs[dstBufIndex].acrylicAcquireFenceFd >= 0) {
1882         MPP_LOGD(eDebugFence,"mDstImgs[%d].acrylicAcquireFenceFd: %d is closed", dstBufIndex,
1883                 mDstImgs[dstBufIndex].acrylicAcquireFenceFd);
1884         fence_close(mDstImgs[dstBufIndex].acrylicAcquireFenceFd, mAssignedDisplay,
1885                 FENCE_TYPE_DST_ACQUIRE, FENCE_IP_ALL);
1886     }
1887     if (mPhysicalType == MPP_MSC)
1888         mDstImgs[dstBufIndex].acrylicAcquireFenceFd =
1889             hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE, FENCE_IP_MSC, acquireFence);
1890     else
1891         mDstImgs[dstBufIndex].acrylicAcquireFenceFd =
1892             hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE, FENCE_IP_G2D, acquireFence);
1893 
1894     MPP_LOGD(eDebugFence,"->mDstImgs[%d].acrylicAcquireFenceFd: %d", dstBufIndex,
1895             mDstImgs[dstBufIndex].acrylicAcquireFenceFd);
1896 
1897     mPrivDstBuf = mCurrentDstBuf;
1898 
1899     return NO_ERROR;
1900 }
1901 
resetDstReleaseFence()1902 int32_t ExynosMPP::resetDstReleaseFence()
1903 {
1904     MPP_LOGD(eDebugFence, "");
1905 
1906     if (mCurrentDstBuf < 0 || mCurrentDstBuf >= NUM_MPP_DST_BUFS(mLogicalType))
1907         return -EINVAL;
1908 
1909     mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd = -1;
1910 
1911     return NO_ERROR;
1912 }
1913 
requestHWStateChange(uint32_t state)1914 int32_t ExynosMPP::requestHWStateChange(uint32_t state)
1915 {
1916     MPP_LOGD(eDebugMPP|eDebugFence|eDebugBuf, "state: %d", state);
1917     /* Set HW state to running */
1918     if (mHWState == state) {
1919         if ((mPhysicalType == MPP_G2D) && (state == MPP_HW_STATE_IDLE) && (mHWBusyFlag == false)) {
1920             int ret = NO_ERROR;
1921             if ((ret = prioritize(-1)) != NO_ERROR)
1922                 MPP_LOGI("prioritize (%d) will be applied on next work", ret);
1923         }
1924         return NO_ERROR;
1925     }
1926 
1927     if (state == MPP_HW_STATE_RUNNING) {
1928         mHWState = MPP_HW_STATE_RUNNING;
1929     } else if (state == MPP_HW_STATE_IDLE) {
1930         if (mLastStateFenceFd >= 0) {
1931             mResourceManageThread->addStateFence(mLastStateFenceFd);
1932         } else {
1933             mHWState = MPP_HW_STATE_IDLE;
1934         }
1935 
1936         mLastStateFenceFd = -1;
1937 
1938         if ((mPhysicalType == MPP_G2D) && (mHWBusyFlag == false)) {
1939             int ret = NO_ERROR;
1940             if ((ret = prioritize(-1)) != NO_ERROR)
1941                 MPP_LOGI("prioritize (%d) is not applied on next work", ret);
1942         }
1943 
1944         /* Free all of output buffers */
1945         if (mMPPType == MPP_TYPE_M2M) {
1946             for(uint32_t i = 0; i < NUM_MPP_DST_BUFS(mLogicalType); i++) {
1947                 exynos_mpp_img_info freeDstBuf = mDstImgs[i];
1948                 memset(&mDstImgs[i], 0, sizeof(mDstImgs[i]));
1949                 mDstImgs[i].acrylicAcquireFenceFd = freeDstBuf.acrylicAcquireFenceFd;
1950                 mDstImgs[i].acrylicReleaseFenceFd = freeDstBuf.acrylicReleaseFenceFd;
1951                 freeDstBuf.acrylicAcquireFenceFd = -1;
1952                 freeDstBuf.acrylicReleaseFenceFd = -1;
1953 
1954                 if (mFreeOutBufFlag == true) {
1955                     MPP_LOGD(eDebugMPP|eDebugFence|eDebugBuf, "free outbuf[%d] %p",
1956                             i, freeDstBuf.bufferHandle);
1957                     if (freeDstBuf.bufferHandle != NULL && mAllocOutBufFlag) {
1958                         freeOutBuf(freeDstBuf);
1959                     }
1960                 } else {
1961                     mDstImgs[i].bufferHandle = freeDstBuf.bufferHandle;
1962                     mDstImgs[i].bufferType = freeDstBuf.bufferType;
1963                     mDstImgs[i].assignedDisplay = freeDstBuf.assignedDisplay;
1964                 }
1965             }
1966         }
1967 
1968         for (uint32_t i = 0; i < NUM_MPP_SRC_BUFS; i++)
1969         {
1970             if (mSrcImgs[i].mppLayer != NULL) {
1971                 delete mSrcImgs[i].mppLayer;
1972                 mSrcImgs[i].mppLayer = NULL;
1973             }
1974         }
1975         memset(&mPrevFrameInfo, 0, sizeof(mPrevFrameInfo));
1976         for (int i = 0; i < NUM_MPP_SRC_BUFS; i++) {
1977             mPrevFrameInfo.srcInfo[i].acquireFenceFd = -1;
1978             mPrevFrameInfo.srcInfo[i].releaseFenceFd = -1;
1979             mPrevFrameInfo.dstInfo[i].acquireFenceFd = -1;
1980             mPrevFrameInfo.dstInfo[i].releaseFenceFd = -1;
1981         }
1982     }
1983 
1984     return NO_ERROR;
1985 }
1986 
setHWStateFence(int32_t fence)1987 int32_t ExynosMPP::setHWStateFence(int32_t fence)
1988 {
1989     MPP_LOGD(eDebugFence, "Update HWState fence, Close(%d), set(%d)",
1990             mLastStateFenceFd, fence);
1991     mLastStateFenceFd = fence;
1992 
1993     return NO_ERROR;
1994 }
1995 
1996 /**
1997  * @param ..
1998  * @return int32_t
1999  */
setupRestriction()2000 int32_t ExynosMPP::setupRestriction() {
2001 
2002     MPP_LOGD(eDebugMPP, "mPhysicalType(%d)", mPhysicalType);
2003 
2004     for (uint32_t i = 0; i < RESTRICTION_MAX; i++) {
2005         const restriction_size_element *restriction_size_table = mResourceManager->mSizeRestrictions[i];
2006         for (uint32_t j = 0; j < mResourceManager->mSizeRestrictionCnt[i]; j++) {
2007             if (restriction_size_table[j].key.hwType == mPhysicalType) {
2008                 if ((restriction_size_table[j].key.nodeType == NODE_SRC) ||
2009                         (restriction_size_table[j].key.nodeType == NODE_NONE)) {
2010                     memcpy(&mSrcSizeRestrictions[i], &restriction_size_table[j].sizeRestriction,
2011                             sizeof(mSrcSizeRestrictions[i]));
2012                     MPP_LOGD(eDebugMPP, "\tSet mSrcSizeRestrictions[%d], "
2013                             "[%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d]",
2014                             i, mSrcSizeRestrictions[i].maxDownScale, mSrcSizeRestrictions[i].maxUpScale,
2015                             mSrcSizeRestrictions[i].maxFullWidth, mSrcSizeRestrictions[i].maxFullHeight,
2016                             mSrcSizeRestrictions[i].minFullWidth, mSrcSizeRestrictions[i].minFullHeight,
2017                             mSrcSizeRestrictions[i].fullWidthAlign, mSrcSizeRestrictions[i].fullHeightAlign,
2018                             mSrcSizeRestrictions[i].maxCropWidth, mSrcSizeRestrictions[i].maxCropHeight,
2019                             mSrcSizeRestrictions[i].minCropWidth, mSrcSizeRestrictions[i].minCropHeight,
2020                             mSrcSizeRestrictions[i].cropXAlign, mSrcSizeRestrictions[i].cropYAlign,
2021                             mSrcSizeRestrictions[i].cropWidthAlign, mSrcSizeRestrictions[i].cropHeightAlign);
2022 
2023                 }
2024                 if ((restriction_size_table[j].key.nodeType == NODE_DST) ||
2025                         (restriction_size_table[j].key.nodeType == NODE_NONE)) {
2026                     memcpy(&mDstSizeRestrictions[i], &restriction_size_table[j].sizeRestriction,
2027                             sizeof(mDstSizeRestrictions[i]));
2028                     MPP_LOGD(eDebugMPP, "\tSet mDstSizeRestrictions[%d], "
2029                             "[%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d]",
2030                             i, mDstSizeRestrictions[i].maxDownScale, mDstSizeRestrictions[i].maxUpScale,
2031                             mDstSizeRestrictions[i].maxFullWidth, mDstSizeRestrictions[i].maxFullHeight,
2032                             mDstSizeRestrictions[i].minFullWidth, mDstSizeRestrictions[i].minFullHeight,
2033                             mDstSizeRestrictions[i].fullWidthAlign, mDstSizeRestrictions[i].fullHeightAlign,
2034                             mDstSizeRestrictions[i].maxCropWidth, mDstSizeRestrictions[i].maxCropHeight,
2035                             mDstSizeRestrictions[i].minCropWidth, mDstSizeRestrictions[i].minCropHeight,
2036                             mDstSizeRestrictions[i].cropXAlign, mDstSizeRestrictions[i].cropYAlign,
2037                             mDstSizeRestrictions[i].cropWidthAlign, mDstSizeRestrictions[i].cropHeightAlign);
2038                 }
2039             }
2040         }
2041     }
2042 
2043     return NO_ERROR;
2044 }
2045 
isSupported(ExynosDisplay & display,struct exynos_image & src,struct exynos_image & dst)2046 int64_t ExynosMPP::isSupported(ExynosDisplay &display, struct exynos_image &src, struct exynos_image &dst)
2047 {
2048     uint32_t maxSrcWidth = getSrcMaxWidth(src);
2049     uint32_t maxSrcHeight = getSrcMaxHeight(src);
2050     uint32_t minSrcWidth = getSrcMinWidth(src);
2051     uint32_t minSrcHeight = getSrcMinHeight(src);
2052     uint32_t srcWidthAlign = getSrcWidthAlign(src);
2053     uint32_t srcHeightAlign = getSrcHeightAlign(src);
2054 
2055     uint32_t maxSrcCropWidth = getSrcMaxCropWidth(src);
2056     uint32_t maxSrcCropHeight = getSrcMaxCropHeight(src);
2057     uint32_t maxSrcCropSize = getSrcMaxCropSize(src);
2058     uint32_t minSrcCropWidth = getSrcMinCropWidth(src);
2059     uint32_t minSrcCropHeight = getSrcMinCropHeight(src);
2060     uint32_t srcCropWidthAlign = getSrcCropWidthAlign(src);
2061     uint32_t srcCropHeightAlign = getSrcCropHeightAlign(src);
2062     uint32_t srcXOffsetAlign = getSrcXOffsetAlign(src);
2063     uint32_t srcYOffsetAlign = getSrcYOffsetAlign(src);
2064 
2065     uint32_t maxDstWidth = getDstMaxWidth(dst);
2066     uint32_t maxDstHeight = getDstMaxHeight(dst);
2067     uint32_t minDstWidth = getDstMinWidth(dst);
2068     uint32_t minDstHeight = getDstMinHeight(dst);
2069     uint32_t dstWidthAlign = getDstWidthAlign(dst);
2070     uint32_t dstHeightAlign = getDstHeightAlign(dst);
2071     uint32_t dstXOffsetAlign = getDstXOffsetAlign(dst);
2072     uint32_t dstYOffsetAlign = getDstYOffsetAlign(dst);
2073 
2074     uint32_t maxDownscale = getMaxDownscale(display, src, dst);
2075     uint32_t maxUpscale = getMaxUpscale(src, dst);
2076 
2077     exynos_image rot_dst = dst;
2078     bool isPerpendicular = !!(src.transform & HAL_TRANSFORM_ROT_90);
2079     if (isPerpendicular) {
2080         rot_dst.w = dst.h;
2081         rot_dst.h = dst.w;
2082     }
2083 
2084     if (dst.w > maxDstWidth)
2085         return -eMPPExeedMaxDstWidth;
2086     else if (dst.h > maxDstHeight)
2087         return -eMPPExeedMaxDstHeight;
2088     else if (dst.w < minDstWidth)
2089         return -eMPPExeedMinDstWidth;
2090     else if (dst.h < minDstHeight)
2091         return -eMPPExeedMinDstHeight;
2092     else if (src.isDimLayer()) { // Dim layer
2093         if (isDimLayerSupported()) {
2094             return NO_ERROR;
2095         } else {
2096             return -eMPPUnsupportedDIMLayer;
2097         }
2098     }
2099     if (!isSupportedCapability(display, src))
2100         return -eMPPSaveCapability;
2101     else if (!isSrcFormatSupported(src))
2102         return -eMPPUnsupportedFormat;
2103     else if (!isDstFormatSupported(dst))
2104         return -eMPPUnsupportedFormat;
2105     else if (!isDataspaceSupportedByMPP(src, dst))
2106         return -eMPPUnsupportedCSC;
2107     else if (!isSupportedHDR(src, dst))
2108         return -eMPPUnsupportedDynamicMeta;
2109     else if (!isSupportedBlend(src))
2110         return -eMPPUnsupportedBlending;
2111     else if (!isSupportedTransform(src))
2112         return -eMPPUnsupportedRotation;
2113     else if (src.fullWidth < minSrcWidth)
2114         return -eMPPExeedMinSrcWidth;
2115     else if (src.fullHeight < minSrcHeight)
2116         return -eMPPExeedMinSrcHeight;
2117     else if (src.w < minSrcCropWidth)
2118         return -eMPPExeedSrcWCropMin;
2119     else if (src.h < minSrcCropHeight)
2120         return -eMPPExeedSrcHCropMin;
2121     else if ((dst.w % dstWidthAlign != 0) || (dst.h % dstHeightAlign != 0))
2122         return -eMPPNotAlignedDstSize;
2123     else if (src.w > rot_dst.w * maxDownscale)
2124         return -eMPPExeedMaxDownScale;
2125     else if (rot_dst.w > src.w * maxUpscale)
2126         return -eMPPExeedMaxUpScale;
2127     else if (src.h > rot_dst.h * maxDownscale)
2128         return -eMPPExeedMaxDownScale;
2129     else if (rot_dst.h > src.h * maxUpscale)
2130         return -eMPPExeedMaxUpScale;
2131     else if (!isSupportedDRM(src))
2132         return -eMPPUnsupportedDRM;
2133     else if (!isSupportedHStrideCrop(src))
2134         return -eMPPStrideCrop;
2135     else if (src.fullWidth > maxSrcWidth)
2136         return -eMPPExceedHStrideMaximum;
2137     else if (src.fullWidth % srcWidthAlign != 0)
2138         return -eMPPNotAlignedHStride;
2139 
2140     if ((src.w * src.h) > maxSrcCropSize)
2141         return -eMPPExeedSrcCropMax;
2142 
2143     if (getDrmMode(src.usageFlags) == NO_DRM) {
2144         if (src.fullHeight > maxSrcHeight)
2145             return -eMPPExceedVStrideMaximum;
2146         else if (src.fullHeight % srcHeightAlign != 0)
2147             return -eMPPNotAlignedVStride;
2148         else if (src.w > maxSrcCropWidth)
2149             return -eMPPExeedSrcWCropMax;
2150         else if (src.h > maxSrcCropHeight)
2151             return -eMPPExeedSrcHCropMax;
2152         else if ((src.w % srcCropWidthAlign != 0) || (src.h % srcCropHeightAlign != 0))
2153             return -eMPPNotAlignedCrop;
2154         else if ((src.x % srcXOffsetAlign != 0) || (src.y % srcYOffsetAlign != 0))
2155             return -eMPPNotAlignedOffset;
2156     }
2157 
2158     if ((dst.x % dstXOffsetAlign != 0) || (dst.y % dstYOffsetAlign != 0))
2159         return -eMPPNotAlignedOffset;
2160 
2161     if (!isSupportedCompression(src))
2162         return -eMPPUnsupportedCompression;
2163 
2164     if (!isSupportLayerColorTransform(src,dst))
2165         return -eMPPUnsupportedColorTransform;
2166 
2167     return NO_ERROR;
2168 }
2169 
resetMPP()2170 int32_t ExynosMPP::resetMPP()
2171 {
2172     mAssignedState = MPP_ASSIGN_STATE_FREE;
2173     mAssignedDisplay = NULL;
2174     mAssignedSources.clear();
2175     resetUsedCapacity();
2176     mReservedDisplay = -1;
2177     mHWBusyFlag = false;
2178 
2179     return NO_ERROR;
2180 }
2181 
resetAssignedState()2182 int32_t ExynosMPP::resetAssignedState()
2183 {
2184     for (int i = (int)mAssignedSources.size(); i-- > 0;) {
2185         ExynosMPPSource *mppSource = mAssignedSources[i];
2186         if (mppSource->mOtfMPP == this) {
2187             mppSource->mOtfMPP = NULL;
2188         }
2189         if (mppSource->mM2mMPP == this) {
2190             mppSource->mM2mMPP = NULL;
2191         }
2192         mAssignedSources.removeItemsAt(i);
2193     }
2194 
2195     /* Keep status if mAssignedState is MPP_ASSIGN_STATE_RESERVED */
2196     if ((mAssignedState & MPP_ASSIGN_STATE_ASSIGNED) &&
2197         (mAssignedSources.size() == 0)) {
2198         mAssignedState &= ~MPP_ASSIGN_STATE_ASSIGNED;
2199         mAssignedDisplay = NULL;
2200     }
2201 
2202     /* All mpp source are removed, reset capacity information */
2203     resetUsedCapacity();
2204 
2205     return NO_ERROR;
2206 }
2207 
resetAssignedState(ExynosMPPSource * mppSource)2208 int32_t ExynosMPP::resetAssignedState(ExynosMPPSource *mppSource)
2209 {
2210     bool needUpdateCapacity = false;
2211     for (int i = (int)mAssignedSources.size(); i-- > 0;) {
2212         ExynosMPPSource *source = mAssignedSources[i];
2213         if (source == mppSource) {
2214             if (mppSource->mM2mMPP == this) {
2215                 mppSource->mM2mMPP = NULL;
2216             }
2217             /* Update information for used capacity */
2218             /* This should be called before mAssignedSources.removeItemsAt(mppSource) */
2219             needUpdateCapacity = removeCapacity(mppSource);
2220 
2221             mAssignedSources.removeItemsAt(i);
2222 
2223             if (needUpdateCapacity)
2224                 updateUsedCapacity();
2225 
2226             break;
2227         }
2228     }
2229 
2230     /* Keep status if mAssignedState is MPP_ASSIGN_STATE_RESERVED */
2231     if ((mAssignedState & MPP_ASSIGN_STATE_ASSIGNED) &&
2232         (mAssignedSources.size() == 0)) {
2233         mAssignedState &= ~MPP_ASSIGN_STATE_ASSIGNED;
2234         mAssignedDisplay = NULL;
2235     }
2236 
2237     return NO_ERROR;
2238 }
2239 
reserveMPP(int32_t displayId)2240 int32_t ExynosMPP::reserveMPP(int32_t displayId)
2241 {
2242     mAssignedState |= MPP_ASSIGN_STATE_RESERVED;
2243     mReservedDisplay = displayId;
2244 
2245     return NO_ERROR;
2246 }
2247 
assignMPP(ExynosDisplay * display,ExynosMPPSource * mppSource)2248 int32_t ExynosMPP::assignMPP(ExynosDisplay *display, ExynosMPPSource* mppSource)
2249 {
2250     mAssignedState |= MPP_ASSIGN_STATE_ASSIGNED;
2251 
2252     if (mMPPType == MPP_TYPE_OTF)
2253         mppSource->mOtfMPP = this;
2254     else if (mMPPType == MPP_TYPE_M2M)
2255         mppSource->mM2mMPP = this;
2256     else {
2257         MPP_LOGE("%s:: Invalid mppType(%d)", __func__, mMPPType);
2258         return -EINVAL;
2259     }
2260 
2261     mAssignedDisplay = display;
2262 
2263     /* Update information for used capacity */
2264     /* This should be called before mAssignedSources.add(mppSource) */
2265     bool needUpdateCapacity = addCapacity(mppSource);
2266 
2267     mAssignedSources.add(mppSource);
2268 
2269     MPP_LOGD(eDebugCapacity|eDebugMPP, "\tassigned to source(%p) type(%d), mAssignedSources(%zu)",
2270             mppSource, mppSource->mSourceType,
2271             mAssignedSources.size());
2272 
2273     if (needUpdateCapacity)
2274         updateUsedCapacity();
2275 
2276     if (mMaxSrcLayerNum > 1) {
2277         std::sort(mAssignedSources.begin(), mAssignedSources.end(), exynosMPPSourceComp);
2278     }
2279 
2280     return NO_ERROR;
2281 }
2282 
getSrcMaxBlendingNum(struct exynos_image __unused & src,struct exynos_image __unused & dst)2283 uint32_t ExynosMPP::getSrcMaxBlendingNum(struct exynos_image __unused &src, struct exynos_image __unused &dst)
2284 {
2285     uint32_t maxSrcLayerNum = mMaxSrcLayerNum;
2286     return maxSrcLayerNum;
2287 }
2288 
getAssignedSourceNum()2289 uint32_t ExynosMPP::getAssignedSourceNum()
2290 {
2291     return mAssignedSources.size();
2292 }
2293 
2294 /* Based on multi-resolution support */
setDstAllocSize(uint32_t width,uint32_t height)2295 void ExynosMPP::setDstAllocSize(uint32_t width, uint32_t height)
2296 {
2297     switch(width) {
2298     case 720:
2299         mDstAllocatedSize = ((height >= 1480) ? DST_SIZE_HD_PLUS : DST_SIZE_HD);
2300         break;
2301     case 1080:
2302         mDstAllocatedSize = ((height >= 2220) ? DST_SIZE_FHD_PLUS : DST_SIZE_FHD);
2303         break;
2304     case 1440:
2305         mDstAllocatedSize = ((height >= 2960) ? DST_SIZE_WQHD_PLUS : DST_SIZE_WQHD);
2306         break;
2307     default:
2308         mDstAllocatedSize = DST_SIZE_UNKNOWN;
2309         break;
2310     }
2311 }
2312 
getDstAllocSize()2313 dst_alloc_buf_size_t ExynosMPP::getDstAllocSize()
2314 {
2315     return mDstAllocatedSize;
2316 }
2317 
needPreAllocation()2318 bool ExynosMPP::needPreAllocation()
2319 {
2320     bool ret = false;
2321 
2322     if ((mLogicalType == MPP_LOGICAL_G2D_RGB) &&
2323         (mPreAssignDisplayList[mResourceManager->mDevice->mDisplayMode] == HWC_DISPLAY_PRIMARY_BIT))
2324         ret = true;
2325 
2326     return ret;
2327 }
2328 
isAssignableState(ExynosDisplay * display,struct exynos_image & src,struct exynos_image & dst)2329 bool ExynosMPP::isAssignableState(ExynosDisplay *display, struct exynos_image &src, struct exynos_image &dst)
2330 {
2331     bool isAssignable = false;
2332 
2333     if (mAssignedState == MPP_ASSIGN_STATE_FREE) {
2334         if (mHWState == MPP_HW_STATE_IDLE)
2335             isAssignable = true;
2336         else {
2337             if ((mPrevAssignedDisplayType < 0) ||
2338                 ((uint32_t)mPrevAssignedDisplayType == display->mType))
2339                 isAssignable = true;
2340             else
2341                 isAssignable = false;
2342         }
2343     }
2344 
2345     if ((mAssignedState & MPP_ASSIGN_STATE_ASSIGNED) && (mAssignedState & MPP_ASSIGN_STATE_RESERVED))
2346     {
2347         if (mReservedDisplay == (int32_t)display->getId()) {
2348             if (mAssignedSources.size() < getSrcMaxBlendingNum(src, dst))
2349                 isAssignable = true;
2350             else
2351                 isAssignable = false;
2352         } else {
2353             isAssignable = false;
2354         }
2355     } else if ((mAssignedState & MPP_ASSIGN_STATE_ASSIGNED) && !(mAssignedState & MPP_ASSIGN_STATE_RESERVED)) {
2356         if (mAssignedSources.size() < getSrcMaxBlendingNum(src, dst))
2357             isAssignable = true;
2358         else
2359             isAssignable = false;
2360     } else if (mAssignedState & MPP_ASSIGN_STATE_RESERVED) {
2361         if (mReservedDisplay == (int32_t)display->getId())
2362             isAssignable = true;
2363         else
2364             isAssignable = false;
2365     }
2366 
2367     MPP_LOGD(eDebugMPP, "\tisAssignableState(%d), assigned size(%zu), getSrcMaxBlendingNum(%d)",
2368             isAssignable, mAssignedSources.size(), getSrcMaxBlendingNum(src, dst));
2369     return isAssignable;
2370 }
2371 
isAssignable(ExynosDisplay * display,struct exynos_image & src,struct exynos_image & dst,float totalUsedCapacity)2372 bool ExynosMPP::isAssignable(ExynosDisplay *display, struct exynos_image &src,
2373                              struct exynos_image &dst, float totalUsedCapacity)
2374 {
2375     bool isAssignable = isAssignableState(display, src, dst);
2376     return (isAssignable && hasEnoughCapa(display, src, dst, totalUsedCapacity));
2377 }
2378 
hasEnoughCapa(ExynosDisplay * display,struct exynos_image & src,struct exynos_image & dst,float totalUsedCapacity)2379 bool ExynosMPP::hasEnoughCapa(ExynosDisplay *display, struct exynos_image &src,
2380                               struct exynos_image &dst, float totalUsedCapacity)
2381 {
2382     if (mCapacity == -1)
2383         return true;
2384 
2385     MPP_LOGD(eDebugCapacity | eDebugMPP, "totalUsedCapacity(%f), mUsedCapacity(%f)",
2386              totalUsedCapacity, mUsedCapacity);
2387 
2388     /* mUsedCapacity should be re-calculated including src, dst passed as parameters*/
2389     totalUsedCapacity -= mUsedCapacity;
2390 
2391     float requiredCapacity = getRequiredCapacity(display, src, dst);
2392 
2393     MPP_LOGD(eDebugCapacity | eDebugMPP, "mCapacity(%f), usedCapacity(%f), RequiredCapacity(%f)",
2394              mCapacity, totalUsedCapacity, requiredCapacity);
2395 
2396     if (mCapacity >= (totalUsedCapacity + requiredCapacity))
2397         return true;
2398     else if (isCapacityExceptionCondition(totalUsedCapacity, requiredCapacity, src))
2399         return true;
2400     else
2401         return false;
2402 }
2403 
isCapacityExceptionCondition(float totalUsedCapacity,float requiredCapacity,struct exynos_image & src)2404 bool ExynosMPP::isCapacityExceptionCondition(float totalUsedCapacity, float requiredCapacity,
2405                                              struct exynos_image &src)
2406 {
2407     if ((hasHdrInfo(src) && (totalUsedCapacity == 0) &&
2408          (requiredCapacity < (mCapacity * MPP_HDR_MARGIN)))) {
2409         return true;
2410     } else {
2411         return false;
2412     }
2413 }
2414 
getPPCIndex(const struct exynos_image & src,const struct exynos_image & dst,uint32_t & formatIndex,uint32_t & rotIndex,uint32_t & scaleIndex,const struct exynos_image & criteria)2415 void ExynosMPP::getPPCIndex(const struct exynos_image &src,
2416         const struct exynos_image &dst,
2417         uint32_t &formatIndex, uint32_t &rotIndex, uint32_t &scaleIndex,
2418         const struct exynos_image &criteria)
2419 {
2420     formatIndex = 0;
2421     rotIndex = 0;
2422     scaleIndex = 0;
2423 
2424     /* Compare SBWC, AFBC and 10bitYUV420 first! because can be overlapped with other format */
2425     if (isFormatSBWC(criteria.format) && hasPPC(mPhysicalType, PPC_FORMAT_SBWC, PPC_ROT_NO))
2426         formatIndex = PPC_FORMAT_SBWC;
2427     else if (src.compressionInfo.type == COMP_TYPE_AFBC) {
2428         if ((isFormatRgb(criteria.format)) && hasPPC(mPhysicalType, PPC_FORMAT_AFBC_RGB, PPC_ROT_NO))
2429             formatIndex = PPC_FORMAT_AFBC_RGB;
2430         else if ((isFormatYUV(criteria.format)) && hasPPC(mPhysicalType, PPC_FORMAT_AFBC_YUV, PPC_ROT_NO))
2431             formatIndex = PPC_FORMAT_AFBC_YUV;
2432         else {
2433             formatIndex = PPC_FORMAT_RGB32;
2434             MPP_LOGW("%s:: AFBC PPC is not existed. Use default PPC", __func__);
2435         }
2436     } else if (isFormatP010(criteria.format) && hasPPC(mPhysicalType, PPC_FORMAT_P010, PPC_ROT_NO))
2437         formatIndex = PPC_FORMAT_P010;
2438     else if (isFormatYUV420(criteria.format) && hasPPC(mPhysicalType, PPC_FORMAT_YUV420, PPC_ROT_NO))
2439         formatIndex = PPC_FORMAT_YUV420;
2440     else if (isFormatYUV422(criteria.format) && hasPPC(mPhysicalType, PPC_FORMAT_YUV422, PPC_ROT_NO))
2441         formatIndex = PPC_FORMAT_YUV422;
2442     else
2443         formatIndex = PPC_FORMAT_RGB32;
2444 
2445     if (((criteria.transform & HAL_TRANSFORM_ROT_90) != 0) ||
2446         (mRotatedSrcCropBW > 0))
2447         rotIndex = PPC_ROT;
2448     else
2449         rotIndex = PPC_ROT_NO;
2450 
2451     uint32_t srcResolution = src.w * src.h;
2452     uint32_t dstResolution = dst.w * dst.h;
2453 
2454     if (mPhysicalType == MPP_G2D) {
2455         if (srcResolution == dstResolution) {
2456             scaleIndex = PPC_SCALE_NO;
2457         } else if (dstResolution > srcResolution) {
2458             /* scale up case */
2459             if (dstResolution >= (srcResolution * 4))
2460                 scaleIndex = PPC_SCALE_UP_4_;
2461             else
2462                 scaleIndex = PPC_SCALE_UP_1_4;
2463         } else {
2464             /* scale down case */
2465             if ((dstResolution * 16) <= srcResolution)
2466                 scaleIndex = PPC_SCALE_DOWN_16_;
2467             else if (((dstResolution * 9) <= srcResolution) &&
2468                     (srcResolution < (dstResolution * 16)))
2469                 scaleIndex = PPC_SCALE_DOWN_9_16;
2470             else if (((dstResolution * 4) <= srcResolution) &&
2471                     (srcResolution < (dstResolution * 9)))
2472                 scaleIndex = PPC_SCALE_DOWN_4_9;
2473             else
2474                 scaleIndex = PPC_SCALE_DOWN_1_4;
2475         }
2476     } else scaleIndex = 0; /* MSC doesn't refer scale Index */
2477 }
2478 
getPPC(const struct exynos_image & src,const struct exynos_image & dst,const struct exynos_image & criteria,const struct exynos_image * assignCheckSrc,const struct exynos_image * assignCheckDst)2479 float ExynosMPP::getPPC(const struct exynos_image &src,
2480         const struct exynos_image &dst, const struct exynos_image &criteria,
2481         const struct exynos_image *assignCheckSrc,
2482         const struct exynos_image *assignCheckDst)
2483 {
2484     float PPC = 0;
2485     uint32_t formatIndex = 0;
2486     uint32_t rotIndex = 0;
2487     uint32_t scaleIndex = 0;
2488 
2489     getPPCIndex(src, dst, formatIndex, rotIndex, scaleIndex, criteria);
2490 
2491     if ((rotIndex == PPC_ROT_NO) && (assignCheckSrc != NULL) &&
2492         ((assignCheckSrc->transform & HAL_TRANSFORM_ROT_90) != 0)) {
2493         rotIndex = PPC_ROT;
2494     }
2495 
2496     if (mPhysicalType == MPP_G2D || mPhysicalType == MPP_MSC) {
2497         if (hasPPC(mPhysicalType, formatIndex, rotIndex)) {
2498             PPC = ppc_table_map.at(PPC_IDX(mPhysicalType, formatIndex, rotIndex)).ppcList[scaleIndex];
2499         }
2500     }
2501 
2502     if (PPC == 0) {
2503         MPP_LOGE("%s:: mPhysicalType(%d), formatIndex(%d), rotIndex(%d), scaleIndex(%d), PPC(%f) is not valid",
2504                 __func__, mPhysicalType, formatIndex, rotIndex, scaleIndex, PPC);
2505         PPC = 0.000001;  /* It means can't use mPhysicalType H/W  */
2506     }
2507 
2508     MPP_LOGD(eDebugCapacity, "srcW(%d), srcH(%d), dstW(%d), dstH(%d), rot(%d)"
2509             "formatIndex(%d), rotIndex(%d), scaleIndex(%d), PPC(%f)",
2510             src.w, src.h, dst.w, dst.h, src.transform,
2511             formatIndex, rotIndex, scaleIndex, PPC);
2512     return PPC;
2513 }
2514 
getAssignedCapacity()2515 float ExynosMPP::getAssignedCapacity()
2516 {
2517     float capacity = 0;
2518     float baseCycles = 0;
2519     uint32_t rotIndex = 0;
2520 
2521     if (mPhysicalType != MPP_G2D)
2522         return 0;
2523 
2524     /*
2525      * Client target is assigned to m2mMPP
2526      * even if capacity is not enough
2527      */
2528     if ((mAssignedDisplay != NULL) &&
2529         (mAssignedDisplay->mType == HWC_DISPLAY_VIRTUAL))
2530         return 0;
2531 
2532 
2533     for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
2534         if ((mAssignedSources[i]->mSrcImg.transform & HAL_TRANSFORM_ROT_90) != 0)
2535             rotIndex = PPC_ROT;
2536     }
2537 
2538     MPP_LOGD(eDebugCapacity, "Check all of assigned layers cycles");
2539     /* PPC of layers that were added before should be changed */
2540     /* Check cycles of all assigned layers again */
2541     if ((mAssignedDisplay != NULL) && (mMaxSrcLayerNum > 1)) {
2542         baseCycles += ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL);
2543         MPP_LOGD(eDebugCapacity, "colorfill cycles: %f, total cycles: %f",
2544                 ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL), baseCycles);
2545     }
2546 
2547     for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
2548         float srcCycles = 0;
2549         uint32_t srcResolution = mAssignedSources[i]->mSrcImg.w * mAssignedSources[i]->mSrcImg.h;
2550         uint32_t dstResolution = mAssignedSources[i]->mMidImg.w * mAssignedSources[i]->mMidImg.h;
2551         uint32_t maxResolution = max(srcResolution, dstResolution);
2552         float PPC = 0;
2553 
2554         if (mAssignedSources[i]->mSrcImg.layerFlags & EXYNOS_HWC_DIM_LAYER) {
2555             PPC = G2D_BASE_PPC_COLORFILL;
2556         } else {
2557             PPC = getPPC(mAssignedSources[i]->mSrcImg, mAssignedSources[i]->mMidImg, mAssignedSources[i]->mSrcImg,
2558                     &mAssignedSources[i]->mSrcImg, &mAssignedSources[i]->mMidImg);
2559         }
2560         srcCycles = maxResolution/PPC;
2561 
2562         /* Hdr and drm layer is exception */
2563         if ((hasHdrInfo(mAssignedSources[i]->mSrcImg) ||
2564             (getDrmMode(mAssignedSources[i]->mSrcImg.usageFlags) != NO_DRM))) {
2565             MPP_LOGD(eDebugCapacity, "Src[%d] is skipped(drm or hdr), cycles: %f, PPC: %f, srcResolution: %d, dstResolution: %d, rot(%d)",
2566                     i, srcCycles, PPC, srcResolution, dstResolution, mAssignedSources[i]->mSrcImg.transform);
2567             continue;
2568         }
2569 
2570         baseCycles += srcCycles;
2571 
2572         MPP_LOGD(eDebugCapacity, "Src[%d] cycles: %f, total cycles: %f, PPC: %f, srcResolution: %d, dstResolution: %d, rot(%d)",
2573                 i, srcCycles, baseCycles, PPC, srcResolution, dstResolution, mAssignedSources[i]->mSrcImg.transform);
2574     }
2575 
2576     capacity = baseCycles / mClockKhz;
2577 
2578     return capacity;
2579 }
2580 
getRequiredCapacity(ExynosDisplay * display,struct exynos_image & src,struct exynos_image & dst)2581 float ExynosMPP::getRequiredCapacity(ExynosDisplay *display, struct exynos_image &src,
2582         struct exynos_image &dst)
2583 {
2584     float capacity = 0;
2585     float cycles = 0;
2586     if (mPhysicalType == MPP_G2D) {
2587         /* Initialize value with the cycles that were already assigned */
2588         float baseCycles = mUsedBaseCycles;
2589         float srcCycles = 0;
2590         uint32_t srcResolution = src.w * src.h;
2591         uint32_t dstResolution = dst.w * dst.h;
2592         uint32_t maxResolution = max(srcResolution, dstResolution);
2593         float curBaseCycles = 0;
2594         float PPC = 0;
2595 
2596         if ((mAssignedSources.size() == 0) ||
2597             (mRotatedSrcCropBW != 0) ||
2598             ((mRotatedSrcCropBW == 0) &&
2599              ((src.transform & HAL_TRANSFORM_ROT_90) == 0))) {
2600             /* Just add cycles for current layer */
2601             if ((mAssignedSources.size() == 0) &&
2602                 (display != NULL) && (mMaxSrcLayerNum > 1)) {
2603                 curBaseCycles = ((display->mXres * display->mYres) / G2D_BASE_PPC_COLORFILL);
2604                 MPP_LOGD(eDebugCapacity, "There is no assigned layer. Colorfill cycles: %f should be added",
2605                         curBaseCycles);
2606             }
2607             curBaseCycles += getRequiredBaseCycles(src, dst);
2608             baseCycles += curBaseCycles;
2609             MPP_LOGD(eDebugCapacity, "mUsedBaseCycles was %f, Add base cycles %f, totalBaseCycle(%f)",
2610                     mUsedBaseCycles, curBaseCycles, baseCycles);
2611         } else {
2612             /* Recalculate cycles for all of layers */
2613             baseCycles = 0;
2614             MPP_LOGD(eDebugCapacity, "Check all of assigned layers cycles");
2615             /* PPC of layers that were added before should be changed */
2616             /* Check cycles of all assigned layers again */
2617             if ((display != NULL) && (mMaxSrcLayerNum > 1)) {
2618                 baseCycles += ((display->mXres * display->mYres) / G2D_BASE_PPC_COLORFILL);
2619                 MPP_LOGD(eDebugCapacity, "colorfill cycles: %f, total cycles: %f",
2620                         ((display->mXres * display->mYres) / G2D_BASE_PPC_COLORFILL), cycles);
2621             }
2622 
2623             for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
2624                 float assignedSrcCycles = 0;
2625                 uint32_t assignedSrcResolution = mAssignedSources[i]->mSrcImg.w * mAssignedSources[i]->mSrcImg.h;
2626                 uint32_t assignedDstResolution = mAssignedSources[i]->mMidImg.w * mAssignedSources[i]->mMidImg.h;
2627                 uint32_t assignedMaxResolution = max(assignedSrcResolution, assignedDstResolution);
2628                 float assignedPPC = getPPC(mAssignedSources[i]->mSrcImg, mAssignedSources[i]->mMidImg,
2629                         mAssignedSources[i]->mSrcImg, &src, &dst);
2630 
2631                 assignedSrcCycles = assignedMaxResolution/assignedPPC;
2632                 baseCycles += assignedSrcCycles;
2633 
2634                 MPP_LOGD(eDebugCapacity, "Src[%d] cycles: %f, total cycles: %f, PPC: %f, srcResolution: %d, dstResolution: %d, rot(%d)",
2635                         i, assignedSrcCycles, baseCycles, assignedPPC, assignedSrcResolution, assignedDstResolution, mAssignedSources[i]->mSrcImg.transform);
2636             }
2637 
2638             PPC = getPPC(src, dst, src, &src, &dst);
2639 
2640             srcCycles = maxResolution/PPC;
2641             baseCycles += srcCycles;
2642 
2643             MPP_LOGD(eDebugCapacity, "check mppSource cycles: %f, total cycles: %f, PPC: %f, srcResolution: %d, dstResolution: %d, rot(%d)",
2644                     srcCycles, baseCycles, PPC, srcResolution, dstResolution, src.transform);
2645         }
2646 
2647         capacity = baseCycles / mClockKhz;
2648 
2649         MPP_LOGD(eDebugCapacity, "baseCycles: %f, capacity: %f",
2650                 baseCycles, capacity);
2651     } else if (mPhysicalType == MPP_MSC) {
2652         /* Initialize value with the capacity that were already assigned */
2653         capacity = mUsedCapacity;
2654 
2655         /* Just add capacity for current layer */
2656         float srcPPC = getPPC(src, dst, src);
2657         float dstPPC = getPPC(src, dst, dst);
2658         float srcCapacity = (float((src.w * src.h))) / (mClockKhz * srcPPC);
2659         float dstCapacity = (float((dst.w * dst.h))) / (mClockKhz * dstPPC);
2660 
2661         capacity += max(srcCapacity, dstCapacity);
2662 
2663     }
2664 
2665     return capacity;
2666 }
2667 
getRequiredBaseCycles(struct exynos_image & src,struct exynos_image & dst)2668 float ExynosMPP::getRequiredBaseCycles(struct exynos_image &src, struct exynos_image &dst)
2669 {
2670     if (mPhysicalType != MPP_G2D)
2671         return 0;
2672 
2673     uint32_t srcResolution = src.w * src.h;
2674     uint32_t dstResolution = dst.w * dst.h;
2675     uint32_t maxResolution = max(srcResolution, dstResolution);
2676 
2677     return maxResolution/(float)getPPC(src, dst, src);
2678 }
2679 
addCapacity(ExynosMPPSource * mppSource)2680 bool ExynosMPP::addCapacity(ExynosMPPSource* mppSource)
2681 {
2682     if ((mppSource == NULL) || mCapacity == -1)
2683         return false;
2684 
2685     if (mPhysicalType == MPP_G2D) {
2686         bool needUpdateCapacity = true;
2687         if ((mAssignedSources.size() == 0) ||
2688             (mRotatedSrcCropBW != 0) ||
2689             ((mRotatedSrcCropBW == 0) &&
2690              ((mppSource->mSrcImg.transform & HAL_TRANSFORM_ROT_90) == 0))) {
2691             needUpdateCapacity = false;
2692         }
2693 
2694         if (needUpdateCapacity)
2695             return true;
2696 
2697         if ((mMaxSrcLayerNum > 1) &&
2698             (mAssignedSources.size() == 0)) {
2699             if (mAssignedDisplay != NULL) {
2700                 /* This will be the first mppSource that is assigned to the ExynosMPP */
2701                 /* Add capacity for background */
2702                 mUsedBaseCycles += ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL);
2703                 MPP_LOGD(eDebugCapacity, "\tcolorfill cycles: %f, total cycles: %f",
2704                         ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL), mUsedBaseCycles);
2705             } else {
2706                 MPP_LOGE("mAssignedDisplay is null");
2707             }
2708         }
2709 
2710         float baseCycles = getRequiredBaseCycles(mppSource->mSrcImg, mppSource->mMidImg);
2711         mUsedBaseCycles += baseCycles;
2712 
2713         uint32_t srcResolution = mppSource->mSrcImg.w * mppSource->mSrcImg.h;
2714         uint32_t dstResolution = mppSource->mMidImg.w * mppSource->mMidImg.h;
2715         if ((mppSource->mSrcImg.transform & HAL_TRANSFORM_ROT_90) == 0)
2716             mNoRotatedSrcCropBW += srcResolution;
2717         else
2718             mRotatedSrcCropBW += srcResolution;
2719 
2720         mUsedCapacity = mUsedBaseCycles / mClockKhz;
2721 
2722         MPP_LOGD(eDebugCapacity, "src num: %zu base cycle is added: %f, mUsedBaseCycles: %f, mUsedCapacity(%f), srcResolution: %d, dstResolution: %d, rot: %d, mNoRotatedSrcCropBW(%d), mRotatedSrcCropBW(%d)",
2723                 mAssignedSources.size(),
2724                 baseCycles, mUsedBaseCycles, mUsedCapacity, srcResolution, dstResolution,
2725                 mppSource->mSrcImg.transform, mNoRotatedSrcCropBW, mRotatedSrcCropBW);
2726     } else if (mPhysicalType == MPP_MSC) {
2727         mUsedCapacity = getRequiredCapacity(NULL, mppSource->mSrcImg, mppSource->mMidImg);
2728     }
2729 
2730     return false;
2731 }
2732 
removeCapacity(ExynosMPPSource * mppSource)2733 bool ExynosMPP::removeCapacity(ExynosMPPSource* mppSource)
2734 {
2735     if ((mppSource == NULL) || (mCapacity == -1))
2736         return false;
2737 
2738     if (mPhysicalType == MPP_G2D) {
2739         uint32_t srcResolution = mppSource->mSrcImg.w * mppSource->mSrcImg.h;
2740         uint32_t dstResolution = mppSource->mDstImg.w * mppSource->mDstImg.h;
2741 
2742         uint32_t prevRotatedSrcCropBW = mRotatedSrcCropBW;
2743 
2744         if (mppSource->mSrcImg.transform == 0)
2745             mNoRotatedSrcCropBW -= srcResolution;
2746         else
2747             mRotatedSrcCropBW -= srcResolution;
2748 
2749         if ((prevRotatedSrcCropBW > 0) && (mRotatedSrcCropBW == 0))
2750             return true;
2751 
2752         float baseCycles = getRequiredBaseCycles(mppSource->mSrcImg, mppSource->mMidImg);
2753         mUsedBaseCycles -= baseCycles;
2754 
2755         mUsedCapacity = mUsedBaseCycles / mClockKhz;
2756 
2757         MPP_LOGD(eDebugCapacity, "src num: %zu, base cycle is removed: %f, mUsedBaseCycles: %f, mUsedCapacity(%f), srcResolution: %d, dstResolution: %d, rot: %d, mNoRotatedSrcCropBW(%d), mRotatedSrcCropBW(%d)",
2758                 mAssignedSources.size(),
2759                 baseCycles, mUsedBaseCycles, mUsedCapacity, srcResolution, dstResolution,
2760                 mppSource->mSrcImg.transform, mNoRotatedSrcCropBW, mRotatedSrcCropBW);
2761     } else if (mPhysicalType == MPP_MSC) {
2762         exynos_image &src = mppSource->mSrcImg;
2763         exynos_image &dst = mppSource->mDstImg;
2764         uint32_t srcResolution = src.w * src.h;
2765         uint32_t dstResolution = dst.w * dst.h;
2766 
2767         float srcCapacity = (float)srcResolution / getPPC(src, dst, src);
2768         float dstCapacity  = (float)dstResolution  / getPPC(src, dst, dst);
2769 
2770         mUsedCapacity -= max(srcCapacity, dstCapacity);
2771     }
2772 
2773     return false;
2774 }
2775 
resetUsedCapacity()2776 void ExynosMPP::resetUsedCapacity()
2777 {
2778     mUsedCapacity = 0;
2779     mUsedBaseCycles = 0;
2780     mRotatedSrcCropBW = 0;
2781     mNoRotatedSrcCropBW = 0;
2782 }
2783 
updateUsedCapacity()2784 int32_t ExynosMPP::updateUsedCapacity()
2785 {
2786     int32_t ret = NO_ERROR;
2787     if (mCapacity == -1)
2788         return ret;
2789 
2790     float capacity = 0;
2791     mUsedCapacity = 0;
2792 
2793     mRotatedSrcCropBW = 0;
2794     mNoRotatedSrcCropBW = 0;
2795 
2796     if ((mPhysicalType == MPP_G2D) &&
2797         (mAssignedDisplay != NULL) &&
2798         (mAssignedSources.size() > 0)) {
2799         float cycles = 0;
2800 
2801         if (mMaxSrcLayerNum > 1) {
2802             cycles += ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL);
2803             MPP_LOGD(eDebugCapacity, "\tcolorfill cycles: %f, total cycles: %f",
2804                     ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL), cycles);
2805         }
2806         for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
2807             uint32_t srcResolution = mAssignedSources[i]->mSrcImg.w * mAssignedSources[i]->mSrcImg.h;
2808             if ((mAssignedSources[i]->mSrcImg.transform & HAL_TRANSFORM_ROT_90) == 0)
2809                 mNoRotatedSrcCropBW += srcResolution;
2810             else
2811                 mRotatedSrcCropBW += srcResolution;
2812         }
2813         MPP_LOGD(eDebugCapacity, "mNoRotatedSrcCropBW(%d), mRotatedSrcCropBW(%d)",
2814                 mNoRotatedSrcCropBW, mRotatedSrcCropBW);
2815         for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
2816             float srcCycles = 0;
2817             uint32_t srcResolution = mAssignedSources[i]->mSrcImg.w * mAssignedSources[i]->mSrcImg.h;
2818             uint32_t dstResolution = mAssignedSources[i]->mMidImg.w * mAssignedSources[i]->mMidImg.h;
2819             uint32_t maxResolution = max(srcResolution, dstResolution);
2820             float PPC = getPPC(mAssignedSources[i]->mSrcImg, mAssignedSources[i]->mMidImg, mAssignedSources[i]->mSrcImg);
2821             srcCycles = maxResolution/PPC;
2822             cycles += srcCycles;
2823 
2824             MPP_LOGD(eDebugCapacity, "Src[%d] cycles: %f, total cycles: %f, PPC: %f, srcResolution: %d, dstResolution: %d, rot(%d)",
2825                     i, srcCycles, cycles, PPC, srcResolution, dstResolution, mAssignedSources[i]->mSrcImg.transform);
2826         }
2827 
2828         mUsedBaseCycles = cycles;
2829         capacity = cycles / mClockKhz;
2830 
2831         mUsedCapacity = capacity;
2832 
2833     }
2834     MPP_LOGD(eDebugCapacity, "assigned layer size(%zu), mUsedCapacity: %f", mAssignedSources.size(), mUsedCapacity);
2835 
2836     return mUsedCapacity;
2837 }
2838 
getRestrictionClassification(const struct exynos_image & img) const2839 uint32_t ExynosMPP::getRestrictionClassification(const struct exynos_image &img) const {
2840     return !!(isFormatRgb(img.format) == false);
2841 }
2842 
prioritize(int priority)2843 int ExynosMPP::prioritize(int priority)
2844 {
2845     if ((mPhysicalType != MPP_G2D) ||
2846         (mAcrylicHandle == NULL)) {
2847         MPP_LOGE("invalid function call");
2848         return -1;
2849     }
2850     int ret = NO_ERROR;
2851     ret = mAcrylicHandle->prioritize(priority);
2852 
2853     if ((priority > 0) && (ret == 1))
2854     {
2855         /* G2D Driver returned EBUSY */
2856         mHWBusyFlag = true;
2857     }
2858     MPP_LOGD(eDebugMPP, "set resource prioritize (%d), ret(%d), mHWBusyFlag(%d)", priority, ret, mHWBusyFlag);
2859 
2860     return ret;
2861 }
2862 
increaseDstBuffIndex()2863 uint32_t ExynosMPP::increaseDstBuffIndex()
2864 {
2865     if (mAllocOutBufFlag)
2866         mCurrentDstBuf = (mCurrentDstBuf + 1) % NUM_MPP_DST_BUFS(mLogicalType);
2867     return mCurrentDstBuf;
2868 }
2869 
reloadResourceForHWFC()2870 void ExynosMPP::reloadResourceForHWFC()
2871 {
2872     ALOGI("reloadResourceForHWFC()");
2873     delete mAcrylicHandle;
2874     mAcrylicHandle = AcrylicFactory::createAcrylic("default_compositor");
2875     if (mAcrylicHandle == NULL) {
2876         MPP_LOGE("Fail to allocate compositor");
2877     } else {
2878         mAcrylicHandle->setDefaultColor(0, 0, 0, 0);
2879         MPP_LOGI("The resource is reloaded for HWFC: %p", mAcrylicHandle);
2880     }
2881     for (uint32_t i = 0; i < NUM_MPP_SRC_BUFS; i++)
2882     {
2883         if (mSrcImgs[i].mppLayer != NULL) {
2884             delete mSrcImgs[i].mppLayer;
2885             mSrcImgs[i].mppLayer = NULL;
2886         }
2887     }
2888 }
2889 
setTargetDisplayLuminance(uint16_t min,uint16_t max)2890 void ExynosMPP::setTargetDisplayLuminance(uint16_t min, uint16_t max)
2891 {
2892     MPP_LOGD(eDebugMPP, "%s: min(%d), max(%d)", __func__, min, max);
2893     if (mAcrylicHandle == NULL) {
2894         MPP_LOGE("mAcrylicHandle is NULL");
2895     } else
2896         mAcrylicHandle->setTargetDisplayLuminance(min, max);
2897 }
2898 
setTargetDisplayDevice(int device)2899 void ExynosMPP::setTargetDisplayDevice(int device)
2900 {
2901     ALOGI("%s: device(%d)", __func__, device);
2902     if (mAcrylicHandle == NULL) {
2903         MPP_LOGE("mAcrylicHandle is NULL");
2904     } else
2905         mAcrylicHandle->setTargetDisplayInfo(&device);
2906 }
2907 
dump(String8 & result)2908 void ExynosMPP::dump(String8& result)
2909 {
2910     int32_t assignedDisplayType = -1;
2911     if (mAssignedDisplay != NULL)
2912         assignedDisplayType = mAssignedDisplay->mType;
2913 
2914     result.appendFormat("%s: types mppType(%d), (p:%d, l:0x%2x), indexs(p:%d, l:%d), preAssignDisplay(0x%2x)\n",
2915             mName.c_str(), mMPPType, mPhysicalType, mLogicalType, mPhysicalIndex, mLogicalIndex, mPreAssignDisplayInfo);
2916     result.appendFormat("\tEnable: %d, HWState: %d, AssignedState: %d, assignedDisplay(%d)\n",
2917             mEnable, mHWState, mAssignedState, assignedDisplayType);
2918     result.appendFormat("\tPrevAssignedState: %d, PrevAssignedDisplayType: %d, ReservedDisplay: %d\n",
2919             mPrevAssignedState, mPrevAssignedDisplayType, mReservedDisplay);
2920     result.appendFormat("\tassinedSourceNum(%zu), Capacity(%f), CapaUsed(%f), mCurrentDstBuf(%d)\n",
2921             mAssignedSources.size(), mCapacity, mUsedCapacity, mCurrentDstBuf);
2922 
2923 }
2924 
closeFences()2925 void ExynosMPP::closeFences()
2926 {
2927     for (uint32_t i = 0; i < mAssignedSources.size(); i++)
2928     {
2929         mSrcImgs[i].acrylicAcquireFenceFd =
2930             fence_close(mSrcImgs[i].acrylicAcquireFenceFd, mAssignedDisplay,
2931                     FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_G2D);
2932         mSrcImgs[i].acrylicReleaseFenceFd =
2933             fence_close(mSrcImgs[i].acrylicReleaseFenceFd, mAssignedDisplay,
2934                     FENCE_TYPE_SRC_RELEASE, FENCE_IP_G2D);
2935     }
2936 
2937     mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd =
2938         fence_close(mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd, mAssignedDisplay,
2939                 FENCE_TYPE_DST_ACQUIRE, FENCE_IP_G2D);
2940     mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd =
2941         fence_close(mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd, mAssignedDisplay,
2942                 FENCE_TYPE_DST_RELEASE, FENCE_IP_G2D);
2943 }
2944 
updateAttr()2945 void ExynosMPP::updateAttr()
2946 {
2947     MPP_LOGD(eDebugAttrSetting, "updateAttr::mPhysicalType(%d), mAttr(0x%" PRIx64 ")",
2948             mPhysicalType, mAttr);
2949 
2950     if (mResourceManager == NULL) return;
2951 
2952     auto iter = mResourceManager->mMPPAttrs.find(mPhysicalType);
2953     if (iter != mResourceManager->mMPPAttrs.end()) {
2954         mAttr = iter->second;
2955         MPP_LOGD(eDebugAttrSetting, "After mAttr(0x%" PRIx64 ")", mAttr);
2956     }
2957 }
2958 
updatePreassignedDisplay(uint32_t fromDisplayBit,uint32_t toDisplayBit)2959 void ExynosMPP::updatePreassignedDisplay(uint32_t fromDisplayBit, uint32_t toDisplayBit)
2960 {
2961     /*
2962      * If the pre-assigned resources are required to changed,
2963      * this function will modify PreAssign table.
2964      */
2965     for (uint32_t i = 0; i < DISPLAY_MODE_NUM; i++) {
2966         if (mPreAssignDisplayList[i] == fromDisplayBit)
2967             mPreAssignDisplayList[i] = toDisplayBit;
2968     }
2969 
2970     if (mPreAssignDisplayInfo == fromDisplayBit)
2971         mPreAssignDisplayInfo = toDisplayBit;
2972 }
2973