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++) {
81 this = &videomode_list[i];
82 mref = this->dot_clock * 1000 / (this->htotal * this->vtotal);
83 diff = abs(mref - refresh);
84 if ((this->hdisplay != width) || (this->vdisplay != height))
86 DPRINTF("%s in %d hz, diff %d\n", this->name, mref, diff);
98 DPRINTF("found %s %d\n", best->name, best->dot_clock);
104 swap_modes(struct videomode *left, struct videomode *right)
106 struct videomode temp;
114 * Sort modes by refresh rate, aspect ratio (*), then resolution.
115 * Preferred mode or largest mode is first in the list and other modes
116 * are sorted on closest match to that mode.
117 * (*) Note that the aspect ratio calculation treats "close" aspect ratios
118 * (within 12.5%) as the same for this purpose.
120 #define DIVIDE(x, y) (((x) + ((y) / 2)) / (y))
122 sort_modes(struct videomode *modes, struct videomode **preferred, int nmodes)
124 int aspect, refresh, hbest, vbest, abest, atemp, rbest, rtemp;
126 struct videomode *mtemp = NULL;
131 if (*preferred != NULL) {
132 /* Put the preferred mode first in the list */
133 aspect = (*preferred)->hdisplay * 100 / (*preferred)->vdisplay;
134 refresh = DIVIDE(DIVIDE((*preferred)->dot_clock * 1000,
135 (*preferred)->htotal), (*preferred)->vtotal);
136 if (*preferred != modes) {
137 swap_modes(*preferred, modes);
142 * Find the largest horizontal and vertical mode and put that
143 * first in the list. Preferred refresh rate is taken from
144 * the first mode of this size.
148 for (i = 0; i < nmodes; i++) {
149 if (modes[i].hdisplay > hbest) {
150 hbest = modes[i].hdisplay;
151 vbest = modes[i].vdisplay;
153 } else if (modes[i].hdisplay == hbest &&
154 modes[i].vdisplay > vbest) {
155 vbest = modes[i].vdisplay;
159 aspect = mtemp->hdisplay * 100 / mtemp->vdisplay;
160 refresh = DIVIDE(DIVIDE(mtemp->dot_clock * 1000,
161 mtemp->htotal), mtemp->vtotal);
163 swap_modes(mtemp, modes);
166 /* Sort other modes by refresh rate, aspect ratio, then resolution */
167 for (j = 1; j < nmodes - 1; j++) {
172 for (i = j; i < nmodes; i++) {
173 rtemp = abs(refresh -
174 DIVIDE(DIVIDE(modes[i].dot_clock * 1000,
175 modes[i].htotal), modes[i].vtotal));
176 atemp = (modes[i].hdisplay * 100 / modes[i].vdisplay);
181 if (rtemp == rbest) {
182 /* Treat "close" aspect ratios as identical */
183 if (abs(abest - atemp) > (abest / 8) &&
184 abs(aspect - atemp) < abs(aspect - abest)) {
188 if (atemp == abest ||
189 abs(abest - atemp) <= (abest / 8)) {
190 if (modes[i].hdisplay > hbest) {
191 hbest = modes[i].hdisplay;
194 if (modes[i].hdisplay == hbest &&
195 modes[i].vdisplay > vbest) {
196 vbest = modes[i].vdisplay;
202 if (mtemp != &modes[j])
203 swap_modes(mtemp, &modes[j]);