1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #include <math.h>
19 #include <mdp_version.h>
20 #include "overlayUtils.h"
21 #include "overlayMdp.h"
22 #include "mdp_version.h"
23 #include <overlay.h>
24 #include <dlfcn.h>
25 
26 #define HSIC_SETTINGS_DEBUG 0
27 
28 using namespace qdutils;
29 
isEqual(float f1,float f2)30 static inline bool isEqual(float f1, float f2) {
31         return ((int)(f1*100) == (int)(f2*100)) ? true : false;
32 }
33 
34 namespace ovutils = overlay::utils;
35 namespace overlay {
36 
init(const int & dpy)37 bool MdpCtrl::init(const int& dpy) {
38     int fbnum = Overlay::getFbForDpy(dpy);
39     if( fbnum < 0 ) {
40         ALOGE("%s: Invalid FB for the display: %d",__FUNCTION__, dpy);
41         return false;
42     }
43 
44     // FD init
45     if(!utils::openDev(mFd, fbnum,
46                 Res::fbPath, O_RDWR)){
47         ALOGE("Ctrl failed to init fbnum=%d", fbnum);
48         return false;
49     }
50     mDpy = dpy;
51     return true;
52 }
53 
reset()54 void MdpCtrl::reset() {
55     utils::memset0(mOVInfo);
56     mOVInfo.id = MSMFB_NEW_REQUEST;
57     mOrientation = utils::OVERLAY_TRANSFORM_0;
58     mDpy = 0;
59 #ifdef USES_POST_PROCESSING
60     memset(&mParams, 0, sizeof(struct compute_params));
61     mParams.params.conv_params.order = hsic_order_hsc_i;
62     mParams.params.conv_params.interface = interface_rec601;
63     mParams.params.conv_params.cc_matrix[0][0] = 1;
64     mParams.params.conv_params.cc_matrix[1][1] = 1;
65     mParams.params.conv_params.cc_matrix[2][2] = 1;
66 #endif
67 }
68 
close()69 bool MdpCtrl::close() {
70     bool result = true;
71     if(MSMFB_NEW_REQUEST != static_cast<int>(mOVInfo.id)) {
72         if(!mdp_wrapper::unsetOverlay(mFd.getFD(), mOVInfo.id)) {
73             ALOGE("MdpCtrl close error in unset");
74             result = false;
75         }
76     }
77 #ifdef USES_POST_PROCESSING
78     /* free allocated memory in PP */
79     if (mOVInfo.overlay_pp_cfg.igc_cfg.c0_c1_data)
80             free(mOVInfo.overlay_pp_cfg.igc_cfg.c0_c1_data);
81 #endif
82     reset();
83 
84     if(!mFd.close()) {
85         result = false;
86     }
87 
88     return result;
89 }
90 
setSource(const utils::PipeArgs & args)91 void MdpCtrl::setSource(const utils::PipeArgs& args) {
92     setSrcWhf(args.whf);
93 
94     //TODO These are hardcoded. Can be moved out of setSource.
95     mOVInfo.transp_mask = 0xffffffff;
96 
97     //TODO These calls should ideally be a part of setPipeParams API
98     setFlags(args.mdpFlags);
99     setZ(args.zorder);
100     setPlaneAlpha(args.planeAlpha);
101     setBlending(args.blending);
102 }
103 
setCrop(const utils::Dim & d)104 void MdpCtrl::setCrop(const utils::Dim& d) {
105     setSrcRectDim(d);
106 }
107 
setColor(const uint32_t color)108 void MdpCtrl::setColor(const uint32_t color) {
109     mOVInfo.bg_color = color;
110 }
111 
setPosition(const overlay::utils::Dim & d)112 void MdpCtrl::setPosition(const overlay::utils::Dim& d) {
113     setDstRectDim(d);
114 }
115 
setTransform(const utils::eTransform & orient)116 void MdpCtrl::setTransform(const utils::eTransform& orient) {
117     int rot = utils::getMdpOrient(orient);
118     setUserData(rot);
119     mOrientation = static_cast<utils::eTransform>(rot);
120 }
121 
setPipeType(const utils::eMdpPipeType & pType)122 void MdpCtrl::setPipeType(const utils::eMdpPipeType& pType){
123     switch((int) pType){
124         case utils::OV_MDP_PIPE_RGB:
125             mOVInfo.pipe_type = PIPE_TYPE_RGB;
126             break;
127         case utils::OV_MDP_PIPE_VG:
128             mOVInfo.pipe_type = PIPE_TYPE_VIG;
129             break;
130         case utils::OV_MDP_PIPE_DMA:
131             mOVInfo.pipe_type = PIPE_TYPE_DMA;
132             break;
133         default:
134             mOVInfo.pipe_type = PIPE_TYPE_AUTO;
135             break;
136     }
137 }
138 
doTransform()139 void MdpCtrl::doTransform() {
140     setRotationFlags();
141     utils::Whf whf = getSrcWhf();
142     utils::Dim dim = getSrcRectDim();
143     utils::preRotateSource(mOrientation, whf, dim);
144     setSrcWhf(whf);
145     setSrcRectDim(dim);
146 }
147 
doDownscale()148 void MdpCtrl::doDownscale() {
149     if(MDPVersion::getInstance().supportsDecimation()) {
150         utils::getDecimationFactor(mOVInfo.src_rect.w, mOVInfo.src_rect.h,
151                 mOVInfo.dst_rect.w, mOVInfo.dst_rect.h, mOVInfo.horz_deci,
152                 mOVInfo.vert_deci);
153     }
154 }
155 
set()156 bool MdpCtrl::set() {
157     int mdpVersion = MDPVersion::getInstance().getMDPVersion();
158     //deferred calcs, so APIs could be called in any order.
159     doTransform();
160     utils::Whf whf = getSrcWhf();
161     if(utils::isYuv(whf.format)) {
162         utils::normalizeCrop(mOVInfo.src_rect.x, mOVInfo.src_rect.w);
163         utils::normalizeCrop(mOVInfo.src_rect.y, mOVInfo.src_rect.h);
164         if(mdpVersion < MDSS_V5) {
165             utils::even_floor(mOVInfo.dst_rect.w);
166             utils::even_floor(mOVInfo.dst_rect.h);
167         } else if (mOVInfo.flags & MDP_DEINTERLACE) {
168             // For interlaced, crop.h should be 4-aligned
169             if (!(mOVInfo.flags & MDP_SOURCE_ROTATED_90) &&
170                 (mOVInfo.src_rect.h % 4))
171                 mOVInfo.src_rect.h = utils::aligndown(mOVInfo.src_rect.h, 4);
172             // For interlaced, width must be multiple of 4 when rotated 90deg.
173             else if ((mOVInfo.flags & MDP_SOURCE_ROTATED_90) &&
174                 (mOVInfo.src_rect.w % 4))
175                 mOVInfo.src_rect.w = utils::aligndown(mOVInfo.src_rect.w, 4);
176         }
177     } else {
178         if (mdpVersion >= MDSS_V5) {
179             // Check for 1-pixel down-scaling
180             if (mOVInfo.src_rect.w - mOVInfo.dst_rect.w == 1)
181                 mOVInfo.src_rect.w -= 1;
182             if (mOVInfo.src_rect.h - mOVInfo.dst_rect.h == 1)
183                 mOVInfo.src_rect.h -= 1;
184         }
185     }
186 
187     doDownscale();
188     return true;
189 }
190 
191 //Update src format based on rotator's destination format.
updateSrcFormat(const uint32_t & rotDestFmt)192 void MdpCtrl::updateSrcFormat(const uint32_t& rotDestFmt) {
193     utils::Whf whf = getSrcWhf();
194     whf.format =  rotDestFmt;
195     setSrcWhf(whf);
196 }
197 
dump() const198 void MdpCtrl::dump() const {
199     ALOGE("== Dump MdpCtrl start ==");
200     mFd.dump();
201     mdp_wrapper::dump("mOVInfo", mOVInfo);
202     ALOGE("== Dump MdpCtrl end ==");
203 }
204 
getDump(char * buf,size_t len)205 void MdpCtrl::getDump(char *buf, size_t len) {
206     ovutils::getDump(buf, len, "Ctrl", mOVInfo);
207 }
208 
dump() const209 void MdpData::dump() const {
210     ALOGE("== Dump MdpData start ==");
211     mFd.dump();
212     mdp_wrapper::dump("mOvData", mOvData);
213     ALOGE("== Dump MdpData end ==");
214 }
215 
getDump(char * buf,size_t len)216 void MdpData::getDump(char *buf, size_t len) {
217     ovutils::getDump(buf, len, "Data", mOvData);
218 }
219 
setVisualParams(const MetaData_t & data)220 bool MdpCtrl::setVisualParams(const MetaData_t& data) {
221     ALOGD_IF(0, "In %s: data.operation = %d", __FUNCTION__, data.operation);
222 
223     // Set Color Space for MDP to configure CSC matrix
224     mOVInfo.color_space = ITU_R_601;
225     if (data.operation & UPDATE_COLOR_SPACE) {
226         mOVInfo.color_space = data.colorSpace;
227     }
228 
229 #ifdef USES_POST_PROCESSING
230     bool needUpdate = false;
231     /* calculate the data */
232     if (data.operation & PP_PARAM_HSIC) {
233         if (mParams.params.pa_params.hue != data.hsicData.hue) {
234             ALOGD_IF(HSIC_SETTINGS_DEBUG,
235                 "Hue has changed from %d to %d",
236                 mParams.params.pa_params.hue,data.hsicData.hue);
237             needUpdate = true;
238         }
239 
240         if (!isEqual(mParams.params.pa_params.sat,
241             data.hsicData.saturation)) {
242             ALOGD_IF(HSIC_SETTINGS_DEBUG,
243                 "Saturation has changed from %f to %f",
244                 mParams.params.pa_params.sat,
245                 data.hsicData.saturation);
246             needUpdate = true;
247         }
248 
249         if (mParams.params.pa_params.intensity != data.hsicData.intensity) {
250             ALOGD_IF(HSIC_SETTINGS_DEBUG,
251                 "Intensity has changed from %d to %d",
252                 mParams.params.pa_params.intensity,
253                 data.hsicData.intensity);
254             needUpdate = true;
255         }
256 
257         if (!isEqual(mParams.params.pa_params.contrast,
258             data.hsicData.contrast)) {
259             ALOGD_IF(HSIC_SETTINGS_DEBUG,
260                 "Contrast has changed from %f to %f",
261                 mParams.params.pa_params.contrast,
262                 data.hsicData.contrast);
263             needUpdate = true;
264         }
265 
266         if (needUpdate) {
267             mParams.params.pa_params.hue = (float)data.hsicData.hue;
268             mParams.params.pa_params.sat = data.hsicData.saturation;
269             mParams.params.pa_params.intensity = data.hsicData.intensity;
270             mParams.params.pa_params.contrast = data.hsicData.contrast;
271             mParams.params.pa_params.ops = MDP_PP_OPS_WRITE | MDP_PP_OPS_ENABLE;
272             mParams.operation |= PP_OP_PA;
273         }
274     }
275 
276     if (data.operation & PP_PARAM_SHARP2) {
277         if (mParams.params.sharp_params.strength != data.Sharp2Data.strength) {
278             needUpdate = true;
279         }
280         if (mParams.params.sharp_params.edge_thr != data.Sharp2Data.edge_thr) {
281             needUpdate = true;
282         }
283         if (mParams.params.sharp_params.smooth_thr !=
284                 data.Sharp2Data.smooth_thr) {
285             needUpdate = true;
286         }
287         if (mParams.params.sharp_params.noise_thr !=
288                 data.Sharp2Data.noise_thr) {
289             needUpdate = true;
290         }
291 
292         if (needUpdate) {
293             mParams.params.sharp_params.strength = data.Sharp2Data.strength;
294             mParams.params.sharp_params.edge_thr = data.Sharp2Data.edge_thr;
295             mParams.params.sharp_params.smooth_thr =
296                 data.Sharp2Data.smooth_thr;
297             mParams.params.sharp_params.noise_thr = data.Sharp2Data.noise_thr;
298             mParams.params.sharp_params.ops =
299                 MDP_PP_OPS_WRITE | MDP_PP_OPS_ENABLE;
300             mParams.operation |= PP_OP_SHARP;
301         }
302     }
303 
304     if (data.operation & PP_PARAM_IGC) {
305         if (mOVInfo.overlay_pp_cfg.igc_cfg.c0_c1_data == NULL){
306             uint32_t *igcData
307                 = (uint32_t *)malloc(2 * MAX_IGC_LUT_ENTRIES * sizeof(uint32_t));
308             if (!igcData) {
309                 ALOGE("IGC storage allocated failed");
310                 return false;
311             }
312             mOVInfo.overlay_pp_cfg.igc_cfg.c0_c1_data = igcData;
313             mOVInfo.overlay_pp_cfg.igc_cfg.c2_data
314                 = igcData + MAX_IGC_LUT_ENTRIES;
315         }
316 
317         memcpy(mParams.params.igc_lut_params.c0,
318             data.igcData.c0, sizeof(uint16_t) * MAX_IGC_LUT_ENTRIES);
319         memcpy(mParams.params.igc_lut_params.c1,
320             data.igcData.c1, sizeof(uint16_t) * MAX_IGC_LUT_ENTRIES);
321         memcpy(mParams.params.igc_lut_params.c2,
322             data.igcData.c2, sizeof(uint16_t) * MAX_IGC_LUT_ENTRIES);
323 
324         mParams.params.igc_lut_params.ops
325             = MDP_PP_OPS_WRITE | MDP_PP_OPS_ENABLE;
326         mParams.operation |= PP_OP_IGC;
327         needUpdate = true;
328     }
329 
330     if (data.operation & PP_PARAM_VID_INTFC) {
331         mParams.params.conv_params.interface =
332             (interface_type) data.video_interface;
333         needUpdate = true;
334     }
335 
336     if (needUpdate) {
337         int (*sFnppParams)(const struct compute_params *,
338                            struct mdp_overlay_pp_params *) =
339                            Overlay::getFnPpParams();
340         if(sFnppParams) {
341            int ret = sFnppParams(&mParams, &mOVInfo.overlay_pp_cfg);
342            if (ret) {
343              ALOGE("%s: Unable to set PP params", __FUNCTION__);
344            }
345         }
346     }
347 #endif
348     return true;
349 }
350 
validateAndSet(MdpCtrl * mdpCtrlArray[],const int & count,const int & fbFd)351 bool MdpCtrl::validateAndSet(MdpCtrl* mdpCtrlArray[], const int& count,
352         const int& fbFd) {
353     mdp_overlay* ovArray[count];
354     memset(&ovArray, 0, sizeof(ovArray));
355 
356     uint8_t max_horz_deci = 0, max_vert_deci = 0;
357 
358     // Decimation factor for the left and right pipe differs, when there is a
359     // one pixel difference in the dst width of right pipe and the left pipe.
360     // libscalar returns a failure as it expects decimation on both the pipe
361     // to be same. So compare the decimation factor on both the pipes and assign
362     // the maximum of it.
363     for(int i = 0; i < count; i++) {
364         mdp_overlay *ov_current = &mdpCtrlArray[i]->mOVInfo;
365         for(int j = i + 1; j < count; j++) {
366             mdp_overlay *ov_next = &mdpCtrlArray[j]->mOVInfo;
367             if(ov_current->z_order == ov_next->z_order) {
368                 max_horz_deci = utils::max(ov_current->horz_deci,
369                                            ov_next->horz_deci);
370                 max_vert_deci = utils::max(ov_current->vert_deci,
371                                            ov_next->vert_deci);
372 
373                 ov_current->horz_deci = max_horz_deci;
374                 ov_next->horz_deci = max_horz_deci;
375                 ov_current->vert_deci = max_vert_deci;
376                 ov_next->vert_deci = max_vert_deci;
377                 break;
378             }
379         }
380         ovArray[i] = ov_current;
381     }
382 
383     struct mdp_overlay_list list;
384     memset(&list, 0, sizeof(struct mdp_overlay_list));
385     list.num_overlays = count;
386     list.overlay_list = ovArray;
387 
388    int (*fnProgramScale)(struct mdp_overlay_list *) =
389         Overlay::getFnProgramScale();
390     if(fnProgramScale) {
391         fnProgramScale(&list);
392     }
393 
394     // Error value is based on file errno-base.h
395     // 0 - indicates no error.
396     int errVal = mdp_wrapper::validateAndSet(fbFd, list);
397     if(errVal) {
398         /* No dump for failure due to insufficient resource */
399         if(errVal != E2BIG) {
400             mdp_wrapper::dump("Bad ov dump: ",
401                 *list.overlay_list[list.processed_overlays]);
402         }
403         return false;
404     }
405 
406     return true;
407 }
408 
409 
410 //// MdpData ////////////
init(const int & dpy)411 bool MdpData::init(const int& dpy) {
412     int fbnum = Overlay::getFbForDpy(dpy);
413     if( fbnum < 0 ) {
414         ALOGE("%s: Invalid FB for the display: %d",__FUNCTION__, dpy);
415         return false;
416     }
417 
418     // FD init
419     if(!utils::openDev(mFd, fbnum, Res::fbPath, O_RDWR)){
420         ALOGE("Ctrl failed to init fbnum=%d", fbnum);
421         return false;
422     }
423     return true;
424 }
425 
426 } // overlay
427