Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624
  1. /* $Id: xm_api.c,v 1.2 2000/09/26 20:54:13 brianp Exp $ */
  2. /*
  3. * Mesa 3-D graphics library
  4. * Version: 3.3
  5. *
  6. * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  22. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. /*
  26. * This file contains the implementations of all the XMesa* functions.
  27. *
  28. *
  29. * NOTES:
  30. *
  31. * The window coordinate system origin (0,0) is in the lower-left corner
  32. * of the window. X11's window coordinate origin is in the upper-left
  33. * corner of the window. Therefore, most drawing functions in this
  34. * file have to flip Y coordinates.
  35. *
  36. * Define USE_XSHM in the Makefile with -DUSE_XSHM if you want to compile
  37. * in support for the MIT Shared Memory extension. If enabled, when you
  38. * use an Ximage for the back buffer in double buffered mode, the "swap"
  39. * operation will be faster. You must also link with -lXext.
  40. *
  41. * Byte swapping: If the Mesa host and the X display use a different
  42. * byte order then there's some trickiness to be aware of when using
  43. * XImages. The byte ordering used for the XImage is that of the X
  44. * display, not the Mesa host.
  45. * The color-to-pixel encoding for True/DirectColor must be done
  46. * according to the display's visual red_mask, green_mask, and blue_mask.
  47. * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
  48. * do byte swapping if needed. If one wants to directly "poke" the pixel
  49. * into the XImage's buffer then the pixel must be byte swapped first. In
  50. * Mesa, when byte swapping is needed we use the PF_TRUECOLOR pixel format
  51. * and use XPutPixel everywhere except in the implementation of
  52. * glClear(GL_COLOR_BUFFER_BIT). We want this function to be fast so
  53. * instead of using XPutPixel we "poke" our values after byte-swapping
  54. * the clear pixel value if needed.
  55. *
  56. */
  57. #ifdef __CYGWIN__
  58. #undef WIN32
  59. #undef __WIN32__
  60. #endif
  61. #include "glxheader.h"
  62. #include "GL/xmesa.h"
  63. #include "xmesaP.h"
  64. #include "context.h"
  65. #include "extensions.h"
  66. #include "glthread.h"
  67. #include "matrix.h"
  68. #include "mem.h"
  69. #include "types.h"
  70. #ifdef HAVE_CONFIG_H
  71. #include "conf.h"
  72. #endif
  73. #ifndef GLX_NONE_EXT
  74. #define GLX_NONE_EXT 0x8000
  75. #endif
  76. /*
  77. * Global X driver lock
  78. */
  79. _glthread_Mutex _xmesa_lock;
  80. /*
  81. * Lookup tables for HPCR pixel format:
  82. */
  83. static short hpcr_rgbTbl[3][256] = {
  84. {
  85. 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
  86. 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
  87. 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
  88. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  89. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  90. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  91. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  92. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  93. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  94. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  95. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  96. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  97. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  98. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  99. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  100. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
  101. },
  102. {
  103. 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
  104. 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
  105. 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
  106. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  107. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  108. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  109. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  110. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  111. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  112. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  113. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  114. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  115. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  116. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  117. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  118. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
  119. },
  120. {
  121. 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
  122. 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 47, 47,
  123. 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55,
  124. 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63,
  125. 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71,
  126. 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78, 79, 79,
  127. 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87,
  128. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  129. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  130. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  131. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  132. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  133. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  134. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  135. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  136. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223
  137. }
  138. };
  139. /**********************************************************************/
  140. /***** X Utility Functions *****/
  141. /**********************************************************************/
  142. /*
  143. * X/Mesa error reporting function:
  144. */
  145. static void error( const char *msg )
  146. {
  147. if (getenv("MESA_DEBUG"))
  148. fprintf( stderr, "X/Mesa error: %s\n", msg );
  149. }
  150. /*
  151. * Return the host's byte order as LSBFirst or MSBFirst ala X.
  152. */
  153. #ifndef XFree86Server
  154. static int host_byte_order( void )
  155. {
  156. int i = 1;
  157. char *cptr = (char *) &i;
  158. return (*cptr==1) ? LSBFirst : MSBFirst;
  159. }
  160. #endif
  161. /*
  162. * Error handling.
  163. */
  164. #ifndef XFree86Server
  165. static int mesaXErrorFlag = 0;
  166. static int mesaHandleXError( XMesaDisplay *dpy, XErrorEvent *event )
  167. {
  168. (void) dpy;
  169. (void) event;
  170. mesaXErrorFlag = 1;
  171. return 0;
  172. }
  173. #endif
  174. /*
  175. * Check if the X Shared Memory extension is available.
  176. * Return: 0 = not available
  177. * 1 = shared XImage support available
  178. * 2 = shared Pixmap support available also
  179. */
  180. #ifndef XFree86Server
  181. static int check_for_xshm( XMesaDisplay *display )
  182. {
  183. #ifdef USE_XSHM
  184. int major, minor, ignore;
  185. Bool pixmaps;
  186. if (XQueryExtension( display, "MIT-SHM", &ignore, &ignore, &ignore )) {
  187. if (XShmQueryVersion( display, &major, &minor, &pixmaps )==True) {
  188. return (pixmaps==True) ? 2 : 1;
  189. }
  190. else {
  191. return 0;
  192. }
  193. }
  194. else {
  195. return 0;
  196. }
  197. #else
  198. /* Can't compile XSHM support */
  199. return 0;
  200. #endif
  201. }
  202. #endif
  203. /*
  204. * Return the width and height of the given drawable.
  205. */
  206. static void get_drawable_size( XMesaDisplay *dpy, XMesaDrawable d,
  207. unsigned int *width, unsigned int *height)
  208. {
  209. #ifdef XFree86Server
  210. (void) dpy;
  211. *width = d->width;
  212. *height = d->height;
  213. #else
  214. Window root;
  215. int x, y;
  216. unsigned int bw, depth;
  217. _glthread_LOCK_MUTEX(_xmesa_lock);
  218. XGetGeometry( dpy, d, &root, &x, &y, width, height, &bw, &depth );
  219. _glthread_UNLOCK_MUTEX(_xmesa_lock);
  220. #endif
  221. }
  222. /*
  223. * Apply gamma correction to an intensity value in [0..max]. Return the
  224. * new intensity value.
  225. */
  226. static GLint gamma_adjust( GLfloat gamma, GLint value, GLint max )
  227. {
  228. if (gamma == 1.0) {
  229. return value;
  230. }
  231. else {
  232. double x = (double) value / (double) max;
  233. return (GLint) ((GLfloat) max * pow( x, 1.0F/gamma ) + 0.5F);
  234. }
  235. }
  236. /*
  237. * Return the true number of bits per pixel for XImages.
  238. * For example, if we request a 24-bit deep visual we may actually need/get
  239. * 32bpp XImages. This function returns the appropriate bpp.
  240. * Input: dpy - the X display
  241. * visinfo - desribes the visual to be used for XImages
  242. * Return: true number of bits per pixel for XImages
  243. */
  244. #define GET_BITS_PER_PIXEL(xmv) bits_per_pixel(xmv)
  245. #ifdef XFree86Server
  246. static int bits_per_pixel( XMesaVisual xmv )
  247. {
  248. XMesaVisualInfo visinfo = xmv->visinfo;
  249. const int depth = visinfo->nplanes;
  250. int i;
  251. for (i = 0; i < screenInfo.numPixmapFormats; i++) {
  252. if (screenInfo.formats[i].depth == depth)
  253. return screenInfo.formats[i].bitsPerPixel;
  254. }
  255. return depth; /* should never get here, but this should be safe */
  256. }
  257. #else
  258. static int bits_per_pixel( XMesaVisual xmv )
  259. {
  260. XMesaDisplay *dpy = xmv->display;
  261. XMesaVisualInfo visinfo = xmv->visinfo;
  262. XMesaImage *img;
  263. int bitsPerPixel;
  264. /* Create a temporary XImage */
  265. img = XCreateImage( dpy, visinfo->visual, visinfo->depth,
  266. ZPixmap, 0, /*format, offset*/
  267. (char*) MALLOC(8), /*data*/
  268. 1, 1, /*width, height*/
  269. 32, /*bitmap_pad*/
  270. 0 /*bytes_per_line*/
  271. );
  272. assert(img);
  273. /* grab the bits/pixel value */
  274. bitsPerPixel = img->bits_per_pixel;
  275. /* free the XImage */
  276. FREE( img->data );
  277. img->data = NULL;
  278. XMesaDestroyImage( img );
  279. return bitsPerPixel;
  280. }
  281. #endif
  282. /*
  283. * Determine if a given X window ID is valid (window exists).
  284. * Do this by calling XGetWindowAttributes() for the window and
  285. * checking if we catch an X error.
  286. * Input: dpy - the display
  287. * win - the window to check for existance
  288. * Return: GL_TRUE - window exists
  289. * GL_FALSE - window doesn't exist
  290. */
  291. #ifndef XFree86Server
  292. static GLboolean WindowExistsFlag;
  293. static int window_exists_err_handler( XMesaDisplay* dpy, XErrorEvent* xerr )
  294. {
  295. (void) dpy;
  296. if (xerr->error_code == BadWindow) {
  297. WindowExistsFlag = GL_FALSE;
  298. }
  299. return 0;
  300. }
  301. static GLboolean window_exists( XMesaDisplay *dpy, Window win )
  302. {
  303. XWindowAttributes wa;
  304. int (*old_handler)( XMesaDisplay*, XErrorEvent* );
  305. WindowExistsFlag = GL_TRUE;
  306. old_handler = XSetErrorHandler(window_exists_err_handler);
  307. XGetWindowAttributes( dpy, win, &wa ); /* dummy request */
  308. XSetErrorHandler(old_handler);
  309. return WindowExistsFlag;
  310. }
  311. #endif
  312. /**********************************************************************/
  313. /***** Linked list of XMesaBuffers *****/
  314. /**********************************************************************/
  315. static XMesaBuffer XMesaBufferList = NULL;
  316. /* Allocate a new XMesaBuffer, add to linked list */
  317. static XMesaBuffer alloc_xmesa_buffer(void)
  318. {
  319. XMesaBuffer b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer);
  320. if (b) {
  321. b->Next = XMesaBufferList;
  322. XMesaBufferList = b;
  323. }
  324. return b;
  325. }
  326. /*
  327. * Find an XMesaBuffer by matching X display and colormap but NOT matching
  328. * the notThis buffer.
  329. */
  330. static XMesaBuffer find_xmesa_buffer(XMesaDisplay *dpy,
  331. XMesaColormap cmap,
  332. XMesaBuffer notThis)
  333. {
  334. XMesaBuffer b;
  335. for (b=XMesaBufferList; b; b=b->Next) {
  336. if (b->display==dpy && b->cmap==cmap && b!=notThis) {
  337. return b;
  338. }
  339. }
  340. return NULL;
  341. }
  342. /*
  343. * Free an XMesaBuffer, remove from linked list, perhaps free X colormap
  344. * entries.
  345. */
  346. static void free_xmesa_buffer(int client, XMesaBuffer buffer)
  347. {
  348. XMesaBuffer prev = NULL, b;
  349. (void) client;
  350. for (b=XMesaBufferList; b; b=b->Next) {
  351. if (b==buffer) {
  352. /* unlink bufer from list */
  353. if (prev)
  354. prev->Next = buffer->Next;
  355. else
  356. XMesaBufferList = buffer->Next;
  357. /* Check to free X colors */
  358. if (buffer->num_alloced>0) {
  359. /* If no other buffer uses this X colormap then free the colors. */
  360. if (!find_xmesa_buffer(buffer->display, buffer->cmap, buffer)) {
  361. #ifdef XFree86Server
  362. (void)FreeColors(buffer->cmap, client,
  363. buffer->num_alloced, buffer->alloced_colors,
  364. 0);
  365. #else
  366. XFreeColors(buffer->display, buffer->cmap,
  367. buffer->alloced_colors, buffer->num_alloced, 0);
  368. #endif
  369. }
  370. }
  371. FREE(buffer);
  372. return;
  373. }
  374. /* continue search */
  375. prev = b;
  376. }
  377. /* buffer not found in XMesaBufferList */
  378. gl_problem(NULL,"free_xmesa_buffer() - buffer not found\n");
  379. }
  380. /* Copy X color table stuff from one XMesaBuffer to another. */
  381. static void copy_colortable_info(XMesaBuffer dst, const XMesaBuffer src)
  382. {
  383. MEMCPY(dst->color_table, src->color_table, sizeof(src->color_table));
  384. MEMCPY(dst->pixel_to_r, src->pixel_to_r, sizeof(src->pixel_to_r));
  385. MEMCPY(dst->pixel_to_g, src->pixel_to_g, sizeof(src->pixel_to_g));
  386. MEMCPY(dst->pixel_to_b, src->pixel_to_b, sizeof(src->pixel_to_b));
  387. dst->num_alloced = src->num_alloced;
  388. MEMCPY(dst->alloced_colors, src->alloced_colors,
  389. sizeof(src->alloced_colors));
  390. }
  391. /**********************************************************************/
  392. /***** Misc Private Functions *****/
  393. /**********************************************************************/
  394. /*
  395. * Return number of bits set in n.
  396. */
  397. static int bitcount( unsigned long n )
  398. {
  399. int bits;
  400. for (bits=0; n>0; n=n>>1) {
  401. if (n&1) {
  402. bits++;
  403. }
  404. }
  405. return bits;
  406. }
  407. /*
  408. * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
  409. * Return: GL_TRUE if success, GL_FALSE if error
  410. */
  411. #ifndef XFree86Server
  412. static GLboolean alloc_shm_back_buffer( XMesaBuffer b )
  413. {
  414. #ifdef USE_XSHM
  415. /*
  416. * We have to do a _lot_ of error checking here to be sure we can
  417. * really use the XSHM extension. It seems different servers trigger
  418. * errors at different points if the extension won't work. Therefore
  419. * we have to be very careful...
  420. */
  421. GC gc;
  422. int (*old_handler)( XMesaDisplay *, XErrorEvent * );
  423. b->backimage = XShmCreateImage( b->xm_visual->display,
  424. b->xm_visual->visinfo->visual,
  425. b->xm_visual->visinfo->depth,
  426. ZPixmap, NULL, &b->shminfo,
  427. b->width, b->height );
  428. if (b->backimage == NULL) {
  429. error("alloc_back_buffer: Shared memory error (XShmCreateImage), disabling.");
  430. b->shm = 0;
  431. return GL_FALSE;
  432. }
  433. b->shminfo.shmid = shmget( IPC_PRIVATE, b->backimage->bytes_per_line
  434. * b->backimage->height, IPC_CREAT|0777 );
  435. if (b->shminfo.shmid < 0) {
  436. if (getenv("MESA_DEBUG"))
  437. perror("alloc_back_buffer");
  438. XDestroyImage( b->backimage );
  439. b->backimage = NULL;
  440. error("alloc_back_buffer: Shared memory error (shmget), disabling.");
  441. b->shm = 0;
  442. return GL_FALSE;
  443. }
  444. b->shminfo.shmaddr = b->backimage->data
  445. = (char*)shmat( b->shminfo.shmid, 0, 0 );
  446. if (b->shminfo.shmaddr == (char *) -1) {
  447. if (getenv("MESA_DEBUG"))
  448. perror("alloc_back_buffer");
  449. XDestroyImage( b->backimage );
  450. shmctl( b->shminfo.shmid, IPC_RMID, 0 );
  451. b->backimage = NULL;
  452. error("alloc_back_buffer: Shared memory error (shmat), disabling.");
  453. b->shm = 0;
  454. return GL_FALSE;
  455. }
  456. b->shminfo.readOnly = False;
  457. mesaXErrorFlag = 0;
  458. old_handler = XSetErrorHandler( mesaHandleXError );
  459. /* This may trigger the X protocol error we're ready to catch: */
  460. XShmAttach( b->xm_visual->display, &b->shminfo );
  461. XSync( b->xm_visual->display, False );
  462. if (mesaXErrorFlag) {
  463. /* we are on a remote display, this error is normal, don't print it */
  464. XFlush( b->xm_visual->display );
  465. mesaXErrorFlag = 0;
  466. XDestroyImage( b->backimage );
  467. shmdt( b->shminfo.shmaddr );
  468. shmctl( b->shminfo.shmid, IPC_RMID, 0 );
  469. b->backimage = NULL;
  470. b->shm = 0;
  471. (void) XSetErrorHandler( old_handler );
  472. return GL_FALSE;
  473. }
  474. shmctl( b->shminfo.shmid, IPC_RMID, 0 ); /* nobody else needs it */
  475. /* Finally, try an XShmPutImage to be really sure the extension works */
  476. gc = XCreateGC( b->xm_visual->display, b->frontbuffer, 0, NULL );
  477. XShmPutImage( b->xm_visual->display, b->frontbuffer, gc,
  478. b->backimage, 0, 0, 0, 0, 1, 1 /*one pixel*/, False );
  479. XSync( b->xm_visual->display, False );
  480. XFreeGC( b->xm_visual->display, gc );
  481. (void) XSetErrorHandler( old_handler );
  482. if (mesaXErrorFlag) {
  483. XFlush( b->xm_visual->display );
  484. mesaXErrorFlag = 0;
  485. XDestroyImage( b->backimage );
  486. shmdt( b->shminfo.shmaddr );
  487. shmctl( b->shminfo.shmid, IPC_RMID, 0 );
  488. b->backimage = NULL;
  489. b->shm = 0;
  490. return GL_FALSE;
  491. }
  492. if (b->backimage) {
  493. int height = b->backimage->height;
  494. /* Needed by PIXELADDR1 macro */
  495. b->ximage_width1 = b->backimage->bytes_per_line;
  496. b->ximage_origin1 = (GLubyte *) b->backimage->data
  497. + b->ximage_width1 * (height-1);
  498. /* Needed by PIXELADDR2 macro */
  499. b->ximage_width2 = b->backimage->bytes_per_line / 2;
  500. b->ximage_origin2 = (GLushort *) b->backimage->data
  501. + b->ximage_width2 * (height-1);
  502. /* Needed by PIXELADDR3 macro */
  503. b->ximage_width3 = b->backimage->bytes_per_line;
  504. b->ximage_origin3 = (GLubyte *) b->backimage->data
  505. + b->ximage_width3 * (height-1);
  506. /* Needed by PIXELADDR4 macro */
  507. b->ximage_width4 = b->backimage->width;
  508. b->ximage_origin4 = (GLuint *) b->backimage->data
  509. + b->ximage_width4 * (height-1);
  510. }
  511. return GL_TRUE;
  512. #else
  513. /* Can't compile XSHM support */
  514. return GL_FALSE;
  515. #endif
  516. }
  517. #endif
  518. /*
  519. * Setup an off-screen pixmap or Ximage to use as the back buffer.
  520. * Input: b - the X/Mesa buffer
  521. */
  522. void xmesa_alloc_back_buffer( XMesaBuffer b )
  523. {
  524. if (b->db_state==BACK_XIMAGE) {
  525. /* Deallocate the old backimage, if any */
  526. if (b->backimage) {
  527. #if defined(USE_XSHM) && !defined(XFree86Server)
  528. if (b->shm) {
  529. XShmDetach( b->xm_visual->display, &b->shminfo );
  530. XDestroyImage( b->backimage );
  531. shmdt( b->shminfo.shmaddr );
  532. }
  533. else
  534. #endif
  535. XMesaDestroyImage( b->backimage );
  536. b->backimage = NULL;
  537. }
  538. /* Allocate new back buffer */
  539. #ifdef XFree86Server
  540. {
  541. /* Allocate a regular XImage for the back buffer. */
  542. b->backimage = XMesaCreateImage(b->xm_visual->BitsPerPixel,
  543. b->width, b->height, NULL);
  544. #else
  545. if (b->shm==0
  546. || alloc_shm_back_buffer(b)==GL_FALSE
  547. ) {
  548. /* Allocate a regular XImage for the back buffer. */
  549. b->backimage = XCreateImage( b->xm_visual->display,
  550. b->xm_visual->visinfo->visual,
  551. GET_VISUAL_DEPTH(b->xm_visual),
  552. ZPixmap, 0, /* format, offset */
  553. NULL, b->width, b->height,
  554. 8, 0 ); /* pad, bytes_per_line */
  555. #endif
  556. if (!b->backimage) {
  557. error("alloc_back_buffer: XCreateImage failed.");
  558. }
  559. b->backimage->data = (char *) MALLOC( b->backimage->height
  560. * b->backimage->bytes_per_line );
  561. if (!b->backimage->data) {
  562. error("alloc_back_buffer: MALLOC failed.");
  563. XMesaDestroyImage( b->backimage );
  564. b->backimage = NULL;
  565. }
  566. }
  567. b->backpixmap = None;
  568. }
  569. else if (b->db_state==BACK_PIXMAP) {
  570. XMesaPixmap old_pixmap = b->backpixmap;
  571. /* Free the old back pixmap */
  572. if (b->backpixmap) {
  573. XMesaFreePixmap( b->xm_visual->display, b->backpixmap );
  574. }
  575. /* Allocate new back pixmap */
  576. b->backpixmap = XMesaCreatePixmap( b->xm_visual->display, b->frontbuffer,
  577. b->width, b->height,
  578. GET_VISUAL_DEPTH(b->xm_visual) );
  579. b->backimage = NULL;
  580. /* update other references to backpixmap */
  581. if (b->buffer==(XMesaDrawable)old_pixmap) {
  582. b->buffer = (XMesaDrawable)b->backpixmap;
  583. }
  584. }
  585. }
  586. /*
  587. * A replacement for XAllocColor. This function should never
  588. * fail to allocate a color. When XAllocColor fails, we return
  589. * the nearest matching color. If we have to allocate many colors
  590. * this function isn't too efficient; the XQueryColors() could be
  591. * done just once.
  592. * Written by Michael Pichler, Brian Paul, Mark Kilgard
  593. * Input: dpy - X display
  594. * cmap - X colormap
  595. * cmapSize - size of colormap
  596. * In/Out: color - the XColor struct
  597. * Output: exact - 1=exact color match, 0=closest match
  598. * alloced - 1=XAlloc worked, 0=XAlloc failed
  599. */
  600. static void
  601. noFaultXAllocColor( int client,
  602. XMesaDisplay *dpy,
  603. XMesaColormap cmap,
  604. int cmapSize,
  605. XMesaColor *color,
  606. int *exact, int *alloced )
  607. {
  608. #ifdef XFree86Server
  609. Pixel *ppixIn;
  610. xrgb *ctable;
  611. #else
  612. /* we'll try to cache ctable for better remote display performance */
  613. static Display *prevDisplay = NULL;
  614. static XMesaColormap prevCmap = 0;
  615. static int prevCmapSize = 0;
  616. static XMesaColor *ctable = NULL;
  617. #endif
  618. XMesaColor subColor;
  619. int i, bestmatch;
  620. double mindist; /* 3*2^16^2 exceeds long int precision. */
  621. (void) client;
  622. /* First try just using XAllocColor. */
  623. #ifdef XFree86Server
  624. if (AllocColor(cmap,
  625. &color->red, &color->green, &color->blue,
  626. &color->pixel,
  627. client) == Success) {
  628. #else
  629. if (XAllocColor(dpy, cmap, color)) {
  630. #endif
  631. *exact = 1;
  632. *alloced = 1;
  633. return;
  634. }
  635. /* Alloc failed, search for closest match */
  636. /* Retrieve color table entries. */
  637. /* XXX alloca candidate. */
  638. #ifdef XFree86Server
  639. ppixIn = (Pixel *) MALLOC(cmapSize * sizeof(Pixel));
  640. ctable = (xrgb *) MALLOC(cmapSize * sizeof(xrgb));
  641. for (i = 0; i < cmapSize; i++) {
  642. ppixIn[i] = i;
  643. }
  644. QueryColors(cmap, cmapSize, ppixIn, ctable);
  645. #else
  646. if (prevDisplay != dpy || prevCmap != cmap
  647. || prevCmapSize != cmapSize || !ctable) {
  648. /* free previously cached color table */
  649. if (ctable)
  650. FREE(ctable);
  651. /* Get the color table from X */
  652. ctable = (XMesaColor *) MALLOC(cmapSize * sizeof(XMesaColor));
  653. assert(ctable);
  654. for (i = 0; i < cmapSize; i++) {
  655. ctable[i].pixel = i;
  656. }
  657. XQueryColors(dpy, cmap, ctable, cmapSize);
  658. prevDisplay = dpy;
  659. prevCmap = cmap;
  660. prevCmapSize = cmapSize;
  661. }
  662. #endif
  663. /* Find best match. */
  664. bestmatch = -1;
  665. mindist = 0.0;
  666. for (i = 0; i < cmapSize; i++) {
  667. double dr = 0.30 * ((double) color->red - (double) ctable[i].red);
  668. double dg = 0.59 * ((double) color->green - (double) ctable[i].green);
  669. double db = 0.11 * ((double) color->blue - (double) ctable[i].blue);
  670. double dist = dr * dr + dg * dg + db * db;
  671. if (bestmatch < 0 || dist < mindist) {
  672. bestmatch = i;
  673. mindist = dist;
  674. }
  675. }
  676. /* Return result. */
  677. subColor.red = ctable[bestmatch].red;
  678. subColor.green = ctable[bestmatch].green;
  679. subColor.blue = ctable[bestmatch].blue;
  680. /* Try to allocate the closest match color. This should only
  681. * fail if the cell is read/write. Otherwise, we're incrementing
  682. * the cell's reference count.
  683. */
  684. #ifdef XFree86Server
  685. if (AllocColor(cmap,
  686. &subColor.red, &subColor.green, &subColor.blue,
  687. &subColor.pixel,
  688. client) == Success) {
  689. #else
  690. if (XAllocColor(dpy, cmap, &subColor)) {
  691. #endif
  692. *alloced = 1;
  693. }
  694. else {
  695. /* do this to work around a problem reported by Frank Ortega */
  696. subColor.pixel = (unsigned long) bestmatch;
  697. subColor.red = ctable[bestmatch].red;
  698. subColor.green = ctable[bestmatch].green;
  699. subColor.blue = ctable[bestmatch].blue;
  700. subColor.flags = DoRed | DoGreen | DoBlue;
  701. *alloced = 0;
  702. }
  703. #ifdef XFree86Server
  704. FREE(ppixIn);
  705. FREE(ctable);
  706. #else
  707. /* don't free table, save it for next time */
  708. #endif
  709. *color = subColor;
  710. *exact = 0;
  711. }
  712. /*
  713. * Do setup for PF_GRAYSCALE pixel format.
  714. * Note that buffer may be NULL.
  715. */
  716. static GLboolean setup_grayscale( int client, XMesaVisual v,
  717. XMesaBuffer buffer, XMesaColormap cmap )
  718. {
  719. if (GET_VISUAL_DEPTH(v)<4 || GET_VISUAL_DEPTH(v)>16) {
  720. return GL_FALSE;
  721. }
  722. if (buffer) {
  723. XMesaBuffer prevBuffer;
  724. if (!cmap) {
  725. return GL_FALSE;
  726. }
  727. prevBuffer = find_xmesa_buffer(v->display, cmap, buffer);
  728. if (prevBuffer &&
  729. (buffer->xm_visual->gl_visual->RGBAflag ==
  730. prevBuffer->xm_visual->gl_visual->RGBAflag)) {
  731. /* Copy colormap stuff from previous XMesaBuffer which uses same
  732. * X colormap. Do this to avoid time spent in noFaultXAllocColor.
  733. */
  734. copy_colortable_info(buffer, prevBuffer);
  735. }
  736. else {
  737. /* Allocate 256 shades of gray */
  738. int gray;
  739. int colorsfailed = 0;
  740. for (gray=0;gray<256;gray++) {
  741. GLint r = gamma_adjust( v->RedGamma, gray, 255 );
  742. GLint g = gamma_adjust( v->GreenGamma, gray, 255 );
  743. GLint b = gamma_adjust( v->BlueGamma, gray, 255 );
  744. int exact, alloced;
  745. XMesaColor xcol;
  746. xcol.red = (r << 8) | r;
  747. xcol.green = (g << 8) | g;
  748. xcol.blue = (b << 8) | b;
  749. noFaultXAllocColor( client, v->display,
  750. cmap, GET_COLORMAP_SIZE(v),
  751. &xcol, &exact, &alloced );
  752. if (!exact) {
  753. colorsfailed++;
  754. }
  755. if (alloced) {
  756. assert(buffer->num_alloced<256);
  757. buffer->alloced_colors[buffer->num_alloced] = xcol.pixel;
  758. buffer->num_alloced++;
  759. }
  760. /*OLD
  761. assert(gray < 576);
  762. buffer->color_table[gray*3+0] = xcol.pixel;
  763. buffer->color_table[gray*3+1] = xcol.pixel;
  764. buffer->color_table[gray*3+2] = xcol.pixel;
  765. assert(xcol.pixel < 65536);
  766. buffer->pixel_to_r[xcol.pixel] = gray * 30 / 100;
  767. buffer->pixel_to_g[xcol.pixel] = gray * 59 / 100;
  768. buffer->pixel_to_b[xcol.pixel] = gray * 11 / 100;
  769. */
  770. buffer->color_table[gray] = xcol.pixel;
  771. assert(xcol.pixel < 65536);
  772. buffer->pixel_to_r[xcol.pixel] = gray;
  773. buffer->pixel_to_g[xcol.pixel] = gray;
  774. buffer->pixel_to_b[xcol.pixel] = gray;
  775. }
  776. if (colorsfailed && getenv("MESA_DEBUG")) {
  777. fprintf( stderr,
  778. "Note: %d out of 256 needed colors do not match exactly.\n",
  779. colorsfailed );
  780. }
  781. }
  782. }
  783. v->dithered_pf = PF_GRAYSCALE;
  784. v->undithered_pf = PF_GRAYSCALE;
  785. return GL_TRUE;
  786. }
  787. /*
  788. * Setup RGB rendering for a window with a PseudoColor, StaticColor,
  789. * or 8-bit TrueColor visual visual. We try to allocate a palette of 225
  790. * colors (5 red, 9 green, 5 blue) and dither to approximate a 24-bit RGB
  791. * color. While this function was originally designed just for 8-bit
  792. * visuals, it has also proven to work from 4-bit up to 16-bit visuals.
  793. * Dithering code contributed by Bob Mercier.
  794. */
  795. static GLboolean setup_dithered_color( int client, XMesaVisual v,
  796. XMesaBuffer buffer, XMesaColormap cmap )
  797. {
  798. if (GET_VISUAL_DEPTH(v)<4 || GET_VISUAL_DEPTH(v)>16) {
  799. return GL_FALSE;
  800. }
  801. if (buffer) {
  802. XMesaBuffer prevBuffer;
  803. if (!cmap) {
  804. return GL_FALSE;
  805. }
  806. prevBuffer = find_xmesa_buffer(v->display, cmap, buffer);
  807. if (prevBuffer &&
  808. (buffer->xm_visual->gl_visual->RGBAflag ==
  809. prevBuffer->xm_visual->gl_visual->RGBAflag)) {
  810. /* Copy colormap stuff from previous, matching XMesaBuffer.
  811. * Do this to avoid time spent in noFaultXAllocColor.
  812. */
  813. copy_colortable_info(buffer, prevBuffer);
  814. }
  815. else {
  816. /* Allocate X colors and initialize color_table[], red_table[], etc */
  817. int r, g, b, i;
  818. int colorsfailed = 0;
  819. for (r = 0; r < _R; r++) {
  820. for (g = 0; g < _G; g++) {
  821. for (b = 0; b < _B; b++) {
  822. XMesaColor xcol;
  823. int exact, alloced;
  824. xcol.red =gamma_adjust(v->RedGamma, r*65535/(_R-1),65535);
  825. xcol.green=gamma_adjust(v->GreenGamma, g*65535/(_G-1),65535);
  826. xcol.blue =gamma_adjust(v->BlueGamma, b*65535/(_B-1),65535);
  827. noFaultXAllocColor( client, v->display,
  828. cmap, GET_COLORMAP_SIZE(v),
  829. &xcol, &exact, &alloced );
  830. if (!exact) {
  831. colorsfailed++;
  832. }
  833. if (alloced) {
  834. assert(buffer->num_alloced<256);
  835. buffer->alloced_colors[buffer->num_alloced] = xcol.pixel;
  836. buffer->num_alloced++;
  837. }
  838. i = _MIX( r, g, b );
  839. assert(i < 576);
  840. buffer->color_table[i] = xcol.pixel;
  841. assert(xcol.pixel < 65536);
  842. buffer->pixel_to_r[xcol.pixel] = r * 255 / (_R-1);
  843. buffer->pixel_to_g[xcol.pixel] = g * 255 / (_G-1);
  844. buffer->pixel_to_b[xcol.pixel] = b * 255 / (_B-1);
  845. }
  846. }
  847. }
  848. if (colorsfailed && getenv("MESA_DEBUG")) {
  849. fprintf( stderr,
  850. "Note: %d out of %d needed colors do not match exactly.\n",
  851. colorsfailed, _R*_G*_B );
  852. }
  853. }
  854. }
  855. v->dithered_pf = PF_DITHER;
  856. v->undithered_pf = PF_LOOKUP;
  857. return GL_TRUE;
  858. }
  859. /*
  860. * Setup for Hewlett Packard Color Recovery 8-bit TrueColor mode.
  861. * HPCR simulates 24-bit color fidelity with an 8-bit frame buffer.
  862. * Special dithering tables have to be initialized.
  863. */
  864. static void setup_8bit_hpcr( XMesaVisual v )
  865. {
  866. /* HP Color Recovery contributed by: Alex De Bruyn (ad@lms.be)
  867. * To work properly, the atom _HP_RGB_SMOOTH_MAP_LIST must be defined
  868. * on the root window AND the colormap obtainable by XGetRGBColormaps
  869. * for that atom must be set on the window. (see also tkInitWindow)
  870. * If that colormap is not set, the output will look stripy.
  871. */
  872. /* Setup color tables with gamma correction */
  873. int i;
  874. double g;
  875. g = 1.0 / v->RedGamma;
  876. for (i=0; i<256; i++) {
  877. GLint red = (GLint) (255.0 * pow( hpcr_rgbTbl[0][i]/255.0, g ) + 0.5);
  878. v->hpcr_rgbTbl[0][i] = CLAMP( red, 16, 239 );
  879. }
  880. g = 1.0 / v->GreenGamma;
  881. for (i=0; i<256; i++) {
  882. GLint green = (GLint) (255.0 * pow( hpcr_rgbTbl[1][i]/255.0, g ) + 0.5);
  883. v->hpcr_rgbTbl[1][i] = CLAMP( green, 16, 239 );
  884. }
  885. g = 1.0 / v->BlueGamma;
  886. for (i=0; i<256; i++) {
  887. GLint blue = (GLint) (255.0 * pow( hpcr_rgbTbl[2][i]/255.0, g ) + 0.5);
  888. v->hpcr_rgbTbl[2][i] = CLAMP( blue, 32, 223 );
  889. }
  890. v->undithered_pf = PF_HPCR; /* can't really disable dithering for now */
  891. v->dithered_pf = PF_HPCR;
  892. /* which method should I use to clear */
  893. /* GL_FALSE: keep the ordinary method */
  894. /* GL_TRUE : clear with dither pattern */
  895. v->hpcr_clear_flag = getenv("MESA_HPCR_CLEAR") ? GL_TRUE : GL_FALSE;
  896. if (v->hpcr_clear_flag) {
  897. v->hpcr_clear_pixmap = XMesaCreatePixmap(v->display,
  898. DefaultRootWindow(v->display),
  899. 16, 2, 8);
  900. #ifndef XFree86Server
  901. v->hpcr_clear_ximage = XGetImage(v->display, v->hpcr_clear_pixmap,
  902. 0, 0, 16, 2, AllPlanes, ZPixmap);
  903. #endif
  904. }
  905. }
  906. /*
  907. * Setup RGB rendering for a window with a True/DirectColor visual.
  908. */
  909. static void setup_truecolor( XMesaVisual v, XMesaBuffer buffer,
  910. XMesaWindow window, XMesaColormap cmap )
  911. {
  912. unsigned long rmask, gmask, bmask;
  913. (void) buffer;
  914. (void) window;
  915. (void) cmap;
  916. /* Compute red multiplier (mask) and bit shift */
  917. v->rshift = 0;
  918. rmask = GET_REDMASK(v);
  919. while ((rmask & 1)==0) {
  920. v->rshift++;
  921. rmask = rmask >> 1;
  922. }
  923. /* Compute green multiplier (mask) and bit shift */
  924. v->gshift = 0;
  925. gmask = GET_GREENMASK(v);
  926. while ((gmask & 1)==0) {
  927. v->gshift++;
  928. gmask = gmask >> 1;
  929. }
  930. /* Compute blue multiplier (mask) and bit shift */
  931. v->bshift = 0;
  932. bmask = GET_BLUEMASK(v);
  933. while ((bmask & 1)==0) {
  934. v->bshift++;
  935. bmask = bmask >> 1;
  936. }
  937. /*
  938. * Compute component-to-pixel lookup tables and dithering kernel
  939. */
  940. {
  941. static GLubyte kernel[16] = {
  942. 0*16, 8*16, 2*16, 10*16,
  943. 12*16, 4*16, 14*16, 6*16,
  944. 3*16, 11*16, 1*16, 9*16,
  945. 15*16, 7*16, 13*16, 5*16,
  946. };
  947. GLint rBits = bitcount(rmask);
  948. GLint gBits = bitcount(gmask);
  949. GLint bBits = bitcount(bmask);
  950. GLint minBits;
  951. GLuint i;
  952. /* convert pixel components in [0,_mask] to RGB values in [0,255] */
  953. for (i=0; i<=rmask; i++)
  954. v->PixelToR[i] = (unsigned char) ((i * 255) / rmask);
  955. for (i=0; i<=gmask; i++)
  956. v->PixelToG[i] = (unsigned char) ((i * 255) / gmask);
  957. for (i=0; i<=bmask; i++)
  958. v->PixelToB[i] = (unsigned char) ((i * 255) / bmask);
  959. /* convert RGB values from [0,255] to pixel components */
  960. for (i=0;i<256;i++) {
  961. GLint r = gamma_adjust(v->RedGamma, i, 255);
  962. GLint g = gamma_adjust(v->GreenGamma, i, 255);
  963. GLint b = gamma_adjust(v->BlueGamma, i, 255);
  964. v->RtoPixel[i] = (r >> (8-rBits)) << v->rshift;
  965. v->GtoPixel[i] = (g >> (8-gBits)) << v->gshift;
  966. v->BtoPixel[i] = (b >> (8-bBits)) << v->bshift;
  967. }
  968. /* overflow protection */
  969. for (i=256;i<512;i++) {
  970. v->RtoPixel[i] = v->RtoPixel[255];
  971. v->GtoPixel[i] = v->GtoPixel[255];
  972. v->BtoPixel[i] = v->BtoPixel[255];
  973. }
  974. /* setup dithering kernel */
  975. minBits = rBits;
  976. if (gBits < minBits) minBits = gBits;
  977. if (bBits < minBits) minBits = bBits;
  978. for (i=0;i<16;i++) {
  979. v->Kernel[i] = kernel[i] >> minBits;
  980. }
  981. v->undithered_pf = PF_TRUECOLOR;
  982. v->dithered_pf = (GET_VISUAL_DEPTH(v)<24) ? PF_TRUEDITHER : PF_TRUECOLOR;
  983. }
  984. /*
  985. * Now check for TrueColor visuals which we can optimize.
  986. */
  987. if ( GET_REDMASK(v) ==0x0000ff
  988. && GET_GREENMASK(v)==0x00ff00
  989. && GET_BLUEMASK(v) ==0xff0000
  990. && CHECK_BYTE_ORDER(v)
  991. && v->BitsPerPixel==32
  992. && sizeof(GLuint)==4
  993. && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) {
  994. /* common 32 bpp config used on SGI, Sun */
  995. v->undithered_pf = v->dithered_pf = PF_8A8B8G8R;
  996. }
  997. else if (GET_REDMASK(v) ==0xff0000
  998. && GET_GREENMASK(v)==0x00ff00
  999. && GET_BLUEMASK(v) ==0x0000ff
  1000. && CHECK_BYTE_ORDER(v)
  1001. && v->BitsPerPixel==32
  1002. && sizeof(GLuint)==4
  1003. && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) {
  1004. /* common 32 bpp config used on Linux, HP, IBM */
  1005. v->undithered_pf = v->dithered_pf = PF_8R8G8B;
  1006. }
  1007. else if (GET_REDMASK(v) ==0xff0000
  1008. && GET_GREENMASK(v)==0x00ff00
  1009. && GET_BLUEMASK(v) ==0x0000ff
  1010. && CHECK_BYTE_ORDER(v)
  1011. && v->BitsPerPixel==24
  1012. && sizeof(GLuint)==4
  1013. && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) {
  1014. /* common packed 24 bpp config used on Linux */
  1015. v->undithered_pf = v->dithered_pf = PF_8R8G8B24;
  1016. }
  1017. else if (GET_REDMASK(v) ==0xf800
  1018. && GET_GREENMASK(v)==0x07e0
  1019. && GET_BLUEMASK(v) ==0x001f
  1020. && CHECK_BYTE_ORDER(v)
  1021. && v->BitsPerPixel==16
  1022. && sizeof(GLushort)==2
  1023. && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) {
  1024. /* 5-6-5 color weight on common PC VGA boards */
  1025. v->undithered_pf = PF_5R6G5B;
  1026. v->dithered_pf = PF_DITHER_5R6G5B;
  1027. }
  1028. else if (GET_REDMASK(v) ==0xe0
  1029. && GET_GREENMASK(v)==0x1c
  1030. && GET_BLUEMASK(v) ==0x03
  1031. && CHECK_FOR_HPCR(v)) {
  1032. setup_8bit_hpcr( v );
  1033. }
  1034. }
  1035. /*
  1036. * Setup RGB rendering for a window with a monochrome visual.
  1037. */
  1038. static void setup_monochrome( XMesaVisual v, XMesaBuffer b )
  1039. {
  1040. (void) b;
  1041. v->dithered_pf = v->undithered_pf = PF_1BIT;
  1042. /* if black=1 then we must flip pixel values */
  1043. v->bitFlip = (GET_BLACK_PIXEL(v) != 0);
  1044. }
  1045. /*
  1046. * When a context is "made current" for the first time, we can finally
  1047. * finish initializing the context's visual and buffer information.
  1048. * Input: v - the XMesaVisual to initialize
  1049. * b - the XMesaBuffer to initialize (may be NULL)
  1050. * rgb_flag - TRUE = RGBA mode, FALSE = color index mode
  1051. * window - the window/pixmap we're rendering into
  1052. * cmap - the colormap associated with the window/pixmap
  1053. * Return: GL_TRUE=success, GL_FALSE=failure
  1054. */
  1055. static GLboolean initialize_visual_and_buffer( int client,
  1056. XMesaVisual v,
  1057. XMesaBuffer b,
  1058. GLboolean rgb_flag,
  1059. XMesaDrawable window,
  1060. XMesaColormap cmap
  1061. )
  1062. {
  1063. #ifndef XFree86Server
  1064. XGCValues gcvalues;
  1065. #endif
  1066. if (b) {
  1067. assert(b->xm_visual == v);
  1068. }
  1069. /* Save true bits/pixel */
  1070. v->BitsPerPixel = GET_BITS_PER_PIXEL(v);
  1071. assert(v->BitsPerPixel > 0);
  1072. if (rgb_flag==GL_FALSE) {
  1073. /* COLOR-INDEXED WINDOW:
  1074. * Even if the visual is TrueColor or DirectColor we treat it as
  1075. * being color indexed. This is weird but might be useful to someone.
  1076. */
  1077. v->dithered_pf = v->undithered_pf = PF_INDEX;
  1078. v->index_bits = GET_VISUAL_DEPTH(v);
  1079. }
  1080. else {
  1081. /* RGB WINDOW:
  1082. * We support RGB rendering into almost any kind of visual.
  1083. */
  1084. int xclass;
  1085. xclass = GET_VISUAL_CLASS(v);
  1086. if (xclass==TrueColor || xclass==DirectColor) {
  1087. setup_truecolor( v, b, (XMesaWindow)window, cmap );
  1088. }
  1089. else if (xclass==StaticGray && GET_VISUAL_DEPTH(v)==1) {
  1090. setup_monochrome( v, b );
  1091. }
  1092. else if (xclass==GrayScale || xclass==StaticGray) {
  1093. if (!setup_grayscale( client, v, b, cmap )) {
  1094. return GL_FALSE;
  1095. }
  1096. }
  1097. else if ((xclass==PseudoColor || xclass==StaticColor)
  1098. && GET_VISUAL_DEPTH(v)>=4 && GET_VISUAL_DEPTH(v)<=16) {
  1099. if (!setup_dithered_color( client, v, b, cmap )) {
  1100. return GL_FALSE;
  1101. }
  1102. }
  1103. else {
  1104. error("XMesa: RGB mode rendering not supported in given visual.");
  1105. return GL_FALSE;
  1106. }
  1107. v->index_bits = 0;
  1108. if (getenv("MESA_NO_DITHER")) {
  1109. v->dithered_pf = v->undithered_pf;
  1110. }
  1111. }
  1112. /*
  1113. * If MESA_INFO env var is set print out some debugging info
  1114. * which can help Brian figure out what's going on when a user
  1115. * reports bugs.
  1116. */
  1117. if (getenv("MESA_INFO")) {
  1118. fprintf(stderr, "X/Mesa visual = %p\n", v);
  1119. fprintf(stderr, "X/Mesa dithered pf = %u\n", v->dithered_pf);
  1120. fprintf(stderr, "X/Mesa undithered pf = %u\n", v->undithered_pf);
  1121. fprintf(stderr, "X/Mesa level = %d\n", v->level);
  1122. fprintf(stderr, "X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v));
  1123. fprintf(stderr, "X/Mesa bits per pixel = %d\n", v->BitsPerPixel);
  1124. }
  1125. if (b && window) {
  1126. /* Do window-specific initializations */
  1127. /* Window dimensions */
  1128. unsigned int w, h;
  1129. get_drawable_size( v->display, window, &w, &h );
  1130. b->width = w;
  1131. b->height = h;
  1132. b->frontbuffer = window;
  1133. assert( v->gl_visual );
  1134. /* Setup for single/double buffering */
  1135. if (v->gl_visual->DBflag) {
  1136. /* Double buffered */
  1137. #ifndef XFree86Server
  1138. b->shm = check_for_xshm( v->display );
  1139. #endif
  1140. xmesa_alloc_back_buffer( b );
  1141. if (b->db_state==BACK_PIXMAP) {
  1142. b->buffer = (XMesaDrawable)b->backpixmap;
  1143. }
  1144. else {
  1145. b->buffer = XIMAGE;
  1146. }
  1147. }
  1148. else {
  1149. /* Single Buffered */
  1150. b->buffer = b->frontbuffer;
  1151. }
  1152. /* X11 graphics contexts */
  1153. #ifdef XFree86Server
  1154. b->gc1 = CreateScratchGC(v->display, window->depth);
  1155. b->gc2 = CreateScratchGC(v->display, window->depth);
  1156. #else
  1157. b->gc1 = XCreateGC( v->display, window, 0, NULL );
  1158. b->gc2 = XCreateGC( v->display, window, 0, NULL );
  1159. #endif
  1160. XMesaSetFunction( v->display, b->gc1, GXcopy );
  1161. XMesaSetFunction( v->display, b->gc2, GXcopy );
  1162. /*
  1163. * Don't generate Graphics Expose/NoExpose events in swapbuffers().
  1164. * Patch contributed by Michael Pichler May 15, 1995.
  1165. */
  1166. #ifdef XFree86Server
  1167. b->cleargc = CreateScratchGC(v->display, window->depth);
  1168. {
  1169. CARD32 v[1];
  1170. v[0] = FALSE;
  1171. dixChangeGC(NullClient, b->cleargc, GCGraphicsExposures, v, NULL);
  1172. }
  1173. #else
  1174. gcvalues.graphics_exposures = False;
  1175. b->cleargc = XCreateGC( v->display, window,
  1176. GCGraphicsExposures, &gcvalues);
  1177. #endif
  1178. XMesaSetFunction( v->display, b->cleargc, GXcopy );
  1179. /*
  1180. * Set fill style and tile pixmap once for all for HPCR stuff
  1181. * (instead of doing it each time in clear_color_HPCR_pixmap())
  1182. * Initialize whole stuff
  1183. * Patch contributed by Jacques Leroy March 8, 1998.
  1184. */
  1185. if (v->hpcr_clear_flag && b->buffer!=XIMAGE) {
  1186. int i;
  1187. for (i=0; i<16; i++)
  1188. {
  1189. XMesaPutPixel(v->hpcr_clear_ximage, i, 0, 0);
  1190. XMesaPutPixel(v->hpcr_clear_ximage, i, 1, 0);
  1191. }
  1192. XMesaPutImage(b->display, (XMesaDrawable)v->hpcr_clear_pixmap,
  1193. b->cleargc, v->hpcr_clear_ximage, 0, 0, 0, 0, 16, 2);
  1194. XMesaSetFillStyle( v->display, b->cleargc, FillTiled);
  1195. XMesaSetTile( v->display, b->cleargc, v->hpcr_clear_pixmap );
  1196. }
  1197. /* Initialize the row buffer XImage for use in write_color_span() */
  1198. #ifdef XFree86Server
  1199. b->rowimage = XMesaCreateImage(GET_VISUAL_DEPTH(v), MAX_WIDTH, 1,
  1200. (char *)MALLOC(MAX_WIDTH*4));
  1201. #else
  1202. b->rowimage = XCreateImage( v->display,
  1203. v->visinfo->visual,
  1204. v->visinfo->depth,
  1205. ZPixmap, 0, /*format, offset*/
  1206. (char*) MALLOC(MAX_WIDTH*4), /*data*/
  1207. MAX_WIDTH, 1, /*width, height*/
  1208. 32, /*bitmap_pad*/
  1209. 0 /*bytes_per_line*/ );
  1210. #endif
  1211. }
  1212. return GL_TRUE;
  1213. }
  1214. /*
  1215. * Convert an RGBA color to a pixel value.
  1216. */
  1217. unsigned long
  1218. xmesa_color_to_pixel( XMesaContext xmesa, GLubyte r, GLubyte g, GLubyte b, GLubyte a,
  1219. GLuint pixelFormat)
  1220. {
  1221. switch (pixelFormat) {
  1222. case PF_INDEX:
  1223. return 0;
  1224. case PF_TRUECOLOR:
  1225. {
  1226. unsigned long p;
  1227. PACK_TRUECOLOR( p, r, g, b );
  1228. return p;
  1229. }
  1230. case PF_8A8B8G8R:
  1231. return PACK_8A8B8G8R( r, g, b, a );
  1232. case PF_8R8G8B:
  1233. /* fall through */
  1234. case PF_8R8G8B24:
  1235. return PACK_8R8G8B( r, g, b );
  1236. case PF_5R6G5B:
  1237. return PACK_5R6G5B( r, g, b );
  1238. case PF_DITHER:
  1239. {
  1240. DITHER_SETUP;
  1241. return DITHER( 1, 0, r, g, b );
  1242. }
  1243. case PF_1BIT:
  1244. /* 382 = (3*255)/2 */
  1245. return ((r+g+b) > 382) ^ xmesa->xm_visual->bitFlip;
  1246. case PF_HPCR:
  1247. return DITHER_HPCR(1, 1, r, g, b);
  1248. case PF_LOOKUP:
  1249. {
  1250. LOOKUP_SETUP;
  1251. return LOOKUP( r, g, b );
  1252. }
  1253. case PF_GRAYSCALE:
  1254. return GRAY_RGB( r, g, b );
  1255. case PF_TRUEDITHER:
  1256. /* fall through */
  1257. case PF_DITHER_5R6G5B:
  1258. {
  1259. unsigned long p;
  1260. PACK_TRUEDITHER(p, 1, 0, r, g, b);
  1261. return p;
  1262. }
  1263. default:
  1264. gl_problem(NULL, "Bad pixel format in xmesa_color_to_pixel");
  1265. }
  1266. return 0;
  1267. }
  1268. /**********************************************************************/
  1269. /***** Public Functions *****/
  1270. /**********************************************************************/
  1271. /*
  1272. * Create a new X/Mesa visual.
  1273. * Input: display - X11 display
  1274. * visinfo - an XVisualInfo pointer
  1275. * rgb_flag - GL_TRUE = RGB mode,
  1276. * GL_FALSE = color index mode
  1277. * alpha_flag - alpha buffer requested?
  1278. * db_flag - GL_TRUE = double-buffered,
  1279. * GL_FALSE = single buffered
  1280. * stereo_flag - stereo visual?
  1281. * ximage_flag - GL_TRUE = use an XImage for back buffer,
  1282. * GL_FALSE = use an off-screen pixmap for back buffer
  1283. * depth_size - requested bits/depth values, or zero
  1284. * stencil_size - requested bits/stencil values, or zero
  1285. * accum_red_size - requested bits/red accum values, or zero
  1286. * accum_green_size - requested bits/green accum values, or zero
  1287. * accum_blue_size - requested bits/blue accum values, or zero
  1288. * accum_alpha_size - requested bits/alpha accum values, or zero
  1289. * num_samples - number of samples/pixel if multisampling, or zero
  1290. * level - visual level, usually 0
  1291. * visualCaveat - ala the GLX extension, usually GLX_NONE_EXT
  1292. * Return; a new XMesaVisual or 0 if error.
  1293. */
  1294. XMesaVisual XMesaCreateVisual( XMesaDisplay *display,
  1295. XMesaVisualInfo visinfo,
  1296. GLboolean rgb_flag,
  1297. GLboolean alpha_flag,
  1298. GLboolean db_flag,
  1299. GLboolean stereo_flag,
  1300. GLboolean ximage_flag,
  1301. GLint depth_size,
  1302. GLint stencil_size,
  1303. GLint accum_red_size,
  1304. GLint accum_green_size,
  1305. GLint accum_blue_size,
  1306. GLint accum_alpha_size,
  1307. GLint num_samples,
  1308. GLint level,
  1309. GLint visualCaveat )
  1310. {
  1311. char *gamma;
  1312. XMesaVisual v;
  1313. GLint red_bits, green_bits, blue_bits, alpha_bits;
  1314. /* For debugging only */
  1315. if (getenv("MESA_XSYNC")) {
  1316. /* This makes debugging X easier.
  1317. * In your debugger, set a breakpoint on _XError to stop when an
  1318. * X protocol error is generated.
  1319. */
  1320. #ifdef XFree86Server
  1321. /* NOT_NEEDED */
  1322. #else
  1323. XSynchronize( display, 1 );
  1324. #endif
  1325. }
  1326. v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual);
  1327. if (!v) {
  1328. return NULL;
  1329. }
  1330. /*
  1331. * In the X server, NULL is passed in for the display. It will have
  1332. * to be set before using this visual. See XMesaSetVisualDisplay()
  1333. * below.
  1334. */
  1335. v->display = display;
  1336. /* Save a copy of the XVisualInfo struct because the user may XFREE()
  1337. * the struct but we may need some of the information contained in it
  1338. * at a later time.
  1339. */
  1340. #ifdef XFree86Server
  1341. v->visinfo = visinfo;
  1342. #else
  1343. v->visinfo = (XVisualInfo *) MALLOC(sizeof(*visinfo));
  1344. if(!v->visinfo) {
  1345. FREE(v);
  1346. return NULL;
  1347. }
  1348. MEMCPY(v->visinfo, visinfo, sizeof(*visinfo));
  1349. /* Save a copy of the pointer now so we can find this visual again
  1350. * if we need to search for it in find_glx_visual().
  1351. */
  1352. v->vishandle = visinfo;
  1353. #endif
  1354. #ifdef XFree86Server
  1355. /* Initialize the depth of the screen */
  1356. {
  1357. PixmapFormatRec *format;
  1358. for (format = screenInfo.formats;
  1359. format->depth != display->rootDepth;
  1360. format++)
  1361. ;
  1362. v->screen_depth = format->bitsPerPixel;
  1363. }
  1364. #endif
  1365. /* check for MESA_GAMMA environment variable */
  1366. gamma = getenv("MESA_GAMMA");
  1367. if (gamma) {
  1368. v->RedGamma = v->GreenGamma = v->BlueGamma = 0.0;
  1369. sscanf( gamma, "%f %f %f", &v->RedGamma, &v->GreenGamma, &v->BlueGamma );
  1370. if (v->RedGamma<=0.0) v->RedGamma = 1.0;
  1371. if (v->GreenGamma<=0.0) v->GreenGamma = v->RedGamma;
  1372. if (v->BlueGamma<=0.0) v->BlueGamma = v->RedGamma;
  1373. }
  1374. else {
  1375. v->RedGamma = v->GreenGamma = v->BlueGamma = 1.0;
  1376. }
  1377. v->ximage_flag = ximage_flag;
  1378. v->level = level;
  1379. v->VisualCaveat = visualCaveat;
  1380. (void) initialize_visual_and_buffer( 0, v, NULL, rgb_flag, 0, 0 );
  1381. {
  1382. int xclass;
  1383. xclass = GET_VISUAL_CLASS(v);
  1384. if (xclass==TrueColor || xclass==DirectColor) {
  1385. red_bits = bitcount(GET_REDMASK(v));
  1386. green_bits = bitcount(GET_GREENMASK(v));
  1387. blue_bits = bitcount(GET_BLUEMASK(v));
  1388. alpha_bits = 0;
  1389. }
  1390. else {
  1391. /* this is an approximation */
  1392. int depth;
  1393. depth = GET_VISUAL_DEPTH(v);
  1394. red_bits = depth / 3;
  1395. depth -= red_bits;
  1396. green_bits = depth / 2;
  1397. depth -= green_bits;
  1398. blue_bits = depth;
  1399. alpha_bits = 0;
  1400. assert( red_bits + green_bits + blue_bits == GET_VISUAL_DEPTH(v) );
  1401. }
  1402. }
  1403. if (alpha_flag && alpha_bits == 0)
  1404. alpha_bits = 8;
  1405. v->gl_visual = _mesa_create_visual( rgb_flag, db_flag, stereo_flag,
  1406. red_bits, green_bits,
  1407. blue_bits, alpha_bits,
  1408. v->index_bits,
  1409. depth_size,
  1410. stencil_size,
  1411. accum_red_size, accum_green_size,
  1412. accum_blue_size, accum_alpha_size,
  1413. 0 );
  1414. if (!v->gl_visual) {
  1415. #ifndef XFree86Server
  1416. FREE(v->visinfo);
  1417. #endif
  1418. FREE(v);
  1419. return NULL;
  1420. }
  1421. return v;
  1422. }
  1423. void XMesaSetVisualDisplay( XMesaDisplay *dpy, XMesaVisual v )
  1424. {
  1425. v->display = dpy;
  1426. }
  1427. void XMesaDestroyVisual( XMesaVisual v )
  1428. {
  1429. _mesa_destroy_visual( v->gl_visual );
  1430. #ifndef XFree86Server
  1431. FREE(v->visinfo);
  1432. #endif
  1433. FREE(v);
  1434. }
  1435. /*
  1436. * Create a new XMesaContext.
  1437. * Input: v - XMesaVisual
  1438. * share_list - another XMesaContext with which to share display
  1439. * lists or NULL if no sharing is wanted.
  1440. * Return: an XMesaContext or NULL if error.
  1441. */
  1442. XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list )
  1443. {
  1444. XMesaContext c;
  1445. GLboolean direct = GL_TRUE; /* XXXX */
  1446. /* NOT_DONE: should this be GL_FALSE??? */
  1447. static GLboolean firstTime = GL_TRUE;
  1448. if (firstTime) {
  1449. _glthread_INIT_MUTEX(_xmesa_lock);
  1450. firstTime = GL_FALSE;
  1451. }
  1452. c = (XMesaContext) CALLOC_STRUCT(xmesa_context);
  1453. if (!c) {
  1454. return NULL;
  1455. }
  1456. c->gl_ctx = _mesa_create_context( v->gl_visual,
  1457. share_list ? share_list->gl_ctx : (GLcontext *) NULL,
  1458. (void *) c, direct );
  1459. if (!c->gl_ctx) {
  1460. FREE(c);
  1461. return NULL;
  1462. }
  1463. gl_extensions_enable(c->gl_ctx, "GL_HP_occlusion_test");
  1464. gl_extensions_enable(c->gl_ctx, "GL_ARB_texture_cube_map");
  1465. gl_extensions_enable(c->gl_ctx, "GL_EXT_texture_env_combine");
  1466. if (CHECK_BYTE_ORDER(v)) {
  1467. c->swapbytes = GL_FALSE;
  1468. }
  1469. else {
  1470. c->swapbytes = GL_TRUE;
  1471. }
  1472. c->xm_visual = v;
  1473. c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */
  1474. c->display = v->display;
  1475. c->pixelformat = v->dithered_pf; /* Dithering is enabled by default */
  1476. c->gl_ctx->Driver.UpdateState = xmesa_update_state;
  1477. #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
  1478. c->driContextPriv = driContextPriv;
  1479. #endif
  1480. /* Run the config file */
  1481. _mesa_context_initialize( c->gl_ctx );
  1482. return c;
  1483. }
  1484. void XMesaDestroyContext( XMesaContext c )
  1485. {
  1486. #ifdef FX
  1487. if (c->xm_buffer && c->xm_buffer->FXctx)
  1488. fxMesaDestroyContext(c->xm_buffer->FXctx);
  1489. #endif
  1490. if (c->gl_ctx)
  1491. _mesa_destroy_context( c->gl_ctx );
  1492. /* Disassociate old buffer with this context */
  1493. if (c->xm_buffer)
  1494. c->xm_buffer->xm_context = NULL;
  1495. /* Destroy any buffers which are using this context. If we don't
  1496. * we may have dangling references. Hmm, maybe we should just
  1497. * set the buffer's context pointer to NULL instead of deleting it?
  1498. * Let's see if we get any bug reports...
  1499. * This contributed by Doug Rabson <dfr@calcaphon.com>
  1500. */
  1501. {
  1502. XMesaBuffer b, next;
  1503. for (b = XMesaBufferList; b; b = next) {
  1504. next = b->Next;
  1505. if (!b->pixmap_flag) {
  1506. #ifndef XFree86Server
  1507. XSync(b->display, False);
  1508. #endif
  1509. if (b->xm_context == c) {
  1510. /* found a context created for this context */
  1511. XMesaDestroyBuffer( b );
  1512. }
  1513. }
  1514. }
  1515. }
  1516. FREE( c );
  1517. }
  1518. /*
  1519. * XXX this isn't a public function! It's a hack for the 3Dfx driver.
  1520. * Create a new XMesaBuffer from an X window.
  1521. * Input: v - the XMesaVisual
  1522. * w - the window
  1523. * c - the context
  1524. * Return: new XMesaBuffer or NULL if error
  1525. */
  1526. XMesaBuffer XMesaCreateWindowBuffer2( XMesaVisual v, XMesaWindow w,
  1527. XMesaContext c )
  1528. {
  1529. #ifndef XFree86Server
  1530. XWindowAttributes attr;
  1531. #endif
  1532. #ifdef FX
  1533. char *fxEnvVar;
  1534. #endif
  1535. int client = 0;
  1536. XMesaBuffer b = alloc_xmesa_buffer();
  1537. if (!b) {
  1538. return NULL;
  1539. }
  1540. (void) c;
  1541. #ifdef XFree86Server
  1542. client = CLIENT_ID(((XMesaDrawable)w)->id);
  1543. #endif
  1544. assert(v);
  1545. #ifdef XFree86Server
  1546. if (GET_VISUAL_DEPTH(v) != ((XMesaDrawable)w)->depth) {
  1547. #else
  1548. XGetWindowAttributes( v->display, w, &attr );
  1549. if (GET_VISUAL_DEPTH(v) != attr.depth) {
  1550. #endif
  1551. if (getenv("MESA_DEBUG")) {
  1552. fprintf(stderr, "XMesaCreateWindowBuffer: depth mismatch between visual and window!\n");
  1553. }
  1554. return NULL;
  1555. }
  1556. b->xm_context = NULL; /* Associate no context with this buffer */
  1557. b->xm_visual = v;
  1558. b->pixmap_flag = GL_FALSE;
  1559. b->display = v->display;
  1560. #ifdef XFree86Server
  1561. b->cmap = (ColormapPtr)LookupIDByType(wColormap(w), RT_COLORMAP);
  1562. #else
  1563. if (attr.colormap) {
  1564. b->cmap = attr.colormap;
  1565. }
  1566. else {
  1567. if (getenv("MESA_DEBUG")) {
  1568. fprintf(stderr, "Window %u has no colormap!\n", (unsigned int) w);
  1569. }
  1570. /* this is weird, a window w/out a colormap!? */
  1571. /* OK, let's just allocate a new one and hope for the best */
  1572. b->cmap = XCreateColormap(v->display, w, attr.visual, AllocNone);
  1573. }
  1574. #endif
  1575. /* determine back buffer implementation */
  1576. if (v->gl_visual->DBflag) {
  1577. if (v->ximage_flag) {
  1578. b->db_state = BACK_XIMAGE;
  1579. }
  1580. else {
  1581. b->db_state = BACK_PIXMAP;
  1582. }
  1583. }
  1584. else {
  1585. b->db_state = 0;
  1586. }
  1587. b->gl_buffer = _mesa_create_framebuffer( v->gl_visual,
  1588. v->gl_visual->DepthBits > 0,
  1589. v->gl_visual->StencilBits > 0,
  1590. v->gl_visual->AccumRedBits > 0,
  1591. v->gl_visual->AlphaBits > 0 );
  1592. if (!b->gl_buffer) {
  1593. free_xmesa_buffer(client, b);
  1594. return NULL;
  1595. }
  1596. if (!initialize_visual_and_buffer( client, v, b, v->gl_visual->RGBAflag,
  1597. (XMesaDrawable)w, b->cmap )) {
  1598. _mesa_destroy_framebuffer( b->gl_buffer );
  1599. free_xmesa_buffer(client, b);
  1600. return NULL;
  1601. }
  1602. #ifdef FX
  1603. fxEnvVar = getenv("MESA_GLX_FX");
  1604. if (fxEnvVar) {
  1605. if (fxEnvVar[0]!='d') {
  1606. int attribs[100];
  1607. int numAttribs = 0;
  1608. int hw;
  1609. if (v->gl_visual->DepthBits > 0) {
  1610. attribs[numAttribs++] = FXMESA_DEPTH_SIZE;
  1611. attribs[numAttribs++] = 1;
  1612. }
  1613. if (v->gl_visual->DBflag) {
  1614. attribs[numAttribs++] = FXMESA_DOUBLEBUFFER;
  1615. }
  1616. if (v->gl_visual->AccumRedBits > 0) {
  1617. attribs[numAttribs++] = FXMESA_ACCUM_SIZE;
  1618. attribs[numAttribs++] = v->gl_visual->AccumRedBits;
  1619. }
  1620. if (v->gl_visual->StencilBits > 0) {
  1621. attribs[numAttribs++] = FXMESA_STENCIL_SIZE;
  1622. attribs[numAttribs++] = v->gl_visual->StencilBits;
  1623. }
  1624. if (v->gl_visual->AlphaBits > 0) {
  1625. attribs[numAttribs++] = FXMESA_ALPHA_SIZE;
  1626. attribs[numAttribs++] = 1;
  1627. }
  1628. if (c->gl_ctx) {
  1629. #define FXMESA_SHARE_CONTEXT 990099 /* keep in sync with fxapi.c! */
  1630. attribs[numAttribs++] = FXMESA_SHARE_CONTEXT;
  1631. attribs[numAttribs++] = (int) c->gl_ctx;
  1632. }
  1633. attribs[numAttribs++] = FXMESA_NONE;
  1634. if ((hw = fxQueryHardware())==GR_SSTTYPE_VOODOO) {
  1635. b->FXctx = fxMesaCreateBestContext(0, b->width, b->height, attribs);
  1636. if ((v->undithered_pf!=PF_INDEX) && (b->backimage)) {
  1637. b->FXisHackUsable = b->FXctx ? GL_TRUE : GL_FALSE;
  1638. if (fxEnvVar[0]=='w' || fxEnvVar[0]=='W')
  1639. b->FXwindowHack = b->FXctx ? GL_TRUE : GL_FALSE;
  1640. else
  1641. b->FXwindowHack = GL_FALSE;
  1642. }
  1643. }
  1644. else {
  1645. if (fxEnvVar[0]=='w' || fxEnvVar[0]=='W')
  1646. b->FXctx = fxMesaCreateContext(w, GR_RESOLUTION_NONE,
  1647. GR_REFRESH_75Hz, attribs);
  1648. else
  1649. b->FXctx = fxMesaCreateBestContext(0, b->width, b->height, attribs);
  1650. b->FXisHackUsable = GL_FALSE;
  1651. b->FXwindowHack = GL_FALSE;
  1652. }
  1653. /*
  1654. fprintf(stderr,
  1655. "voodoo %d, wid %d height %d hack: usable %d active %d\n",
  1656. hw, b->width, b->height, b->FXisHackUsable, b->FXwindowHack);
  1657. */
  1658. }
  1659. }
  1660. else {
  1661. fprintf(stderr,"WARNING: This Mesa Library includes the Glide driver but\n");
  1662. fprintf(stderr," you have not defined the MESA_GLX_FX env. var.\n");
  1663. fprintf(stderr," (check the README.3DFX file for more information).\n\n");
  1664. fprintf(stderr," you can disable this message with a 'export MESA_GLX_FX=disable'.\n");
  1665. }
  1666. #endif
  1667. #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
  1668. b->driDrawPriv = driDrawPriv;
  1669. #endif
  1670. return b;
  1671. }
  1672. XMesaBuffer XMesaCreateWindowBuffer( XMesaVisual v, XMesaWindow w )
  1673. {
  1674. return XMesaCreateWindowBuffer2( v, w, NULL );
  1675. }
  1676. /*
  1677. * Create a new XMesaBuffer from an X pixmap.
  1678. * Input: v - the XMesaVisual
  1679. * p - the pixmap
  1680. * cmap - the colormap, may be 0 if using a TrueColor or DirectColor
  1681. * visual for the pixmap
  1682. * Return: new XMesaBuffer or NULL if error
  1683. */
  1684. XMesaBuffer XMesaCreatePixmapBuffer( XMesaVisual v,
  1685. XMesaPixmap p, XMesaColormap cmap )
  1686. {
  1687. int client = 0;
  1688. XMesaBuffer b = alloc_xmesa_buffer();
  1689. if (!b) {
  1690. return NULL;
  1691. }
  1692. #ifdef XFree86Server
  1693. client = CLIENT_ID(((XMesaDrawable)p)->id);
  1694. #endif
  1695. assert(v);
  1696. b->xm_context = NULL; /* Associate no context with this buffer */
  1697. b->xm_visual = v;
  1698. b->pixmap_flag = GL_TRUE;
  1699. b->display = v->display;
  1700. b->cmap = cmap;
  1701. /* determine back buffer implementation */
  1702. if (v->gl_visual->DBflag) {
  1703. if (v->ximage_flag) {
  1704. b->db_state = BACK_XIMAGE;
  1705. }
  1706. else {
  1707. b->db_state = BACK_PIXMAP;
  1708. }
  1709. }
  1710. else {
  1711. b->db_state = 0;
  1712. }
  1713. b->gl_buffer = _mesa_create_framebuffer( v->gl_visual,
  1714. v->gl_visual->DepthBits > 0,
  1715. v->gl_visual->StencilBits > 0,
  1716. v->gl_visual->AccumRedBits +
  1717. v->gl_visual->AccumGreenBits +
  1718. v->gl_visual->AccumBlueBits > 0,
  1719. v->gl_visual->AlphaBits > 0 );
  1720. if (!b->gl_buffer) {
  1721. free_xmesa_buffer(client, b);
  1722. return NULL;
  1723. }
  1724. if (!initialize_visual_and_buffer(client, v, b, v->gl_visual->RGBAflag,
  1725. (XMesaDrawable)p, cmap)) {
  1726. _mesa_destroy_framebuffer( b->gl_buffer );
  1727. free_xmesa_buffer(client, b);
  1728. return NULL;
  1729. }
  1730. #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
  1731. b->driDrawPriv = driDrawPriv;
  1732. #endif
  1733. return b;
  1734. }
  1735. /*
  1736. * Deallocate an XMesaBuffer structure and all related info.
  1737. */
  1738. void XMesaDestroyBuffer( XMesaBuffer b )
  1739. {
  1740. int client = 0;
  1741. #ifdef XFree86Server
  1742. if (b->frontbuffer)
  1743. client = CLIENT_ID(b->frontbuffer->id);
  1744. #endif
  1745. if (b->gc1) XMesaFreeGC( b->xm_visual->display, b->gc1 );
  1746. if (b->gc2) XMesaFreeGC( b->xm_visual->display, b->gc2 );
  1747. if (b->cleargc) XMesaFreeGC( b->xm_visual->display, b->cleargc );
  1748. if (b->backimage) {
  1749. #if defined(USE_XSHM) && !defined(XFree86Server)
  1750. if (b->shm) {
  1751. XShmDetach( b->xm_visual->display, &b->shminfo );
  1752. XDestroyImage( b->backimage );
  1753. shmdt( b->shminfo.shmaddr );
  1754. }
  1755. else
  1756. #endif
  1757. XMesaDestroyImage( b->backimage );
  1758. }
  1759. if (b->backpixmap) {
  1760. XMesaFreePixmap( b->xm_visual->display, b->backpixmap );
  1761. if (b->xm_visual->hpcr_clear_flag) {
  1762. XMesaFreePixmap( b->xm_visual->display,
  1763. b->xm_visual->hpcr_clear_pixmap );
  1764. XMesaDestroyImage( b->xm_visual->hpcr_clear_ximage );
  1765. }
  1766. }
  1767. if (b->rowimage) {
  1768. FREE( b->rowimage->data );
  1769. b->rowimage->data = NULL;
  1770. XMesaDestroyImage( b->rowimage );
  1771. }
  1772. if (b->xm_context)
  1773. b->xm_context->xm_buffer = NULL;
  1774. _mesa_destroy_framebuffer( b->gl_buffer );
  1775. free_xmesa_buffer(client, b);
  1776. }
  1777. /*
  1778. * Bind buffer b to context c and make c the current rendering context.
  1779. */
  1780. GLboolean XMesaMakeCurrent( XMesaContext c, XMesaBuffer b )
  1781. {
  1782. return XMesaMakeCurrent2( c, b, b );
  1783. }
  1784. /*
  1785. * Bind buffer b to context c and make c the current rendering context.
  1786. */
  1787. GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer,
  1788. XMesaBuffer readBuffer )
  1789. {
  1790. if (c) {
  1791. if (!drawBuffer || !readBuffer)
  1792. return GL_FALSE; /* must specify buffers! */
  1793. #ifdef FX
  1794. if (drawBuffer->FXctx) {
  1795. fxMesaMakeCurrent(drawBuffer->FXctx);
  1796. /* Disassociate old buffer from this context */
  1797. if (c->xm_buffer)
  1798. c->xm_buffer->xm_context = NULL;
  1799. /* Associate the context with this buffer */
  1800. drawBuffer->xm_context = c;
  1801. c->xm_buffer = drawBuffer;
  1802. c->xm_read_buffer = readBuffer;
  1803. c->use_read_buffer = (drawBuffer != readBuffer);
  1804. return GL_TRUE;
  1805. }
  1806. #endif
  1807. if (c->gl_ctx == _mesa_get_current_context()
  1808. && c->xm_buffer == drawBuffer
  1809. && c->xm_read_buffer == readBuffer
  1810. && c->xm_buffer->wasCurrent) {
  1811. /* same context and buffer, do nothing */
  1812. return GL_TRUE;
  1813. }
  1814. /* Disassociate old buffer with this context */
  1815. if (c->xm_buffer)
  1816. c->xm_buffer->xm_context = NULL;
  1817. drawBuffer->xm_context = c; /* Associate the context with this buffer */
  1818. c->xm_buffer = drawBuffer;
  1819. c->xm_read_buffer = readBuffer;
  1820. c->use_read_buffer = (drawBuffer != readBuffer);
  1821. _mesa_make_current2(c->gl_ctx, drawBuffer->gl_buffer, readBuffer->gl_buffer);
  1822. if (c->gl_ctx->Viewport.Width == 0) {
  1823. /* initialize viewport to window size */
  1824. _mesa_Viewport( 0, 0, drawBuffer->width, drawBuffer->height );
  1825. c->gl_ctx->Scissor.Width = drawBuffer->width;
  1826. c->gl_ctx->Scissor.Height = drawBuffer->height;
  1827. }
  1828. if (c->xm_visual->gl_visual->RGBAflag) {
  1829. /*
  1830. * Must recompute and set these pixel values because colormap
  1831. * can be different for different windows.
  1832. */
  1833. c->pixel = xmesa_color_to_pixel( c, c->red, c->green,
  1834. c->blue, c->alpha, c->pixelformat );
  1835. XMesaSetForeground( c->display, c->xm_buffer->gc1, c->pixel );
  1836. c->clearpixel = xmesa_color_to_pixel( c,
  1837. c->clearcolor[0],
  1838. c->clearcolor[1],
  1839. c->clearcolor[2],
  1840. c->clearcolor[3],
  1841. c->xm_visual->undithered_pf);
  1842. XMesaSetForeground(c->display, c->xm_buffer->cleargc, c->clearpixel);
  1843. }
  1844. /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
  1845. c->xm_buffer->wasCurrent = GL_TRUE;
  1846. }
  1847. else {
  1848. /* Detach */
  1849. _mesa_make_current2( NULL, NULL, NULL );
  1850. }
  1851. return GL_TRUE;
  1852. }
  1853. /*
  1854. * Unbind the context c from its buffer.
  1855. */
  1856. GLboolean XMesaUnbindContext( XMesaContext c )
  1857. {
  1858. /* A no-op for XFree86 integration purposes */
  1859. return GL_TRUE;
  1860. }
  1861. XMesaContext XMesaGetCurrentContext( void )
  1862. {
  1863. GET_CURRENT_CONTEXT(ctx);
  1864. if (ctx) {
  1865. XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1866. return xmesa;
  1867. }
  1868. else {
  1869. return 0;
  1870. }
  1871. }
  1872. XMesaBuffer XMesaGetCurrentBuffer( void )
  1873. {
  1874. GET_CURRENT_CONTEXT(ctx);
  1875. if (ctx) {
  1876. XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1877. return xmesa->xm_buffer;
  1878. }
  1879. else {
  1880. return 0;
  1881. }
  1882. }
  1883. /* New in Mesa 3.1 */
  1884. XMesaBuffer XMesaGetCurrentReadBuffer( void )
  1885. {
  1886. GET_CURRENT_CONTEXT(ctx);
  1887. if (ctx) {
  1888. XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1889. return xmesa->xm_buffer;
  1890. }
  1891. else {
  1892. return 0;
  1893. }
  1894. }
  1895. GLboolean XMesaForceCurrent(XMesaContext c)
  1896. {
  1897. if (c) {
  1898. if (c->gl_ctx != _mesa_get_current_context()) {
  1899. _mesa_make_current(c->gl_ctx, c->xm_buffer->gl_buffer);
  1900. }
  1901. }
  1902. else {
  1903. _mesa_make_current(NULL, NULL);
  1904. }
  1905. return GL_TRUE;
  1906. }
  1907. GLboolean XMesaLoseCurrent(XMesaContext c)
  1908. {
  1909. (void) c;
  1910. _mesa_make_current(NULL, NULL);
  1911. return GL_TRUE;
  1912. }
  1913. /*
  1914. * Switch 3Dfx support hack between window and full-screen mode.
  1915. */
  1916. GLboolean XMesaSetFXmode( GLint mode )
  1917. {
  1918. #ifdef FX
  1919. const char *fx = getenv("MESA_GLX_FX");
  1920. if (fx && fx[0] != 'd') {
  1921. GET_CURRENT_CONTEXT(ctx);
  1922. GrHwConfiguration hw;
  1923. if (!FX_grSstQueryHardware(&hw)) {
  1924. /*fprintf(stderr, "!grSstQueryHardware\n");*/
  1925. return GL_FALSE;
  1926. }
  1927. if (hw.num_sst < 1) {
  1928. /*fprintf(stderr, "hw.num_sst < 1\n");*/
  1929. return GL_FALSE;
  1930. }
  1931. if (ctx) {
  1932. XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1933. if (mode == XMESA_FX_WINDOW) {
  1934. if (xmesa->xm_buffer->FXisHackUsable) {
  1935. FX_grSstControl(GR_CONTROL_DEACTIVATE);
  1936. xmesa->xm_buffer->FXwindowHack = GL_TRUE;
  1937. return GL_TRUE;
  1938. }
  1939. }
  1940. else if (mode == XMESA_FX_FULLSCREEN) {
  1941. FX_grSstControl(GR_CONTROL_ACTIVATE);
  1942. xmesa->xm_buffer->FXwindowHack = GL_FALSE;
  1943. return GL_TRUE;
  1944. }
  1945. else {
  1946. /* Error: Bad mode value */
  1947. }
  1948. }
  1949. }
  1950. /*fprintf(stderr, "fallthrough\n");*/
  1951. #else
  1952. (void) mode;
  1953. #endif
  1954. return GL_FALSE;
  1955. }
  1956. #ifdef FX
  1957. /*
  1958. * Read image from VooDoo frame buffer into X/Mesa's back XImage.
  1959. */
  1960. static void FXgetImage( XMesaBuffer b )
  1961. {
  1962. static unsigned short pixbuf[MAX_WIDTH];
  1963. int x, y;
  1964. XMesaWindow root;
  1965. unsigned int bw, depth, width, height;
  1966. XMesaContext xmesa = (XMesaContext) b->xm_context->gl_ctx->DriverCtx;
  1967. assert(xmesa->xm_buffer->FXctx);
  1968. #ifdef XFree86Server
  1969. x = b->frontbuffer->x;
  1970. y = b->frontbuffer->y;
  1971. width = b->frontbuffer->width;
  1972. height = b->frontbuffer->height;
  1973. depth = b->frontbuffer->depth;
  1974. #else
  1975. XGetGeometry( xmesa->xm_visual->display, b->frontbuffer,
  1976. &root, &x, &y, &width, &height, &bw, &depth);
  1977. #endif
  1978. if (b->width != width || b->height != height) {
  1979. b->width = MIN2((int)width, xmesa->xm_buffer->FXctx->width);
  1980. b->height = MIN2((int)height, xmesa->xm_buffer->FXctx->height);
  1981. if (b->width & 1)
  1982. b->width--; /* prevent odd width */
  1983. xmesa_alloc_back_buffer( b );
  1984. }
  1985. grLfbWriteColorFormat(GR_COLORFORMAT_ARGB);
  1986. if (xmesa->xm_visual->undithered_pf==PF_5R6G5B) {
  1987. /* Special case: 16bpp RGB */
  1988. grLfbReadRegion( GR_BUFFER_FRONTBUFFER, /* src buffer */
  1989. 0, xmesa->xm_buffer->FXctx->height - b->height, /*pos*/
  1990. b->width, b->height, /* size */
  1991. b->width * sizeof(GLushort), /* stride */
  1992. b->backimage->data); /* dest buffer */
  1993. }
  1994. else if (xmesa->xm_visual->dithered_pf==PF_DITHER
  1995. && GET_VISUAL_DEPTH(xmesa->xm_visual)==8) {
  1996. /* Special case: 8bpp RGB */
  1997. for (y=0;y<b->height;y++) {
  1998. GLubyte *ptr = (GLubyte*) xmesa->xm_buffer->backimage->data
  1999. + xmesa->xm_buffer->backimage->bytes_per_line * y;
  2000. XDITHER_SETUP(y);
  2001. /* read row from 3Dfx frame buffer */
  2002. grLfbReadRegion( GR_BUFFER_FRONTBUFFER,
  2003. 0, xmesa->xm_buffer->FXctx->height-(b->height-y),
  2004. b->width, 1,
  2005. 0,
  2006. pixbuf );
  2007. /* write to XImage back buffer */
  2008. for (x=0;x<b->width;x++) {
  2009. GLubyte r = (pixbuf[x] & 0xf800) >> 8;
  2010. GLubyte g = (pixbuf[x] & 0x07e0) >> 3;
  2011. GLubyte b = (pixbuf[x] & 0x001f) << 3;
  2012. *ptr++ = XDITHER( x, r, g, b);
  2013. }
  2014. }
  2015. }
  2016. else {
  2017. /* General case: slow! */
  2018. for (y=0;y<b->height;y++) {
  2019. /* read row from 3Dfx frame buffer */
  2020. grLfbReadRegion( GR_BUFFER_FRONTBUFFER,
  2021. 0, xmesa->xm_buffer->FXctx->height-(b->height-y),
  2022. b->width, 1,
  2023. 0,
  2024. pixbuf );
  2025. /* write to XImage back buffer */
  2026. for (x=0;x<b->width;x++) {
  2027. XMesaPutPixel(b->backimage,x,y,
  2028. xmesa_color_to_pixel(xmesa,
  2029. (pixbuf[x] & 0xf800) >> 8,
  2030. (pixbuf[x] & 0x07e0) >> 3,
  2031. (pixbuf[x] & 0x001f) << 3,
  2032. 0xff, xmesa->pixelformat));
  2033. }
  2034. }
  2035. }
  2036. grLfbWriteColorFormat(GR_COLORFORMAT_ABGR);
  2037. }
  2038. #endif
  2039. /*
  2040. * Copy the back buffer to the front buffer. If there's no back buffer
  2041. * this is a no-op.
  2042. */
  2043. void XMesaSwapBuffers( XMesaBuffer b )
  2044. {
  2045. GET_CURRENT_CONTEXT(ctx);
  2046. /* If we're swapping the buffer associated with the current context
  2047. * we have to flush any pending rendering commands first.
  2048. */
  2049. if (b->xm_context && b->xm_context->gl_ctx == ctx)
  2050. _mesa_swapbuffers(ctx);
  2051. if (b->db_state) {
  2052. #ifdef FX
  2053. if (b->FXctx) {
  2054. fxMesaSwapBuffers();
  2055. if (b->FXwindowHack)
  2056. FXgetImage(b);
  2057. else
  2058. return;
  2059. }
  2060. #endif
  2061. if (b->backimage) {
  2062. /* Copy Ximage from host's memory to server's window */
  2063. #if defined(USE_XSHM) && !defined(XFree86Server)
  2064. if (b->shm) {
  2065. /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
  2066. XShmPutImage( b->xm_visual->display, b->frontbuffer,
  2067. b->cleargc,
  2068. b->backimage, 0, 0,
  2069. 0, 0, b->width, b->height, False );
  2070. /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
  2071. }
  2072. else
  2073. #endif
  2074. {
  2075. #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
  2076. XMesaDriSwapBuffers( b );
  2077. #else
  2078. /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
  2079. XMesaPutImage( b->xm_visual->display, b->frontbuffer,
  2080. b->cleargc,
  2081. b->backimage, 0, 0,
  2082. 0, 0, b->width, b->height );
  2083. /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
  2084. #endif
  2085. }
  2086. }
  2087. else {
  2088. /* Copy pixmap to window on server */
  2089. /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
  2090. XMesaCopyArea( b->xm_visual->display,
  2091. b->backpixmap, /* source drawable */
  2092. b->frontbuffer, /* dest. drawable */
  2093. b->cleargc,
  2094. 0, 0, b->width, b->height, /* source region */
  2095. 0, 0 /* dest region */
  2096. );
  2097. /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
  2098. }
  2099. }
  2100. #if !defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
  2101. XSync( b->xm_visual->display, False );
  2102. #endif
  2103. }
  2104. /*
  2105. * Copy sub-region of back buffer to front buffer
  2106. */
  2107. void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height )
  2108. {
  2109. GET_CURRENT_CONTEXT(ctx);
  2110. /* If we're swapping the buffer associated with the current context
  2111. * we have to flush any pending rendering commands first.
  2112. */
  2113. if (b->xm_context->gl_ctx == ctx)
  2114. _mesa_swapbuffers(ctx);
  2115. if (b->db_state) {
  2116. int yTop = b->bottom - y - height;
  2117. #ifdef FX
  2118. if (b->FXctx) {
  2119. fxMesaSwapBuffers();
  2120. if (b->FXwindowHack)
  2121. FXgetImage(b);
  2122. else
  2123. return;
  2124. }
  2125. #endif
  2126. if (b->backimage) {
  2127. /* Copy Ximage from host's memory to server's window */
  2128. #if defined(USE_XSHM) && !defined(XFree86Server)
  2129. if (b->shm) {
  2130. /* XXX assuming width and height aren't too large! */
  2131. XShmPutImage( b->xm_visual->display, b->frontbuffer,
  2132. b->cleargc,
  2133. b->backimage, x, yTop,
  2134. x, yTop, width, height, False );
  2135. /* wait for finished event??? */
  2136. }
  2137. else
  2138. #endif
  2139. {
  2140. /* XXX assuming width and height aren't too large! */
  2141. XMesaPutImage( b->xm_visual->display, b->frontbuffer,
  2142. b->cleargc,
  2143. b->backimage, x, yTop,
  2144. x, yTop, width, height );
  2145. }
  2146. }
  2147. else {
  2148. /* Copy pixmap to window on server */
  2149. XMesaCopyArea( b->xm_visual->display,
  2150. b->backpixmap, /* source drawable */
  2151. b->frontbuffer, /* dest. drawable */
  2152. b->cleargc,
  2153. x, yTop, width, height, /* source region */
  2154. x, yTop /* dest region */
  2155. );
  2156. }
  2157. }
  2158. }
  2159. /*
  2160. * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function
  2161. * is a way to get "under the hood" of X/Mesa so one can manipulate the
  2162. * back buffer directly.
  2163. * Output: pixmap - pointer to back buffer's Pixmap, or 0
  2164. * ximage - pointer to back buffer's XImage, or NULL
  2165. * Return: GL_TRUE = context is double buffered
  2166. * GL_FALSE = context is single buffered
  2167. */
  2168. GLboolean XMesaGetBackBuffer( XMesaBuffer b,
  2169. XMesaPixmap *pixmap,
  2170. XMesaImage **ximage )
  2171. {
  2172. if (b->db_state) {
  2173. if (pixmap) *pixmap = b->backpixmap;
  2174. if (ximage) *ximage = b->backimage;
  2175. return GL_TRUE;
  2176. }
  2177. else {
  2178. *pixmap = 0;
  2179. *ximage = NULL;
  2180. return GL_FALSE;
  2181. }
  2182. }
  2183. /*
  2184. * Return the depth buffer associated with an XMesaBuffer.
  2185. * Input: b - the XMesa buffer handle
  2186. * Output: width, height - size of buffer in pixels
  2187. * bytesPerValue - bytes per depth value (2 or 4)
  2188. * buffer - pointer to depth buffer values
  2189. * Return: GL_TRUE or GL_FALSE to indicate success or failure.
  2190. */
  2191. GLboolean XMesaGetDepthBuffer( XMesaBuffer b, GLint *width, GLint *height,
  2192. GLint *bytesPerValue, void **buffer )
  2193. {
  2194. if ((!b->gl_buffer) || (!b->gl_buffer->DepthBuffer)) {
  2195. *width = 0;
  2196. *height = 0;
  2197. *bytesPerValue = 0;
  2198. *buffer = 0;
  2199. return GL_FALSE;
  2200. }
  2201. else {
  2202. *width = b->gl_buffer->Width;
  2203. *height = b->gl_buffer->Height;
  2204. *bytesPerValue = sizeof(GLdepth);
  2205. *buffer = b->gl_buffer->DepthBuffer;
  2206. return GL_TRUE;
  2207. }
  2208. }
  2209. void XMesaFlush( XMesaContext c )
  2210. {
  2211. if (c && c->xm_visual) {
  2212. #ifdef XFree86Server
  2213. /* NOT_NEEDED */
  2214. #else
  2215. XSync( c->xm_visual->display, False );
  2216. #endif
  2217. }
  2218. }
  2219. const char *XMesaGetString( XMesaContext c, int name )
  2220. {
  2221. (void) c;
  2222. if (name==XMESA_VERSION) {
  2223. return "3.1";
  2224. }
  2225. else if (name==XMESA_EXTENSIONS) {
  2226. return "";
  2227. }
  2228. else {
  2229. return NULL;
  2230. }
  2231. }
  2232. XMesaBuffer XMesaFindBuffer( XMesaDisplay *dpy, XMesaDrawable d )
  2233. {
  2234. XMesaBuffer b;
  2235. for (b=XMesaBufferList; b; b=b->Next) {
  2236. if (b->frontbuffer==d && b->display==dpy) {
  2237. return b;
  2238. }
  2239. }
  2240. return NULL;
  2241. }
  2242. /*
  2243. * Look for XMesaBuffers whose X window has been destroyed.
  2244. * Deallocate any such XMesaBuffers.
  2245. */
  2246. void XMesaGarbageCollect( void )
  2247. {
  2248. XMesaBuffer b, next;
  2249. for (b=XMesaBufferList; b; b=next) {
  2250. next = b->Next;
  2251. if (!b->pixmap_flag) {
  2252. #ifdef XFree86Server
  2253. /* NOT_NEEDED */
  2254. #else
  2255. XSync(b->display, False);
  2256. if (!window_exists( b->display, b->frontbuffer )) {
  2257. /* found a dead window, free the ancillary info */
  2258. XMesaDestroyBuffer( b );
  2259. }
  2260. #endif
  2261. }
  2262. }
  2263. }
  2264. void XMesaReset( void )
  2265. {
  2266. while (XMesaBufferList)
  2267. XMesaDestroyBuffer(XMesaBufferList);
  2268. XMesaBufferList = NULL;
  2269. }
  2270. unsigned long XMesaDitherColor( XMesaContext xmesa, GLint x, GLint y,
  2271. GLfloat red, GLfloat green,
  2272. GLfloat blue, GLfloat alpha )
  2273. {
  2274. GLint r = (GLint) (red * 255.0F);
  2275. GLint g = (GLint) (green * 255.0F);
  2276. GLint b = (GLint) (blue * 255.0F);
  2277. GLint a = (GLint) (alpha * 255.0F);
  2278. switch (xmesa->pixelformat) {
  2279. case PF_INDEX:
  2280. return 0;
  2281. case PF_TRUECOLOR:
  2282. {
  2283. unsigned long p;
  2284. PACK_TRUECOLOR( p, r, g, b );
  2285. return p;
  2286. }
  2287. case PF_8A8B8G8R:
  2288. return PACK_8A8B8G8R( r, g, b, a );
  2289. case PF_8R8G8B:
  2290. return PACK_8R8G8B( r, g, b );
  2291. case PF_5R6G5B:
  2292. return PACK_5R6G5B( r, g, b );
  2293. case PF_DITHER:
  2294. {
  2295. DITHER_SETUP;
  2296. return DITHER( x, y, r, g, b );
  2297. }
  2298. case PF_1BIT:
  2299. /* 382 = (3*255)/2 */
  2300. return ((r+g+b) > 382) ^ xmesa->xm_visual->bitFlip;
  2301. case PF_HPCR:
  2302. return DITHER_HPCR(x, y, r, g, b);
  2303. case PF_LOOKUP:
  2304. {
  2305. LOOKUP_SETUP;
  2306. return LOOKUP( r, g, b );
  2307. }
  2308. case PF_GRAYSCALE:
  2309. return GRAY_RGB( r, g, b );
  2310. case PF_DITHER_5R6G5B:
  2311. /* fall through */
  2312. case PF_TRUEDITHER:
  2313. {
  2314. unsigned long p;
  2315. PACK_TRUEDITHER(p, x, y, r, g, b);
  2316. return p;
  2317. }
  2318. default:
  2319. gl_problem(NULL, "Bad pixel format in XMesaDitherColor");
  2320. }
  2321. return 0;
  2322. }