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