1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_canvas.hxx"
26 
27 #include <canvas/debug.hxx>
28 #include <tools/diagnose_ex.h>
29 #include <canvas/verbosetrace.hxx>
30 
31 #include <rtl/math.hxx>
32 
33 #include <vcl/outdev.hxx>
34 #include <vcl/bitmap.hxx>
35 #include <vcl/alpha.hxx>
36 #include <vcl/bitmapex.hxx>
37 #include <vcl/canvastools.hxx>
38 
39 #include <basegfx/matrix/b2dhommatrix.hxx>
40 #include <basegfx/point/b2dpoint.hxx>
41 #include <basegfx/tools/canvastools.hxx>
42 #include <basegfx/polygon/b2dpolygon.hxx>
43 #include <basegfx/polygon/b2dpolygontools.hxx>
44 #include <basegfx/polygon/b2dpolypolygontools.hxx>
45 #include <basegfx/numeric/ftools.hxx>
46 
47 #include <canvas/canvastools.hxx>
48 
49 #include "canvascustomsprite.hxx"
50 
51 
52 using namespace ::com::sun::star;
53 
54 
55 namespace vclcanvas
56 {
57 
CanvasCustomSprite(const geometry::RealSize2D & rSpriteSize,rendering::XGraphicDevice & rDevice,const::canvas::SpriteSurface::Reference & rOwningSpriteCanvas,const OutDevProviderSharedPtr & rOutDevProvider,bool bShowSpriteBounds)58     CanvasCustomSprite::CanvasCustomSprite( const geometry::RealSize2D&               rSpriteSize,
59                                             rendering::XGraphicDevice&                rDevice,
60                                             const ::canvas::SpriteSurface::Reference& rOwningSpriteCanvas,
61                                             const OutDevProviderSharedPtr&            rOutDevProvider,
62                                             bool                                      bShowSpriteBounds )
63     {
64         ENSURE_OR_THROW( rOwningSpriteCanvas.get() &&
65                          rOutDevProvider,
66                          "CanvasCustomSprite::CanvasCustomSprite(): Invalid sprite canvas" );
67 
68         // setup back buffer
69         // -----------------
70 
71         const ::Size aSize(
72             static_cast<sal_Int32>( ::std::max( 1.0,
73                                                 ceil( rSpriteSize.Width ))),  // round up to nearest int,
74                 															  // enforce sprite to have at
75                 											 				  // least (1,1) pixel size
76             static_cast<sal_Int32>( ::std::max( 1.0,
77                                                 ceil( rSpriteSize.Height ))) );
78 
79         // create content backbuffer in screen depth
80         BackBufferSharedPtr pBackBuffer( new BackBuffer( rOutDevProvider->getOutDev() ) );
81         pBackBuffer->setSize( aSize );
82 
83         // create mask backbuffer, with one bit color depth #122485# use full depth to avoid problem with 1bit depth, get AAed masks
84         BackBufferSharedPtr pBackBufferMask(
85 #if defined LINUX || defined FREEBSD || defined NETBSD
86             // #122485# no 1bit buffers on Linuxes, 1bit Vdev seems to work no longer
87             new BackBuffer( rOutDevProvider->getOutDev() ) );
88 #else
89             // 1bit mask buffer for all others
90             new BackBuffer( rOutDevProvider->getOutDev(), true ) );
91 #endif
92         pBackBufferMask->setSize( aSize );
93 
94         // TODO(F1): Implement alpha vdev (could prolly enable
95         // antialiasing again, then)
96 
97         // disable font antialiasing (causes ugly shadows otherwise)
98         pBackBuffer->getOutDev().SetAntialiasing( ANTIALIASING_DISABLE_TEXT );
99         pBackBufferMask->getOutDev().SetAntialiasing( ANTIALIASING_DISABLE_TEXT );
100 
101         // set mask vdev drawmode, such that everything is painted
102         // black. That leaves us with a binary image, white for
103         // background, black for painted content
104         pBackBufferMask->getOutDev().SetDrawMode( DRAWMODE_BLACKLINE | DRAWMODE_BLACKFILL | DRAWMODE_BLACKTEXT |
105                                                   DRAWMODE_BLACKGRADIENT | DRAWMODE_BLACKBITMAP );
106 
107 
108         // setup canvas helper
109         // -------------------
110 
111         // always render into back buffer, don't preserve state (it's
112         // our private VDev, after all), have notion of alpha
113         maCanvasHelper.init( rDevice,
114                              pBackBuffer,
115                              false,
116                              true );
117         maCanvasHelper.setBackgroundOutDev( pBackBufferMask );
118 
119 
120         // setup sprite helper
121         // -------------------
122 
123         maSpriteHelper.init( rSpriteSize,
124                              rOwningSpriteCanvas,
125                              pBackBuffer,
126                              pBackBufferMask,
127                              bShowSpriteBounds );
128 
129         // clear sprite to 100% transparent
130         maCanvasHelper.clear();
131     }
132 
disposing()133     void SAL_CALL CanvasCustomSprite::disposing()
134     {
135         tools::LocalGuard aGuard;
136 
137         // forward to parent
138         CanvasCustomSpriteBaseT::disposing();
139     }
140 
141 #define IMPLEMENTATION_NAME "VCLCanvas.CanvasCustomSprite"
142 #define SERVICE_NAME "com.sun.star.rendering.CanvasCustomSprite"
143 
getImplementationName()144     ::rtl::OUString SAL_CALL CanvasCustomSprite::getImplementationName() throw( uno::RuntimeException )
145     {
146         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
147     }
148 
supportsService(const::rtl::OUString & ServiceName)149     sal_Bool SAL_CALL CanvasCustomSprite::supportsService( const ::rtl::OUString& ServiceName ) throw( uno::RuntimeException )
150     {
151         return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
152     }
153 
getSupportedServiceNames()154     uno::Sequence< ::rtl::OUString > SAL_CALL CanvasCustomSprite::getSupportedServiceNames()  throw( uno::RuntimeException )
155     {
156         uno::Sequence< ::rtl::OUString > aRet(1);
157         aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
158 
159         return aRet;
160     }
161 
162     // Sprite
redraw(OutputDevice & rOutDev,bool bBufferedUpdate) const163     void CanvasCustomSprite::redraw( OutputDevice& rOutDev,
164                                      bool          bBufferedUpdate ) const
165     {
166         tools::LocalGuard aGuard;
167 
168         redraw( rOutDev, maSpriteHelper.getPosPixel(), bBufferedUpdate );
169     }
170 
redraw(OutputDevice & rOutDev,const::basegfx::B2DPoint & rOrigOutputPos,bool bBufferedUpdate) const171     void CanvasCustomSprite::redraw( OutputDevice&              rOutDev,
172                                      const ::basegfx::B2DPoint& rOrigOutputPos,
173                                      bool                       bBufferedUpdate ) const
174     {
175         tools::LocalGuard aGuard;
176 
177         maSpriteHelper.redraw( rOutDev,
178                                rOrigOutputPos,
179                                mbSurfaceDirty,
180                                bBufferedUpdate );
181 
182         mbSurfaceDirty = false;
183     }
184 
repaint(const GraphicObjectSharedPtr & rGrf,const rendering::ViewState & viewState,const rendering::RenderState & renderState,const::Point & rPt,const::Size & rSz,const GraphicAttr & rAttr) const185     bool CanvasCustomSprite::repaint( const GraphicObjectSharedPtr&	rGrf,
186                                       const rendering::ViewState&   viewState,
187                                       const rendering::RenderState& renderState,
188                                       const ::Point& 				rPt,
189                                       const ::Size& 				rSz,
190                                       const GraphicAttr&			rAttr ) const
191     {
192         tools::LocalGuard aGuard;
193 
194         mbSurfaceDirty = true;
195 
196         return maCanvasHelper.repaint( rGrf, viewState, renderState, rPt, rSz, rAttr );
197     }
198 
199 }
200