• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "WindowContextFactory_unix.h"
9 #include "../RasterWindowContext.h"
10 #include "SkSurface.h"
11 
12 using sk_app::RasterWindowContext;
13 using sk_app::DisplayParams;
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;
22     void swapBuffers() override;
isValid()23     bool isValid() override { return SkToBool(fWindow); }
24     void resize(int  w, int h) override;
25     void setDisplayParams(const DisplayParams& params) override;
26 
27 protected:
28     sk_sp<SkSurface> fBackbufferSurface;
29     Display* fDisplay;
30     XWindow  fWindow;
31     GC       fGC;
32 };
33 
RasterWindowContext_xlib(Display * display,XWindow window,int width,int height,const DisplayParams & params)34 RasterWindowContext_xlib::RasterWindowContext_xlib(Display* display, XWindow window, int width,
35                                                    int height, const DisplayParams& params)
36         : fDisplay(display)
37         , fWindow(window) {
38     fDisplayParams = params;
39     fGC = XCreateGC(fDisplay, fWindow, 0, nullptr);
40     this->resize(width, height);
41     fWidth = width;
42     fHeight = height;
43 }
44 
setDisplayParams(const DisplayParams & params)45 void RasterWindowContext_xlib::setDisplayParams(const DisplayParams& params) {
46     fDisplayParams = params;
47     XWindowAttributes attrs;
48     XGetWindowAttributes(fDisplay, fWindow, &attrs);
49     this->resize(attrs.width, attrs.height);
50 }
51 
resize(int w,int h)52 void RasterWindowContext_xlib::resize(int  w, int h) {
53     SkImageInfo info = SkImageInfo::Make(w, h, fDisplayParams.fColorType, kPremul_SkAlphaType,
54                                          fDisplayParams.fColorSpace);
55     fBackbufferSurface = SkSurface::MakeRaster(info);
56 
57 }
58 
getBackbufferSurface()59 sk_sp<SkSurface> RasterWindowContext_xlib::getBackbufferSurface() { return fBackbufferSurface; }
60 
swapBuffers()61 void RasterWindowContext_xlib::swapBuffers() {
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.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 sk_app {
89 namespace window_context_factory {
90 
NewRasterForXlib(const XlibWindowInfo & info,const DisplayParams & params)91 WindowContext* NewRasterForXlib(const XlibWindowInfo& info, const DisplayParams& params) {
92     WindowContext* ctx = new RasterWindowContext_xlib(info.fDisplay, info.fWindow, info.fWidth,
93                                                       info.fHeight, params);
94     if (!ctx->isValid()) {
95         delete ctx;
96         ctx = nullptr;
97     }
98     return ctx;
99 }
100 
101 }  // namespace window_context_factory
102 }  // namespace sk_app
103