1 /* $NetBSD: pickmode.c,v 1.3 2011/04/09 18:22:31 jdc Exp $ */
5 * Copyright (c) 2006 The NetBSD Foundation
8 * this code was contributed to The NetBSD Foundation by Michael Lorenz
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE NETBSD FOUNDATION BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/libkern.h>
37 #include <dev/videomode/videomode.h>
38 #include "opt_videomode.h"
41 #define DPRINTF printf
43 #define DPRINTF while (0) printf
46 const struct videomode *
47 pick_mode_by_dotclock(int width, int height, int dotclock)
49 const struct videomode *this, *best = NULL;
52 DPRINTF("%s: looking for %d x %d at up to %d kHz\n", __func__, width,
54 for (i = 0; i < videomode_count; i++) {
55 this = &videomode_list[i];
56 if ((this->hdisplay != width) || (this->vdisplay != height) ||
57 (this->dot_clock > dotclock))
60 if (this->dot_clock > best->dot_clock)
66 DPRINTF("found %s\n", best->name);
71 const struct videomode *
72 pick_mode_by_ref(int width, int height, int refresh)
74 const struct videomode *this, *best = NULL;
75 int mref, closest = 1000, i, diff;
77 DPRINTF("%s: looking for %d x %d at up to %d Hz\n", __func__, width,
79 for (i = 0; i < videomode_count; i++) {
80 this = &videomode_list[i];
81 mref = this->dot_clock * 1000 / (this->htotal * this->vtotal);
82 diff = abs(mref - refresh);
83 if ((this->hdisplay != width) || (this->vdisplay != height))
85 DPRINTF("%s in %d hz, diff %d\n", this->name, mref, diff);
97 DPRINTF("found %s %d\n", best->name, best->dot_clock);
103 swap_modes(struct videomode *left, struct videomode *right)
105 struct videomode temp;
113 * Sort modes by refresh rate, aspect ratio (*), then resolution.
114 * Preferred mode or largest mode is first in the list and other modes
115 * are sorted on closest match to that mode.
116 * (*) Note that the aspect ratio calculation treats "close" aspect ratios
117 * (within 12.5%) as the same for this purpose.
119 #define DIVIDE(x, y) (((x) + ((y) / 2)) / (y))
121 sort_modes(struct videomode *modes, struct videomode **preferred, int nmodes)
123 int aspect, refresh, hbest, vbest, abest, atemp, rbest, rtemp;
125 struct videomode *mtemp = NULL;
130 if (*preferred != NULL) {
131 /* Put the preferred mode first in the list */
132 aspect = (*preferred)->hdisplay * 100 / (*preferred)->vdisplay;
133 refresh = DIVIDE(DIVIDE((*preferred)->dot_clock * 1000,
134 (*preferred)->htotal), (*preferred)->vtotal);
135 if (*preferred != modes) {
136 swap_modes(*preferred, modes);
141 * Find the largest horizontal and vertical mode and put that
142 * first in the list. Preferred refresh rate is taken from
143 * the first mode of this size.
147 for (i = 0; i < nmodes; i++) {
148 if (modes[i].hdisplay > hbest) {
149 hbest = modes[i].hdisplay;
150 vbest = modes[i].vdisplay;
152 } else if (modes[i].hdisplay == hbest &&
153 modes[i].vdisplay > vbest) {
154 vbest = modes[i].vdisplay;
158 aspect = mtemp->hdisplay * 100 / mtemp->vdisplay;
159 refresh = DIVIDE(DIVIDE(mtemp->dot_clock * 1000,
160 mtemp->htotal), mtemp->vtotal);
162 swap_modes(mtemp, modes);
165 /* Sort other modes by refresh rate, aspect ratio, then resolution */
166 for (j = 1; j < nmodes - 1; j++) {
171 for (i = j; i < nmodes; i++) {
172 rtemp = abs(refresh -
173 DIVIDE(DIVIDE(modes[i].dot_clock * 1000,
174 modes[i].htotal), modes[i].vtotal));
175 atemp = (modes[i].hdisplay * 100 / modes[i].vdisplay);
180 if (rtemp == rbest) {
181 /* Treat "close" aspect ratios as identical */
182 if (abs(abest - atemp) > (abest / 8) &&
183 abs(aspect - atemp) < abs(aspect - abest)) {
187 if (atemp == abest ||
188 abs(abest - atemp) <= (abest / 8)) {
189 if (modes[i].hdisplay > hbest) {
190 hbest = modes[i].hdisplay;
193 if (modes[i].hdisplay == hbest &&
194 modes[i].vdisplay > vbest) {
195 vbest = modes[i].vdisplay;
201 if (mtemp != &modes[j])
202 swap_modes(mtemp, &modes[j]);