Home | History | Annotate | Line # | Download | only in videomode
pickmode.c revision 1.2.2.1
      1  1.2.2.1    jruoho /* $NetBSD: pickmode.c,v 1.2.2.1 2011/06/06 09:08:45 jruoho Exp $ */
      2      1.1  macallan 
      3      1.1  macallan /*-
      4      1.1  macallan  * Copyright (c) 2006 The NetBSD Foundation
      5      1.1  macallan  * All rights reserved.
      6      1.1  macallan  *
      7      1.1  macallan  * this code was contributed to The NetBSD Foundation by Michael Lorenz
      8      1.1  macallan  *
      9      1.1  macallan  * Redistribution and use in source and binary forms, with or without
     10      1.1  macallan  * modification, are permitted provided that the following conditions
     11      1.1  macallan  * are met:
     12      1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     13      1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     14      1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     16      1.1  macallan  *    documentation and/or other materials provided with the distribution.
     17      1.1  macallan  *
     18      1.1  macallan  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS
     19      1.1  macallan  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20      1.1  macallan  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21      1.1  macallan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE NETBSD FOUNDATION BE LIABLE
     22      1.1  macallan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23      1.1  macallan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     24      1.1  macallan  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25      1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     26      1.1  macallan  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     27      1.1  macallan  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     28      1.1  macallan  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29      1.1  macallan  */
     30      1.1  macallan 
     31      1.1  macallan #include <sys/cdefs.h>
     32  1.2.2.1    jruoho __KERNEL_RCSID(0, "$NetBSD: pickmode.c,v 1.2.2.1 2011/06/06 09:08:45 jruoho Exp $");
     33      1.1  macallan 
     34      1.1  macallan #include <sys/param.h>
     35      1.1  macallan #include <dev/videomode/videomode.h>
     36      1.1  macallan #include "opt_videomode.h"
     37      1.1  macallan 
     38      1.1  macallan #ifdef PICKMODE_DEBUG
     39      1.1  macallan #define DPRINTF printf
     40      1.1  macallan #else
     41      1.1  macallan #define DPRINTF while (0) printf
     42      1.1  macallan #endif
     43      1.1  macallan 
     44      1.1  macallan const struct videomode *
     45      1.1  macallan pick_mode_by_dotclock(int width, int height, int dotclock)
     46      1.1  macallan {
     47      1.1  macallan 	const struct videomode *this, *best = NULL;
     48      1.1  macallan 	int i;
     49      1.1  macallan 
     50      1.1  macallan 	DPRINTF("%s: looking for %d x %d at up to %d kHz\n", __func__, width,
     51      1.1  macallan 	    height, dotclock);
     52      1.1  macallan 	for (i = 0; i < videomode_count; i++) {
     53      1.1  macallan 		this = &videomode_list[i];
     54      1.1  macallan 		if ((this->hdisplay != width) || (this->vdisplay != height) ||
     55      1.1  macallan 		    (this->dot_clock > dotclock))
     56      1.1  macallan 			continue;
     57      1.1  macallan 		if (best != NULL) {
     58      1.1  macallan 			if (this->dot_clock > best->dot_clock)
     59      1.1  macallan 				best = this;
     60      1.1  macallan 		} else
     61      1.1  macallan 			best = this;
     62      1.1  macallan 	}
     63  1.2.2.1    jruoho 	if (best != NULL)
     64      1.1  macallan 		DPRINTF("found %s\n", best->name);
     65      1.1  macallan 
     66      1.1  macallan 	return best;
     67      1.1  macallan }
     68      1.1  macallan 
     69      1.1  macallan const struct videomode *
     70      1.1  macallan pick_mode_by_ref(int width, int height, int refresh)
     71      1.1  macallan {
     72      1.1  macallan 	const struct videomode *this, *best = NULL;
     73      1.1  macallan 	int mref, closest = 1000, i, diff;
     74      1.1  macallan 
     75      1.1  macallan 	DPRINTF("%s: looking for %d x %d at up to %d Hz\n", __func__, width,
     76      1.1  macallan 	    height, refresh);
     77      1.1  macallan 	for (i = 0; i < videomode_count; i++) {
     78      1.1  macallan 
     79      1.1  macallan 		this = &videomode_list[i];
     80      1.1  macallan 		mref = this->dot_clock * 1000 / (this->htotal * this->vtotal);
     81      1.1  macallan 		diff = abs(mref - refresh);
     82      1.2  macallan 		if ((this->hdisplay != width) || (this->vdisplay != height))
     83      1.1  macallan 			continue;
     84      1.2  macallan 		DPRINTF("%s in %d hz, diff %d\n", this->name, mref, diff);
     85      1.1  macallan 		if (best != NULL) {
     86      1.1  macallan 			if (diff < closest) {
     87      1.1  macallan 				best = this;
     88      1.1  macallan 				closest = diff;
     89      1.1  macallan 			}
     90      1.2  macallan 		} else {
     91      1.1  macallan 			best = this;
     92      1.2  macallan 			closest = diff;
     93      1.2  macallan 		}
     94      1.1  macallan 	}
     95  1.2.2.1    jruoho 	if (best != NULL)
     96      1.2  macallan 		DPRINTF("found %s %d\n", best->name, best->dot_clock);
     97      1.1  macallan 
     98      1.1  macallan 	return best;
     99      1.1  macallan }
    100  1.2.2.1    jruoho 
    101  1.2.2.1    jruoho static inline void
    102  1.2.2.1    jruoho swap_modes(struct videomode *left, struct videomode *right)
    103  1.2.2.1    jruoho {
    104  1.2.2.1    jruoho 	struct videomode temp;
    105  1.2.2.1    jruoho 
    106  1.2.2.1    jruoho 	temp = *left;
    107  1.2.2.1    jruoho 	*left = *right;
    108  1.2.2.1    jruoho 	*right = temp;
    109  1.2.2.1    jruoho }
    110  1.2.2.1    jruoho 
    111  1.2.2.1    jruoho /*
    112  1.2.2.1    jruoho  * Sort modes by refresh rate, aspect ratio (*), then resolution.
    113  1.2.2.1    jruoho  * Preferred mode or largest mode is first in the list and other modes
    114  1.2.2.1    jruoho  * are sorted on closest match to that mode.
    115  1.2.2.1    jruoho  * (*) Note that the aspect ratio calculation treats "close" aspect ratios
    116  1.2.2.1    jruoho  * (within 12.5%) as the same for this purpose.
    117  1.2.2.1    jruoho  */
    118  1.2.2.1    jruoho #define	DIVIDE(x, y)	(((x) + ((y) / 2)) / (y))
    119  1.2.2.1    jruoho void
    120  1.2.2.1    jruoho sort_modes(struct videomode *modes, struct videomode **preferred, int nmodes)
    121  1.2.2.1    jruoho {
    122  1.2.2.1    jruoho 	int aspect, refresh, hbest, vbest, abest, atemp, rbest, rtemp;
    123  1.2.2.1    jruoho 	int i, j;
    124  1.2.2.1    jruoho 	struct videomode *mtemp = NULL;
    125  1.2.2.1    jruoho 
    126  1.2.2.1    jruoho 	if (nmodes < 2)
    127  1.2.2.1    jruoho 		return;
    128  1.2.2.1    jruoho 
    129  1.2.2.1    jruoho 	if (*preferred != NULL) {
    130  1.2.2.1    jruoho 		/* Put the preferred mode first in the list */
    131  1.2.2.1    jruoho 		aspect = (*preferred)->hdisplay * 100 / (*preferred)->vdisplay;
    132  1.2.2.1    jruoho 		refresh = DIVIDE(DIVIDE((*preferred)->dot_clock * 1000,
    133  1.2.2.1    jruoho 		    (*preferred)->htotal), (*preferred)->vtotal);
    134  1.2.2.1    jruoho 		if (*preferred != modes) {
    135  1.2.2.1    jruoho 			swap_modes(*preferred, modes);
    136  1.2.2.1    jruoho 			*preferred = modes;
    137  1.2.2.1    jruoho 		}
    138  1.2.2.1    jruoho 	} else {
    139  1.2.2.1    jruoho 		/*
    140  1.2.2.1    jruoho 		 * Find the largest horizontal and vertical mode and put that
    141  1.2.2.1    jruoho 		 * first in the list.  Preferred refresh rate is taken from
    142  1.2.2.1    jruoho 		 * the first mode of this size.
    143  1.2.2.1    jruoho 		 */
    144  1.2.2.1    jruoho 		hbest = 0;
    145  1.2.2.1    jruoho 		vbest = 0;
    146  1.2.2.1    jruoho 		for (i = 0; i < nmodes; i++) {
    147  1.2.2.1    jruoho 			if (modes[i].hdisplay > hbest) {
    148  1.2.2.1    jruoho 				hbest = modes[i].hdisplay;
    149  1.2.2.1    jruoho 				vbest = modes[i].vdisplay;
    150  1.2.2.1    jruoho 				mtemp = &modes[i];
    151  1.2.2.1    jruoho 			} else if (modes[i].hdisplay == hbest &&
    152  1.2.2.1    jruoho 			    modes[i].vdisplay > vbest) {
    153  1.2.2.1    jruoho 				vbest = modes[i].vdisplay;
    154  1.2.2.1    jruoho 				mtemp = &modes[i];
    155  1.2.2.1    jruoho 			}
    156  1.2.2.1    jruoho 		}
    157  1.2.2.1    jruoho 		aspect = mtemp->hdisplay * 100 / mtemp->vdisplay;
    158  1.2.2.1    jruoho 		refresh = DIVIDE(DIVIDE(mtemp->dot_clock * 1000,
    159  1.2.2.1    jruoho 		    mtemp->htotal), mtemp->vtotal);
    160  1.2.2.1    jruoho 		if (mtemp != modes)
    161  1.2.2.1    jruoho 			swap_modes(mtemp, modes);
    162  1.2.2.1    jruoho 	}
    163  1.2.2.1    jruoho 
    164  1.2.2.1    jruoho 	/* Sort other modes by refresh rate, aspect ratio, then resolution */
    165  1.2.2.1    jruoho 	for (j = 1; j < nmodes - 1; j++) {
    166  1.2.2.1    jruoho 		rbest = 1000;
    167  1.2.2.1    jruoho 		abest = 1000;
    168  1.2.2.1    jruoho 		hbest = 0;
    169  1.2.2.1    jruoho 		vbest = 0;
    170  1.2.2.1    jruoho 		for (i = j; i < nmodes; i++) {
    171  1.2.2.1    jruoho 			rtemp = abs(refresh -
    172  1.2.2.1    jruoho 			    DIVIDE(DIVIDE(modes[i].dot_clock * 1000,
    173  1.2.2.1    jruoho 			    modes[i].htotal), modes[i].vtotal));
    174  1.2.2.1    jruoho 			atemp = (modes[i].hdisplay * 100 / modes[i].vdisplay);
    175  1.2.2.1    jruoho 			if (rtemp < rbest) {
    176  1.2.2.1    jruoho 				rbest = rtemp;
    177  1.2.2.1    jruoho 				mtemp = &modes[i];
    178  1.2.2.1    jruoho 			}
    179  1.2.2.1    jruoho 			if (rtemp == rbest) {
    180  1.2.2.1    jruoho 				/* Treat "close" aspect ratios as identical */
    181  1.2.2.1    jruoho 				if (abs(abest - atemp) > (abest / 8) &&
    182  1.2.2.1    jruoho 				    abs(aspect - atemp) < abs(aspect - abest)) {
    183  1.2.2.1    jruoho 					abest = atemp;
    184  1.2.2.1    jruoho 					mtemp = &modes[i];
    185  1.2.2.1    jruoho 				}
    186  1.2.2.1    jruoho 				if (atemp == abest ||
    187  1.2.2.1    jruoho 				    abs(abest - atemp) <= (abest / 8)) {
    188  1.2.2.1    jruoho 					if (modes[i].hdisplay > hbest) {
    189  1.2.2.1    jruoho 						hbest = modes[i].hdisplay;
    190  1.2.2.1    jruoho 						mtemp = &modes[i];
    191  1.2.2.1    jruoho 					}
    192  1.2.2.1    jruoho 					if (modes[i].hdisplay == hbest &&
    193  1.2.2.1    jruoho 					    modes[i].vdisplay > vbest) {
    194  1.2.2.1    jruoho 						vbest = modes[i].vdisplay;
    195  1.2.2.1    jruoho 						mtemp = &modes[i];
    196  1.2.2.1    jruoho 					}
    197  1.2.2.1    jruoho 				}
    198  1.2.2.1    jruoho 			}
    199  1.2.2.1    jruoho 		}
    200  1.2.2.1    jruoho 		if (mtemp != &modes[j])
    201  1.2.2.1    jruoho 			swap_modes(mtemp, &modes[j]);
    202  1.2.2.1    jruoho 	}
    203  1.2.2.1    jruoho }
    204