Mesa3d Source Code Reading Notes (2)-wglCreateContext debugging and execution

Source: Internet
Author: User

 

Compile a small OpenGL program, attach it to the mesa project for debugging, and start to enter:
// OpenGLDC. cpp file, starting from line 3 // before this code block, ChoosePixelFormat/SetPixelFormat will be called, but the corresponding release function/* 59 */if (! (M_hRC = wglCreateContext (m_hDC) // obtain the rendering description handle {MessageBox (NULL, "OpenGL rendering description table cannot be created", "error", MB_ OK | MB_ICONEXCLAMATION ); return false;} if (! WglMakeCurrent (m_hDC, m_hRC) // try to activate the coloring description table {MessageBox (NULL, "Current OpenGL rendering description table cannot be activated", "error", MB_ OK | MB_ICONEXCLAMATION ); return false;/* 70 */}

The wglCreateContext function at row 59th enters the wglCreateContext at row 179th in wgl. c. This is the wglCreateContext function of the mesa project. The code for this function is as follows:

WglCreateContext // wgl. c from row 179th to row 203 wingdiapi hglrc glapientry wglCreateContext (HDC hdc) {int I = 0; if (! Ctx_count) {for (I = 0; I <MESAWGL_CTX_MAX_COUNT; I ++) {wgl_ctx [I]. ctx = NULL ;}for (I = 0; I <MESAWGL_CTX_MAX_COUNT; I ++) {if (wgl_ctx [I]. ctx = NULL) {wgl_ctx [I]. ctx = WMesaCreateContext (hdc, NULL, (GLboolean) GL_TRUE, (GLboolean) (pfd [curPFD-1]. doubleBuffered? GL_TRUE: GL_FALSE), (GLboolean) (pfd [curPFD-1]. pfd. cAlphaBits? GL_TRUE: GL_FALSE); if (wgl_ctx [I]. ctx = NULL) break; ctx_count ++; return (HGLRC) wgl_ctx [I]. ctx) ;}} SetLastError (0); return (NULL );}

The wgl_ctx array is declared in the wgl. c file (the size of MESAWGL_CTX_MAX_COUNT is limited, and the current value is 20). ctx_count stores the number of initialized arrays.
182nd the row determines that if ctx_count is greater than 0, all elements in the wgl_ctx array are null for the first initialization;
The for loop at 187th aims to find an empty wgl_ctx array element to store the address of the WMesaContext structure to be created. If you create one, add ctx_count 1, return the created structure address (HGLRC handle );
201st should not be executed. When the number of created WMesaContext is greater than the width of the wgl_ctx array, it will arrive here. Therefore, you cannot create more than 20 HGLRC handles.

The WMesaCreateContext function starts in row 1,418th of wmesa. c. The code of this function is as follows;

Wmesacreatecontext // wmesa. c from row 1,418th to row 1531 wmesacontext partition (HDC, hpalette * pal, glboolean kernel, glboolean kernel, glboolean alpha_flag) {wmesacontext C; struct functions; glint red_bits, green_bits, blue_bits, alpha_bits; glcontext * CTX; glvisual * visual; (void) pal;/* indexed mode not supported */If (! Rgb_flag) return NULL;/* allocate wmesa context */C = calloc_struct (wmesa_context); If (! C) return NULL; # If 0/* I do not understand this contributed Code * // * Support memory and device contexts */If (windowfromdc (HDC )! = NULL) {C-> HDC = getdc (windowfromdc (HDC);/* HUH ???? */} Else {C-> HDC = HDC;} # else C-> HDC = HDC; # endif/* Get data for visual * // * dealing with this is actually a bit of overkill because Mesa will end * up treating all color component size requests less than 8 by using * single byte per channel. in addition, the interface to the span * routines passes colors as an entire byte per channel anyway, so there * is nothing to be saved by telling Visual to be 16 bits if the device * is 16 bits. that is, Mesa is going to compute colors down to 8 bits per * channel anyway. * But we go through the motions here anyway. */switch (getdevicecaps (c-> HDC, bitspixel) {Case 16: red_bits = green_bits = blue_bits = 5; alpha_bits = 0; break; default: red_bits = green_bits = blue_bits = 8; alpha_bits = 8; break;}/* Create visual Based on flags */visual = _ Mesa_create_visual (rgb_flag, db_flag,/* db_flag */gl_false,/* stereo */red_bits, green_bits, blue_bits,/* color RGB */alpha_flag? Alpha_bits: 0,/* color a */0,/* index bits */default_software_depth_bits,/* depth_bits */8,/* stencil_bits */16, 16, 16, /* accum RGB */alpha_flag? 16: 0,/* accum A */1);/* num samples */If (! Visual) {_ mesa_free (c); return NULL;}/* set up driver functions */_ mesa_init_driver_functions (& functions); functions. getstring = wmesa_get_string; functions. updatestate = wmesa_update_state; functions. getbuffersize = wmesa_get_buffer_size; functions. flush = wmesa_flush; functions. clear = clear; functions. clearindex = clear_index; functions. clearcolor = clear_color; functions. resizebuffers = wmesa _ Resize_buffers; functions. viewport = wmesa_viewport;/* initialize the mesa Context Data */CTX = & C-> gl_ctx; _ mesa_initialize_context (CTX, visual, null, & functions, (void *) C ); /* visual no longer needed-it was copied by _ mesa_initialize_context () */_ mesa_destroy_visual (visual); _ mesa_enable_sw_extensions (CTX); _ mesa_enable_1_3_extensions (CTX ); _ mesa_enable_1_4_extensions (CTX); _ mesa_enable_1 _ 5_extensions (CTX); _ mesa_enable_2_0_extensions (CTX); _ mesa_enable_2_1_extensions (CTX);/* initialize the software Rasterizer and helper modules. */If (! _ Swrast_createcontext (CTX) |! _ Vbo_createcontext (CTX) |! _ Tnl_createcontext (CTX) |! _ Swsetup_createcontext (CTX) {_ mesa_free_context_data (CTX); _ mesa_free (c); return NULL;} _ swsetup_wakeup (CTX); tnl_context (CTX)-> driver. runpipeline = _ tnl_run_pipeline; return C ;}

The _ mesa_init_driver_functions (& functions) in row 1,493rd initializes the dd_function_table structure, that is, sets the function pointer address of the structure, which is defined in dd. the header file starts with 68th lines, Device driver functions table. (device-driven function table). The vast majority of these functions correspond directly to OpenGL status commands.
The _ mesa_initialize_context function of Row 3 initializes a GLcontext structure, which is one of the three core architectures of mesa and has many members. See the following description.
There are four internal modules in row 1,519th for creation initialization: _ swrast, _ vbo, _ tnl, and _ swsetup.
1,528th set the _ tnl pipeline function at the line, which guides the subsequent vertex transformation, window cropping, illumination texture calculation, and final rendering.

Temporarily stop analyzing these called internal functions to see what the wglMakeCurrent function will do. Line wgl. c is the wglMakeCurrent release function corresponding to the project.

WglMakeCurrent // wgl. c file 234th to 254 lines wingdiapi bool glapientry wglMakeCurrent (HDC hdc, HGLRC hglrc) {int I; CurrentHDC = hdc; if (! Hdc |! Hglrc) {WMesaMakeCurrent (NULL, NULL); ctx_current =-1; return TRUE ;}for (I = 0; I <MESAWGL_CTX_MAX_COUNT; I ++) {if (wgl_ctx [I]. ctx = (WMesaContext) hglrc) {WMesaMakeCurrent (WMesaContext) hglrc, hdc); ctx_current = I; return TRUE ;}} return FALSE ;}

Row 3 stores the DC handle of the current device;
Row 3 checks whether the association between hDC and hglrc is to be canceled. If yes, WMesaMakeCurrent is called with NULL as the parameter;
Starting from row 3, The for loop checks whether the input parameter hglrc is valid, that is, it must be the variable address stored during previous creation. If it can be found, WMesaMakeCurrent is called to associate hglrc with the hdc device DC;
Ctx_current is the serial number of the wgl_ctx array corresponding to the currently valid hglrc.

Then, the program is transferred to the WMesaMakeCurrent function in wmesa. c. The source code is:

WMesaMakeCurrent // wmesa. from Row c 1,592nd to row 3 void WMesaMakeCurrent (WMesaContext c, HDC hdc) {WMesaFramebuffer pwfb; {/* return if already current */GET_CURRENT_CONTEXT (ctx ); WMesaContext pwc = wmesa_context (ctx); if (pwc & c = pwc & pwc-> hDC = hdc) return;} pwfb = wmesa_lookup_framebuffer (hdc ); /* Lazy creation of framebuffers */if (c &&! Pwfb & hdc) {struct gl_renderbuffer * rb; GLvisual * visual = & c-> gl_ctx.Visual; GLuint width, height; get_window_size (hdc, & width, & height ); c-> clearPen = CreatePen (PS_SOLID, 1, 0); c-> clearBrush = CreateSolidBrush (0); pwfb = wmesa_new_framebuffer (hdc, visual ); /* Create back buffer if double buffered */if (visual-> doubleBufferMode = 1) {wmCreateBackingStore (pwfb, width, height );} /* make render buffers */if (visual-> doubleBufferMode = 1) {rb = wmesa_new_renderbuffer (); _ mesa_add_renderbuffer (& pwfb-> Base, BUFFER_BACK_LEFT, rb ); trim (rb, pwfb-> pixelformat, pwfb-> cColorBits, 1);} rb = wmesa_new_renderbuffer (); _ mesa_add_renderbuffer (& pwfb-> Base, BUFFER_FRONT_LEFT, rb ); wmesa_set_renderbuffer_funcs (rb, pwfb-> pixelformat, pwfb-> cColorBits, 0);/* Let Mesa own the Depth, stencel, and Accum buffers */_ mesa_add_soft_renderbuffers (& pwfb-> Base, GL_FALSE,/* color */visual-> depthBits> 0, visual-> stencilBits> 0, visual-> accumRedBits> 0, visual-> alphaBits> 0, GL_FALSE);} if (c & pwfb) _ mesa_make_current (& c-> gl_ctx, & pwfb-> Base, & pwfb-> Base); else _ mesa_make_current (NULL, NULL, NULL );}

Lines 1,596th to 1602nd clearly confirm whether hglrc is associated with hdc. If yes, you can directly return the result without doing anything;
Row 3 searches for the created WMesaFramebuffer that uses hdc. If no WMesaFramebuffer is found, NULL is returned;
If judgment at location 1607th: c (hglrc) and hdc are valid, that is, if pwfb is NULL, the Code body between 1608th and 1642 is executed;
Check whether C (hglrc) and pwfb are valid. If so, call _ mesa_make_current. Otherwise, use the null parameter to cancel the association.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.