1 /* $NetBSD: pickmode.c,v 1.3 2011/04/09 18:22:31 jdc Exp $ */
4 * Copyright (c) 2006 The NetBSD Foundation
7 * this code was contributed to The NetBSD Foundation by Michael Lorenz
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE NETBSD FOUNDATION BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/param.h>
32 #include <sys/libkern.h>
33 #include <dev/videomode/videomode.h>
34 #include "opt_videomode.h"
37 #define DPRINTF printf
39 #define DPRINTF while (0) printf
42 const struct videomode *
43 pick_mode_by_dotclock(int width, int height, int dotclock)
45 const struct videomode *this, *best = NULL;
48 DPRINTF("%s: looking for %d x %d at up to %d kHz\n", __func__, width,
50 for (i = 0; i < videomode_count; i++) {
51 this = &videomode_list[i];
52 if ((this->hdisplay != width) || (this->vdisplay != height) ||
53 (this->dot_clock > dotclock))
56 if (this->dot_clock > best->dot_clock)
62 DPRINTF("found %s\n", best->name);
67 const struct videomode *
68 pick_mode_by_ref(int width, int height, int refresh)
70 const struct videomode *this, *best = NULL;
71 int mref, closest = 1000, i, diff;
73 DPRINTF("%s: looking for %d x %d at up to %d Hz\n", __func__, width,
75 for (i = 0; i < videomode_count; i++) {
76 this = &videomode_list[i];
77 mref = this->dot_clock * 1000 / (this->htotal * this->vtotal);
78 diff = abs(mref - refresh);
79 if ((this->hdisplay != width) || (this->vdisplay != height))
81 DPRINTF("%s in %d hz, diff %d\n", this->name, mref, diff);
93 DPRINTF("found %s %d\n", best->name, best->dot_clock);
99 swap_modes(struct videomode *left, struct videomode *right)
101 struct videomode temp;
109 * Sort modes by refresh rate, aspect ratio (*), then resolution.
110 * Preferred mode or largest mode is first in the list and other modes
111 * are sorted on closest match to that mode.
112 * (*) Note that the aspect ratio calculation treats "close" aspect ratios
113 * (within 12.5%) as the same for this purpose.
115 #define DIVIDE(x, y) (((x) + ((y) / 2)) / (y))
117 sort_modes(struct videomode *modes, struct videomode **preferred, int nmodes)
119 int aspect, refresh, hbest, vbest, abest, atemp, rbest, rtemp;
121 struct videomode *mtemp = NULL;
126 if (*preferred != NULL) {
127 /* Put the preferred mode first in the list */
128 aspect = (*preferred)->hdisplay * 100 / (*preferred)->vdisplay;
129 refresh = DIVIDE(DIVIDE((*preferred)->dot_clock * 1000,
130 (*preferred)->htotal), (*preferred)->vtotal);
131 if (*preferred != modes) {
132 swap_modes(*preferred, modes);
137 * Find the largest horizontal and vertical mode and put that
138 * first in the list. Preferred refresh rate is taken from
139 * the first mode of this size.
143 for (i = 0; i < nmodes; i++) {
144 if (modes[i].hdisplay > hbest) {
145 hbest = modes[i].hdisplay;
146 vbest = modes[i].vdisplay;
148 } else if (modes[i].hdisplay == hbest &&
149 modes[i].vdisplay > vbest) {
150 vbest = modes[i].vdisplay;
154 aspect = mtemp->hdisplay * 100 / mtemp->vdisplay;
155 refresh = DIVIDE(DIVIDE(mtemp->dot_clock * 1000,
156 mtemp->htotal), mtemp->vtotal);
158 swap_modes(mtemp, modes);
161 /* Sort other modes by refresh rate, aspect ratio, then resolution */
162 for (j = 1; j < nmodes - 1; j++) {
167 for (i = j; i < nmodes; i++) {
168 rtemp = abs(refresh -
169 DIVIDE(DIVIDE(modes[i].dot_clock * 1000,
170 modes[i].htotal), modes[i].vtotal));
171 atemp = (modes[i].hdisplay * 100 / modes[i].vdisplay);
176 if (rtemp == rbest) {
177 /* Treat "close" aspect ratios as identical */
178 if (abs(abest - atemp) > (abest / 8) &&
179 abs(aspect - atemp) < abs(aspect - abest)) {
183 if (atemp == abest ||
184 abs(abest - atemp) <= (abest / 8)) {
185 if (modes[i].hdisplay > hbest) {
186 hbest = modes[i].hdisplay;
189 if (modes[i].hdisplay == hbest &&
190 modes[i].vdisplay > vbest) {
191 vbest = modes[i].vdisplay;
197 if (mtemp != &modes[j])
198 swap_modes(mtemp, &modes[j]);