1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
/* CDK - The GIMP Drawing Kit
 *
 * cdkglcontext-win32.c: Win32 specific OpenGL wrappers
 *
 * Copyright © 2014 Emmanuele Bassi
 * Copyright © 2014 Alexander Larsson
 * Copyright © 2014 Chun-wei Fan
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "cdkprivate-win32.h"
#include "cdkwindow-win32.h"
#include "cdkglcontext-win32.h"
#include "cdkdisplay-win32.h"

#include "cdkwin32display.h"
#include "cdkwin32glcontext.h"
#include "cdkwin32misc.h"
#include "cdkwin32screen.h"
#include "cdkwin32window.h"

#include "cdkglcontext.h"
#include "cdkwindow.h"
#include "cdkinternals.h"
#include "cdkintl.h"

#include <cairo.h>
#include <epoxy/wgl.h>

#ifdef CDK_WIN32_ENABLE_EGL
# include <epoxy/egl.h>
#endif

G_DEFINE_TYPE (CdkWin32GLContext, cdk_win32_gl_context, CDK_TYPE_GL_CONTEXT)

static void
_cdk_win32_gl_context_dispose (GObject *gobject)
{
  CdkGLContext *context = CDK_GL_CONTEXT (gobject);
  CdkWin32GLContext *context_win32 = CDK_WIN32_GL_CONTEXT (gobject);
  CdkWin32Display *display_win32 = CDK_WIN32_DISPLAY (cdk_gl_context_get_display (context));
  CdkWindow *window = cdk_gl_context_get_window (context);
  CdkWindowImplWin32 *impl = NULL;

  if (context_win32->hglrc != NULL)
    {
      if (wglGetCurrentContext () == context_win32->hglrc)
        wglMakeCurrent (NULL, NULL);

      CDK_NOTE (OPENGL, g_print ("Destroying WGL context\n"));

      wglDeleteContext (context_win32->hglrc);
      context_win32->hglrc = NULL;

      ReleaseDC (display_win32->gl_hwnd, context_win32->gl_hdc);
    }

#ifdef CDK_WIN32_ENABLE_EGL
  if (context_win32->egl_context != EGL_NO_CONTEXT)
    {
      if (eglGetCurrentContext () == context_win32->egl_context)
        eglMakeCurrent(display_win32->egl_disp, EGL_NO_SURFACE, EGL_NO_SURFACE,
                       EGL_NO_CONTEXT);

      CDK_NOTE (OPENGL, g_message ("Destroying EGL (ANGLE) context"));

      eglDestroyContext (display_win32->egl_disp,
                         context_win32->egl_context);
      context_win32->egl_context = EGL_NO_CONTEXT;

      impl = CDK_WINDOW_IMPL_WIN32 (window->impl);

      ReleaseDC (display_win32->gl_hwnd, context_win32->gl_hdc);
    }
#endif

  if (window != NULL && window->impl != NULL)
    {
      impl = CDK_WINDOW_IMPL_WIN32 (window->impl);

      if (impl->suppress_layered > 0)
        impl->suppress_layered--;

      /* If we don't have any window that forces layered windows off,
       * trigger update_style_bits() to enable layered windows again
       */
      if (impl->suppress_layered == 0)
        _cdk_win32_window_update_style_bits (window);
    }

  G_OBJECT_CLASS (cdk_win32_gl_context_parent_class)->dispose (gobject);
}

static void
cdk_win32_gl_context_class_init (CdkWin32GLContextClass *klass)
{
  CdkGLContextClass *context_class = CDK_GL_CONTEXT_CLASS (klass);
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  context_class->end_frame = _cdk_win32_gl_context_end_frame;
  context_class->realize = _cdk_win32_gl_context_realize;

  gobject_class->dispose = _cdk_win32_gl_context_dispose;
}

static void
cdk_win32_gl_context_init (CdkWin32GLContext *self)<--- Parameter 'self' can be declared as pointer to const
{
}

static void
cdk_gl_blit_region (CdkWindow *window, cairo_region_t *region)
{
  int n_rects, i;
  int scale = cdk_window_get_scale_factor (window);
  int wh = cdk_window_get_height (window);
  cairo_rectangle_int_t rect;

  n_rects = cairo_region_num_rectangles (region);
  for (i = 0; i < n_rects; i++)
    {
      cairo_region_get_rectangle (region, i, &rect);
      glScissor (rect.x * scale, (wh - rect.y - rect.height) * scale, rect.width * scale, rect.height * scale);
      glBlitFramebuffer (rect.x * scale, (wh - rect.y - rect.height) * scale, (rect.x + rect.width) * scale, (wh - rect.y) * scale,
                         rect.x * scale, (wh - rect.y - rect.height) * scale, (rect.x + rect.width) * scale, (wh - rect.y) * scale,
                         GL_COLOR_BUFFER_BIT, GL_NEAREST);
    }
}

static gboolean
_get_is_egl_force_redraw (CdkWindow *window)<--- Parameter 'window' can be declared as pointer to const
{
  /* We only need to call cdk_window_invalidate_rect () if necessary */
#ifdef CDK_WIN32_ENABLE_EGL
  if (window->gl_paint_context != NULL && cdk_gl_context_get_use_es (window->gl_paint_context))
    {
      CdkWindowImplWin32 *impl = CDK_WINDOW_IMPL_WIN32 (window->impl);

      return impl->egl_force_redraw_all;
    }
#endif
  return FALSE;
}

static void
_reset_egl_force_redraw (CdkWindow *window)<--- Parameter 'window' can be declared as pointer to const
{
#ifdef CDK_WIN32_ENABLE_EGL
  if (window->gl_paint_context != NULL && cdk_gl_context_get_use_es (window->gl_paint_context))
    {
      CdkWindowImplWin32 *impl = CDK_WINDOW_IMPL_WIN32 (window->impl);

      if (impl->egl_force_redraw_all)
        impl->egl_force_redraw_all = FALSE;
    }
#endif
}

void
_cdk_win32_gl_context_end_frame (CdkGLContext *context,
                                 cairo_region_t *painted,
                                 cairo_region_t *damage)
{
  CdkWin32GLContext *context_win32 = CDK_WIN32_GL_CONTEXT (context);
  CdkWindow *window = cdk_gl_context_get_window (context);
  CdkWin32Display *display = (CDK_WIN32_DISPLAY (cdk_gl_context_get_display (context)));

  cdk_gl_context_make_current (context);

  if (!cdk_gl_context_get_use_es (context))
    {
      gboolean can_wait = display->hasWglOMLSyncControl;

      if (context_win32->do_frame_sync)
        {
          glFinish ();

          if (can_wait)
            {
              gint64 ust, msc, sbc;

              wglGetSyncValuesOML (context_win32->gl_hdc, &ust, &msc, &sbc);
              wglWaitForMscOML (context_win32->gl_hdc,
                                0,
                                2,
                                (msc + 1) % 2,
                                &ust, &msc, &sbc);
            }
        }

      if (context_win32->do_blit_swap)
        {
          glDrawBuffer(GL_FRONT);
          glReadBuffer(GL_BACK);
          cdk_gl_blit_region (window, painted);
          glDrawBuffer(GL_BACK);
          glFlush();

          if (cdk_gl_context_has_frame_terminator (context))
            glFrameTerminatorGREMEDY ();
        }
      else
        SwapBuffers (context_win32->gl_hdc);
    }

#ifdef CDK_WIN32_ENABLE_EGL
  else
    {
      EGLSurface egl_surface = _cdk_win32_window_get_egl_surface (window, context_win32->egl_config, FALSE);
      gboolean force_egl_redraw_all = _get_is_egl_force_redraw (window);

      if (context_win32->do_blit_swap && !force_egl_redraw_all)
        cdk_gl_blit_region (window, painted);
      else if (force_egl_redraw_all)
        {
          CdkRectangle rect = {0, 0, cdk_window_get_width (window), cdk_window_get_height (window)};

          /* We need to do cdk_window_invalidate_rect() so that we don't get glitches after maximizing or
           *  restoring or using aerosnap
           */
          cdk_window_invalidate_rect (window, &rect, TRUE);
          _reset_egl_force_redraw (window);
        }

      eglSwapBuffers (display->egl_disp, egl_surface);
    }
#endif
}

void
_cdk_win32_window_invalidate_for_new_frame (CdkWindow *window,
                                            cairo_region_t *update_area)
{
  cairo_rectangle_int_t window_rect;
  gboolean invalidate_all = FALSE;
  CdkWin32GLContext *context_win32;
  cairo_rectangle_int_t whole_window = { 0, 0, cdk_window_get_width (window), cdk_window_get_height (window) };

  /* Minimal update is ok if we're not drawing with gl */
  if (window->gl_paint_context == NULL)
    return;

  context_win32 = CDK_WIN32_GL_CONTEXT (window->gl_paint_context);
  context_win32->do_blit_swap = FALSE;

  if (cdk_gl_context_has_framebuffer_blit (window->gl_paint_context) &&
      cairo_region_contains_rectangle (update_area, &whole_window) != CAIRO_REGION_OVERLAP_IN)
    {
      context_win32->do_blit_swap = TRUE;
    }
  else
    invalidate_all = TRUE;

  if (invalidate_all)
    {
      window_rect.x = 0;
      window_rect.y = 0;
      window_rect.width = cdk_window_get_width (window);
      window_rect.height = cdk_window_get_height (window);

      /* If nothing else is known, repaint everything so that the back
         buffer is fully up-to-date for the swapbuffer */
      cairo_region_union_rectangle (update_area, &window_rect);
    }
}

typedef struct
{
  ATOM wc_atom;
  HWND hwnd;
  HDC hdc;
  HGLRC hglrc;
  gboolean inited;
} CdkWGLDummy;

static void
_destroy_dummy_gl_context (CdkWGLDummy dummy)
{
  if (dummy.hglrc != NULL)
    {
      wglDeleteContext (dummy.hglrc);
      dummy.hglrc = NULL;<--- Variable 'dummy.hglrc' is assigned a value that is never used.
    }
  if (dummy.hdc != NULL)
    {
      DeleteDC (dummy.hdc);
      dummy.hdc = NULL;<--- Variable 'dummy.hdc' is assigned a value that is never used.
    }
  if (dummy.hwnd != NULL)
    {
      DestroyWindow (dummy.hwnd);
      dummy.hwnd = NULL;<--- Variable 'dummy.hwnd' is assigned a value that is never used.
    }
  if (dummy.wc_atom != 0)
    {
      UnregisterClass (MAKEINTATOM (dummy.wc_atom), GetModuleHandle (NULL));
      dummy.wc_atom = 0;<--- Variable 'dummy.wc_atom' is assigned a value that is never used.
    }
  dummy.inited = FALSE;<--- Variable 'dummy.inited' is assigned a value that is never used.
}

/* Yup, we need to create a dummy window for the dummy WGL context */
static void
_get_dummy_window_hwnd (CdkWGLDummy *dummy)
{
  WNDCLASSEX dummy_wc;

  memset (&dummy_wc, 0, sizeof (WNDCLASSEX));

  dummy_wc.cbSize = sizeof( WNDCLASSEX );
  dummy_wc.style = CS_OWNDC;
  dummy_wc.lpfnWndProc = (WNDPROC) DefWindowProc;
  dummy_wc.cbClsExtra = 0;
  dummy_wc.cbWndExtra = 0;
  dummy_wc.hInstance = GetModuleHandle( NULL );
  dummy_wc.hIcon = 0;
  dummy_wc.hCursor = NULL;
  dummy_wc.hbrBackground = 0;
  dummy_wc.lpszMenuName = 0;
  dummy_wc.lpszClassName = "dummy";
  dummy_wc.hIconSm = 0;

  dummy->wc_atom = RegisterClassEx (&dummy_wc);

  dummy->hwnd =
    CreateWindowEx (WS_EX_APPWINDOW,
                    MAKEINTATOM (dummy->wc_atom),
                    "",
                    WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
                    0,
                    0,
                    0,
                    0,
                    NULL,
                    NULL,
                    GetModuleHandle (NULL),
                    NULL);
}

static gint
_cdk_init_dummy_context (CdkWGLDummy    *dummy,
                         const gboolean  need_alpha_bits);

#define PIXEL_ATTRIBUTES 19

static gint
_get_wgl_pfd (HDC                    hdc,
              const gboolean         need_alpha_bits,
              PIXELFORMATDESCRIPTOR *pfd,
              CdkWin32Display       *display)<--- Parameter 'display' can be declared as pointer to const
{
  gint best_pf = 0;

  pfd->nSize = sizeof (PIXELFORMATDESCRIPTOR);

  if (display != NULL && display->hasWglARBPixelFormat)
    {
      CdkWGLDummy dummy;
      UINT num_formats;
      gint colorbits = GetDeviceCaps (hdc, BITSPIXEL);
      guint extra_fields = 1;
      gint i = 0;
      int pixelAttribs[PIXEL_ATTRIBUTES];
      int alpha_idx = 0;

      /* Save up the HDC and HGLRC that we are currently using, to restore back to it when we are done here */
      HDC hdc_current = wglGetCurrentDC ();
      HGLRC hglrc_current = wglGetCurrentContext ();

      if (display->hasWglARBmultisample)
        {
          /* 2 pairs of values needed for multisampling/AA support */
          extra_fields += 2 * 2;<--- Variable 'extra_fields' is assigned a value that is never used.
        }

      /* Update PIXEL_ATTRIBUTES above if any groups are added here! */
      /* one group contains a value pair for both pixelAttribs and pixelAttribsNoAlpha */
      pixelAttribs[i] = WGL_DRAW_TO_WINDOW_ARB;
      pixelAttribs[i++] = GL_TRUE;

      pixelAttribs[i++] = WGL_SUPPORT_OPENGL_ARB;
      pixelAttribs[i++] = GL_TRUE;

      pixelAttribs[i++] = WGL_DOUBLE_BUFFER_ARB;
      pixelAttribs[i++] = GL_TRUE;

      pixelAttribs[i++] = WGL_ACCELERATION_ARB;
      pixelAttribs[i++] = WGL_FULL_ACCELERATION_ARB;

      pixelAttribs[i++] = WGL_PIXEL_TYPE_ARB;
      pixelAttribs[i++] = WGL_TYPE_RGBA_ARB;

      pixelAttribs[i++] = WGL_COLOR_BITS_ARB;
      pixelAttribs[i++] = colorbits;

      /* end of "Update PIXEL_ATTRIBUTES above if any groups are added here!" */

      if (display->hasWglARBmultisample)
        {
          pixelAttribs[i++] = WGL_SAMPLE_BUFFERS_ARB;
          pixelAttribs[i++] = 1;

          pixelAttribs[i++] = WGL_SAMPLES_ARB;
          pixelAttribs[i++] = 8;
        }

      pixelAttribs[i++] = WGL_ALPHA_BITS_ARB;

      /* track the spot where the alpha bits are, so that we can clear it if needed */
      alpha_idx = i;

      pixelAttribs[i++] = 8;
      pixelAttribs[i++] = 0; /* end of pixelAttribs */

      memset (&dummy, 0, sizeof (CdkWGLDummy));

      /* acquire and cache dummy Window (HWND & HDC) and
       * dummy GL Context, we need it for wglChoosePixelFormatARB()
       */
      best_pf = _cdk_init_dummy_context (&dummy, need_alpha_bits);

      if (best_pf == 0 || !wglMakeCurrent (dummy.hdc, dummy.hglrc))
        {
          wglMakeCurrent (hdc_current, hglrc_current);
          return 0;
        }

      wglChoosePixelFormatARB (hdc,
                               pixelAttribs,
                               NULL,
                               1,
                               &best_pf,
                               &num_formats);

      if (best_pf == 0)
        {
          if (!need_alpha_bits)
            {
              pixelAttribs[alpha_idx] = 0;
              pixelAttribs[alpha_idx + 1] = 0;

              /* give another chance if need_alpha_bits is FALSE,
               * meaning we prefer to have an alpha channel anyways
               */
              wglChoosePixelFormatARB (hdc,
                                       pixelAttribs,
                                       NULL,
                                       1,
                                       &best_pf,
                                       &num_formats);

            }
        }

      /* Go back to the HDC that we were using, since we are done with the dummy HDC and GL Context */
      wglMakeCurrent (hdc_current, hglrc_current);
      _destroy_dummy_gl_context (dummy);
    }
  else
    {
      pfd->nVersion = 1;
      pfd->dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
      pfd->iPixelType = PFD_TYPE_RGBA;
      pfd->cColorBits = GetDeviceCaps (hdc, BITSPIXEL);
      pfd->cAlphaBits = 8;
      pfd->dwLayerMask = PFD_MAIN_PLANE;

      best_pf = ChoosePixelFormat (hdc, pfd);

      if (best_pf == 0)
        /* give another chance if need_alpha_bits is FALSE,
         * meaning we prefer to have an alpha channel anyways
         */
        if (!need_alpha_bits)
          {
            pfd->cAlphaBits = 0;
            best_pf = ChoosePixelFormat (hdc, pfd);
          }
    }

  return best_pf;
}

/* in WGL, for many OpenGL items, we need a dummy WGL context, so create
 * one and cache it for later use
 */
static gint
_cdk_init_dummy_context (CdkWGLDummy    *dummy,
                         const gboolean  need_alpha_bits)
{
  PIXELFORMATDESCRIPTOR pfd;
  gboolean set_pixel_format_result = FALSE;
  gint best_idx = 0;

  _get_dummy_window_hwnd (dummy);

  dummy->hdc = GetDC (dummy->hwnd);
  memset (&pfd, 0, sizeof (PIXELFORMATDESCRIPTOR));

  best_idx = _get_wgl_pfd (dummy->hdc, need_alpha_bits, &pfd, NULL);

  if (best_idx != 0)
    set_pixel_format_result = SetPixelFormat (dummy->hdc,
                                              best_idx,
                                              &pfd);

  if (best_idx == 0 || !set_pixel_format_result)
    return 0;

  dummy->hglrc = wglCreateContext (dummy->hdc);
  if (dummy->hglrc == NULL)
    return 0;

  dummy->inited = TRUE;

  return best_idx;
}

#ifdef CDK_WIN32_ENABLE_EGL

#ifndef EGL_PLATFORM_ANGLE_ANGLE
#define EGL_PLATFORM_ANGLE_ANGLE          0x3202
#endif

#ifndef EGL_PLATFORM_ANGLE_TYPE_ANGLE
#define EGL_PLATFORM_ANGLE_TYPE_ANGLE     0x3203
#endif

#ifndef EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE
#define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208
#endif

static EGLDisplay
_cdk_win32_get_egl_display (CdkWin32Display *display)
{
  EGLDisplay disp;
  gboolean success = FALSE;

  if (epoxy_has_egl_extension (NULL, "EGL_EXT_platform_base"))
    {
      PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplay = (void *) eglGetProcAddress ("eglGetPlatformDisplayEXT");
      if (getPlatformDisplay)
        {
          EGLint disp_attr[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, EGL_NONE};

          disp = getPlatformDisplay (EGL_PLATFORM_ANGLE_ANGLE, display->hdc_egl_temp, disp_attr);

          if (disp != EGL_NO_DISPLAY)
            return disp;
        }
    }
  return eglGetDisplay (display->hdc_egl_temp);
}
#endif

static gboolean
_cdk_win32_display_init_gl (CdkDisplay *display,
                            const gboolean need_alpha_bits)
{
  CdkWin32Display *display_win32 = CDK_WIN32_DISPLAY (display);
  gint best_idx = 0;<--- Variable 'best_idx' is assigned a value that is never used.
  gboolean disable_wgl = FALSE;

#ifdef CDK_WIN32_ENABLE_EGL
  EGLDisplay egl_disp;

  disable_wgl = ((_cdk_gl_flags & CDK_GL_GLES) != 0) ||
                display_win32->running_on_arm64;
#endif

  if (display_win32->have_wgl
#ifdef CDK_WIN32_ENABLE_EGL
      || display_win32->have_egl
#endif
     )
    return TRUE;

  if (!disable_wgl)
    {
      /* acquire and cache dummy Window (HWND & HDC) and
       * dummy GL Context, it is used to query functions
       * and used for other stuff as well
       */
      CdkWGLDummy dummy;
      memset (&dummy, 0, sizeof (CdkWGLDummy));

      best_idx = _cdk_init_dummy_context (&dummy, need_alpha_bits);

      if (best_idx == 0 || !wglMakeCurrent (dummy.hdc, dummy.hglrc))
        return FALSE;

      display_win32->have_wgl = TRUE;
      display_win32->gl_version = epoxy_gl_version ();

      display_win32->hasWglARBCreateContext =
        epoxy_has_wgl_extension (dummy.hdc, "WGL_ARB_create_context");
      display_win32->hasWglEXTSwapControl =
        epoxy_has_wgl_extension (dummy.hdc, "WGL_EXT_swap_control");
      display_win32->hasWglOMLSyncControl =
        epoxy_has_wgl_extension (dummy.hdc, "WGL_OML_sync_control");
      display_win32->hasWglARBPixelFormat =
        epoxy_has_wgl_extension (dummy.hdc, "WGL_ARB_pixel_format");
      display_win32->hasWglARBmultisample =
        epoxy_has_wgl_extension (dummy.hdc, "WGL_ARB_multisample");

      CDK_NOTE (OPENGL,
                g_print ("WGL API version %d.%d found\n"
                         " - Vendor: %s\n"
                         " - Checked extensions:\n"
                         "\t* WGL_ARB_pixel_format: %s\n"
                         "\t* WGL_ARB_create_context: %s\n"
                         "\t* WGL_EXT_swap_control: %s\n"
                         "\t* WGL_OML_sync_control: %s\n"
                         "\t* WGL_ARB_multisample: %s\n",
                         display_win32->gl_version / 10,
                         display_win32->gl_version % 10,
                         glGetString (GL_VENDOR),
                         display_win32->hasWglARBPixelFormat ? "yes" : "no",
                         display_win32->hasWglARBCreateContext ? "yes" : "no",
                         display_win32->hasWglEXTSwapControl ? "yes" : "no",
                         display_win32->hasWglOMLSyncControl ? "yes" : "no",
                         display_win32->hasWglARBmultisample ? "yes" : "no"));

      wglMakeCurrent (NULL, NULL);

      _destroy_dummy_gl_context (dummy);
      return TRUE;
    }

#ifdef CDK_WIN32_ENABLE_EGL
  egl_disp = _cdk_win32_get_egl_display (display_win32);

  if (egl_disp == EGL_NO_DISPLAY ||
      !eglInitialize (egl_disp, NULL, NULL))
    {
      if (egl_disp != EGL_NO_DISPLAY)
        {
          eglTerminate (egl_disp);
          egl_disp = EGL_NO_DISPLAY;
        }

      return FALSE;
    }

  display_win32->egl_disp = egl_disp;
  display_win32->have_egl = TRUE;
  display_win32->egl_version = epoxy_egl_version (egl_disp);

  eglBindAPI(EGL_OPENGL_ES_API);

  display_win32->hasEglSurfacelessContext =
    epoxy_has_egl_extension (egl_disp, "EGL_KHR_surfaceless_context");


  CDK_NOTE (OPENGL,
            g_print ("EGL API version %d.%d found\n"
                     " - Vendor: %s\n"
                     " - Checked extensions:\n"
                     "\t* EGL_KHR_surfaceless_context: %s\n",
                     display_win32->egl_version / 10,
                     display_win32->egl_version % 10,
                     eglQueryString (display_win32->egl_disp, EGL_VENDOR),
                     display_win32->hasEglSurfacelessContext ? "yes" : "no"));

  return TRUE;
#endif
}

/* Setup the legacy context after creating it */
static gboolean
_ensure_legacy_gl_context (HDC           hdc,
                           HGLRC         hglrc_legacy,
                           CdkGLContext *share)
{
  if (!wglMakeCurrent (hdc, hglrc_legacy))
    return FALSE;

  if (share != NULL)
    {
      CdkWin32GLContext *context_win32;

      context_win32 = CDK_WIN32_GL_CONTEXT (share);

      return wglShareLists (hglrc_legacy, context_win32->hglrc);
    }

  return TRUE;
}

static HGLRC
_create_gl_context_with_attribs (HDC           hdc,
                                 HGLRC         hglrc_base,
                                 CdkGLContext *share,
                                 int           flags,
                                 int           major,
                                 int           minor,
                                 gboolean     *is_legacy)
{
  HGLRC hglrc;
  CdkWin32GLContext *context_win32;

  /* if we have wglCreateContextAttribsARB(), create a
  * context with the compatibility profile if a legacy
  * context is requested, or when we go into fallback mode
  */
  int profile = *is_legacy ? WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB :
                             WGL_CONTEXT_CORE_PROFILE_BIT_ARB;

  int attribs[] = {
    WGL_CONTEXT_PROFILE_MASK_ARB,   profile,
    WGL_CONTEXT_MAJOR_VERSION_ARB, *is_legacy ? 3 : major,
    WGL_CONTEXT_MINOR_VERSION_ARB, *is_legacy ? 0 : minor,
    WGL_CONTEXT_FLAGS_ARB,          flags,
    0
  };

  if (share != NULL)
    context_win32 = CDK_WIN32_GL_CONTEXT (share);

  hglrc = wglCreateContextAttribsARB (hdc,
                                      share != NULL ? context_win32->hglrc : NULL,
                                      attribs);

  return hglrc;
}

static HGLRC
_create_gl_context (HDC           hdc,
                    CdkGLContext *share,
                    int           flags,
                    int           major,
                    int           minor,
                    gboolean     *is_legacy,
                    gboolean      hasWglARBCreateContext)
{
  /* We need a legacy context for *all* cases */
  HGLRC hglrc_base = wglCreateContext (hdc);
  gboolean success = TRUE;

  /* Save up the HDC and HGLRC that we are currently using, to restore back to it when we are done here  */
  HDC hdc_current = wglGetCurrentDC ();
  HGLRC hglrc_current = wglGetCurrentContext ();

  /* if we have no wglCreateContextAttribsARB(), return the legacy context when all is set */
  if (*is_legacy && !hasWglARBCreateContext)
    {
      if (_ensure_legacy_gl_context (hdc, hglrc_base, share))
        {
          wglMakeCurrent (hdc_current, hglrc_current);
          return hglrc_base;
        }

      success = FALSE;
      goto gl_fail;
    }
  else
    {
      HGLRC hglrc;

      if (!wglMakeCurrent (hdc, hglrc_base))
        {
          success = FALSE;
          goto gl_fail;
        }

      hglrc = _create_gl_context_with_attribs (hdc,
                                               hglrc_base,
                                               share,
                                               flags,
                                               major,
                                               minor,
                                               is_legacy);

      /* return the legacy context we have if it could be setup properly, in case the 3.0+ context creation failed */
      if (hglrc == NULL)
        {
          if (!(*is_legacy))
            {
              /* If we aren't using a legacy context in the beginning, try again with a compatibility profile 3.0 context */
              hglrc = _create_gl_context_with_attribs (hdc,
                                                       hglrc_base,
                                                       share,
                                                       flags,
                                                       0, 0,
                                                       is_legacy);

              *is_legacy = TRUE;
            }

          if (hglrc == NULL)
            {
              if (!_ensure_legacy_gl_context (hdc, hglrc_base, share))
                success = FALSE;
            }

          if (success)
            CDK_NOTE (OPENGL, g_print ("Using legacy context as fallback\n"));
        }

gl_fail:

      if (!success)
        {
          wglMakeCurrent (NULL, NULL);
          wglDeleteContext (hglrc_base);
          return NULL;
        }

      wglMakeCurrent (hdc_current, hglrc_current);

      if (hglrc != NULL)
        {
          wglDeleteContext (hglrc_base);
          return hglrc;
        }

      return hglrc_base;
  }
}

static gboolean
_set_pixformat_for_hdc (HDC              hdc,
                        gint            *best_idx,
                        const gboolean   need_alpha_bits,
                        CdkWin32Display *display)
{
  PIXELFORMATDESCRIPTOR pfd;
  gboolean set_pixel_format_result = FALSE;

  /* one is only allowed to call SetPixelFormat(), and so ChoosePixelFormat()
   * one single time per window HDC
   */
  *best_idx = _get_wgl_pfd (hdc, need_alpha_bits, &pfd, display);
  if (*best_idx != 0)
    set_pixel_format_result = SetPixelFormat (hdc, *best_idx, &pfd);

  /* ChoosePixelFormat() or SetPixelFormat() failed, bail out */
  if (*best_idx == 0 || !set_pixel_format_result)
    return FALSE;

  return TRUE;
}

#ifdef CDK_WIN32_ENABLE_EGL

#define MAX_EGL_ATTRS   30

static gboolean
find_eglconfig_for_window (CdkWin32Display  *display,
                           EGLConfig        *egl_config_out,
                           EGLint           *min_swap_interval_out,
                           gboolean          need_alpha_bits,
                           GError          **error)
{
  EGLint attrs[MAX_EGL_ATTRS];
  EGLint count;
  EGLConfig *configs, chosen_config;

  int i = 0;

  EGLDisplay egl_disp = display->egl_disp;

  attrs[i++] = EGL_CONFORMANT;
  attrs[i++] = EGL_OPENGL_ES2_BIT;
  attrs[i++] = EGL_SURFACE_TYPE;
  attrs[i++] = EGL_WINDOW_BIT;

  attrs[i++] = EGL_COLOR_BUFFER_TYPE;
  attrs[i++] = EGL_RGB_BUFFER;

  attrs[i++] = EGL_RED_SIZE;
  attrs[i++] = 1;
  attrs[i++] = EGL_GREEN_SIZE;
  attrs[i++] = 1;
  attrs[i++] = EGL_BLUE_SIZE;
  attrs[i++] = 1;

  if (need_alpha_bits)
    {
      attrs[i++] = EGL_ALPHA_SIZE;
      attrs[i++] = 1;
    }
  else
    {
      attrs[i++] = EGL_ALPHA_SIZE;
      attrs[i++] = EGL_DONT_CARE;
    }

  attrs[i++] = EGL_NONE;
  g_assert (i < MAX_EGL_ATTRS);

  if (!eglChooseConfig (display->egl_disp, attrs, NULL, 0, &count) || count < 1)
    {
      g_set_error_literal (error, CDK_GL_ERROR,
                           CDK_GL_ERROR_UNSUPPORTED_FORMAT,
                           _("No available configurations for the given pixel format"));
      return FALSE;
    }

  configs = g_new (EGLConfig, count);

  if (!eglChooseConfig (display->egl_disp, attrs, configs, count, &count) || count < 1)
    {
      g_set_error_literal (error, CDK_GL_ERROR,
                           CDK_GL_ERROR_UNSUPPORTED_FORMAT,
                           _("No available configurations for the given pixel format"));
      return FALSE;
    }

  /* Pick first valid configuration i guess? */
  chosen_config = configs[0];

  if (!eglGetConfigAttrib (display->egl_disp, chosen_config,
                           EGL_MIN_SWAP_INTERVAL, min_swap_interval_out))
    {
      g_set_error_literal (error, CDK_GL_ERROR,
                           CDK_GL_ERROR_NOT_AVAILABLE,
                           "Could not retrieve the minimum swap interval");
      g_free (configs);
      return FALSE;
    }

  if (egl_config_out != NULL)
    *egl_config_out = chosen_config;

  g_free (configs);

  return TRUE;
}

#define N_EGL_ATTRS     16

static EGLContext
_create_egl_context (EGLDisplay    display,
                     EGLConfig     config,
                     CdkGLContext *share,
                     int           flags,
                     int           major,
                     int           minor,
                     gboolean     *is_legacy)
{
  EGLContext ctx;
  EGLint context_attribs[N_EGL_ATTRS];
  int i = 0;

  /* ANGLE does not support the GL_OES_vertex_array_object extension, so we need to use ES3 directly */
  context_attribs[i++] = EGL_CONTEXT_CLIENT_VERSION;
  context_attribs[i++] = 3;

  /* Specify the flags */
  context_attribs[i++] = EGL_CONTEXT_FLAGS_KHR;
  context_attribs[i++] = flags;

  context_attribs[i++] = EGL_NONE;
  g_assert (i < N_EGL_ATTRS);

  ctx = eglCreateContext (display,
                          config,
                          share != NULL ? CDK_WIN32_GL_CONTEXT (share)->egl_context
                                        : EGL_NO_CONTEXT,
                          context_attribs);

  if (ctx != EGL_NO_CONTEXT)
    CDK_NOTE (OPENGL, g_message ("Created EGL context[%p]", ctx));

  return ctx;
}
#endif /* CDK_WIN32_ENABLE_EGL */

gboolean
_cdk_win32_gl_context_realize (CdkGLContext *context,
                               GError **error)
{
  CdkGLContext *share = cdk_gl_context_get_shared_context (context);
  CdkWin32GLContext *context_win32 = CDK_WIN32_GL_CONTEXT (context);

  /* These are the real WGL/EGL context items that we will want to use later */
  gboolean debug_bit, compat_bit, legacy_bit;

  /* These are the real WGL context items that we will want to use later */
  HGLRC hglrc;
  gint pixel_format;
  gboolean use_es = FALSE;

  /* request flags and specific versions for core (3.2+) WGL context */
  gint flags = 0;
  gint major = 0;
  gint minor = 0;

#ifdef CDK_WIN32_ENABLE_EGL
  EGLContext egl_context;
#endif

  CdkWindow *window = cdk_gl_context_get_window (context);
  CdkWindowImplWin32 *impl = CDK_WINDOW_IMPL_WIN32 (window->impl);
  CdkWin32Display *win32_display = CDK_WIN32_DISPLAY (cdk_window_get_display (window));

  /*
   * A legacy context cannot be shared with core profile ones, so this means we
   * must stick to a legacy context if the shared context is a legacy context
   */
  if ((_cdk_gl_flags & CDK_GL_LEGACY) != 0 ||
       share != NULL && cdk_gl_context_is_legacy (share))
    legacy_bit = TRUE;

  if ((_cdk_gl_flags & CDK_GL_GLES) != 0 ||
      (share != NULL && cdk_gl_context_get_use_es (share)))
    use_es = TRUE;

  cdk_gl_context_get_required_version (context, &major, &minor);
  debug_bit = cdk_gl_context_get_debug_enabled (context);
  compat_bit = cdk_gl_context_get_forward_compatible (context);

  if (win32_display->have_wgl)
    {
      if (!_set_pixformat_for_hdc (context_win32->gl_hdc,
                                   &pixel_format,
                                   context_win32->need_alpha_bits,
                                   win32_display))
        {
          g_set_error_literal (error, CDK_GL_ERROR,
                               CDK_GL_ERROR_UNSUPPORTED_FORMAT,
                               _("No available configurations for the given pixel format"));

          return FALSE;
        }

      /* if there isn't wglCreateContextAttribsARB(), or if CDK_GL_LEGACY is set, we default to a legacy context */
      legacy_bit = !win32_display->hasWglARBCreateContext ||
                   g_getenv ("CDK_GL_LEGACY") != NULL;

      if (debug_bit)
        flags |= WGL_CONTEXT_DEBUG_BIT_ARB;
      if (compat_bit)
        flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;

      CDK_NOTE (OPENGL,
                g_print ("Creating %s WGL context (version:%d.%d, debug:%s, forward:%s, legacy: %s)\n",
                          compat_bit ? "core" : "compat",
                          major,
                          minor,
                          debug_bit ? "yes" : "no",
                          compat_bit ? "yes" : "no",
                          legacy_bit ? "yes" : "no"));

      hglrc = _create_gl_context (context_win32->gl_hdc,
                                  share,
                                  flags,
                                  major,
                                  minor,
                                  &legacy_bit,
                                  win32_display->hasWglARBCreateContext);

      if (hglrc == NULL)
        {
          g_set_error_literal (error, CDK_GL_ERROR,
                               CDK_GL_ERROR_NOT_AVAILABLE,
                               _("Unable to create a GL context"));
          return FALSE;
        }

      CDK_NOTE (OPENGL,
                g_print ("Created WGL context[%p], pixel_format=%d\n",
                         hglrc,
                         pixel_format));

      context_win32->hglrc = hglrc;
    }

#ifdef CDK_WIN32_ENABLE_EGL
  if (win32_display->have_egl)
    {
      EGLContext ctx;

      if (debug_bit)
        flags |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
      if (compat_bit)
        flags |= EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR;

      CDK_NOTE (OPENGL, g_message ("Creating EGL context version %d.%d (debug:%s, forward:%s, legacy:%s)",
                                   major, minor,
                                   debug_bit ? "yes" : "no",
                                   compat_bit ? "yes" : "no",
                                   legacy_bit ? "yes" : "no"));

      ctx = _create_egl_context (win32_display->egl_disp,
                                 context_win32->egl_config,
                                 share,
                                 flags,
                                 major,
                                 minor,
                                &legacy_bit);

      if (ctx == EGL_NO_CONTEXT)
        {
          g_set_error_literal (error, CDK_GL_ERROR,
                               CDK_GL_ERROR_NOT_AVAILABLE,
                               _("Unable to create a GL context"));

          return FALSE;
        }

      CDK_NOTE (OPENGL,
                g_print ("Created EGL context[%p]\n",
                         ctx));

      context_win32->egl_context = ctx;
      use_es = TRUE;
    }
#endif

  /* set whether we are using GLES */
  cdk_gl_context_set_use_es(context, use_es);

  /* OpenGL does not work with WS_EX_LAYERED enabled, so we need to
   * disable WS_EX_LAYERED when we acquire a valid HGLRC
   */
  impl->suppress_layered++;

  /* if this is the first time a GL context is acquired for the window,
   * disable layered windows by triggering update_style_bits()
   */
  if (impl->suppress_layered == 1)
    _cdk_win32_window_update_style_bits (window);

  /* Ensure that any other context is created with a legacy bit set */
  cdk_gl_context_set_is_legacy (context, legacy_bit);

  return TRUE;
}

CdkGLContext *
_cdk_win32_window_create_gl_context (CdkWindow *window,
                                     gboolean attached,
                                     CdkGLContext *share,
                                     GError **error)
{
  CdkDisplay *display = cdk_window_get_display (window);
  CdkWin32Display *display_win32 = CDK_WIN32_DISPLAY (cdk_window_get_display (window));
  CdkWin32GLContext *context = NULL;
  CdkVisual *visual = cdk_window_get_visual (window);<--- Variable 'visual' can be declared as pointer to const

  gboolean need_alpha_bits = (visual == cdk_screen_get_rgba_visual (cdk_display_get_default_screen (display)));

  /* Acquire and store up the Windows-specific HWND and HDC */
  HDC hdc;

#ifdef CDK_WIN32_ENABLE_EGL
  EGLContext egl_context;
  EGLConfig config;
#endif

  display_win32->gl_hwnd = CDK_WINDOW_HWND (window);
  hdc = GetDC (display_win32->gl_hwnd);

#ifdef CDK_WIN32_ENABLE_EGL
  /* display_win32->hdc_egl_temp should *not* be destroyed here!  It is destroyed at dispose()! */
  display_win32->hdc_egl_temp = hdc;
#endif

  if (!_cdk_win32_display_init_gl (display, need_alpha_bits))
    {
      g_set_error_literal (error, CDK_GL_ERROR,
                           CDK_GL_ERROR_NOT_AVAILABLE,
                           _("No GL implementation is available"));
      return NULL;
    }

#ifdef CDK_WIN32_ENABLE_EGL
  if (display_win32->have_egl && !find_eglconfig_for_window (display_win32, &config,
                                  &display_win32->egl_min_swap_interval, need_alpha_bits,
                                  error))
    return NULL;
#endif

  context = g_object_new (CDK_TYPE_WIN32_GL_CONTEXT,
                          "display", display,
                          "window", window,
                          "shared-context", share,
                          NULL);

  context->need_alpha_bits = need_alpha_bits;
  context->gl_hdc = hdc;

#ifdef CDK_WIN32_ENABLE_EGL
  if (display_win32->have_egl)
    context->egl_config = config;
#endif

  context->is_attached = attached;

  return CDK_GL_CONTEXT (context);
}

gboolean
_cdk_win32_display_make_gl_context_current (CdkDisplay *display,
                                            CdkGLContext *context)
{
  CdkWin32GLContext *context_win32;
  CdkWin32Display *display_win32 = CDK_WIN32_DISPLAY (display);
  CdkWindow *window;
  CdkScreen *screen;

#if CDK_WIN32_ENABLE_EGL
  EGLSurface egl_surface;
#endif

  gboolean do_frame_sync = FALSE;<--- Variable 'do_frame_sync' is assigned a value that is never used.

  if (context == NULL)
    {
      if (display_win32->have_wgl)
        wglMakeCurrent(NULL, NULL);

#ifdef CDK_WIN32_ENABLE_EGL
      else if (display_win32->have_egl)
        eglMakeCurrent (display_win32->egl_disp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
#endif
      return TRUE;
    }

  context_win32 = CDK_WIN32_GL_CONTEXT (context);
  window = cdk_gl_context_get_window (context);

  if (!cdk_gl_context_get_use_es (context))
    {
      if (!wglMakeCurrent (context_win32->gl_hdc, context_win32->hglrc))
        {
          CDK_NOTE (OPENGL,
                    g_print ("Making WGL context current failed\n"));
          return FALSE;
        }

      if (context_win32->is_attached)
        {
          if (display_win32->hasWglEXTSwapControl)
            {
              /* If there is compositing there is no particular need to delay
               * the swap when drawing on the offscreen, rendering to the screen
               * happens later anyway, and its up to the compositor to sync that
               * to the vblank. */
              screen = cdk_window_get_screen (window);
              do_frame_sync = ! cdk_screen_is_composited (screen);

              if (do_frame_sync != context_win32->do_frame_sync)
                {
                  context_win32->do_frame_sync = do_frame_sync;

                  if (do_frame_sync)
                    wglSwapIntervalEXT (1);
                  else
                    wglSwapIntervalEXT (0);
                }
            }
        }
    }

#ifdef CDK_WIN32_ENABLE_EGL
  else
    {
      if (context_win32->is_attached)
        egl_surface = _cdk_win32_window_get_egl_surface (window, context_win32->egl_config, FALSE);
      else
        {
          if (display_win32->hasEglSurfacelessContext)
            egl_surface = EGL_NO_SURFACE;
          else
            egl_surface = _cdk_win32_window_get_egl_surface (window, context_win32->egl_config, TRUE);
        }

      if (!eglMakeCurrent (display_win32->egl_disp,
                           egl_surface,
                           egl_surface,
                           context_win32->egl_context))
        {
          g_warning ("eglMakeCurrent failed");
          return FALSE;
        }

      if (display_win32->egl_min_swap_interval == 0)
        eglSwapInterval (display_win32->egl_disp, 0);
      else
        g_debug ("Can't disable GL swap interval");
    }
#endif

  return TRUE;
}

/**
 * cdk_win32_display_get_wgl_version:
 * @display: a #CdkDisplay
 * @major: (out): return location for the WGL major version
 * @minor: (out): return location for the WGL minor version
 *
 * Retrieves the version of the WGL implementation.
 *
 * Returns: %TRUE if WGL is available
 *
 * Since: 3.16
 */
gboolean
cdk_win32_display_get_wgl_version (CdkDisplay *display,
                                   gint *major,
                                   gint *minor)
{
  CdkWin32Display *display_win32 = NULL;
  g_return_val_if_fail (CDK_IS_DISPLAY (display), FALSE);

  if (!CDK_IS_WIN32_DISPLAY (display))
    return FALSE;

  display_win32 = CDK_WIN32_DISPLAY (display);

  if (!_cdk_win32_display_init_gl (display, FALSE) || !display_win32->have_wgl
#ifdef CDK_WIN32_ENABLE_EGL
      || !display_win32->have_egl
#endif
     )
    return FALSE;

  if (display_win32->have_wgl)<--- Condition 'display_win32->have_wgl' is always true
    {
      if (major != NULL)
        *major = CDK_WIN32_DISPLAY (display)->gl_version / 10;
      if (minor != NULL)
        *minor = CDK_WIN32_DISPLAY (display)->gl_version % 10;
    }

#ifdef CDK_WIN32_ENABLE_EGL
  else if (display_win32->have_egl)
    {
      if (major != NULL)
        *major = CDK_WIN32_DISPLAY (display)->egl_version / 10;
      if (minor != NULL)
        *minor = CDK_WIN32_DISPLAY (display)->egl_version % 10;
    }
#endif

  return TRUE;
}

void
_cdk_win32_window_invalidate_egl_framebuffer (CdkWindow *window)<--- Parameter 'window' can be declared as pointer to const
{
/* If we are using ANGLE, we need to force redraw of the whole Window and its child windows
 *  as we need to re-acquire the EGL surfaces that we rendered to upload to Cairo explicitly,
 *  using cdk_window_invalidate_rect (), when we maximize or restore or use aerosnap
 */
#ifdef CDK_WIN32_ENABLE_EGL
  if (window->gl_paint_context != NULL && cdk_gl_context_get_use_es (window->gl_paint_context))
    {
      CdkWindowImplWin32 *impl = CDK_WINDOW_IMPL_WIN32 (window->impl);

      impl->egl_force_redraw_all = TRUE;
    }
#endif
}