def _set_logical_size(self): # There is unclarity about the window size in "screen pixels". # It appears that on Windows and X11 its the same as the # framebuffer size, and on macOS it's logical pixels. # See https://github.com/glfw/glfw/issues/845 # Here, we simply do a quick test so we can compensate. # The target logical size lsize = self._logical_size pixel_ratio = glfw.get_window_content_scale(self._window)[0] # The current screen size and physical size, and its ratio ssize = glfw.get_window_size(self._window) psize = glfw.get_framebuffer_size(self._window) # Apply if is_wayland: # Not sure why, but on Wayland things work differently screen_ratio = ssize[0] / lsize[0] glfw.set_window_size( self._window, int(lsize[0] / screen_ratio), int(lsize[1] / screen_ratio), ) else: screen_ratio = ssize[0] / psize[0] glfw.set_window_size( self._window, int(lsize[0] * pixel_ratio / screen_ratio), int(lsize[1] * pixel_ratio / screen_ratio), )
def _get_logical_size(self): # Because the value of get_window_size is in physical pixels # on some systems and logical pixels on other, we use the # framebuffer size and pixel ratio to derive the logical size. psize = glfw.get_framebuffer_size(self._window) psize = int(psize[0]), int(psize[1]) ratio = glfw.get_window_content_scale(self._window)[0] return psize[0] / ratio, psize[1] / ratio
def get_pixel_ratio(self): return glfw.get_window_content_scale(self._window)[0]
def get_content_scale(window) -> float: return glfw.get_window_content_scale(window)[0]
def draw(self, return_mat=False): if self.client: img = self.client.new_frame_image() if img and img.texture_size().width and img.texture_size().height: if self.first_frame_flg: # set window size and show option glfw.set_window_size(self.window, int(img.texture_size().width), int(img.texture_size().height)) if not self.show: glfw.hide_window(self.window) # hide window # OpenGL init glMatrixMode(GL_PROJECTION) glOrtho(0, int(img.texture_size().width), int(img.texture_size().height), 0, 1, -1) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glDisable(GL_DEPTH_TEST) glClearColor(0.0, 0.0, 0.0, 0.0) glEnable(GL_TEXTURE_RECTANGLE) self.first_frame_flg = False print(f"server: \"{self.server.app_name}/{self.server.name}\", window: \"{self.window_name}\", size: \"{img.texture_size().width} × {img.texture_size().height}\"") glfw.make_context_current(self.window) # Clear screen glClearColor(0.0, 0.0, 0.0, 0.0) glClear(GL_COLOR_BUFFER_BIT) # Bind the texture glEnable(GL_TEXTURE_RECTANGLE) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_RECTANGLE, img.texture_name()) # Configure texturing as we want it glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glColor4f(1.0, 1.0, 1.0, 1.0) texCoords = np.array([ 0.0, img.texture_size().height, img.texture_size().width, img.texture_size().height, img.texture_size().width, 0.0, 0.0, 0.0], dtype=GLfloat) verts = np.array([ 0.0, 0.0, img.texture_size().width, 0.0, img.texture_size().width, img.texture_size().height, 0.0, img.texture_size().height], dtype=GLfloat) glEnableClientState( GL_TEXTURE_COORD_ARRAY ) glTexCoordPointer(2, GL_FLOAT, 0, texCoords) glEnableClientState(GL_VERTEX_ARRAY) glVertexPointer(2, GL_FLOAT, 0, verts) glDrawArrays( GL_TRIANGLE_FAN, 0, 4 ) if return_mat: x_scale, y_scale = glfw.get_window_content_scale(self.window) frame = np.zeros((int(img.texture_size().height * x_scale), int(img.texture_size().width * y_scale), 4), np.uint8) glPixelStorei(GL_PACK_ALIGNMENT, 1) glPixelStorei(GL_PACK_ROW_LENGTH, (int(img.texture_size().width * x_scale))) glReadPixels(0, 0, int(img.texture_size().width * x_scale), int(img.texture_size().height * y_scale), GL_BGRA, GL_UNSIGNED_BYTE, frame.data) frame = cv2.resize(frame, (int(img.texture_size().width), int(img.texture_size().height))) frame = cv2.flip(frame, 0) return frame glfw.swap_buffers(self.window) glfw.poll_events() # unbind our sender texture glBindTexture(GL_TEXTURE_RECTANGLE, 0)