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
8 #include "include/core/SkSurface.h"
9 #include "tools/window/RasterWindowContext.h"
10 #include "tools/window/unix/WindowContextFactory_unix.h"
11
12 using skwindow::DisplayParams;
13 using skwindow::internal::RasterWindowContext;
14
15 namespace {
16
17 class RasterWindowContext_xlib : public RasterWindowContext {
18 public:
19 RasterWindowContext_xlib(Display*, XWindow, int width, int height, const DisplayParams&);
20
21 sk_sp<SkSurface> getBackbufferSurface() override;
isValid()22 bool isValid() override { return SkToBool(fWindow); }
23 void resize(int w, int h) override;
24 void setDisplayParams(const DisplayParams& params) override;
25
26 protected:
27 void onSwapBuffers() override;
28
29 sk_sp<SkSurface> fBackbufferSurface;
30 Display* fDisplay;
31 XWindow fWindow;
32 GC fGC;
33 };
34
RasterWindowContext_xlib(Display * display,XWindow window,int width,int height,const DisplayParams & params)35 RasterWindowContext_xlib::RasterWindowContext_xlib(Display* display, XWindow window, int width,
36 int height, const DisplayParams& params)
37 : RasterWindowContext(params)
38 , fDisplay(display)
39 , fWindow(window) {
40 fGC = XCreateGC(fDisplay, fWindow, 0, nullptr);
41 this->resize(width, height);
42 fWidth = width;
43 fHeight = height;
44 }
45
setDisplayParams(const DisplayParams & params)46 void RasterWindowContext_xlib::setDisplayParams(const DisplayParams& params) {
47 fDisplayParams = params;
48 XWindowAttributes attrs;
49 XGetWindowAttributes(fDisplay, fWindow, &attrs);
50 this->resize(attrs.width, attrs.height);
51 }
52
resize(int w,int h)53 void RasterWindowContext_xlib::resize(int w, int h) {
54 SkImageInfo info = SkImageInfo::Make(w, h, fDisplayParams.fColorType, kPremul_SkAlphaType,
55 fDisplayParams.fColorSpace);
56 fBackbufferSurface = SkSurfaces::Raster(info, &fDisplayParams.fSurfaceProps);
57 }
58
getBackbufferSurface()59 sk_sp<SkSurface> RasterWindowContext_xlib::getBackbufferSurface() { return fBackbufferSurface; }
60
onSwapBuffers()61 void RasterWindowContext_xlib::onSwapBuffers() {
62 SkPixmap pm;
63 if (!fBackbufferSurface->peekPixels(&pm)) {
64 return;
65 }
66 int bitsPerPixel = pm.info().bytesPerPixel() * 8;
67 XImage image;
68 memset(&image, 0, sizeof(image));
69 image.width = pm.width();
70 image.height = pm.height();
71 image.format = ZPixmap;
72 image.data = (char*) pm.writable_addr();
73 image.byte_order = LSBFirst;
74 image.bitmap_unit = bitsPerPixel;
75 image.bitmap_bit_order = LSBFirst;
76 image.bitmap_pad = bitsPerPixel;
77 image.depth = 24;
78 image.bytes_per_line = pm.rowBytes() - pm.width() * pm.info().bytesPerPixel();
79 image.bits_per_pixel = bitsPerPixel;
80 if (!XInitImage(&image)) {
81 return;
82 }
83 XPutImage(fDisplay, fWindow, fGC, &image, 0, 0, 0, 0, pm.width(), pm.height());
84 }
85
86 } // anonymous namespace
87
88 namespace skwindow {
89
MakeRasterForXlib(const XlibWindowInfo & info,const DisplayParams & params)90 std::unique_ptr<WindowContext> MakeRasterForXlib(const XlibWindowInfo& info,
91 const DisplayParams& params) {
92 std::unique_ptr<WindowContext> ctx(new RasterWindowContext_xlib(
93 info.fDisplay, info.fWindow, info.fWidth, info.fHeight, params));
94 if (!ctx->isValid()) {
95 ctx = nullptr;
96 }
97 return ctx;
98 }
99
100 } // namespace skwindow
101