Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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 |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 8 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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. |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 14 | |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 15 | #include "FrameBufferX11.hpp" |
| 16 | |
| 17 | #include "libX11.hpp" |
Nicolas Capens | 708c24b | 2017-10-26 13:07:10 -0400 | [diff] [blame] | 18 | #include "Common/Timer.hpp" |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 19 | |
| 20 | #include <sys/ipc.h> |
| 21 | #include <sys/shm.h> |
| 22 | #include <string.h> |
| 23 | #include <assert.h> |
Nicolas Capens | fbba490 | 2018-07-05 13:11:03 -0400 | [diff] [blame] | 24 | #include <stdlib.h> |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 25 | |
| 26 | namespace sw |
| 27 | { |
| 28 | static int (*PreviousXErrorHandler)(Display *display, XErrorEvent *event) = 0; |
| 29 | static bool shmBadAccess = false; |
| 30 | |
| 31 | // Catches BadAcces errors so we can fall back to not using MIT-SHM |
| 32 | static int XShmErrorHandler(Display *display, XErrorEvent *event) |
| 33 | { |
| 34 | if(event->error_code == BadAccess) |
| 35 | { |
| 36 | shmBadAccess = true; |
| 37 | return 0; |
| 38 | } |
| 39 | else |
| 40 | { |
| 41 | return PreviousXErrorHandler(display, event); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | FrameBufferX11::FrameBufferX11(Display *display, Window window, int width, int height) : FrameBuffer(width, height, false, false), ownX11(!display), x_display(display), x_window(window) |
| 46 | { |
| 47 | if(!x_display) |
| 48 | { |
| 49 | x_display = libX11->XOpenDisplay(0); |
Nicolas Capens | fbba490 | 2018-07-05 13:11:03 -0400 | [diff] [blame] | 50 | assert(x_display); |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int screen = DefaultScreen(x_display); |
| 54 | x_gc = libX11->XDefaultGC(x_display, screen); |
| 55 | int depth = libX11->XDefaultDepth(x_display, screen); |
| 56 | |
Nicolas Capens | 8fb6f6a | 2018-07-13 13:51:20 -0400 | [diff] [blame] | 57 | XVisualInfo x_visual; |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 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); |
Nicolas Capens | fbba490 | 2018-07-05 13:11:03 -0400 | [diff] [blame] | 69 | shminfo.shmaddr = x_image->data = (char*)shmat(shminfo.shmid, 0, 0); |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 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 | { |
Alexis Hetu | aa2666f | 2017-09-25 15:56:44 -0400 | [diff] [blame] | 91 | int bytes_per_line = width * 4; |
| 92 | int bytes_per_image = height * bytes_per_line; |
Nicolas Capens | fbba490 | 2018-07-05 13:11:03 -0400 | [diff] [blame] | 93 | char *buffer = (char*)malloc(bytes_per_image); |
Alexis Hetu | aa2666f | 2017-09-25 15:56:44 -0400 | [diff] [blame] | 94 | memset(buffer, 0, bytes_per_image); |
Nicolas Capens | fbba490 | 2018-07-05 13:11:03 -0400 | [diff] [blame] | 95 | |
Alexis Hetu | aa2666f | 2017-09-25 15:56:44 -0400 | [diff] [blame] | 96 | x_image = libX11->XCreateImage(x_display, visual, depth, ZPixmap, 0, buffer, width, height, 32, bytes_per_line); |
Nicolas Capens | fbba490 | 2018-07-05 13:11:03 -0400 | [diff] [blame] | 97 | assert(x_image); |
| 98 | |
| 99 | if(!x_image) |
| 100 | { |
| 101 | free(buffer); |
| 102 | } |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | FrameBufferX11::~FrameBufferX11() |
| 107 | { |
| 108 | if(!mit_shm) |
| 109 | { |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 110 | XDestroyImage(x_image); |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
| 114 | libX11->XShmDetach(x_display, &shminfo); |
| 115 | XDestroyImage(x_image); |
| 116 | shmdt(shminfo.shmaddr); |
| 117 | shmctl(shminfo.shmid, IPC_RMID, 0); |
| 118 | } |
| 119 | |
| 120 | if(ownX11) |
| 121 | { |
| 122 | libX11->XCloseDisplay(x_display); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void *FrameBufferX11::lock() |
| 127 | { |
Nicolas Capens | fbba490 | 2018-07-05 13:11:03 -0400 | [diff] [blame] | 128 | if(x_image) |
| 129 | { |
| 130 | stride = x_image->bytes_per_line; |
| 131 | framebuffer = x_image->data; |
| 132 | } |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 133 | |
Nicolas Capens | e5a9637 | 2017-08-11 15:14:25 -0400 | [diff] [blame] | 134 | return framebuffer; |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void FrameBufferX11::unlock() |
| 138 | { |
Nicolas Capens | e5a9637 | 2017-08-11 15:14:25 -0400 | [diff] [blame] | 139 | framebuffer = nullptr; |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Nicolas Capens | 241f789 | 2015-12-30 23:40:45 -0500 | [diff] [blame] | 142 | void FrameBufferX11::blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect) |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 143 | { |
Nicolas Capens | 241f789 | 2015-12-30 23:40:45 -0500 | [diff] [blame] | 144 | copy(source); |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 145 | |
| 146 | if(!mit_shm) |
| 147 | { |
| 148 | libX11->XPutImage(x_display, x_window, x_gc, x_image, 0, 0, 0, 0, width, height); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | libX11->XShmPutImage(x_display, x_window, x_gc, x_image, 0, 0, 0, 0, width, height, False); |
| 153 | } |
| 154 | |
| 155 | libX11->XSync(x_display, False); |
Nicolas Capens | bba05f3 | 2017-09-15 14:59:38 -0400 | [diff] [blame] | 156 | |
| 157 | if(false) // Draw the framerate on screen |
| 158 | { |
| 159 | static double fpsTime = sw::Timer::seconds(); |
Nicolas Capens | 0952c7d | 2017-09-27 10:22:08 -0400 | [diff] [blame] | 160 | static int frames = -1; |
Nicolas Capens | bba05f3 | 2017-09-15 14:59:38 -0400 | [diff] [blame] | 161 | |
| 162 | double time = sw::Timer::seconds(); |
| 163 | double delta = time - fpsTime; |
Nicolas Capens | 0952c7d | 2017-09-27 10:22:08 -0400 | [diff] [blame] | 164 | frames++; |
Nicolas Capens | bba05f3 | 2017-09-15 14:59:38 -0400 | [diff] [blame] | 165 | |
| 166 | static double FPS = 0.0; |
Nicolas Capens | 0952c7d | 2017-09-27 10:22:08 -0400 | [diff] [blame] | 167 | static double maxFPS = 0.0; |
Nicolas Capens | bba05f3 | 2017-09-15 14:59:38 -0400 | [diff] [blame] | 168 | |
| 169 | if(delta > 1.0) |
| 170 | { |
Nicolas Capens | 0952c7d | 2017-09-27 10:22:08 -0400 | [diff] [blame] | 171 | FPS = frames / delta; |
Nicolas Capens | bba05f3 | 2017-09-15 14:59:38 -0400 | [diff] [blame] | 172 | |
| 173 | fpsTime = time; |
Nicolas Capens | 0952c7d | 2017-09-27 10:22:08 -0400 | [diff] [blame] | 174 | frames = 0; |
| 175 | |
| 176 | if(FPS > maxFPS) |
| 177 | { |
| 178 | maxFPS = FPS; |
| 179 | } |
Nicolas Capens | bba05f3 | 2017-09-15 14:59:38 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | char string[256]; |
Nicolas Capens | 0952c7d | 2017-09-27 10:22:08 -0400 | [diff] [blame] | 183 | sprintf(string, "FPS: %.2f (max: %.2f)", FPS, maxFPS); |
Nicolas Capens | bba05f3 | 2017-09-15 14:59:38 -0400 | [diff] [blame] | 184 | libX11->XDrawString(x_display, x_window, x_gc, 50, 50, string, strlen(string)); |
| 185 | } |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Nicolas Capens | 506cc5e | 2017-07-24 11:30:55 -0400 | [diff] [blame] | 189 | NO_SANITIZE_FUNCTION sw::FrameBuffer *createFrameBuffer(void *display, Window window, int width, int height) |
Nicolas Capens | e63db96 | 2015-12-11 17:24:40 -0500 | [diff] [blame] | 190 | { |
| 191 | return new sw::FrameBufferX11((::Display*)display, window, width, height); |
| 192 | } |