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