OLD | NEW |
(Empty) | |
| 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, const DisplayParams&); |
| 20 |
| 21 sk_sp<SkSurface> getBackbufferSurface() override; |
| 22 void swapBuffers() override; |
| 23 bool isValid() override { return SkToBool(fWindow); } |
| 24 void resize(uint32_t w, uint32_t 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 |
| 34 RasterWindowContext_xlib::RasterWindowContext_xlib(Display* display, XWindow win
dow, |
| 35 const DisplayParams& params) |
| 36 : fDisplay(display) |
| 37 , fWindow(window) { |
| 38 fDisplayParams = params; |
| 39 XWindowAttributes attrs; |
| 40 XGetWindowAttributes(fDisplay, fWindow, &attrs); |
| 41 fGC = XCreateGC(fDisplay, fWindow, 0, nullptr); |
| 42 this->resize(attrs.width, attrs.height); |
| 43 } |
| 44 |
| 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 |
| 52 void RasterWindowContext_xlib::resize(uint32_t w, uint32_t h) { |
| 53 SkImageInfo info = SkImageInfo::Make(w, h, fDisplayParams.fColorType, kPremu
l_SkAlphaType, |
| 54 fDisplayParams.fColorSpace); |
| 55 fBackbufferSurface = SkSurface::MakeRaster(info); |
| 56 |
| 57 } |
| 58 |
| 59 sk_sp<SkSurface> RasterWindowContext_xlib::getBackbufferSurface() { return fBack
bufferSurface; } |
| 60 |
| 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 image.width = pm.width(); |
| 69 image.height = pm.height(); |
| 70 image.format = ZPixmap; |
| 71 image.data = (char*) pm.addr(); |
| 72 image.byte_order = LSBFirst; |
| 73 image.bitmap_unit = bitsPerPixel; |
| 74 image.bitmap_bit_order = LSBFirst; |
| 75 image.bitmap_pad = bitsPerPixel; |
| 76 image.depth = 24; |
| 77 image.bytes_per_line = pm.rowBytes() - pm.width() * pm.info().bytesPerPixel(
); |
| 78 image.bits_per_pixel = bitsPerPixel; |
| 79 if (!XInitImage(&image)) { |
| 80 return; |
| 81 } |
| 82 XPutImage(fDisplay, fWindow, fGC, &image, 0, 0, 0, 0, pm.width(), pm.height(
)); |
| 83 } |
| 84 |
| 85 } // anonymous namespace |
| 86 |
| 87 namespace sk_app { |
| 88 namespace window_context_factory { |
| 89 |
| 90 WindowContext* NewRasterForXlib(const XlibWindowInfo& info, const DisplayParams&
params) { |
| 91 WindowContext* ctx = new RasterWindowContext_xlib(info.fDisplay, info.fWindo
w, params); |
| 92 if (!ctx->isValid()) { |
| 93 delete ctx; |
| 94 ctx = nullptr; |
| 95 } |
| 96 return ctx; |
| 97 } |
| 98 |
| 99 } // namespace window_context_factory |
| 100 } // namespace sk_app |
OLD | NEW |