| 1 |
|
| 2 |
#include <qapplication.h> |
| 3 |
#include <qwidget.h> |
| 4 |
#include <qpaintdevice.h> |
| 5 |
|
| 6 |
#include "qtrenderer.h" |
| 7 |
|
| 8 |
#include <renderer.h> |
| 9 |
#include <mmx.h> |
| 10 |
#include <avm_except.h> |
| 11 |
|
| 12 |
//#define QT_CLEAN_NAMESPACE |
| 13 |
|
| 14 |
#include <unistd.h> |
| 15 |
#ifdef __FreeBSD__ |
| 16 |
#include <machine/param.h> |
| 17 |
#include <sys/types.h> |
| 18 |
#endif |
| 19 |
#include <sys/ipc.h> |
| 20 |
#include <sys/shm.h> |
| 21 |
|
| 22 |
#include <pthread.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <stdio.h> |
| 26 |
|
| 27 |
#define __MODULE__ "Shm renderer" |
| 28 |
inline static void qSafeXDestroyImage( XImage *x ) |
| 29 |
{ |
| 30 |
if ( x->data ) { |
| 31 |
free( x->data ); |
| 32 |
x->data = 0; |
| 33 |
} |
| 34 |
XDestroyImage( x ); |
| 35 |
} |
| 36 |
|
| 37 |
ShmRenderer::ShmRenderer(QWidget* w, int x, int y, int _xpos, int _ypos) |
| 38 |
: xshmimg(0), xshmpm(0), xpos(_xpos), ypos(_ypos), m_w(x), m_h(y), |
| 39 |
pic_w(x), pic_h(y), dev((QPaintDevice*)w), xshminit(false) |
| 40 |
{ |
| 41 |
alloc(); |
| 42 |
} |
| 43 |
|
| 44 |
ShmRenderer::~ShmRenderer() |
| 45 |
{ |
| 46 |
free(); |
| 47 |
} |
| 48 |
|
| 49 |
void ShmRenderer::alloc() |
| 50 |
{ |
| 51 |
int major, minor; |
| 52 |
Bool pixmaps_ok; |
| 53 |
Display *dpy = dev->x11Display(); |
| 54 |
int dd = dev->x11Depth(); |
| 55 |
Visual *vis = (Visual*)dev->x11Visual(); |
| 56 |
// printf("Creating SHM renderer, width %d, height %d\n", pic_w, pic_h); |
| 57 |
// printf("Server vendor %s, release %d\n", ServerVendor(dpy), VendorRelease(dpy)); |
| 58 |
|
| 59 |
try |
| 60 |
{ |
| 61 |
XGCValues xcg; |
| 62 |
xcg.graphics_exposures=false; |
| 63 |
gc=XCreateGC(dpy, dev->handle(), GCGraphicsExposures, &xcg); |
| 64 |
if ( !XShmQueryVersion(dpy, &major, &minor, &pixmaps_ok) ) |
| 65 |
throw FATAL("MIT SHM extension not supported"); |
| 66 |
|
| 67 |
bool ok; |
| 68 |
int _pic_w=((pic_w+m_w-1)/m_w)*m_w; |
| 69 |
int _pic_h=((pic_h+m_h-1)/m_h)*m_h; |
| 70 |
xshminfo.shmid = shmget( IPC_PRIVATE, _pic_w*_pic_h*4, |
| 71 |
IPC_CREAT | 0777 ); |
| 72 |
ok = xshminfo.shmid != -1; |
| 73 |
if(!ok) |
| 74 |
throw FATAL("Can't get shared memory segment"); |
| 75 |
|
| 76 |
xshminfo.shmaddr = (char*)shmat( xshminfo.shmid, 0, 0 ); |
| 77 |
ok = xshminfo.shmaddr != 0; |
| 78 |
xshminfo.readOnly = FALSE; |
| 79 |
|
| 80 |
if ( !ok ) |
| 81 |
throw FATAL("Can't attach shared memory segment"); |
| 82 |
|
| 83 |
ok = XShmAttach( dpy, &xshminfo ); |
| 84 |
if ( !ok ) |
| 85 |
throw FATAL("XShmAttach failed"); |
| 86 |
|
| 87 |
xshmimg = XShmCreateImage( dpy, vis, dd, ZPixmap, xshminfo.shmaddr, &xshminfo, _pic_w, _pic_h ); |
| 88 |
|
| 89 |
if ( !xshmimg ) |
| 90 |
throw FATAL("Can't create shared image"); |
| 91 |
} |
| 92 |
catch(...) |
| 93 |
{ |
| 94 |
xshmimg = 0; |
| 95 |
if ( xshminfo.shmaddr ) |
| 96 |
shmdt( xshminfo.shmaddr ); |
| 97 |
if ( xshminfo.shmid != -1 ) |
| 98 |
shmctl( xshminfo.shmid, IPC_RMID, 0 ); |
| 99 |
throw; |
| 100 |
} |
| 101 |
} |
| 102 |
int ShmRenderer::free() |
| 103 |
{ |
| 104 |
printf("Free()\n"); |
| 105 |
if ( xshmimg == 0 ) |
| 106 |
return 0; |
| 107 |
Display *dpy = dev->x11Display(); |
| 108 |
XSync(dpy, false); |
| 109 |
if ( xshmpm ) { |
| 110 |
XFreePixmap( dpy, xshmpm ); |
| 111 |
xshmpm = 0; |
| 112 |
} |
| 113 |
XShmDetach( dpy, &xshminfo ); xshmimg->data = 0; |
| 114 |
qSafeXDestroyImage( xshmimg ); xshmimg = 0; |
| 115 |
shmdt( xshminfo.shmaddr ); |
| 116 |
shmctl( xshminfo.shmid, IPC_RMID, 0 ); |
| 117 |
XFreeGC(dpy, gc); |
| 118 |
|
| 119 |
xshminfo.shmaddr=0; |
| 120 |
xshminfo.shmid=0; |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
int ShmRenderer::resize(int& new_w, int& new_h) |
| 125 |
{ |
| 126 |
if(new_w<0)return -1; |
| 127 |
if(new_h<0)return -1; |
| 128 |
new_w&=(~7); |
| 129 |
new_h&=(~7); |
| 130 |
int xratio=(new_w+m_w-1)/m_w; |
| 131 |
int yratio=(new_h+m_h-1)/m_h; |
| 132 |
int old_xratio=(pic_w+m_w-1)/m_w; |
| 133 |
int old_yratio=(pic_h+m_h-1)/m_h; |
| 134 |
printf("New size: %d %d\n", new_w, new_h); |
| 135 |
pic_w=new_w; |
| 136 |
pic_h=new_h; |
| 137 |
if((xratio!=old_xratio)||(yratio!=old_yratio)) |
| 138 |
{ |
| 139 |
mutex.Lock(); |
| 140 |
free(); |
| 141 |
alloc(); |
| 142 |
mutex.Unlock(); |
| 143 |
} |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
static void copy_deinterlace_24(void* outpic, const void* inpic, int xdim, int height) |
| 148 |
{ |
| 149 |
#ifdef ARCH_X86_32 |
| 150 |
for(int i=0; i<height; i++) |
| 151 |
{ |
| 152 |
char* outp=((char*)outpic)+i*xdim; |
| 153 |
const char* inp=((const char*)inpic)+i*xdim; |
| 154 |
if((i==0) || (i==height-1)) |
| 155 |
{ |
| 156 |
memcpy(outp, inp, xdim); |
| 157 |
continue; |
| 158 |
} |
| 159 |
int count=xdim/8; |
| 160 |
__asm__ __volatile__ ( |
| 161 |
"movl %2, %%esi\n\t" |
| 162 |
"movl %3, %%edi\n\t" |
| 163 |
"pxor %%mm3, %%mm3\n\t" |
| 164 |
"1:\n\t" |
| 165 |
"movq (%%ecx, %%esi), %%mm0\n\t" |
| 166 |
"movq (%%ecx), %%mm1\n\t" |
| 167 |
"movq %%mm1, %%mm2\n\t" |
| 168 |
"pavgb %%mm0, %%mm2\n\t" |
| 169 |
"psadbw %%mm2, %%mm3\n\t" |
| 170 |
"movd %%mm3, %%eax\n\t" |
| 171 |
"cmpl $48, %%eax\n\t" |
| 172 |
"jb 2f\n\t" |
| 173 |
"pavgb %%mm1, %%mm0\n\t" |
| 174 |
"2:\n\t" |
| 175 |
"movq %%mm0, (%%edx)\n\t" |
| 176 |
"addl $8, %%ecx\n\t" |
| 177 |
"addl $8, %%edx\n\t" |
| 178 |
"decl %%edi\n\t" |
| 179 |
"jnz 1b\n\t" |
| 180 |
: |
| 181 |
: "c" (inp-xdim), "d" (outp), "r" (xdim), "r" (count) |
| 182 |
: "esi", "edi" |
| 183 |
); |
| 184 |
} |
| 185 |
__asm__ __volatile__ ("emms\n\t"); |
| 186 |
#else |
| 187 |
#warning missing copy_deinterlace_24 |
| 188 |
#endif |
| 189 |
} |
| 190 |
|
| 191 |
void ShmRenderer::move(int x, int y) |
| 192 |
{ |
| 193 |
xpos = x; |
| 194 |
ypos = y; |
| 195 |
} |
| 196 |
|
| 197 |
int ShmRenderer::draw(QPainter* pm, const void* data, bool deinterlace) |
| 198 |
{ |
| 199 |
char* outpic=xshmimg->data; |
| 200 |
if(outpic==0) |
| 201 |
return 0; |
| 202 |
if(data==0) |
| 203 |
return 0; |
| 204 |
|
| 205 |
Display *dpy = dev->x11Display(); |
| 206 |
Visual *vis = (Visual*)dev->x11Visual(); |
| 207 |
|
| 208 |
int bit_depth = avm::GetPhysicalDepth(dpy); |
| 209 |
|
| 210 |
if (mutex.TryLock() != 0) |
| 211 |
return -1; |
| 212 |
// pthread_mutex_lock(&mutex); |
| 213 |
|
| 214 |
sync(); |
| 215 |
const char* src; |
| 216 |
|
| 217 |
// if((pic_h==m_h) && (pic_w==m_w)) |
| 218 |
if(!deinterlace) |
| 219 |
memcpy(outpic, data, m_w*m_h*((bit_depth+7)/8)); |
| 220 |
else |
| 221 |
switch(bit_depth) |
| 222 |
{ |
| 223 |
// case 15: |
| 224 |
// copy_deinterlace_555(outpic, data, m_w, m_h); |
| 225 |
// break; |
| 226 |
// case 16: |
| 227 |
// copy_deinterlace_565(outpic, data, m_w, m_h); |
| 228 |
// break; |
| 229 |
case 24: |
| 230 |
case 32: |
| 231 |
copy_deinterlace_24(outpic, data, m_w*bit_depth/8, m_h); |
| 232 |
break; |
| 233 |
default: |
| 234 |
memcpy(outpic, data, m_w*m_h*((bit_depth+7)/8)); |
| 235 |
break; |
| 236 |
} |
| 237 |
// else |
| 238 |
// { |
| 239 |
// printf("Zooming\n"); |
| 240 |
// zoom((unsigned short*)outpic, (unsigned short*)data, pic_w, pic_h, m_w, m_h, bit_depth, m_w*((pic_w+m_w-1)/m_w)); |
| 241 |
// } |
| 242 |
|
| 243 |
XShmPutImage(dpy, dev->handle(), gc, |
| 244 |
xshmimg, 0, 0, xpos, ypos, pic_w, pic_h, true); |
| 245 |
|
| 246 |
mutex.Unlock(); |
| 247 |
|
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
int ShmRenderer::sync() |
| 252 |
{ |
| 253 |
Display *dpy = dev->x11Display(); |
| 254 |
XSync(dpy, false); |
| 255 |
return 0; |
| 256 |
} |