1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Surface.cpp: Implements the egl::Surface class, representing a drawing surface
16 // such as the client area of a window, including any back buffers.
17 // Implements EGLSurface and related functionality. [EGL 1.4] section 2.2 page 3.
18
19 #include "FrameBufferX11.hpp"
20
21 #include "libX11.hpp"
22
23 #include <sys/ipc.h>
24 #include <sys/shm.h>
25 #include <string.h>
26 #include <assert.h>
27
28 namespace sw
29 {
30 static int (*PreviousXErrorHandler)(Display *display, XErrorEvent *event) = 0;
31 static bool shmBadAccess = false;
32
33 // Catches BadAcces errors so we can fall back to not using MIT-SHM
XShmErrorHandler(Display * display,XErrorEvent * event)34 static int XShmErrorHandler(Display *display, XErrorEvent *event)
35 {
36 if(event->error_code == BadAccess)
37 {
38 shmBadAccess = true;
39 return 0;
40 }
41 else
42 {
43 return PreviousXErrorHandler(display, event);
44 }
45 }
46
FrameBufferX11(Display * display,Window window,int width,int height)47 FrameBufferX11::FrameBufferX11(Display *display, Window window, int width, int height) : FrameBuffer(width, height, false, false), ownX11(!display), x_display(display), x_window(window)
48 {
49 if(!x_display)
50 {
51 x_display = libX11->XOpenDisplay(0);
52 }
53
54 int screen = DefaultScreen(x_display);
55 x_gc = libX11->XDefaultGC(x_display, screen);
56 int depth = libX11->XDefaultDepth(x_display, screen);
57
58 Status status = libX11->XMatchVisualInfo(x_display, screen, 32, TrueColor, &x_visual);
59 bool match = (status != 0 && x_visual.blue_mask == 0xFF); // Prefer X8R8G8B8
60 Visual *visual = match ? x_visual.visual : libX11->XDefaultVisual(x_display, screen);
61
62 mit_shm = (libX11->XShmQueryExtension && libX11->XShmQueryExtension(x_display) == True);
63
64 if(mit_shm)
65 {
66 x_image = libX11->XShmCreateImage(x_display, visual, depth, ZPixmap, 0, &shminfo, width, height);
67
68 shminfo.shmid = shmget(IPC_PRIVATE, x_image->bytes_per_line * x_image->height, IPC_CREAT | SHM_R | SHM_W);
69 shminfo.shmaddr = x_image->data = buffer = (char*)shmat(shminfo.shmid, 0, 0);
70 shminfo.readOnly = False;
71
72 PreviousXErrorHandler = libX11->XSetErrorHandler(XShmErrorHandler);
73 libX11->XShmAttach(x_display, &shminfo); // May produce a BadAccess error
74 libX11->XSync(x_display, False);
75 libX11->XSetErrorHandler(PreviousXErrorHandler);
76
77 if(shmBadAccess)
78 {
79 mit_shm = false;
80
81 XDestroyImage(x_image);
82 shmdt(shminfo.shmaddr);
83 shmctl(shminfo.shmid, IPC_RMID, 0);
84
85 shmBadAccess = false;
86 }
87 }
88
89 if(!mit_shm)
90 {
91 buffer = new char[width * height * 4];
92 x_image = libX11->XCreateImage(x_display, visual, depth, ZPixmap, 0, buffer, width, height, 32, width * 4);
93 }
94 }
95
~FrameBufferX11()96 FrameBufferX11::~FrameBufferX11()
97 {
98 if(!mit_shm)
99 {
100 x_image->data = 0;
101 XDestroyImage(x_image);
102
103 delete[] buffer;
104 buffer = 0;
105 }
106 else
107 {
108 libX11->XShmDetach(x_display, &shminfo);
109 XDestroyImage(x_image);
110 shmdt(shminfo.shmaddr);
111 shmctl(shminfo.shmid, IPC_RMID, 0);
112 }
113
114 if(ownX11)
115 {
116 libX11->XCloseDisplay(x_display);
117 }
118 }
119
lock()120 void *FrameBufferX11::lock()
121 {
122 stride = x_image->bytes_per_line;
123 locked = buffer;
124
125 return locked;
126 }
127
unlock()128 void FrameBufferX11::unlock()
129 {
130 locked = 0;
131 }
132
blit(void * source,const Rect * sourceRect,const Rect * destRect,Format sourceFormat,size_t sourceStride)133 void FrameBufferX11::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
134 {
135 copy(source, sourceFormat, sourceStride);
136
137 if(!mit_shm)
138 {
139 libX11->XPutImage(x_display, x_window, x_gc, x_image, 0, 0, 0, 0, width, height);
140 }
141 else
142 {
143 libX11->XShmPutImage(x_display, x_window, x_gc, x_image, 0, 0, 0, 0, width, height, False);
144 }
145
146 libX11->XSync(x_display, False);
147 }
148 }
149
createFrameBuffer(void * display,Window window,int width,int height)150 sw::FrameBuffer *createFrameBuffer(void *display, Window window, int width, int height)
151 {
152 return new sw::FrameBufferX11((::Display*)display, window, width, height);
153 }
154