xf86Crtc.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390
  1. /*
  2. * Copyright © 2006 Keith Packard
  3. * Copyright © 2008 Red Hat, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and its
  6. * documentation for any purpose is hereby granted without fee, provided that
  7. * the above copyright notice appear in all copies and that both that copyright
  8. * notice and this permission notice appear in supporting documentation, and
  9. * that the name of the copyright holders not be used in advertising or
  10. * publicity pertaining to distribution of the software without specific,
  11. * written prior permission. The copyright holders make no representations
  12. * about the suitability of this software for any purpose. It is provided "as
  13. * is" without express or implied warranty.
  14. *
  15. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  20. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  21. * OF THIS SOFTWARE.
  22. */
  23. #ifdef HAVE_XORG_CONFIG_H
  24. #include <xorg-config.h>
  25. #else
  26. #ifdef HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29. #endif
  30. #include <stddef.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include "xf86.h"
  34. #include "xf86DDC.h"
  35. #include "xf86Crtc.h"
  36. #include "xf86Modes.h"
  37. #include "xf86Priv.h"
  38. #include "xf86RandR12.h"
  39. #include "X11/extensions/render.h"
  40. #include "X11/extensions/dpmsconst.h"
  41. #include "X11/Xatom.h"
  42. #include "picturestr.h"
  43. #ifdef XV
  44. #include "xf86xv.h"
  45. #endif
  46. #define NO_OUTPUT_DEFAULT_WIDTH 1024
  47. #define NO_OUTPUT_DEFAULT_HEIGHT 768
  48. /*
  49. * Initialize xf86CrtcConfig structure
  50. */
  51. int xf86CrtcConfigPrivateIndex = -1;
  52. void
  53. xf86CrtcConfigInit(ScrnInfoPtr scrn, const xf86CrtcConfigFuncsRec * funcs)
  54. {
  55. xf86CrtcConfigPtr config;
  56. if (xf86CrtcConfigPrivateIndex == -1)
  57. xf86CrtcConfigPrivateIndex = xf86AllocateScrnInfoPrivateIndex();
  58. config = xnfcalloc(1, sizeof(xf86CrtcConfigRec));
  59. config->funcs = funcs;
  60. config->compat_output = -1;
  61. scrn->privates[xf86CrtcConfigPrivateIndex].ptr = config;
  62. }
  63. void
  64. xf86CrtcSetSizeRange(ScrnInfoPtr scrn,
  65. int minWidth, int minHeight, int maxWidth, int maxHeight)
  66. {
  67. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  68. config->minWidth = minWidth;
  69. config->minHeight = minHeight;
  70. config->maxWidth = maxWidth;
  71. config->maxHeight = maxHeight;
  72. }
  73. /*
  74. * Crtc functions
  75. */
  76. xf86CrtcPtr
  77. xf86CrtcCreate(ScrnInfoPtr scrn, const xf86CrtcFuncsRec * funcs)
  78. {
  79. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
  80. xf86CrtcPtr crtc, *crtcs;
  81. crtc = calloc(sizeof(xf86CrtcRec), 1);
  82. if (!crtc)
  83. return NULL;
  84. crtc->version = XF86_CRTC_VERSION;
  85. crtc->scrn = scrn;
  86. crtc->funcs = funcs;
  87. #ifdef RANDR_12_INTERFACE
  88. crtc->randr_crtc = NULL;
  89. #endif
  90. crtc->rotation = RR_Rotate_0;
  91. crtc->desiredRotation = RR_Rotate_0;
  92. pixman_transform_init_identity(&crtc->crtc_to_framebuffer);
  93. pixman_f_transform_init_identity(&crtc->f_crtc_to_framebuffer);
  94. pixman_f_transform_init_identity(&crtc->f_framebuffer_to_crtc);
  95. crtc->filter = NULL;
  96. crtc->params = NULL;
  97. crtc->nparams = 0;
  98. crtc->filter_width = 0;
  99. crtc->filter_height = 0;
  100. crtc->transform_in_use = FALSE;
  101. crtc->transformPresent = FALSE;
  102. crtc->desiredTransformPresent = FALSE;
  103. memset(&crtc->bounds, '\0', sizeof(crtc->bounds));
  104. /* Preallocate gamma at a sensible size. */
  105. crtc->gamma_size = 256;
  106. crtc->gamma_red = malloc(3 * crtc->gamma_size * sizeof(CARD16));
  107. if (!crtc->gamma_red) {
  108. free(crtc);
  109. return NULL;
  110. }
  111. crtc->gamma_green = crtc->gamma_red + crtc->gamma_size;
  112. crtc->gamma_blue = crtc->gamma_green + crtc->gamma_size;
  113. if (xf86_config->crtc)
  114. crtcs = realloc(xf86_config->crtc,
  115. (xf86_config->num_crtc + 1) * sizeof(xf86CrtcPtr));
  116. else
  117. crtcs = malloc((xf86_config->num_crtc + 1) * sizeof(xf86CrtcPtr));
  118. if (!crtcs) {
  119. free(crtc->gamma_red);
  120. free(crtc);
  121. return NULL;
  122. }
  123. xf86_config->crtc = crtcs;
  124. xf86_config->crtc[xf86_config->num_crtc++] = crtc;
  125. return crtc;
  126. }
  127. void
  128. xf86CrtcDestroy(xf86CrtcPtr crtc)
  129. {
  130. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
  131. int c;
  132. (*crtc->funcs->destroy) (crtc);
  133. for (c = 0; c < xf86_config->num_crtc; c++)
  134. if (xf86_config->crtc[c] == crtc) {
  135. memmove(&xf86_config->crtc[c],
  136. &xf86_config->crtc[c + 1],
  137. ((xf86_config->num_crtc - (c + 1)) * sizeof(void *)));
  138. xf86_config->num_crtc--;
  139. break;
  140. }
  141. free(crtc->params);
  142. free(crtc->gamma_red);
  143. free(crtc);
  144. }
  145. /**
  146. * Return whether any outputs are connected to the specified pipe
  147. */
  148. Bool
  149. xf86CrtcInUse(xf86CrtcPtr crtc)
  150. {
  151. ScrnInfoPtr pScrn = crtc->scrn;
  152. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
  153. int o;
  154. for (o = 0; o < xf86_config->num_output; o++)
  155. if (xf86_config->output[o]->crtc == crtc)
  156. return TRUE;
  157. return FALSE;
  158. }
  159. void
  160. xf86CrtcSetScreenSubpixelOrder(ScreenPtr pScreen)
  161. {
  162. int subpixel_order = SubPixelUnknown;
  163. Bool has_none = FALSE;
  164. ScrnInfoPtr scrn = xf86ScreenToScrn(pScreen);
  165. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
  166. int icrtc, o;
  167. for (icrtc = 0; icrtc < xf86_config->num_crtc; icrtc++) {
  168. xf86CrtcPtr crtc = xf86_config->crtc[icrtc];
  169. for (o = 0; o < xf86_config->num_output; o++) {
  170. xf86OutputPtr output = xf86_config->output[o];
  171. if (output->crtc == crtc) {
  172. switch (output->subpixel_order) {
  173. case SubPixelNone:
  174. has_none = TRUE;
  175. break;
  176. case SubPixelUnknown:
  177. break;
  178. default:
  179. subpixel_order = output->subpixel_order;
  180. break;
  181. }
  182. }
  183. if (subpixel_order != SubPixelUnknown)
  184. break;
  185. }
  186. if (subpixel_order != SubPixelUnknown) {
  187. static const int circle[4] = {
  188. SubPixelHorizontalRGB,
  189. SubPixelVerticalRGB,
  190. SubPixelHorizontalBGR,
  191. SubPixelVerticalBGR,
  192. };
  193. int rotate;
  194. int sc;
  195. for (rotate = 0; rotate < 4; rotate++)
  196. if (crtc->rotation & (1 << rotate))
  197. break;
  198. for (sc = 0; sc < 4; sc++)
  199. if (circle[sc] == subpixel_order)
  200. break;
  201. sc = (sc + rotate) & 0x3;
  202. if ((crtc->rotation & RR_Reflect_X) && !(sc & 1))
  203. sc ^= 2;
  204. if ((crtc->rotation & RR_Reflect_Y) && (sc & 1))
  205. sc ^= 2;
  206. subpixel_order = circle[sc];
  207. break;
  208. }
  209. }
  210. if (subpixel_order == SubPixelUnknown && has_none)
  211. subpixel_order = SubPixelNone;
  212. PictureSetSubpixelOrder(pScreen, subpixel_order);
  213. }
  214. /**
  215. * Sets the given video mode on the given crtc
  216. */
  217. Bool
  218. xf86CrtcSetModeTransform(xf86CrtcPtr crtc, DisplayModePtr mode,
  219. Rotation rotation, RRTransformPtr transform, int x,
  220. int y)
  221. {
  222. ScrnInfoPtr scrn = crtc->scrn;
  223. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
  224. int i;
  225. Bool ret = FALSE;
  226. Bool didLock = FALSE;
  227. DisplayModePtr adjusted_mode;
  228. DisplayModeRec saved_mode;
  229. int saved_x, saved_y;
  230. Rotation saved_rotation;
  231. RRTransformRec saved_transform;
  232. Bool saved_transform_present;
  233. crtc->enabled = xf86CrtcInUse(crtc);
  234. /* We only hit this if someone explicitly sends a "disabled" modeset. */
  235. if (!crtc->enabled) {
  236. /* Check everything for stuff that should be off. */
  237. xf86DisableUnusedFunctions(scrn);
  238. return TRUE;
  239. }
  240. adjusted_mode = xf86DuplicateMode(mode);
  241. saved_mode = crtc->mode;
  242. saved_x = crtc->x;
  243. saved_y = crtc->y;
  244. saved_rotation = crtc->rotation;
  245. if (crtc->transformPresent) {
  246. RRTransformInit(&saved_transform);
  247. RRTransformCopy(&saved_transform, &crtc->transform);
  248. }
  249. saved_transform_present = crtc->transformPresent;
  250. /* Update crtc values up front so the driver can rely on them for mode
  251. * setting.
  252. */
  253. crtc->mode = *mode;
  254. crtc->x = x;
  255. crtc->y = y;
  256. crtc->rotation = rotation;
  257. if (transform) {
  258. RRTransformCopy(&crtc->transform, transform);
  259. crtc->transformPresent = TRUE;
  260. }
  261. else
  262. crtc->transformPresent = FALSE;
  263. if (crtc->funcs->set_mode_major) {
  264. ret = crtc->funcs->set_mode_major(crtc, mode, rotation, x, y);
  265. goto done;
  266. }
  267. didLock = crtc->funcs->lock(crtc);
  268. /* Pass our mode to the outputs and the CRTC to give them a chance to
  269. * adjust it according to limitations or output properties, and also
  270. * a chance to reject the mode entirely.
  271. */
  272. for (i = 0; i < xf86_config->num_output; i++) {
  273. xf86OutputPtr output = xf86_config->output[i];
  274. if (output->crtc != crtc)
  275. continue;
  276. if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
  277. goto done;
  278. }
  279. }
  280. if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
  281. goto done;
  282. }
  283. if (!xf86CrtcRotate(crtc))
  284. goto done;
  285. /* Prepare the outputs and CRTCs before setting the mode. */
  286. for (i = 0; i < xf86_config->num_output; i++) {
  287. xf86OutputPtr output = xf86_config->output[i];
  288. if (output->crtc != crtc)
  289. continue;
  290. /* Disable the output as the first thing we do. */
  291. output->funcs->prepare(output);
  292. }
  293. crtc->funcs->prepare(crtc);
  294. /* Set up the DPLL and any output state that needs to adjust or depend
  295. * on the DPLL.
  296. */
  297. crtc->funcs->mode_set(crtc, mode, adjusted_mode, crtc->x, crtc->y);
  298. for (i = 0; i < xf86_config->num_output; i++) {
  299. xf86OutputPtr output = xf86_config->output[i];
  300. if (output->crtc == crtc)
  301. output->funcs->mode_set(output, mode, adjusted_mode);
  302. }
  303. /* Only upload when needed, to avoid unneeded delays. */
  304. if (!crtc->active && crtc->funcs->gamma_set)
  305. crtc->funcs->gamma_set(crtc, crtc->gamma_red, crtc->gamma_green,
  306. crtc->gamma_blue, crtc->gamma_size);
  307. /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
  308. crtc->funcs->commit(crtc);
  309. for (i = 0; i < xf86_config->num_output; i++) {
  310. xf86OutputPtr output = xf86_config->output[i];
  311. if (output->crtc == crtc)
  312. output->funcs->commit(output);
  313. }
  314. ret = TRUE;
  315. done:
  316. if (ret) {
  317. crtc->active = TRUE;
  318. if (scrn->pScreen)
  319. xf86CrtcSetScreenSubpixelOrder(scrn->pScreen);
  320. if (scrn->ModeSet)
  321. scrn->ModeSet(scrn);
  322. }
  323. else {
  324. crtc->x = saved_x;
  325. crtc->y = saved_y;
  326. crtc->rotation = saved_rotation;
  327. crtc->mode = saved_mode;
  328. if (saved_transform_present)
  329. RRTransformCopy(&crtc->transform, &saved_transform);
  330. crtc->transformPresent = saved_transform_present;
  331. }
  332. free((void *) adjusted_mode->name);
  333. free(adjusted_mode);
  334. if (didLock)
  335. crtc->funcs->unlock(crtc);
  336. return ret;
  337. }
  338. /**
  339. * Sets the given video mode on the given crtc, but without providing
  340. * a transform
  341. */
  342. Bool
  343. xf86CrtcSetMode(xf86CrtcPtr crtc, DisplayModePtr mode, Rotation rotation,
  344. int x, int y)
  345. {
  346. return xf86CrtcSetModeTransform(crtc, mode, rotation, NULL, x, y);
  347. }
  348. /**
  349. * Pans the screen, does not change the mode
  350. */
  351. void
  352. xf86CrtcSetOrigin(xf86CrtcPtr crtc, int x, int y)
  353. {
  354. ScrnInfoPtr scrn = crtc->scrn;
  355. crtc->x = x;
  356. crtc->y = y;
  357. if (crtc->funcs->set_origin) {
  358. if (!xf86CrtcRotate(crtc))
  359. return;
  360. crtc->funcs->set_origin(crtc, x, y);
  361. if (scrn->ModeSet)
  362. scrn->ModeSet(scrn);
  363. }
  364. else
  365. xf86CrtcSetMode(crtc, &crtc->mode, crtc->rotation, x, y);
  366. }
  367. /*
  368. * Output functions
  369. */
  370. extern XF86ConfigPtr xf86configptr;
  371. typedef enum {
  372. OPTION_PREFERRED_MODE,
  373. OPTION_ZOOM_MODES,
  374. OPTION_POSITION,
  375. OPTION_BELOW,
  376. OPTION_RIGHT_OF,
  377. OPTION_ABOVE,
  378. OPTION_LEFT_OF,
  379. OPTION_ENABLE,
  380. OPTION_DISABLE,
  381. OPTION_MIN_CLOCK,
  382. OPTION_MAX_CLOCK,
  383. OPTION_IGNORE,
  384. OPTION_ROTATE,
  385. OPTION_PANNING,
  386. OPTION_PRIMARY,
  387. OPTION_DEFAULT_MODES,
  388. } OutputOpts;
  389. static OptionInfoRec xf86OutputOptions[] = {
  390. {OPTION_PREFERRED_MODE, "PreferredMode", OPTV_STRING, {0}, FALSE},
  391. {OPTION_ZOOM_MODES, "ZoomModes", OPTV_STRING, {0}, FALSE },
  392. {OPTION_POSITION, "Position", OPTV_STRING, {0}, FALSE},
  393. {OPTION_BELOW, "Below", OPTV_STRING, {0}, FALSE},
  394. {OPTION_RIGHT_OF, "RightOf", OPTV_STRING, {0}, FALSE},
  395. {OPTION_ABOVE, "Above", OPTV_STRING, {0}, FALSE},
  396. {OPTION_LEFT_OF, "LeftOf", OPTV_STRING, {0}, FALSE},
  397. {OPTION_ENABLE, "Enable", OPTV_BOOLEAN, {0}, FALSE},
  398. {OPTION_DISABLE, "Disable", OPTV_BOOLEAN, {0}, FALSE},
  399. {OPTION_MIN_CLOCK, "MinClock", OPTV_FREQ, {0}, FALSE},
  400. {OPTION_MAX_CLOCK, "MaxClock", OPTV_FREQ, {0}, FALSE},
  401. {OPTION_IGNORE, "Ignore", OPTV_BOOLEAN, {0}, FALSE},
  402. {OPTION_ROTATE, "Rotate", OPTV_STRING, {0}, FALSE},
  403. {OPTION_PANNING, "Panning", OPTV_STRING, {0}, FALSE},
  404. {OPTION_PRIMARY, "Primary", OPTV_BOOLEAN, {0}, FALSE},
  405. {OPTION_DEFAULT_MODES, "DefaultModes", OPTV_BOOLEAN, {0}, FALSE},
  406. {-1, NULL, OPTV_NONE, {0}, FALSE},
  407. };
  408. enum {
  409. OPTION_MODEDEBUG,
  410. };
  411. static OptionInfoRec xf86DeviceOptions[] = {
  412. {OPTION_MODEDEBUG, "ModeDebug", OPTV_BOOLEAN, {0}, FALSE},
  413. {-1, NULL, OPTV_NONE, {0}, FALSE},
  414. };
  415. static void
  416. xf86OutputSetMonitor(xf86OutputPtr output)
  417. {
  418. char *option_name;
  419. const char *monitor;
  420. if (!output->name)
  421. return;
  422. free(output->options);
  423. output->options = xnfalloc(sizeof(xf86OutputOptions));
  424. memcpy(output->options, xf86OutputOptions, sizeof(xf86OutputOptions));
  425. XNFasprintf(&option_name, "monitor-%s", output->name);
  426. monitor = xf86findOptionValue(output->scrn->options, option_name);
  427. if (!monitor)
  428. monitor = output->name;
  429. else
  430. xf86MarkOptionUsedByName(output->scrn->options, option_name);
  431. free(option_name);
  432. output->conf_monitor = xf86findMonitor(monitor,
  433. xf86configptr->conf_monitor_lst);
  434. /*
  435. * Find the monitor section of the screen and use that
  436. */
  437. if (!output->conf_monitor && output->use_screen_monitor)
  438. output->conf_monitor = xf86findMonitor(output->scrn->monitor->id,
  439. xf86configptr->conf_monitor_lst);
  440. if (output->conf_monitor) {
  441. xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
  442. "Output %s using monitor section %s\n",
  443. output->name, output->conf_monitor->mon_identifier);
  444. xf86ProcessOptions(output->scrn->scrnIndex,
  445. output->conf_monitor->mon_option_lst,
  446. output->options);
  447. }
  448. else
  449. xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
  450. "Output %s has no monitor section\n", output->name);
  451. }
  452. static Bool
  453. xf86OutputEnabled(xf86OutputPtr output, Bool strict)
  454. {
  455. Bool enable, disable;
  456. /* check to see if this output was enabled in the config file */
  457. if (xf86GetOptValBool(output->options, OPTION_ENABLE, &enable) && enable) {
  458. xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
  459. "Output %s enabled by config file\n", output->name);
  460. return TRUE;
  461. }
  462. /* or if this output was disabled in the config file */
  463. if (xf86GetOptValBool(output->options, OPTION_DISABLE, &disable) && disable) {
  464. xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
  465. "Output %s disabled by config file\n", output->name);
  466. return FALSE;
  467. }
  468. /* If not, try to only light up the ones we know are connected */
  469. if (strict) {
  470. enable = output->status == XF86OutputStatusConnected;
  471. }
  472. /* But if that fails, try to light up even outputs we're unsure of */
  473. else {
  474. enable = output->status != XF86OutputStatusDisconnected;
  475. }
  476. xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
  477. "Output %s %sconnected\n", output->name, enable ? "" : "dis");
  478. return enable;
  479. }
  480. static Bool
  481. xf86OutputIgnored(xf86OutputPtr output)
  482. {
  483. return xf86ReturnOptValBool(output->options, OPTION_IGNORE, FALSE);
  484. }
  485. static const char *direction[4] = {
  486. "normal",
  487. "left",
  488. "inverted",
  489. "right"
  490. };
  491. static Rotation
  492. xf86OutputInitialRotation(xf86OutputPtr output)
  493. {
  494. const char *rotate_name = xf86GetOptValString(output->options,
  495. OPTION_ROTATE);
  496. int i;
  497. if (!rotate_name) {
  498. if (output->initial_rotation)
  499. return output->initial_rotation;
  500. return RR_Rotate_0;
  501. }
  502. for (i = 0; i < 4; i++)
  503. if (xf86nameCompare(direction[i], rotate_name) == 0)
  504. return 1 << i;
  505. return RR_Rotate_0;
  506. }
  507. xf86OutputPtr
  508. xf86OutputCreate(ScrnInfoPtr scrn,
  509. const xf86OutputFuncsRec * funcs, const char *name)
  510. {
  511. xf86OutputPtr output, *outputs;
  512. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
  513. int len;
  514. Bool primary;
  515. if (name)
  516. len = strlen(name) + 1;
  517. else
  518. len = 0;
  519. output = calloc(sizeof(xf86OutputRec) + len, 1);
  520. if (!output)
  521. return NULL;
  522. output->scrn = scrn;
  523. output->funcs = funcs;
  524. if (name) {
  525. output->name = (char *) (output + 1);
  526. strcpy(output->name, name);
  527. }
  528. output->subpixel_order = SubPixelUnknown;
  529. /*
  530. * Use the old per-screen monitor section for the first output
  531. */
  532. output->use_screen_monitor = (xf86_config->num_output == 0);
  533. #ifdef RANDR_12_INTERFACE
  534. output->randr_output = NULL;
  535. #endif
  536. if (name) {
  537. xf86OutputSetMonitor(output);
  538. if (xf86OutputIgnored(output)) {
  539. free(output);
  540. return FALSE;
  541. }
  542. }
  543. if (xf86_config->output)
  544. outputs = realloc(xf86_config->output,
  545. (xf86_config->num_output +
  546. 1) * sizeof(xf86OutputPtr));
  547. else
  548. outputs = malloc((xf86_config->num_output + 1) * sizeof(xf86OutputPtr));
  549. if (!outputs) {
  550. free(output);
  551. return NULL;
  552. }
  553. xf86_config->output = outputs;
  554. if (xf86GetOptValBool(output->options, OPTION_PRIMARY, &primary) && primary) {
  555. memmove(xf86_config->output + 1, xf86_config->output,
  556. xf86_config->num_output * sizeof(xf86OutputPtr));
  557. xf86_config->output[0] = output;
  558. }
  559. else {
  560. xf86_config->output[xf86_config->num_output] = output;
  561. }
  562. xf86_config->num_output++;
  563. return output;
  564. }
  565. Bool
  566. xf86OutputRename(xf86OutputPtr output, const char *name)
  567. {
  568. char *newname = strdup(name);
  569. if (!newname)
  570. return FALSE; /* so sorry... */
  571. if (output->name && output->name != (char *) (output + 1))
  572. free(output->name);
  573. output->name = newname;
  574. xf86OutputSetMonitor(output);
  575. if (xf86OutputIgnored(output))
  576. return FALSE;
  577. return TRUE;
  578. }
  579. void
  580. xf86OutputUseScreenMonitor(xf86OutputPtr output, Bool use_screen_monitor)
  581. {
  582. if (use_screen_monitor != output->use_screen_monitor) {
  583. output->use_screen_monitor = use_screen_monitor;
  584. xf86OutputSetMonitor(output);
  585. }
  586. }
  587. void
  588. xf86OutputDestroy(xf86OutputPtr output)
  589. {
  590. ScrnInfoPtr scrn = output->scrn;
  591. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
  592. int o;
  593. (*output->funcs->destroy) (output);
  594. while (output->probed_modes)
  595. xf86DeleteMode(&output->probed_modes, output->probed_modes);
  596. for (o = 0; o < xf86_config->num_output; o++)
  597. if (xf86_config->output[o] == output) {
  598. memmove(&xf86_config->output[o],
  599. &xf86_config->output[o + 1],
  600. ((xf86_config->num_output - (o + 1)) * sizeof(void *)));
  601. xf86_config->num_output--;
  602. break;
  603. }
  604. if (output->name && output->name != (char *) (output + 1))
  605. free(output->name);
  606. free(output);
  607. }
  608. /*
  609. * Called during CreateScreenResources to hook up RandR
  610. */
  611. static Bool
  612. xf86CrtcCreateScreenResources(ScreenPtr screen)
  613. {
  614. ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
  615. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  616. screen->CreateScreenResources = config->CreateScreenResources;
  617. if (!(*screen->CreateScreenResources) (screen))
  618. return FALSE;
  619. if (!xf86RandR12CreateScreenResources(screen))
  620. return FALSE;
  621. return TRUE;
  622. }
  623. /*
  624. * Clean up config on server reset
  625. */
  626. static Bool
  627. xf86CrtcCloseScreen(ScreenPtr screen)
  628. {
  629. ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
  630. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  631. int o, c;
  632. screen->CloseScreen = config->CloseScreen;
  633. xf86RotateCloseScreen(screen);
  634. xf86RandR12CloseScreen(screen);
  635. screen->CloseScreen(screen);
  636. for (o = 0; o < config->num_output; o++) {
  637. xf86OutputPtr output = config->output[o];
  638. output->randr_output = NULL;
  639. }
  640. for (c = 0; c < config->num_crtc; c++) {
  641. xf86CrtcPtr crtc = config->crtc[c];
  642. crtc->randr_crtc = NULL;
  643. }
  644. /* detach any providers */
  645. if (config->randr_provider) {
  646. RRProviderDestroy(config->randr_provider);
  647. config->randr_provider = NULL;
  648. }
  649. return TRUE;
  650. }
  651. /*
  652. * Called at ScreenInit time to set up
  653. */
  654. #ifdef RANDR_13_INTERFACE
  655. int
  656. #else
  657. Bool
  658. #endif
  659. xf86CrtcScreenInit(ScreenPtr screen)
  660. {
  661. ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
  662. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  663. int c;
  664. /* Rotation */
  665. xf86DrvMsg(scrn->scrnIndex, X_INFO,
  666. "RandR 1.2 enabled, ignore the following RandR disabled message.\n");
  667. xf86DisableRandR(); /* Disable old RandR extension support */
  668. xf86RandR12Init(screen);
  669. /* support all rotations if every crtc has the shadow alloc funcs */
  670. for (c = 0; c < config->num_crtc; c++) {
  671. xf86CrtcPtr crtc = config->crtc[c];
  672. if (!crtc->funcs->shadow_allocate || !crtc->funcs->shadow_create)
  673. break;
  674. }
  675. if (c == config->num_crtc) {
  676. xf86RandR12SetRotations(screen, RR_Rotate_0 | RR_Rotate_90 |
  677. RR_Rotate_180 | RR_Rotate_270 |
  678. RR_Reflect_X | RR_Reflect_Y);
  679. xf86RandR12SetTransformSupport(screen, TRUE);
  680. }
  681. else {
  682. xf86RandR12SetRotations(screen, RR_Rotate_0);
  683. xf86RandR12SetTransformSupport(screen, FALSE);
  684. }
  685. /* Wrap CreateScreenResources so we can initialize the RandR code */
  686. config->CreateScreenResources = screen->CreateScreenResources;
  687. screen->CreateScreenResources = xf86CrtcCreateScreenResources;
  688. config->CloseScreen = screen->CloseScreen;
  689. screen->CloseScreen = xf86CrtcCloseScreen;
  690. /* This might still be marked wrapped from a previous generation */
  691. config->BlockHandler = NULL;
  692. #ifdef XFreeXDGA
  693. _xf86_di_dga_init_internal(screen);
  694. #endif
  695. #ifdef RANDR_13_INTERFACE
  696. return RANDR_INTERFACE_VERSION;
  697. #else
  698. return TRUE;
  699. #endif
  700. }
  701. static DisplayModePtr
  702. xf86DefaultMode(xf86OutputPtr output, int width, int height)
  703. {
  704. DisplayModePtr target_mode = NULL;
  705. DisplayModePtr mode;
  706. int target_diff = 0;
  707. int target_preferred = 0;
  708. int mm_height;
  709. mm_height = output->mm_height;
  710. if (!mm_height)
  711. mm_height = (768 * 25.4) / DEFAULT_DPI;
  712. /*
  713. * Pick a mode closest to DEFAULT_DPI
  714. */
  715. for (mode = output->probed_modes; mode; mode = mode->next) {
  716. int dpi;
  717. int preferred = (((mode->type & M_T_PREFERRED) != 0) +
  718. ((mode->type & M_T_USERPREF) != 0));
  719. int diff;
  720. if (xf86ModeWidth(mode, output->initial_rotation) > width ||
  721. xf86ModeHeight(mode, output->initial_rotation) > height)
  722. continue;
  723. /* yes, use VDisplay here, not xf86ModeHeight */
  724. dpi = (mode->VDisplay * 254) / (mm_height * 10);
  725. diff = dpi - DEFAULT_DPI;
  726. diff = diff < 0 ? -diff : diff;
  727. if (target_mode == NULL || (preferred > target_preferred) ||
  728. (preferred == target_preferred && diff < target_diff)) {
  729. target_mode = mode;
  730. target_diff = diff;
  731. target_preferred = preferred;
  732. }
  733. }
  734. return target_mode;
  735. }
  736. static DisplayModePtr
  737. xf86ClosestMode(xf86OutputPtr output,
  738. DisplayModePtr match, Rotation match_rotation,
  739. int width, int height)
  740. {
  741. DisplayModePtr target_mode = NULL;
  742. DisplayModePtr mode;
  743. int target_diff = 0;
  744. /*
  745. * Pick a mode closest to the specified mode
  746. */
  747. for (mode = output->probed_modes; mode; mode = mode->next) {
  748. int dx, dy;
  749. int diff;
  750. if (xf86ModeWidth(mode, output->initial_rotation) > width ||
  751. xf86ModeHeight(mode, output->initial_rotation) > height)
  752. continue;
  753. /* exact matches are preferred */
  754. if (output->initial_rotation == match_rotation &&
  755. xf86ModesEqual(mode, match))
  756. return mode;
  757. dx = xf86ModeWidth(match, match_rotation) - xf86ModeWidth(mode,
  758. output->
  759. initial_rotation);
  760. dy = xf86ModeHeight(match, match_rotation) - xf86ModeHeight(mode,
  761. output->
  762. initial_rotation);
  763. diff = dx * dx + dy * dy;
  764. if (target_mode == NULL || diff < target_diff) {
  765. target_mode = mode;
  766. target_diff = diff;
  767. }
  768. }
  769. return target_mode;
  770. }
  771. static DisplayModePtr
  772. xf86OutputHasPreferredMode(xf86OutputPtr output, int width, int height)
  773. {
  774. DisplayModePtr mode;
  775. for (mode = output->probed_modes; mode; mode = mode->next) {
  776. if (xf86ModeWidth(mode, output->initial_rotation) > width ||
  777. xf86ModeHeight(mode, output->initial_rotation) > height)
  778. continue;
  779. if (mode->type & M_T_PREFERRED)
  780. return mode;
  781. }
  782. return NULL;
  783. }
  784. static DisplayModePtr
  785. xf86OutputHasUserPreferredMode(xf86OutputPtr output)
  786. {
  787. DisplayModePtr mode, first = output->probed_modes;
  788. for (mode = first; mode && mode->next != first; mode = mode->next)
  789. if (mode->type & M_T_USERPREF)
  790. return mode;
  791. return NULL;
  792. }
  793. static int
  794. xf86PickCrtcs(ScrnInfoPtr scrn,
  795. xf86CrtcPtr * best_crtcs,
  796. DisplayModePtr * modes, int n, int width, int height)
  797. {
  798. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  799. int c, o;
  800. xf86OutputPtr output;
  801. xf86CrtcPtr crtc;
  802. xf86CrtcPtr *crtcs;
  803. int best_score;
  804. int score;
  805. int my_score;
  806. if (n == config->num_output)
  807. return 0;
  808. output = config->output[n];
  809. /*
  810. * Compute score with this output disabled
  811. */
  812. best_crtcs[n] = NULL;
  813. best_score = xf86PickCrtcs(scrn, best_crtcs, modes, n + 1, width, height);
  814. if (modes[n] == NULL)
  815. return best_score;
  816. crtcs = malloc(config->num_output * sizeof(xf86CrtcPtr));
  817. if (!crtcs)
  818. return best_score;
  819. my_score = 1;
  820. /* Score outputs that are known to be connected higher */
  821. if (output->status == XF86OutputStatusConnected)
  822. my_score++;
  823. /* Score outputs with preferred modes higher */
  824. if (xf86OutputHasPreferredMode(output, width, height))
  825. my_score++;
  826. /*
  827. * Select a crtc for this output and
  828. * then attempt to configure the remaining
  829. * outputs
  830. */
  831. for (c = 0; c < config->num_crtc; c++) {
  832. if ((output->possible_crtcs & (1 << c)) == 0)
  833. continue;
  834. crtc = config->crtc[c];
  835. /*
  836. * Check to see if some other output is
  837. * using this crtc
  838. */
  839. for (o = 0; o < n; o++)
  840. if (best_crtcs[o] == crtc)
  841. break;
  842. if (o < n) {
  843. /*
  844. * If the two outputs desire the same mode,
  845. * see if they can be cloned
  846. */
  847. if (xf86ModesEqual(modes[o], modes[n]) &&
  848. config->output[o]->initial_rotation ==
  849. config->output[n]->initial_rotation &&
  850. config->output[o]->initial_x == config->output[n]->initial_x &&
  851. config->output[o]->initial_y == config->output[n]->initial_y) {
  852. if ((output->possible_clones & (1 << o)) == 0)
  853. continue; /* nope, try next CRTC */
  854. }
  855. else
  856. continue; /* different modes, can't clone */
  857. }
  858. crtcs[n] = crtc;
  859. memcpy(crtcs, best_crtcs, n * sizeof(xf86CrtcPtr));
  860. score =
  861. my_score + xf86PickCrtcs(scrn, crtcs, modes, n + 1, width, height);
  862. if (score > best_score) {
  863. best_score = score;
  864. memcpy(best_crtcs, crtcs, config->num_output * sizeof(xf86CrtcPtr));
  865. }
  866. }
  867. free(crtcs);
  868. return best_score;
  869. }
  870. /*
  871. * Compute the virtual size necessary to place all of the available
  872. * crtcs in the specified configuration.
  873. *
  874. * canGrow indicates that the driver can make the screen larger than its initial
  875. * configuration. If FALSE, this function will enlarge the screen to include
  876. * the largest available mode.
  877. */
  878. static void
  879. xf86DefaultScreenLimits(ScrnInfoPtr scrn, int *widthp, int *heightp,
  880. Bool canGrow)
  881. {
  882. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  883. int width = 0, height = 0;
  884. int o;
  885. int c;
  886. int s;
  887. for (c = 0; c < config->num_crtc; c++) {
  888. int crtc_width = 0, crtc_height = 0;
  889. xf86CrtcPtr crtc = config->crtc[c];
  890. if (crtc->enabled) {
  891. crtc_width =
  892. crtc->desiredX + xf86ModeWidth(&crtc->desiredMode,
  893. crtc->desiredRotation);
  894. crtc_height =
  895. crtc->desiredY + xf86ModeHeight(&crtc->desiredMode,
  896. crtc->desiredRotation);
  897. }
  898. if (!canGrow) {
  899. for (o = 0; o < config->num_output; o++) {
  900. xf86OutputPtr output = config->output[o];
  901. for (s = 0; s < config->num_crtc; s++)
  902. if (output->possible_crtcs & (1 << s)) {
  903. DisplayModePtr mode;
  904. for (mode = output->probed_modes; mode;
  905. mode = mode->next) {
  906. if (mode->HDisplay > crtc_width)
  907. crtc_width = mode->HDisplay;
  908. if (mode->VDisplay > crtc_width)
  909. crtc_width = mode->VDisplay;
  910. if (mode->VDisplay > crtc_height)
  911. crtc_height = mode->VDisplay;
  912. if (mode->HDisplay > crtc_height)
  913. crtc_height = mode->HDisplay;
  914. }
  915. }
  916. }
  917. }
  918. if (crtc_width > width)
  919. width = crtc_width;
  920. if (crtc_height > height)
  921. height = crtc_height;
  922. }
  923. if (config->maxWidth && width > config->maxWidth)
  924. width = config->maxWidth;
  925. if (config->maxHeight && height > config->maxHeight)
  926. height = config->maxHeight;
  927. if (config->minWidth && width < config->minWidth)
  928. width = config->minWidth;
  929. if (config->minHeight && height < config->minHeight)
  930. height = config->minHeight;
  931. *widthp = width;
  932. *heightp = height;
  933. }
  934. #define POSITION_UNSET -100000
  935. /*
  936. * check if the user configured any outputs at all
  937. * with either a position or a relative setting or a mode.
  938. */
  939. static Bool
  940. xf86UserConfiguredOutputs(ScrnInfoPtr scrn, DisplayModePtr * modes)
  941. {
  942. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  943. int o;
  944. Bool user_conf = FALSE;
  945. for (o = 0; o < config->num_output; o++) {
  946. xf86OutputPtr output = config->output[o];
  947. const char *position;
  948. const char *relative_name;
  949. OutputOpts relation;
  950. int r;
  951. static const OutputOpts relations[] = {
  952. OPTION_BELOW, OPTION_RIGHT_OF, OPTION_ABOVE, OPTION_LEFT_OF
  953. };
  954. position = xf86GetOptValString(output->options, OPTION_POSITION);
  955. if (position)
  956. user_conf = TRUE;
  957. relation = 0;
  958. relative_name = NULL;
  959. for (r = 0; r < 4; r++) {
  960. relation = relations[r];
  961. relative_name = xf86GetOptValString(output->options, relation);
  962. if (relative_name)
  963. break;
  964. }
  965. if (relative_name)
  966. user_conf = TRUE;
  967. modes[o] = xf86OutputHasUserPreferredMode(output);
  968. if (modes[o])
  969. user_conf = TRUE;
  970. }
  971. return user_conf;
  972. }
  973. static Bool
  974. xf86InitialOutputPositions(ScrnInfoPtr scrn, DisplayModePtr * modes)
  975. {
  976. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  977. int o;
  978. int min_x, min_y;
  979. for (o = 0; o < config->num_output; o++) {
  980. xf86OutputPtr output = config->output[o];
  981. output->initial_x = output->initial_y = POSITION_UNSET;
  982. }
  983. /*
  984. * Loop until all outputs are set
  985. */
  986. for (;;) {
  987. Bool any_set = FALSE;
  988. Bool keep_going = FALSE;
  989. for (o = 0; o < config->num_output; o++) {
  990. static const OutputOpts relations[] = {
  991. OPTION_BELOW, OPTION_RIGHT_OF, OPTION_ABOVE, OPTION_LEFT_OF
  992. };
  993. xf86OutputPtr output = config->output[o];
  994. xf86OutputPtr relative;
  995. const char *relative_name;
  996. const char *position;
  997. OutputOpts relation;
  998. int r;
  999. if (output->initial_x != POSITION_UNSET)
  1000. continue;
  1001. position = xf86GetOptValString(output->options, OPTION_POSITION);
  1002. /*
  1003. * Absolute position wins
  1004. */
  1005. if (position) {
  1006. int x, y;
  1007. if (sscanf(position, "%d %d", &x, &y) == 2) {
  1008. output->initial_x = x;
  1009. output->initial_y = y;
  1010. }
  1011. else {
  1012. xf86DrvMsg(scrn->scrnIndex, X_ERROR,
  1013. "Output %s position not of form \"x y\"\n",
  1014. output->name);
  1015. output->initial_x = output->initial_y = 0;
  1016. }
  1017. any_set = TRUE;
  1018. continue;
  1019. }
  1020. /*
  1021. * Next comes relative positions
  1022. */
  1023. relation = 0;
  1024. relative_name = NULL;
  1025. for (r = 0; r < 4; r++) {
  1026. relation = relations[r];
  1027. relative_name = xf86GetOptValString(output->options, relation);
  1028. if (relative_name)
  1029. break;
  1030. }
  1031. if (relative_name) {
  1032. int or;
  1033. relative = NULL;
  1034. for (or = 0; or < config->num_output; or++) {
  1035. xf86OutputPtr out_rel = config->output[or];
  1036. XF86ConfMonitorPtr rel_mon = out_rel->conf_monitor;
  1037. if (rel_mon) {
  1038. if (xf86nameCompare(rel_mon->mon_identifier,
  1039. relative_name) == 0) {
  1040. relative = config->output[or];
  1041. break;
  1042. }
  1043. }
  1044. if (strcmp(out_rel->name, relative_name) == 0) {
  1045. relative = config->output[or];
  1046. break;
  1047. }
  1048. }
  1049. if (!relative) {
  1050. xf86DrvMsg(scrn->scrnIndex, X_ERROR,
  1051. "Cannot position output %s relative to unknown output %s\n",
  1052. output->name, relative_name);
  1053. output->initial_x = 0;
  1054. output->initial_y = 0;
  1055. any_set = TRUE;
  1056. continue;
  1057. }
  1058. if (!modes[or]) {
  1059. xf86DrvMsg(scrn->scrnIndex, X_ERROR,
  1060. "Cannot position output %s relative to output %s without modes\n",
  1061. output->name, relative_name);
  1062. output->initial_x = 0;
  1063. output->initial_y = 0;
  1064. any_set = TRUE;
  1065. continue;
  1066. }
  1067. if (relative->initial_x == POSITION_UNSET) {
  1068. keep_going = TRUE;
  1069. continue;
  1070. }
  1071. output->initial_x = relative->initial_x;
  1072. output->initial_y = relative->initial_y;
  1073. switch (relation) {
  1074. case OPTION_BELOW:
  1075. output->initial_y +=
  1076. xf86ModeHeight(modes[or], relative->initial_rotation);
  1077. break;
  1078. case OPTION_RIGHT_OF:
  1079. output->initial_x +=
  1080. xf86ModeWidth(modes[or], relative->initial_rotation);
  1081. break;
  1082. case OPTION_ABOVE:
  1083. if (modes[o])
  1084. output->initial_y -=
  1085. xf86ModeHeight(modes[o], output->initial_rotation);
  1086. break;
  1087. case OPTION_LEFT_OF:
  1088. if (modes[o])
  1089. output->initial_x -=
  1090. xf86ModeWidth(modes[o], output->initial_rotation);
  1091. break;
  1092. default:
  1093. break;
  1094. }
  1095. any_set = TRUE;
  1096. continue;
  1097. }
  1098. /* Nothing set, just stick them at 0,0 */
  1099. output->initial_x = 0;
  1100. output->initial_y = 0;
  1101. any_set = TRUE;
  1102. }
  1103. if (!keep_going)
  1104. break;
  1105. if (!any_set) {
  1106. for (o = 0; o < config->num_output; o++) {
  1107. xf86OutputPtr output = config->output[o];
  1108. if (output->initial_x == POSITION_UNSET) {
  1109. xf86DrvMsg(scrn->scrnIndex, X_ERROR,
  1110. "Output position loop. Moving %s to 0,0\n",
  1111. output->name);
  1112. output->initial_x = output->initial_y = 0;
  1113. break;
  1114. }
  1115. }
  1116. }
  1117. }
  1118. /*
  1119. * normalize positions
  1120. */
  1121. min_x = 1000000;
  1122. min_y = 1000000;
  1123. for (o = 0; o < config->num_output; o++) {
  1124. xf86OutputPtr output = config->output[o];
  1125. if (output->initial_x < min_x)
  1126. min_x = output->initial_x;
  1127. if (output->initial_y < min_y)
  1128. min_y = output->initial_y;
  1129. }
  1130. for (o = 0; o < config->num_output; o++) {
  1131. xf86OutputPtr output = config->output[o];
  1132. output->initial_x -= min_x;
  1133. output->initial_y -= min_y;
  1134. }
  1135. return TRUE;
  1136. }
  1137. static void
  1138. xf86InitialPanning(ScrnInfoPtr scrn)
  1139. {
  1140. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  1141. int o;
  1142. for (o = 0; o < config->num_output; o++) {
  1143. xf86OutputPtr output = config->output[o];
  1144. const char *panning = xf86GetOptValString(output->options, OPTION_PANNING);
  1145. int width, height, left, top;
  1146. int track_width, track_height, track_left, track_top;
  1147. int brdr[4];
  1148. memset(&output->initialTotalArea, 0, sizeof(BoxRec));
  1149. memset(&output->initialTrackingArea, 0, sizeof(BoxRec));
  1150. memset(output->initialBorder, 0, 4 * sizeof(INT16));
  1151. if (!panning)
  1152. continue;
  1153. switch (sscanf(panning, "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d",
  1154. &width, &height, &left, &top,
  1155. &track_width, &track_height, &track_left, &track_top,
  1156. &brdr[0], &brdr[1], &brdr[2], &brdr[3])) {
  1157. case 12:
  1158. output->initialBorder[0] = brdr[0];
  1159. output->initialBorder[1] = brdr[1];
  1160. output->initialBorder[2] = brdr[2];
  1161. output->initialBorder[3] = brdr[3];
  1162. /* fall through */
  1163. case 8:
  1164. output->initialTrackingArea.x1 = track_left;
  1165. output->initialTrackingArea.y1 = track_top;
  1166. output->initialTrackingArea.x2 = track_left + track_width;
  1167. output->initialTrackingArea.y2 = track_top + track_height;
  1168. /* fall through */
  1169. case 4:
  1170. output->initialTotalArea.x1 = left;
  1171. output->initialTotalArea.y1 = top;
  1172. /* fall through */
  1173. case 2:
  1174. output->initialTotalArea.x2 = output->initialTotalArea.x1 + width;
  1175. output->initialTotalArea.y2 = output->initialTotalArea.y1 + height;
  1176. break;
  1177. default:
  1178. xf86DrvMsg(scrn->scrnIndex, X_ERROR,
  1179. "Broken panning specification '%s' for output %s in config file\n",
  1180. panning, output->name);
  1181. }
  1182. }
  1183. }
  1184. /** Return - 0 + if a should be earlier, same or later than b in list
  1185. */
  1186. static int
  1187. xf86ModeCompare(DisplayModePtr a, DisplayModePtr b)
  1188. {
  1189. int diff;
  1190. diff = ((b->type & M_T_PREFERRED) != 0) - ((a->type & M_T_PREFERRED) != 0);
  1191. if (diff)
  1192. return diff;
  1193. diff = b->HDisplay * b->VDisplay - a->HDisplay * a->VDisplay;
  1194. if (diff)
  1195. return diff;
  1196. diff = b->Clock - a->Clock;
  1197. return diff;
  1198. }
  1199. /**
  1200. * Insertion sort input in-place and return the resulting head
  1201. */
  1202. static DisplayModePtr
  1203. xf86SortModes(DisplayModePtr input)
  1204. {
  1205. DisplayModePtr output = NULL, i, o, n, *op, prev;
  1206. /* sort by preferred status and pixel area */
  1207. while (input) {
  1208. i = input;
  1209. input = input->next;
  1210. for (op = &output; (o = *op); op = &o->next)
  1211. if (xf86ModeCompare(o, i) > 0)
  1212. break;
  1213. i->next = *op;
  1214. *op = i;
  1215. }
  1216. /* prune identical modes */
  1217. for (o = output; o && (n = o->next); o = n) {
  1218. if (!strcmp(o->name, n->name) && xf86ModesEqual(o, n)) {
  1219. o->next = n->next;
  1220. free((void *) n->name);
  1221. free(n);
  1222. n = o;
  1223. }
  1224. }
  1225. /* hook up backward links */
  1226. prev = NULL;
  1227. for (o = output; o; o = o->next) {
  1228. o->prev = prev;
  1229. prev = o;
  1230. }
  1231. return output;
  1232. }
  1233. static const char *
  1234. preferredMode(ScrnInfoPtr pScrn, xf86OutputPtr output)
  1235. {
  1236. const char *preferred_mode = NULL;
  1237. /* Check for a configured preference for a particular mode */
  1238. preferred_mode = xf86GetOptValString(output->options,
  1239. OPTION_PREFERRED_MODE);
  1240. if (preferred_mode)
  1241. return preferred_mode;
  1242. if (pScrn->display->modes && *pScrn->display->modes)
  1243. preferred_mode = *pScrn->display->modes;
  1244. return preferred_mode;
  1245. }
  1246. /** identify a token
  1247. * args
  1248. * *src a string with zero or more tokens, e.g. "tok0 tok1",
  1249. * **token stores a pointer to the first token character,
  1250. * *len stores the token length.
  1251. * return
  1252. * a pointer into src[] at the token terminating character, or
  1253. * NULL if no token is found.
  1254. */
  1255. static const char *
  1256. gettoken(const char *src, const char **token, int *len)
  1257. {
  1258. const char *delim = " \t";
  1259. int skip;
  1260. if (!src)
  1261. return NULL;
  1262. skip = strspn(src, delim);
  1263. *token = &src[skip];
  1264. *len = strcspn(*token, delim);
  1265. /* Support for backslash escaped delimiters could be implemented
  1266. * here.
  1267. */
  1268. /* (*token)[0] != '\0' <==> *len > 0 */
  1269. if (*len > 0)
  1270. return &(*token)[*len];
  1271. else
  1272. return NULL;
  1273. }
  1274. /** Check for a user configured zoom mode list, Option "ZoomModes":
  1275. *
  1276. * Section "Monitor"
  1277. * Identifier "a21inch"
  1278. * Option "ZoomModes" "1600x1200 1280x1024 1280x1024 640x480"
  1279. * EndSection
  1280. *
  1281. * Each user mode name is searched for independently so the list
  1282. * specification order is free. An output mode is matched at most
  1283. * once, a mode with an already set M_T_USERDEF type bit is skipped.
  1284. * Thus a repeat mode name specification matches the next output mode
  1285. * with the same name.
  1286. *
  1287. * Ctrl+Alt+Keypad-{Plus,Minus} zooms {in,out} by selecting the
  1288. * {next,previous} M_T_USERDEF mode in the screen modes list, itself
  1289. * sorted toward lower dot area or lower dot clock frequency, see
  1290. * modes/xf86Crtc.c: xf86SortModes() xf86SetScrnInfoModes(), and
  1291. * common/xf86Cursor.c: xf86ZoomViewport().
  1292. */
  1293. static int
  1294. processZoomModes(xf86OutputPtr output)
  1295. {
  1296. const char *zoom_modes;
  1297. int count = 0;
  1298. zoom_modes = xf86GetOptValString(output->options, OPTION_ZOOM_MODES);
  1299. if (zoom_modes) {
  1300. const char *token, *next;
  1301. int len;
  1302. next = gettoken(zoom_modes, &token, &len);
  1303. while (next) {
  1304. DisplayModePtr mode;
  1305. for (mode = output->probed_modes; mode; mode = mode->next)
  1306. if (!strncmp(token, mode->name, len) /* prefix match */
  1307. && mode->name[len] == '\0' /* equal length */
  1308. && !(mode->type & M_T_USERDEF)) { /* no rematch */
  1309. mode->type |= M_T_USERDEF;
  1310. break;
  1311. }
  1312. count++;
  1313. next = gettoken(next, &token, &len);
  1314. }
  1315. }
  1316. return count;
  1317. }
  1318. static void
  1319. GuessRangeFromModes(MonPtr mon, DisplayModePtr mode)
  1320. {
  1321. if (!mon || !mode)
  1322. return;
  1323. mon->nHsync = 1;
  1324. mon->hsync[0].lo = 1024.0;
  1325. mon->hsync[0].hi = 0.0;
  1326. mon->nVrefresh = 1;
  1327. mon->vrefresh[0].lo = 1024.0;
  1328. mon->vrefresh[0].hi = 0.0;
  1329. while (mode) {
  1330. if (!mode->HSync)
  1331. mode->HSync = ((float) mode->Clock) / ((float) mode->HTotal);
  1332. if (!mode->VRefresh)
  1333. mode->VRefresh = (1000.0 * ((float) mode->Clock)) /
  1334. ((float) (mode->HTotal * mode->VTotal));
  1335. if (mode->HSync < mon->hsync[0].lo)
  1336. mon->hsync[0].lo = mode->HSync;
  1337. if (mode->HSync > mon->hsync[0].hi)
  1338. mon->hsync[0].hi = mode->HSync;
  1339. if (mode->VRefresh < mon->vrefresh[0].lo)
  1340. mon->vrefresh[0].lo = mode->VRefresh;
  1341. if (mode->VRefresh > mon->vrefresh[0].hi)
  1342. mon->vrefresh[0].hi = mode->VRefresh;
  1343. mode = mode->next;
  1344. }
  1345. /* stretch out the bottom to fit 640x480@60 */
  1346. if (mon->hsync[0].lo > 31.0)
  1347. mon->hsync[0].lo = 31.0;
  1348. if (mon->vrefresh[0].lo > 58.0)
  1349. mon->vrefresh[0].lo = 58.0;
  1350. }
  1351. enum det_monrec_source {
  1352. sync_config, sync_edid, sync_default
  1353. };
  1354. struct det_monrec_parameter {
  1355. MonRec *mon_rec;
  1356. int *max_clock;
  1357. Bool set_hsync;
  1358. Bool set_vrefresh;
  1359. enum det_monrec_source *sync_source;
  1360. };
  1361. static void
  1362. handle_detailed_monrec(struct detailed_monitor_section *det_mon, void *data)
  1363. {
  1364. struct det_monrec_parameter *p;
  1365. p = (struct det_monrec_parameter *) data;
  1366. if (det_mon->type == DS_RANGES) {
  1367. struct monitor_ranges *ranges = &det_mon->section.ranges;
  1368. if (p->set_hsync && ranges->max_h) {
  1369. p->mon_rec->hsync[p->mon_rec->nHsync].lo = ranges->min_h;
  1370. p->mon_rec->hsync[p->mon_rec->nHsync].hi = ranges->max_h;
  1371. p->mon_rec->nHsync++;
  1372. if (*p->sync_source == sync_default)
  1373. *p->sync_source = sync_edid;
  1374. }
  1375. if (p->set_vrefresh && ranges->max_v) {
  1376. p->mon_rec->vrefresh[p->mon_rec->nVrefresh].lo = ranges->min_v;
  1377. p->mon_rec->vrefresh[p->mon_rec->nVrefresh].hi = ranges->max_v;
  1378. p->mon_rec->nVrefresh++;
  1379. if (*p->sync_source == sync_default)
  1380. *p->sync_source = sync_edid;
  1381. }
  1382. if (ranges->max_clock * 1000 > *p->max_clock)
  1383. *p->max_clock = ranges->max_clock * 1000;
  1384. }
  1385. }
  1386. void
  1387. xf86ProbeOutputModes(ScrnInfoPtr scrn, int maxX, int maxY)
  1388. {
  1389. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  1390. int o;
  1391. /* When canGrow was TRUE in the initial configuration we have to
  1392. * compare against the maximum values so that we don't drop modes.
  1393. * When canGrow was FALSE, the maximum values would have been clamped
  1394. * anyway.
  1395. */
  1396. if (maxX == 0 || maxY == 0) {
  1397. maxX = config->maxWidth;
  1398. maxY = config->maxHeight;
  1399. }
  1400. /* Probe the list of modes for each output. */
  1401. for (o = 0; o < config->num_output; o++) {
  1402. xf86OutputPtr output = config->output[o];
  1403. DisplayModePtr mode;
  1404. DisplayModePtr config_modes = NULL, output_modes, default_modes = NULL;
  1405. const char *preferred_mode;
  1406. xf86MonPtr edid_monitor;
  1407. XF86ConfMonitorPtr conf_monitor;
  1408. MonRec mon_rec;
  1409. int min_clock = 0;
  1410. int max_clock = 0;
  1411. double clock;
  1412. Bool add_default_modes;
  1413. Bool debug_modes = config->debug_modes || xf86Initialising;
  1414. enum det_monrec_source sync_source = sync_default;
  1415. while (output->probed_modes != NULL)
  1416. xf86DeleteMode(&output->probed_modes, output->probed_modes);
  1417. /*
  1418. * Check connection status
  1419. */
  1420. output->status = (*output->funcs->detect) (output);
  1421. if (output->status == XF86OutputStatusDisconnected &&
  1422. !xf86ReturnOptValBool(output->options, OPTION_ENABLE, FALSE)) {
  1423. xf86OutputSetEDID(output, NULL);
  1424. continue;
  1425. }
  1426. memset(&mon_rec, '\0', sizeof(mon_rec));
  1427. conf_monitor = output->conf_monitor;
  1428. if (conf_monitor) {
  1429. int i;
  1430. for (i = 0; i < conf_monitor->mon_n_hsync; i++) {
  1431. mon_rec.hsync[mon_rec.nHsync].lo =
  1432. conf_monitor->mon_hsync[i].lo;
  1433. mon_rec.hsync[mon_rec.nHsync].hi =
  1434. conf_monitor->mon_hsync[i].hi;
  1435. mon_rec.nHsync++;
  1436. sync_source = sync_config;
  1437. }
  1438. for (i = 0; i < conf_monitor->mon_n_vrefresh; i++) {
  1439. mon_rec.vrefresh[mon_rec.nVrefresh].lo =
  1440. conf_monitor->mon_vrefresh[i].lo;
  1441. mon_rec.vrefresh[mon_rec.nVrefresh].hi =
  1442. conf_monitor->mon_vrefresh[i].hi;
  1443. mon_rec.nVrefresh++;
  1444. sync_source = sync_config;
  1445. }
  1446. config_modes = xf86GetMonitorModes(scrn, conf_monitor);
  1447. }
  1448. output_modes = (*output->funcs->get_modes) (output);
  1449. /*
  1450. * If the user has a preference, respect it.
  1451. * Otherwise, don't second-guess the driver.
  1452. */
  1453. if (!xf86GetOptValBool(output->options, OPTION_DEFAULT_MODES,
  1454. &add_default_modes))
  1455. add_default_modes = (output_modes == NULL);
  1456. edid_monitor = output->MonInfo;
  1457. if (edid_monitor) {
  1458. struct det_monrec_parameter p;
  1459. struct disp_features *features = &edid_monitor->features;
  1460. struct cea_data_block *hdmi_db;
  1461. /* if display is not continuous-frequency, don't add default modes */
  1462. if (!GTF_SUPPORTED(features->msc))
  1463. add_default_modes = FALSE;
  1464. p.mon_rec = &mon_rec;
  1465. p.max_clock = &max_clock;
  1466. p.set_hsync = mon_rec.nHsync == 0;
  1467. p.set_vrefresh = mon_rec.nVrefresh == 0;
  1468. p.sync_source = &sync_source;
  1469. xf86ForEachDetailedBlock(edid_monitor, handle_detailed_monrec, &p);
  1470. /* Look at the CEA HDMI vendor block for the max TMDS freq */
  1471. hdmi_db = xf86MonitorFindHDMIBlock(edid_monitor);
  1472. if (hdmi_db && hdmi_db->len >= 7) {
  1473. int tmds_freq = hdmi_db->u.vendor.hdmi.max_tmds_clock * 5000;
  1474. xf86DrvMsg(scrn->scrnIndex, X_PROBED,
  1475. "HDMI max TMDS frequency %dKHz\n", tmds_freq);
  1476. if (tmds_freq > max_clock)
  1477. max_clock = tmds_freq;
  1478. }
  1479. }
  1480. if (xf86GetOptValFreq(output->options, OPTION_MIN_CLOCK,
  1481. OPTUNITS_KHZ, &clock))
  1482. min_clock = (int) clock;
  1483. if (xf86GetOptValFreq(output->options, OPTION_MAX_CLOCK,
  1484. OPTUNITS_KHZ, &clock))
  1485. max_clock = (int) clock;
  1486. /* If we still don't have a sync range, guess wildly */
  1487. if (!mon_rec.nHsync || !mon_rec.nVrefresh)
  1488. GuessRangeFromModes(&mon_rec, output_modes);
  1489. /*
  1490. * These limits will end up setting a 1024x768@60Hz mode by default,
  1491. * which seems like a fairly good mode to use when nothing else is
  1492. * specified
  1493. */
  1494. if (mon_rec.nHsync == 0) {
  1495. mon_rec.hsync[0].lo = 31.0;
  1496. mon_rec.hsync[0].hi = 55.0;
  1497. mon_rec.nHsync = 1;
  1498. }
  1499. if (mon_rec.nVrefresh == 0) {
  1500. mon_rec.vrefresh[0].lo = 58.0;
  1501. mon_rec.vrefresh[0].hi = 62.0;
  1502. mon_rec.nVrefresh = 1;
  1503. }
  1504. if (add_default_modes)
  1505. default_modes = xf86GetDefaultModes();
  1506. /*
  1507. * If this is not an RB monitor, remove RB modes from the default
  1508. * pool. RB modes from the config or the monitor itself are fine.
  1509. */
  1510. if (!mon_rec.reducedblanking)
  1511. xf86ValidateModesReducedBlanking(scrn, default_modes);
  1512. if (sync_source == sync_config) {
  1513. /*
  1514. * Check output and config modes against sync range from config file
  1515. */
  1516. xf86ValidateModesSync(scrn, output_modes, &mon_rec);
  1517. xf86ValidateModesSync(scrn, config_modes, &mon_rec);
  1518. }
  1519. /*
  1520. * Check default modes against sync range
  1521. */
  1522. xf86ValidateModesSync(scrn, default_modes, &mon_rec);
  1523. /*
  1524. * Check default modes against monitor max clock
  1525. */
  1526. if (max_clock) {
  1527. xf86ValidateModesClocks(scrn, default_modes,
  1528. &min_clock, &max_clock, 1);
  1529. xf86ValidateModesClocks(scrn, output_modes,
  1530. &min_clock, &max_clock, 1);
  1531. }
  1532. output->probed_modes = NULL;
  1533. output->probed_modes = xf86ModesAdd(output->probed_modes, config_modes);
  1534. output->probed_modes = xf86ModesAdd(output->probed_modes, output_modes);
  1535. output->probed_modes =
  1536. xf86ModesAdd(output->probed_modes, default_modes);
  1537. /*
  1538. * Check all modes against max size, interlace, and doublescan
  1539. */
  1540. if (maxX && maxY)
  1541. xf86ValidateModesSize(scrn, output->probed_modes, maxX, maxY, 0);
  1542. {
  1543. int flags = (output->interlaceAllowed ? V_INTERLACE : 0) |
  1544. (output->doubleScanAllowed ? V_DBLSCAN : 0);
  1545. xf86ValidateModesFlags(scrn, output->probed_modes, flags);
  1546. }
  1547. /*
  1548. * Check all modes against output
  1549. */
  1550. for (mode = output->probed_modes; mode != NULL; mode = mode->next)
  1551. if (mode->status == MODE_OK)
  1552. mode->status = (*output->funcs->mode_valid) (output, mode);
  1553. xf86PruneInvalidModes(scrn, &output->probed_modes, debug_modes);
  1554. output->probed_modes = xf86SortModes(output->probed_modes);
  1555. /* Check for a configured preference for a particular mode */
  1556. preferred_mode = preferredMode(scrn, output);
  1557. if (preferred_mode) {
  1558. for (mode = output->probed_modes; mode; mode = mode->next) {
  1559. if (!strcmp(preferred_mode, mode->name)) {
  1560. if (mode != output->probed_modes) {
  1561. if (mode->prev)
  1562. mode->prev->next = mode->next;
  1563. if (mode->next)
  1564. mode->next->prev = mode->prev;
  1565. mode->next = output->probed_modes;
  1566. output->probed_modes->prev = mode;
  1567. mode->prev = NULL;
  1568. output->probed_modes = mode;
  1569. }
  1570. mode->type |= (M_T_PREFERRED | M_T_USERPREF);
  1571. break;
  1572. }
  1573. }
  1574. }
  1575. /* Ctrl+Alt+Keypad-{Plus,Minus} zoom mode: M_T_USERDEF mode type */
  1576. processZoomModes(output);
  1577. output->initial_rotation = xf86OutputInitialRotation(output);
  1578. if (debug_modes) {
  1579. if (output->probed_modes != NULL) {
  1580. xf86DrvMsg(scrn->scrnIndex, X_INFO,
  1581. "Printing probed modes for output %s\n",
  1582. output->name);
  1583. }
  1584. else {
  1585. xf86DrvMsg(scrn->scrnIndex, X_INFO,
  1586. "No remaining probed modes for output %s\n",
  1587. output->name);
  1588. }
  1589. }
  1590. for (mode = output->probed_modes; mode != NULL; mode = mode->next) {
  1591. /* The code to choose the best mode per pipe later on will require
  1592. * VRefresh to be set.
  1593. */
  1594. mode->VRefresh = xf86ModeVRefresh(mode);
  1595. xf86SetModeCrtc(mode, INTERLACE_HALVE_V);
  1596. if (debug_modes)
  1597. xf86PrintModeline(scrn->scrnIndex, mode);
  1598. }
  1599. }
  1600. }
  1601. /**
  1602. * Copy one of the output mode lists to the ScrnInfo record
  1603. */
  1604. static DisplayModePtr
  1605. biggestMode(DisplayModePtr a, DisplayModePtr b)
  1606. {
  1607. int A, B;
  1608. if (!a)
  1609. return b;
  1610. if (!b)
  1611. return a;
  1612. A = a->HDisplay * a->VDisplay;
  1613. B = b->HDisplay * b->VDisplay;
  1614. if (A > B)
  1615. return a;
  1616. return b;
  1617. }
  1618. static xf86OutputPtr
  1619. SetCompatOutput(xf86CrtcConfigPtr config)
  1620. {
  1621. xf86OutputPtr output = NULL, test = NULL;
  1622. DisplayModePtr maxmode = NULL, testmode, mode;
  1623. int o, compat = -1, count, mincount = 0;
  1624. if (config->num_output == 0)
  1625. return NULL;
  1626. /* Look for one that's definitely connected */
  1627. for (o = 0; o < config->num_output; o++) {
  1628. test = config->output[o];
  1629. if (!test->crtc)
  1630. continue;
  1631. if (test->status != XF86OutputStatusConnected)
  1632. continue;
  1633. if (!test->probed_modes)
  1634. continue;
  1635. testmode = mode = test->probed_modes;
  1636. for (count = 0; mode; mode = mode->next, count++)
  1637. testmode = biggestMode(testmode, mode);
  1638. if (!output) {
  1639. output = test;
  1640. compat = o;
  1641. maxmode = testmode;
  1642. mincount = count;
  1643. }
  1644. else if (maxmode == biggestMode(maxmode, testmode)) {
  1645. output = test;
  1646. compat = o;
  1647. maxmode = testmode;
  1648. mincount = count;
  1649. }
  1650. else if ((maxmode->HDisplay == testmode->HDisplay) &&
  1651. (maxmode->VDisplay == testmode->VDisplay) &&
  1652. count <= mincount) {
  1653. output = test;
  1654. compat = o;
  1655. maxmode = testmode;
  1656. mincount = count;
  1657. }
  1658. }
  1659. /* If we didn't find one, take anything we can get */
  1660. if (!output) {
  1661. for (o = 0; o < config->num_output; o++) {
  1662. test = config->output[o];
  1663. if (!test->crtc)
  1664. continue;
  1665. if (!test->probed_modes)
  1666. continue;
  1667. if (!output) {
  1668. output = test;
  1669. compat = o;
  1670. }
  1671. else if (test->probed_modes->HDisplay <
  1672. output->probed_modes->HDisplay) {
  1673. output = test;
  1674. compat = o;
  1675. }
  1676. }
  1677. }
  1678. if (compat >= 0) {
  1679. config->compat_output = compat;
  1680. }
  1681. else if (config->compat_output >= 0 && config->compat_output < config->num_output) {
  1682. /* Don't change the compat output when no valid outputs found */
  1683. output = config->output[config->compat_output];
  1684. }
  1685. /* All outputs are disconnected, select one to fake */
  1686. if (!output && config->num_output) {
  1687. config->compat_output = 0;
  1688. output = config->output[config->compat_output];
  1689. }
  1690. return output;
  1691. }
  1692. void
  1693. xf86SetScrnInfoModes(ScrnInfoPtr scrn)
  1694. {
  1695. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  1696. xf86OutputPtr output;
  1697. xf86CrtcPtr crtc;
  1698. DisplayModePtr last, mode = NULL;
  1699. output = SetCompatOutput(config);
  1700. if (!output)
  1701. return; /* punt */
  1702. crtc = output->crtc;
  1703. /* Clear any existing modes from scrn->modes */
  1704. while (scrn->modes != NULL)
  1705. xf86DeleteMode(&scrn->modes, scrn->modes);
  1706. /* Set scrn->modes to the mode list for the 'compat' output */
  1707. scrn->modes = xf86DuplicateModes(scrn, output->probed_modes);
  1708. if (crtc) {
  1709. for (mode = scrn->modes; mode; mode = mode->next)
  1710. if (xf86ModesEqual(mode, &crtc->desiredMode))
  1711. break;
  1712. }
  1713. if (!scrn->modes) {
  1714. scrn->modes = xf86ModesAdd(scrn->modes,
  1715. xf86CVTMode(scrn->display->virtualX,
  1716. scrn->display->virtualY,
  1717. 60, 0, 0));
  1718. }
  1719. /* For some reason, scrn->modes is circular, unlike the other mode
  1720. * lists. How great is that?
  1721. */
  1722. for (last = scrn->modes; last && last->next; last = last->next);
  1723. last->next = scrn->modes;
  1724. scrn->modes->prev = last;
  1725. if (mode) {
  1726. while (scrn->modes != mode)
  1727. scrn->modes = scrn->modes->next;
  1728. }
  1729. scrn->currentMode = scrn->modes;
  1730. #ifdef XFreeXDGA
  1731. if (scrn->pScreen)
  1732. _xf86_di_dga_reinit_internal(scrn->pScreen);
  1733. #endif
  1734. }
  1735. static Bool
  1736. xf86CollectEnabledOutputs(ScrnInfoPtr scrn, xf86CrtcConfigPtr config,
  1737. Bool *enabled)
  1738. {
  1739. Bool any_enabled = FALSE;
  1740. int o;
  1741. /*
  1742. * Don't bother enabling outputs on GPU screens: a client needs to attach
  1743. * it to a source provider before setting a mode that scans out a shared
  1744. * pixmap.
  1745. */
  1746. if (scrn->is_gpu)
  1747. return FALSE;
  1748. for (o = 0; o < config->num_output; o++)
  1749. any_enabled |= enabled[o] = xf86OutputEnabled(config->output[o], TRUE);
  1750. if (!any_enabled) {
  1751. xf86DrvMsg(scrn->scrnIndex, X_WARNING,
  1752. "No outputs definitely connected, trying again...\n");
  1753. for (o = 0; o < config->num_output; o++)
  1754. any_enabled |= enabled[o] =
  1755. xf86OutputEnabled(config->output[o], FALSE);
  1756. }
  1757. return any_enabled;
  1758. }
  1759. static Bool
  1760. nextEnabledOutput(xf86CrtcConfigPtr config, Bool *enabled, int *index)
  1761. {
  1762. int o = *index;
  1763. for (o++; o < config->num_output; o++) {
  1764. if (enabled[o]) {
  1765. *index = o;
  1766. return TRUE;
  1767. }
  1768. }
  1769. return FALSE;
  1770. }
  1771. static Bool
  1772. aspectMatch(float a, float b)
  1773. {
  1774. return fabs(1 - (a / b)) < 0.05;
  1775. }
  1776. static DisplayModePtr
  1777. nextAspectMode(xf86OutputPtr o, DisplayModePtr last, float aspect)
  1778. {
  1779. DisplayModePtr m = NULL;
  1780. if (!o)
  1781. return NULL;
  1782. if (!last)
  1783. m = o->probed_modes;
  1784. else
  1785. m = last->next;
  1786. for (; m; m = m->next)
  1787. if (aspectMatch(aspect, (float) m->HDisplay / (float) m->VDisplay))
  1788. return m;
  1789. return NULL;
  1790. }
  1791. static DisplayModePtr
  1792. bestModeForAspect(xf86CrtcConfigPtr config, Bool *enabled, float aspect)
  1793. {
  1794. int o = -1, p;
  1795. DisplayModePtr mode = NULL, test = NULL, match = NULL;
  1796. if (!nextEnabledOutput(config, enabled, &o))
  1797. return NULL;
  1798. while ((mode = nextAspectMode(config->output[o], mode, aspect))) {
  1799. test = mode;
  1800. for (p = o; nextEnabledOutput(config, enabled, &p);) {
  1801. test = xf86OutputFindClosestMode(config->output[p], mode);
  1802. if (!test)
  1803. break;
  1804. if (test->HDisplay != mode->HDisplay ||
  1805. test->VDisplay != mode->VDisplay) {
  1806. test = NULL;
  1807. break;
  1808. }
  1809. }
  1810. /* if we didn't match it on all outputs, try the next one */
  1811. if (!test)
  1812. continue;
  1813. /* if it's bigger than the last one, save it */
  1814. if (!match || (test->HDisplay > match->HDisplay))
  1815. match = test;
  1816. }
  1817. /* return the biggest one found */
  1818. return match;
  1819. }
  1820. static Bool
  1821. xf86TargetPreferred(ScrnInfoPtr scrn, xf86CrtcConfigPtr config,
  1822. DisplayModePtr * modes, Bool *enabled,
  1823. int width, int height)
  1824. {
  1825. int o, p;
  1826. int max_pref_width = 0, max_pref_height = 0;
  1827. DisplayModePtr *preferred, *preferred_match;
  1828. Bool ret = FALSE;
  1829. preferred = xnfcalloc(config->num_output, sizeof(DisplayModePtr));
  1830. preferred_match = xnfcalloc(config->num_output, sizeof(DisplayModePtr));
  1831. /* Check if the preferred mode is available on all outputs */
  1832. for (p = -1; nextEnabledOutput(config, enabled, &p);) {
  1833. Rotation r = config->output[p]->initial_rotation;
  1834. DisplayModePtr mode;
  1835. if ((preferred[p] = xf86OutputHasPreferredMode(config->output[p],
  1836. width, height))) {
  1837. int pref_width = xf86ModeWidth(preferred[p], r);
  1838. int pref_height = xf86ModeHeight(preferred[p], r);
  1839. Bool all_match = TRUE;
  1840. for (o = -1; nextEnabledOutput(config, enabled, &o);) {
  1841. Bool match = FALSE;
  1842. xf86OutputPtr output = config->output[o];
  1843. if (o == p)
  1844. continue;
  1845. /*
  1846. * First see if the preferred mode matches on the next
  1847. * output as well. This catches the common case of identical
  1848. * monitors and makes sure they all have the same timings
  1849. * and refresh. If that fails, we fall back to trying to
  1850. * match just width & height.
  1851. */
  1852. mode = xf86OutputHasPreferredMode(output, pref_width,
  1853. pref_height);
  1854. if (mode && xf86ModesEqual(mode, preferred[p])) {
  1855. preferred[o] = mode;
  1856. match = TRUE;
  1857. }
  1858. else {
  1859. for (mode = output->probed_modes; mode; mode = mode->next) {
  1860. Rotation ir = output->initial_rotation;
  1861. if (xf86ModeWidth(mode, ir) == pref_width &&
  1862. xf86ModeHeight(mode, ir) == pref_height) {
  1863. preferred[o] = mode;
  1864. match = TRUE;
  1865. }
  1866. }
  1867. }
  1868. all_match &= match;
  1869. }
  1870. if (all_match &&
  1871. (pref_width * pref_height > max_pref_width * max_pref_height)) {
  1872. for (o = -1; nextEnabledOutput(config, enabled, &o);)
  1873. preferred_match[o] = preferred[o];
  1874. max_pref_width = pref_width;
  1875. max_pref_height = pref_height;
  1876. ret = TRUE;
  1877. }
  1878. }
  1879. }
  1880. /*
  1881. * If there's no preferred mode, but only one monitor, pick the
  1882. * biggest mode for its aspect ratio or 4:3, assuming one exists.
  1883. */
  1884. if (!ret)
  1885. do {
  1886. int i = 0;
  1887. float aspect = 0.0;
  1888. DisplayModePtr a = NULL, b = NULL;
  1889. /* count the number of enabled outputs */
  1890. for (i = 0, p = -1; nextEnabledOutput(config, enabled, &p); i++);
  1891. if (i != 1)
  1892. break;
  1893. p = -1;
  1894. nextEnabledOutput(config, enabled, &p);
  1895. if (config->output[p]->mm_height)
  1896. aspect = (float) config->output[p]->mm_width /
  1897. (float) config->output[p]->mm_height;
  1898. a = bestModeForAspect(config, enabled, 4.0/3.0);
  1899. if (aspect)
  1900. b = bestModeForAspect(config, enabled, aspect);
  1901. preferred_match[p] = biggestMode(a, b);
  1902. if (preferred_match[p])
  1903. ret = TRUE;
  1904. } while (0);
  1905. if (ret) {
  1906. /* oh good, there is a match. stash the selected modes and return. */
  1907. memcpy(modes, preferred_match,
  1908. config->num_output * sizeof(DisplayModePtr));
  1909. }
  1910. free(preferred);
  1911. free(preferred_match);
  1912. return ret;
  1913. }
  1914. static Bool
  1915. xf86TargetAspect(ScrnInfoPtr scrn, xf86CrtcConfigPtr config,
  1916. DisplayModePtr * modes, Bool *enabled, int width, int height)
  1917. {
  1918. int o;
  1919. float aspect = 0.0, *aspects;
  1920. xf86OutputPtr output;
  1921. Bool ret = FALSE;
  1922. DisplayModePtr guess = NULL, aspect_guess = NULL, base_guess = NULL;
  1923. aspects = xnfcalloc(config->num_output, sizeof(float));
  1924. /* collect the aspect ratios */
  1925. for (o = -1; nextEnabledOutput(config, enabled, &o);) {
  1926. output = config->output[o];
  1927. if (output->mm_height)
  1928. aspects[o] = (float) output->mm_width / (float) output->mm_height;
  1929. else
  1930. aspects[o] = 4.0 / 3.0;
  1931. }
  1932. /* check that they're all the same */
  1933. for (o = -1; nextEnabledOutput(config, enabled, &o);) {
  1934. output = config->output[o];
  1935. if (!aspect) {
  1936. aspect = aspects[o];
  1937. }
  1938. else if (!aspectMatch(aspect, aspects[o])) {
  1939. goto no_aspect_match;
  1940. }
  1941. }
  1942. /* if they're all 4:3, just skip ahead and save effort */
  1943. if (!aspectMatch(aspect, 4.0 / 3.0))
  1944. aspect_guess = bestModeForAspect(config, enabled, aspect);
  1945. no_aspect_match:
  1946. base_guess = bestModeForAspect(config, enabled, 4.0 / 3.0);
  1947. guess = biggestMode(base_guess, aspect_guess);
  1948. if (!guess)
  1949. goto out;
  1950. /* found a mode that works everywhere, now apply it */
  1951. for (o = -1; nextEnabledOutput(config, enabled, &o);) {
  1952. modes[o] = xf86OutputFindClosestMode(config->output[o], guess);
  1953. }
  1954. ret = TRUE;
  1955. out:
  1956. free(aspects);
  1957. return ret;
  1958. }
  1959. static Bool
  1960. xf86TargetFallback(ScrnInfoPtr scrn, xf86CrtcConfigPtr config,
  1961. DisplayModePtr * modes, Bool *enabled, int width, int height)
  1962. {
  1963. DisplayModePtr target_mode = NULL;
  1964. Rotation target_rotation = RR_Rotate_0;
  1965. DisplayModePtr default_mode;
  1966. int default_preferred, target_preferred = 0, o;
  1967. /* User preferred > preferred > other modes */
  1968. for (o = -1; nextEnabledOutput(config, enabled, &o);) {
  1969. default_mode = xf86DefaultMode(config->output[o], width, height);
  1970. if (!default_mode)
  1971. continue;
  1972. default_preferred = (((default_mode->type & M_T_PREFERRED) != 0) +
  1973. ((default_mode->type & M_T_USERPREF) != 0));
  1974. if (default_preferred > target_preferred || !target_mode) {
  1975. target_mode = default_mode;
  1976. target_preferred = default_preferred;
  1977. target_rotation = config->output[o]->initial_rotation;
  1978. config->compat_output = o;
  1979. }
  1980. }
  1981. if (target_mode)
  1982. modes[config->compat_output] = target_mode;
  1983. /* Fill in other output modes */
  1984. for (o = -1; nextEnabledOutput(config, enabled, &o);) {
  1985. if (!modes[o])
  1986. modes[o] = xf86ClosestMode(config->output[o], target_mode,
  1987. target_rotation, width, height);
  1988. }
  1989. return target_mode != NULL;
  1990. }
  1991. static Bool
  1992. xf86TargetUserpref(ScrnInfoPtr scrn, xf86CrtcConfigPtr config,
  1993. DisplayModePtr * modes, Bool *enabled, int width, int height)
  1994. {
  1995. int o;
  1996. if (xf86UserConfiguredOutputs(scrn, modes))
  1997. return xf86TargetFallback(scrn, config, modes, enabled, width, height);
  1998. for (o = -1; nextEnabledOutput(config, enabled, &o);)
  1999. if (xf86OutputHasUserPreferredMode(config->output[o]))
  2000. return
  2001. xf86TargetFallback(scrn, config, modes, enabled, width, height);
  2002. return FALSE;
  2003. }
  2004. static Bool
  2005. xf86CrtcSetInitialGamma(xf86CrtcPtr crtc, float gamma_red, float gamma_green,
  2006. float gamma_blue)
  2007. {
  2008. int i, size = 256;
  2009. CARD16 *red, *green, *blue;
  2010. red = malloc(3 * size * sizeof(CARD16));
  2011. green = red + size;
  2012. blue = green + size;
  2013. /* Only cause warning if user wanted gamma to be set. */
  2014. if (!crtc->funcs->gamma_set &&
  2015. (gamma_red != 1.0 || gamma_green != 1.0 || gamma_blue != 1.0)) {
  2016. free(red);
  2017. return FALSE;
  2018. }
  2019. else if (!crtc->funcs->gamma_set) {
  2020. free(red);
  2021. return TRUE;
  2022. }
  2023. /* At this early stage none of the randr-interface stuff is up.
  2024. * So take the default gamma size for lack of something better.
  2025. */
  2026. for (i = 0; i < size; i++) {
  2027. if (gamma_red == 1.0)
  2028. red[i] = i << 8;
  2029. else
  2030. red[i] = (CARD16) (pow((double) i / (double) (size - 1),
  2031. 1. / (double) gamma_red) * (double) (size -
  2032. 1) *
  2033. 256);
  2034. if (gamma_green == 1.0)
  2035. green[i] = i << 8;
  2036. else
  2037. green[i] = (CARD16) (pow((double) i / (double) (size - 1),
  2038. 1. / (double) gamma_green) *
  2039. (double) (size - 1) * 256);
  2040. if (gamma_blue == 1.0)
  2041. blue[i] = i << 8;
  2042. else
  2043. blue[i] = (CARD16) (pow((double) i / (double) (size - 1),
  2044. 1. / (double) gamma_blue) * (double) (size -
  2045. 1) *
  2046. 256);
  2047. }
  2048. /* Default size is 256, so anything else is failure. */
  2049. if (size != crtc->gamma_size) {
  2050. free(red);
  2051. return FALSE;
  2052. }
  2053. crtc->gamma_size = size;
  2054. memcpy(crtc->gamma_red, red, crtc->gamma_size * sizeof(CARD16));
  2055. memcpy(crtc->gamma_green, green, crtc->gamma_size * sizeof(CARD16));
  2056. memcpy(crtc->gamma_blue, blue, crtc->gamma_size * sizeof(CARD16));
  2057. /* Do not set gamma now, delay until the crtc is activated. */
  2058. free(red);
  2059. return TRUE;
  2060. }
  2061. static Bool
  2062. xf86OutputSetInitialGamma(xf86OutputPtr output)
  2063. {
  2064. XF86ConfMonitorPtr mon = output->conf_monitor;
  2065. float gamma_red = 1.0, gamma_green = 1.0, gamma_blue = 1.0;
  2066. if (!mon)
  2067. return TRUE;
  2068. if (!output->crtc)
  2069. return FALSE;
  2070. /* Get configured values, where they exist. */
  2071. if (mon->mon_gamma_red >= GAMMA_MIN && mon->mon_gamma_red <= GAMMA_MAX)
  2072. gamma_red = mon->mon_gamma_red;
  2073. if (mon->mon_gamma_green >= GAMMA_MIN && mon->mon_gamma_green <= GAMMA_MAX)
  2074. gamma_green = mon->mon_gamma_green;
  2075. if (mon->mon_gamma_blue >= GAMMA_MIN && mon->mon_gamma_blue <= GAMMA_MAX)
  2076. gamma_blue = mon->mon_gamma_blue;
  2077. /* This avoids setting gamma 1.0 in case another cloned output on this crtc has a specific gamma. */
  2078. if (gamma_red != 1.0 || gamma_green != 1.0 || gamma_blue != 1.0) {
  2079. xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
  2080. "Output %s wants gamma correction (%.1f, %.1f, %.1f)\n",
  2081. output->name, gamma_red, gamma_green, gamma_blue);
  2082. return xf86CrtcSetInitialGamma(output->crtc, gamma_red, gamma_green,
  2083. gamma_blue);
  2084. }
  2085. else
  2086. return TRUE;
  2087. }
  2088. /**
  2089. * Construct default screen configuration
  2090. *
  2091. * Given auto-detected (and, eventually, configured) values,
  2092. * construct a usable configuration for the system
  2093. *
  2094. * canGrow indicates that the driver can resize the screen to larger than its
  2095. * initially configured size via the config->funcs->resize hook. If TRUE, this
  2096. * function will set virtualX and virtualY to match the initial configuration
  2097. * and leave config->max{Width,Height} alone. If FALSE, it will bloat
  2098. * virtual[XY] to include the largest modes and set config->max{Width,Height}
  2099. * accordingly.
  2100. */
  2101. Bool
  2102. xf86InitialConfiguration(ScrnInfoPtr scrn, Bool canGrow)
  2103. {
  2104. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  2105. int o, c;
  2106. xf86CrtcPtr *crtcs;
  2107. DisplayModePtr *modes;
  2108. Bool *enabled;
  2109. int width, height;
  2110. int i = scrn->scrnIndex;
  2111. Bool have_outputs = TRUE;
  2112. Bool ret;
  2113. Bool success = FALSE;
  2114. /* Set up the device options */
  2115. config->options = xnfalloc(sizeof(xf86DeviceOptions));
  2116. memcpy(config->options, xf86DeviceOptions, sizeof(xf86DeviceOptions));
  2117. xf86ProcessOptions(scrn->scrnIndex, scrn->options, config->options);
  2118. config->debug_modes = xf86ReturnOptValBool(config->options,
  2119. OPTION_MODEDEBUG, FALSE);
  2120. if (scrn->display->virtualX && !scrn->is_gpu)
  2121. width = scrn->display->virtualX;
  2122. else
  2123. width = config->maxWidth;
  2124. if (scrn->display->virtualY && !scrn->is_gpu)
  2125. height = scrn->display->virtualY;
  2126. else
  2127. height = config->maxHeight;
  2128. xf86ProbeOutputModes(scrn, width, height);
  2129. crtcs = xnfcalloc(config->num_output, sizeof(xf86CrtcPtr));
  2130. modes = xnfcalloc(config->num_output, sizeof(DisplayModePtr));
  2131. enabled = xnfcalloc(config->num_output, sizeof(Bool));
  2132. ret = xf86CollectEnabledOutputs(scrn, config, enabled);
  2133. if (ret == FALSE && canGrow) {
  2134. if (!scrn->is_gpu)
  2135. xf86DrvMsg(i, X_WARNING,
  2136. "Unable to find connected outputs - setting %dx%d "
  2137. "initial framebuffer\n",
  2138. NO_OUTPUT_DEFAULT_WIDTH, NO_OUTPUT_DEFAULT_HEIGHT);
  2139. have_outputs = FALSE;
  2140. }
  2141. else {
  2142. if (xf86TargetUserpref(scrn, config, modes, enabled, width, height))
  2143. xf86DrvMsg(i, X_INFO, "Using user preference for initial modes\n");
  2144. else if (xf86TargetPreferred
  2145. (scrn, config, modes, enabled, width, height))
  2146. xf86DrvMsg(i, X_INFO, "Using exact sizes for initial modes\n");
  2147. else if (xf86TargetAspect(scrn, config, modes, enabled, width, height))
  2148. xf86DrvMsg(i, X_INFO,
  2149. "Using fuzzy aspect match for initial modes\n");
  2150. else if (xf86TargetFallback
  2151. (scrn, config, modes, enabled, width, height))
  2152. xf86DrvMsg(i, X_INFO, "Using sloppy heuristic for initial modes\n");
  2153. else
  2154. xf86DrvMsg(i, X_WARNING, "Unable to find initial modes\n");
  2155. }
  2156. for (o = -1; nextEnabledOutput(config, enabled, &o);) {
  2157. if (!modes[o])
  2158. xf86DrvMsg(scrn->scrnIndex, X_ERROR,
  2159. "Output %s enabled but has no modes\n",
  2160. config->output[o]->name);
  2161. else
  2162. xf86DrvMsg(scrn->scrnIndex, X_INFO,
  2163. "Output %s using initial mode %s\n",
  2164. config->output[o]->name, modes[o]->name);
  2165. }
  2166. /*
  2167. * Set the position of each output
  2168. */
  2169. if (!xf86InitialOutputPositions(scrn, modes))
  2170. goto bailout;
  2171. /*
  2172. * Set initial panning of each output
  2173. */
  2174. xf86InitialPanning(scrn);
  2175. /*
  2176. * Assign CRTCs to fit output configuration
  2177. */
  2178. if (have_outputs && !xf86PickCrtcs(scrn, crtcs, modes, 0, width, height))
  2179. goto bailout;
  2180. /* XXX override xf86 common frame computation code */
  2181. if (!scrn->is_gpu) {
  2182. scrn->display->frameX0 = 0;
  2183. scrn->display->frameY0 = 0;
  2184. }
  2185. for (c = 0; c < config->num_crtc; c++) {
  2186. xf86CrtcPtr crtc = config->crtc[c];
  2187. crtc->enabled = FALSE;
  2188. memset(&crtc->desiredMode, '\0', sizeof(crtc->desiredMode));
  2189. /* Set default gamma for all crtc's. */
  2190. /* This is done to avoid problems later on with cloned outputs. */
  2191. xf86CrtcSetInitialGamma(crtc, 1.0, 1.0, 1.0);
  2192. }
  2193. if (xf86_crtc_supports_gamma(scrn))
  2194. xf86DrvMsg(scrn->scrnIndex, X_INFO,
  2195. "Using default gamma of (1.0, 1.0, 1.0) unless otherwise stated.\n");
  2196. /*
  2197. * Set initial configuration
  2198. */
  2199. for (o = 0; o < config->num_output; o++) {
  2200. xf86OutputPtr output = config->output[o];
  2201. DisplayModePtr mode = modes[o];
  2202. xf86CrtcPtr crtc = crtcs[o];
  2203. if (mode && crtc) {
  2204. xf86SaveModeContents(&crtc->desiredMode, mode);
  2205. crtc->desiredRotation = output->initial_rotation;
  2206. crtc->desiredX = output->initial_x;
  2207. crtc->desiredY = output->initial_y;
  2208. crtc->desiredTransformPresent = FALSE;
  2209. crtc->enabled = TRUE;
  2210. memcpy(&crtc->panningTotalArea, &output->initialTotalArea,
  2211. sizeof(BoxRec));
  2212. memcpy(&crtc->panningTrackingArea, &output->initialTrackingArea,
  2213. sizeof(BoxRec));
  2214. memcpy(crtc->panningBorder, output->initialBorder,
  2215. 4 * sizeof(INT16));
  2216. output->crtc = crtc;
  2217. if (!xf86OutputSetInitialGamma(output))
  2218. xf86DrvMsg(scrn->scrnIndex, X_WARNING,
  2219. "Initial gamma correction for output %s: failed.\n",
  2220. output->name);
  2221. }
  2222. else {
  2223. output->crtc = NULL;
  2224. }
  2225. }
  2226. if (scrn->display->virtualX == 0 || scrn->is_gpu) {
  2227. /*
  2228. * Expand virtual size to cover the current config and potential mode
  2229. * switches, if the driver can't enlarge the screen later.
  2230. */
  2231. xf86DefaultScreenLimits(scrn, &width, &height, canGrow);
  2232. if (have_outputs == FALSE) {
  2233. if (width < NO_OUTPUT_DEFAULT_WIDTH &&
  2234. height < NO_OUTPUT_DEFAULT_HEIGHT) {
  2235. width = NO_OUTPUT_DEFAULT_WIDTH;
  2236. height = NO_OUTPUT_DEFAULT_HEIGHT;
  2237. }
  2238. }
  2239. if (!scrn->is_gpu) {
  2240. scrn->display->virtualX = width;
  2241. scrn->display->virtualY = height;
  2242. }
  2243. }
  2244. if (width > scrn->virtualX)
  2245. scrn->virtualX = width;
  2246. if (height > scrn->virtualY)
  2247. scrn->virtualY = height;
  2248. /*
  2249. * Make sure the configuration isn't too small.
  2250. */
  2251. if (width < config->minWidth || height < config->minHeight)
  2252. goto bailout;
  2253. /*
  2254. * Limit the crtc config to virtual[XY] if the driver can't grow the
  2255. * desktop.
  2256. */
  2257. if (!canGrow) {
  2258. xf86CrtcSetSizeRange(scrn, config->minWidth, config->minHeight,
  2259. width, height);
  2260. }
  2261. xf86SetScrnInfoModes(scrn);
  2262. success = TRUE;
  2263. bailout:
  2264. free(crtcs);
  2265. free(modes);
  2266. free(enabled);
  2267. return success;
  2268. }
  2269. /*
  2270. * Check the CRTC we're going to map each output to vs. it's current
  2271. * CRTC. If they don't match, we have to disable the output and the CRTC
  2272. * since the driver will have to re-route things.
  2273. */
  2274. static void
  2275. xf86PrepareOutputs(ScrnInfoPtr scrn)
  2276. {
  2277. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  2278. int o;
  2279. for (o = 0; o < config->num_output; o++) {
  2280. xf86OutputPtr output = config->output[o];
  2281. #if RANDR_GET_CRTC_INTERFACE
  2282. /* Disable outputs that are unused or will be re-routed */
  2283. if (!output->funcs->get_crtc ||
  2284. output->crtc != (*output->funcs->get_crtc) (output) ||
  2285. output->crtc == NULL)
  2286. #endif
  2287. (*output->funcs->dpms) (output, DPMSModeOff);
  2288. }
  2289. }
  2290. static void
  2291. xf86PrepareCrtcs(ScrnInfoPtr scrn)
  2292. {
  2293. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  2294. int c;
  2295. for (c = 0; c < config->num_crtc; c++) {
  2296. #if RANDR_GET_CRTC_INTERFACE
  2297. xf86CrtcPtr crtc = config->crtc[c];
  2298. xf86OutputPtr output = NULL;
  2299. uint32_t desired_outputs = 0, current_outputs = 0;
  2300. int o;
  2301. for (o = 0; o < config->num_output; o++) {
  2302. output = config->output[o];
  2303. if (output->crtc == crtc)
  2304. desired_outputs |= (1 << o);
  2305. /* If we can't tell where it's mapped, force it off */
  2306. if (!output->funcs->get_crtc) {
  2307. desired_outputs = 0;
  2308. break;
  2309. }
  2310. if ((*output->funcs->get_crtc) (output) == crtc)
  2311. current_outputs |= (1 << o);
  2312. }
  2313. /*
  2314. * If mappings are different or the CRTC is unused,
  2315. * we need to disable it
  2316. */
  2317. if (desired_outputs != current_outputs || !desired_outputs)
  2318. (*crtc->funcs->dpms) (crtc, DPMSModeOff);
  2319. #else
  2320. (*crtc->funcs->dpms) (crtc, DPMSModeOff);
  2321. #endif
  2322. }
  2323. }
  2324. /*
  2325. * Using the desired mode information in each crtc, set
  2326. * modes (used in EnterVT functions, or at server startup)
  2327. */
  2328. Bool
  2329. xf86SetDesiredModes(ScrnInfoPtr scrn)
  2330. {
  2331. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  2332. xf86CrtcPtr crtc = config->crtc[0];
  2333. int enabled = 0, failed = 0;
  2334. int c;
  2335. /* A driver with this hook will take care of this */
  2336. if (!crtc->funcs->set_mode_major) {
  2337. xf86PrepareOutputs(scrn);
  2338. xf86PrepareCrtcs(scrn);
  2339. }
  2340. for (c = 0; c < config->num_crtc; c++) {
  2341. xf86OutputPtr output = NULL;
  2342. int o;
  2343. RRTransformPtr transform;
  2344. crtc = config->crtc[c];
  2345. /* Skip disabled CRTCs */
  2346. if (!crtc->enabled)
  2347. continue;
  2348. if (xf86CompatOutput(scrn) && xf86CompatCrtc(scrn) == crtc)
  2349. output = xf86CompatOutput(scrn);
  2350. else {
  2351. for (o = 0; o < config->num_output; o++)
  2352. if (config->output[o]->crtc == crtc) {
  2353. output = config->output[o];
  2354. break;
  2355. }
  2356. }
  2357. /* paranoia */
  2358. if (!output)
  2359. continue;
  2360. /* Mark that we'll need to re-set the mode for sure */
  2361. memset(&crtc->mode, 0, sizeof(crtc->mode));
  2362. if (!crtc->desiredMode.CrtcHDisplay) {
  2363. DisplayModePtr mode =
  2364. xf86OutputFindClosestMode(output, scrn->currentMode);
  2365. if (!mode)
  2366. return FALSE;
  2367. xf86SaveModeContents(&crtc->desiredMode, mode);
  2368. crtc->desiredRotation = RR_Rotate_0;
  2369. crtc->desiredTransformPresent = FALSE;
  2370. crtc->desiredX = 0;
  2371. crtc->desiredY = 0;
  2372. }
  2373. if (crtc->desiredTransformPresent)
  2374. transform = &crtc->desiredTransform;
  2375. else
  2376. transform = NULL;
  2377. if (xf86CrtcSetModeTransform
  2378. (crtc, &crtc->desiredMode, crtc->desiredRotation, transform,
  2379. crtc->desiredX, crtc->desiredY)) {
  2380. ++enabled;
  2381. } else {
  2382. for (o = 0; o < config->num_output; o++)
  2383. if (config->output[o]->crtc == crtc)
  2384. config->output[o]->crtc = NULL;
  2385. crtc->enabled = FALSE;
  2386. ++failed;
  2387. }
  2388. }
  2389. xf86DisableUnusedFunctions(scrn);
  2390. return enabled != 0 || failed == 0;
  2391. }
  2392. /**
  2393. * In the current world order, there are lists of modes per output, which may
  2394. * or may not include the mode that was asked to be set by XFree86's mode
  2395. * selection. Find the closest one, in the following preference order:
  2396. *
  2397. * - Equality
  2398. * - Closer in size to the requested mode, but no larger
  2399. * - Closer in refresh rate to the requested mode.
  2400. */
  2401. DisplayModePtr
  2402. xf86OutputFindClosestMode(xf86OutputPtr output, DisplayModePtr desired)
  2403. {
  2404. DisplayModePtr best = NULL, scan = NULL;
  2405. for (scan = output->probed_modes; scan != NULL; scan = scan->next) {
  2406. /* If there's an exact match, we're done. */
  2407. if (xf86ModesEqual(scan, desired)) {
  2408. best = desired;
  2409. break;
  2410. }
  2411. /* Reject if it's larger than the desired mode. */
  2412. if (scan->HDisplay > desired->HDisplay ||
  2413. scan->VDisplay > desired->VDisplay) {
  2414. continue;
  2415. }
  2416. /*
  2417. * If we haven't picked a best mode yet, use the first
  2418. * one in the size range
  2419. */
  2420. if (best == NULL) {
  2421. best = scan;
  2422. continue;
  2423. }
  2424. /* Find if it's closer to the right size than the current best
  2425. * option.
  2426. */
  2427. if ((scan->HDisplay > best->HDisplay &&
  2428. scan->VDisplay >= best->VDisplay) ||
  2429. (scan->HDisplay >= best->HDisplay &&
  2430. scan->VDisplay > best->VDisplay)) {
  2431. best = scan;
  2432. continue;
  2433. }
  2434. /* Find if it's still closer to the right refresh than the current
  2435. * best resolution.
  2436. */
  2437. if (scan->HDisplay == best->HDisplay &&
  2438. scan->VDisplay == best->VDisplay &&
  2439. (fabs(scan->VRefresh - desired->VRefresh) <
  2440. fabs(best->VRefresh - desired->VRefresh))) {
  2441. best = scan;
  2442. }
  2443. }
  2444. return best;
  2445. }
  2446. /**
  2447. * When setting a mode through XFree86-VidModeExtension or XFree86-DGA,
  2448. * take the specified mode and apply it to the crtc connected to the compat
  2449. * output. Then, find similar modes for the other outputs, as with the
  2450. * InitialConfiguration code above. The goal is to clone the desired
  2451. * mode across all outputs that are currently active.
  2452. */
  2453. Bool
  2454. xf86SetSingleMode(ScrnInfoPtr pScrn, DisplayModePtr desired, Rotation rotation)
  2455. {
  2456. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
  2457. Bool ok = TRUE;
  2458. xf86OutputPtr compat_output;
  2459. DisplayModePtr compat_mode = NULL;
  2460. int c;
  2461. /*
  2462. * Let the compat output drive the final mode selection
  2463. */
  2464. compat_output = xf86CompatOutput(pScrn);
  2465. if (compat_output)
  2466. compat_mode = xf86OutputFindClosestMode(compat_output, desired);
  2467. if (compat_mode)
  2468. desired = compat_mode;
  2469. for (c = 0; c < config->num_crtc; c++) {
  2470. xf86CrtcPtr crtc = config->crtc[c];
  2471. DisplayModePtr crtc_mode = NULL;
  2472. int o;
  2473. if (!crtc->enabled)
  2474. continue;
  2475. for (o = 0; o < config->num_output; o++) {
  2476. xf86OutputPtr output = config->output[o];
  2477. DisplayModePtr output_mode;
  2478. /* skip outputs not on this crtc */
  2479. if (output->crtc != crtc)
  2480. continue;
  2481. if (crtc_mode) {
  2482. output_mode = xf86OutputFindClosestMode(output, crtc_mode);
  2483. if (output_mode != crtc_mode)
  2484. output->crtc = NULL;
  2485. }
  2486. else
  2487. crtc_mode = xf86OutputFindClosestMode(output, desired);
  2488. }
  2489. if (!crtc_mode) {
  2490. crtc->enabled = FALSE;
  2491. continue;
  2492. }
  2493. if (!xf86CrtcSetModeTransform(crtc, crtc_mode, rotation, NULL, 0, 0))
  2494. ok = FALSE;
  2495. else {
  2496. xf86SaveModeContents(&crtc->desiredMode, crtc_mode);
  2497. crtc->desiredRotation = rotation;
  2498. crtc->desiredTransformPresent = FALSE;
  2499. crtc->desiredX = 0;
  2500. crtc->desiredY = 0;
  2501. }
  2502. }
  2503. xf86DisableUnusedFunctions(pScrn);
  2504. #ifdef RANDR_12_INTERFACE
  2505. xf86RandR12TellChanged(pScrn->pScreen);
  2506. #endif
  2507. return ok;
  2508. }
  2509. /**
  2510. * Set the DPMS power mode of all outputs and CRTCs.
  2511. *
  2512. * If the new mode is off, it will turn off outputs and then CRTCs.
  2513. * Otherwise, it will affect CRTCs before outputs.
  2514. */
  2515. void
  2516. xf86DPMSSet(ScrnInfoPtr scrn, int mode, int flags)
  2517. {
  2518. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  2519. int i;
  2520. if (!scrn->vtSema)
  2521. return;
  2522. if (mode == DPMSModeOff) {
  2523. for (i = 0; i < config->num_output; i++) {
  2524. xf86OutputPtr output = config->output[i];
  2525. if (output->crtc != NULL)
  2526. (*output->funcs->dpms) (output, mode);
  2527. }
  2528. }
  2529. for (i = 0; i < config->num_crtc; i++) {
  2530. xf86CrtcPtr crtc = config->crtc[i];
  2531. if (crtc->enabled)
  2532. (*crtc->funcs->dpms) (crtc, mode);
  2533. }
  2534. if (mode != DPMSModeOff) {
  2535. for (i = 0; i < config->num_output; i++) {
  2536. xf86OutputPtr output = config->output[i];
  2537. if (output->crtc != NULL)
  2538. (*output->funcs->dpms) (output, mode);
  2539. }
  2540. }
  2541. }
  2542. /**
  2543. * Implement the screensaver by just calling down into the driver DPMS hooks.
  2544. *
  2545. * Even for monitors with no DPMS support, by the definition of our DPMS hooks,
  2546. * the outputs will still get disabled (blanked).
  2547. */
  2548. Bool
  2549. xf86SaveScreen(ScreenPtr pScreen, int mode)
  2550. {
  2551. ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
  2552. if (xf86IsUnblank(mode))
  2553. xf86DPMSSet(pScrn, DPMSModeOn, 0);
  2554. else
  2555. xf86DPMSSet(pScrn, DPMSModeOff, 0);
  2556. return TRUE;
  2557. }
  2558. /**
  2559. * Disable all inactive crtcs and outputs
  2560. */
  2561. void
  2562. xf86DisableUnusedFunctions(ScrnInfoPtr pScrn)
  2563. {
  2564. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
  2565. int o, c;
  2566. for (o = 0; o < xf86_config->num_output; o++) {
  2567. xf86OutputPtr output = xf86_config->output[o];
  2568. if (!output->crtc)
  2569. (*output->funcs->dpms) (output, DPMSModeOff);
  2570. }
  2571. for (c = 0; c < xf86_config->num_crtc; c++) {
  2572. xf86CrtcPtr crtc = xf86_config->crtc[c];
  2573. if (!crtc->enabled) {
  2574. crtc->funcs->dpms(crtc, DPMSModeOff);
  2575. memset(&crtc->mode, 0, sizeof(crtc->mode));
  2576. xf86RotateDestroy(crtc);
  2577. crtc->active = FALSE;
  2578. }
  2579. }
  2580. if (pScrn->pScreen)
  2581. xf86_crtc_notify(pScrn->pScreen);
  2582. if (pScrn->ModeSet)
  2583. pScrn->ModeSet(pScrn);
  2584. }
  2585. #ifdef RANDR_12_INTERFACE
  2586. #define EDID_ATOM_NAME "EDID"
  2587. /**
  2588. * Set the RandR EDID property
  2589. */
  2590. static void
  2591. xf86OutputSetEDIDProperty(xf86OutputPtr output, void *data, int data_len)
  2592. {
  2593. Atom edid_atom = MakeAtom(EDID_ATOM_NAME, sizeof(EDID_ATOM_NAME) - 1, TRUE);
  2594. /* This may get called before the RandR resources have been created */
  2595. if (output->randr_output == NULL)
  2596. return;
  2597. if (data_len != 0) {
  2598. RRChangeOutputProperty(output->randr_output, edid_atom, XA_INTEGER, 8,
  2599. PropModeReplace, data_len, data, FALSE, TRUE);
  2600. }
  2601. else {
  2602. RRDeleteOutputProperty(output->randr_output, edid_atom);
  2603. }
  2604. }
  2605. #endif
  2606. /* Pull out a phyiscal size from a detailed timing if available. */
  2607. struct det_phySize_parameter {
  2608. xf86OutputPtr output;
  2609. ddc_quirk_t quirks;
  2610. Bool ret;
  2611. };
  2612. static void
  2613. handle_detailed_physical_size(struct detailed_monitor_section
  2614. *det_mon, void *data)
  2615. {
  2616. struct det_phySize_parameter *p;
  2617. p = (struct det_phySize_parameter *) data;
  2618. if (p->ret == TRUE)
  2619. return;
  2620. xf86DetTimingApplyQuirks(det_mon, p->quirks,
  2621. p->output->MonInfo->features.hsize,
  2622. p->output->MonInfo->features.vsize);
  2623. if (det_mon->type == DT &&
  2624. det_mon->section.d_timings.h_size != 0 &&
  2625. det_mon->section.d_timings.v_size != 0) {
  2626. /* some sanity checking for aspect ratio:
  2627. assume any h / v (or v / h) > 2.4 to be bogus.
  2628. This would even include cinemascope */
  2629. if (((det_mon->section.d_timings.h_size * 5) <
  2630. (det_mon->section.d_timings.v_size * 12)) &&
  2631. ((det_mon->section.d_timings.v_size * 5) <
  2632. (det_mon->section.d_timings.h_size * 12))) {
  2633. p->output->mm_width = det_mon->section.d_timings.h_size;
  2634. p->output->mm_height = det_mon->section.d_timings.v_size;
  2635. p->ret = TRUE;
  2636. } else
  2637. xf86DrvMsg(p->output->scrn->scrnIndex, X_WARNING,
  2638. "Output %s: Strange aspect ratio (%i/%i), "
  2639. "consider adding a quirk\n", p->output->name,
  2640. det_mon->section.d_timings.h_size,
  2641. det_mon->section.d_timings.v_size);
  2642. }
  2643. }
  2644. /**
  2645. * Set the EDID information for the specified output
  2646. */
  2647. void
  2648. xf86OutputSetEDID(xf86OutputPtr output, xf86MonPtr edid_mon)
  2649. {
  2650. ScrnInfoPtr scrn = output->scrn;
  2651. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  2652. Bool debug_modes = config->debug_modes || xf86Initialising;
  2653. #ifdef RANDR_12_INTERFACE
  2654. int size;
  2655. #endif
  2656. free(output->MonInfo);
  2657. output->MonInfo = edid_mon;
  2658. output->mm_width = 0;
  2659. output->mm_height = 0;
  2660. if (debug_modes) {
  2661. xf86DrvMsg(scrn->scrnIndex, X_INFO, "EDID for output %s\n",
  2662. output->name);
  2663. xf86PrintEDID(edid_mon);
  2664. }
  2665. /* Set the DDC properties for the 'compat' output */
  2666. if (output == xf86CompatOutput(scrn))
  2667. xf86SetDDCproperties(scrn, edid_mon);
  2668. #ifdef RANDR_12_INTERFACE
  2669. /* Set the RandR output properties */
  2670. size = 0;
  2671. if (edid_mon) {
  2672. if (edid_mon->ver.version == 1) {
  2673. size = 128;
  2674. if (edid_mon->flags & EDID_COMPLETE_RAWDATA)
  2675. size += edid_mon->no_sections * 128;
  2676. }
  2677. else if (edid_mon->ver.version == 2)
  2678. size = 256;
  2679. }
  2680. xf86OutputSetEDIDProperty(output, edid_mon ? edid_mon->rawData : NULL,
  2681. size);
  2682. #endif
  2683. if (edid_mon) {
  2684. struct det_phySize_parameter p;
  2685. p.output = output;
  2686. p.quirks = xf86DDCDetectQuirks(scrn->scrnIndex, edid_mon, FALSE);
  2687. p.ret = FALSE;
  2688. xf86ForEachDetailedBlock(edid_mon, handle_detailed_physical_size, &p);
  2689. /* if no mm size is available from a detailed timing, check the max size field */
  2690. if ((!output->mm_width || !output->mm_height) &&
  2691. (edid_mon->features.hsize && edid_mon->features.vsize)) {
  2692. output->mm_width = edid_mon->features.hsize * 10;
  2693. output->mm_height = edid_mon->features.vsize * 10;
  2694. }
  2695. }
  2696. }
  2697. /**
  2698. * Return the list of modes supported by the EDID information
  2699. * stored in 'output'
  2700. */
  2701. DisplayModePtr
  2702. xf86OutputGetEDIDModes(xf86OutputPtr output)
  2703. {
  2704. ScrnInfoPtr scrn = output->scrn;
  2705. xf86MonPtr edid_mon = output->MonInfo;
  2706. if (!edid_mon)
  2707. return NULL;
  2708. return xf86DDCGetModes(scrn->scrnIndex, edid_mon);
  2709. }
  2710. /* maybe we should care about DDC1? meh. */
  2711. xf86MonPtr
  2712. xf86OutputGetEDID(xf86OutputPtr output, I2CBusPtr pDDCBus)
  2713. {
  2714. ScrnInfoPtr scrn = output->scrn;
  2715. xf86MonPtr mon;
  2716. mon = xf86DoEEDID(scrn, pDDCBus, TRUE);
  2717. if (mon)
  2718. xf86DDCApplyQuirks(scrn->scrnIndex, mon);
  2719. return mon;
  2720. }
  2721. static const char *_xf86ConnectorNames[] = {
  2722. "None", "VGA", "DVI-I", "DVI-D",
  2723. "DVI-A", "Composite", "S-Video",
  2724. "Component", "LFP", "Proprietary",
  2725. "HDMI", "DisplayPort",
  2726. };
  2727. const char *
  2728. xf86ConnectorGetName(xf86ConnectorType connector)
  2729. {
  2730. return _xf86ConnectorNames[connector];
  2731. }
  2732. static void
  2733. x86_crtc_box_intersect(BoxPtr dest, BoxPtr a, BoxPtr b)
  2734. {
  2735. dest->x1 = a->x1 > b->x1 ? a->x1 : b->x1;
  2736. dest->x2 = a->x2 < b->x2 ? a->x2 : b->x2;
  2737. dest->y1 = a->y1 > b->y1 ? a->y1 : b->y1;
  2738. dest->y2 = a->y2 < b->y2 ? a->y2 : b->y2;
  2739. if (dest->x1 >= dest->x2 || dest->y1 >= dest->y2)
  2740. dest->x1 = dest->x2 = dest->y1 = dest->y2 = 0;
  2741. }
  2742. static void
  2743. x86_crtc_box(xf86CrtcPtr crtc, BoxPtr crtc_box)
  2744. {
  2745. if (crtc->enabled) {
  2746. crtc_box->x1 = crtc->x;
  2747. crtc_box->x2 = crtc->x + xf86ModeWidth(&crtc->mode, crtc->rotation);
  2748. crtc_box->y1 = crtc->y;
  2749. crtc_box->y2 = crtc->y + xf86ModeHeight(&crtc->mode, crtc->rotation);
  2750. }
  2751. else
  2752. crtc_box->x1 = crtc_box->x2 = crtc_box->y1 = crtc_box->y2 = 0;
  2753. }
  2754. static int
  2755. xf86_crtc_box_area(BoxPtr box)
  2756. {
  2757. return (int) (box->x2 - box->x1) * (int) (box->y2 - box->y1);
  2758. }
  2759. #ifdef XV
  2760. /*
  2761. * Return the crtc covering 'box'. If two crtcs cover a portion of
  2762. * 'box', then prefer 'desired'. If 'desired' is NULL, then prefer the crtc
  2763. * with greater coverage
  2764. */
  2765. static xf86CrtcPtr
  2766. xf86_covering_crtc(ScrnInfoPtr pScrn,
  2767. BoxPtr box, xf86CrtcPtr desired, BoxPtr crtc_box_ret)
  2768. {
  2769. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
  2770. xf86CrtcPtr crtc, best_crtc;
  2771. int coverage, best_coverage;
  2772. int c;
  2773. BoxRec crtc_box, cover_box;
  2774. best_crtc = NULL;
  2775. best_coverage = 0;
  2776. crtc_box_ret->x1 = 0;
  2777. crtc_box_ret->x2 = 0;
  2778. crtc_box_ret->y1 = 0;
  2779. crtc_box_ret->y2 = 0;
  2780. for (c = 0; c < xf86_config->num_crtc; c++) {
  2781. crtc = xf86_config->crtc[c];
  2782. x86_crtc_box(crtc, &crtc_box);
  2783. x86_crtc_box_intersect(&cover_box, &crtc_box, box);
  2784. coverage = xf86_crtc_box_area(&cover_box);
  2785. if (coverage && crtc == desired) {
  2786. *crtc_box_ret = crtc_box;
  2787. return crtc;
  2788. }
  2789. else if (coverage > best_coverage) {
  2790. *crtc_box_ret = crtc_box;
  2791. best_crtc = crtc;
  2792. best_coverage = coverage;
  2793. }
  2794. }
  2795. return best_crtc;
  2796. }
  2797. /*
  2798. * For overlay video, compute the relevant CRTC and
  2799. * clip video to that.
  2800. *
  2801. * returning FALSE means there was a memory failure of some kind,
  2802. * not that the video shouldn't be displayed
  2803. */
  2804. Bool
  2805. xf86_crtc_clip_video_helper(ScrnInfoPtr pScrn,
  2806. xf86CrtcPtr * crtc_ret,
  2807. xf86CrtcPtr desired_crtc,
  2808. BoxPtr dst,
  2809. INT32 *xa,
  2810. INT32 *xb,
  2811. INT32 *ya,
  2812. INT32 *yb, RegionPtr reg, INT32 width, INT32 height)
  2813. {
  2814. Bool ret;
  2815. RegionRec crtc_region_local;
  2816. RegionPtr crtc_region = reg;
  2817. if (crtc_ret) {
  2818. BoxRec crtc_box;
  2819. xf86CrtcPtr crtc = xf86_covering_crtc(pScrn, dst,
  2820. desired_crtc,
  2821. &crtc_box);
  2822. if (crtc) {
  2823. RegionInit(&crtc_region_local, &crtc_box, 1);
  2824. crtc_region = &crtc_region_local;
  2825. RegionIntersect(crtc_region, crtc_region, reg);
  2826. }
  2827. *crtc_ret = crtc;
  2828. }
  2829. ret = xf86XVClipVideoHelper(dst, xa, xb, ya, yb,
  2830. crtc_region, width, height);
  2831. if (crtc_region != reg)
  2832. RegionUninit(&crtc_region_local);
  2833. return ret;
  2834. }
  2835. #endif
  2836. xf86_crtc_notify_proc_ptr
  2837. xf86_wrap_crtc_notify(ScreenPtr screen, xf86_crtc_notify_proc_ptr new)
  2838. {
  2839. if (xf86CrtcConfigPrivateIndex != -1) {
  2840. ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
  2841. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  2842. xf86_crtc_notify_proc_ptr old;
  2843. old = config->xf86_crtc_notify;
  2844. config->xf86_crtc_notify = new;
  2845. return old;
  2846. }
  2847. return NULL;
  2848. }
  2849. void
  2850. xf86_unwrap_crtc_notify(ScreenPtr screen, xf86_crtc_notify_proc_ptr old)
  2851. {
  2852. if (xf86CrtcConfigPrivateIndex != -1) {
  2853. ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
  2854. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  2855. config->xf86_crtc_notify = old;
  2856. }
  2857. }
  2858. void
  2859. xf86_crtc_notify(ScreenPtr screen)
  2860. {
  2861. ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
  2862. xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
  2863. if (config->xf86_crtc_notify)
  2864. config->xf86_crtc_notify(screen);
  2865. }
  2866. Bool
  2867. xf86_crtc_supports_gamma(ScrnInfoPtr pScrn)
  2868. {
  2869. if (xf86CrtcConfigPrivateIndex != -1) {
  2870. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
  2871. xf86CrtcPtr crtc;
  2872. /* for multiple drivers loaded we need this */
  2873. if (!xf86_config)
  2874. return FALSE;
  2875. if (xf86_config->num_crtc == 0)
  2876. return FALSE;
  2877. crtc = xf86_config->crtc[0];
  2878. return crtc->funcs->gamma_set != NULL;
  2879. }
  2880. return FALSE;
  2881. }
  2882. void
  2883. xf86ProviderSetup(ScrnInfoPtr scrn,
  2884. const xf86ProviderFuncsRec *funcs, const char *name)
  2885. {
  2886. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
  2887. assert(!xf86_config->name);
  2888. assert(name);
  2889. xf86_config->name = strdup(name);
  2890. xf86_config->provider_funcs = funcs;
  2891. #ifdef RANDR_12_INTERFACE
  2892. xf86_config->randr_provider = NULL;
  2893. #endif
  2894. }
  2895. void
  2896. xf86DetachAllCrtc(ScrnInfoPtr scrn)
  2897. {
  2898. xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
  2899. int i;
  2900. for (i = 0; i < xf86_config->num_crtc; i++) {
  2901. xf86CrtcPtr crtc = xf86_config->crtc[i];
  2902. if (crtc->randr_crtc)
  2903. RRCrtcDetachScanoutPixmap(crtc->randr_crtc);
  2904. /* dpms off */
  2905. (*crtc->funcs->dpms) (crtc, DPMSModeOff);
  2906. /* force a reset the next time its used */
  2907. crtc->randr_crtc->mode = NULL;
  2908. crtc->mode.HDisplay = 0;
  2909. crtc->x = crtc->y = 0;
  2910. }
  2911. }