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] = (preAssignInfo >> (DISPLAY_MODE_MASK_LEN * i)) & DISPLAY_MODE_MASK_BIT;
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.string());
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.string(), 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.string());
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.string());
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.string(), 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 
needDstBufRealloc(struct exynos_image & dst,uint32_t index)1064 bool ExynosMPP::needDstBufRealloc(struct exynos_image &dst, uint32_t index)
1065 {
1066     MPP_LOGD(eDebugMPP|eDebugBuf, "index: %d++++++++", index);
1067 
1068     if (index >= NUM_MPP_DST_BUFS(mLogicalType)) {
1069         MPP_LOGE("%s:: index(%d) is not valid", __func__, index);
1070         return false;
1071     }
1072     buffer_handle_t dst_handle = NULL;
1073     if (mDstImgs[index].bufferHandle != NULL)
1074         dst_handle = mDstImgs[index].bufferHandle;
1075 
1076     if (dst_handle == NULL) {
1077         MPP_LOGD(eDebugMPP|eDebugBuf, "\tDstImag[%d]  handle is NULL", index);
1078         return true;
1079     }
1080 
1081     int32_t assignedDisplay = -1;
1082     if (mAssignedDisplay != NULL) {
1083         assignedDisplay = mAssignedDisplay->mType;
1084     } else {
1085         MPP_LOGE("%s:: mpp is not assigned", __func__);
1086         return false;
1087     }
1088 
1089     VendorGraphicBufferMeta gmeta(dst_handle);
1090 
1091     uint32_t prevAssignedBufferNum =
1092             getBufferNumOfFormat(gmeta.format, getCompressionType(dst_handle));
1093     uint32_t assignedBufferNum = getBufferNumOfFormat(dst.format, getCompressionType(dst_handle));
1094 
1095     MPP_LOGD(eDebugMPP | eDebugBuf, "\tdst_handle(%p) afbc (%u) sbwc (%u) lossy (%u)", dst_handle,
1096              isAFBCCompressed(dst_handle), isFormatSBWC(gmeta.format), isFormatLossy(gmeta.format));
1097     MPP_LOGD(eDebugMPP | eDebugBuf,
1098              "\tAssignedDisplay[%d, %d] format[0x%8x, 0x%8x], bufferType[%d, %d], bufferNum[%d, "
1099              "%d] "
1100              "usageFlags: 0x%" PRIx64 ", need comp_type 0x%x lossy %u",
1101              mPrevAssignedDisplayType, assignedDisplay, gmeta.format, dst.format,
1102              mDstImgs[index].bufferType, getBufferType(dst.usageFlags), prevAssignedBufferNum,
1103              assignedBufferNum, dst.usageFlags, dst.compressionInfo.type,
1104              isFormatLossy(dst.format));
1105 
1106     bool realloc = (mPrevAssignedDisplayType != assignedDisplay) ||
1107             (prevAssignedBufferNum < assignedBufferNum) ||
1108             (formatToBpp(gmeta.format) < formatToBpp(dst.format)) ||
1109             ((gmeta.stride * gmeta.vstride) < (int)(dst.fullWidth * dst.fullHeight)) ||
1110             (mDstImgs[index].bufferType != getBufferType(dst.usageFlags)) ||
1111             (isAFBCCompressed(dst_handle) != (dst.compressionInfo.type == COMP_TYPE_AFBC)) ||
1112             (isFormatSBWC(gmeta.format) != isFormatSBWC(dst.format)) ||
1113             (isFormatLossy(gmeta.format) != isFormatLossy(dst.format));
1114 
1115     MPP_LOGD(eDebugMPP|eDebugBuf, "realloc: %d--------", realloc);
1116     return realloc;
1117 }
1118 
canUsePrevFrame()1119 bool ExynosMPP::canUsePrevFrame()
1120 {
1121     if ((mAssignedDisplay && !mAssignedDisplay->mDisplayControl.skipM2mProcessing) ||
1122         !exynosHWCControl.skipM2mProcessing)
1123         return false;
1124 
1125     /* virtual display always require composition */
1126     if (mAllocOutBufFlag == false)
1127         return false;
1128 
1129     if (mPrevFrameInfo.srcNum != mAssignedSources.size())
1130         return false;
1131 
1132     for (uint32_t i = 0; i < mPrevFrameInfo.srcNum; i++) {
1133         if ((mPrevFrameInfo.srcInfo[i].bufferHandle != mAssignedSources[i]->mSrcImg.bufferHandle) ||
1134             (mPrevFrameInfo.srcInfo[i].x != mAssignedSources[i]->mSrcImg.x) ||
1135             (mPrevFrameInfo.srcInfo[i].y != mAssignedSources[i]->mSrcImg.y) ||
1136             (mPrevFrameInfo.srcInfo[i].w != mAssignedSources[i]->mSrcImg.w) ||
1137             (mPrevFrameInfo.srcInfo[i].h != mAssignedSources[i]->mSrcImg.h) ||
1138             (mPrevFrameInfo.srcInfo[i].format != mAssignedSources[i]->mSrcImg.format) ||
1139             (mPrevFrameInfo.srcInfo[i].usageFlags != mAssignedSources[i]->mSrcImg.usageFlags) ||
1140             (mPrevFrameInfo.srcInfo[i].dataSpace != mAssignedSources[i]->mSrcImg.dataSpace) ||
1141             (mPrevFrameInfo.srcInfo[i].blending != mAssignedSources[i]->mSrcImg.blending) ||
1142             (mPrevFrameInfo.srcInfo[i].transform != mAssignedSources[i]->mSrcImg.transform) ||
1143             (mPrevFrameInfo.srcInfo[i].compressionInfo.type !=
1144              mAssignedSources[i]->mSrcImg.compressionInfo.type) ||
1145             (mPrevFrameInfo.srcInfo[i].planeAlpha != mAssignedSources[i]->mSrcImg.planeAlpha) ||
1146             (mPrevFrameInfo.dstInfo[i].x != mAssignedSources[i]->mMidImg.x) ||
1147             (mPrevFrameInfo.dstInfo[i].y != mAssignedSources[i]->mMidImg.y) ||
1148             (mPrevFrameInfo.dstInfo[i].w != mAssignedSources[i]->mMidImg.w) ||
1149             (mPrevFrameInfo.dstInfo[i].h != mAssignedSources[i]->mMidImg.h) ||
1150             (mPrevFrameInfo.dstInfo[i].format != mAssignedSources[i]->mMidImg.format))
1151             return false;
1152     }
1153 
1154    int32_t prevDstIndex  = (mCurrentDstBuf + NUM_MPP_DST_BUFS(mLogicalType) - 1)% NUM_MPP_DST_BUFS(mLogicalType);
1155    if (mDstImgs[prevDstIndex].bufferHandle == NULL)
1156        return false;
1157 
1158     return true;
1159 }
1160 
setupLayer(exynos_mpp_img_info * srcImgInfo,struct exynos_image & src,struct exynos_image & dst)1161 int32_t ExynosMPP::setupLayer(exynos_mpp_img_info *srcImgInfo, struct exynos_image &src, struct exynos_image &dst)
1162 {
1163     int ret = NO_ERROR;
1164 
1165     if (srcImgInfo->mppLayer == NULL) {
1166         if ((srcImgInfo->mppLayer = mAcrylicHandle->createLayer()) == NULL)
1167         {
1168             MPP_LOGE("%s:: Fail to create layer", __func__);
1169             return -EINVAL;
1170         }
1171     }
1172 
1173     if (src.bufferHandle == NULL) {
1174         MPP_LOGE("%s:: Invalid source handle", __func__);
1175         return -EINVAL;
1176     }
1177 
1178     buffer_handle_t srcHandle = NULL;
1179     if (src.bufferHandle != NULL)
1180         srcHandle = src.bufferHandle;
1181 
1182     VendorGraphicBufferMeta gmeta(srcHandle);
1183     int bufFds[MAX_HW2D_PLANES];
1184     size_t bufLength[MAX_HW2D_PLANES];
1185     uint32_t attribute = 0;
1186     uint32_t bufferNum = getBufferNumOfFormat(gmeta.format, getCompressionType(srcHandle));
1187     android_dataspace_t dataspace = src.dataSpace;
1188     if (dataspace == HAL_DATASPACE_UNKNOWN)
1189     {
1190         if (isFormatRgb(gmeta.format))
1191             dataspace = HAL_DATASPACE_V0_SRGB;
1192         else
1193             dataspace = HAL_DATASPACE_V0_BT601_625;
1194     }
1195 
1196     if (bufferNum == 0)
1197     {
1198         MPP_LOGE("%s:: Fail to get bufferNum(%d), format(0x%8x, afbc %d)", __func__, bufferNum,
1199                  gmeta.format, isAFBCCompressed(srcHandle));
1200         return -EINVAL;
1201     }
1202     bufFds[0] = gmeta.fd;
1203     bufFds[1] = gmeta.fd1;
1204     bufFds[2] = gmeta.fd2;
1205     if (getBufLength(srcHandle, MAX_HW2D_PLANES, bufLength, gmeta.format, src.fullWidth, src.fullHeight) != NO_ERROR) {
1206         MPP_LOGE("%s:: invalid bufferLength(%zu, %zu, %zu), format(0x%8x)", __func__,
1207                 bufLength[0], bufLength[1], bufLength[2], gmeta.format);
1208         return -EINVAL;
1209     }
1210 
1211     /* HDR process */
1212     if (hasHdrInfo(src)) {
1213         unsigned int max = (src.metaParcel.sHdrStaticInfo.sType1.mMaxDisplayLuminance/10000);
1214         unsigned int min = src.metaParcel.sHdrStaticInfo.sType1.mMinDisplayLuminance;
1215 
1216         srcImgInfo->mppLayer->setMasterDisplayLuminance(min,max);
1217         MPP_LOGD(eDebugMPP, "HWC2: G2D luminance min %d, max %d", min, max);
1218         MPP_LOGD(eDebugMPP|eDebugFence, "G2D getting HDR source!");
1219 
1220         srcImgInfo->mppLayer->setLayerHDR(true);
1221     } else
1222         srcImgInfo->mppLayer->setLayerHDR(false);
1223 
1224     /* Transfer MetaData */
1225     if (src.hasMetaParcel) {
1226         srcImgInfo->mppLayer->setLayerData(&src.metaParcel, sizeof(src.metaParcel));
1227     }
1228 
1229     srcImgInfo->bufferType = getBufferType(srcHandle);
1230     if (srcImgInfo->bufferType == MPP_BUFFER_SECURE_DRM)
1231         attribute |= AcrylicCanvas::ATTR_PROTECTED;
1232     /*Change AFBC attribute on the basis of the modifier*/
1233     if (src.compressionInfo.type == COMP_TYPE_AFBC) {
1234         if ((src.compressionInfo.modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) ==
1235             AFBC_FORMAT_MOD_BLOCK_SIZE_32x8) {
1236             attribute |= AcrylicCanvas::ATTR_COMPRESSED_WIDEBLK;
1237         } else {
1238             attribute |= AcrylicCanvas::ATTR_COMPRESSED;
1239         }
1240     }
1241 
1242     srcImgInfo->bufferHandle = srcHandle;
1243     srcImgInfo->acrylicAcquireFenceFd =
1244         hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_G2D, src.acquireFenceFd);
1245 
1246     MPP_LOGD(eDebugMPP|eDebugFence, "source configuration:");
1247     MPP_LOGD(eDebugMPP, "\tImageDimension[%d, %d], ImageType[0x%8x, 0x%8x]",
1248             src.fullWidth, src.fullHeight,
1249             gmeta.format, dataspace);
1250     MPP_LOGD(eDebugMPP|eDebugFence, "\tImageBuffer handle: %p, fds[%d, %d, %d], bufLength[%zu, %zu, %zu], bufferNum: %d, acquireFence: %d, attribute: %d",
1251             srcHandle, bufFds[0], bufFds[1], bufFds[2], bufLength[0], bufLength[1], bufLength[2],
1252             bufferNum, srcImgInfo->acrylicAcquireFenceFd, attribute);
1253     MPP_LOGD(eDebugMPP, "\tsrc_rect[%d, %d, %d, %d], dst_rect[%d, %d, %d, %d], transform(0x%4x)",
1254             (int)src.x, (int)src.y, (int)(src.x + src.w), (int)(src.y + src.h),
1255             (int)dst.x, (int)dst.y, (int)(dst.x + dst.w), (int)(dst.y + dst.h), src.transform);
1256 
1257     srcImgInfo->mppLayer->setImageDimension(src.fullWidth, src.fullHeight);
1258 
1259     if (gmeta.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV) {
1260         srcImgInfo->mppLayer->setImageType(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M, dataspace);
1261     } else {
1262         srcImgInfo->mppLayer->setImageType(gmeta.format, dataspace);
1263     }
1264 
1265     if (mPhysicalType == MPP_G2D) {
1266         setFenceName(srcImgInfo->acrylicAcquireFenceFd, FENCE_G2D_SRC_LAYER);
1267         setFenceInfo(srcImgInfo->acrylicAcquireFenceFd, mAssignedDisplay, FENCE_TYPE_SRC_ACQUIRE,
1268                      FENCE_IP_G2D, HwcFenceDirection::TO);
1269     } else if (mPhysicalType == MPP_MSC) {
1270         setFenceName(srcImgInfo->acrylicAcquireFenceFd, FENCE_MSC_SRC_LAYER);
1271         setFenceInfo(srcImgInfo->acrylicAcquireFenceFd, mAssignedDisplay, FENCE_TYPE_SRC_ACQUIRE,
1272                      FENCE_IP_MSC, HwcFenceDirection::TO);
1273     } else {
1274         MPP_LOGE("%s:: invalid mPhysicalType(%d)", __func__, mPhysicalType);
1275     }
1276 
1277     srcImgInfo->mppLayer->setImageBuffer(bufFds, bufLength, bufferNum,
1278             srcImgInfo->acrylicAcquireFenceFd, attribute);
1279 
1280     if (mMaxSrcLayerNum > 1) {
1281         srcImgInfo->mppLayer->setCompositMode(src.blending, (uint8_t)(255 * src.planeAlpha), src.zOrder);
1282     } else {
1283         srcImgInfo->mppLayer->setCompositMode(src.blending, 255, src.zOrder);
1284     }
1285 
1286     hwc_rect_t src_rect = {(int)src.x, (int)src.y, (int)(src.x + src.w), (int)(src.y + src.h)};
1287     hwc_rect_t dst_rect = {(int)dst.x, (int)dst.y, (int)(dst.x + dst.w), (int)(dst.y + dst.h)};
1288 
1289     if ((mAssignedDisplay != NULL) &&
1290         ((mAssignedDisplay->mType == HWC_DISPLAY_VIRTUAL) ||
1291          (mAssignedDisplay->mType == HWC_DISPLAY_EXTERNAL)))
1292         srcImgInfo->mppLayer->setCompositArea(src_rect, dst_rect, src.transform, AcrylicLayer::ATTR_NORESAMPLING);
1293     else {
1294         if(isFormatYUV(src.format))
1295             srcImgInfo->mppLayer->setCompositArea(src_rect, dst_rect, src.transform, AcrylicLayer::ATTR_NORESAMPLING);
1296         else
1297             srcImgInfo->mppLayer->setCompositArea(src_rect, dst_rect, src.transform);
1298     }
1299 
1300     srcImgInfo->acrylicAcquireFenceFd = -1;
1301     srcImgInfo->format = gmeta.format;
1302 
1303     if (gmeta.format == HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV) {
1304         srcImgInfo->format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M;
1305     }
1306 
1307     return ret;
1308 }
1309 
getDstMetaInfo(android_dataspace_t dstDataspace)1310 dstMetaInfo_t ExynosMPP::getDstMetaInfo(android_dataspace_t dstDataspace)
1311 {
1312     dstMetaInfo_t metaInfo;
1313 
1314     if ((mAssignedSources.size() <= 1) &&
1315             (mAssignedSources[0]->mSrcImg.dataSpace == dstDataspace)) {
1316         metaInfo.minLuminance =
1317             (uint16_t)mAssignedSources[0]->mSrcImg.metaParcel.sHdrStaticInfo.sType1.mMinDisplayLuminance;
1318         metaInfo.maxLuminance =
1319             (uint16_t)(mAssignedSources[0]->mSrcImg.metaParcel.sHdrStaticInfo.sType1.mMaxDisplayLuminance/10000);
1320     } else {
1321         // minLuminance: 0.0001nit unit, maxLuminance: 1nit unit
1322         metaInfo.minLuminance = (uint16_t)(mAssignedDisplay->mMinLuminance * 10000);
1323         metaInfo.maxLuminance = (uint16_t)mAssignedDisplay->mMaxLuminance;
1324     }
1325 
1326     return metaInfo;
1327 }
1328 
setupDst(exynos_mpp_img_info * dstImgInfo)1329 int32_t ExynosMPP::setupDst(exynos_mpp_img_info *dstImgInfo)
1330 {
1331     int ret = NO_ERROR;
1332     bool isComposition = (mMaxSrcLayerNum > 1);
1333     buffer_handle_t dstHandle = dstImgInfo->bufferHandle;
1334     int bufFds[MAX_HW2D_PLANES];
1335     size_t bufLength[MAX_HW2D_PLANES];
1336     uint32_t attribute = 0;
1337     uint32_t bufferNum = getBufferNumOfFormat(dstImgInfo->format, getCompressionType(dstHandle));
1338     if (bufferNum == 0)
1339     {
1340         MPP_LOGE("%s:: Fail to get bufferNum(%d), format(0x%8x, afbc %d)", __func__, bufferNum,
1341                  dstImgInfo->format, isAFBCCompressed(dstHandle));
1342         return -EINVAL;
1343     }
1344 
1345     android_dataspace_t dataspace = HAL_DATASPACE_UNKNOWN;
1346     VendorGraphicBufferMeta gmeta(dstHandle);
1347 
1348     if (isComposition) {
1349         if (isFormatRgb(dstImgInfo->format)) {
1350             if ((mAssignedDisplay != NULL) &&
1351                 (mAssignedDisplay->mColorMode != HAL_COLOR_MODE_NATIVE))
1352                 dataspace = colorModeToDataspace(mAssignedDisplay->mColorMode);
1353         } else {
1354             dataspace =
1355                 (android_dataspace)(HAL_DATASPACE_STANDARD_BT709 | HAL_DATASPACE_TRANSFER_GAMMA2_2 | HAL_DATASPACE_RANGE_LIMITED);
1356         }
1357     } else {
1358         dataspace = mAssignedSources[0]->mMidImg.dataSpace;
1359     }
1360 
1361     if (dataspace == HAL_DATASPACE_UNKNOWN)
1362     {
1363         if (isFormatRgb(dstImgInfo->format))
1364             dataspace = HAL_DATASPACE_V0_SRGB;
1365         else
1366             dataspace = HAL_DATASPACE_V0_BT601_625;
1367     }
1368 
1369     bufFds[0] = gmeta.fd;
1370     bufFds[1] = gmeta.fd1;
1371     bufFds[2] = gmeta.fd2;
1372     if (getBufLength(dstHandle, MAX_HW2D_PLANES, bufLength, dstImgInfo->format,
1373                 gmeta.stride, gmeta.vstride) != NO_ERROR) {
1374         MPP_LOGE("%s:: invalid bufferLength(%zu, %zu, %zu), format(0x%8x)", __func__,
1375                 bufLength[0], bufLength[1], bufLength[2], dstImgInfo->format);
1376         return -EINVAL;
1377     }
1378 
1379     dstImgInfo->bufferType = getBufferType(dstHandle);
1380     if (dstImgInfo->bufferType == MPP_BUFFER_SECURE_DRM)
1381         attribute |= AcrylicCanvas::ATTR_PROTECTED;
1382 
1383     if (mAssignedDisplay != NULL) {
1384         mAcrylicHandle->setCanvasDimension(pixel_align(mAssignedDisplay->mXres, G2D_JUSTIFIED_DST_ALIGN),
1385                 pixel_align(mAssignedDisplay->mYres, G2D_JUSTIFIED_DST_ALIGN));
1386     }
1387 
1388     /* setup dst */
1389     if (needCompressDstBuf()) {
1390         attribute |= AcrylicCanvas::ATTR_COMPRESSED;
1391     }
1392 
1393     if (mPhysicalType == MPP_G2D) {
1394         setFenceName(dstImgInfo->acrylicAcquireFenceFd, FENCE_G2D_DST_DPP);
1395         /* Might be closed next frame */
1396         setFenceInfo(dstImgInfo->acrylicAcquireFenceFd, mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE,
1397                      FENCE_IP_G2D, HwcFenceDirection::TO);
1398     } else if (mPhysicalType == MPP_MSC) {
1399         setFenceName(dstImgInfo->acrylicAcquireFenceFd, FENCE_MSC_DST_DPP);
1400         /* Might be closed next frame */
1401         setFenceInfo(dstImgInfo->acrylicAcquireFenceFd, mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE,
1402                      FENCE_IP_MSC, HwcFenceDirection::TO);
1403     } else {
1404         MPP_LOGE("%s:: invalid mPhysicalType(%d)", __func__, mPhysicalType);
1405     }
1406 
1407     mAcrylicHandle->setCanvasImageType(dstImgInfo->format, dataspace);
1408 
1409     if ((mLogicalType == MPP_LOGICAL_G2D_COMBO) &&
1410             (mAssignedDisplay != NULL) &&
1411             (mAssignedDisplay->mType == HWC_DISPLAY_VIRTUAL) &&
1412             (((ExynosVirtualDisplay *)mAssignedDisplay)->mIsWFDState == (int)LLWFD)) {
1413         mAcrylicHandle->setCanvasImageType(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN, dataspace);
1414         dstImgInfo->acrylicAcquireFenceFd = fence_close(dstImgInfo->acrylicAcquireFenceFd,
1415                 mAssignedDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_G2D);
1416         mAcrylicHandle->setCanvasBuffer(bufFds, bufLength, bufferNum,
1417                 dstImgInfo->acrylicAcquireFenceFd, attribute);
1418         mAcrylicHandle->setCanvasOTF(attribute);
1419     }
1420     else {
1421         dstImgInfo->acrylicAcquireFenceFd =
1422             hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE, FENCE_IP_G2D, dstImgInfo->acrylicAcquireFenceFd);
1423         mAcrylicHandle->setCanvasBuffer(bufFds, bufLength, bufferNum,
1424                 dstImgInfo->acrylicAcquireFenceFd, attribute);
1425     }
1426 
1427     dstMetaInfo_t metaInfo = getDstMetaInfo(dataspace);
1428     if ((mAssignedDisplay != NULL) &&
1429         (mAssignedDisplay->mType != HWC_DISPLAY_VIRTUAL)) {
1430         mAcrylicHandle->setTargetDisplayLuminance(metaInfo.minLuminance, metaInfo.maxLuminance);
1431     }
1432 
1433     MPP_LOGD(eDebugMPP|eDebugFence, "destination configuration:");
1434     MPP_LOGD(eDebugMPP, "\tImageDimension[%d, %d], ImageType[0x%8x, %d], target luminance[%d, %d]",
1435             gmeta.stride, gmeta.vstride,
1436             dstImgInfo->format, dataspace, metaInfo.minLuminance, metaInfo.maxLuminance);
1437     MPP_LOGD(eDebugMPP|eDebugFence, "\tImageBuffer handle: %p, fds[%d, %d, %d], bufLength[%zu, %zu, %zu], bufferNum: %d, acquireFence: %d, attribute: %d",
1438             dstHandle, bufFds[0], bufFds[1], bufFds[2], bufLength[0], bufLength[1], bufLength[2],
1439             bufferNum, dstImgInfo->acrylicAcquireFenceFd, attribute);
1440 
1441 
1442     dstImgInfo->acrylicAcquireFenceFd = -1;
1443     dstImgInfo->dataspace = dataspace;
1444 
1445     return ret;
1446 }
1447 
doPostProcessingInternal()1448 int32_t ExynosMPP::doPostProcessingInternal()
1449 {
1450     ATRACE_CALL();
1451     int ret = NO_ERROR;
1452     size_t sourceNum = mAssignedSources.size();
1453 
1454     if (mAcrylicHandle == NULL) {
1455         MPP_LOGE("%s:: mAcrylicHandle is NULL", __func__);
1456         return -EINVAL;
1457     }
1458 
1459     /* setup source layers */
1460     for(size_t i = 0; i < sourceNum; i++) {
1461         MPP_LOGD(eDebugMPP|eDebugFence, "Setup [%zu] source: %p", i, mAssignedSources[i]);
1462         if ((ret = setupLayer(&mSrcImgs[i], mAssignedSources[i]->mSrcImg, mAssignedSources[i]->mMidImg)) != NO_ERROR) {
1463             MPP_LOGE("%s:: fail to setupLayer[%zu], ret %d",
1464                     __func__, i, ret);
1465             return ret;
1466         }
1467     }
1468 
1469     if ((ret = setColorConversionInfo()) != NO_ERROR) {
1470             MPP_LOGE("%s:: fail to setColorConversionInfo ret %d",
1471                     __func__, ret);
1472             return ret;
1473     }
1474 
1475     if (mPrevFrameInfo.srcNum > sourceNum) {
1476         MPP_LOGD(eDebugMPP, "prev sourceNum(%d), current sourceNum(%zu)",
1477                 mPrevFrameInfo.srcNum, sourceNum);
1478         for (size_t i = sourceNum; i < mPrevFrameInfo.srcNum; i++)
1479         {
1480             MPP_LOGD(eDebugMPP, "Remove mSrcImgs[%zu], %p", i, mSrcImgs[i].mppLayer);
1481             if (mSrcImgs[i].mppLayer != NULL) {
1482                 delete mSrcImgs[i].mppLayer;
1483                 mSrcImgs[i].mppLayer = NULL;
1484             }
1485         }
1486     }
1487 
1488     if (mAcrylicHandle->layerCount() != mAssignedSources.size()) {
1489         MPP_LOGE("Different layer number, acrylic layers(%d), assigned size(%zu)",
1490                 mAcrylicHandle->layerCount(), mAssignedSources.size());
1491         return -EINVAL;
1492     }
1493     MPP_LOGD(eDebugFence, "setupDst ++ mDstImgs[%d] acrylicAcquireFenceFd(%d)",
1494             mCurrentDstBuf, mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd);
1495 
1496     setupDst(&mDstImgs[mCurrentDstBuf]);
1497 
1498     MPP_LOGD(eDebugFence, "setupDst -- mDstImgs[%d] acrylicAcquireFenceFd(%d) closed",
1499             mCurrentDstBuf, mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd);
1500 
1501 
1502     int usingFenceCnt = 1;
1503     bool acrylicReturn = true;
1504 
1505 #ifndef DISABLE_FENCE
1506     if (mUseM2MSrcFence)
1507         usingFenceCnt = sourceNum + 1; // Get and Use src + dst fence
1508     else
1509         usingFenceCnt = 1;             // Get and Use only dst fence
1510     int *releaseFences = new int[usingFenceCnt];
1511     int dstBufIdx = usingFenceCnt - 1;
1512 #else
1513     usingFenceCnt = 0;                 // Get and Use no fences
1514     int dstBufIdx = 0;
1515     int *releaseFences = NULL;
1516 #endif
1517 
1518     acrylicReturn = mAcrylicHandle->execute(releaseFences, usingFenceCnt);
1519 
1520     if (acrylicReturn == false) {
1521         MPP_LOGE("%s:: fail to excute compositor", __func__);
1522         for(size_t i = 0; i < sourceNum; i++) {
1523             mSrcImgs[i].acrylicReleaseFenceFd = -1;
1524             MPP_LOGE("src[%zu]: ImageDimension[%d, %d], src_rect[%d, %d, %d, %d], dst_rect[%d, %d, %d, %d], transform(0x%4x)",
1525                     i,
1526                     mAssignedSources[i]->mSrcImg.fullWidth, mAssignedSources[i]->mSrcImg.fullHeight,
1527                     mAssignedSources[i]->mSrcImg.x, mAssignedSources[i]->mSrcImg.y,
1528                     mAssignedSources[i]->mSrcImg.x + mAssignedSources[i]->mSrcImg.w,
1529                     mAssignedSources[i]->mSrcImg.y + mAssignedSources[i]->mSrcImg.h,
1530                     mAssignedSources[i]->mMidImg.x, mAssignedSources[i]->mMidImg.y,
1531                     mAssignedSources[i]->mMidImg.x + mAssignedSources[i]->mMidImg.w,
1532                     mAssignedSources[i]->mMidImg.y + mAssignedSources[i]->mMidImg.h,
1533                     mAssignedSources[i]->mSrcImg.transform);
1534         }
1535         mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd = -1;
1536         ret = -EPERM;
1537     } else {
1538 
1539         // set fence informations from acryl
1540         if (mPhysicalType == MPP_G2D) {
1541             setFenceInfo(releaseFences[dstBufIdx], mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE,
1542                          FENCE_IP_G2D, HwcFenceDirection::FROM);
1543             if (usingFenceCnt > 1) {
1544                 for(size_t i = 0; i < sourceNum; i++) {
1545                     // TODO DPU release fence is tranferred to m2mMPP's source layer fence
1546                     setFenceInfo(releaseFences[i], mAssignedDisplay, FENCE_TYPE_SRC_RELEASE,
1547                                  FENCE_IP_G2D, HwcFenceDirection::FROM);
1548                 }
1549             }
1550         } else if (mPhysicalType == MPP_MSC) {
1551             setFenceInfo(releaseFences[dstBufIdx], mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE,
1552                          FENCE_IP_MSC, HwcFenceDirection::FROM);
1553             if (usingFenceCnt > 1) {
1554                 for(size_t i = 0; i < sourceNum; i++) {
1555                     // TODO DPU release fence is tranferred to m2mMPP's source layer fence
1556                     setFenceInfo(releaseFences[i], mAssignedDisplay, FENCE_TYPE_SRC_RELEASE,
1557                                  FENCE_IP_MSC, HwcFenceDirection::FROM);
1558                 }
1559             }
1560         } else {
1561             MPP_LOGE("%s:: invalid mPhysicalType(%d)", __func__, mPhysicalType);
1562         }
1563 
1564         if ((mLogicalType == MPP_LOGICAL_G2D_COMBO) &&
1565                 (mAssignedDisplay != NULL) &&
1566                 (mAssignedDisplay->mType == HWC_DISPLAY_VIRTUAL)) {
1567             if (((ExynosVirtualDisplay *)mAssignedDisplay)->mIsWFDState == (int)LLWFD) {
1568                 if (usingFenceCnt != 0) // Use no fences
1569                     releaseFences[dstBufIdx] = fence_close(releaseFences[dstBufIdx],
1570                             mAssignedDisplay, FENCE_TYPE_SRC_RELEASE, FENCE_IP_G2D); // Close dst buf's fence
1571             }
1572             if (mUseM2MSrcFence) {
1573                 if (((ExynosVirtualDisplay *)mAssignedDisplay)->mIsWFDState != (int)GOOGLEWFD) {
1574                     for (size_t i = 0; i < sourceNum; i++)
1575                         releaseFences[i] = fence_close(releaseFences[i],
1576                                 mAssignedDisplay, FENCE_TYPE_SRC_RELEASE, FENCE_IP_G2D);
1577                 }
1578             }
1579         }
1580 
1581         if (usingFenceCnt == 0) { // Use no fences
1582             for(size_t i = 0; i < sourceNum; i++) {
1583                 mSrcImgs[i].acrylicReleaseFenceFd = -1;
1584             }
1585             mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd = -1;
1586         } else {
1587             for(size_t i = 0; i < sourceNum; i++) {
1588                 if (mUseM2MSrcFence)
1589                     mSrcImgs[i].acrylicReleaseFenceFd =
1590                         hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_SRC_RELEASE, FENCE_IP_G2D, releaseFences[i]);
1591                 else
1592                     mSrcImgs[i].acrylicReleaseFenceFd = -1;
1593                 MPP_LOGD(eDebugFence, "mSrcImgs[%zu] acrylicReleaseFenceFd: %d",
1594                         i, mSrcImgs[i].acrylicReleaseFenceFd);
1595             }
1596 
1597             if (mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd >= 0) {
1598                 MPP_LOGE("mDstImgs[%d].acrylicReleaseFenceFd(%d) is not initialized",
1599                         mCurrentDstBuf,
1600                         mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd);
1601             }
1602 
1603             if (mPhysicalType == MPP_G2D)
1604                 mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd =
1605                     hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_DST_RELEASE, FENCE_IP_G2D, releaseFences[dstBufIdx]);
1606             else if (mPhysicalType == MPP_MSC)
1607                 mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd =
1608                     hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_DST_RELEASE, FENCE_IP_MSC, releaseFences[dstBufIdx]);
1609 
1610             MPP_LOGD(eDebugFence, "mDstImgs[%d] acrylicReleaseFenceFd: %d , releaseFences[%d]",
1611                     mCurrentDstBuf, mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd, dstBufIdx);
1612         }
1613 
1614         if (exynosHWCControl.dumpMidBuf) {
1615             ALOGI("dump image");
1616             exynosHWCControl.dumpMidBuf = false;
1617             if ((mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd > 0) &&
1618                 (sync_wait(mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd, 1000) < 0)) {
1619                 ALOGE("%s:: fence sync_wait error to dump image", __func__);
1620             } else {
1621                 buffer_handle_t dstHandle = mDstImgs[mCurrentDstBuf].bufferHandle;
1622                 VendorGraphicBufferMeta gmeta(dstHandle);
1623 
1624                 ALOGI("dump image fw: %d, fh:%d, size: %d", gmeta.stride, gmeta.vstride, gmeta.size);
1625                 FILE *fp;
1626                 fp = fopen(MPP_DUMP_PATH,"ab");
1627 
1628                 if (fp) {
1629                     void *temp = mmap(0, gmeta.size, PROT_READ|PROT_WRITE, MAP_SHARED, gmeta.fd, 0);
1630                     if (temp) {
1631                         ALOGI("write...%p", temp);
1632                         int write_size = fwrite(temp, gmeta.size, 1, fp);
1633                         if (write_size < 0) {
1634                             ALOGI("write error: %s", strerror(errno));
1635                         } else {
1636                             ALOGI("write size: %d", write_size);
1637                         }
1638                         munmap(temp, gmeta.size);
1639                     } else {
1640                         ALOGE("mmap is NULL %s", strerror(errno));
1641                     }
1642                     fclose(fp);
1643                 } else {
1644                     ALOGE("open fail %s", strerror(errno));
1645                 }
1646             }
1647         }
1648     }
1649 
1650 #ifndef DISABLE_FENCE
1651     delete [] releaseFences;
1652 #endif
1653 
1654     return ret;
1655 }
1656 
canSkipProcessing()1657 bool ExynosMPP::canSkipProcessing()
1658 {
1659     if ((mAssignedDisplay == NULL) || (mAssignedSources.size() == 0))
1660         return true;
1661     ExynosMPPSource *source = mAssignedSources[0];
1662     exynos_image dst = source->mMidImg;
1663     if ((mLogicalType == MPP_LOGICAL_G2D_RGB) ||
1664         (mLogicalType == MPP_LOGICAL_G2D_COMBO)) {
1665         dst = mAssignedDisplay->mExynosCompositionInfo.mDstImg;
1666     }
1667     return ((needDstBufRealloc(dst, mCurrentDstBuf) == false) & canUsePrevFrame());
1668 
1669 }
1670 
1671 /**
1672  * @param src
1673  * @param dst
1674  * @return int32_t releaseFenceFd of src buffer
1675  */
doPostProcessing(struct exynos_image & src,struct exynos_image & dst)1676 int32_t ExynosMPP::doPostProcessing(struct exynos_image &src, struct exynos_image &dst)
1677 {
1678     ATRACE_CALL();
1679     MPP_LOGD(eDebugMPP, "total assigned sources (%zu)++++++++", mAssignedSources.size());
1680 
1681     int ret = NO_ERROR;
1682     bool realloc = false;
1683     if (mAssignedSources.size() == 0) {
1684         MPP_LOGE("Assigned source size(%zu) is not valid",
1685                 mAssignedSources.size());
1686         ret = -EINVAL;
1687         goto save_frame_info;
1688     }
1689 
1690     // Check whether destination buffer allocation is required
1691     if (mAllocOutBufFlag) {
1692         if ((realloc = needDstBufRealloc(dst, mCurrentDstBuf)) == true) {
1693             //  allocate mDstImgs[mCurrentDstBuf]
1694             uint32_t bufAlign = getOutBufAlign();
1695             bool isComposition = (mMaxSrcLayerNum > 1);
1696             if (isComposition)
1697                 dst.format = DEFAULT_MPP_DST_FORMAT;
1698 
1699             uint32_t allocFormat = dst.format;
1700             if (mFreeOutBufFlag == false)
1701                 allocFormat = DEFAULT_MPP_DST_FORMAT;
1702 
1703             if ((allocFormat == HAL_PIXEL_FORMAT_RGBA_1010102) ||
1704                 (allocFormat == HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B) ||
1705                 (allocFormat == HAL_PIXEL_FORMAT_YCBCR_P010))
1706                 allocFormat = DEFAULT_MPP_DST_FORMAT;
1707 
1708             ret = allocOutBuf(ALIGN_UP(mAssignedDisplay->mXres, bufAlign),
1709                     ALIGN_UP(mAssignedDisplay->mYres, bufAlign),
1710                     allocFormat, dst.usageFlags, mCurrentDstBuf);
1711         }
1712         if (ret < 0) {
1713             MPP_LOGE("%s:: fail to allocate dst buffer[%d]", __func__, mCurrentDstBuf);
1714             goto save_frame_info;
1715         }
1716         if (mDstImgs[mCurrentDstBuf].format != dst.format) {
1717             MPP_LOGD(eDebugMPP, "dst format is changed (%d -> %d)",
1718                     mDstImgs[mCurrentDstBuf].format, dst.format);
1719             mDstImgs[mCurrentDstBuf].format = dst.format;
1720         }
1721     }
1722 
1723     if ((realloc == false) && canUsePrevFrame()) {
1724         mCurrentDstBuf = (mCurrentDstBuf + NUM_MPP_DST_BUFS(mLogicalType) - 1)% NUM_MPP_DST_BUFS(mLogicalType);
1725         MPP_LOGD(eDebugMPP|eDebugFence, "Reuse previous frame, dstImg[%d]", mCurrentDstBuf);
1726         for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
1727             mAssignedSources[i]->mSrcImg.acquireFenceFd =
1728                 fence_close(mAssignedSources[i]->mSrcImg.acquireFenceFd,
1729                         mAssignedDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_G2D);
1730         }
1731         goto save_frame_info;
1732     }
1733 
1734     /* G2D or sclaer case */
1735     if ((ret = doPostProcessingInternal()) < 0) {
1736         MPP_LOGE("%s:: fail to post processing, ret %d",
1737                 __func__, ret);
1738         goto save_frame_info;
1739     }
1740 
1741 save_frame_info:
1742     /* Save current frame information for next frame*/
1743     mPrevAssignedDisplayType = mAssignedDisplay->mType;
1744     mPrevFrameInfo.srcNum = (uint32_t)mAssignedSources.size();
1745     for (uint32_t i = 0; i < mPrevFrameInfo.srcNum; i++) {
1746         mPrevFrameInfo.srcInfo[i] = mAssignedSources[i]->mSrcImg;
1747         mPrevFrameInfo.dstInfo[i] = mAssignedSources[i]->mMidImg;
1748     }
1749 
1750     MPP_LOGD(eDebugMPP, "mPrevAssignedState: %d, mPrevAssignedDisplayType: %d--------------",
1751             mAssignedState, mAssignedDisplay->mType);
1752 
1753     return ret;
1754 }
1755 
1756 /*
1757  * This function should be called after doPostProcessing()
1758  * because doPostProcessing() sets
1759  * mSrcImgs[].mppImg.releaseFenceFd
1760  */
getSrcReleaseFence(uint32_t srcIndex)1761 int32_t ExynosMPP::getSrcReleaseFence(uint32_t srcIndex)
1762 {
1763     if (srcIndex >= NUM_MPP_SRC_BUFS)
1764         return -EINVAL;
1765 
1766     return mSrcImgs[srcIndex].acrylicReleaseFenceFd;
1767 
1768     return -EINVAL;
1769 }
1770 
resetSrcReleaseFence()1771 int32_t ExynosMPP::resetSrcReleaseFence()
1772 {
1773     MPP_LOGD(eDebugFence, "");
1774     for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
1775         mSrcImgs[i].acrylicReleaseFenceFd = -1;
1776     }
1777     return NO_ERROR;
1778 }
1779 
getDstImageInfo(exynos_image * img)1780 int32_t ExynosMPP::getDstImageInfo(exynos_image *img)
1781 {
1782     if ((mCurrentDstBuf < 0) || (mCurrentDstBuf >= NUM_MPP_DST_BUFS(mLogicalType)) ||
1783         (mAssignedDisplay == NULL)) {
1784         MPP_LOGE("mCurrentDstBuf(%d), mAssignedDisplay(%p)", mCurrentDstBuf, mAssignedDisplay);
1785         return -EINVAL;
1786     }
1787 
1788     memset(img, 0, sizeof(exynos_image));
1789     img->acquireFenceFd = -1;
1790     img->releaseFenceFd = -1;
1791 
1792     if (needCompressDstBuf()) {
1793         img->compressionInfo.type = COMP_TYPE_AFBC;
1794         img->compressionInfo.modifier = AFBC_FORMAT_MOD_BLOCK_SIZE_16x16;
1795     }
1796 
1797     if (mDstImgs[mCurrentDstBuf].bufferHandle == NULL) {
1798         img->acquireFenceFd = -1;
1799         img->releaseFenceFd = -1;
1800         return -EFAULT;
1801     } else {
1802         img->bufferHandle = mDstImgs[mCurrentDstBuf].bufferHandle;
1803         img->compressionInfo = getCompressionInfo(img->bufferHandle);
1804         VendorGraphicBufferMeta gmeta(img->bufferHandle);
1805         img->fullWidth = gmeta.stride;
1806         img->fullHeight = gmeta.vstride;
1807         if ((mLogicalType == MPP_LOGICAL_G2D_RGB) ||
1808             (mLogicalType == MPP_LOGICAL_G2D_COMBO)) {
1809             if (mAssignedSources.size() == 1) {
1810                 img->x = mAssignedSources[0]->mDstImg.x;
1811                 img->y = mAssignedSources[0]->mDstImg.y;
1812                 img->w = mAssignedSources[0]->mDstImg.w;
1813                 img->h = mAssignedSources[0]->mDstImg.h;
1814             } else {
1815                 img->x = 0;
1816                 img->y = 0;
1817                 img->w = mAssignedDisplay->mXres;
1818                 img->h = mAssignedDisplay->mXres;
1819             }
1820         } else {
1821             img->x = mAssignedSources[0]->mMidImg.x;
1822             img->y = mAssignedSources[0]->mMidImg.y;
1823             img->w = mAssignedSources[0]->mMidImg.w;
1824             img->h = mAssignedSources[0]->mMidImg.h;
1825             img->needColorTransform =
1826                 mAssignedSources[0]->mMidImg.needColorTransform;
1827         }
1828 
1829         img->format = mDstImgs[mCurrentDstBuf].format;
1830         MPP_LOGD(eDebugFence, "get dstBuf[%d] accquireFence(%d)", mCurrentDstBuf,
1831                 mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd);
1832         img->acquireFenceFd = mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd;
1833         img->releaseFenceFd = mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd;
1834         img->dataSpace = mDstImgs[mCurrentDstBuf].dataspace;
1835     }
1836     return NO_ERROR;
1837 }
1838 
1839 /*
1840  * This function should be called after getDstReleaseFence()
1841  * by ExynosDisplay
1842  */
setDstAcquireFence(int acquireFence)1843 int32_t ExynosMPP::setDstAcquireFence(int acquireFence)
1844 {
1845 
1846     int dstBufIndex = 0;
1847 
1848     dstBufIndex = mPrivDstBuf;
1849 
1850     if (mPrivDstBuf == mCurrentDstBuf)
1851         MPP_LOGD(eDebugFence|eDebugMPP,
1852                 "M2MMPP : same buffer was reused idx %d, %d",mPrivDstBuf, mCurrentDstBuf);
1853 
1854     if (dstBufIndex < 0 || dstBufIndex >= NUM_MPP_DST_BUFS(mLogicalType)) {
1855         // TODO fence_close..
1856         acquireFence = fence_close(acquireFence, mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE, FENCE_IP_ALL);
1857         mPrivDstBuf = mCurrentDstBuf;
1858         return -EINVAL;
1859     }
1860 
1861     if (acquireFence < 0) {
1862         mPrivDstBuf = mCurrentDstBuf;
1863         return -EINVAL;
1864     }
1865 
1866     if (mDstImgs[dstBufIndex].acrylicAcquireFenceFd >= 0) {
1867         MPP_LOGD(eDebugFence,"mDstImgs[%d].acrylicAcquireFenceFd: %d is closed", dstBufIndex,
1868                 mDstImgs[dstBufIndex].acrylicAcquireFenceFd);
1869         fence_close(mDstImgs[dstBufIndex].acrylicAcquireFenceFd, mAssignedDisplay,
1870                 FENCE_TYPE_DST_ACQUIRE, FENCE_IP_ALL);
1871     }
1872     if (mPhysicalType == MPP_MSC)
1873         mDstImgs[dstBufIndex].acrylicAcquireFenceFd =
1874             hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE, FENCE_IP_MSC, acquireFence);
1875     else
1876         mDstImgs[dstBufIndex].acrylicAcquireFenceFd =
1877             hwcCheckFenceDebug(mAssignedDisplay, FENCE_TYPE_DST_ACQUIRE, FENCE_IP_G2D, acquireFence);
1878 
1879     MPP_LOGD(eDebugFence,"->mDstImgs[%d].acrylicAcquireFenceFd: %d", dstBufIndex,
1880             mDstImgs[dstBufIndex].acrylicAcquireFenceFd);
1881 
1882     mPrivDstBuf = mCurrentDstBuf;
1883 
1884     return NO_ERROR;
1885 }
1886 
resetDstReleaseFence()1887 int32_t ExynosMPP::resetDstReleaseFence()
1888 {
1889     MPP_LOGD(eDebugFence, "");
1890 
1891     if (mCurrentDstBuf < 0 || mCurrentDstBuf >= NUM_MPP_DST_BUFS(mLogicalType))
1892         return -EINVAL;
1893 
1894     mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd = -1;
1895 
1896     return NO_ERROR;
1897 }
1898 
requestHWStateChange(uint32_t state)1899 int32_t ExynosMPP::requestHWStateChange(uint32_t state)
1900 {
1901     MPP_LOGD(eDebugMPP|eDebugFence|eDebugBuf, "state: %d", state);
1902     /* Set HW state to running */
1903     if (mHWState == state) {
1904         if ((mPhysicalType == MPP_G2D) && (state == MPP_HW_STATE_IDLE) && (mHWBusyFlag == false)) {
1905             int ret = NO_ERROR;
1906             if ((ret = prioritize(-1)) != NO_ERROR)
1907                 MPP_LOGI("prioritize (%d) will be applied on next work", ret);
1908         }
1909         return NO_ERROR;
1910     }
1911 
1912     if (state == MPP_HW_STATE_RUNNING) {
1913         mHWState = MPP_HW_STATE_RUNNING;
1914     } else if (state == MPP_HW_STATE_IDLE) {
1915         if (mLastStateFenceFd >= 0) {
1916             mResourceManageThread->addStateFence(mLastStateFenceFd);
1917         } else {
1918             mHWState = MPP_HW_STATE_IDLE;
1919         }
1920 
1921         mLastStateFenceFd = -1;
1922 
1923         if ((mPhysicalType == MPP_G2D) && (mHWBusyFlag == false)) {
1924             int ret = NO_ERROR;
1925             if ((ret = prioritize(-1)) != NO_ERROR)
1926                 MPP_LOGI("prioritize (%d) is not applied on next work", ret);
1927         }
1928 
1929         /* Free all of output buffers */
1930         if (mMPPType == MPP_TYPE_M2M) {
1931             for(uint32_t i = 0; i < NUM_MPP_DST_BUFS(mLogicalType); i++) {
1932                 exynos_mpp_img_info freeDstBuf = mDstImgs[i];
1933                 memset(&mDstImgs[i], 0, sizeof(mDstImgs[i]));
1934                 mDstImgs[i].acrylicAcquireFenceFd = freeDstBuf.acrylicAcquireFenceFd;
1935                 mDstImgs[i].acrylicReleaseFenceFd = freeDstBuf.acrylicReleaseFenceFd;
1936                 freeDstBuf.acrylicAcquireFenceFd = -1;
1937                 freeDstBuf.acrylicReleaseFenceFd = -1;
1938 
1939                 if (mFreeOutBufFlag == true) {
1940                     MPP_LOGD(eDebugMPP|eDebugFence|eDebugBuf, "free outbuf[%d] %p",
1941                             i, freeDstBuf.bufferHandle);
1942                     if (freeDstBuf.bufferHandle != NULL && mAllocOutBufFlag) {
1943                         freeOutBuf(freeDstBuf);
1944                     }
1945                 } else {
1946                     mDstImgs[i].bufferHandle = freeDstBuf.bufferHandle;
1947                     mDstImgs[i].bufferType = freeDstBuf.bufferType;
1948                 }
1949             }
1950         }
1951 
1952         for (uint32_t i = 0; i < NUM_MPP_SRC_BUFS; i++)
1953         {
1954             if (mSrcImgs[i].mppLayer != NULL) {
1955                 delete mSrcImgs[i].mppLayer;
1956                 mSrcImgs[i].mppLayer = NULL;
1957             }
1958         }
1959         memset(&mPrevFrameInfo, 0, sizeof(mPrevFrameInfo));
1960         for (int i = 0; i < NUM_MPP_SRC_BUFS; i++) {
1961             mPrevFrameInfo.srcInfo[i].acquireFenceFd = -1;
1962             mPrevFrameInfo.srcInfo[i].releaseFenceFd = -1;
1963             mPrevFrameInfo.dstInfo[i].acquireFenceFd = -1;
1964             mPrevFrameInfo.dstInfo[i].releaseFenceFd = -1;
1965         }
1966     }
1967 
1968     return NO_ERROR;
1969 }
1970 
setHWStateFence(int32_t fence)1971 int32_t ExynosMPP::setHWStateFence(int32_t fence)
1972 {
1973     MPP_LOGD(eDebugFence, "Update HWState fence, Close(%d), set(%d)",
1974             mLastStateFenceFd, fence);
1975     mLastStateFenceFd = fence;
1976 
1977     return NO_ERROR;
1978 }
1979 
1980 /**
1981  * @param ..
1982  * @return int32_t
1983  */
setupRestriction()1984 int32_t ExynosMPP::setupRestriction() {
1985 
1986     MPP_LOGD(eDebugMPP, "mPhysicalType(%d)", mPhysicalType);
1987 
1988     for (uint32_t i = 0; i < RESTRICTION_MAX; i++) {
1989         const restriction_size_element *restriction_size_table = mResourceManager->mSizeRestrictions[i];
1990         for (uint32_t j = 0; j < mResourceManager->mSizeRestrictionCnt[i]; j++) {
1991             if (restriction_size_table[j].key.hwType == mPhysicalType) {
1992                 if ((restriction_size_table[j].key.nodeType == NODE_SRC) ||
1993                         (restriction_size_table[j].key.nodeType == NODE_NONE)) {
1994                     memcpy(&mSrcSizeRestrictions[i], &restriction_size_table[j].sizeRestriction,
1995                             sizeof(mSrcSizeRestrictions[i]));
1996                     MPP_LOGD(eDebugMPP, "\tSet mSrcSizeRestrictions[%d], "
1997                             "[%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d]",
1998                             i, mSrcSizeRestrictions[i].maxDownScale, mSrcSizeRestrictions[i].maxUpScale,
1999                             mSrcSizeRestrictions[i].maxFullWidth, mSrcSizeRestrictions[i].maxFullHeight,
2000                             mSrcSizeRestrictions[i].minFullWidth, mSrcSizeRestrictions[i].minFullHeight,
2001                             mSrcSizeRestrictions[i].fullWidthAlign, mSrcSizeRestrictions[i].fullHeightAlign,
2002                             mSrcSizeRestrictions[i].maxCropWidth, mSrcSizeRestrictions[i].maxCropHeight,
2003                             mSrcSizeRestrictions[i].minCropWidth, mSrcSizeRestrictions[i].minCropHeight,
2004                             mSrcSizeRestrictions[i].cropXAlign, mSrcSizeRestrictions[i].cropYAlign,
2005                             mSrcSizeRestrictions[i].cropWidthAlign, mSrcSizeRestrictions[i].cropHeightAlign);
2006 
2007                 }
2008                 if ((restriction_size_table[j].key.nodeType == NODE_DST) ||
2009                         (restriction_size_table[j].key.nodeType == NODE_NONE)) {
2010                     memcpy(&mDstSizeRestrictions[i], &restriction_size_table[j].sizeRestriction,
2011                             sizeof(mDstSizeRestrictions[i]));
2012                     MPP_LOGD(eDebugMPP, "\tSet mDstSizeRestrictions[%d], "
2013                             "[%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d]",
2014                             i, mDstSizeRestrictions[i].maxDownScale, mDstSizeRestrictions[i].maxUpScale,
2015                             mDstSizeRestrictions[i].maxFullWidth, mDstSizeRestrictions[i].maxFullHeight,
2016                             mDstSizeRestrictions[i].minFullWidth, mDstSizeRestrictions[i].minFullHeight,
2017                             mDstSizeRestrictions[i].fullWidthAlign, mDstSizeRestrictions[i].fullHeightAlign,
2018                             mDstSizeRestrictions[i].maxCropWidth, mDstSizeRestrictions[i].maxCropHeight,
2019                             mDstSizeRestrictions[i].minCropWidth, mDstSizeRestrictions[i].minCropHeight,
2020                             mDstSizeRestrictions[i].cropXAlign, mDstSizeRestrictions[i].cropYAlign,
2021                             mDstSizeRestrictions[i].cropWidthAlign, mDstSizeRestrictions[i].cropHeightAlign);
2022                 }
2023             }
2024         }
2025     }
2026 
2027     return NO_ERROR;
2028 }
2029 
isSupported(ExynosDisplay & display,struct exynos_image & src,struct exynos_image & dst)2030 int64_t ExynosMPP::isSupported(ExynosDisplay &display, struct exynos_image &src, struct exynos_image &dst)
2031 {
2032     uint32_t maxSrcWidth = getSrcMaxWidth(src);
2033     uint32_t maxSrcHeight = getSrcMaxHeight(src);
2034     uint32_t minSrcWidth = getSrcMinWidth(src);
2035     uint32_t minSrcHeight = getSrcMinHeight(src);
2036     uint32_t srcWidthAlign = getSrcWidthAlign(src);
2037     uint32_t srcHeightAlign = getSrcHeightAlign(src);
2038 
2039     uint32_t maxSrcCropWidth = getSrcMaxCropWidth(src);
2040     uint32_t maxSrcCropHeight = getSrcMaxCropHeight(src);
2041     uint32_t maxSrcCropSize = getSrcMaxCropSize(src);
2042     uint32_t minSrcCropWidth = getSrcMinCropWidth(src);
2043     uint32_t minSrcCropHeight = getSrcMinCropHeight(src);
2044     uint32_t srcCropWidthAlign = getSrcCropWidthAlign(src);
2045     uint32_t srcCropHeightAlign = getSrcCropHeightAlign(src);
2046     uint32_t srcXOffsetAlign = getSrcXOffsetAlign(src);
2047     uint32_t srcYOffsetAlign = getSrcYOffsetAlign(src);
2048 
2049     uint32_t maxDstWidth = getDstMaxWidth(dst);
2050     uint32_t maxDstHeight = getDstMaxHeight(dst);
2051     uint32_t minDstWidth = getDstMinWidth(dst);
2052     uint32_t minDstHeight = getDstMinHeight(dst);
2053     uint32_t dstWidthAlign = getDstWidthAlign(dst);
2054     uint32_t dstHeightAlign = getDstHeightAlign(dst);
2055     uint32_t dstXOffsetAlign = getDstXOffsetAlign(dst);
2056     uint32_t dstYOffsetAlign = getDstYOffsetAlign(dst);
2057 
2058     uint32_t maxDownscale = getMaxDownscale(display, src, dst);
2059     uint32_t maxUpscale = getMaxUpscale(src, dst);
2060 
2061     exynos_image rot_dst = dst;
2062     bool isPerpendicular = !!(src.transform & HAL_TRANSFORM_ROT_90);
2063     if (isPerpendicular) {
2064         rot_dst.w = dst.h;
2065         rot_dst.h = dst.w;
2066     }
2067 
2068     if (dst.w > maxDstWidth)
2069         return -eMPPExeedMaxDstWidth;
2070     else if (dst.h > maxDstHeight)
2071         return -eMPPExeedMaxDstHeight;
2072     else if (dst.w < minDstWidth)
2073         return -eMPPExeedMinDstWidth;
2074     else if (dst.h < minDstHeight)
2075         return -eMPPExeedMinDstHeight;
2076     else if (src.isDimLayer()) { // Dim layer
2077         if (isDimLayerSupported()) {
2078             return NO_ERROR;
2079         } else {
2080             return -eMPPUnsupportedDIMLayer;
2081         }
2082     }
2083     if (!isSupportedCapability(display, src))
2084         return -eMPPSaveCapability;
2085     else if (!isSrcFormatSupported(src))
2086         return -eMPPUnsupportedFormat;
2087     else if (!isDstFormatSupported(dst))
2088         return -eMPPUnsupportedFormat;
2089     else if (!isDataspaceSupportedByMPP(src, dst))
2090         return -eMPPUnsupportedCSC;
2091     else if (!isSupportedHDR(src, dst))
2092         return -eMPPUnsupportedDynamicMeta;
2093     else if (!isSupportedBlend(src))
2094         return -eMPPUnsupportedBlending;
2095     else if (!isSupportedTransform(src))
2096         return -eMPPUnsupportedRotation;
2097     else if (src.fullWidth < minSrcWidth)
2098         return -eMPPExeedMinSrcWidth;
2099     else if (src.fullHeight < minSrcHeight)
2100         return -eMPPExeedMinSrcHeight;
2101     else if (src.w < minSrcCropWidth)
2102         return -eMPPExeedSrcWCropMin;
2103     else if (src.h < minSrcCropHeight)
2104         return -eMPPExeedSrcHCropMin;
2105     else if ((dst.w % dstWidthAlign != 0) || (dst.h % dstHeightAlign != 0))
2106         return -eMPPNotAlignedDstSize;
2107     else if (src.w > rot_dst.w * maxDownscale)
2108         return -eMPPExeedMaxDownScale;
2109     else if (rot_dst.w > src.w * maxUpscale)
2110         return -eMPPExeedMaxUpScale;
2111     else if (src.h > rot_dst.h * maxDownscale)
2112         return -eMPPExeedMaxDownScale;
2113     else if (rot_dst.h > src.h * maxUpscale)
2114         return -eMPPExeedMaxUpScale;
2115     else if (!isSupportedDRM(src))
2116         return -eMPPUnsupportedDRM;
2117     else if (!isSupportedHStrideCrop(src))
2118         return -eMPPStrideCrop;
2119     else if (src.fullWidth > maxSrcWidth)
2120         return -eMPPExceedHStrideMaximum;
2121     else if (src.fullWidth % srcWidthAlign != 0)
2122         return -eMPPNotAlignedHStride;
2123 
2124     if ((src.w * src.h) > maxSrcCropSize)
2125         return -eMPPExeedSrcCropMax;
2126 
2127     if (getDrmMode(src.usageFlags) == NO_DRM) {
2128         if (src.fullHeight > maxSrcHeight)
2129             return -eMPPExceedVStrideMaximum;
2130         else if (src.fullHeight % srcHeightAlign != 0)
2131             return -eMPPNotAlignedVStride;
2132         else if (src.w > maxSrcCropWidth)
2133             return -eMPPExeedSrcWCropMax;
2134         else if (src.h > maxSrcCropHeight)
2135             return -eMPPExeedSrcHCropMax;
2136         else if ((src.w % srcCropWidthAlign != 0) || (src.h % srcCropHeightAlign != 0))
2137             return -eMPPNotAlignedCrop;
2138         else if ((src.x % srcXOffsetAlign != 0) || (src.y % srcYOffsetAlign != 0))
2139             return -eMPPNotAlignedOffset;
2140     }
2141 
2142     if ((dst.x % dstXOffsetAlign != 0) || (dst.y % dstYOffsetAlign != 0))
2143         return -eMPPNotAlignedOffset;
2144 
2145     if (!isSupportedCompression(src))
2146         return -eMPPUnsupportedCompression;
2147 
2148     if (!isSupportLayerColorTransform(src,dst))
2149         return -eMPPUnsupportedColorTransform;
2150 
2151     return NO_ERROR;
2152 }
2153 
resetMPP()2154 int32_t ExynosMPP::resetMPP()
2155 {
2156     mAssignedState = MPP_ASSIGN_STATE_FREE;
2157     mAssignedDisplay = NULL;
2158     mAssignedSources.clear();
2159     resetUsedCapacity();
2160     mReservedDisplay = -1;
2161     mHWBusyFlag = false;
2162 
2163     return NO_ERROR;
2164 }
2165 
resetAssignedState()2166 int32_t ExynosMPP::resetAssignedState()
2167 {
2168     for (int i = (int)mAssignedSources.size(); i-- > 0;) {
2169         ExynosMPPSource *mppSource = mAssignedSources[i];
2170         if (mppSource->mOtfMPP == this) {
2171             mppSource->mOtfMPP = NULL;
2172         }
2173         if (mppSource->mM2mMPP == this) {
2174             mppSource->mM2mMPP = NULL;
2175         }
2176         mAssignedSources.removeItemsAt(i);
2177     }
2178 
2179     /* Keep status if mAssignedState is MPP_ASSIGN_STATE_RESERVED */
2180     if ((mAssignedState & MPP_ASSIGN_STATE_ASSIGNED) &&
2181         (mAssignedSources.size() == 0)) {
2182         mAssignedState &= ~MPP_ASSIGN_STATE_ASSIGNED;
2183         mAssignedDisplay = NULL;
2184     }
2185 
2186     /* All mpp source are removed, reset capacity information */
2187     resetUsedCapacity();
2188 
2189     return NO_ERROR;
2190 }
2191 
resetAssignedState(ExynosMPPSource * mppSource)2192 int32_t ExynosMPP::resetAssignedState(ExynosMPPSource *mppSource)
2193 {
2194     bool needUpdateCapacity = false;
2195     for (int i = (int)mAssignedSources.size(); i-- > 0;) {
2196         ExynosMPPSource *source = mAssignedSources[i];
2197         if (source == mppSource) {
2198             if (mppSource->mM2mMPP == this) {
2199                 mppSource->mM2mMPP = NULL;
2200             }
2201             /* Update information for used capacity */
2202             /* This should be called before mAssignedSources.removeItemsAt(mppSource) */
2203             needUpdateCapacity = removeCapacity(mppSource);
2204 
2205             mAssignedSources.removeItemsAt(i);
2206 
2207             if (needUpdateCapacity)
2208                 updateUsedCapacity();
2209 
2210             break;
2211         }
2212     }
2213 
2214     /* Keep status if mAssignedState is MPP_ASSIGN_STATE_RESERVED */
2215     if ((mAssignedState & MPP_ASSIGN_STATE_ASSIGNED) &&
2216         (mAssignedSources.size() == 0)) {
2217         mAssignedState &= ~MPP_ASSIGN_STATE_ASSIGNED;
2218         mAssignedDisplay = NULL;
2219     }
2220 
2221     return NO_ERROR;
2222 }
2223 
reserveMPP(int32_t displayId)2224 int32_t ExynosMPP::reserveMPP(int32_t displayId)
2225 {
2226     mAssignedState |= MPP_ASSIGN_STATE_RESERVED;
2227     mReservedDisplay = displayId;
2228 
2229     return NO_ERROR;
2230 }
2231 
assignMPP(ExynosDisplay * display,ExynosMPPSource * mppSource)2232 int32_t ExynosMPP::assignMPP(ExynosDisplay *display, ExynosMPPSource* mppSource)
2233 {
2234     mAssignedState |= MPP_ASSIGN_STATE_ASSIGNED;
2235 
2236     if (mMPPType == MPP_TYPE_OTF)
2237         mppSource->mOtfMPP = this;
2238     else if (mMPPType == MPP_TYPE_M2M)
2239         mppSource->mM2mMPP = this;
2240     else {
2241         MPP_LOGE("%s:: Invalid mppType(%d)", __func__, mMPPType);
2242         return -EINVAL;
2243     }
2244 
2245     mAssignedDisplay = display;
2246 
2247     /* Update information for used capacity */
2248     /* This should be called before mAssignedSources.add(mppSource) */
2249     bool needUpdateCapacity = addCapacity(mppSource);
2250 
2251     mAssignedSources.add(mppSource);
2252 
2253     MPP_LOGD(eDebugCapacity|eDebugMPP, "\tassigned to source(%p) type(%d), mAssignedSources(%zu)",
2254             mppSource, mppSource->mSourceType,
2255             mAssignedSources.size());
2256 
2257     if (needUpdateCapacity)
2258         updateUsedCapacity();
2259 
2260     if (mMaxSrcLayerNum > 1) {
2261         std::sort(mAssignedSources.begin(), mAssignedSources.end(), exynosMPPSourceComp);
2262     }
2263 
2264     return NO_ERROR;
2265 }
2266 
getSrcMaxBlendingNum(struct exynos_image __unused & src,struct exynos_image __unused & dst)2267 uint32_t ExynosMPP::getSrcMaxBlendingNum(struct exynos_image __unused &src, struct exynos_image __unused &dst)
2268 {
2269     uint32_t maxSrcLayerNum = mMaxSrcLayerNum;
2270     return maxSrcLayerNum;
2271 }
2272 
getAssignedSourceNum()2273 uint32_t ExynosMPP::getAssignedSourceNum()
2274 {
2275     return mAssignedSources.size();
2276 }
2277 
2278 /* Based on multi-resolution support */
setDstAllocSize(uint32_t width,uint32_t height)2279 void ExynosMPP::setDstAllocSize(uint32_t width, uint32_t height)
2280 {
2281     switch(width) {
2282     case 720:
2283         mDstAllocatedSize = ((height >= 1480) ? DST_SIZE_HD_PLUS : DST_SIZE_HD);
2284         break;
2285     case 1080:
2286         mDstAllocatedSize = ((height >= 2220) ? DST_SIZE_FHD_PLUS : DST_SIZE_FHD);
2287         break;
2288     case 1440:
2289         mDstAllocatedSize = ((height >= 2960) ? DST_SIZE_WQHD_PLUS : DST_SIZE_WQHD);
2290         break;
2291     default:
2292         mDstAllocatedSize = DST_SIZE_UNKNOWN;
2293         break;
2294     }
2295 }
2296 
getDstAllocSize()2297 dst_alloc_buf_size_t ExynosMPP::getDstAllocSize()
2298 {
2299     return mDstAllocatedSize;
2300 }
2301 
needPreAllocation()2302 bool ExynosMPP::needPreAllocation()
2303 {
2304     bool ret = false;
2305 
2306     if ((mLogicalType == MPP_LOGICAL_G2D_RGB) &&
2307         (mPreAssignDisplayList[mResourceManager->mDevice->mDisplayMode] == HWC_DISPLAY_PRIMARY_BIT))
2308         ret = true;
2309 
2310     return ret;
2311 }
2312 
isAssignableState(ExynosDisplay * display,struct exynos_image & src,struct exynos_image & dst)2313 bool ExynosMPP::isAssignableState(ExynosDisplay *display, struct exynos_image &src, struct exynos_image &dst)
2314 {
2315     bool isAssignable = false;
2316 
2317     if (mAssignedState == MPP_ASSIGN_STATE_FREE) {
2318         if (mHWState == MPP_HW_STATE_IDLE)
2319             isAssignable = true;
2320         else {
2321             if ((mPrevAssignedDisplayType < 0) ||
2322                 ((uint32_t)mPrevAssignedDisplayType == display->mType))
2323                 isAssignable = true;
2324             else
2325                 isAssignable = false;
2326         }
2327     }
2328 
2329     if ((mAssignedState & MPP_ASSIGN_STATE_ASSIGNED) && (mAssignedState & MPP_ASSIGN_STATE_RESERVED))
2330     {
2331         if (mReservedDisplay == (int32_t)display->getId()) {
2332             if (mAssignedSources.size() < getSrcMaxBlendingNum(src, dst))
2333                 isAssignable = true;
2334             else
2335                 isAssignable = false;
2336         } else {
2337             isAssignable = false;
2338         }
2339     } else if ((mAssignedState & MPP_ASSIGN_STATE_ASSIGNED) && !(mAssignedState & MPP_ASSIGN_STATE_RESERVED)) {
2340         if (mAssignedSources.size() < getSrcMaxBlendingNum(src, dst))
2341             isAssignable = true;
2342         else
2343             isAssignable = false;
2344     } else if (mAssignedState & MPP_ASSIGN_STATE_RESERVED) {
2345         if (mReservedDisplay == (int32_t)display->getId())
2346             isAssignable = true;
2347         else
2348             isAssignable = false;
2349     }
2350 
2351     MPP_LOGD(eDebugMPP, "\tisAssignableState(%d), assigned size(%zu), getSrcMaxBlendingNum(%d)",
2352             isAssignable, mAssignedSources.size(), getSrcMaxBlendingNum(src, dst));
2353     return isAssignable;
2354 }
2355 
isAssignable(ExynosDisplay * display,struct exynos_image & src,struct exynos_image & dst,float totalUsedCapacity)2356 bool ExynosMPP::isAssignable(ExynosDisplay *display, struct exynos_image &src,
2357                              struct exynos_image &dst, float totalUsedCapacity)
2358 {
2359     bool isAssignable = isAssignableState(display, src, dst);
2360     return (isAssignable && hasEnoughCapa(display, src, dst, totalUsedCapacity));
2361 }
2362 
hasEnoughCapa(ExynosDisplay * display,struct exynos_image & src,struct exynos_image & dst,float totalUsedCapacity)2363 bool ExynosMPP::hasEnoughCapa(ExynosDisplay *display, struct exynos_image &src,
2364                               struct exynos_image &dst, float totalUsedCapacity)
2365 {
2366     if (mCapacity == -1)
2367         return true;
2368 
2369     MPP_LOGD(eDebugCapacity | eDebugMPP, "totalUsedCapacity(%f), mUsedCapacity(%f)",
2370              totalUsedCapacity, mUsedCapacity);
2371 
2372     /* mUsedCapacity should be re-calculated including src, dst passed as parameters*/
2373     totalUsedCapacity -= mUsedCapacity;
2374 
2375     float requiredCapacity = getRequiredCapacity(display, src, dst);
2376 
2377     MPP_LOGD(eDebugCapacity | eDebugMPP, "mCapacity(%f), usedCapacity(%f), RequiredCapacity(%f)",
2378              mCapacity, totalUsedCapacity, requiredCapacity);
2379 
2380     if (mCapacity >= (totalUsedCapacity + requiredCapacity))
2381         return true;
2382     else if (isCapacityExceptionCondition(totalUsedCapacity, requiredCapacity, src))
2383         return true;
2384     else
2385         return false;
2386 }
2387 
isCapacityExceptionCondition(float totalUsedCapacity,float requiredCapacity,struct exynos_image & src)2388 bool ExynosMPP::isCapacityExceptionCondition(float totalUsedCapacity, float requiredCapacity,
2389                                              struct exynos_image &src)
2390 {
2391     if ((hasHdrInfo(src) && (totalUsedCapacity == 0) &&
2392          (requiredCapacity < (mCapacity * MPP_HDR_MARGIN)))) {
2393         return true;
2394     } else {
2395         return false;
2396     }
2397 }
2398 
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)2399 void ExynosMPP::getPPCIndex(const struct exynos_image &src,
2400         const struct exynos_image &dst,
2401         uint32_t &formatIndex, uint32_t &rotIndex, uint32_t &scaleIndex,
2402         const struct exynos_image &criteria)
2403 {
2404     formatIndex = 0;
2405     rotIndex = 0;
2406     scaleIndex = 0;
2407 
2408     /* Compare SBWC, AFBC and 10bitYUV420 first! because can be overlapped with other format */
2409     if (isFormatSBWC(criteria.format) && hasPPC(mPhysicalType, PPC_FORMAT_SBWC, PPC_ROT_NO))
2410         formatIndex = PPC_FORMAT_SBWC;
2411     else if (src.compressionInfo.type == COMP_TYPE_AFBC) {
2412         if ((isFormatRgb(criteria.format)) && hasPPC(mPhysicalType, PPC_FORMAT_AFBC_RGB, PPC_ROT_NO))
2413             formatIndex = PPC_FORMAT_AFBC_RGB;
2414         else if ((isFormatYUV(criteria.format)) && hasPPC(mPhysicalType, PPC_FORMAT_AFBC_YUV, PPC_ROT_NO))
2415             formatIndex = PPC_FORMAT_AFBC_YUV;
2416         else {
2417             formatIndex = PPC_FORMAT_RGB32;
2418             MPP_LOGW("%s:: AFBC PPC is not existed. Use default PPC", __func__);
2419         }
2420     } else if (isFormatP010(criteria.format) && hasPPC(mPhysicalType, PPC_FORMAT_P010, PPC_ROT_NO))
2421         formatIndex = PPC_FORMAT_P010;
2422     else if (isFormatYUV420(criteria.format) && hasPPC(mPhysicalType, PPC_FORMAT_YUV420, PPC_ROT_NO))
2423         formatIndex = PPC_FORMAT_YUV420;
2424     else if (isFormatYUV422(criteria.format) && hasPPC(mPhysicalType, PPC_FORMAT_YUV422, PPC_ROT_NO))
2425         formatIndex = PPC_FORMAT_YUV422;
2426     else
2427         formatIndex = PPC_FORMAT_RGB32;
2428 
2429     if (((criteria.transform & HAL_TRANSFORM_ROT_90) != 0) ||
2430         (mRotatedSrcCropBW > 0))
2431         rotIndex = PPC_ROT;
2432     else
2433         rotIndex = PPC_ROT_NO;
2434 
2435     uint32_t srcResolution = src.w * src.h;
2436     uint32_t dstResolution = dst.w * dst.h;
2437 
2438     if (mPhysicalType == MPP_G2D) {
2439         if (srcResolution == dstResolution) {
2440             scaleIndex = PPC_SCALE_NO;
2441         } else if (dstResolution > srcResolution) {
2442             /* scale up case */
2443             if (dstResolution >= (srcResolution * 4))
2444                 scaleIndex = PPC_SCALE_UP_4_;
2445             else
2446                 scaleIndex = PPC_SCALE_UP_1_4;
2447         } else {
2448             /* scale down case */
2449             if ((dstResolution * 16) <= srcResolution)
2450                 scaleIndex = PPC_SCALE_DOWN_16_;
2451             else if (((dstResolution * 9) <= srcResolution) &&
2452                     (srcResolution < (dstResolution * 16)))
2453                 scaleIndex = PPC_SCALE_DOWN_9_16;
2454             else if (((dstResolution * 4) <= srcResolution) &&
2455                     (srcResolution < (dstResolution * 9)))
2456                 scaleIndex = PPC_SCALE_DOWN_4_9;
2457             else
2458                 scaleIndex = PPC_SCALE_DOWN_1_4;
2459         }
2460     } else scaleIndex = 0; /* MSC doesn't refer scale Index */
2461 }
2462 
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)2463 float ExynosMPP::getPPC(const struct exynos_image &src,
2464         const struct exynos_image &dst, const struct exynos_image &criteria,
2465         const struct exynos_image *assignCheckSrc,
2466         const struct exynos_image *assignCheckDst)
2467 {
2468     float PPC = 0;
2469     uint32_t formatIndex = 0;
2470     uint32_t rotIndex = 0;
2471     uint32_t scaleIndex = 0;
2472 
2473     getPPCIndex(src, dst, formatIndex, rotIndex, scaleIndex, criteria);
2474 
2475     if ((rotIndex == PPC_ROT_NO) && (assignCheckSrc != NULL) &&
2476         ((assignCheckSrc->transform & HAL_TRANSFORM_ROT_90) != 0)) {
2477         rotIndex = PPC_ROT;
2478     }
2479 
2480     if (mPhysicalType == MPP_G2D || mPhysicalType == MPP_MSC) {
2481         if (hasPPC(mPhysicalType, formatIndex, rotIndex)) {
2482             PPC = ppc_table_map.at(PPC_IDX(mPhysicalType, formatIndex, rotIndex)).ppcList[scaleIndex];
2483         }
2484     }
2485 
2486     if (PPC == 0) {
2487         MPP_LOGE("%s:: mPhysicalType(%d), formatIndex(%d), rotIndex(%d), scaleIndex(%d), PPC(%f) is not valid",
2488                 __func__, mPhysicalType, formatIndex, rotIndex, scaleIndex, PPC);
2489         PPC = 0.000001;  /* It means can't use mPhysicalType H/W  */
2490     }
2491 
2492     MPP_LOGD(eDebugCapacity, "srcW(%d), srcH(%d), dstW(%d), dstH(%d), rot(%d)"
2493             "formatIndex(%d), rotIndex(%d), scaleIndex(%d), PPC(%f)",
2494             src.w, src.h, dst.w, dst.h, src.transform,
2495             formatIndex, rotIndex, scaleIndex, PPC);
2496     return PPC;
2497 }
2498 
getAssignedCapacity()2499 float ExynosMPP::getAssignedCapacity()
2500 {
2501     float capacity = 0;
2502     float baseCycles = 0;
2503     uint32_t rotIndex = 0;
2504 
2505     if (mPhysicalType != MPP_G2D)
2506         return 0;
2507 
2508     /*
2509      * Client target is assigned to m2mMPP
2510      * even if capacity is not enough
2511      */
2512     if ((mAssignedDisplay != NULL) &&
2513         (mAssignedDisplay->mType == HWC_DISPLAY_VIRTUAL))
2514         return 0;
2515 
2516 
2517     for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
2518         if ((mAssignedSources[i]->mSrcImg.transform & HAL_TRANSFORM_ROT_90) != 0)
2519             rotIndex = PPC_ROT;
2520     }
2521 
2522     MPP_LOGD(eDebugCapacity, "Check all of assigned layers cycles");
2523     /* PPC of layers that were added before should be changed */
2524     /* Check cycles of all assigned layers again */
2525     if ((mAssignedDisplay != NULL) && (mMaxSrcLayerNum > 1)) {
2526         baseCycles += ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL);
2527         MPP_LOGD(eDebugCapacity, "colorfill cycles: %f, total cycles: %f",
2528                 ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL), baseCycles);
2529     }
2530 
2531     for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
2532         float srcCycles = 0;
2533         uint32_t srcResolution = mAssignedSources[i]->mSrcImg.w * mAssignedSources[i]->mSrcImg.h;
2534         uint32_t dstResolution = mAssignedSources[i]->mMidImg.w * mAssignedSources[i]->mMidImg.h;
2535         uint32_t maxResolution = max(srcResolution, dstResolution);
2536         float PPC = 0;
2537 
2538         if (mAssignedSources[i]->mSrcImg.layerFlags & EXYNOS_HWC_DIM_LAYER) {
2539             PPC = G2D_BASE_PPC_COLORFILL;
2540         } else {
2541             PPC = getPPC(mAssignedSources[i]->mSrcImg, mAssignedSources[i]->mMidImg, mAssignedSources[i]->mSrcImg,
2542                     &mAssignedSources[i]->mSrcImg, &mAssignedSources[i]->mMidImg);
2543         }
2544         srcCycles = maxResolution/PPC;
2545 
2546         /* Hdr and drm layer is exception */
2547         if ((hasHdrInfo(mAssignedSources[i]->mSrcImg) ||
2548             (getDrmMode(mAssignedSources[i]->mSrcImg.usageFlags) != NO_DRM))) {
2549             MPP_LOGD(eDebugCapacity, "Src[%d] is skipped(drm or hdr), cycles: %f, PPC: %f, srcResolution: %d, dstResolution: %d, rot(%d)",
2550                     i, srcCycles, PPC, srcResolution, dstResolution, mAssignedSources[i]->mSrcImg.transform);
2551             continue;
2552         }
2553 
2554         baseCycles += srcCycles;
2555 
2556         MPP_LOGD(eDebugCapacity, "Src[%d] cycles: %f, total cycles: %f, PPC: %f, srcResolution: %d, dstResolution: %d, rot(%d)",
2557                 i, srcCycles, baseCycles, PPC, srcResolution, dstResolution, mAssignedSources[i]->mSrcImg.transform);
2558     }
2559 
2560     capacity = baseCycles / mClockKhz;
2561 
2562     return capacity;
2563 }
2564 
getRequiredCapacity(ExynosDisplay * display,struct exynos_image & src,struct exynos_image & dst)2565 float ExynosMPP::getRequiredCapacity(ExynosDisplay *display, struct exynos_image &src,
2566         struct exynos_image &dst)
2567 {
2568     float capacity = 0;
2569     float cycles = 0;
2570     if (mPhysicalType == MPP_G2D) {
2571         /* Initialize value with the cycles that were already assigned */
2572         float baseCycles = mUsedBaseCycles;
2573         float srcCycles = 0;
2574         uint32_t srcResolution = src.w * src.h;
2575         uint32_t dstResolution = dst.w * dst.h;
2576         uint32_t maxResolution = max(srcResolution, dstResolution);
2577         float curBaseCycles = 0;
2578         float PPC = 0;
2579 
2580         if ((mAssignedSources.size() == 0) ||
2581             (mRotatedSrcCropBW != 0) ||
2582             ((mRotatedSrcCropBW == 0) &&
2583              ((src.transform & HAL_TRANSFORM_ROT_90) == 0))) {
2584             /* Just add cycles for current layer */
2585             if ((mAssignedSources.size() == 0) &&
2586                 (display != NULL) && (mMaxSrcLayerNum > 1)) {
2587                 curBaseCycles = ((display->mXres * display->mYres) / G2D_BASE_PPC_COLORFILL);
2588                 MPP_LOGD(eDebugCapacity, "There is no assigned layer. Colorfill cycles: %f should be added",
2589                         curBaseCycles);
2590             }
2591             curBaseCycles += getRequiredBaseCycles(src, dst);
2592             baseCycles += curBaseCycles;
2593             MPP_LOGD(eDebugCapacity, "mUsedBaseCycles was %f, Add base cycles %f, totalBaseCycle(%f)",
2594                     mUsedBaseCycles, curBaseCycles, baseCycles);
2595         } else {
2596             /* Recalculate cycles for all of layers */
2597             baseCycles = 0;
2598             MPP_LOGD(eDebugCapacity, "Check all of assigned layers cycles");
2599             /* PPC of layers that were added before should be changed */
2600             /* Check cycles of all assigned layers again */
2601             if ((display != NULL) && (mMaxSrcLayerNum > 1)) {
2602                 baseCycles += ((display->mXres * display->mYres) / G2D_BASE_PPC_COLORFILL);
2603                 MPP_LOGD(eDebugCapacity, "colorfill cycles: %f, total cycles: %f",
2604                         ((display->mXres * display->mYres) / G2D_BASE_PPC_COLORFILL), cycles);
2605             }
2606 
2607             for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
2608                 float assignedSrcCycles = 0;
2609                 uint32_t assignedSrcResolution = mAssignedSources[i]->mSrcImg.w * mAssignedSources[i]->mSrcImg.h;
2610                 uint32_t assignedDstResolution = mAssignedSources[i]->mMidImg.w * mAssignedSources[i]->mMidImg.h;
2611                 uint32_t assignedMaxResolution = max(assignedSrcResolution, assignedDstResolution);
2612                 float assignedPPC = getPPC(mAssignedSources[i]->mSrcImg, mAssignedSources[i]->mMidImg,
2613                         mAssignedSources[i]->mSrcImg, &src, &dst);
2614 
2615                 assignedSrcCycles = assignedMaxResolution/assignedPPC;
2616                 baseCycles += assignedSrcCycles;
2617 
2618                 MPP_LOGD(eDebugCapacity, "Src[%d] cycles: %f, total cycles: %f, PPC: %f, srcResolution: %d, dstResolution: %d, rot(%d)",
2619                         i, assignedSrcCycles, baseCycles, assignedPPC, assignedSrcResolution, assignedDstResolution, mAssignedSources[i]->mSrcImg.transform);
2620             }
2621 
2622             PPC = getPPC(src, dst, src, &src, &dst);
2623 
2624             srcCycles = maxResolution/PPC;
2625             baseCycles += srcCycles;
2626 
2627             MPP_LOGD(eDebugCapacity, "check mppSource cycles: %f, total cycles: %f, PPC: %f, srcResolution: %d, dstResolution: %d, rot(%d)",
2628                     srcCycles, baseCycles, PPC, srcResolution, dstResolution, src.transform);
2629         }
2630 
2631         capacity = baseCycles / mClockKhz;
2632 
2633         MPP_LOGD(eDebugCapacity, "baseCycles: %f, capacity: %f",
2634                 baseCycles, capacity);
2635     } else if (mPhysicalType == MPP_MSC) {
2636         /* Initialize value with the capacity that were already assigned */
2637         capacity = mUsedCapacity;
2638 
2639         /* Just add capacity for current layer */
2640         float srcPPC = getPPC(src, dst, src);
2641         float dstPPC = getPPC(src, dst, dst);
2642         float srcCapacity = (float((src.w * src.h))) / (mClockKhz * srcPPC);
2643         float dstCapacity = (float((dst.w * dst.h))) / (mClockKhz * dstPPC);
2644 
2645         capacity += max(srcCapacity, dstCapacity);
2646 
2647     }
2648 
2649     return capacity;
2650 }
2651 
getRequiredBaseCycles(struct exynos_image & src,struct exynos_image & dst)2652 float ExynosMPP::getRequiredBaseCycles(struct exynos_image &src, struct exynos_image &dst)
2653 {
2654     if (mPhysicalType != MPP_G2D)
2655         return 0;
2656 
2657     uint32_t srcResolution = src.w * src.h;
2658     uint32_t dstResolution = dst.w * dst.h;
2659     uint32_t maxResolution = max(srcResolution, dstResolution);
2660 
2661     return maxResolution/(float)getPPC(src, dst, src);
2662 }
2663 
addCapacity(ExynosMPPSource * mppSource)2664 bool ExynosMPP::addCapacity(ExynosMPPSource* mppSource)
2665 {
2666     if ((mppSource == NULL) || mCapacity == -1)
2667         return false;
2668 
2669     if (mPhysicalType == MPP_G2D) {
2670         bool needUpdateCapacity = true;
2671         if ((mAssignedSources.size() == 0) ||
2672             (mRotatedSrcCropBW != 0) ||
2673             ((mRotatedSrcCropBW == 0) &&
2674              ((mppSource->mSrcImg.transform & HAL_TRANSFORM_ROT_90) == 0))) {
2675             needUpdateCapacity = false;
2676         }
2677 
2678         if (needUpdateCapacity)
2679             return true;
2680 
2681         if ((mMaxSrcLayerNum > 1) &&
2682             (mAssignedSources.size() == 0)) {
2683             if (mAssignedDisplay != NULL) {
2684                 /* This will be the first mppSource that is assigned to the ExynosMPP */
2685                 /* Add capacity for background */
2686                 mUsedBaseCycles += ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL);
2687                 MPP_LOGD(eDebugCapacity, "\tcolorfill cycles: %f, total cycles: %f",
2688                         ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL), mUsedBaseCycles);
2689             } else {
2690                 MPP_LOGE("mAssignedDisplay is null");
2691             }
2692         }
2693 
2694         float baseCycles = getRequiredBaseCycles(mppSource->mSrcImg, mppSource->mMidImg);
2695         mUsedBaseCycles += baseCycles;
2696 
2697         uint32_t srcResolution = mppSource->mSrcImg.w * mppSource->mSrcImg.h;
2698         uint32_t dstResolution = mppSource->mMidImg.w * mppSource->mMidImg.h;
2699         if ((mppSource->mSrcImg.transform & HAL_TRANSFORM_ROT_90) == 0)
2700             mNoRotatedSrcCropBW += srcResolution;
2701         else
2702             mRotatedSrcCropBW += srcResolution;
2703 
2704         mUsedCapacity = mUsedBaseCycles / mClockKhz;
2705 
2706         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)",
2707                 mAssignedSources.size(),
2708                 baseCycles, mUsedBaseCycles, mUsedCapacity, srcResolution, dstResolution,
2709                 mppSource->mSrcImg.transform, mNoRotatedSrcCropBW, mRotatedSrcCropBW);
2710     } else if (mPhysicalType == MPP_MSC) {
2711         mUsedCapacity = getRequiredCapacity(NULL, mppSource->mSrcImg, mppSource->mMidImg);
2712     }
2713 
2714     return false;
2715 }
2716 
removeCapacity(ExynosMPPSource * mppSource)2717 bool ExynosMPP::removeCapacity(ExynosMPPSource* mppSource)
2718 {
2719     if ((mppSource == NULL) || (mCapacity == -1))
2720         return false;
2721 
2722     if (mPhysicalType == MPP_G2D) {
2723         uint32_t srcResolution = mppSource->mSrcImg.w * mppSource->mSrcImg.h;
2724         uint32_t dstResolution = mppSource->mDstImg.w * mppSource->mDstImg.h;
2725 
2726         uint32_t prevRotatedSrcCropBW = mRotatedSrcCropBW;
2727 
2728         if (mppSource->mSrcImg.transform == 0)
2729             mNoRotatedSrcCropBW -= srcResolution;
2730         else
2731             mRotatedSrcCropBW -= srcResolution;
2732 
2733         if ((prevRotatedSrcCropBW > 0) && (mRotatedSrcCropBW == 0))
2734             return true;
2735 
2736         float baseCycles = getRequiredBaseCycles(mppSource->mSrcImg, mppSource->mMidImg);
2737         mUsedBaseCycles -= baseCycles;
2738 
2739         mUsedCapacity = mUsedBaseCycles / mClockKhz;
2740 
2741         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)",
2742                 mAssignedSources.size(),
2743                 baseCycles, mUsedBaseCycles, mUsedCapacity, srcResolution, dstResolution,
2744                 mppSource->mSrcImg.transform, mNoRotatedSrcCropBW, mRotatedSrcCropBW);
2745     } else if (mPhysicalType == MPP_MSC) {
2746         exynos_image &src = mppSource->mSrcImg;
2747         exynos_image &dst = mppSource->mDstImg;
2748         uint32_t srcResolution = src.w * src.h;
2749         uint32_t dstResolution = dst.w * dst.h;
2750 
2751         float srcCapacity = (float)srcResolution / getPPC(src, dst, src);
2752         float dstCapacity  = (float)dstResolution  / getPPC(src, dst, dst);
2753 
2754         mUsedCapacity -= max(srcCapacity, dstCapacity);
2755     }
2756 
2757     return false;
2758 }
2759 
resetUsedCapacity()2760 void ExynosMPP::resetUsedCapacity()
2761 {
2762     mUsedCapacity = 0;
2763     mUsedBaseCycles = 0;
2764     mRotatedSrcCropBW = 0;
2765     mNoRotatedSrcCropBW = 0;
2766 }
2767 
updateUsedCapacity()2768 int32_t ExynosMPP::updateUsedCapacity()
2769 {
2770     int32_t ret = NO_ERROR;
2771     if (mCapacity == -1)
2772         return ret;
2773 
2774     float capacity = 0;
2775     mUsedCapacity = 0;
2776 
2777     mRotatedSrcCropBW = 0;
2778     mNoRotatedSrcCropBW = 0;
2779 
2780     if ((mPhysicalType == MPP_G2D) &&
2781         (mAssignedDisplay != NULL) &&
2782         (mAssignedSources.size() > 0)) {
2783         float cycles = 0;
2784 
2785         if (mMaxSrcLayerNum > 1) {
2786             cycles += ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL);
2787             MPP_LOGD(eDebugCapacity, "\tcolorfill cycles: %f, total cycles: %f",
2788                     ((mAssignedDisplay->mXres * mAssignedDisplay->mYres) / G2D_BASE_PPC_COLORFILL), cycles);
2789         }
2790         for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
2791             uint32_t srcResolution = mAssignedSources[i]->mSrcImg.w * mAssignedSources[i]->mSrcImg.h;
2792             if ((mAssignedSources[i]->mSrcImg.transform & HAL_TRANSFORM_ROT_90) == 0)
2793                 mNoRotatedSrcCropBW += srcResolution;
2794             else
2795                 mRotatedSrcCropBW += srcResolution;
2796         }
2797         MPP_LOGD(eDebugCapacity, "mNoRotatedSrcCropBW(%d), mRotatedSrcCropBW(%d)",
2798                 mNoRotatedSrcCropBW, mRotatedSrcCropBW);
2799         for (uint32_t i = 0; i < mAssignedSources.size(); i++) {
2800             float srcCycles = 0;
2801             uint32_t srcResolution = mAssignedSources[i]->mSrcImg.w * mAssignedSources[i]->mSrcImg.h;
2802             uint32_t dstResolution = mAssignedSources[i]->mMidImg.w * mAssignedSources[i]->mMidImg.h;
2803             uint32_t maxResolution = max(srcResolution, dstResolution);
2804             float PPC = getPPC(mAssignedSources[i]->mSrcImg, mAssignedSources[i]->mMidImg, mAssignedSources[i]->mSrcImg);
2805             srcCycles = maxResolution/PPC;
2806             cycles += srcCycles;
2807 
2808             MPP_LOGD(eDebugCapacity, "Src[%d] cycles: %f, total cycles: %f, PPC: %f, srcResolution: %d, dstResolution: %d, rot(%d)",
2809                     i, srcCycles, cycles, PPC, srcResolution, dstResolution, mAssignedSources[i]->mSrcImg.transform);
2810         }
2811 
2812         mUsedBaseCycles = cycles;
2813         capacity = cycles / mClockKhz;
2814 
2815         mUsedCapacity = capacity;
2816 
2817     }
2818     MPP_LOGD(eDebugCapacity, "assigned layer size(%zu), mUsedCapacity: %f", mAssignedSources.size(), mUsedCapacity);
2819 
2820     return mUsedCapacity;
2821 }
2822 
getRestrictionClassification(const struct exynos_image & img) const2823 uint32_t ExynosMPP::getRestrictionClassification(const struct exynos_image &img) const {
2824     return !!(isFormatRgb(img.format) == false);
2825 }
2826 
prioritize(int priority)2827 int ExynosMPP::prioritize(int priority)
2828 {
2829     if ((mPhysicalType != MPP_G2D) ||
2830         (mAcrylicHandle == NULL)) {
2831         MPP_LOGE("invalid function call");
2832         return -1;
2833     }
2834     int ret = NO_ERROR;
2835     ret = mAcrylicHandle->prioritize(priority);
2836 
2837     if ((priority > 0) && (ret == 1))
2838     {
2839         /* G2D Driver returned EBUSY */
2840         mHWBusyFlag = true;
2841     }
2842     MPP_LOGD(eDebugMPP, "set resource prioritize (%d), ret(%d), mHWBusyFlag(%d)", priority, ret, mHWBusyFlag);
2843 
2844     return ret;
2845 }
2846 
increaseDstBuffIndex()2847 uint32_t ExynosMPP::increaseDstBuffIndex()
2848 {
2849     if (mAllocOutBufFlag)
2850         mCurrentDstBuf = (mCurrentDstBuf + 1) % NUM_MPP_DST_BUFS(mLogicalType);
2851     return mCurrentDstBuf;
2852 }
2853 
reloadResourceForHWFC()2854 void ExynosMPP::reloadResourceForHWFC()
2855 {
2856     ALOGI("reloadResourceForHWFC()");
2857     delete mAcrylicHandle;
2858     mAcrylicHandle = AcrylicFactory::createAcrylic("default_compositor");
2859     if (mAcrylicHandle == NULL) {
2860         MPP_LOGE("Fail to allocate compositor");
2861     } else {
2862         mAcrylicHandle->setDefaultColor(0, 0, 0, 0);
2863         MPP_LOGI("The resource is reloaded for HWFC: %p", mAcrylicHandle);
2864     }
2865     for (uint32_t i = 0; i < NUM_MPP_SRC_BUFS; i++)
2866     {
2867         if (mSrcImgs[i].mppLayer != NULL) {
2868             delete mSrcImgs[i].mppLayer;
2869             mSrcImgs[i].mppLayer = NULL;
2870         }
2871     }
2872 }
2873 
setTargetDisplayLuminance(uint16_t min,uint16_t max)2874 void ExynosMPP::setTargetDisplayLuminance(uint16_t min, uint16_t max)
2875 {
2876     MPP_LOGD(eDebugMPP, "%s: min(%d), max(%d)", __func__, min, max);
2877     if (mAcrylicHandle == NULL) {
2878         MPP_LOGE("mAcrylicHandle is NULL");
2879     } else
2880         mAcrylicHandle->setTargetDisplayLuminance(min, max);
2881 }
2882 
setTargetDisplayDevice(int device)2883 void ExynosMPP::setTargetDisplayDevice(int device)
2884 {
2885     ALOGI("%s: device(%d)", __func__, device);
2886     if (mAcrylicHandle == NULL) {
2887         MPP_LOGE("mAcrylicHandle is NULL");
2888     } else
2889         mAcrylicHandle->setTargetDisplayInfo(&device);
2890 }
2891 
dump(String8 & result)2892 void ExynosMPP::dump(String8& result)
2893 {
2894     int32_t assignedDisplayType = -1;
2895     if (mAssignedDisplay != NULL)
2896         assignedDisplayType = mAssignedDisplay->mType;
2897 
2898     result.appendFormat("%s: types mppType(%d), (p:%d, l:0x%2x), indexs(p:%d, l:%d), preAssignDisplay(0x%2x)\n",
2899             mName.string(), mMPPType, mPhysicalType, mLogicalType, mPhysicalIndex, mLogicalIndex, mPreAssignDisplayInfo);
2900     result.appendFormat("\tEnable: %d, HWState: %d, AssignedState: %d, assignedDisplay(%d)\n",
2901             mEnable, mHWState, mAssignedState, assignedDisplayType);
2902     result.appendFormat("\tPrevAssignedState: %d, PrevAssignedDisplayType: %d, ReservedDisplay: %d\n",
2903             mPrevAssignedState, mPrevAssignedDisplayType, mReservedDisplay);
2904     result.appendFormat("\tassinedSourceNum(%zu), Capacity(%f), CapaUsed(%f), mCurrentDstBuf(%d)\n",
2905             mAssignedSources.size(), mCapacity, mUsedCapacity, mCurrentDstBuf);
2906 
2907 }
2908 
closeFences()2909 void ExynosMPP::closeFences()
2910 {
2911     for (uint32_t i = 0; i < mAssignedSources.size(); i++)
2912     {
2913         mSrcImgs[i].acrylicAcquireFenceFd =
2914             fence_close(mSrcImgs[i].acrylicAcquireFenceFd, mAssignedDisplay,
2915                     FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_G2D);
2916         mSrcImgs[i].acrylicReleaseFenceFd =
2917             fence_close(mSrcImgs[i].acrylicReleaseFenceFd, mAssignedDisplay,
2918                     FENCE_TYPE_SRC_RELEASE, FENCE_IP_G2D);
2919     }
2920 
2921     mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd =
2922         fence_close(mDstImgs[mCurrentDstBuf].acrylicAcquireFenceFd, mAssignedDisplay,
2923                 FENCE_TYPE_DST_ACQUIRE, FENCE_IP_G2D);
2924     mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd =
2925         fence_close(mDstImgs[mCurrentDstBuf].acrylicReleaseFenceFd, mAssignedDisplay,
2926                 FENCE_TYPE_DST_RELEASE, FENCE_IP_G2D);
2927 }
2928 
updateAttr()2929 void ExynosMPP::updateAttr()
2930 {
2931     MPP_LOGD(eDebugAttrSetting, "updateAttr::mPhysicalType(%d), mAttr(0x%" PRIx64 ")",
2932             mPhysicalType, mAttr);
2933 
2934     if (mResourceManager == NULL) return;
2935 
2936     auto iter = mResourceManager->mMPPAttrs.find(mPhysicalType);
2937     if (iter != mResourceManager->mMPPAttrs.end()) {
2938         mAttr = iter->second;
2939         MPP_LOGD(eDebugAttrSetting, "After mAttr(0x%" PRIx64 ")", mAttr);
2940     }
2941 }
2942 
updatePreassignedDisplay(uint32_t fromDisplayBit,uint32_t toDisplayBit)2943 void ExynosMPP::updatePreassignedDisplay(uint32_t fromDisplayBit, uint32_t toDisplayBit)
2944 {
2945     /*
2946      * If the pre-assigned resources are required to changed,
2947      * this function will modify PreAssign table.
2948      */
2949     for (uint32_t i = 0; i < DISPLAY_MODE_NUM; i++) {
2950         if (mPreAssignDisplayList[i] == fromDisplayBit)
2951             mPreAssignDisplayList[i] = toDisplayBit;
2952     }
2953 
2954     if (mPreAssignDisplayInfo == fromDisplayBit)
2955         mPreAssignDisplayInfo = toDisplayBit;
2956 }
2957