LCOV - code coverage report
Current view: top level - canvas/source/cairo - cairo_canvasbitmap.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 61 85 71.8 %
Date: 2015-06-13 12:38:46 Functions: 6 14 42.9 %
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             : using namespace ::cairo;
      31             : using namespace ::com::sun::star;
      32             : 
      33             : namespace cairocanvas
      34             : {
      35           2 :     CanvasBitmap::CanvasBitmap( const ::basegfx::B2ISize&  rSize,
      36             :                                 const SurfaceProviderRef&  rSurfaceProvider,
      37             :                                 rendering::XGraphicDevice* pDevice,
      38             :                                 bool                       bHasAlpha ) :
      39             :         mpSurfaceProvider( rSurfaceProvider ),
      40             :         mpBufferSurface(),
      41             :         mpBufferCairo(),
      42             :         maSize(rSize),
      43           2 :         mbHasAlpha(bHasAlpha)
      44             :     {
      45           2 :         ENSURE_OR_THROW( mpSurfaceProvider.is(),
      46             :                           "CanvasBitmap::CanvasBitmap(): Invalid surface or device" );
      47             : 
      48             :         SAL_INFO(
      49             :             "canvas.cairo",
      50             :             "bitmap size: " << rSize.getX() << "x" << rSize.getY());
      51             : 
      52           2 :         mpBufferSurface = mpSurfaceProvider->createSurface( rSize, bHasAlpha ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR );
      53           2 :         mpBufferCairo = mpBufferSurface->getCairo();
      54             : 
      55           2 :         maCanvasHelper.init( rSize, *mpSurfaceProvider, pDevice );
      56           2 :         maCanvasHelper.setSurface( mpBufferSurface, bHasAlpha );
      57             : 
      58             :         // clear bitmap to 100% transparent
      59           2 :         maCanvasHelper.clear();
      60           2 :     }
      61             : 
      62           2 :     void CanvasBitmap::disposeThis()
      63             :     {
      64           2 :         mpSurfaceProvider.clear();
      65             : 
      66           2 :         mpBufferCairo.reset();
      67           2 :         mpBufferSurface.reset();
      68             : 
      69             :         // forward to parent
      70           2 :         CanvasBitmap_Base::disposeThis();
      71           2 :     }
      72             : 
      73           2 :     SurfaceSharedPtr CanvasBitmap::getSurface()
      74             :     {
      75           2 :         return mpBufferSurface;
      76             :     }
      77             : 
      78           0 :     SurfaceSharedPtr CanvasBitmap::createSurface( const ::basegfx::B2ISize& rSize, int aContent )
      79             :     {
      80           0 :         return mpSurfaceProvider->createSurface(rSize,aContent);
      81             :     }
      82             : 
      83           0 :     SurfaceSharedPtr CanvasBitmap::createSurface( ::Bitmap& rBitmap )
      84             :     {
      85           0 :         return mpSurfaceProvider->createSurface(rBitmap);
      86             :     }
      87             : 
      88           0 :     SurfaceSharedPtr CanvasBitmap::changeSurface( bool, bool )
      89             :     {
      90             :         // non-modifiable surface here
      91           0 :         return SurfaceSharedPtr();
      92             :     }
      93             : 
      94           0 :     OutputDevice* CanvasBitmap::getOutputDevice()
      95             :     {
      96           0 :         return mpSurfaceProvider->getOutputDevice();
      97             :     }
      98             : 
      99           0 :     bool CanvasBitmap::repaint( const SurfaceSharedPtr&       pSurface,
     100             :                                 const rendering::ViewState&   viewState,
     101             :                                 const rendering::RenderState& renderState )
     102             :     {
     103           0 :         return maCanvasHelper.repaint( pSurface, viewState, renderState );
     104             :     }
     105             : 
     106           2 :     uno::Any SAL_CALL CanvasBitmap::getFastPropertyValue( sal_Int32 nHandle )  throw (uno::RuntimeException, std::exception)
     107             :     {
     108           2 :         uno::Any aRV( sal_Int32(0) );
     109             :         // 0 ... get BitmapEx
     110             :         // 1 ... get Pixbuf with bitmap RGB content
     111             :         // 2 ... get Pixbuf with bitmap alpha mask
     112           2 :         switch( nHandle )
     113             :         {
     114             :             case 0:
     115             :             {
     116           2 :                 aRV = uno::Any( reinterpret_cast<sal_Int64>( nullptr ) );
     117           2 :                 if ( !mbHasAlpha )
     118           0 :                     break;
     119             : 
     120           2 :                 ::Size aSize( maSize.getX(), maSize.getY() );
     121             :                 // FIXME: if we could teach VCL/ about cairo handles, life could
     122             :                 // be significantly better here perhaps.
     123             :                 cairo_surface_t *pPixels;
     124             :                 pPixels = cairo_image_surface_create( CAIRO_FORMAT_ARGB32,
     125           2 :                                                       aSize.Width(), aSize.Height() );
     126           2 :                 cairo_t *pCairo = cairo_create( pPixels );
     127           2 :                 if( !pPixels || !pCairo || cairo_status(pCairo) != CAIRO_STATUS_SUCCESS )
     128           0 :                     break;
     129             : 
     130             :                 // suck ourselves from the X server to this buffer so then we can fiddle with
     131             :                 // Alpha to turn it into the ultra-lame vcl required format and then push it
     132             :                 // all back again later at vast expense [ urgh ]
     133           2 :                 cairo_set_source_surface( pCairo, getSurface()->getCairoSurface().get(), 0, 0 );
     134           2 :                 cairo_set_operator( pCairo, CAIRO_OPERATOR_SOURCE );
     135           2 :                 cairo_paint( pCairo );
     136             : 
     137           2 :                 ::Bitmap aRGB( aSize, 24 );
     138           4 :                 ::AlphaMask aMask( aSize );
     139             : 
     140           2 :                 BitmapWriteAccess *pRGBWrite( aRGB.AcquireWriteAccess() );
     141           2 :                 if( pRGBWrite )
     142             :                 {
     143           2 :                     BitmapWriteAccess *pMaskWrite( aMask.AcquireWriteAccess() );
     144           2 :                     if( pMaskWrite )
     145             :                     {
     146           2 :                         cairo_surface_flush(pPixels);
     147           2 :                         unsigned char *pSrc = cairo_image_surface_get_data( pPixels );
     148           2 :                         unsigned int nStride = cairo_image_surface_get_stride( pPixels );
     149         808 :                         for( unsigned long y = 0; y < (unsigned long) aSize.Height(); y++ )
     150             :                         {
     151         806 :                             sal_uInt32 *pPix = reinterpret_cast<sal_uInt32 *>(pSrc + nStride * y);
     152      432822 :                             for( unsigned long x = 0; x < (unsigned long) aSize.Width(); x++ )
     153             :                             {
     154      432016 :                                 sal_uInt8 nAlpha = (*pPix >> 24);
     155      432016 :                                 sal_uInt8 nR = (*pPix >> 16) & 0xff;
     156      432016 :                                 sal_uInt8 nG = (*pPix >> 8) & 0xff;
     157      432016 :                                 sal_uInt8 nB = *pPix & 0xff;
     158      432016 :                                 if( nAlpha != 0 && nAlpha != 255 )
     159             :                                 {
     160             :                                     // Cairo uses pre-multiplied alpha - we do not => re-multiply
     161        1872 :                                     nR = (sal_uInt8) MinMax( ((sal_uInt32)nR * 255) / nAlpha, 0, 255 );
     162        1872 :                                     nG = (sal_uInt8) MinMax( ((sal_uInt32)nG * 255) / nAlpha, 0, 255 );
     163        1872 :                                     nB = (sal_uInt8) MinMax( ((sal_uInt32)nB * 255) / nAlpha, 0, 255 );
     164             :                                 }
     165      432016 :                                 pRGBWrite->SetPixel( y, x, BitmapColor( nR, nG, nB ) );
     166      432016 :                                 pMaskWrite->SetPixelIndex( y, x, 255 - nAlpha );
     167      432016 :                                 pPix++;
     168             :                             }
     169             :                         }
     170           2 :                         aMask.ReleaseAccess( pMaskWrite );
     171             :                     }
     172           2 :                     ::Bitmap::ReleaseAccess( pRGBWrite );
     173             :                 }
     174             : 
     175             :                 // ignore potential errors above. will get caller a
     176             :                 // uniformely white bitmap, but not that there would
     177             :                 // be error handling in calling code ...
     178           2 :                 ::BitmapEx *pBitmapEx = new ::BitmapEx( aRGB, aMask );
     179             : 
     180           2 :                 cairo_destroy( pCairo );
     181           2 :                 cairo_surface_destroy( pPixels );
     182             : 
     183           2 :                 aRV = uno::Any( reinterpret_cast<sal_Int64>( pBitmapEx ) );
     184           4 :                 break;
     185             :             }
     186             :             case 1:
     187             :             {
     188           0 :                 aRV = getOutputDevice()->GetNativeSurfaceHandle(mpBufferSurface, maSize);
     189           0 :                 break;
     190             :             }
     191             :             case 2:
     192             :             {
     193             :                 // Always return nothing - for the RGB surface support.
     194             :                 // Alpha code paths go via the above case 0.
     195           0 :                 aRV = uno::Any();
     196           0 :                 break;
     197             :             }
     198             :         }
     199             : 
     200           2 :         return aRV;
     201             :     }
     202             : 
     203           0 :     OUString SAL_CALL CanvasBitmap::getImplementationName(  ) throw (uno::RuntimeException, std::exception)
     204             :     {
     205           0 :         return OUString( "CairoCanvas.CanvasBitmap" );
     206             :     }
     207             : 
     208           0 :     sal_Bool SAL_CALL CanvasBitmap::supportsService( const OUString& ServiceName ) throw (uno::RuntimeException, std::exception)
     209             :     {
     210           0 :         return cppu::supportsService( this, ServiceName );
     211             :     }
     212             : 
     213           0 :     uno::Sequence< OUString > SAL_CALL CanvasBitmap::getSupportedServiceNames(  ) throw (uno::RuntimeException, std::exception)
     214             :     {
     215           0 :         uno::Sequence< OUString > aRet(1);
     216           0 :         aRet[0] = "com.sun.star.rendering.CanvasBitmap";
     217             : 
     218           0 :         return aRet;
     219             :     }
     220             : 
     221           6 : }
     222             : 
     223             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11