LCOV - code coverage report
Current view: top level - canvas/source/cairo - cairo_canvasbitmap.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 89 0.0 %
Date: 2014-04-14 Functions: 0 14 0.0 %
Legend: Lines: hit not hit

          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 <canvas/debug.hxx>
      21             : #include <canvas/canvastools.hxx>
      22             : #include <cppuhelper/supportsservice.hxx>
      23             : #include <tools/diagnose_ex.h>
      24             : 
      25             : #include "cairo_canvasbitmap.hxx"
      26             : 
      27             : #include <vcl/bmpacc.hxx>
      28             : #include <vcl/bitmapex.hxx>
      29             : 
      30             : #ifdef CAIRO_HAS_XLIB_SURFACE
      31             : # include "cairo_xlib_cairo.hxx"
      32             : #elif defined CAIRO_HAS_QUARTZ_SURFACE
      33             : # include "cairo_quartz_cairo.hxx"
      34             : #elif defined CAIRO_HAS_WIN32_SURFACE
      35             : # include "cairo_win32_cairo.hxx"
      36             : # include <cairo-win32.h>
      37             : #else
      38             : # error Native API needed.
      39             : #endif
      40             : 
      41             : using namespace ::cairo;
      42             : using namespace ::com::sun::star;
      43             : 
      44             : #ifdef CAIRO_HAS_WIN32_SURFACE
      45             : namespace
      46             : {
      47             :     HBITMAP surface2HBitmap( const SurfaceSharedPtr& rSurface, const basegfx::B2ISize& rSize )
      48             :     {
      49             :         // cant seem to retrieve HBITMAP from cairo. copy content then
      50             :         HDC hScreenDC=GetDC(NULL);
      51             :         HBITMAP hBmpBitmap = CreateCompatibleBitmap( hScreenDC,
      52             :                                                      rSize.getX(),
      53             :                                                      rSize.getY() );
      54             : 
      55             :         HDC     hBmpDC = CreateCompatibleDC( 0 );
      56             :         HBITMAP hBmpOld = (HBITMAP) SelectObject( hBmpDC, hBmpBitmap );
      57             : 
      58             :         BitBlt( hBmpDC, 0, 0, rSize.getX(), rSize.getX(),
      59             :                 cairo_win32_surface_get_dc(rSurface->getCairoSurface().get()),
      60             :                 0, 0, SRCCOPY );
      61             : 
      62             :         SelectObject( hBmpDC, hBmpOld );
      63             :         DeleteDC( hBmpDC );
      64             : 
      65             :         return hBmpBitmap;
      66             :     }
      67             : }
      68             : #endif
      69             : 
      70             : namespace cairocanvas
      71             : {
      72           0 :     CanvasBitmap::CanvasBitmap( const ::basegfx::B2ISize&  rSize,
      73             :                                 const SurfaceProviderRef&  rSurfaceProvider,
      74             :                                 rendering::XGraphicDevice* pDevice,
      75             :                                 bool                       bHasAlpha ) :
      76             :         mpSurfaceProvider( rSurfaceProvider ),
      77             :         mpBufferSurface(),
      78             :         mpBufferCairo(),
      79             :         maSize(rSize),
      80           0 :         mbHasAlpha(bHasAlpha)
      81             :     {
      82           0 :         ENSURE_OR_THROW( mpSurfaceProvider.is(),
      83             :                           "CanvasBitmap::CanvasBitmap(): Invalid surface or device" );
      84             : 
      85             :         OSL_TRACE( "bitmap size: %dx%d", rSize.getX(), rSize.getY() );
      86             : 
      87           0 :         mpBufferSurface = mpSurfaceProvider->createSurface( rSize, bHasAlpha ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR );
      88           0 :         mpBufferCairo = mpBufferSurface->getCairo();
      89             : 
      90           0 :         maCanvasHelper.init( rSize, *mpSurfaceProvider, pDevice );
      91           0 :         maCanvasHelper.setSurface( mpBufferSurface, bHasAlpha );
      92             : 
      93             :         // clear bitmap to 100% transparent
      94           0 :         maCanvasHelper.clear();
      95           0 :     }
      96             : 
      97           0 :     void CanvasBitmap::disposeThis()
      98             :     {
      99           0 :         mpSurfaceProvider.clear();
     100             : 
     101           0 :         mpBufferCairo.reset();
     102           0 :         mpBufferSurface.reset();
     103             : 
     104             :         // forward to parent
     105           0 :         CanvasBitmap_Base::disposeThis();
     106           0 :     }
     107             : 
     108           0 :     SurfaceSharedPtr CanvasBitmap::getSurface()
     109             :     {
     110           0 :         return mpBufferSurface;
     111             :     }
     112             : 
     113           0 :     SurfaceSharedPtr CanvasBitmap::createSurface( const ::basegfx::B2ISize& rSize, Content aContent )
     114             :     {
     115           0 :         return mpSurfaceProvider->createSurface(rSize,aContent);
     116             :     }
     117             : 
     118           0 :     SurfaceSharedPtr CanvasBitmap::createSurface( ::Bitmap& rBitmap )
     119             :     {
     120           0 :         return mpSurfaceProvider->createSurface(rBitmap);
     121             :     }
     122             : 
     123           0 :     SurfaceSharedPtr CanvasBitmap::changeSurface( bool, bool )
     124             :     {
     125             :         // non-modifiable surface here
     126           0 :         return SurfaceSharedPtr();
     127             :     }
     128             : 
     129           0 :     OutputDevice* CanvasBitmap::getOutputDevice()
     130             :     {
     131           0 :         return mpSurfaceProvider->getOutputDevice();
     132             :     }
     133             : 
     134           0 :     bool CanvasBitmap::repaint( const SurfaceSharedPtr&       pSurface,
     135             :                                 const rendering::ViewState&   viewState,
     136             :                                 const rendering::RenderState& renderState )
     137             :     {
     138           0 :         return maCanvasHelper.repaint( pSurface, viewState, renderState );
     139             :     }
     140             : 
     141           0 :     uno::Any SAL_CALL CanvasBitmap::getFastPropertyValue( sal_Int32 nHandle )  throw (uno::RuntimeException, std::exception)
     142             :     {
     143           0 :         uno::Any aRV( sal_Int32(0) );
     144             :         // 0 ... get BitmapEx
     145             :         // 1 ... get Pixbuf with bitmap RGB content
     146             :         // 2 ... get Pixbuf with bitmap alpha mask
     147           0 :         switch( nHandle )
     148             :         {
     149             :             case 0:
     150             :             {
     151           0 :                 aRV = uno::Any( reinterpret_cast<sal_Int64>( (BitmapEx *) NULL ) );
     152           0 :                 if ( !mbHasAlpha )
     153           0 :                     break;
     154             : 
     155           0 :                 ::Size aSize( maSize.getX(), maSize.getY() );
     156             :                 // FIXME: if we could teach VCL/ about cairo handles, life could
     157             :                 // be significantly better here perhaps.
     158             :                 cairo_surface_t *pPixels;
     159             :                 pPixels = cairo_image_surface_create( CAIRO_FORMAT_ARGB32,
     160           0 :                                                       aSize.Width(), aSize.Height() );
     161           0 :                 cairo_t *pCairo = cairo_create( pPixels );
     162           0 :                 if( !pPixels || !pCairo )
     163             :                     break;
     164             : 
     165             :                 // suck ourselves from the X server to this buffer so then we can fiddle with
     166             :                 // Alpha to turn it into the ultra-lame vcl required format and then push it
     167             :                 // all back again later at vast expense [ urgh ]
     168           0 :                 cairo_set_source_surface( pCairo, getSurface()->getCairoSurface().get(), 0, 0 );
     169           0 :                 cairo_set_operator( pCairo, CAIRO_OPERATOR_SOURCE );
     170           0 :                 cairo_paint( pCairo );
     171             : 
     172           0 :                 ::Bitmap aRGB( aSize, 24 );
     173           0 :                 ::AlphaMask aMask( aSize );
     174             : 
     175           0 :                 BitmapWriteAccess *pRGBWrite( aRGB.AcquireWriteAccess() );
     176           0 :                 if( pRGBWrite )
     177             :                 {
     178           0 :                     BitmapWriteAccess *pMaskWrite( aMask.AcquireWriteAccess() );
     179           0 :                     if( pMaskWrite )
     180             :                     {
     181           0 :                         cairo_surface_flush(pPixels);
     182           0 :                         unsigned char *pSrc = cairo_image_surface_get_data( pPixels );
     183           0 :                         unsigned int nStride = cairo_image_surface_get_stride( pPixels );
     184           0 :                         for( unsigned long y = 0; y < (unsigned long) aSize.Height(); y++ )
     185             :                         {
     186           0 :                             sal_uInt32 *pPix = (sal_uInt32 *)(pSrc + nStride * y);
     187           0 :                             for( unsigned long x = 0; x < (unsigned long) aSize.Width(); x++ )
     188             :                             {
     189           0 :                                 sal_uInt8 nAlpha = (*pPix >> 24);
     190           0 :                                 sal_uInt8 nR = (*pPix >> 16) & 0xff;
     191           0 :                                 sal_uInt8 nG = (*pPix >> 8) & 0xff;
     192           0 :                                 sal_uInt8 nB = *pPix & 0xff;
     193           0 :                                 if( nAlpha != 0 && nAlpha != 255 )
     194             :                                 {
     195             :                                     // Cairo uses pre-multiplied alpha - we do not => re-multiply
     196           0 :                                     nR = (sal_uInt8) MinMax( ((sal_uInt32)nR * 255) / nAlpha, 0, 255 );
     197           0 :                                     nG = (sal_uInt8) MinMax( ((sal_uInt32)nG * 255) / nAlpha, 0, 255 );
     198           0 :                                     nB = (sal_uInt8) MinMax( ((sal_uInt32)nB * 255) / nAlpha, 0, 255 );
     199             :                                 }
     200           0 :                                 pRGBWrite->SetPixel( y, x, BitmapColor( nR, nG, nB ) );
     201           0 :                                 pMaskWrite->SetPixelIndex( y, x, 255 - nAlpha );
     202           0 :                                 pPix++;
     203             :                             }
     204             :                         }
     205           0 :                         aMask.ReleaseAccess( pMaskWrite );
     206             :                     }
     207           0 :                     aRGB.ReleaseAccess( pRGBWrite );
     208             :                 }
     209             : 
     210             :                 // ignore potential errors above. will get caller a
     211             :                 // uniformely white bitmap, but not that there would
     212             :                 // be error handling in calling code ...
     213           0 :                 ::BitmapEx *pBitmapEx = new ::BitmapEx( aRGB, aMask );
     214             : 
     215           0 :                 cairo_destroy( pCairo );
     216           0 :                 cairo_surface_destroy( pPixels );
     217             : 
     218           0 :                 aRV = uno::Any( reinterpret_cast<sal_Int64>( pBitmapEx ) );
     219           0 :                 break;
     220             :             }
     221             :             case 1:
     222             :             {
     223             : #ifdef CAIRO_HAS_XLIB_SURFACE
     224           0 :                 X11Surface* pXlibSurface=dynamic_cast<X11Surface*>(mpBufferSurface.get());
     225             :                 OSL_ASSERT(pXlibSurface);
     226           0 :                 uno::Sequence< uno::Any > args( 3 );
     227           0 :                 args[0] = uno::Any( false );  // do not call XFreePixmap on it
     228           0 :                 args[1] = uno::Any( pXlibSurface->getPixmap()->mhDrawable );
     229           0 :                 args[2] = uno::Any( sal_Int32( pXlibSurface->getDepth() ) );
     230             : 
     231           0 :                 aRV = uno::Any( args );
     232             : #elif defined CAIRO_HAS_QUARTZ_SURFACE
     233             :                 QuartzSurface* pQuartzSurface = dynamic_cast<QuartzSurface*>(mpBufferSurface.get());
     234             :                 OSL_ASSERT(pQuartzSurface);
     235             :                 uno::Sequence< uno::Any > args( 1 );
     236             :                 args[0] = uno::Any( sal_IntPtr (pQuartzSurface->getCGContext()) );
     237             :                 aRV = uno::Any( args );
     238             : #elif defined CAIRO_HAS_WIN32_SURFACE
     239             :                 // TODO(F2): check whether under all circumstances,
     240             :                 // the alpha channel is ignored here.
     241             :                 uno::Sequence< uno::Any > args( 1 );
     242             :                 args[1] = uno::Any( sal_Int64(surface2HBitmap(mpBufferSurface,maSize)) );
     243             : 
     244             :                 aRV = uno::Any( args );
     245             :                 // caller frees the bitmap
     246             : #else
     247             : # error Please define fast prop retrieval for your platform!
     248             : #endif
     249           0 :                 break;
     250             :             }
     251             :             case 2:
     252             :             {
     253             :                 // Always return nothing - for the RGB surface support.
     254             :                 // Alpha code paths go via the above case 0.
     255           0 :                 aRV = uno::Any();
     256           0 :                 break;
     257             :             }
     258             :         }
     259             : 
     260           0 :         return aRV;
     261             :     }
     262             : 
     263           0 :     OUString SAL_CALL CanvasBitmap::getImplementationName(  ) throw (uno::RuntimeException, std::exception)
     264             :     {
     265           0 :         return OUString( "CairoCanvas.CanvasBitmap" );
     266             :     }
     267             : 
     268           0 :     sal_Bool SAL_CALL CanvasBitmap::supportsService( const OUString& ServiceName ) throw (uno::RuntimeException, std::exception)
     269             :     {
     270           0 :         return cppu::supportsService( this, ServiceName );
     271             :     }
     272             : 
     273           0 :     uno::Sequence< OUString > SAL_CALL CanvasBitmap::getSupportedServiceNames(  ) throw (uno::RuntimeException, std::exception)
     274             :     {
     275           0 :         uno::Sequence< OUString > aRet(1);
     276           0 :         aRet[0] = "com.sun.star.rendering.CanvasBitmap";
     277             : 
     278           0 :         return aRet;
     279             :     }
     280             : 
     281           0 : }
     282             : 
     283             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10