1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2016 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker *
4*c8dee2aaSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker */
7*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/unix/RasterWindowContext_unix.h"
8*c8dee2aaSAndroid Build Coastguard Worker
9*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkSurface.h"
10*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/RasterWindowContext.h"
11*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/unix/XlibWindowInfo.h"
12*c8dee2aaSAndroid Build Coastguard Worker
13*c8dee2aaSAndroid Build Coastguard Worker #include <X11/Xlib.h>
14*c8dee2aaSAndroid Build Coastguard Worker
15*c8dee2aaSAndroid Build Coastguard Worker using skwindow::DisplayParams;
16*c8dee2aaSAndroid Build Coastguard Worker using skwindow::internal::RasterWindowContext;
17*c8dee2aaSAndroid Build Coastguard Worker
18*c8dee2aaSAndroid Build Coastguard Worker namespace {
19*c8dee2aaSAndroid Build Coastguard Worker
20*c8dee2aaSAndroid Build Coastguard Worker class RasterWindowContext_xlib : public RasterWindowContext {
21*c8dee2aaSAndroid Build Coastguard Worker public:
22*c8dee2aaSAndroid Build Coastguard Worker RasterWindowContext_xlib(
23*c8dee2aaSAndroid Build Coastguard Worker Display*, XWindow, int width, int height, std::unique_ptr<const DisplayParams>);
24*c8dee2aaSAndroid Build Coastguard Worker
25*c8dee2aaSAndroid Build Coastguard Worker sk_sp<SkSurface> getBackbufferSurface() override;
isValid()26*c8dee2aaSAndroid Build Coastguard Worker bool isValid() override { return SkToBool(fWindow); }
27*c8dee2aaSAndroid Build Coastguard Worker void resize(int w, int h) override;
28*c8dee2aaSAndroid Build Coastguard Worker void setDisplayParams(std::unique_ptr<const DisplayParams> params) override;
29*c8dee2aaSAndroid Build Coastguard Worker
30*c8dee2aaSAndroid Build Coastguard Worker protected:
31*c8dee2aaSAndroid Build Coastguard Worker void onSwapBuffers() override;
32*c8dee2aaSAndroid Build Coastguard Worker
33*c8dee2aaSAndroid Build Coastguard Worker sk_sp<SkSurface> fBackbufferSurface;
34*c8dee2aaSAndroid Build Coastguard Worker Display* fDisplay;
35*c8dee2aaSAndroid Build Coastguard Worker XWindow fWindow;
36*c8dee2aaSAndroid Build Coastguard Worker GC fGC;
37*c8dee2aaSAndroid Build Coastguard Worker };
38*c8dee2aaSAndroid Build Coastguard Worker
RasterWindowContext_xlib(Display * display,XWindow window,int width,int height,std::unique_ptr<const DisplayParams> params)39*c8dee2aaSAndroid Build Coastguard Worker RasterWindowContext_xlib::RasterWindowContext_xlib(Display* display,
40*c8dee2aaSAndroid Build Coastguard Worker XWindow window,
41*c8dee2aaSAndroid Build Coastguard Worker int width,
42*c8dee2aaSAndroid Build Coastguard Worker int height,
43*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<const DisplayParams> params)
44*c8dee2aaSAndroid Build Coastguard Worker : RasterWindowContext(std::move(params)), fDisplay(display), fWindow(window) {
45*c8dee2aaSAndroid Build Coastguard Worker fGC = XCreateGC(fDisplay, fWindow, 0, nullptr);
46*c8dee2aaSAndroid Build Coastguard Worker this->resize(width, height);
47*c8dee2aaSAndroid Build Coastguard Worker fWidth = width;
48*c8dee2aaSAndroid Build Coastguard Worker fHeight = height;
49*c8dee2aaSAndroid Build Coastguard Worker }
50*c8dee2aaSAndroid Build Coastguard Worker
setDisplayParams(std::unique_ptr<const DisplayParams> params)51*c8dee2aaSAndroid Build Coastguard Worker void RasterWindowContext_xlib::setDisplayParams(std::unique_ptr<const DisplayParams> params) {
52*c8dee2aaSAndroid Build Coastguard Worker fDisplayParams = std::move(params);
53*c8dee2aaSAndroid Build Coastguard Worker XWindowAttributes attrs;
54*c8dee2aaSAndroid Build Coastguard Worker XGetWindowAttributes(fDisplay, fWindow, &attrs);
55*c8dee2aaSAndroid Build Coastguard Worker this->resize(attrs.width, attrs.height);
56*c8dee2aaSAndroid Build Coastguard Worker }
57*c8dee2aaSAndroid Build Coastguard Worker
resize(int w,int h)58*c8dee2aaSAndroid Build Coastguard Worker void RasterWindowContext_xlib::resize(int w, int h) {
59*c8dee2aaSAndroid Build Coastguard Worker SkImageInfo info = SkImageInfo::Make(
60*c8dee2aaSAndroid Build Coastguard Worker w, h, fDisplayParams->colorType(), kPremul_SkAlphaType, fDisplayParams->colorSpace());
61*c8dee2aaSAndroid Build Coastguard Worker fBackbufferSurface = SkSurfaces::Raster(info, &fDisplayParams->surfaceProps());
62*c8dee2aaSAndroid Build Coastguard Worker }
63*c8dee2aaSAndroid Build Coastguard Worker
getBackbufferSurface()64*c8dee2aaSAndroid Build Coastguard Worker sk_sp<SkSurface> RasterWindowContext_xlib::getBackbufferSurface() { return fBackbufferSurface; }
65*c8dee2aaSAndroid Build Coastguard Worker
onSwapBuffers()66*c8dee2aaSAndroid Build Coastguard Worker void RasterWindowContext_xlib::onSwapBuffers() {
67*c8dee2aaSAndroid Build Coastguard Worker SkPixmap pm;
68*c8dee2aaSAndroid Build Coastguard Worker if (!fBackbufferSurface->peekPixels(&pm)) {
69*c8dee2aaSAndroid Build Coastguard Worker return;
70*c8dee2aaSAndroid Build Coastguard Worker }
71*c8dee2aaSAndroid Build Coastguard Worker int bitsPerPixel = pm.info().bytesPerPixel() * 8;
72*c8dee2aaSAndroid Build Coastguard Worker XImage image;
73*c8dee2aaSAndroid Build Coastguard Worker memset(&image, 0, sizeof(image));
74*c8dee2aaSAndroid Build Coastguard Worker image.width = pm.width();
75*c8dee2aaSAndroid Build Coastguard Worker image.height = pm.height();
76*c8dee2aaSAndroid Build Coastguard Worker image.format = ZPixmap;
77*c8dee2aaSAndroid Build Coastguard Worker image.data = (char*) pm.writable_addr();
78*c8dee2aaSAndroid Build Coastguard Worker image.byte_order = LSBFirst;
79*c8dee2aaSAndroid Build Coastguard Worker image.bitmap_unit = bitsPerPixel;
80*c8dee2aaSAndroid Build Coastguard Worker image.bitmap_bit_order = LSBFirst;
81*c8dee2aaSAndroid Build Coastguard Worker image.bitmap_pad = bitsPerPixel;
82*c8dee2aaSAndroid Build Coastguard Worker image.depth = 24;
83*c8dee2aaSAndroid Build Coastguard Worker image.bytes_per_line = pm.rowBytes() - pm.width() * pm.info().bytesPerPixel();
84*c8dee2aaSAndroid Build Coastguard Worker image.bits_per_pixel = bitsPerPixel;
85*c8dee2aaSAndroid Build Coastguard Worker if (!XInitImage(&image)) {
86*c8dee2aaSAndroid Build Coastguard Worker return;
87*c8dee2aaSAndroid Build Coastguard Worker }
88*c8dee2aaSAndroid Build Coastguard Worker XPutImage(fDisplay, fWindow, fGC, &image, 0, 0, 0, 0, pm.width(), pm.height());
89*c8dee2aaSAndroid Build Coastguard Worker }
90*c8dee2aaSAndroid Build Coastguard Worker
91*c8dee2aaSAndroid Build Coastguard Worker } // anonymous namespace
92*c8dee2aaSAndroid Build Coastguard Worker
93*c8dee2aaSAndroid Build Coastguard Worker namespace skwindow {
94*c8dee2aaSAndroid Build Coastguard Worker
MakeRasterForXlib(const XlibWindowInfo & info,std::unique_ptr<const DisplayParams> params)95*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<WindowContext> MakeRasterForXlib(const XlibWindowInfo& info,
96*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<const DisplayParams> params) {
97*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<WindowContext> ctx(new RasterWindowContext_xlib(
98*c8dee2aaSAndroid Build Coastguard Worker info.fDisplay, info.fWindow, info.fWidth, info.fHeight, std::move(params)));
99*c8dee2aaSAndroid Build Coastguard Worker if (!ctx->isValid()) {
100*c8dee2aaSAndroid Build Coastguard Worker ctx = nullptr;
101*c8dee2aaSAndroid Build Coastguard Worker }
102*c8dee2aaSAndroid Build Coastguard Worker return ctx;
103*c8dee2aaSAndroid Build Coastguard Worker }
104*c8dee2aaSAndroid Build Coastguard Worker
105*c8dee2aaSAndroid Build Coastguard Worker } // namespace skwindow
106