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/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/libkern.h>
34 #include <dev/videomode/videomode.h>
35 #include "opt_videomode.h"
38 #define DPRINTF printf
40 #define DPRINTF while (0) printf
43 const struct videomode *
44 pick_mode_by_dotclock(int width, int height, int dotclock)
46 const struct videomode *this, *best = NULL;
49 DPRINTF("%s: looking for %d x %d at up to %d kHz\n", __func__, width,
51 for (i = 0; i < videomode_count; i++) {
52 this = &videomode_list[i];
53 if ((this->hdisplay != width) || (this->vdisplay != height) ||
54 (this->dot_clock > dotclock))
57 if (this->dot_clock > best->dot_clock)
63 DPRINTF("found %s\n", best->name);
68 const struct videomode *
69 pick_mode_by_ref(int width, int height, int refresh)
71 const struct videomode *this, *best = NULL;
72 int mref, closest = 1000, i, diff;
74 DPRINTF("%s: looking for %d x %d at up to %d Hz\n", __func__, width,
76 for (i = 0; i < videomode_count; i++) {
77 this = &videomode_list[i];
78 mref = this->dot_clock * 1000 / (this->htotal * this->vtotal);
79 diff = abs(mref - refresh);
80 if ((this->hdisplay != width) || (this->vdisplay != height))
82 DPRINTF("%s in %d hz, diff %d\n", this->name, mref, diff);
94 DPRINTF("found %s %d\n", best->name, best->dot_clock);
100 swap_modes(struct videomode *left, struct videomode *right)
102 struct videomode temp;
110 * Sort modes by refresh rate, aspect ratio (*), then resolution.
111 * Preferred mode or largest mode is first in the list and other modes
112 * are sorted on closest match to that mode.
113 * (*) Note that the aspect ratio calculation treats "close" aspect ratios
114 * (within 12.5%) as the same for this purpose.
116 #define DIVIDE(x, y) (((x) + ((y) / 2)) / (y))
118 sort_modes(struct videomode *modes, struct videomode **preferred, int nmodes)
120 int aspect, refresh, hbest, vbest, abest, atemp, rbest, rtemp;
122 struct videomode *mtemp = NULL;
127 if (*preferred != NULL) {
128 /* Put the preferred mode first in the list */
129 aspect = (*preferred)->hdisplay * 100 / (*preferred)->vdisplay;
130 refresh = DIVIDE(DIVIDE((*preferred)->dot_clock * 1000,
131 (*preferred)->htotal), (*preferred)->vtotal);
132 if (*preferred != modes) {
133 swap_modes(*preferred, modes);
138 * Find the largest horizontal and vertical mode and put that
139 * first in the list. Preferred refresh rate is taken from
140 * the first mode of this size.
144 for (i = 0; i < nmodes; i++) {
145 if (modes[i].hdisplay > hbest) {
146 hbest = modes[i].hdisplay;
147 vbest = modes[i].vdisplay;
149 } else if (modes[i].hdisplay == hbest &&
150 modes[i].vdisplay > vbest) {
151 vbest = modes[i].vdisplay;
155 aspect = mtemp->hdisplay * 100 / mtemp->vdisplay;
156 refresh = DIVIDE(DIVIDE(mtemp->dot_clock * 1000,
157 mtemp->htotal), mtemp->vtotal);
159 swap_modes(mtemp, modes);
162 /* Sort other modes by refresh rate, aspect ratio, then resolution */
163 for (j = 1; j < nmodes - 1; j++) {
168 for (i = j; i < nmodes; i++) {
169 rtemp = abs(refresh -
170 DIVIDE(DIVIDE(modes[i].dot_clock * 1000,
171 modes[i].htotal), modes[i].vtotal));
172 atemp = (modes[i].hdisplay * 100 / modes[i].vdisplay);
177 if (rtemp == rbest) {
178 /* Treat "close" aspect ratios as identical */
179 if (abs(abest - atemp) > (abest / 8) &&
180 abs(aspect - atemp) < abs(aspect - abest)) {
184 if (atemp == abest ||
185 abs(abest - atemp) <= (abest / 8)) {
186 if (modes[i].hdisplay > hbest) {
187 hbest = modes[i].hdisplay;
190 if (modes[i].hdisplay == hbest &&
191 modes[i].vdisplay > vbest) {
192 vbest = modes[i].vdisplay;
198 if (mtemp != &modes[j])
199 swap_modes(mtemp, &modes[j]);