RasterWindowContext_unix.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkSurface.h"
  8. #include "tools/sk_app/RasterWindowContext.h"
  9. #include "tools/sk_app/unix/WindowContextFactory_unix.h"
  10. using sk_app::RasterWindowContext;
  11. using sk_app::DisplayParams;
  12. namespace {
  13. class RasterWindowContext_xlib : public RasterWindowContext {
  14. public:
  15. RasterWindowContext_xlib(Display*, XWindow, int width, int height, const DisplayParams&);
  16. sk_sp<SkSurface> getBackbufferSurface() override;
  17. void swapBuffers() override;
  18. bool isValid() override { return SkToBool(fWindow); }
  19. void resize(int w, int h) override;
  20. void setDisplayParams(const DisplayParams& params) override;
  21. protected:
  22. sk_sp<SkSurface> fBackbufferSurface;
  23. Display* fDisplay;
  24. XWindow fWindow;
  25. GC fGC;
  26. typedef RasterWindowContext INHERITED;
  27. };
  28. RasterWindowContext_xlib::RasterWindowContext_xlib(Display* display, XWindow window, int width,
  29. int height, const DisplayParams& params)
  30. : INHERITED(params)
  31. , fDisplay(display)
  32. , fWindow(window) {
  33. fGC = XCreateGC(fDisplay, fWindow, 0, nullptr);
  34. this->resize(width, height);
  35. fWidth = width;
  36. fHeight = height;
  37. }
  38. void RasterWindowContext_xlib::setDisplayParams(const DisplayParams& params) {
  39. fDisplayParams = params;
  40. XWindowAttributes attrs;
  41. XGetWindowAttributes(fDisplay, fWindow, &attrs);
  42. this->resize(attrs.width, attrs.height);
  43. }
  44. void RasterWindowContext_xlib::resize(int w, int h) {
  45. SkImageInfo info = SkImageInfo::Make(w, h, fDisplayParams.fColorType, kPremul_SkAlphaType,
  46. fDisplayParams.fColorSpace);
  47. fBackbufferSurface = SkSurface::MakeRaster(info, &fDisplayParams.fSurfaceProps);
  48. }
  49. sk_sp<SkSurface> RasterWindowContext_xlib::getBackbufferSurface() { return fBackbufferSurface; }
  50. void RasterWindowContext_xlib::swapBuffers() {
  51. SkPixmap pm;
  52. if (!fBackbufferSurface->peekPixels(&pm)) {
  53. return;
  54. }
  55. int bitsPerPixel = pm.info().bytesPerPixel() * 8;
  56. XImage image;
  57. memset(&image, 0, sizeof(image));
  58. image.width = pm.width();
  59. image.height = pm.height();
  60. image.format = ZPixmap;
  61. image.data = (char*) pm.addr();
  62. image.byte_order = LSBFirst;
  63. image.bitmap_unit = bitsPerPixel;
  64. image.bitmap_bit_order = LSBFirst;
  65. image.bitmap_pad = bitsPerPixel;
  66. image.depth = 24;
  67. image.bytes_per_line = pm.rowBytes() - pm.width() * pm.info().bytesPerPixel();
  68. image.bits_per_pixel = bitsPerPixel;
  69. if (!XInitImage(&image)) {
  70. return;
  71. }
  72. XPutImage(fDisplay, fWindow, fGC, &image, 0, 0, 0, 0, pm.width(), pm.height());
  73. }
  74. } // anonymous namespace
  75. namespace sk_app {
  76. namespace window_context_factory {
  77. WindowContext* NewRasterForXlib(const XlibWindowInfo& info, const DisplayParams& params) {
  78. WindowContext* ctx = new RasterWindowContext_xlib(info.fDisplay, info.fWindow, info.fWidth,
  79. info.fHeight, params);
  80. if (!ctx->isValid()) {
  81. delete ctx;
  82. ctx = nullptr;
  83. }
  84. return ctx;
  85. }
  86. } // namespace window_context_factory
  87. } // namespace sk_app