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