File: ARTContext.m

package info (click to toggle)
gnustep-back 0.14.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,416 kB
  • ctags: 1,025
  • sloc: objc: 35,429; ansic: 6,048; makefile: 212; sh: 116
file content (181 lines) | stat: -rw-r--r-- 4,641 bytes parent folder | download
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
/*
   Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.

   Author:  Alexander Malmberg <alexander@malmberg.org>

   This file is part of GNUstep.

   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; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/


#include <Foundation/NSDebug.h>
#include <Foundation/NSDictionary.h>

#include "ARTGState.h"
#include "blit.h"
#include "ftfont.h"

#ifndef RDS
#include "x11/XWindowBuffer.h"
#endif

// Could use NSSwapInt() instead
static unsigned int flip_bytes(unsigned int i)
{
  return ((i >> 24) & 0xff)
	|((i >>  8) & 0xff00)
	|((i <<  8) & 0xff0000)
	|((i << 24) & 0xff000000);
}

static int byte_order(void)
{
  union
  {
    unsigned int i;
    char c;
  } foo;
  foo.i = 1;
  return foo.c != 1;
}

@implementation ARTContext

+ (void)initializeBackend
{
  NSDebugLLog(@"back-art",@"Initializing libart/freetype backend");

  [NSGraphicsContext setDefaultContextClass: [ARTContext class]];
  [FTFontInfo initializeBackend];
}

+ (Class) GStateClass
{
  return [ARTGState class];
}

- (void) setupDrawInfo
{
#ifdef RDS
  {
    RDSServer *s = (RDSServer *)server;
    int bpp;
    int red_mask, green_mask, blue_mask;

    [s getPixelFormat: &bpp masks: &red_mask : &green_mask : &blue_mask];
    artcontext_setup_draw_info(&DI, red_mask, green_mask, blue_mask, bpp);
  }
#else
  {
    Display *d = [(XGServer *)server xDisplay];
    int bpp;
    Visual *visual;
    XVisualInfo template;
    XVisualInfo *visualInfo;
    int numMatches;
    XImage *i;

    /*
    We need a visual that we can generate pixel values for by ourselves.
    Thus, we try to find a DirectColor or TrueColor visual. If that fails,
    we use the default visual and hope that it's usable.
    */
    template.class = DirectColor;
    visualInfo = XGetVisualInfo(d, VisualClassMask, &template, &numMatches);
    if (!visualInfo)
      {
        template.class = TrueColor;
        visualInfo = XGetVisualInfo(d, VisualClassMask, &template, &numMatches);
      }
    if (visualInfo)
      {
        visual = visualInfo->visual;
        bpp = visualInfo->depth;
        XFree(visualInfo);
      }
    else
      {
        visual = DefaultVisual(d, DefaultScreen(d));
        bpp = DefaultDepth(d, DefaultScreen(d));
      }
    
    i = XCreateImage(d, visual, bpp, ZPixmap, 0, NULL, 8, 8, 8, 0);
    bpp = i->bits_per_pixel;
    XDestroyImage(i);

    /* If the server doesn't have the same endianness as we do, we need
       to flip the masks around (well, at least sometimes; not sure
       what'll really happen for 15/16bpp modes).  */
    {
      int us = byte_order(); /* True iff we're big-endian.  */
      int them = ImageByteOrder(d); /* True iff the server is big-endian.  */
      if (us != them)
        {
          visual->red_mask = flip_bytes(visual->red_mask);
          visual->green_mask = flip_bytes(visual->green_mask);
          visual->blue_mask = flip_bytes(visual->blue_mask);
        }
    }

    /* Only returns if the visual was usable.  */
    artcontext_setup_draw_info(&DI, visual->red_mask, visual->green_mask,
			       visual->blue_mask, bpp);
  }
#endif
}

- (void) flushGraphics
{ 
  /* TODO: _really_ flush? (ie. force updates and wait for shm completion?) */
#ifndef RDS
  XFlush([(XGServer *)server xDisplay]);
#endif
}

#ifndef RDS
+ (void) _gotShmCompletion: (Drawable)d
{
  [XWindowBuffer _gotShmCompletion: d];
}

- (void) gotShmCompletion: (Drawable)d
{
  [XWindowBuffer _gotShmCompletion: d];
}
#endif

/* Private backend methods */
+ (void) handleExposeRect: (NSRect)rect forDriver: (void *)driver
{
  [(XWindowBuffer *)driver _exposeRect: rect];
}

@end

@implementation ARTContext (ops)
- (void) GSSetDevice: (void*)device : (int)x : (int)y
{
  [self setupDrawInfo];
  [(ARTGState *)gstate GSSetDevice: device : x : y];
}

- (void) GSCurrentDevice: (void **)device : (int *)x : (int *)y
{
  [(ARTGState *)gstate GSCurrentDevice: device : x : y];
}
@end