Handler for rendering web pages to the screen via SDL2. The object's texture property is exposed to allow the main rendering loop to access the SDL2 texture.
| 483 | |
| 484 | |
| 485 | class RenderHandler(object): |
| 486 | """ |
| 487 | Handler for rendering web pages to the |
| 488 | screen via SDL2. |
| 489 | |
| 490 | The object's texture property is exposed |
| 491 | to allow the main rendering loop to access |
| 492 | the SDL2 texture. |
| 493 | """ |
| 494 | |
| 495 | def __init__(self, renderer, width, height): |
| 496 | self.__width = width |
| 497 | self.__height = height |
| 498 | self.__renderer = renderer |
| 499 | self.texture = None |
| 500 | |
| 501 | def GetViewRect(self, rect_out, **_): |
| 502 | rect_out.extend([0, 0, self.__width, self.__height]) |
| 503 | return True |
| 504 | |
| 505 | def OnPaint(self, element_type, paint_buffer, **_): |
| 506 | """ |
| 507 | Using the pixel data from CEF's offscreen rendering |
| 508 | the data is converted by PIL into a SDL2 surface |
| 509 | which can then be rendered as a SDL2 texture. |
| 510 | """ |
| 511 | if element_type == cef.PET_VIEW: |
| 512 | image = Image.frombuffer( |
| 513 | 'RGBA', |
| 514 | (self.__width, self.__height), |
| 515 | paint_buffer.GetString(mode="rgba", origin="top-left"), |
| 516 | 'raw', |
| 517 | 'BGRA' |
| 518 | ) |
| 519 | # Following PIL to SDL2 surface code from pysdl2 source. |
| 520 | mode = image.mode |
| 521 | rmask = gmask = bmask = amask = 0 |
| 522 | depth = None |
| 523 | pitch = None |
| 524 | if mode == "RGB": |
| 525 | # 3x8-bit, 24bpp |
| 526 | if sdl2.endian.SDL_BYTEORDER == sdl2.endian.SDL_LIL_ENDIAN: |
| 527 | rmask = 0x0000FF |
| 528 | gmask = 0x00FF00 |
| 529 | bmask = 0xFF0000 |
| 530 | else: |
| 531 | rmask = 0xFF0000 |
| 532 | gmask = 0x00FF00 |
| 533 | bmask = 0x0000FF |
| 534 | depth = 24 |
| 535 | pitch = self.__width * 3 |
| 536 | elif mode in ("RGBA", "RGBX"): |
| 537 | # RGBX: 4x8-bit, no alpha |
| 538 | # RGBA: 4x8-bit, alpha |
| 539 | if sdl2.endian.SDL_BYTEORDER == sdl2.endian.SDL_LIL_ENDIAN: |
| 540 | rmask = 0x00000000 |
| 541 | gmask = 0x0000FF00 |
| 542 | bmask = 0x00FF0000 |