summaryrefslogtreecommitdiff
path: root/Source/DirectFB/gfxdrivers/gl/gl_2d.c
blob: a56f845fb30816bb30d79a458bd5f1050e51f4c2 (plain)
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
/*
   (c) Copyright 2001-2009  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

//#define DIRECT_ENABLE_DEBUG

#include <config.h>

#include <directfb.h>

#include <direct/debug.h>
#include <direct/memcpy.h>
#include <direct/messages.h>

#include <core/state.h>
#include <core/surface.h>
#include <core/system.h>

#include <gfx/convert.h>

#include "gl_2d.h"
#include "gl_gfxdriver.h"


D_DEBUG_DOMAIN( GL__2D, "GL/2D", "OpenGL 2D Acceleration" );

/*
 * State validation flags.
 *
 * There's no prefix because of the macros below.
 */
enum {
     DESTINATION  = 0x00000001,
     SCISSOR      = 0x00000002,
     MATRIX       = 0x00000004,
     RENDER_OPTS  = 0x00000008,

     COLOR_DRAW   = 0x00000010,

     SOURCE       = 0x00000100,
     COLOR_BLIT   = 0x00000200,

     BLENDFUNC    = 0x00010000,

     ALL          = 0x0001031F
};

/*
 * State handling macros.
 */

#define GL_VALIDATE(flags)        do { gdev->v_flags |=  (flags); } while (0)
#define GL_INVALIDATE(flags)      do { gdev->v_flags &= ~(flags); } while (0)

#define GL_CHECK_VALIDATE(flag)   do {                                               \
                                       if ((gdev->v_flags & flag) != flag)           \
                                            gl_validate_##flag( gdrv, gdev, state ); \
                                  } while (0)


/**********************************************************************************************************************/

/*
 * Called by glSetState() to ensure that the destination parameters are properly set
 * for execution of rendering functions.
 */
static inline void
gl_validate_DESTINATION( GLDriverData *gdrv,
                         GLDeviceData *gdev,
                         CardState    *state )
{
     CoreSurface  *surface = state->destination;
     GLBufferData *buffer  = state->dst.handle;

     D_DEBUG_AT( GL__2D, "%s( %p )\n", __FUNCTION__, buffer );

     D_MAGIC_ASSERT( buffer, GLBufferData );

     if (buffer->flags & GLBF_UPDATE_TARGET) {
          glViewport( 0, 0, surface->config.size.w, surface->config.size.h );

          glMatrixMode( GL_PROJECTION );
          glLoadIdentity();
          glOrtho( 0, surface->config.size.w, 0, surface->config.size.h, -1, 1 );

          glMatrixMode( GL_MODELVIEW );
          glLoadIdentity();
          glScalef( 1, -1, 1 );
          glTranslatef( 0, - surface->config.size.h, 0 );

          glShadeModel( GL_FLAT );
          glDisable( GL_LIGHTING );

          glDepthMask( GL_FALSE );
          glDisable( GL_DEPTH_TEST );

          glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
          glDisable( GL_CULL_FACE );

          glEnable( GL_SCISSOR_TEST );

          GL_INVALIDATE( ALL );

          buffer->flags &= ~GLBF_UPDATE_TARGET;
     }

     /* Set the flag. */
     GL_VALIDATE( DESTINATION );
}

/*
 * Called by glSetState() to ensure that the clip is properly set
 * for execution of rendering functions.
 */
static inline void
gl_validate_SCISSOR( GLDriverData *gdrv,
                     GLDeviceData *gdev,
                     CardState    *state )
{
     CoreSurface *surface = state->destination;

     D_DEBUG_AT( GL__2D, "%s()\n", __FUNCTION__ );

     glScissor( state->clip.x1,
                surface->config.size.h - state->clip.y2 - 1,
                state->clip.x2 - state->clip.x1 + 1,
                state->clip.y2 - state->clip.y1 + 1 );

     /* Set the flag. */
     GL_VALIDATE( SCISSOR );
}

/*
 * Called by glSetState() to ensure that the matrix is properly set
 * for execution of rendering functions.
 */
static inline void
gl_validate_MATRIX( GLDriverData *gdrv,
                    GLDeviceData *gdev,
                    CardState    *state )
{
     CoreSurface *surface = state->destination;

     D_DEBUG_AT( GL__2D, "%s()\n", __FUNCTION__ );

     glMatrixMode( GL_MODELVIEW );
     glLoadIdentity();
     glScalef( 1, -1, 1 );
     glTranslatef( 0, - surface->config.size.h, 0 );

     if (state->render_options & DSRO_MATRIX) {
          float m[16] = { 0 };

#define M(n)   (state->matrix[n] / 65536.0f)

          m[0] = M(0); m[4] = M(1); m[ 8] = 0.0f; m[12] = M(2);
          m[1] = M(3); m[5] = M(4); m[ 9] = 0.0f; m[13] = M(5);
          m[2] = 0.0f; m[6] = 0.0f; m[10] = 1.0f; m[14] = 0.0f;
          m[3] = M(6); m[7] = M(7); m[11] = 0.0f; m[15] = M(8);

#undef M

          D_DEBUG_AT( GL__2D, "  -> %7.2f %7.2f %7.2f %7.2f\n", m[0], m[4], m[8], m[12] );
          D_DEBUG_AT( GL__2D, "  -> %7.2f %7.2f %7.2f %7.2f\n", m[1], m[5], m[9], m[13] );
          D_DEBUG_AT( GL__2D, "  -> %7.2f %7.2f %7.2f %7.2f\n", m[2], m[6], m[10], m[14] );
          D_DEBUG_AT( GL__2D, "  -> %7.2f %7.2f %7.2f %7.2f\n", m[3], m[7], m[11], m[15] );

          glMultMatrixf( m );
     }

     /* Set the flag. */
     GL_VALIDATE( MATRIX );
}

/*
 * Called by glSetState() to ensure that the rendering options are properly set
 * for execution of rendering functions.
 */
static inline void
gl_validate_RENDER_OPTS( GLDriverData *gdrv,
                         GLDeviceData *gdev,
                         CardState    *state )
{
     D_DEBUG_AT( GL__2D, "%s()\n", __FUNCTION__ );

     if (state->render_options & DSRO_ANTIALIAS) {
          glEnable( GL_LINE_SMOOTH );
          //glEnable( GL_POLYGON_SMOOTH );
     }
     else {
          glDisable( GL_LINE_SMOOTH );
          //glDisable( GL_POLYGON_SMOOTH );
     }

     /* Set the flag. */
     GL_VALIDATE( RENDER_OPTS );
}

/*
 * Called by glSetState() to ensure that the color is properly set
 * for execution of drawing functions.
 */
static inline void
gl_validate_COLOR_DRAW( GLDriverData *gdrv,
                        GLDeviceData *gdev,
                        CardState    *state )
{
     D_DEBUG_AT( GL__2D, "%s()\n", __FUNCTION__ );

     if (state->drawingflags & DSDRAW_SRC_PREMULTIPLY) {
          int A = state->color.a + 1;

          glColor4ub( (state->color.r * A) >> 8,
                      (state->color.g * A) >> 8,
                      (state->color.b * A) >> 8, state->color.a );
     }
     else
          glColor4ub( state->color.r, state->color.g, state->color.b, state->color.a );

     /* Set the flag. */
     GL_VALIDATE( COLOR_DRAW );

     /* Invalidates blitting color. */
     GL_INVALIDATE( COLOR_BLIT );
}

/*
 * Called by glSetState() to ensure that the source parameters are properly set
 * for execution of blitting functions.
 */
static inline void
gl_validate_SOURCE( GLDriverData *gdrv,
                    GLDeviceData *gdev,
                    CardState    *state )
{
     GLBufferData *buffer = state->src.handle;

     D_DEBUG_AT( GL__2D, "%s( %p )\n", __FUNCTION__, buffer );

     D_MAGIC_ASSERT( buffer, GLBufferData );

     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->texture );

     if (buffer->flags & GLBF_UPDATE_TEXTURE) {
          glTexParameterf( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
          glTexParameterf( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

          glTexParameterf( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
          glTexParameterf( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

          buffer->flags &= ~GLBF_UPDATE_TEXTURE;
     }

     /* Set the flag. */
     GL_VALIDATE( SOURCE );
}

/*
 * Called by glSetState() to ensure that the color is properly set
 * for execution of blitting functions.
 */
static inline void
gl_validate_COLOR_BLIT( GLDriverData *gdrv,
                        GLDeviceData *gdev,
                        CardState    *state )
{
     int r, g, b, a;

     D_DEBUG_AT( GL__2D, "%s()\n", __FUNCTION__ );

     if (state->blittingflags & DSBLIT_COLORIZE) {
          r = state->color.r;
          g = state->color.g;
          b = state->color.b;
     }
     else
          r = g = b = 0xff;

     if (state->blittingflags & DSBLIT_BLEND_COLORALPHA)
          a = state->color.a;
     else
          a = 0xff;

     if (state->blittingflags & DSBLIT_SRC_PREMULTCOLOR) {
          int A = state->color.a + 1;

          r = (r * A) >> 8;
          g = (g * A) >> 8;
          b = (b * A) >> 8;
     }

     glColor4ub( r, g, b, a );

     /* Set the flag. */
     GL_VALIDATE( COLOR_BLIT );

     /* Invalidates drawing color. */
     GL_INVALIDATE( COLOR_DRAW );
}

/*
 * Called by glSetState() to ensure that the blend functions are properly set
 * for execution of drawing and blitting functions.
 */
static inline void
gl_validate_BLENDFUNC( GLDriverData *gdrv,
                       GLDeviceData *gdev,
                       CardState    *state )
{
     GLenum src = GL_ZERO, dst = GL_ZERO;

     D_DEBUG_AT( GL__2D, "%s()\n", __FUNCTION__ );

     switch (state->src_blend) {
          case DSBF_ZERO:
               break;

          case DSBF_ONE:
               src = GL_ONE;
               break;

          case DSBF_SRCCOLOR:
               src = GL_SRC_COLOR;
               break;

          case DSBF_INVSRCCOLOR:
               src = GL_ONE_MINUS_SRC_COLOR;
               break;

          case DSBF_SRCALPHA:
               src = GL_SRC_ALPHA;
               break;

          case DSBF_INVSRCALPHA:
               src = GL_ONE_MINUS_SRC_ALPHA;
               break;

          case DSBF_DESTALPHA:
               src = GL_DST_ALPHA;
               break;

          case DSBF_INVDESTALPHA:
               src = GL_ONE_MINUS_DST_ALPHA;
               break;

          case DSBF_DESTCOLOR:
               src = GL_DST_COLOR;
               break;

          case DSBF_INVDESTCOLOR:
               src = GL_ONE_MINUS_DST_COLOR;
               break;

          case DSBF_SRCALPHASAT:
               src = GL_SRC_ALPHA_SATURATE;
               break;

          default:
               D_BUG( "unexpected src blend function %d", state->src_blend );
     }

     switch (state->dst_blend) {
          case DSBF_ZERO:
               break;

          case DSBF_ONE:
               dst = GL_ONE;
               break;

          case DSBF_SRCCOLOR:
               dst = GL_SRC_COLOR;
               break;

          case DSBF_INVSRCCOLOR:
               dst = GL_ONE_MINUS_SRC_COLOR;
               break;

          case DSBF_SRCALPHA:
               dst = GL_SRC_ALPHA;
               break;

          case DSBF_INVSRCALPHA:
               dst = GL_ONE_MINUS_SRC_ALPHA;
               break;

          case DSBF_DESTALPHA:
               dst = GL_DST_ALPHA;
               break;

          case DSBF_INVDESTALPHA:
               dst = GL_ONE_MINUS_DST_ALPHA;
               break;

          case DSBF_DESTCOLOR:
               dst = GL_DST_COLOR;
               break;

          case DSBF_INVDESTCOLOR:
               dst = GL_ONE_MINUS_DST_COLOR;
               break;

          case DSBF_SRCALPHASAT:
               dst = GL_SRC_ALPHA_SATURATE;
               break;

          default:
               D_BUG( "unexpected dst blend function %d", state->dst_blend );
     }

     glBlendFunc( src, dst );

     /* Set the flag. */
     GL_VALIDATE( BLENDFUNC );
}

/**********************************************************************************************************************/

/*
 * Wait for the blitter to be idle.
 *
 * This function is called before memory that has been written to by the hardware is about to be
 * accessed by the CPU (software driver) or another hardware entity like video encoder (by Flip()).
 * It can also be called by applications explicitly, e.g. at the end of a benchmark loop to include
 * execution time of queued commands in the measurement.
 */
DFBResult
glEngineSync( void *drv, void *dev )
{
     GLDriverData *gdrv = drv;

     D_DEBUG_AT( GL__2D, "%s()\n", __FUNCTION__ );

     if (gdrv->calls > 0) {
          glFinish();

          gdrv->calls = 0;
     }

     return DFB_OK;
}

/*
 * Reset the graphics engine.
 */
void
glEngineReset( void *drv, void *dev )
{
     D_DEBUG_AT( GL__2D, "%s()\n", __FUNCTION__ );
}

/*
 * Start processing of queued commands if required.
 *
 * This function is called before returning from the graphics core to the application.
 * Usually that's after each rendering function. The only functions causing multiple commands
 * to be queued with a single emition at the end are DrawString(), TileBlit(), BatchBlit(),
 * DrawLines() and possibly FillTriangle() which is emulated using multiple FillRectangle() calls.
 */
void
glEmitCommands( void *drv, void *dev )
{
     GLDriverData *gdrv = drv;

     D_DEBUG_AT( GL__2D, "%s()\n", __FUNCTION__ );

     if (gdrv->calls > 523) {
          glFlush();

          gdrv->calls = 1;
     }
}

/**********************************************************************************************************************/

/*
 * Check for acceleration of 'accel' using the given 'state'.
 */
void
glCheckState( void                *drv,
              void                *dev,
              CardState           *state,
              DFBAccelerationMask  accel )
{
     D_DEBUG_AT( GL__2D, "%s( state %p, accel 0x%08x ) <- dest %p [%lu]\n", __FUNCTION__,
                 state, accel, state->destination, state->dst.offset );

     /* Return if the desired function is not supported at all. */
     if (accel & ~(GL_SUPPORTED_DRAWINGFUNCTIONS | GL_SUPPORTED_BLITTINGFUNCTIONS)) {
          D_DEBUG_AT( GL__2D, "  -> unsupported function\n" );
          return;
     }

     /* Return if the destination format is not supported. */
     switch (state->destination->config.format) {
          case DSPF_ARGB:
          case DSPF_RGB32:
               break;

          default:
               D_DEBUG_AT( GL__2D, "  -> unsupported destination format %s\n",
                           dfb_pixelformat_name(state->destination->config.format) );
               return;
     }

     /* Check if drawing or blitting is requested. */
     if (DFB_DRAWING_FUNCTION( accel )) {
          /* Return if unsupported drawing flags are set. */
          if (state->drawingflags & ~GL_SUPPORTED_DRAWINGFLAGS) {
               D_DEBUG_AT( GL__2D, "  -> unsupported drawing flags 0x%08x\n", state->drawingflags );
               return;
          }
     }
     else {
          /* Return if the source format is not supported. */
          switch (state->source->config.format) {
               case DSPF_ARGB:
               case DSPF_RGB32:
                    break;

               default:
                    D_DEBUG_AT( GL__2D, "  -> unsupported source format %s\n",
                                dfb_pixelformat_name(state->source->config.format) );
                    return;
          }

          /* Return if unsupported blitting flags are set. */
          if (state->blittingflags & ~GL_SUPPORTED_BLITTINGFLAGS) {
               D_DEBUG_AT( GL__2D, "  -> unsupported blitting flags 0x%08x\n", state->blittingflags );
               return;
          }
     }

     /* Enable acceleration of the function. */
     state->accel |= accel;

     D_DEBUG_AT( GL__2D, "  => OK\n" );
}

/*
 * Make sure that the hardware is programmed for execution of 'accel' according to the 'state'.
 */
void
glSetState( void                *drv,
            void                *dev,
            GraphicsDeviceFuncs *funcs,
            CardState           *state,
            DFBAccelerationMask  accel )
{
     GLDriverData           *gdrv     = drv;
     GLDeviceData           *gdev     = dev;
     StateModificationFlags  modified = state->mod_hw;

     D_DEBUG_AT( GL__2D, "%s( state %p, accel 0x%08x ) <- dest %p, modified 0x%08x\n", __FUNCTION__,
                 state, accel, state->destination, modified );

     /*
      * 1) Invalidate hardware states
      *
      * Each modification to the hw independent state invalidates one or more hardware states.
      */

     /* Simply invalidate all? */
     if (modified == SMF_ALL) {
          GL_INVALIDATE( ALL );
     }
     else if (modified) {
          if (modified & SMF_DESTINATION)
               GL_INVALIDATE( DESTINATION );

          if (modified & SMF_CLIP)
               GL_INVALIDATE( SCISSOR );

          if (modified & SMF_MATRIX)
               GL_INVALIDATE( MATRIX );

          if (modified & SMF_RENDER_OPTIONS)
               GL_INVALIDATE( MATRIX | RENDER_OPTS );

          if (modified & SMF_COLOR)
               GL_INVALIDATE( COLOR_DRAW | COLOR_BLIT );

          if (modified & SMF_DRAWING_FLAGS)
               GL_INVALIDATE( COLOR_DRAW );

          if (modified & SMF_BLITTING_FLAGS)
               GL_INVALIDATE( COLOR_BLIT );

          if (modified & SMF_SOURCE)
               GL_INVALIDATE( SOURCE );

          if (modified & (SMF_SRC_BLEND | SMF_DST_BLEND))
               GL_INVALIDATE( BLENDFUNC );
     }

     /*
      * 2) Validate hardware states
      *
      * Each function has its own set of states that need to be validated.
      */

     /* Always requiring valid destination, clip, matrix and options... */
     GL_CHECK_VALIDATE( DESTINATION );
     GL_CHECK_VALIDATE( SCISSOR );
     GL_CHECK_VALIDATE( MATRIX );
     GL_CHECK_VALIDATE( RENDER_OPTS );

     /* Depending on the function... */
     switch (accel) {
          case DFXL_FILLRECTANGLE:
          case DFXL_DRAWRECTANGLE:
          case DFXL_DRAWLINE:
          case DFXL_FILLTRIANGLE:
               glDisable( GL_TEXTURE_RECTANGLE_ARB );

               /* ...require valid drawing color. */
               GL_CHECK_VALIDATE( COLOR_DRAW );

               /* If alpha blending is used... */
               if (state->drawingflags & DSDRAW_BLEND) {
                    /* ...require valid blend functions. */
                    GL_CHECK_VALIDATE( BLENDFUNC );

                    glEnable( GL_BLEND );
               }
               else
                    glDisable( GL_BLEND );


               /*
                * 3) Tell which functions can be called without further validation, i.e. SetState()
                *
                * When the hw independent state is changed, this collection is reset.
                */
               state->set = GL_SUPPORTED_DRAWINGFUNCTIONS;
               break;

          case DFXL_BLIT:
          case DFXL_STRETCHBLIT:
               glEnable( GL_TEXTURE_RECTANGLE_ARB );

               /* ...require valid source. */
               GL_CHECK_VALIDATE( SOURCE );


               /* If alpha blending is used... */
               if (state->blittingflags & (DSBLIT_BLEND_ALPHACHANNEL | DSBLIT_BLEND_COLORALPHA)) {
                    /* ...require valid blend functions. */
                    GL_CHECK_VALIDATE( BLENDFUNC );

                    glEnable( GL_BLEND );
               }
               else
                    glDisable( GL_BLEND );


               /* If colorizing or premultiplication of global alpha is used... */
               if (state->blittingflags & (DSBLIT_COLORIZE | DSBLIT_SRC_PREMULTCOLOR | DSBLIT_BLEND_COLORALPHA)) {
                    /* ...require valid color. */
                    GL_CHECK_VALIDATE( COLOR_BLIT );

                    /* Enable texture modulation */
                    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
               }
               else
                    /* Disable texture modulation */
                    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );


               /*
                * 3) Tell which functions can be called without further validation, i.e. SetState()
                *
                * When the hw independent state is changed, this collection is reset.
                */
               state->set = GL_SUPPORTED_BLITTINGFUNCTIONS;
               break;

          default:
               D_BUG( "unexpected drawing/blitting function" );
               break;
     }

     gdrv->blittingflags = state->blittingflags;

     /*
      * 4) Clear modification flags
      *
      * All flags have been evaluated in 1) and remembered for further validation.
      * If the hw independent state is not modified, this function won't get called
      * for subsequent rendering functions, unless they aren't defined by 3).
      */
     state->mod_hw = 0;
}

/**********************************************************************************************************************/

/*
 * Render a filled rectangle using the current hardware state.
 */
bool
glFillRectangle( void *drv, void *dev, DFBRectangle *rect )
{
     GLDriverData *gdrv = drv;

     int x1 = rect->x;
     int y1 = rect->y;
     int x2 = rect->w + x1;
     int y2 = rect->h + y1;

     D_DEBUG_AT( GL__2D, "%s( %4d,%4d-%4dx%4d )\n", __FUNCTION__, DFB_RECTANGLE_VALS( rect ) );

     glBegin( GL_QUADS );

     glVertex2i( x1, y1 );
     glVertex2i( x2, y1 );
     glVertex2i( x2, y2 );
     glVertex2i( x1, y2 );

     glEnd();


     gdrv->calls += 1 + rect->w * rect->h / (23 * 42);

     return true;
}

/*
 * Render a rectangle outline using the current hardware state.
 */
bool
glDrawRectangle( void *drv, void *dev, DFBRectangle *rect )
{
     GLDriverData *gdrv = drv;

     int x1 = rect->x + 1;
     int y1 = rect->y;
     int x2 = rect->x + rect->w;
     int y2 = rect->y + rect->h - 1;

     D_DEBUG_AT( GL__2D, "%s( %4d,%4d-%4dx%4d )\n", __FUNCTION__, DFB_RECTANGLE_VALS( rect ) );

     glBegin( GL_LINE_LOOP );

     glVertex2i( x1, y1 );
     glVertex2i( x2, y1 );
     glVertex2i( x2, y2 );
     glVertex2i( x1, y2 );

     glEnd();


     gdrv->calls++;

     return true;
}

/*
 * Render a line using the current hardware state.
 */
bool
glDrawLine( void *drv, void *dev, DFBRegion *line )
{
     GLDriverData *gdrv = drv;

     int x1 = line->x1;
     int y1 = line->y1;
     int x2 = line->x2;
     int y2 = line->y2;

     D_DEBUG_AT( GL__2D, "%s( %4d,%4d-%4d,%4d )\n", __FUNCTION__, DFB_REGION_VALS( line ) );

     glBegin( GL_LINES );

     glVertex2i( x1, y1 );
     glVertex2i( x2, y2 );

     glEnd();


     gdrv->calls++;

     return true;
}

/*
 * Render a line using the current hardware state.
 */
bool
glFillTriangle( void *drv, void *dev, DFBTriangle *tri )
{
     GLDriverData *gdrv = drv;

     D_DEBUG_AT( GL__2D, "%s( %4d,%4d-%4d,%4d-%4d,%4d )\n", __FUNCTION__,
                 tri->x1, tri->y1, tri->x2, tri->y2, tri->x3, tri->y3 );

     glBegin( GL_TRIANGLES );

     glVertex2i( tri->x1, tri->y1 );
     glVertex2i( tri->x2, tri->y2 );
     glVertex2i( tri->x3, tri->y3 );

     glEnd();


     gdrv->calls += 23;

     return true;
}

/*
 * Blit a rectangle using the current hardware state.
 */
bool
glBlit( void *drv, void *dev, DFBRectangle *srect, int dx, int dy )
{
     GLDriverData *gdrv = drv;

     int x1 = dx;
     int y1 = dy;
     int x2 = srect->w + x1;
     int y2 = srect->h + y1;

     int tx1 = srect->x;
     int ty1 = srect->y;
     int tx2 = srect->w + tx1;
     int ty2 = srect->h + ty1;

     D_DEBUG_AT( GL__2D, "%s( %4d,%4d-%4dx%4d <- %4d,%4d )\n", __FUNCTION__,
                 dx, dy, srect->w, srect->h, srect->x, srect->y );

     /* Might also use GL_TEXTURE matrix, but isn't this less overhead in state management? */
     if (gdrv->blittingflags & DSBLIT_ROTATE180) {
          int tmp;

          tmp = tx1; tx1 = tx2; tx2 = tmp;
          tmp = ty1; ty1 = ty2; ty2 = tmp;
     }

     glBegin( GL_QUADS );

     glTexCoord2i( tx1, ty1 );
     glVertex2i( x1, y1 );

     glTexCoord2i( tx2, ty1 );
     glVertex2i( x2, y1 );

     glTexCoord2i( tx2, ty2 );
     glVertex2i( x2, y2 );

     glTexCoord2i( tx1, ty2 );
     glVertex2i( x1, y2 );

     glEnd();


     gdrv->calls += 1 + srect->w * srect->h / (23 * 42);

     return true;
}

/*
 * Blit a scaled rectangle using the current hardware state.
 */
bool
glStretchBlit( void *drv, void *dev, DFBRectangle *srect, DFBRectangle *drect )
{
     GLDriverData *gdrv = drv;

     int x1 = drect->x;
     int y1 = drect->y;
     int x2 = drect->w + x1;
     int y2 = drect->h + y1;

     int tx1 = srect->x;
     int ty1 = srect->y;
     int tx2 = srect->w + tx1;
     int ty2 = srect->h + ty1;

     D_DEBUG_AT( GL__2D, "%s( %4d,%4d-%4dx%4d <- %4d,%4d-%4dx%4d )\n", __FUNCTION__,
                 DFB_RECTANGLE_VALS( drect ), DFB_RECTANGLE_VALS( srect ) );

     /* Might also use GL_TEXTURE matrix, but isn't this less overhead in state management? */
     if (gdrv->blittingflags & DSBLIT_ROTATE180) {
          int tmp;

          tmp = tx1; tx1 = tx2; tx2 = tmp;
          tmp = ty1; ty1 = ty2; ty2 = tmp;
     }

     glBegin( GL_QUADS );

     glTexCoord2i( tx1, ty1 );
     glVertex2i( x1, y1 );

     glTexCoord2i( tx2, ty1 );
     glVertex2i( x2, y1 );

     glTexCoord2i( tx2, ty2 );
     glVertex2i( x2, y2 );

     glTexCoord2i( tx1, ty2 );
     glVertex2i( x1, y2 );

     glEnd();


     gdrv->calls += 1 + drect->w * drect->h / (23 * 42);

     return true;
}