summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/render/x11/imageDGAFull.cpp
blob: 6e07b658210ccf75d7318515561b6525590a2499 (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
/*
  xfree 4.0 dga fullscreen mode
  Copyright (C) 2000  Martin Vogt, Christian Gerlach

  This program 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.

  For more information look at the file COPYRIGHT in this package

 */


#include "imageDGAFull.h"

#include <iostream>

using namespace std;

ImageDGAFull::ImageDGAFull() {

  m_iMode = -1;
  m_bIsActive = false;
  lSupport=false;
  m_pxWindow = NULL;
  m_iImageMode = _IMAGE_NONE;
  ditherWrapper=NULL;
  supportedModes = _IMAGE_NONE;
  setIdentifier("DGA");
}


ImageDGAFull::~ImageDGAFull() {
  if (ditherWrapper != NULL) {
    delete ditherWrapper;
  }
}

void ImageDGAFull::init(XWindow *xWindow, YUVPicture*)
{
  int uid;

  m_pxWindow = xWindow;
  if (ditherWrapper == NULL) {
    ditherWrapper=new DitherWrapper(xWindow->depth,
				    xWindow->redMask,
				    xWindow->greenMask,
				    xWindow->blueMask,
				    xWindow->pixel);
  }

#ifndef X11_DGA2
  return;
#endif

#ifdef X11_DGA2
  m_pDGAModes=NULL;
  m_iNumberModes = 0;

  m_iVideoWidth = xWindow->width;
  m_iVideoHeight = xWindow->height;
  uid=getuid();
  if (uid != 0) {
    //cout << "you are :"<<uid<<" and not root(0). DGA 2.0 needs root"<<endl;
    return;
  }
  
  if ((m_pDisplay =xWindow->display)==NULL ) {
    fprintf( stderr, " cannot connect to X server %s\n", XDisplayName(NULL));
    return;
  }

  m_iScreen = DefaultScreen(xWindow->display);


  if (!XF86DGAQueryVersion(xWindow->display, 
			   &m_iMajorVersion, &m_iMinorVersion)) { 
    fprintf(stderr, "Unable to query video extension version\n");
    return ;
  }
  printf("DGA version %d.%d detected!\n", m_iMajorVersion, m_iMinorVersion);
  
  // Fail if the extension version in the server is too old
  if (m_iMajorVersion < DGA_MINMAJOR || 
      (m_iMajorVersion == DGA_MINMAJOR && m_iMinorVersion < DGA_MINMINOR)) {
    fprintf(stderr, "Xserver is running an old XFree86-DGA version"
	    " (%d.%d)\n", m_iMajorVersion, m_iMinorVersion);
    fprintf(stderr, "Minimum required version is %d.%d\n",
	    DGA_MINMAJOR, DGA_MINMINOR);
    return ;
  }
  
  if (!XF86DGAQueryExtension(m_pDisplay, &m_iEventBase, &m_iErrorBase)) {
    fprintf(stderr, "Unable to query video extension information\n");
    return ;
  }
  printf("Event base %d\n", m_iEventBase);
  printf("Error base %d\n", m_iErrorBase);

  lSupport=true;
  supportedModes = _IMAGE_FULL;
#endif
}
	
int ImageDGAFull::support() {
  return lSupport;
}


int ImageDGAFull::openImage(int mode) {
#ifdef X11_DGA2
  int width, bank, ram;
  m_bAllowZoom = IS_DOUBLE(mode);
  m_iImageMode = mode;

  /* Open access to the framebuffer */
  if ( ! XDGAOpenFramebuffer(m_pDisplay,m_iScreen) ) {
    return(false);
  }

  findMode(m_pxWindow->width, m_pxWindow->height, m_pxWindow->depth);
  m_pDevice = XDGASetMode(m_pDisplay, m_iScreen, m_pDGAModes[m_iMode].num);
  

  XDGASelectInput(m_pDisplay, m_iScreen,
		  KeyPressMask | ButtonPressMask | PointerMotionMask);

  XF86DGAGetVideo(m_pDisplay,m_iScreen,&m_pAddr,&width,&bank,&ram);


  if(bank < (ram * 1024)) {
    XF86DGASetVidPage(m_pxWindow->display, 
		      DefaultScreen(m_pxWindow->display), 0);
  }

  XF86DGASetViewPort(m_pxWindow->display, 
		     DefaultScreen(m_pxWindow->display),0,0);


  printf("Offset:%8x\n",m_iOffsetScreen);
  m_pStartAddr = m_pAddr + m_iOffsetScreen;
  m_iOffsetLine = (m_iBytesPerLine - m_iBytesPerRow) / m_iBytesPerPixel; 
  cout << "LineOffset:     " << m_iOffsetLine << endl;

  // Clear the screen
  memset(m_pAddr, 0, m_iBytesPerLine * m_iScreenHeight);
  /*  char *pos = m_pStartAddr;
  int end = (m_bZoom) ? 2*m_iVideoHeight : m_iVideoHeight;
  for (int line=0 ; line<end ; line++) {
    memset(pos, 80, m_iBytesPerRow);
    pos += m_iBytesPerRow + m_iOffsetLine * m_iBytesPerPixel;
  }
  sleep(2);*/

  m_bIsActive = true;
#endif
  return true;
}


int ImageDGAFull::closeImage() {
#ifdef X11_DGA2
  m_bIsActive = false;
  stop();

  // delete resources
  if (m_pDGAModes != NULL) {
    delete m_pDGAModes;
    m_pDGAModes=NULL;
  }
#endif
  return true;
}


unsigned char* ImageDGAFull::address() { 
  return (unsigned char*) m_pStartAddr; 
}
  

int  ImageDGAFull::offset() { 
  return m_iOffsetLine;
}


void ImageDGAFull::ditherImage(YUVPicture* pic) {

	int useMode = (m_bZoom) ? m_iImageMode : m_iImageMode & (!_IMAGE_DOUBLE);
	
    ditherWrapper->doDither(pic,m_pxWindow->depth,useMode,
							address(),offset());
}


void ImageDGAFull::putImage() {
  
  if (event())
    closeImage();
}


int ImageDGAFull::findMode(int width, int height, int bpp) {
#ifdef X11_DGA2
  int minBorder = INT_MAX;
  int yBorder=0;
  int border;
  
  // TODO: also check the y-axis
  
  m_iMode = -1;
  m_iNumberModes = 0;
  m_pDGAModes = XDGAQueryModes(m_pDisplay, m_iScreen, &m_iNumberModes);
  printf("Number modes: %d\n", m_iNumberModes);
  
  for (int count=0 ; count<m_iNumberModes ; count++) {

    
    if (m_pDGAModes[count].depth != bpp)
      continue;

    printf("Mode: %d  %dx%d  \t bpp %d\n",
	   count, 
	   m_pDGAModes[count].viewportWidth,
	   m_pDGAModes[count].viewportHeight, 
	   m_pDGAModes[count].bitsPerPixel);
    
    // test normal video
    border = m_pDGAModes[count].viewportWidth - width;
    if ((border >= 0) && (border < minBorder)) {
      minBorder = border;
      m_iMode = count;
      m_bZoom = false;
      yBorder = m_pDGAModes[count].viewportHeight - height;
    }
    
    // test zoomed video
    if (m_bAllowZoom) {
      border = m_pDGAModes[count].viewportWidth - 2 * width;
      if ((border >= 0) && (border < minBorder)) {
  	minBorder = border;
  	m_iMode = count;
  	m_bZoom = true;
  	yBorder = m_pDGAModes[count].viewportHeight-2*height;
      }	
    }
  }
  
  if (m_iMode != -1) {
    m_iScreenWidth = m_pDGAModes[m_iMode].viewportWidth;
    m_iScreenHeight = m_pDGAModes[m_iMode].viewportHeight;
    
    m_iBytesPerPixel = m_pDGAModes[m_iMode].bitsPerPixel / 8;
    m_iBytesPerLine = m_pDGAModes[m_iMode].bytesPerScanline;
    m_iBytesPerRow = width * m_iBytesPerPixel;
    if (m_bZoom) {
      m_iBytesPerRow += m_iBytesPerRow;
    }

    m_iOffsetScreen = minBorder * (m_iBytesPerPixel / 2) + 
      (yBorder / 2) * m_iBytesPerLine;
  }
  
  cout << "Best Mode:      " << m_iMode << endl;
  cout << "Border Size:    " << minBorder / 2 << endl;
  cout << "Zoom:           " << m_bZoom << endl;
  cout << "Bytes per Line: " << m_iBytesPerLine << endl;
  cout << "Bytes per Row:  " << m_iBytesPerRow << endl;
  cout << "Bytes per Pixel:" << m_iBytesPerPixel << endl;
  cout << "Total offset:   " << m_iOffsetScreen << endl;
#endif  
  return (m_iMode != -1); 
}



int ImageDGAFull::event() {
   XEvent event;
   return XCheckTypedEvent(m_pDisplay, ButtonPress + m_iEventBase, &event);
}

void ImageDGAFull::stop() {
#ifdef X11_DGA2
  m_bIsActive = false;
  XF86DGADirectVideo(m_pDisplay, m_iScreen, 0);
  
  XUngrabPointer(m_pDisplay, CurrentTime);
  XUngrabKeyboard(m_pDisplay, CurrentTime);
#endif
}