Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <utility>
21 :
22 : #include <prex.h>
23 : #include <X11/extensions/Xrender.h>
24 : #include <X11/Xlib.h>
25 : #include <postx.h>
26 :
27 : #include "cairo_xlib_cairo.hxx"
28 :
29 : #include <vcl/sysdata.hxx>
30 : #include <vcl/bitmap.hxx>
31 : #include <vcl/virdev.hxx>
32 : #include <vcl/window.hxx>
33 : #include <basegfx/vector/b2isize.hxx>
34 :
35 : namespace
36 : {
37 0 : Pixmap limitXCreatePixmap(Display *display, Drawable d, unsigned int width, unsigned int height, unsigned int depth)
38 : {
39 : // The X protocol request CreatePixmap puts an upper bound
40 : // of 16 bit to the size. And in practice some drivers
41 : // fall over with values close to the max.
42 :
43 : // see, e.g. moz#424333, fdo#48961, rhbz#1086714
44 : // we've a duplicate of this in vcl :-(
45 0 : if (width > SAL_MAX_INT16-10 || height > SAL_MAX_INT16-10)
46 : {
47 : SAL_WARN("canvas", "overlarge pixmap: " << width << " x " << height);
48 0 : return None;
49 : }
50 0 : return XCreatePixmap(display, d, width, height, depth);
51 : }
52 : }
53 :
54 : namespace cairo
55 : {
56 :
57 : #include <cairo-xlib.h>
58 : #include <cairo-xlib-xrender.h>
59 :
60 : // TODO(F3): svp headless case!
61 :
62 2 : bool IsCairoWorking( OutputDevice* pOutDev )
63 : {
64 2 : if( !pOutDev )
65 0 : return false;
66 :
67 2 : Display* pDisplay = (Display*)pOutDev->GetSystemGfxData().pDisplay;
68 2 : if( !pDisplay )
69 2 : return false;
70 :
71 : int nDummy;
72 0 : return XQueryExtension( pDisplay, "RENDER", &nDummy, &nDummy, &nDummy );
73 : }
74 :
75 0 : X11SysData::X11SysData() :
76 : pDisplay(NULL),
77 : hDrawable(0),
78 : pVisual(NULL),
79 : nScreen(0),
80 : nDepth(-1),
81 : aColormap(-1),
82 0 : pRenderFormat(NULL)
83 0 : {}
84 :
85 0 : X11SysData::X11SysData( const SystemGraphicsData& pSysDat ) :
86 : pDisplay(pSysDat.pDisplay),
87 : hDrawable(pSysDat.hDrawable),
88 : pVisual(pSysDat.pVisual),
89 : nScreen(pSysDat.nScreen),
90 : nDepth(pSysDat.nDepth),
91 : aColormap(pSysDat.aColormap),
92 0 : pRenderFormat(pSysDat.pXRenderFormat)
93 0 : {}
94 :
95 0 : X11SysData::X11SysData( const SystemEnvData& pSysDat ) :
96 : pDisplay(pSysDat.pDisplay),
97 : hDrawable(pSysDat.aWindow),
98 : pVisual(pSysDat.pVisual),
99 : nScreen(pSysDat.nScreen),
100 : nDepth(pSysDat.nDepth),
101 : aColormap(pSysDat.aColormap),
102 0 : pRenderFormat(NULL)
103 0 : {}
104 :
105 0 : X11Pixmap::~X11Pixmap()
106 : {
107 0 : if( mpDisplay && mhDrawable )
108 0 : XFreePixmap( (Display*)mpDisplay, mhDrawable );
109 0 : }
110 :
111 : /**
112 : * Surface::Surface: Create Canvas surface with existing data
113 : * @param pSysData Platform native system environment data (struct SystemEnvData in vcl/inc/sysdata.hxx)
114 : * @param pSurface Cairo surface
115 : *
116 : * pSysData contains the platform native Drawable reference
117 : * This constructor only stores data, it does no processing.
118 : * It is used by e.g. Surface::getSimilar()
119 : *
120 : * Set the mpSurface as pSurface
121 : **/
122 0 : X11Surface::X11Surface( const X11SysData& rSysData,
123 : const X11PixmapSharedPtr& rPixmap,
124 : const CairoSurfaceSharedPtr& pSurface ) :
125 : maSysData(rSysData),
126 : mpPixmap(rPixmap),
127 0 : mpSurface(pSurface)
128 0 : {}
129 :
130 : /**
131 : * Surface::Surface: Create generic Canvas surface using given Cairo Surface
132 : *
133 : * @param pSurface Cairo Surface
134 : *
135 : * This constructor only stores data, it does no processing.
136 : * It is used with e.g. cairo_image_surface_create_for_data()
137 : * Unlike other constructors, mpSysData is set to NULL
138 : *
139 : * Set the mpSurface as pSurface
140 : **/
141 0 : X11Surface::X11Surface( const CairoSurfaceSharedPtr& pSurface ) :
142 : maSysData(),
143 : mpPixmap(),
144 0 : mpSurface(pSurface)
145 0 : {}
146 :
147 : /**
148 : * Surface::Surface: Create Canvas surface from Window reference.
149 : * @param pSysData Platform native system environment data (struct SystemEnvData in vcl/inc/sysdata.hxx)
150 : * @param x horizontal location of the new surface
151 : * @param y vertical location of the new surface
152 : * @param width width of the new surface
153 : * @param height height of the new surface
154 : *
155 : * pSysData contains the platform native Window reference.
156 : *
157 : * pSysData is used to create a surface on the Window
158 : *
159 : * Set the mpSurface to the new surface or NULL
160 : **/
161 0 : X11Surface::X11Surface( const X11SysData& rSysData, int x, int y, int width, int height ) :
162 : maSysData(rSysData),
163 : mpPixmap(),
164 : mpSurface(
165 : cairo_xlib_surface_create( (Display*)rSysData.pDisplay,
166 : rSysData.hDrawable,
167 : (Visual*)rSysData.pVisual,
168 : width + x, height + y ),
169 0 : &cairo_surface_destroy)
170 : {
171 0 : cairo_surface_set_device_offset(mpSurface.get(), x, y );
172 0 : }
173 :
174 : /**
175 : * Surface::Surface: Create platform native Canvas surface from BitmapSystemData
176 : * @param pSysData Platform native system environment data (struct SystemEnvData in vcl/inc/sysdata.hxx)
177 : * @param pBmpData Platform native image data (struct BitmapSystemData in vcl/inc/bitmap.hxx)
178 : * @param width width of the new surface
179 : * @param height height of the new surface
180 : *
181 : * The pBmpData provides the imagedata that the created surface should contain.
182 : *
183 : * Set the mpSurface to the new surface or NULL
184 : **/
185 0 : X11Surface::X11Surface( const X11SysData& rSysData,
186 : const BitmapSystemData& rData ) :
187 : maSysData( rSysData ),
188 : mpPixmap(),
189 : mpSurface(
190 : cairo_xlib_surface_create( (Display*)rSysData.pDisplay,
191 : reinterpret_cast<Drawable>(rData.aPixmap),
192 : (Visual*) rSysData.pVisual,
193 : rData.mnWidth, rData.mnHeight ),
194 0 : &cairo_surface_destroy)
195 : {
196 0 : }
197 :
198 : /**
199 : * Surface::getCairo: Create Cairo (drawing object) for the Canvas surface
200 : *
201 : * @return new Cairo or NULL
202 : **/
203 0 : CairoSharedPtr X11Surface::getCairo() const
204 : {
205 : return CairoSharedPtr( cairo_create(mpSurface.get()),
206 0 : &cairo_destroy );
207 : }
208 :
209 : /**
210 : * Surface::getSimilar: Create new similar Canvas surface
211 : * @param aContent format of the new surface (cairo_content_t from cairo/src/cairo.h)
212 : * @param width width of the new surface
213 : * @param height height of the new surface
214 : *
215 : * Creates a new Canvas surface. This normally creates platform native surface, even though
216 : * generic function is used.
217 : *
218 : * Cairo surface from aContent (cairo_content_t)
219 : *
220 : * @return new surface or NULL
221 : **/
222 0 : SurfaceSharedPtr X11Surface::getSimilar( Content aContent, int width, int height ) const
223 : {
224 : Pixmap hPixmap;
225 :
226 0 : if( maSysData.pDisplay && maSysData.hDrawable )
227 : {
228 : XRenderPictFormat* pFormat;
229 : int nFormat;
230 :
231 0 : switch (aContent)
232 : {
233 : case CAIRO_CONTENT_ALPHA:
234 0 : nFormat = PictStandardA8;
235 0 : break;
236 : case CAIRO_CONTENT_COLOR:
237 0 : nFormat = PictStandardRGB24;
238 0 : break;
239 : case CAIRO_CONTENT_COLOR_ALPHA:
240 : default:
241 0 : nFormat = PictStandardARGB32;
242 0 : break;
243 : }
244 :
245 0 : pFormat = XRenderFindStandardFormat( (Display*)maSysData.pDisplay, nFormat );
246 : hPixmap = limitXCreatePixmap( (Display*)maSysData.pDisplay, maSysData.hDrawable,
247 : width > 0 ? width : 1, height > 0 ? height : 1,
248 0 : pFormat->depth );
249 :
250 0 : X11SysData aSysData(maSysData);
251 0 : aSysData.pRenderFormat = pFormat;
252 : return SurfaceSharedPtr(
253 : new X11Surface( aSysData,
254 : X11PixmapSharedPtr(
255 0 : new X11Pixmap(hPixmap, maSysData.pDisplay)),
256 : CairoSurfaceSharedPtr(
257 : cairo_xlib_surface_create_with_xrender_format(
258 : (Display*)maSysData.pDisplay,
259 : hPixmap,
260 : ScreenOfDisplay((Display *)maSysData.pDisplay, maSysData.nScreen),
261 : pFormat, width, height ),
262 0 : &cairo_surface_destroy) ));
263 : }
264 : else
265 : return SurfaceSharedPtr(
266 : new X11Surface( maSysData,
267 : X11PixmapSharedPtr(),
268 : CairoSurfaceSharedPtr(
269 : cairo_surface_create_similar( mpSurface.get(), aContent, width, height ),
270 0 : &cairo_surface_destroy )));
271 : }
272 :
273 0 : boost::shared_ptr<VirtualDevice> X11Surface::createVirtualDevice() const
274 : {
275 0 : SystemGraphicsData aSystemGraphicsData;
276 :
277 0 : aSystemGraphicsData.nSize = sizeof(SystemGraphicsData);
278 0 : aSystemGraphicsData.hDrawable = getDrawable();
279 0 : aSystemGraphicsData.pXRenderFormat = getRenderFormat();
280 :
281 : return boost::shared_ptr<VirtualDevice>(
282 0 : new VirtualDevice( &aSystemGraphicsData, std::max( getDepth(), 0 ) ));
283 : }
284 :
285 : /**
286 : * Surface::Resize: Resizes the Canvas surface.
287 : * @param width new width of the surface
288 : * @param height new height of the surface
289 : *
290 : * Only used on X11.
291 : *
292 : * @return The new surface or NULL
293 : **/
294 0 : void X11Surface::Resize( int width, int height )
295 : {
296 0 : cairo_xlib_surface_set_size( mpSurface.get(), width, height );
297 0 : }
298 :
299 0 : void X11Surface::flush() const
300 : {
301 0 : XSync( (Display*)maSysData.pDisplay, false );
302 0 : }
303 :
304 : /**
305 : * Surface::getDepth: Get the color depth of the Canvas surface.
306 : *
307 : * @return color depth
308 : **/
309 0 : int X11Surface::getDepth() const
310 : {
311 0 : if( maSysData.pRenderFormat )
312 0 : return ((XRenderPictFormat*) maSysData.pRenderFormat)->depth;
313 :
314 0 : return -1;
315 : }
316 :
317 0 : SurfaceSharedPtr createSurface( const CairoSurfaceSharedPtr& rSurface )
318 : {
319 0 : return SurfaceSharedPtr(new X11Surface(rSurface));
320 : }
321 :
322 0 : static X11SysData getSysData( const vcl::Window& rWindow )
323 : {
324 0 : const SystemEnvData* pSysData = GetSysData(&rWindow);
325 :
326 0 : if( !pSysData )
327 0 : return X11SysData();
328 : else
329 0 : return X11SysData(*pSysData);
330 : }
331 :
332 0 : static X11SysData getSysData( const VirtualDevice& rVirDev )
333 : {
334 0 : return X11SysData( rVirDev.GetSystemGfxData() );
335 : }
336 :
337 0 : SurfaceSharedPtr createSurface( const OutputDevice& rRefDevice,
338 : int x, int y, int width, int height )
339 : {
340 0 : if( rRefDevice.GetOutDevType() == OUTDEV_WINDOW )
341 : return SurfaceSharedPtr(new X11Surface(getSysData(static_cast<const vcl::Window&>(rRefDevice)),
342 0 : x,y,width,height));
343 0 : else if( rRefDevice.GetOutDevType() == OUTDEV_VIRDEV )
344 : return SurfaceSharedPtr(new X11Surface(getSysData(static_cast<const VirtualDevice&>(rRefDevice)),
345 0 : x,y,width,height));
346 : else
347 0 : return SurfaceSharedPtr();
348 : }
349 :
350 0 : SurfaceSharedPtr createBitmapSurface( const OutputDevice& rRefDevice,
351 : const BitmapSystemData& rData,
352 : const Size& rSize )
353 : {
354 : OSL_TRACE( "requested size: %d x %d available size: %d x %d",
355 : rSize.Width(), rSize.Height(), rData.mnWidth, rData.mnHeight );
356 0 : if ( rData.mnWidth == rSize.Width() && rData.mnHeight == rSize.Height() )
357 : {
358 0 : if( rRefDevice.GetOutDevType() == OUTDEV_WINDOW )
359 0 : return SurfaceSharedPtr(new X11Surface(getSysData(static_cast<const vcl::Window&>(rRefDevice)), rData ));
360 0 : else if( rRefDevice.GetOutDevType() == OUTDEV_VIRDEV )
361 0 : return SurfaceSharedPtr(new X11Surface(getSysData(static_cast<const VirtualDevice&>(rRefDevice)), rData ));
362 : }
363 :
364 0 : return SurfaceSharedPtr();
365 : }
366 6 : }
367 :
368 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|