Index: tools/viewer/sk_app/unix/RasterWindowContext_unix.cpp |
diff --git a/tools/viewer/sk_app/unix/RasterWindowContext_unix.cpp b/tools/viewer/sk_app/unix/RasterWindowContext_unix.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..aaf1712be0f285c4ec1ba959f247a5609e7cb2c1 |
--- /dev/null |
+++ b/tools/viewer/sk_app/unix/RasterWindowContext_unix.cpp |
@@ -0,0 +1,100 @@ |
+/* |
+ * Copyright 2016 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "WindowContextFactory_unix.h" |
+#include "../RasterWindowContext.h" |
+#include "SkSurface.h" |
+ |
+using sk_app::RasterWindowContext; |
+using sk_app::DisplayParams; |
+ |
+namespace { |
+ |
+class RasterWindowContext_xlib : public RasterWindowContext { |
+public: |
+ RasterWindowContext_xlib(Display*, XWindow, const DisplayParams&); |
+ |
+ sk_sp<SkSurface> getBackbufferSurface() override; |
+ void swapBuffers() override; |
+ bool isValid() override { return SkToBool(fWindow); } |
+ void resize(uint32_t w, uint32_t h) override; |
+ void setDisplayParams(const DisplayParams& params) override; |
+ |
+protected: |
+ sk_sp<SkSurface> fBackbufferSurface; |
+ Display* fDisplay; |
+ XWindow fWindow; |
+ GC fGC; |
+}; |
+ |
+RasterWindowContext_xlib::RasterWindowContext_xlib(Display* display, XWindow window, |
+ const DisplayParams& params) |
+ : fDisplay(display) |
+ , fWindow(window) { |
+ fDisplayParams = params; |
+ XWindowAttributes attrs; |
+ XGetWindowAttributes(fDisplay, fWindow, &attrs); |
+ fGC = XCreateGC(fDisplay, fWindow, 0, nullptr); |
+ this->resize(attrs.width, attrs.height); |
+} |
+ |
+void RasterWindowContext_xlib::setDisplayParams(const DisplayParams& params) { |
+ fDisplayParams = params; |
+ XWindowAttributes attrs; |
+ XGetWindowAttributes(fDisplay, fWindow, &attrs); |
+ this->resize(attrs.width, attrs.height); |
+} |
+ |
+void RasterWindowContext_xlib::resize(uint32_t w, uint32_t h) { |
+ SkImageInfo info = SkImageInfo::Make(w, h, fDisplayParams.fColorType, kPremul_SkAlphaType, |
+ fDisplayParams.fColorSpace); |
+ fBackbufferSurface = SkSurface::MakeRaster(info); |
+ |
+} |
+ |
+sk_sp<SkSurface> RasterWindowContext_xlib::getBackbufferSurface() { return fBackbufferSurface; } |
+ |
+void RasterWindowContext_xlib::swapBuffers() { |
+ SkPixmap pm; |
+ if (!fBackbufferSurface->peekPixels(&pm)) { |
+ return; |
+ } |
+ int bitsPerPixel = pm.info().bytesPerPixel() * 8; |
+ XImage image; |
+ image.width = pm.width(); |
+ image.height = pm.height(); |
+ image.format = ZPixmap; |
+ image.data = (char*) pm.addr(); |
+ image.byte_order = LSBFirst; |
+ image.bitmap_unit = bitsPerPixel; |
+ image.bitmap_bit_order = LSBFirst; |
+ image.bitmap_pad = bitsPerPixel; |
+ image.depth = 24; |
+ image.bytes_per_line = pm.rowBytes() - pm.width() * pm.info().bytesPerPixel(); |
+ image.bits_per_pixel = bitsPerPixel; |
+ if (!XInitImage(&image)) { |
+ return; |
+ } |
+ XPutImage(fDisplay, fWindow, fGC, &image, 0, 0, 0, 0, pm.width(), pm.height()); |
+} |
+ |
+} // anonymous namespace |
+ |
+namespace sk_app { |
+namespace window_context_factory { |
+ |
+WindowContext* NewRasterForXlib(const XlibWindowInfo& info, const DisplayParams& params) { |
+ WindowContext* ctx = new RasterWindowContext_xlib(info.fDisplay, info.fWindow, params); |
+ if (!ctx->isValid()) { |
+ delete ctx; |
+ ctx = nullptr; |
+ } |
+ return ctx; |
+} |
+ |
+} // namespace window_context_factory |
+} // namespace sk_app |