File: xshm.cpp

package info (click to toggle)
higan 094-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,780 kB
  • ctags: 15,643
  • sloc: cpp: 103,963; ansic: 659; makefile: 531; sh: 25
file content (227 lines) | stat: -rwxr-xr-x 6,753 bytes parent folder | download | duplicates (5)
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
#include <sys/shm.h>
#include <X11/extensions/XShm.h>

namespace ruby {

struct pVideoXShm {
  struct Device {
    Display* display = nullptr;
    int screen;
    int depth;
    Visual* visual = nullptr;
    Window window;

    XShmSegmentInfo shmInfo;
    XImage* image = nullptr;
    uint32_t* buffer = nullptr;
    unsigned width, height;
  } device;

  struct Settings {
    uintptr_t handle;
    unsigned depth = 24;

    uint32_t* buffer = nullptr;
    unsigned width, height;
  } settings;

  struct Color {
    unsigned depth;
    unsigned shift;

    unsigned idepth;
    unsigned ishift;
  } red, green, blue;

  bool cap(const string& name) {
    if(name == Video::Handle) return true;
    if(name == Video::Depth) return true;
    return false;
  }

  any get(const string& name) {
    if(name == Video::Handle) return settings.handle;
    if(name == Video::Depth) return settings.depth;
    return false;
  }

  bool set(const string& name, const any& value) {
    if(name == Video::Handle) {
      settings.handle = any_cast<uintptr_t>(value);
      return true;
    }
    if(name == Video::Depth) {
      return setDepth(any_cast<unsigned>(value));
    }
    return false;
  }

  bool lock(uint32_t*& data, unsigned& pitch, unsigned width, unsigned height) {
    if(settings.buffer == nullptr || settings.width != width || settings.height != height) {
      if(settings.buffer) delete[] settings.buffer;
      settings.width = width, settings.height = height;
      settings.buffer = new uint32_t[width * height]();
    }

    data = settings.buffer;
    pitch = settings.width * sizeof(uint32_t);
    return true;
  }

  void unlock() {
  }

  void clear() {
    if(settings.buffer == nullptr) return;
    memset(settings.buffer, 0, settings.width * settings.height * sizeof(uint32_t));
    refresh();
  }

  void refresh() {
    if(settings.buffer == nullptr) return;
    size();

    float xRatio = (float)settings.width  / (float)device.width;
    float yRatio = (float)settings.height / (float)device.height;
    float yStep = 0;
    for(unsigned y = 0; y < device.height; y++) {
      uint32_t* sp = settings.buffer + (unsigned)yStep * settings.width;
      uint32_t* dp = device.buffer + y * device.width;
      yStep += yRatio;
      float xStep = 0;
      for(unsigned x = 0; x < device.width; x++) {
        uint32_t color = sp[(unsigned)xStep];
        xStep += xRatio;
        unsigned r = (color >> red.ishift  ) & ((1 << red.idepth  ) - 1);
        unsigned g = (color >> green.ishift) & ((1 << green.idepth) - 1);
        unsigned b = (color >> blue.ishift ) & ((1 << blue.idepth ) - 1);
        *dp++ = image::normalize(r, red.idepth,   red.depth  ) << red.shift
              | image::normalize(g, green.idepth, green.depth) << green.shift
              | image::normalize(b, blue.idepth,  blue.depth ) << blue.shift;
      }
    }

    GC gc = XCreateGC(device.display, device.window, 0, 0);
    XShmPutImage(
      device.display, device.window, gc, device.image,
      0, 0, 0, 0, device.width, device.height, False
    );
    XFreeGC(device.display, gc);
    XFlush(device.display);
  }

  bool init() {
    device.display = XOpenDisplay(0);
    device.screen = DefaultScreen(device.display);

    XWindowAttributes getAttributes;
    XGetWindowAttributes(device.display, (Window)settings.handle, &getAttributes);
    device.depth = getAttributes.depth;
    device.visual = getAttributes.visual;
    unsigned visualID = XVisualIDFromVisual(device.visual);

    XVisualInfo visualTemplate = {0};
    visualTemplate.screen = device.screen;
    visualTemplate.depth = device.depth;
    int visualsMatched = 0;
    XVisualInfo* visualList = XGetVisualInfo(device.display, VisualScreenMask | VisualDepthMask, &visualTemplate, &visualsMatched);
    for(unsigned n = 0; n < visualsMatched; n++) {
      auto& v = visualList[n];
      if(v.visualid == visualID) {
        red.depth   = bit::count(v.red_mask),   red.shift   = bit::first(v.red_mask);
        green.depth = bit::count(v.green_mask), green.shift = bit::first(v.green_mask);
        blue.depth  = bit::count(v.blue_mask),  blue.shift  = bit::first(v.blue_mask);
        break;
      }
    }
    XFree(visualList);
    setDepth(settings.depth);

    XSetWindowAttributes setAttributes = {0};
    setAttributes.border_pixel = 0;
    device.window = XCreateWindow(device.display, (Window)settings.handle,
      0, 0, 256, 256, 0,
      getAttributes.depth, InputOutput, getAttributes.visual,
      CWBorderPixel, &setAttributes
    );
    XSetWindowBackground(device.display, device.window, 0);
    XMapWindow(device.display, device.window);
    XFlush(device.display);

    while(XPending(device.display)) {
      XEvent event;
      XNextEvent(device.display, &event);
    }

    if(size() == false) return false;
    return true;
  }

  void term() {
    free();

    if(device.display) { XCloseDisplay(device.display); device.display = nullptr; }
  }

  ~pVideoXShm() {
    term();
  }

//internal:
  bool setDepth(unsigned depth) {
    if(depth == 24) {
      settings.depth = 24;
      red.idepth   = 8, red.ishift   = 16;
      green.idepth = 8, green.ishift =  8;
      blue.idepth  = 8, blue.ishift  =  0;
      return true;
    }

    if(depth == 30) {
      settings.depth = 30;
      red.idepth   = 10, red.ishift   = 20;
      green.idepth = 10, green.ishift = 10;
      blue.idepth  = 10, blue.ishift  =  0;
      return true;
    }

    return false;
  }

  bool size() {
    XWindowAttributes windowAttributes;
    XGetWindowAttributes(device.display, settings.handle, &windowAttributes);

    if(device.buffer && device.width == windowAttributes.width && device.height == windowAttributes.height) return true;
    device.width = windowAttributes.width, device.height = windowAttributes.height;
    XResizeWindow(device.display, device.window, device.width, device.height);
    free();

    //create
    device.shmInfo.shmid = shmget(IPC_PRIVATE, device.width * device.height * sizeof(uint32_t), IPC_CREAT | 0777);
    if(device.shmInfo.shmid < 0) return false;

    device.shmInfo.shmaddr = (char*)shmat(device.shmInfo.shmid, 0, 0);
    device.shmInfo.readOnly = False;
    XShmAttach(device.display, &device.shmInfo);
    device.buffer = (uint32_t*)device.shmInfo.shmaddr;
    device.image = XShmCreateImage(device.display, device.visual, device.depth,
      ZPixmap, device.shmInfo.shmaddr, &device.shmInfo, device.width, device.height
    );

    return true;
  }

  void free() {
    if(device.buffer == nullptr) return;
    device.buffer = nullptr;
    XShmDetach(device.display, &device.shmInfo);
    XDestroyImage(device.image);
    shmdt(device.shmInfo.shmaddr);
    shmctl(device.shmInfo.shmid, IPC_RMID, 0);
  }
};

DeclareVideo(XShm)

}