summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/render/renderMachine.cpp
blob: c632793043db570621394b0d2f417977a32e0817 (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
 /*
  puts the yuv images onto a surface
  Copyright (C) 2000  Martin Vogt

  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 "renderMachine.h"

#include <iostream>

using namespace std;


RenderMachine::RenderMachine() {

#ifndef SDL_WRAPPER 
  surface=new X11Surface();
#endif
#ifdef SDL_WRAPPER 
  surface=new SDLSurface();
#endif


  pictureArray=NULL;

  startTime=new TimeStamp();
  endTime=new TimeStamp();

  initialMode = _IMAGE_DESK;
}


RenderMachine::~RenderMachine() {


  closeWindow();
  delete surface;

  delete startTime;
  delete endTime;

}






void RenderMachine::waitRestTime() {
  endTime->gettimeofday();
  startTime->minus(endTime,endTime);
  endTime->waitForIt();

}

int RenderMachine::x11WindowId()
{
	return surface->x11WindowId();
}


int RenderMachine::openWindow(int width, 
			      int height,const char *title) {
if (surface->open(width,height,title)) {
    pictureArray=new PictureArray(width, height);
    
    return switchToMode(initialMode);
  }
  return false;
}


void RenderMachine::closeWindow() {
  
  if (surface->isOpen()==false) {
    return;
  }
  if (pictureArray != NULL) {
    delete pictureArray;
    pictureArray=NULL;
  }
  surface->close();
}

/**
   important method. This is our only way to switch from
   fullscreen back to the desktop screen.
   This method is called if the video end (normal or by user request)
   We dont have a callback, thus after the image stops, we would
   never get events.
*/
void RenderMachine::flushWindow() {

  // we always switch back to desk mode.
  if (IS_FULL(surface->getImageMode())) {
    switchToMode(surface->getImageMode() ^ _IMAGE_DESK ^ _IMAGE_FULL);
  }


}




void RenderMachine::putImage(YUVPicture* pic,
			     TimeStamp* waitTime,
			     TimeStamp* ) {
  if (pic == NULL) {
    cout << "pic is null"<<endl;
    return;
  }
  startTime->gettimeofday();
  startTime->addOffset(waitTime);

  // need dither?
  surface->dither(pic);
 
  int nextMode;
  if (surface->checkEvent(&nextMode) == true) {
    switchToMode(nextMode);
  }
  surface->putImage(pic);
  waitRestTime();
}


int RenderMachine::switchToMode(int mode) {
  if (surface->getImageMode() != mode) {
    surface->closeImage();
    if (mode != _IMAGE_NONE) {
      surface->openImage(mode);
    }
    
    else {
      cout << "no imageMode, no open, that's life"<<endl;
      return false;
    }
  }
  return true;
}



PictureArray* RenderMachine::lockPictureArray() {
  return pictureArray;
}


void RenderMachine::unlockPictureArray(PictureArray* pictureArray) {

  // chance to switch mode
  
  
  // put picture out
  if (surface->getImageMode() != _IMAGE_NONE) {
    YUVPicture* pic=pictureArray->getYUVPictureCallback();
    if (pic != NULL) {
      TimeStamp* waitTime=pic->getWaitTime();
      TimeStamp* earlyTime=pic->getEarlyTime();
      putImage(pic,waitTime,earlyTime);
    }
  } else {
    cout << "no mode selected"<<endl;
  }
}


void RenderMachine::config(const char* key, 
			   const char* value,void* user_data) {
  if (strcmp(key,"getDepth")==0) {
    int* val=(int*)user_data;
    *val=surface->getDepth();
  }
  if (surface != NULL) {
    int mode = surface->getImageMode();
    if (strcmp(key,"toggleFullscreen")==0) {
      if (surface->findImage(mode ^ _IMAGE_FULL) != NULL) {
	if (surface->isOpen())
	  switchToMode(mode ^ _IMAGE_FULL);
	else
	  initialMode = _IMAGE_FULL;
      }
    }
    if (strcmp(key,"toggleDouble")==0) {
      if (surface->findImage(mode ^ _IMAGE_DOUBLE) != NULL) {
	if (surface->isOpen())
	  switchToMode(mode ^ _IMAGE_DOUBLE);
	else
	  initialMode = _IMAGE_DOUBLE;
      }
    }
  }

  surface->config(key,value,user_data);
}