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 
25 #define HSIC_SETTINGS_DEBUG 0
26 
27 using namespace qdutils;
28 
isEqual(float f1,float f2)29 static inline bool isEqual(float f1, float f2) {
30         return ((int)(f1*100) == (int)(f2*100)) ? true : false;
31 }
32 
33 namespace ovutils = overlay::utils;
34 namespace overlay {
35 
init(const int & dpy)36 bool MdpCtrl::init(const int& dpy) {
37     int fbnum = Overlay::getFbForDpy(dpy);
38     if( fbnum < 0 ) {
39         ALOGE("%s: Invalid FB for the display: %d",__FUNCTION__, dpy);
40         return false;
41     }
42 
43     // FD init
44     if(!utils::openDev(mFd, fbnum,
45                 Res::fbPath, O_RDWR)){
46         ALOGE("Ctrl failed to init fbnum=%d", fbnum);
47         return false;
48     }
49     mDpy = dpy;
50     return true;
51 }
52 
reset()53 void MdpCtrl::reset() {
54     utils::memset0(mOVInfo);
55     mOVInfo.id = MSMFB_NEW_REQUEST;
56     mOrientation = utils::OVERLAY_TRANSFORM_0;
57     mDownscale = 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     setIsFg(args.isFg);
101     setPlaneAlpha(args.planeAlpha);
102     setBlending(args.blending);
103 }
104 
setCrop(const utils::Dim & d)105 void MdpCtrl::setCrop(const utils::Dim& d) {
106     setSrcRectDim(d);
107 }
108 
setColor(const uint32_t color)109 void MdpCtrl::setColor(const uint32_t color) {
110     mOVInfo.bg_color = color;
111 }
112 
setPosition(const overlay::utils::Dim & d)113 void MdpCtrl::setPosition(const overlay::utils::Dim& d) {
114     setDstRectDim(d);
115 }
116 
setTransform(const utils::eTransform & orient)117 void MdpCtrl::setTransform(const utils::eTransform& orient) {
118     int rot = utils::getMdpOrient(orient);
119     setUserData(rot);
120     mOrientation = static_cast<utils::eTransform>(rot);
121 }
122 
doTransform()123 void MdpCtrl::doTransform() {
124     setRotationFlags();
125     utils::Whf whf = getSrcWhf();
126     utils::Dim dim = getSrcRectDim();
127     utils::preRotateSource(mOrientation, whf, dim);
128     setSrcWhf(whf);
129     setSrcRectDim(dim);
130 }
131 
doDownscale()132 void MdpCtrl::doDownscale() {
133     int mdpVersion = MDPVersion::getInstance().getMDPVersion();
134     if(mdpVersion < MDSS_V5) {
135         mOVInfo.src_rect.x >>= mDownscale;
136         mOVInfo.src_rect.y >>= mDownscale;
137         mOVInfo.src_rect.w >>= mDownscale;
138         mOVInfo.src_rect.h >>= mDownscale;
139     } else if(MDPVersion::getInstance().supportsDecimation()) {
140         //Decimation + MDP Downscale
141         mOVInfo.horz_deci = 0;
142         mOVInfo.vert_deci = 0;
143         int minHorDeci = 0;
144         if(mOVInfo.src_rect.w > 2048) {
145             //If the client sends us something > what a layer mixer supports
146             //then it means it doesn't want to use split-pipe but wants us to
147             //decimate. A minimum decimation of 2 will ensure that the width is
148             //always within layer mixer limits.
149             minHorDeci = 2;
150         }
151 
152         float horDscale = 0.0f;
153         float verDscale = 0.0f;
154 
155         utils::getDecimationFactor(mOVInfo.src_rect.w, mOVInfo.src_rect.h,
156                 mOVInfo.dst_rect.w, mOVInfo.dst_rect.h, horDscale, verDscale);
157 
158         if(horDscale < minHorDeci)
159             horDscale = minHorDeci;
160 
161         if((int)horDscale)
162             mOVInfo.horz_deci = (int)log2f(horDscale);
163 
164         if((int)verDscale)
165             mOVInfo.vert_deci = (int)log2f(verDscale);
166     }
167 }
168 
set()169 bool MdpCtrl::set() {
170     int mdpVersion = MDPVersion::getInstance().getMDPVersion();
171     //deferred calcs, so APIs could be called in any order.
172     doTransform();
173     utils::Whf whf = getSrcWhf();
174     if(utils::isYuv(whf.format)) {
175         utils::normalizeCrop(mOVInfo.src_rect.x, mOVInfo.src_rect.w);
176         utils::normalizeCrop(mOVInfo.src_rect.y, mOVInfo.src_rect.h);
177         if(mdpVersion < MDSS_V5) {
178             utils::even_floor(mOVInfo.dst_rect.w);
179             utils::even_floor(mOVInfo.dst_rect.h);
180         } else if (mOVInfo.flags & MDP_DEINTERLACE) {
181             // For interlaced, crop.h should be 4-aligned
182             if (!(mOVInfo.flags & MDP_SOURCE_ROTATED_90) &&
183                 (mOVInfo.src_rect.h % 4))
184                 mOVInfo.src_rect.h = utils::aligndown(mOVInfo.src_rect.h, 4);
185             // For interlaced, width must be multiple of 4 when rotated 90deg.
186             else if ((mOVInfo.flags & MDP_SOURCE_ROTATED_90) &&
187                 (mOVInfo.src_rect.w % 4))
188                 mOVInfo.src_rect.w = utils::aligndown(mOVInfo.src_rect.w, 4);
189         }
190     } else {
191         if (mdpVersion >= MDSS_V5) {
192             // Check for 1-pixel down-scaling
193             if (mOVInfo.src_rect.w - mOVInfo.dst_rect.w == 1)
194                 mOVInfo.src_rect.w -= 1;
195             if (mOVInfo.src_rect.h - mOVInfo.dst_rect.h == 1)
196                 mOVInfo.src_rect.h -= 1;
197         }
198     }
199 
200     doDownscale();
201     return true;
202 }
203 
204 //Update src format based on rotator's destination format.
updateSrcFormat(const uint32_t & rotDestFmt)205 void MdpCtrl::updateSrcFormat(const uint32_t& rotDestFmt) {
206     utils::Whf whf = getSrcWhf();
207     whf.format =  rotDestFmt;
208     setSrcWhf(whf);
209 }
210 
dump() const211 void MdpCtrl::dump() const {
212     ALOGE("== Dump MdpCtrl start ==");
213     mFd.dump();
214     mdp_wrapper::dump("mOVInfo", mOVInfo);
215     ALOGE("== Dump MdpCtrl end ==");
216 }
217 
getDump(char * buf,size_t len)218 void MdpCtrl::getDump(char *buf, size_t len) {
219     ovutils::getDump(buf, len, "Ctrl", mOVInfo);
220 }
221 
dump() const222 void MdpData::dump() const {
223     ALOGE("== Dump MdpData start ==");
224     mFd.dump();
225     mdp_wrapper::dump("mOvData", mOvData);
226     ALOGE("== Dump MdpData end ==");
227 }
228 
getDump(char * buf,size_t len)229 void MdpData::getDump(char *buf, size_t len) {
230     ovutils::getDump(buf, len, "Data", mOvData);
231 }
232 
dump() const233 void MdpCtrl3D::dump() const {
234     ALOGE("== Dump MdpCtrl start ==");
235     mFd.dump();
236     ALOGE("== Dump MdpCtrl end ==");
237 }
238 
setVisualParams(const MetaData_t & data)239 bool MdpCtrl::setVisualParams(const MetaData_t& data) {
240     ALOGD_IF(0, "In %s: data.operation = %d", __FUNCTION__, data.operation);
241 #ifdef USES_POST_PROCESSING
242     bool needUpdate = false;
243     /* calculate the data */
244     if (data.operation & PP_PARAM_HSIC) {
245         if (mParams.params.pa_params.hue != data.hsicData.hue) {
246             ALOGD_IF(HSIC_SETTINGS_DEBUG,
247                 "Hue has changed from %d to %d",
248                 mParams.params.pa_params.hue,data.hsicData.hue);
249             needUpdate = true;
250         }
251 
252         if (!isEqual(mParams.params.pa_params.sat,
253             data.hsicData.saturation)) {
254             ALOGD_IF(HSIC_SETTINGS_DEBUG,
255                 "Saturation has changed from %f to %f",
256                 mParams.params.pa_params.sat,
257                 data.hsicData.saturation);
258             needUpdate = true;
259         }
260 
261         if (mParams.params.pa_params.intensity != data.hsicData.intensity) {
262             ALOGD_IF(HSIC_SETTINGS_DEBUG,
263                 "Intensity has changed from %d to %d",
264                 mParams.params.pa_params.intensity,
265                 data.hsicData.intensity);
266             needUpdate = true;
267         }
268 
269         if (!isEqual(mParams.params.pa_params.contrast,
270             data.hsicData.contrast)) {
271             ALOGD_IF(HSIC_SETTINGS_DEBUG,
272                 "Contrast has changed from %f to %f",
273                 mParams.params.pa_params.contrast,
274                 data.hsicData.contrast);
275             needUpdate = true;
276         }
277 
278         if (needUpdate) {
279             mParams.params.pa_params.hue = data.hsicData.hue;
280             mParams.params.pa_params.sat = data.hsicData.saturation;
281             mParams.params.pa_params.intensity = data.hsicData.intensity;
282             mParams.params.pa_params.contrast = data.hsicData.contrast;
283             mParams.params.pa_params.ops = MDP_PP_OPS_WRITE | MDP_PP_OPS_ENABLE;
284             mParams.operation |= PP_OP_PA;
285         }
286     }
287 
288     if (data.operation & PP_PARAM_SHARP2) {
289         if (mParams.params.sharp_params.strength != data.Sharp2Data.strength) {
290             needUpdate = true;
291         }
292         if (mParams.params.sharp_params.edge_thr != data.Sharp2Data.edge_thr) {
293             needUpdate = true;
294         }
295         if (mParams.params.sharp_params.smooth_thr !=
296                 data.Sharp2Data.smooth_thr) {
297             needUpdate = true;
298         }
299         if (mParams.params.sharp_params.noise_thr !=
300                 data.Sharp2Data.noise_thr) {
301             needUpdate = true;
302         }
303 
304         if (needUpdate) {
305             mParams.params.sharp_params.strength = data.Sharp2Data.strength;
306             mParams.params.sharp_params.edge_thr = data.Sharp2Data.edge_thr;
307             mParams.params.sharp_params.smooth_thr =
308                 data.Sharp2Data.smooth_thr;
309             mParams.params.sharp_params.noise_thr = data.Sharp2Data.noise_thr;
310             mParams.params.sharp_params.ops =
311                 MDP_PP_OPS_WRITE | MDP_PP_OPS_ENABLE;
312             mParams.operation |= PP_OP_SHARP;
313         }
314     }
315 
316     if (data.operation & PP_PARAM_IGC) {
317         if (mOVInfo.overlay_pp_cfg.igc_cfg.c0_c1_data == NULL){
318             uint32_t *igcData
319                 = (uint32_t *)malloc(2 * MAX_IGC_LUT_ENTRIES * sizeof(uint32_t));
320             if (!igcData) {
321                 ALOGE("IGC storage allocated failed");
322                 return false;
323             }
324             mOVInfo.overlay_pp_cfg.igc_cfg.c0_c1_data = igcData;
325             mOVInfo.overlay_pp_cfg.igc_cfg.c2_data
326                 = igcData + MAX_IGC_LUT_ENTRIES;
327         }
328 
329         memcpy(mParams.params.igc_lut_params.c0,
330             data.igcData.c0, sizeof(uint16_t) * MAX_IGC_LUT_ENTRIES);
331         memcpy(mParams.params.igc_lut_params.c1,
332             data.igcData.c1, sizeof(uint16_t) * MAX_IGC_LUT_ENTRIES);
333         memcpy(mParams.params.igc_lut_params.c2,
334             data.igcData.c2, sizeof(uint16_t) * MAX_IGC_LUT_ENTRIES);
335 
336         mParams.params.igc_lut_params.ops
337             = MDP_PP_OPS_WRITE | MDP_PP_OPS_ENABLE;
338         mParams.operation |= PP_OP_IGC;
339         needUpdate = true;
340     }
341 
342     if (data.operation & PP_PARAM_VID_INTFC) {
343         mParams.params.conv_params.interface =
344             (interface_type) data.video_interface;
345         needUpdate = true;
346     }
347 
348     if (needUpdate) {
349         display_pp_compute_params(&mParams, &mOVInfo.overlay_pp_cfg);
350     }
351 #endif
352     return true;
353 }
354 
validateAndSet(MdpCtrl * mdpCtrlArray[],const int & count,const int & fbFd)355 bool MdpCtrl::validateAndSet(MdpCtrl* mdpCtrlArray[], const int& count,
356         const int& fbFd) {
357     mdp_overlay* ovArray[count];
358     memset(&ovArray, 0, sizeof(ovArray));
359 
360     for(int i = 0; i < count; i++) {
361         ovArray[i] = &mdpCtrlArray[i]->mOVInfo;
362     }
363 
364     struct mdp_overlay_list list;
365     memset(&list, 0, sizeof(struct mdp_overlay_list));
366     list.num_overlays = count;
367     list.overlay_list = ovArray;
368 
369    int (*fnProgramScale)(struct mdp_overlay_list *) =
370         Overlay::getFnProgramScale();
371     if(fnProgramScale) {
372         fnProgramScale(&list);
373     }
374 
375     if(!mdp_wrapper::validateAndSet(fbFd, list)) {
376         /* No dump for failure due to insufficient resource */
377         if(errno != E2BIG) {
378             mdp_wrapper::dump("Bad ov dump: ",
379                 *list.overlay_list[list.processed_overlays]);
380         }
381         return false;
382     }
383 
384     return true;
385 }
386 
387 
388 //// MdpData ////////////
init(const int & dpy)389 bool MdpData::init(const int& dpy) {
390     int fbnum = Overlay::getFbForDpy(dpy);
391     if( fbnum < 0 ) {
392         ALOGE("%s: Invalid FB for the display: %d",__FUNCTION__, dpy);
393         return false;
394     }
395 
396     // FD init
397     if(!utils::openDev(mFd, fbnum, Res::fbPath, O_RDWR)){
398         ALOGE("Ctrl failed to init fbnum=%d", fbnum);
399         return false;
400     }
401     return true;
402 }
403 
404 } // overlay
405