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 
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
84         BackBufferSharedPtr pBackBufferMask( new BackBuffer( rOutDevProvider->getOutDev(),
85                                                              true ) );
86         pBackBufferMask->setSize( aSize );
87 
88         // TODO(F1): Implement alpha vdev (could prolly enable
89         // antialiasing again, then)
90 
91         // disable font antialiasing (causes ugly shadows otherwise)
92         pBackBuffer->getOutDev().SetAntialiasing( ANTIALIASING_DISABLE_TEXT );
93         pBackBufferMask->getOutDev().SetAntialiasing( ANTIALIASING_DISABLE_TEXT );
94 
95         // set mask vdev drawmode, such that everything is painted
96         // black. That leaves us with a binary image, white for
97         // background, black for painted content
98         pBackBufferMask->getOutDev().SetDrawMode( DRAWMODE_BLACKLINE | DRAWMODE_BLACKFILL | DRAWMODE_BLACKTEXT |
99                                                   DRAWMODE_BLACKGRADIENT | DRAWMODE_BLACKBITMAP );
100 
101 
102         // setup canvas helper
103         // -------------------
104 
105         // always render into back buffer, don't preserve state (it's
106         // our private VDev, after all), have notion of alpha
107         maCanvasHelper.init( rDevice,
108                              pBackBuffer,
109                              false,
110                              true );
111         maCanvasHelper.setBackgroundOutDev( pBackBufferMask );
112 
113 
114         // setup sprite helper
115         // -------------------
116 
117         maSpriteHelper.init( rSpriteSize,
118                              rOwningSpriteCanvas,
119                              pBackBuffer,
120                              pBackBufferMask,
121                              bShowSpriteBounds );
122 
123         // clear sprite to 100% transparent
124         maCanvasHelper.clear();
125     }
126 
127     void SAL_CALL CanvasCustomSprite::disposing()
128     {
129         tools::LocalGuard aGuard;
130 
131         // forward to parent
132         CanvasCustomSpriteBaseT::disposing();
133     }
134 
135 #define IMPLEMENTATION_NAME "VCLCanvas.CanvasCustomSprite"
136 #define SERVICE_NAME "com.sun.star.rendering.CanvasCustomSprite"
137 
138     ::rtl::OUString SAL_CALL CanvasCustomSprite::getImplementationName() throw( uno::RuntimeException )
139     {
140         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
141     }
142 
143     sal_Bool SAL_CALL CanvasCustomSprite::supportsService( const ::rtl::OUString& ServiceName ) throw( uno::RuntimeException )
144     {
145         return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
146     }
147 
148     uno::Sequence< ::rtl::OUString > SAL_CALL CanvasCustomSprite::getSupportedServiceNames()  throw( uno::RuntimeException )
149     {
150         uno::Sequence< ::rtl::OUString > aRet(1);
151         aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
152 
153         return aRet;
154     }
155 
156     // Sprite
157     void CanvasCustomSprite::redraw( OutputDevice& rOutDev,
158                                      bool          bBufferedUpdate ) const
159     {
160         tools::LocalGuard aGuard;
161 
162         redraw( rOutDev, maSpriteHelper.getPosPixel(), bBufferedUpdate );
163     }
164 
165     void CanvasCustomSprite::redraw( OutputDevice&              rOutDev,
166                                      const ::basegfx::B2DPoint& rOrigOutputPos,
167                                      bool                       bBufferedUpdate ) const
168     {
169         tools::LocalGuard aGuard;
170 
171         maSpriteHelper.redraw( rOutDev,
172                                rOrigOutputPos,
173                                mbSurfaceDirty,
174                                bBufferedUpdate );
175 
176         mbSurfaceDirty = false;
177     }
178 
179     bool CanvasCustomSprite::repaint( const GraphicObjectSharedPtr&	rGrf,
180                                       const rendering::ViewState&   viewState,
181                                       const rendering::RenderState& renderState,
182                                       const ::Point& 				rPt,
183                                       const ::Size& 				rSz,
184                                       const GraphicAttr&			rAttr ) const
185     {
186         tools::LocalGuard aGuard;
187 
188         mbSurfaceDirty = true;
189 
190         return maCanvasHelper.repaint( rGrf, viewState, renderState, rPt, rSz, rAttr );
191     }
192 
193 }
194