LCOV - code coverage report
Current view: top level - libreoffice/canvas/source/vcl - spritehelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 129 0.0 %
Date: 2012-12-27 Functions: 0 7 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             : 
      21             : #include <canvas/debug.hxx>
      22             : #include <tools/diagnose_ex.h>
      23             : #include <canvas/verbosetrace.hxx>
      24             : 
      25             : #include <rtl/math.hxx>
      26             : 
      27             : #include <vcl/outdev.hxx>
      28             : #include <vcl/bitmap.hxx>
      29             : #include <vcl/alpha.hxx>
      30             : #include <vcl/bitmapex.hxx>
      31             : #include <vcl/canvastools.hxx>
      32             : 
      33             : #include <basegfx/matrix/b2dhommatrix.hxx>
      34             : #include <basegfx/point/b2dpoint.hxx>
      35             : #include <basegfx/tools/canvastools.hxx>
      36             : #include <basegfx/polygon/b2dpolygon.hxx>
      37             : #include <basegfx/polygon/b2dpolygontools.hxx>
      38             : #include <basegfx/polygon/b2dpolypolygontools.hxx>
      39             : #include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
      40             : #include <basegfx/polygon/b2dpolygontriangulator.hxx>
      41             : #include <basegfx/polygon/b2dpolygonclipper.hxx>
      42             : #include <basegfx/numeric/ftools.hxx>
      43             : 
      44             : #include <canvas/canvastools.hxx>
      45             : 
      46             : #include "spritehelper.hxx"
      47             : 
      48             : using namespace ::com::sun::star;
      49             : 
      50             : 
      51             : namespace vclcanvas
      52             : {
      53           0 :     SpriteHelper::SpriteHelper() :
      54             :         mpBackBuffer(),
      55             :         mpBackBufferMask(),
      56             :         maContent(),
      57           0 :         mbShowSpriteBounds(false)
      58             :     {
      59           0 :     }
      60             : 
      61           0 :     void SpriteHelper::init( const geometry::RealSize2D&               rSpriteSize,
      62             :                              const ::canvas::SpriteSurface::Reference& rOwningSpriteCanvas,
      63             :                              const BackBufferSharedPtr&                rBackBuffer,
      64             :                              const BackBufferSharedPtr&                rBackBufferMask,
      65             :                              bool                                      bShowSpriteBounds )
      66             :     {
      67           0 :         ENSURE_OR_THROW( rOwningSpriteCanvas.get() && rBackBuffer && rBackBufferMask,
      68             :                          "SpriteHelper::init(): Invalid sprite canvas or back buffer" );
      69             : 
      70           0 :         mpBackBuffer        = rBackBuffer;
      71           0 :         mpBackBufferMask    = rBackBufferMask;
      72           0 :         mbShowSpriteBounds  = bShowSpriteBounds;
      73             : 
      74           0 :         init( rSpriteSize, rOwningSpriteCanvas );
      75           0 :     }
      76             : 
      77           0 :     void SpriteHelper::disposing()
      78             :     {
      79           0 :         mpBackBuffer.reset();
      80           0 :         mpBackBufferMask.reset();
      81             : 
      82             :         // forward to parent
      83           0 :         CanvasCustomSpriteHelper::disposing();
      84           0 :     }
      85             : 
      86           0 :     void SpriteHelper::redraw( OutputDevice&                rTargetSurface,
      87             :                                const ::basegfx::B2DPoint&   rPos,
      88             :                                bool&                        io_bSurfacesDirty,
      89             :                                bool                         bBufferedUpdate ) const
      90             :     {
      91             :         (void)bBufferedUpdate; // not used on every platform
      92             : 
      93           0 :         if( !mpBackBuffer ||
      94           0 :             !mpBackBufferMask )
      95             :         {
      96           0 :             return; // we're disposed
      97             :         }
      98             : 
      99             :         // log output pos in device pixel
     100             :         VERBOSE_TRACE( "SpriteHelper::redraw(): output pos is (%f, %f)",
     101             :                        rPos.getX(),
     102             :                        rPos.getY() );
     103             : 
     104           0 :         const double fAlpha( getAlpha() );
     105             : 
     106           0 :         if( isActive() &&
     107           0 :             !::basegfx::fTools::equalZero( fAlpha ) )
     108             :         {
     109           0 :             const Point                 aEmptyPoint;
     110           0 :             const ::basegfx::B2DVector& rOrigOutputSize( getSizePixel() );
     111             : 
     112             :             // might get changed below (e.g. adapted for
     113             :             // transformations). IMPORTANT: both position and size are
     114             :             // rounded to integer values. From now on, only those
     115             :             // rounded values are used, to keep clip and content in
     116             :             // sync.
     117           0 :             ::Size  aOutputSize( ::vcl::unotools::sizeFromB2DSize( rOrigOutputSize ) );
     118           0 :             ::Point aOutPos( ::vcl::unotools::pointFromB2DPoint( rPos ) );
     119             : 
     120             : 
     121             :             // TODO(F3): Support for alpha-VDev
     122             : 
     123             :             // Do we have to update our bitmaps (necessary if virdev
     124             :             // was painted to, or transformation changed)?
     125             :             const bool bNeedBitmapUpdate( io_bSurfacesDirty ||
     126           0 :                                           hasTransformChanged() ||
     127           0 :                                           maContent->IsEmpty() );
     128             : 
     129             :             // updating content of sprite cache - surface is no
     130             :             // longer dirty in relation to our cache
     131           0 :             io_bSurfacesDirty = false;
     132           0 :             transformUpdated();
     133             : 
     134           0 :             if( bNeedBitmapUpdate )
     135             :             {
     136           0 :                 Bitmap aBmp( mpBackBuffer->getOutDev().GetBitmap( aEmptyPoint,
     137           0 :                                                                   aOutputSize ) );
     138             : 
     139           0 :                 if( isContentFullyOpaque() )
     140             :                 {
     141             :                     // optimized case: content canvas is fully
     142             :                     // opaque. Note: since we retrieved aBmp directly
     143             :                     // from an OutDev, it's already a 'display bitmap'
     144             :                     // on windows.
     145           0 :                     maContent = BitmapEx( aBmp );
     146             :                 }
     147             :                 else
     148             :                 {
     149             :                     // sprite content might contain alpha, create
     150             :                     // BmpEx, then.
     151           0 :                     Bitmap aMask( mpBackBufferMask->getOutDev().GetBitmap( aEmptyPoint,
     152           0 :                                                                            aOutputSize ) );
     153             : 
     154             :                     // bitmasks are much faster than alphamasks on some platforms
     155             :                     // so convert to bitmask if useful
     156             : #ifndef QUARTZ
     157           0 :                     if( aMask.GetBitCount() != 1 )
     158             :                     {
     159             :                         OSL_FAIL("CanvasCustomSprite::redraw(): Mask bitmap is not "
     160             :                                    "monochrome (performance!)");
     161           0 :                         aMask.MakeMono(255);
     162             :                     }
     163             : #endif
     164             : 
     165             :                     // Note: since we retrieved aBmp and aMask
     166             :                     // directly from an OutDev, it's already a
     167             :                     // 'display bitmap' on windows.
     168           0 :                     maContent = BitmapEx( aBmp, aMask );
     169           0 :                 }
     170             :             }
     171             : 
     172           0 :             ::basegfx::B2DHomMatrix aTransform( getTransformation() );
     173             : 
     174             :             // check whether matrix is "easy" to handle - pure
     175             :             // translations or scales are handled by OutputDevice
     176             :             // alone
     177           0 :             const bool bIdentityTransform( aTransform.isIdentity() );
     178             : 
     179             :             // make transformation absolute (put sprite to final
     180             :             // output position). Need to happen here, as we also have
     181             :             // to translate the clip polygon
     182           0 :             aTransform.translate( aOutPos.X(),
     183           0 :                                   aOutPos.Y() );
     184             : 
     185           0 :             if( !bIdentityTransform )
     186             :             {
     187           0 :                 if( !::basegfx::fTools::equalZero( aTransform.get(0,1) ) ||
     188           0 :                     !::basegfx::fTools::equalZero( aTransform.get(1,0) ) )
     189             :                 {
     190             :                     // "complex" transformation, employ affine
     191             :                     // transformator
     192             : 
     193             :                     // modify output position, to account for the fact
     194             :                     // that transformBitmap() always normalizes its output
     195             :                     // bitmap into the smallest enclosing box.
     196           0 :                     ::basegfx::B2DRectangle aDestRect;
     197             :                     ::canvas::tools::calcTransformedRectBounds( aDestRect,
     198             :                                                                 ::basegfx::B2DRectangle(0,
     199             :                                                                                         0,
     200             :                                                                                         rOrigOutputSize.getX(),
     201             :                                                                                         rOrigOutputSize.getY()),
     202           0 :                                                                 aTransform );
     203             : 
     204           0 :                     aOutPos.X() = ::basegfx::fround( aDestRect.getMinX() );
     205           0 :                     aOutPos.Y() = ::basegfx::fround( aDestRect.getMinY() );
     206             : 
     207             :                     // TODO(P3): Use optimized bitmap transformation here.
     208             : 
     209             :                     // actually re-create the bitmap ONLY if necessary
     210           0 :                     if( bNeedBitmapUpdate )
     211           0 :                         maContent = tools::transformBitmap( *maContent,
     212             :                                                             aTransform,
     213             :                                                             uno::Sequence<double>(),
     214           0 :                                                             tools::MODULATE_NONE );
     215             : 
     216           0 :                     aOutputSize = maContent->GetSizePixel();
     217             :                 }
     218             :                 else
     219             :                 {
     220             :                     // relatively 'simplistic' transformation -
     221             :                     // retrieve scale and translational offset
     222             :                     aOutputSize.setWidth (
     223           0 :                         ::basegfx::fround( rOrigOutputSize.getX() * aTransform.get(0,0) ) );
     224             :                     aOutputSize.setHeight(
     225           0 :                         ::basegfx::fround( rOrigOutputSize.getY() * aTransform.get(1,1) ) );
     226             : 
     227           0 :                     aOutPos.X() = ::basegfx::fround( aTransform.get(0,2) );
     228           0 :                     aOutPos.Y() = ::basegfx::fround( aTransform.get(1,2) );
     229             :                 }
     230             :             }
     231             : 
     232             :             // transformBitmap() might return empty bitmaps, for tiny
     233             :             // scales.
     234           0 :             if( !!(*maContent) )
     235             :             {
     236             :                 // when true, fast path for slide transition has
     237             :                 // already redrawn the sprite.
     238           0 :                 bool bSpriteRedrawn( false );
     239             : 
     240           0 :                 rTargetSurface.Push( PUSH_CLIPREGION );
     241             : 
     242             :                 // apply clip (if any)
     243           0 :                 if( getClip().is() )
     244             :                 {
     245             :                     ::basegfx::B2DPolyPolygon aClipPoly(
     246             :                         ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(
     247           0 :                             getClip() ));
     248             : 
     249           0 :                     if( aClipPoly.count() )
     250             :                     {
     251             :                         // aTransform already contains the
     252             :                         // translational component, moving the clip to
     253             :                         // the final sprite output position.
     254           0 :                         aClipPoly.transform( aTransform );
     255             : 
     256             : #if ! defined WNT && ! defined QUARTZ
     257             :                         // non-Windows only - bAtLeastOnePolygon is
     258             :                         // only used in non-WNT code below
     259             : 
     260             :                         // check whether maybe the clip consists
     261             :                         // solely out of rectangular polygons. If this
     262             :                         // is the case, enforce using the triangle
     263             :                         // clip region setup - non-optimized X11
     264             :                         // drivers tend to perform abyssmally on
     265             :                         // XPolygonRegion, which is used internally,
     266             :                         // when filling complex polypolygons.
     267           0 :                         bool bAtLeastOnePolygon( false );
     268           0 :                         const sal_Int32 nPolygons( aClipPoly.count() );
     269             : 
     270           0 :                         for( sal_Int32 i=0; i<nPolygons; ++i )
     271             :                         {
     272           0 :                             if( !::basegfx::tools::isRectangle(
     273           0 :                                     aClipPoly.getB2DPolygon(i)) )
     274             :                             {
     275           0 :                                 bAtLeastOnePolygon = true;
     276           0 :                                 break;
     277             :                             }
     278             :                         }
     279             : #endif
     280             : 
     281           0 :                         if( mbShowSpriteBounds )
     282             :                         {
     283             :                             // Paint green sprite clip area
     284           0 :                             rTargetSurface.SetLineColor( Color( 0,255,0 ) );
     285           0 :                             rTargetSurface.SetFillColor();
     286             : 
     287           0 :                             rTargetSurface.DrawPolyPolygon(PolyPolygon(aClipPoly)); // #i76339#
     288             :                         }
     289             : 
     290             : #if ! defined WNT && ! defined QUARTZ
     291             :                         // as a matter of fact, this fast path only
     292             :                         // performs well for X11 - under Windows, the
     293             :                         // clip via SetTriangleClipRegion is faster.
     294           0 :                         if( bAtLeastOnePolygon &&
     295             :                             bBufferedUpdate &&
     296           0 :                             ::rtl::math::approxEqual(fAlpha, 1.0) &&
     297           0 :                             !maContent->IsTransparent() )
     298             :                         {
     299             :                             // fast path for slide transitions
     300             :                             // (buffered, no alpha, no mask (because
     301             :                             // full slide is contained in the sprite))
     302             : 
     303             :                             // XOR bitmap onto backbuffer, clear area
     304             :                             // that should be _visible_ with black,
     305             :                             // XOR bitmap again on top of that -
     306             :                             // result: XOR cancels out where no black
     307             :                             // has been rendered, and yields the
     308             :                             // original bitmap, where black is
     309             :                             // underneath.
     310           0 :                             rTargetSurface.Push( PUSH_RASTEROP );
     311           0 :                             rTargetSurface.SetRasterOp( ROP_XOR );
     312             :                             rTargetSurface.DrawBitmap( aOutPos,
     313             :                                                        aOutputSize,
     314           0 :                                                        maContent->GetBitmap() );
     315             : 
     316           0 :                             rTargetSurface.SetLineColor();
     317           0 :                             rTargetSurface.SetFillColor( COL_BLACK );
     318           0 :                             rTargetSurface.SetRasterOp( ROP_0 );
     319           0 :                             rTargetSurface.DrawPolyPolygon(PolyPolygon(aClipPoly)); // #i76339#
     320             : 
     321           0 :                             rTargetSurface.SetRasterOp( ROP_XOR );
     322             :                             rTargetSurface.DrawBitmap( aOutPos,
     323             :                                                        aOutputSize,
     324           0 :                                                        maContent->GetBitmap() );
     325             : 
     326           0 :                             rTargetSurface.Pop();
     327             : 
     328           0 :                             bSpriteRedrawn = true;
     329             :                         }
     330             :                         else
     331             : #endif
     332             :                         {
     333           0 :                             Region aClipRegion( aClipPoly );
     334           0 :                             rTargetSurface.SetClipRegion( aClipRegion );
     335             :                         }
     336           0 :                     }
     337             :                 }
     338             : 
     339           0 :                 if( !bSpriteRedrawn )
     340             :                 {
     341           0 :                     if( ::rtl::math::approxEqual(fAlpha, 1.0) )
     342             :                     {
     343             :                         // no alpha modulation -> just copy to output
     344           0 :                         if( maContent->IsTransparent() )
     345           0 :                             rTargetSurface.DrawBitmapEx( aOutPos, aOutputSize, *maContent );
     346             :                         else
     347           0 :                             rTargetSurface.DrawBitmap( aOutPos, aOutputSize, maContent->GetBitmap() );
     348             :                     }
     349             :                     else
     350             :                     {
     351             :                         // TODO(P3): Switch to OutputDevice::DrawTransparent()
     352             :                         // here
     353             : 
     354             :                         // draw semi-transparent
     355           0 :                         sal_uInt8 nColor( static_cast<sal_uInt8>( ::basegfx::fround( 255.0*(1.0 - fAlpha) + .5) ) );
     356           0 :                         AlphaMask aAlpha( maContent->GetSizePixel(),
     357           0 :                                           &nColor );
     358             : 
     359             :                         // mask out fully transparent areas
     360           0 :                         if( maContent->IsTransparent() )
     361           0 :                             aAlpha.Replace( maContent->GetMask(), 255 );
     362             : 
     363             :                         // alpha-blend to output
     364             :                         rTargetSurface.DrawBitmapEx( aOutPos, aOutputSize,
     365             :                                                      BitmapEx( maContent->GetBitmap(),
     366           0 :                                                                aAlpha ) );
     367             :                     }
     368             :                 }
     369             : 
     370           0 :                 rTargetSurface.Pop();
     371             : 
     372           0 :                 if( mbShowSpriteBounds )
     373             :                 {
     374             :                     ::PolyPolygon aMarkerPoly(
     375             :                         ::canvas::tools::getBoundMarksPolyPolygon(
     376           0 :                             ::basegfx::B2DRectangle(aOutPos.X(),
     377           0 :                                                     aOutPos.Y(),
     378           0 :                                                     aOutPos.X() + aOutputSize.Width()-1,
     379           0 :                                                     aOutPos.Y() + aOutputSize.Height()-1) ) );
     380             : 
     381             :                     // Paint little red sprite area markers
     382           0 :                     rTargetSurface.SetLineColor( COL_RED );
     383           0 :                     rTargetSurface.SetFillColor();
     384             : 
     385           0 :                     for( int i=0; i<aMarkerPoly.Count(); ++i )
     386             :                     {
     387           0 :                         rTargetSurface.DrawPolyLine( aMarkerPoly.GetObject((sal_uInt16)i) );
     388             :                     }
     389             : 
     390             :                     // paint sprite prio
     391           0 :                     Font aVCLFont;
     392           0 :                     aVCLFont.SetHeight( std::min(long(20),aOutputSize.Height()) );
     393           0 :                     aVCLFont.SetColor( COL_RED );
     394             : 
     395           0 :                     rTargetSurface.SetTextAlign(ALIGN_TOP);
     396           0 :                     rTargetSurface.SetTextColor( COL_RED );
     397           0 :                     rTargetSurface.SetFont( aVCLFont );
     398             : 
     399             :                     ::rtl::OUString text( ::rtl::math::doubleToUString( getPriority(),
     400             :                                                                         rtl_math_StringFormat_F,
     401           0 :                                                                         2,'.',NULL,' ') );
     402             : 
     403           0 :                     rTargetSurface.DrawText( aOutPos+Point(2,2), text );
     404             :                     VERBOSE_TRACE( "SpriteHelper::redraw(): sprite %X has prio %f\n",
     405           0 :                                    this, getPriority() );
     406             :                 }
     407           0 :             }
     408             :         }
     409             :     }
     410             : 
     411           0 :     ::basegfx::B2DPolyPolygon SpriteHelper::polyPolygonFromXPolyPolygon2D( uno::Reference< rendering::XPolyPolygon2D >& xPoly ) const
     412             :     {
     413           0 :         return ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D( xPoly );
     414             :     }
     415             : 
     416           0 : }
     417             : 
     418             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10