Flutter Engine
The Flutter Engine
RasterWindowContext_unix.cpp
Go to the documentation of this file.
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
11
14
15namespace {
16
17class RasterWindowContext_xlib : public RasterWindowContext {
18public:
19 RasterWindowContext_xlib(Display*, XWindow, int width, int height, const DisplayParams&);
20
22 bool isValid() override { return SkToBool(fWindow); }
23 void resize(int w, int h) override;
24 void setDisplayParams(const DisplayParams& params) override;
25
26protected:
27 void onSwapBuffers() override;
28
29 sk_sp<SkSurface> fBackbufferSurface;
30 Display* fDisplay;
31 XWindow fWindow;
32 GC fGC;
33};
34
35RasterWindowContext_xlib::RasterWindowContext_xlib(Display* display, XWindow window, int width,
36 int height, const DisplayParams& 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
46void 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
53void 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
59sk_sp<SkSurface> RasterWindowContext_xlib::getBackbufferSurface() { return fBackbufferSurface; }
60
61void 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
88namespace skwindow {
89
90std::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
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
size_t rowBytes() const
Definition: SkPixmap.h:145
int width() const
Definition: SkPixmap.h:160
const SkImageInfo & info() const
Definition: SkPixmap.h:135
void * writable_addr() const
Definition: SkPixmap.h:483
int height() const
Definition: SkPixmap.h:166
virtual bool isValid()=0
virtual sk_sp< SkSurface > getBackbufferSurface()=0
virtual void setDisplayParams(const DisplayParams &params)=0
virtual void resize(int w, int h)=0
virtual void onSwapBuffers()=0
const EmbeddedViewParams * params
GLFWwindow * window
Definition: main.cc:45
Definition: dart.idl:411
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
std::unique_ptr< WindowContext > MakeRasterForXlib(const XlibWindowInfo &info, const DisplayParams &params)
SkScalar w
SkScalar h
int32_t height
int32_t width
int bytesPerPixel() const
Definition: SkImageInfo.h:492
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)