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
10*25ea7f45SAndrew Rist  *
11*25ea7f45SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*25ea7f45SAndrew Rist  *
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.
19*25ea7f45SAndrew Rist  *
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 <canvas/canvastools.hxx>
29cdf0e10cSrcweir #include <tools/diagnose_ex.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "cairo_canvasbitmap.hxx"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #ifdef CAIRO_HAS_XLIB_SURFACE
34cdf0e10cSrcweir # include "cairo_xlib_cairo.hxx"
35cdf0e10cSrcweir #elif defined CAIRO_HAS_QUARTZ_SURFACE
36cdf0e10cSrcweir # include "cairo_quartz_cairo.hxx"
37cdf0e10cSrcweir #elif defined CAIRO_HAS_WIN32_SURFACE
38cdf0e10cSrcweir # include "cairo_win32_cairo.hxx"
39cdf0e10cSrcweir # include <cairo-win32.h>
40cdf0e10cSrcweir #else
41cdf0e10cSrcweir # error Native API needed.
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace ::cairo;
45cdf0e10cSrcweir using namespace ::com::sun::star;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #ifdef CAIRO_HAS_WIN32_SURFACE
48cdf0e10cSrcweir namespace
49cdf0e10cSrcweir {
surface2HBitmap(const SurfaceSharedPtr & rSurface,const basegfx::B2ISize & rSize)50cdf0e10cSrcweir     HBITMAP surface2HBitmap( const SurfaceSharedPtr& rSurface, const basegfx::B2ISize& rSize )
51cdf0e10cSrcweir     {
52cdf0e10cSrcweir         // cant seem to retrieve HBITMAP from cairo. copy content then
53cdf0e10cSrcweir         HDC hScreenDC=GetDC(NULL);
54cdf0e10cSrcweir         HBITMAP hBmpBitmap = CreateCompatibleBitmap( hScreenDC,
55cdf0e10cSrcweir                                                      rSize.getX(),
56cdf0e10cSrcweir                                                      rSize.getY() );
57cdf0e10cSrcweir 
58cdf0e10cSrcweir         HDC 	hBmpDC = CreateCompatibleDC( 0 );
59cdf0e10cSrcweir         HBITMAP hBmpOld = (HBITMAP) SelectObject( hBmpDC, hBmpBitmap );
60cdf0e10cSrcweir 
61cdf0e10cSrcweir         BitBlt( hBmpDC, 0, 0, rSize.getX(), rSize.getX(),
62cdf0e10cSrcweir                 cairo_win32_surface_get_dc(rSurface->getCairoSurface().get()),
63cdf0e10cSrcweir                 0, 0, SRCCOPY );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         SelectObject( hBmpDC, hBmpOld );
66cdf0e10cSrcweir         DeleteDC( hBmpDC );
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         return hBmpBitmap;
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir }
71cdf0e10cSrcweir #endif
72cdf0e10cSrcweir 
73cdf0e10cSrcweir namespace cairocanvas
74cdf0e10cSrcweir {
CanvasBitmap(const::basegfx::B2ISize & rSize,const SurfaceProviderRef & rSurfaceProvider,rendering::XGraphicDevice * pDevice,bool bHasAlpha)75cdf0e10cSrcweir     CanvasBitmap::CanvasBitmap( const ::basegfx::B2ISize&  rSize,
76cdf0e10cSrcweir                                 const SurfaceProviderRef&  rSurfaceProvider,
77cdf0e10cSrcweir                                 rendering::XGraphicDevice* pDevice,
78cdf0e10cSrcweir                                 bool                       bHasAlpha ) :
79cdf0e10cSrcweir         mpSurfaceProvider( rSurfaceProvider ),
80cdf0e10cSrcweir         mpBufferSurface(),
81cdf0e10cSrcweir         mpBufferCairo(),
82cdf0e10cSrcweir         maSize(rSize),
83cdf0e10cSrcweir         mbHasAlpha(bHasAlpha)
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         ENSURE_OR_THROW( mpSurfaceProvider.is(),
86cdf0e10cSrcweir                           "CanvasBitmap::CanvasBitmap(): Invalid surface or device" );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 		OSL_TRACE( "bitmap size: %dx%d", rSize.getX(), rSize.getY() );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 		mpBufferSurface = mpSurfaceProvider->createSurface( rSize, bHasAlpha ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR );
91cdf0e10cSrcweir 		mpBufferCairo = mpBufferSurface->getCairo();
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         maCanvasHelper.init( rSize, *mpSurfaceProvider, pDevice );
94cdf0e10cSrcweir 		maCanvasHelper.setSurface( mpBufferSurface, bHasAlpha );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         // clear bitmap to 100% transparent
97cdf0e10cSrcweir         maCanvasHelper.clear();
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir 
disposing()100cdf0e10cSrcweir     void SAL_CALL CanvasBitmap::disposing()
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         mpSurfaceProvider.clear();
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 		mpBufferCairo.reset();
105cdf0e10cSrcweir 		mpBufferSurface.reset();
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         // forward to parent
108cdf0e10cSrcweir         CanvasBitmap_Base::disposing();
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir 
getSurface()111cdf0e10cSrcweir     SurfaceSharedPtr CanvasBitmap::getSurface()
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         return mpBufferSurface;
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
createSurface(const::basegfx::B2ISize & rSize,Content aContent)116cdf0e10cSrcweir     SurfaceSharedPtr CanvasBitmap::createSurface( const ::basegfx::B2ISize& rSize, Content aContent )
117cdf0e10cSrcweir     {
118cdf0e10cSrcweir         return mpSurfaceProvider->createSurface(rSize,aContent);
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
createSurface(::Bitmap & rBitmap)121cdf0e10cSrcweir     SurfaceSharedPtr CanvasBitmap::createSurface( ::Bitmap& rBitmap )
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         return mpSurfaceProvider->createSurface(rBitmap);
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
changeSurface(bool,bool)126cdf0e10cSrcweir     SurfaceSharedPtr CanvasBitmap::changeSurface( bool, bool )
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         // non-modifiable surface here
129cdf0e10cSrcweir         return SurfaceSharedPtr();
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
getOutputDevice()132cdf0e10cSrcweir     OutputDevice* CanvasBitmap::getOutputDevice()
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         return mpSurfaceProvider->getOutputDevice();
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir 
repaint(const SurfaceSharedPtr & pSurface,const rendering::ViewState & viewState,const rendering::RenderState & renderState)137cdf0e10cSrcweir     bool CanvasBitmap::repaint( const SurfaceSharedPtr&       pSurface,
138cdf0e10cSrcweir 								const rendering::ViewState&	  viewState,
139cdf0e10cSrcweir 								const rendering::RenderState& renderState )
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir 		return maCanvasHelper.repaint( pSurface, viewState, renderState );
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
getFastPropertyValue(sal_Int32 nHandle)144cdf0e10cSrcweir     uno::Any SAL_CALL CanvasBitmap::getFastPropertyValue( sal_Int32 nHandle )  throw (uno::RuntimeException)
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         uno::Any aRV( sal_Int32(0) );
147cdf0e10cSrcweir         // 0 ... get BitmapEx
148cdf0e10cSrcweir         // 1 ... get Pixbuf with bitmap RGB content
149cdf0e10cSrcweir         // 2 ... get Pixbuf with bitmap alpha mask
150cdf0e10cSrcweir         switch( nHandle )
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir             case 0:
153cdf0e10cSrcweir             {
154cdf0e10cSrcweir                 aRV = uno::Any( reinterpret_cast<sal_Int64>( (BitmapEx*) NULL ) );
155cdf0e10cSrcweir                 break;
156cdf0e10cSrcweir             }
157cdf0e10cSrcweir             case 1:
158cdf0e10cSrcweir             {
159cdf0e10cSrcweir #ifdef CAIRO_HAS_XLIB_SURFACE
160cdf0e10cSrcweir                 X11Surface* pXlibSurface=dynamic_cast<X11Surface*>(mpBufferSurface.get());
161cdf0e10cSrcweir                 OSL_ASSERT(pXlibSurface);
162cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 3 );
163cdf0e10cSrcweir                 args[0] = uno::Any( false );  // do not call XFreePixmap on it
164cdf0e10cSrcweir                 args[1] = uno::Any( pXlibSurface->getPixmap()->mhDrawable );
165cdf0e10cSrcweir                 args[2] = uno::Any( sal_Int32( pXlibSurface->getDepth() ) );
166cdf0e10cSrcweir 
167cdf0e10cSrcweir                 aRV = uno::Any( args );
168cdf0e10cSrcweir #elif defined CAIRO_HAS_QUARTZ_SURFACE
169cdf0e10cSrcweir                 QuartzSurface* pQuartzSurface = dynamic_cast<QuartzSurface*>(mpBufferSurface.get());
170cdf0e10cSrcweir                 OSL_ASSERT(pQuartzSurface);
171cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 1 );
172cdf0e10cSrcweir                 args[0] = uno::Any( sal_IntPtr (pQuartzSurface->getCGContext()) );
173cdf0e10cSrcweir                 aRV = uno::Any( args );
174cdf0e10cSrcweir #elif defined CAIRO_HAS_WIN32_SURFACE
175cdf0e10cSrcweir                 // TODO(F2): check whether under all circumstances,
176cdf0e10cSrcweir                 // the alpha channel is ignored here.
177cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 1 );
178cdf0e10cSrcweir                 args[1] = uno::Any( sal_Int64(surface2HBitmap(mpBufferSurface,maSize)) );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir                 aRV = uno::Any( args );
181cdf0e10cSrcweir                 // caller frees the bitmap
182cdf0e10cSrcweir #else
183cdf0e10cSrcweir # error Please define fast prop retrieval for your platform!
184cdf0e10cSrcweir #endif
185cdf0e10cSrcweir                 break;
186cdf0e10cSrcweir             }
187cdf0e10cSrcweir             case 2:
188cdf0e10cSrcweir             {
189cdf0e10cSrcweir #ifdef CAIRO_HAS_XLIB_SURFACE
190cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 3 );
191cdf0e10cSrcweir                 SurfaceSharedPtr pAlphaSurface = mpSurfaceProvider->createSurface( maSize, CAIRO_CONTENT_COLOR );
192cdf0e10cSrcweir                 CairoSharedPtr   pAlphaCairo = pAlphaSurface->getCairo();
193cdf0e10cSrcweir                 X11Surface* pXlibSurface=dynamic_cast<X11Surface*>(pAlphaSurface.get());
194cdf0e10cSrcweir                 OSL_ASSERT(pXlibSurface);
195cdf0e10cSrcweir 
196cdf0e10cSrcweir                 // create RGB image (levels of gray) of alpha channel of original picture
197cdf0e10cSrcweir                 cairo_set_source_rgba( pAlphaCairo.get(), 1, 1, 1, 1 );
198cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_SOURCE );
199cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
200cdf0e10cSrcweir                 cairo_set_source_surface( pAlphaCairo.get(), mpBufferSurface->getCairoSurface().get(), 0, 0 );
201cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_XOR );
202cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
203cdf0e10cSrcweir                 pAlphaCairo.reset();
204cdf0e10cSrcweir 
205cdf0e10cSrcweir                 X11PixmapSharedPtr pPixmap = pXlibSurface->getPixmap();
206cdf0e10cSrcweir                 args[0] = uno::Any( true );
207cdf0e10cSrcweir                 args[1] = ::com::sun::star::uno::Any( pPixmap->mhDrawable );
208cdf0e10cSrcweir                 args[2] = ::com::sun::star::uno::Any( sal_Int32( pXlibSurface->getDepth () ) );
209cdf0e10cSrcweir                 pPixmap->clear(); // caller takes ownership of pixmap
210cdf0e10cSrcweir 
211cdf0e10cSrcweir                 // return pixmap and alphachannel pixmap - it will be used in BitmapEx
212cdf0e10cSrcweir                 aRV = uno::Any( args );
213cdf0e10cSrcweir #elif defined CAIRO_HAS_QUARTZ_SURFACE
214cdf0e10cSrcweir                 SurfaceSharedPtr pAlphaSurface = mpSurfaceProvider->createSurface( maSize, CAIRO_CONTENT_COLOR );
215cdf0e10cSrcweir                 CairoSharedPtr   pAlphaCairo = pAlphaSurface->getCairo();
216cdf0e10cSrcweir                 QuartzSurface* pQuartzSurface=dynamic_cast<QuartzSurface*>(pAlphaSurface.get());
217cdf0e10cSrcweir                 OSL_ASSERT(pQuartzSurface);
218cdf0e10cSrcweir 
219cdf0e10cSrcweir                 // create RGB image (levels of gray) of alpha channel of original picture
220cdf0e10cSrcweir                 cairo_set_source_rgba( pAlphaCairo.get(), 1, 1, 1, 1 );
221cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_SOURCE );
222cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
223cdf0e10cSrcweir                 cairo_set_source_surface( pAlphaCairo.get(), mpBufferSurface->getCairoSurface().get(), 0, 0 );
224cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_XOR );
225cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
226cdf0e10cSrcweir                 pAlphaCairo.reset();
227cdf0e10cSrcweir 
228cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 1 );
229cdf0e10cSrcweir                 args[0] = uno::Any( sal_IntPtr (pQuartzSurface->getCGContext()) );
230cdf0e10cSrcweir                 // return ??? and alphachannel ??? - it will be used in BitmapEx
231cdf0e10cSrcweir                 aRV = uno::Any( args );
232cdf0e10cSrcweir #elif defined CAIRO_HAS_WIN32_SURFACE
233cdf0e10cSrcweir                 SurfaceSharedPtr pAlphaSurface = mpSurfaceProvider->createSurface( maSize, CAIRO_CONTENT_COLOR );
234cdf0e10cSrcweir                 CairoSharedPtr   pAlphaCairo = pAlphaSurface->getCairo();
235cdf0e10cSrcweir 
236cdf0e10cSrcweir                 // create RGB image (levels of gray) of alpha channel of original picture
237cdf0e10cSrcweir                 cairo_set_source_rgba( pAlphaCairo.get(), 1, 1, 1, 1 );
238cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_SOURCE );
239cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
240cdf0e10cSrcweir                 cairo_set_source_surface( pAlphaCairo.get(), mpBufferSurface->getCairoSurface().get(), 0, 0 );
241cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_XOR );
242cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
243cdf0e10cSrcweir                 pAlphaCairo.reset();
244cdf0e10cSrcweir 
245cdf0e10cSrcweir                 // cant seem to retrieve HBITMAP from cairo. copy content then
246cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 1 );
247cdf0e10cSrcweir                 args[1] = uno::Any( sal_Int64(surface2HBitmap(pAlphaSurface,maSize)) );
248cdf0e10cSrcweir 
249cdf0e10cSrcweir                 aRV = uno::Any( args );
250cdf0e10cSrcweir                 // caller frees the bitmap
251cdf0e10cSrcweir #else
252cdf0e10cSrcweir # error Please define fast prop retrieval for your platform!
253cdf0e10cSrcweir #endif
254cdf0e10cSrcweir                 break;
255cdf0e10cSrcweir             }
256cdf0e10cSrcweir         }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir         return aRV;
259cdf0e10cSrcweir     }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir #define IMPLEMENTATION_NAME "CairoCanvas.CanvasBitmap"
262cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.rendering.CanvasBitmap"
263cdf0e10cSrcweir 
getImplementationName()264cdf0e10cSrcweir     ::rtl::OUString SAL_CALL CanvasBitmap::getImplementationName(  ) throw (uno::RuntimeException)
265cdf0e10cSrcweir     {
266cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
267cdf0e10cSrcweir     }
268cdf0e10cSrcweir 
supportsService(const::rtl::OUString & ServiceName)269cdf0e10cSrcweir     sal_Bool SAL_CALL CanvasBitmap::supportsService( const ::rtl::OUString& ServiceName ) throw (uno::RuntimeException)
270cdf0e10cSrcweir     {
271cdf0e10cSrcweir         return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
272cdf0e10cSrcweir     }
273cdf0e10cSrcweir 
getSupportedServiceNames()274cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > SAL_CALL CanvasBitmap::getSupportedServiceNames(  ) throw (uno::RuntimeException)
275cdf0e10cSrcweir     {
276cdf0e10cSrcweir         uno::Sequence< ::rtl::OUString > aRet(1);
277cdf0e10cSrcweir         aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
278cdf0e10cSrcweir 
279cdf0e10cSrcweir         return aRet;
280cdf0e10cSrcweir     }
281cdf0e10cSrcweir 
282cdf0e10cSrcweir }
283