2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-10-08 10:00:18 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2006-02-28 09:33:08 +00:00
|
|
|
|
2015-08-10 14:04:31 -05:00
|
|
|
#include <sal/config.h>
|
|
|
|
|
2014-01-14 13:05:02 -02:00
|
|
|
#include <cppuhelper/supportsservice.hxx>
|
2008-06-24 09:19:20 +00:00
|
|
|
#include <tools/diagnose_ex.h>
|
2015-08-10 14:04:31 -05:00
|
|
|
#include <vcl/bitmapex.hxx>
|
2016-02-06 21:03:41 +11:00
|
|
|
#include <vcl/bitmapaccess.hxx>
|
2006-02-28 09:33:08 +00:00
|
|
|
|
2015-08-10 14:04:31 -05:00
|
|
|
#include <canvas/canvastools.hxx>
|
2006-02-28 09:33:08 +00:00
|
|
|
|
2015-08-10 14:04:31 -05:00
|
|
|
#include "cairo_canvasbitmap.hxx"
|
2013-06-10 17:02:06 +01:00
|
|
|
|
2006-02-28 09:33:08 +00:00
|
|
|
using namespace ::cairo;
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
|
|
|
|
namespace cairocanvas
|
|
|
|
{
|
2008-06-24 09:19:20 +00:00
|
|
|
CanvasBitmap::CanvasBitmap( const ::basegfx::B2ISize& rSize,
|
|
|
|
const SurfaceProviderRef& rSurfaceProvider,
|
|
|
|
rendering::XGraphicDevice* pDevice,
|
|
|
|
bool bHasAlpha ) :
|
|
|
|
mpSurfaceProvider( rSurfaceProvider ),
|
|
|
|
mpBufferSurface(),
|
|
|
|
mpBufferCairo(),
|
|
|
|
maSize(rSize),
|
|
|
|
mbHasAlpha(bHasAlpha)
|
2006-02-28 09:33:08 +00:00
|
|
|
{
|
2008-06-24 09:19:20 +00:00
|
|
|
ENSURE_OR_THROW( mpSurfaceProvider.is(),
|
2006-02-28 09:33:08 +00:00
|
|
|
"CanvasBitmap::CanvasBitmap(): Invalid surface or device" );
|
|
|
|
|
2015-03-13 18:20:18 +01:00
|
|
|
SAL_INFO(
|
|
|
|
"canvas.cairo",
|
|
|
|
"bitmap size: " << rSize.getX() << "x" << rSize.getY());
|
2006-02-28 09:33:08 +00:00
|
|
|
|
2008-06-24 09:19:20 +00:00
|
|
|
mpBufferSurface = mpSurfaceProvider->createSurface( rSize, bHasAlpha ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR );
|
2006-02-28 09:33:08 +00:00
|
|
|
mpBufferCairo = mpBufferSurface->getCairo();
|
|
|
|
|
2008-06-24 09:19:20 +00:00
|
|
|
maCanvasHelper.init( rSize, *mpSurfaceProvider, pDevice );
|
2006-02-28 09:33:08 +00:00
|
|
|
maCanvasHelper.setSurface( mpBufferSurface, bHasAlpha );
|
|
|
|
|
2008-06-24 09:19:20 +00:00
|
|
|
// clear bitmap to 100% transparent
|
|
|
|
maCanvasHelper.clear();
|
2006-02-28 09:33:08 +00:00
|
|
|
}
|
|
|
|
|
2011-04-08 16:44:28 +02:00
|
|
|
void CanvasBitmap::disposeThis()
|
2006-02-28 09:33:08 +00:00
|
|
|
{
|
2008-06-24 09:19:20 +00:00
|
|
|
mpSurfaceProvider.clear();
|
2006-02-28 09:33:08 +00:00
|
|
|
|
2008-06-24 09:19:20 +00:00
|
|
|
mpBufferCairo.reset();
|
|
|
|
mpBufferSurface.reset();
|
2006-02-28 09:33:08 +00:00
|
|
|
|
|
|
|
// forward to parent
|
2011-04-08 16:44:28 +02:00
|
|
|
CanvasBitmap_Base::disposeThis();
|
2006-02-28 09:33:08 +00:00
|
|
|
}
|
|
|
|
|
2008-06-24 09:19:20 +00:00
|
|
|
SurfaceSharedPtr CanvasBitmap::getSurface()
|
2006-02-28 09:33:08 +00:00
|
|
|
{
|
|
|
|
return mpBufferSurface;
|
|
|
|
}
|
|
|
|
|
2015-03-19 14:43:08 +00:00
|
|
|
SurfaceSharedPtr CanvasBitmap::createSurface( const ::basegfx::B2ISize& rSize, int aContent )
|
2008-06-24 09:19:20 +00:00
|
|
|
{
|
|
|
|
return mpSurfaceProvider->createSurface(rSize,aContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceSharedPtr CanvasBitmap::createSurface( ::Bitmap& rBitmap )
|
|
|
|
{
|
|
|
|
return mpSurfaceProvider->createSurface(rBitmap);
|
|
|
|
}
|
|
|
|
|
2016-06-01 12:54:31 +02:00
|
|
|
SurfaceSharedPtr CanvasBitmap::changeSurface()
|
2008-06-24 09:19:20 +00:00
|
|
|
{
|
|
|
|
// non-modifiable surface here
|
|
|
|
return SurfaceSharedPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputDevice* CanvasBitmap::getOutputDevice()
|
|
|
|
{
|
|
|
|
return mpSurfaceProvider->getOutputDevice();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CanvasBitmap::repaint( const SurfaceSharedPtr& pSurface,
|
|
|
|
const rendering::ViewState& viewState,
|
|
|
|
const rendering::RenderState& renderState )
|
2006-02-28 09:33:08 +00:00
|
|
|
{
|
|
|
|
return maCanvasHelper.repaint( pSurface, viewState, renderState );
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
uno::Any SAL_CALL CanvasBitmap::getFastPropertyValue( sal_Int32 nHandle )
|
2008-06-24 09:19:20 +00:00
|
|
|
{
|
|
|
|
uno::Any aRV( sal_Int32(0) );
|
|
|
|
// 0 ... get BitmapEx
|
|
|
|
// 1 ... get Pixbuf with bitmap RGB content
|
2017-01-27 12:10:03 +02:00
|
|
|
// 2 ... return nothing (empty Any)
|
2008-06-24 09:19:20 +00:00
|
|
|
switch( nHandle )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
2015-06-08 16:23:21 +02:00
|
|
|
aRV = uno::Any( reinterpret_cast<sal_Int64>( nullptr ) );
|
2013-06-10 17:02:06 +01:00
|
|
|
if ( !mbHasAlpha )
|
|
|
|
break;
|
|
|
|
|
|
|
|
::Size aSize( maSize.getX(), maSize.getY() );
|
|
|
|
// FIXME: if we could teach VCL/ about cairo handles, life could
|
|
|
|
// be significantly better here perhaps.
|
|
|
|
cairo_surface_t *pPixels;
|
|
|
|
pPixels = cairo_image_surface_create( CAIRO_FORMAT_ARGB32,
|
|
|
|
aSize.Width(), aSize.Height() );
|
|
|
|
cairo_t *pCairo = cairo_create( pPixels );
|
2014-04-17 11:56:17 +01:00
|
|
|
if( !pPixels || !pCairo || cairo_status(pCairo) != CAIRO_STATUS_SUCCESS )
|
2013-06-10 17:02:06 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
// suck ourselves from the X server to this buffer so then we can fiddle with
|
|
|
|
// Alpha to turn it into the ultra-lame vcl required format and then push it
|
|
|
|
// all back again later at vast expense [ urgh ]
|
|
|
|
cairo_set_source_surface( pCairo, getSurface()->getCairoSurface().get(), 0, 0 );
|
|
|
|
cairo_set_operator( pCairo, CAIRO_OPERATOR_SOURCE );
|
|
|
|
cairo_paint( pCairo );
|
|
|
|
|
|
|
|
::Bitmap aRGB( aSize, 24 );
|
|
|
|
::AlphaMask aMask( aSize );
|
|
|
|
|
|
|
|
BitmapWriteAccess *pRGBWrite( aRGB.AcquireWriteAccess() );
|
2013-06-13 12:47:02 +02:00
|
|
|
if( pRGBWrite )
|
2013-06-10 17:02:06 +01:00
|
|
|
{
|
2013-06-13 12:47:02 +02:00
|
|
|
BitmapWriteAccess *pMaskWrite( aMask.AcquireWriteAccess() );
|
|
|
|
if( pMaskWrite )
|
2013-06-10 17:02:06 +01:00
|
|
|
{
|
2013-06-13 12:47:02 +02:00
|
|
|
cairo_surface_flush(pPixels);
|
|
|
|
unsigned char *pSrc = cairo_image_surface_get_data( pPixels );
|
|
|
|
unsigned int nStride = cairo_image_surface_get_stride( pPixels );
|
|
|
|
for( unsigned long y = 0; y < (unsigned long) aSize.Height(); y++ )
|
2013-06-10 17:02:06 +01:00
|
|
|
{
|
2015-01-17 18:14:57 +01:00
|
|
|
sal_uInt32 *pPix = reinterpret_cast<sal_uInt32 *>(pSrc + nStride * y);
|
2013-06-13 12:47:02 +02:00
|
|
|
for( unsigned long x = 0; x < (unsigned long) aSize.Width(); x++ )
|
|
|
|
{
|
2016-06-27 20:37:25 +02:00
|
|
|
#if defined OSL_BIGENDIAN
|
|
|
|
sal_uInt8 nB = (*pPix >> 24);
|
|
|
|
sal_uInt8 nG = (*pPix >> 16) & 0xff;
|
|
|
|
sal_uInt8 nR = (*pPix >> 8) & 0xff;
|
|
|
|
sal_uInt8 nAlpha = *pPix & 0xff;
|
|
|
|
#else
|
2013-06-13 12:47:02 +02:00
|
|
|
sal_uInt8 nAlpha = (*pPix >> 24);
|
|
|
|
sal_uInt8 nR = (*pPix >> 16) & 0xff;
|
|
|
|
sal_uInt8 nG = (*pPix >> 8) & 0xff;
|
|
|
|
sal_uInt8 nB = *pPix & 0xff;
|
2016-06-27 20:37:25 +02:00
|
|
|
#endif
|
2013-06-13 12:47:02 +02:00
|
|
|
if( nAlpha != 0 && nAlpha != 255 )
|
|
|
|
{
|
|
|
|
// Cairo uses pre-multiplied alpha - we do not => re-multiply
|
|
|
|
nR = (sal_uInt8) MinMax( ((sal_uInt32)nR * 255) / nAlpha, 0, 255 );
|
|
|
|
nG = (sal_uInt8) MinMax( ((sal_uInt32)nG * 255) / nAlpha, 0, 255 );
|
|
|
|
nB = (sal_uInt8) MinMax( ((sal_uInt32)nB * 255) / nAlpha, 0, 255 );
|
|
|
|
}
|
|
|
|
pRGBWrite->SetPixel( y, x, BitmapColor( nR, nG, nB ) );
|
|
|
|
pMaskWrite->SetPixelIndex( y, x, 255 - nAlpha );
|
|
|
|
pPix++;
|
|
|
|
}
|
2013-06-10 17:02:06 +01:00
|
|
|
}
|
2013-06-13 12:47:02 +02:00
|
|
|
aMask.ReleaseAccess( pMaskWrite );
|
2013-06-10 17:02:06 +01:00
|
|
|
}
|
2015-04-01 16:23:59 +02:00
|
|
|
::Bitmap::ReleaseAccess( pRGBWrite );
|
2013-06-10 17:02:06 +01:00
|
|
|
}
|
|
|
|
|
2013-06-13 12:47:02 +02:00
|
|
|
// ignore potential errors above. will get caller a
|
|
|
|
// uniformely white bitmap, but not that there would
|
|
|
|
// be error handling in calling code ...
|
2013-06-10 17:02:06 +01:00
|
|
|
::BitmapEx *pBitmapEx = new ::BitmapEx( aRGB, aMask );
|
|
|
|
|
2013-06-13 12:47:02 +02:00
|
|
|
cairo_destroy( pCairo );
|
2013-06-10 17:02:06 +01:00
|
|
|
cairo_surface_destroy( pPixels );
|
|
|
|
|
|
|
|
aRV = uno::Any( reinterpret_cast<sal_Int64>( pBitmapEx ) );
|
2008-06-24 09:19:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
{
|
2015-03-19 14:43:08 +00:00
|
|
|
aRV = getOutputDevice()->GetNativeSurfaceHandle(mpBufferSurface, maSize);
|
2008-06-24 09:19:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
{
|
2013-06-10 17:02:06 +01:00
|
|
|
// Always return nothing - for the RGB surface support.
|
|
|
|
// Alpha code paths go via the above case 0.
|
|
|
|
aRV = uno::Any();
|
2008-06-24 09:19:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return aRV;
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
OUString SAL_CALL CanvasBitmap::getImplementationName( )
|
2006-02-28 09:33:08 +00:00
|
|
|
{
|
2014-01-14 13:05:02 -02:00
|
|
|
return OUString( "CairoCanvas.CanvasBitmap" );
|
2006-02-28 09:33:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
sal_Bool SAL_CALL CanvasBitmap::supportsService( const OUString& ServiceName )
|
2006-02-28 09:33:08 +00:00
|
|
|
{
|
2014-01-14 13:05:02 -02:00
|
|
|
return cppu::supportsService( this, ServiceName );
|
2006-02-28 09:33:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
uno::Sequence< OUString > SAL_CALL CanvasBitmap::getSupportedServiceNames( )
|
2006-02-28 09:33:08 +00:00
|
|
|
{
|
2016-08-01 15:00:00 +03:00
|
|
|
return { "com.sun.star.rendering.CanvasBitmap" };
|
2006-02-28 09:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2010-10-14 08:27:31 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|