1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_canvas.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <canvas/debug.hxx> 32*cdf0e10cSrcweir #include <canvas/canvastools.hxx> 33*cdf0e10cSrcweir #include <tools/diagnose_ex.h> 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include "cairo_canvasbitmap.hxx" 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir #ifdef CAIRO_HAS_XLIB_SURFACE 38*cdf0e10cSrcweir # include "cairo_xlib_cairo.hxx" 39*cdf0e10cSrcweir #elif defined CAIRO_HAS_QUARTZ_SURFACE 40*cdf0e10cSrcweir # include "cairo_quartz_cairo.hxx" 41*cdf0e10cSrcweir #elif defined CAIRO_HAS_WIN32_SURFACE 42*cdf0e10cSrcweir # include "cairo_win32_cairo.hxx" 43*cdf0e10cSrcweir # include <cairo-win32.h> 44*cdf0e10cSrcweir #else 45*cdf0e10cSrcweir # error Native API needed. 46*cdf0e10cSrcweir #endif 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir using namespace ::cairo; 49*cdf0e10cSrcweir using namespace ::com::sun::star; 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir #ifdef CAIRO_HAS_WIN32_SURFACE 52*cdf0e10cSrcweir namespace 53*cdf0e10cSrcweir { 54*cdf0e10cSrcweir HBITMAP surface2HBitmap( const SurfaceSharedPtr& rSurface, const basegfx::B2ISize& rSize ) 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir // cant seem to retrieve HBITMAP from cairo. copy content then 57*cdf0e10cSrcweir HDC hScreenDC=GetDC(NULL); 58*cdf0e10cSrcweir HBITMAP hBmpBitmap = CreateCompatibleBitmap( hScreenDC, 59*cdf0e10cSrcweir rSize.getX(), 60*cdf0e10cSrcweir rSize.getY() ); 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir HDC hBmpDC = CreateCompatibleDC( 0 ); 63*cdf0e10cSrcweir HBITMAP hBmpOld = (HBITMAP) SelectObject( hBmpDC, hBmpBitmap ); 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir BitBlt( hBmpDC, 0, 0, rSize.getX(), rSize.getX(), 66*cdf0e10cSrcweir cairo_win32_surface_get_dc(rSurface->getCairoSurface().get()), 67*cdf0e10cSrcweir 0, 0, SRCCOPY ); 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir SelectObject( hBmpDC, hBmpOld ); 70*cdf0e10cSrcweir DeleteDC( hBmpDC ); 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir return hBmpBitmap; 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir #endif 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir namespace cairocanvas 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir CanvasBitmap::CanvasBitmap( const ::basegfx::B2ISize& rSize, 80*cdf0e10cSrcweir const SurfaceProviderRef& rSurfaceProvider, 81*cdf0e10cSrcweir rendering::XGraphicDevice* pDevice, 82*cdf0e10cSrcweir bool bHasAlpha ) : 83*cdf0e10cSrcweir mpSurfaceProvider( rSurfaceProvider ), 84*cdf0e10cSrcweir mpBufferSurface(), 85*cdf0e10cSrcweir mpBufferCairo(), 86*cdf0e10cSrcweir maSize(rSize), 87*cdf0e10cSrcweir mbHasAlpha(bHasAlpha) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir ENSURE_OR_THROW( mpSurfaceProvider.is(), 90*cdf0e10cSrcweir "CanvasBitmap::CanvasBitmap(): Invalid surface or device" ); 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir OSL_TRACE( "bitmap size: %dx%d", rSize.getX(), rSize.getY() ); 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir mpBufferSurface = mpSurfaceProvider->createSurface( rSize, bHasAlpha ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR ); 95*cdf0e10cSrcweir mpBufferCairo = mpBufferSurface->getCairo(); 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir maCanvasHelper.init( rSize, *mpSurfaceProvider, pDevice ); 98*cdf0e10cSrcweir maCanvasHelper.setSurface( mpBufferSurface, bHasAlpha ); 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir // clear bitmap to 100% transparent 101*cdf0e10cSrcweir maCanvasHelper.clear(); 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir void SAL_CALL CanvasBitmap::disposing() 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir mpSurfaceProvider.clear(); 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir mpBufferCairo.reset(); 109*cdf0e10cSrcweir mpBufferSurface.reset(); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir // forward to parent 112*cdf0e10cSrcweir CanvasBitmap_Base::disposing(); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir SurfaceSharedPtr CanvasBitmap::getSurface() 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir return mpBufferSurface; 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir SurfaceSharedPtr CanvasBitmap::createSurface( const ::basegfx::B2ISize& rSize, Content aContent ) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir return mpSurfaceProvider->createSurface(rSize,aContent); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir SurfaceSharedPtr CanvasBitmap::createSurface( ::Bitmap& rBitmap ) 126*cdf0e10cSrcweir { 127*cdf0e10cSrcweir return mpSurfaceProvider->createSurface(rBitmap); 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir SurfaceSharedPtr CanvasBitmap::changeSurface( bool, bool ) 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir // non-modifiable surface here 133*cdf0e10cSrcweir return SurfaceSharedPtr(); 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir OutputDevice* CanvasBitmap::getOutputDevice() 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir return mpSurfaceProvider->getOutputDevice(); 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir bool CanvasBitmap::repaint( const SurfaceSharedPtr& pSurface, 142*cdf0e10cSrcweir const rendering::ViewState& viewState, 143*cdf0e10cSrcweir const rendering::RenderState& renderState ) 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir return maCanvasHelper.repaint( pSurface, viewState, renderState ); 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir uno::Any SAL_CALL CanvasBitmap::getFastPropertyValue( sal_Int32 nHandle ) throw (uno::RuntimeException) 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir uno::Any aRV( sal_Int32(0) ); 151*cdf0e10cSrcweir // 0 ... get BitmapEx 152*cdf0e10cSrcweir // 1 ... get Pixbuf with bitmap RGB content 153*cdf0e10cSrcweir // 2 ... get Pixbuf with bitmap alpha mask 154*cdf0e10cSrcweir switch( nHandle ) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir case 0: 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir aRV = uno::Any( reinterpret_cast<sal_Int64>( (BitmapEx*) NULL ) ); 159*cdf0e10cSrcweir break; 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir case 1: 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir #ifdef CAIRO_HAS_XLIB_SURFACE 164*cdf0e10cSrcweir X11Surface* pXlibSurface=dynamic_cast<X11Surface*>(mpBufferSurface.get()); 165*cdf0e10cSrcweir OSL_ASSERT(pXlibSurface); 166*cdf0e10cSrcweir uno::Sequence< uno::Any > args( 3 ); 167*cdf0e10cSrcweir args[0] = uno::Any( false ); // do not call XFreePixmap on it 168*cdf0e10cSrcweir args[1] = uno::Any( pXlibSurface->getPixmap()->mhDrawable ); 169*cdf0e10cSrcweir args[2] = uno::Any( sal_Int32( pXlibSurface->getDepth() ) ); 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir aRV = uno::Any( args ); 172*cdf0e10cSrcweir #elif defined CAIRO_HAS_QUARTZ_SURFACE 173*cdf0e10cSrcweir QuartzSurface* pQuartzSurface = dynamic_cast<QuartzSurface*>(mpBufferSurface.get()); 174*cdf0e10cSrcweir OSL_ASSERT(pQuartzSurface); 175*cdf0e10cSrcweir uno::Sequence< uno::Any > args( 1 ); 176*cdf0e10cSrcweir args[0] = uno::Any( sal_IntPtr (pQuartzSurface->getCGContext()) ); 177*cdf0e10cSrcweir aRV = uno::Any( args ); 178*cdf0e10cSrcweir #elif defined CAIRO_HAS_WIN32_SURFACE 179*cdf0e10cSrcweir // TODO(F2): check whether under all circumstances, 180*cdf0e10cSrcweir // the alpha channel is ignored here. 181*cdf0e10cSrcweir uno::Sequence< uno::Any > args( 1 ); 182*cdf0e10cSrcweir args[1] = uno::Any( sal_Int64(surface2HBitmap(mpBufferSurface,maSize)) ); 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir aRV = uno::Any( args ); 185*cdf0e10cSrcweir // caller frees the bitmap 186*cdf0e10cSrcweir #else 187*cdf0e10cSrcweir # error Please define fast prop retrieval for your platform! 188*cdf0e10cSrcweir #endif 189*cdf0e10cSrcweir break; 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir case 2: 192*cdf0e10cSrcweir { 193*cdf0e10cSrcweir #ifdef CAIRO_HAS_XLIB_SURFACE 194*cdf0e10cSrcweir uno::Sequence< uno::Any > args( 3 ); 195*cdf0e10cSrcweir SurfaceSharedPtr pAlphaSurface = mpSurfaceProvider->createSurface( maSize, CAIRO_CONTENT_COLOR ); 196*cdf0e10cSrcweir CairoSharedPtr pAlphaCairo = pAlphaSurface->getCairo(); 197*cdf0e10cSrcweir X11Surface* pXlibSurface=dynamic_cast<X11Surface*>(pAlphaSurface.get()); 198*cdf0e10cSrcweir OSL_ASSERT(pXlibSurface); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir // create RGB image (levels of gray) of alpha channel of original picture 201*cdf0e10cSrcweir cairo_set_source_rgba( pAlphaCairo.get(), 1, 1, 1, 1 ); 202*cdf0e10cSrcweir cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_SOURCE ); 203*cdf0e10cSrcweir cairo_paint( pAlphaCairo.get() ); 204*cdf0e10cSrcweir cairo_set_source_surface( pAlphaCairo.get(), mpBufferSurface->getCairoSurface().get(), 0, 0 ); 205*cdf0e10cSrcweir cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_XOR ); 206*cdf0e10cSrcweir cairo_paint( pAlphaCairo.get() ); 207*cdf0e10cSrcweir pAlphaCairo.reset(); 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir X11PixmapSharedPtr pPixmap = pXlibSurface->getPixmap(); 210*cdf0e10cSrcweir args[0] = uno::Any( true ); 211*cdf0e10cSrcweir args[1] = ::com::sun::star::uno::Any( pPixmap->mhDrawable ); 212*cdf0e10cSrcweir args[2] = ::com::sun::star::uno::Any( sal_Int32( pXlibSurface->getDepth () ) ); 213*cdf0e10cSrcweir pPixmap->clear(); // caller takes ownership of pixmap 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir // return pixmap and alphachannel pixmap - it will be used in BitmapEx 216*cdf0e10cSrcweir aRV = uno::Any( args ); 217*cdf0e10cSrcweir #elif defined CAIRO_HAS_QUARTZ_SURFACE 218*cdf0e10cSrcweir SurfaceSharedPtr pAlphaSurface = mpSurfaceProvider->createSurface( maSize, CAIRO_CONTENT_COLOR ); 219*cdf0e10cSrcweir CairoSharedPtr pAlphaCairo = pAlphaSurface->getCairo(); 220*cdf0e10cSrcweir QuartzSurface* pQuartzSurface=dynamic_cast<QuartzSurface*>(pAlphaSurface.get()); 221*cdf0e10cSrcweir OSL_ASSERT(pQuartzSurface); 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir // create RGB image (levels of gray) of alpha channel of original picture 224*cdf0e10cSrcweir cairo_set_source_rgba( pAlphaCairo.get(), 1, 1, 1, 1 ); 225*cdf0e10cSrcweir cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_SOURCE ); 226*cdf0e10cSrcweir cairo_paint( pAlphaCairo.get() ); 227*cdf0e10cSrcweir cairo_set_source_surface( pAlphaCairo.get(), mpBufferSurface->getCairoSurface().get(), 0, 0 ); 228*cdf0e10cSrcweir cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_XOR ); 229*cdf0e10cSrcweir cairo_paint( pAlphaCairo.get() ); 230*cdf0e10cSrcweir pAlphaCairo.reset(); 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir uno::Sequence< uno::Any > args( 1 ); 233*cdf0e10cSrcweir args[0] = uno::Any( sal_IntPtr (pQuartzSurface->getCGContext()) ); 234*cdf0e10cSrcweir // return ??? and alphachannel ??? - it will be used in BitmapEx 235*cdf0e10cSrcweir aRV = uno::Any( args ); 236*cdf0e10cSrcweir #elif defined CAIRO_HAS_WIN32_SURFACE 237*cdf0e10cSrcweir SurfaceSharedPtr pAlphaSurface = mpSurfaceProvider->createSurface( maSize, CAIRO_CONTENT_COLOR ); 238*cdf0e10cSrcweir CairoSharedPtr pAlphaCairo = pAlphaSurface->getCairo(); 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir // create RGB image (levels of gray) of alpha channel of original picture 241*cdf0e10cSrcweir cairo_set_source_rgba( pAlphaCairo.get(), 1, 1, 1, 1 ); 242*cdf0e10cSrcweir cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_SOURCE ); 243*cdf0e10cSrcweir cairo_paint( pAlphaCairo.get() ); 244*cdf0e10cSrcweir cairo_set_source_surface( pAlphaCairo.get(), mpBufferSurface->getCairoSurface().get(), 0, 0 ); 245*cdf0e10cSrcweir cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_XOR ); 246*cdf0e10cSrcweir cairo_paint( pAlphaCairo.get() ); 247*cdf0e10cSrcweir pAlphaCairo.reset(); 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir // cant seem to retrieve HBITMAP from cairo. copy content then 250*cdf0e10cSrcweir uno::Sequence< uno::Any > args( 1 ); 251*cdf0e10cSrcweir args[1] = uno::Any( sal_Int64(surface2HBitmap(pAlphaSurface,maSize)) ); 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir aRV = uno::Any( args ); 254*cdf0e10cSrcweir // caller frees the bitmap 255*cdf0e10cSrcweir #else 256*cdf0e10cSrcweir # error Please define fast prop retrieval for your platform! 257*cdf0e10cSrcweir #endif 258*cdf0e10cSrcweir break; 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir } 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir return aRV; 263*cdf0e10cSrcweir } 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir #define IMPLEMENTATION_NAME "CairoCanvas.CanvasBitmap" 266*cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.rendering.CanvasBitmap" 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir ::rtl::OUString SAL_CALL CanvasBitmap::getImplementationName( ) throw (uno::RuntimeException) 269*cdf0e10cSrcweir { 270*cdf0e10cSrcweir return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) ); 271*cdf0e10cSrcweir } 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir sal_Bool SAL_CALL CanvasBitmap::supportsService( const ::rtl::OUString& ServiceName ) throw (uno::RuntimeException) 274*cdf0e10cSrcweir { 275*cdf0e10cSrcweir return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) ); 276*cdf0e10cSrcweir } 277*cdf0e10cSrcweir 278*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL CanvasBitmap::getSupportedServiceNames( ) throw (uno::RuntimeException) 279*cdf0e10cSrcweir { 280*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > aRet(1); 281*cdf0e10cSrcweir aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) ); 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir return aRet; 284*cdf0e10cSrcweir } 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir } 287