xref: /trunk/main/canvas/source/cairo/cairo_canvasbitmap.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
125ea7f45SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
325ea7f45SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
425ea7f45SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
525ea7f45SAndrew Rist  * distributed with this work for additional information
625ea7f45SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
725ea7f45SAndrew Rist  * to you under the Apache License, Version 2.0 (the
825ea7f45SAndrew Rist  * "License"); you may not use this file except in compliance
925ea7f45SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1125ea7f45SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1325ea7f45SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1425ea7f45SAndrew Rist  * software distributed under the License is distributed on an
1525ea7f45SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1625ea7f45SAndrew Rist  * KIND, either express or implied.  See the License for the
1725ea7f45SAndrew Rist  * specific language governing permissions and limitations
1825ea7f45SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2025ea7f45SAndrew Rist  *************************************************************/
2125ea7f45SAndrew Rist 
2225ea7f45SAndrew 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>
40*906a4e93SYuri Dario #elif defined OS2
41cdf0e10cSrcweir #else
42cdf0e10cSrcweir # error Native API needed.
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using namespace ::cairo;
46cdf0e10cSrcweir using namespace ::com::sun::star;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #ifdef CAIRO_HAS_WIN32_SURFACE
49cdf0e10cSrcweir namespace
50cdf0e10cSrcweir {
surface2HBitmap(const SurfaceSharedPtr & rSurface,const basegfx::B2ISize & rSize)51cdf0e10cSrcweir     HBITMAP surface2HBitmap( const SurfaceSharedPtr& rSurface, const basegfx::B2ISize& rSize )
52cdf0e10cSrcweir     {
5307a3d7f1SPedro Giffuni         // can't seem to retrieve HBITMAP from cairo. copy content then
54cdf0e10cSrcweir         HDC hScreenDC=GetDC(NULL);
55cdf0e10cSrcweir         HBITMAP hBmpBitmap = CreateCompatibleBitmap( hScreenDC,
56cdf0e10cSrcweir                                                      rSize.getX(),
57cdf0e10cSrcweir                                                      rSize.getY() );
58cdf0e10cSrcweir 
59cdf0e10cSrcweir         HDC     hBmpDC = CreateCompatibleDC( 0 );
60cdf0e10cSrcweir         HBITMAP hBmpOld = (HBITMAP) SelectObject( hBmpDC, hBmpBitmap );
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         BitBlt( hBmpDC, 0, 0, rSize.getX(), rSize.getX(),
63cdf0e10cSrcweir                 cairo_win32_surface_get_dc(rSurface->getCairoSurface().get()),
64cdf0e10cSrcweir                 0, 0, SRCCOPY );
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         SelectObject( hBmpDC, hBmpOld );
67cdf0e10cSrcweir         DeleteDC( hBmpDC );
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         return hBmpBitmap;
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir }
72cdf0e10cSrcweir #endif
73cdf0e10cSrcweir 
74cdf0e10cSrcweir namespace cairocanvas
75cdf0e10cSrcweir {
CanvasBitmap(const::basegfx::B2ISize & rSize,const SurfaceProviderRef & rSurfaceProvider,rendering::XGraphicDevice * pDevice,bool bHasAlpha)76cdf0e10cSrcweir     CanvasBitmap::CanvasBitmap( const ::basegfx::B2ISize&  rSize,
77cdf0e10cSrcweir                                 const SurfaceProviderRef&  rSurfaceProvider,
78cdf0e10cSrcweir                                 rendering::XGraphicDevice* pDevice,
79cdf0e10cSrcweir                                 bool                       bHasAlpha ) :
80cdf0e10cSrcweir         mpSurfaceProvider( rSurfaceProvider ),
81cdf0e10cSrcweir         mpBufferSurface(),
82cdf0e10cSrcweir         mpBufferCairo(),
83cdf0e10cSrcweir         maSize(rSize),
84cdf0e10cSrcweir         mbHasAlpha(bHasAlpha)
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         ENSURE_OR_THROW( mpSurfaceProvider.is(),
87cdf0e10cSrcweir                           "CanvasBitmap::CanvasBitmap(): Invalid surface or device" );
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         OSL_TRACE( "bitmap size: %dx%d", rSize.getX(), rSize.getY() );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         mpBufferSurface = mpSurfaceProvider->createSurface( rSize, bHasAlpha ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR );
92cdf0e10cSrcweir         mpBufferCairo = mpBufferSurface->getCairo();
93cdf0e10cSrcweir 
94cdf0e10cSrcweir         maCanvasHelper.init( rSize, *mpSurfaceProvider, pDevice );
95cdf0e10cSrcweir         maCanvasHelper.setSurface( mpBufferSurface, bHasAlpha );
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         // clear bitmap to 100% transparent
98cdf0e10cSrcweir         maCanvasHelper.clear();
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
disposing()101cdf0e10cSrcweir     void SAL_CALL CanvasBitmap::disposing()
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         mpSurfaceProvider.clear();
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         mpBufferCairo.reset();
106cdf0e10cSrcweir         mpBufferSurface.reset();
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         // forward to parent
109cdf0e10cSrcweir         CanvasBitmap_Base::disposing();
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
getSurface()112cdf0e10cSrcweir     SurfaceSharedPtr CanvasBitmap::getSurface()
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         return mpBufferSurface;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
createSurface(const::basegfx::B2ISize & rSize,Content aContent)117cdf0e10cSrcweir     SurfaceSharedPtr CanvasBitmap::createSurface( const ::basegfx::B2ISize& rSize, Content aContent )
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         return mpSurfaceProvider->createSurface(rSize,aContent);
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
createSurface(::Bitmap & rBitmap)122cdf0e10cSrcweir     SurfaceSharedPtr CanvasBitmap::createSurface( ::Bitmap& rBitmap )
123cdf0e10cSrcweir     {
124cdf0e10cSrcweir         return mpSurfaceProvider->createSurface(rBitmap);
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
changeSurface(bool,bool)127cdf0e10cSrcweir     SurfaceSharedPtr CanvasBitmap::changeSurface( bool, bool )
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         // non-modifiable surface here
130cdf0e10cSrcweir         return SurfaceSharedPtr();
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
getOutputDevice()133cdf0e10cSrcweir     OutputDevice* CanvasBitmap::getOutputDevice()
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         return mpSurfaceProvider->getOutputDevice();
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
repaint(const SurfaceSharedPtr & pSurface,const rendering::ViewState & viewState,const rendering::RenderState & renderState)138cdf0e10cSrcweir     bool CanvasBitmap::repaint( const SurfaceSharedPtr&       pSurface,
139cdf0e10cSrcweir                                 const rendering::ViewState&   viewState,
140cdf0e10cSrcweir                                 const rendering::RenderState& renderState )
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         return maCanvasHelper.repaint( pSurface, viewState, renderState );
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
getFastPropertyValue(sal_Int32 nHandle)145cdf0e10cSrcweir     uno::Any SAL_CALL CanvasBitmap::getFastPropertyValue( sal_Int32 nHandle )  throw (uno::RuntimeException)
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         uno::Any aRV( sal_Int32(0) );
148cdf0e10cSrcweir         // 0 ... get BitmapEx
149cdf0e10cSrcweir         // 1 ... get Pixbuf with bitmap RGB content
150cdf0e10cSrcweir         // 2 ... get Pixbuf with bitmap alpha mask
151cdf0e10cSrcweir         switch( nHandle )
152cdf0e10cSrcweir         {
153cdf0e10cSrcweir             case 0:
154cdf0e10cSrcweir             {
155cdf0e10cSrcweir                 aRV = uno::Any( reinterpret_cast<sal_Int64>( (BitmapEx*) NULL ) );
156cdf0e10cSrcweir                 break;
157cdf0e10cSrcweir             }
158cdf0e10cSrcweir             case 1:
159cdf0e10cSrcweir             {
160cdf0e10cSrcweir #ifdef CAIRO_HAS_XLIB_SURFACE
161cdf0e10cSrcweir                 X11Surface* pXlibSurface=dynamic_cast<X11Surface*>(mpBufferSurface.get());
162cdf0e10cSrcweir                 OSL_ASSERT(pXlibSurface);
163cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 3 );
164cdf0e10cSrcweir                 args[0] = uno::Any( false );  // do not call XFreePixmap on it
165cdf0e10cSrcweir                 args[1] = uno::Any( pXlibSurface->getPixmap()->mhDrawable );
166cdf0e10cSrcweir                 args[2] = uno::Any( sal_Int32( pXlibSurface->getDepth() ) );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir                 aRV = uno::Any( args );
169cdf0e10cSrcweir #elif defined CAIRO_HAS_QUARTZ_SURFACE
170cdf0e10cSrcweir                 QuartzSurface* pQuartzSurface = dynamic_cast<QuartzSurface*>(mpBufferSurface.get());
171cdf0e10cSrcweir                 OSL_ASSERT(pQuartzSurface);
172cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 1 );
173cdf0e10cSrcweir                 args[0] = uno::Any( sal_IntPtr (pQuartzSurface->getCGContext()) );
174cdf0e10cSrcweir                 aRV = uno::Any( args );
175cdf0e10cSrcweir #elif defined CAIRO_HAS_WIN32_SURFACE
176cdf0e10cSrcweir                 // TODO(F2): check whether under all circumstances,
177cdf0e10cSrcweir                 // the alpha channel is ignored here.
178cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 1 );
179cdf0e10cSrcweir                 args[1] = uno::Any( sal_Int64(surface2HBitmap(mpBufferSurface,maSize)) );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir                 aRV = uno::Any( args );
182cdf0e10cSrcweir                 // caller frees the bitmap
183*906a4e93SYuri Dario #elif defined CAIRO_HAS_OS2_SURFACE
184*906a4e93SYuri Dario                 OSL_TRACE( "CanvasBitmap::getFastPropertyValue case 1");
185cdf0e10cSrcweir #else
186cdf0e10cSrcweir # error Please define fast prop retrieval for your platform!
187cdf0e10cSrcweir #endif
188cdf0e10cSrcweir                 break;
189cdf0e10cSrcweir             }
190cdf0e10cSrcweir             case 2:
191cdf0e10cSrcweir             {
192cdf0e10cSrcweir #ifdef CAIRO_HAS_XLIB_SURFACE
193cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 3 );
194cdf0e10cSrcweir                 SurfaceSharedPtr pAlphaSurface = mpSurfaceProvider->createSurface( maSize, CAIRO_CONTENT_COLOR );
195cdf0e10cSrcweir                 CairoSharedPtr   pAlphaCairo = pAlphaSurface->getCairo();
196cdf0e10cSrcweir                 X11Surface* pXlibSurface=dynamic_cast<X11Surface*>(pAlphaSurface.get());
197cdf0e10cSrcweir                 OSL_ASSERT(pXlibSurface);
198cdf0e10cSrcweir 
199cdf0e10cSrcweir                 // create RGB image (levels of gray) of alpha channel of original picture
200cdf0e10cSrcweir                 cairo_set_source_rgba( pAlphaCairo.get(), 1, 1, 1, 1 );
201cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_SOURCE );
202cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
203cdf0e10cSrcweir                 cairo_set_source_surface( pAlphaCairo.get(), mpBufferSurface->getCairoSurface().get(), 0, 0 );
204cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_XOR );
205cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
206cdf0e10cSrcweir                 pAlphaCairo.reset();
207cdf0e10cSrcweir 
208cdf0e10cSrcweir                 X11PixmapSharedPtr pPixmap = pXlibSurface->getPixmap();
209cdf0e10cSrcweir                 args[0] = uno::Any( true );
210cdf0e10cSrcweir                 args[1] = ::com::sun::star::uno::Any( pPixmap->mhDrawable );
211cdf0e10cSrcweir                 args[2] = ::com::sun::star::uno::Any( sal_Int32( pXlibSurface->getDepth () ) );
212cdf0e10cSrcweir                 pPixmap->clear(); // caller takes ownership of pixmap
213cdf0e10cSrcweir 
214cdf0e10cSrcweir                 // return pixmap and alphachannel pixmap - it will be used in BitmapEx
215cdf0e10cSrcweir                 aRV = uno::Any( args );
216cdf0e10cSrcweir #elif defined CAIRO_HAS_QUARTZ_SURFACE
217cdf0e10cSrcweir                 SurfaceSharedPtr pAlphaSurface = mpSurfaceProvider->createSurface( maSize, CAIRO_CONTENT_COLOR );
218cdf0e10cSrcweir                 CairoSharedPtr   pAlphaCairo = pAlphaSurface->getCairo();
219cdf0e10cSrcweir                 QuartzSurface* pQuartzSurface=dynamic_cast<QuartzSurface*>(pAlphaSurface.get());
220cdf0e10cSrcweir                 OSL_ASSERT(pQuartzSurface);
221cdf0e10cSrcweir 
222cdf0e10cSrcweir                 // create RGB image (levels of gray) of alpha channel of original picture
223cdf0e10cSrcweir                 cairo_set_source_rgba( pAlphaCairo.get(), 1, 1, 1, 1 );
224cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_SOURCE );
225cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
226cdf0e10cSrcweir                 cairo_set_source_surface( pAlphaCairo.get(), mpBufferSurface->getCairoSurface().get(), 0, 0 );
227cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_XOR );
228cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
229cdf0e10cSrcweir                 pAlphaCairo.reset();
230cdf0e10cSrcweir 
231cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 1 );
232cdf0e10cSrcweir                 args[0] = uno::Any( sal_IntPtr (pQuartzSurface->getCGContext()) );
233cdf0e10cSrcweir                 // return ??? and alphachannel ??? - it will be used in BitmapEx
234cdf0e10cSrcweir                 aRV = uno::Any( args );
235cdf0e10cSrcweir #elif defined CAIRO_HAS_WIN32_SURFACE
236cdf0e10cSrcweir                 SurfaceSharedPtr pAlphaSurface = mpSurfaceProvider->createSurface( maSize, CAIRO_CONTENT_COLOR );
237cdf0e10cSrcweir                 CairoSharedPtr   pAlphaCairo = pAlphaSurface->getCairo();
238cdf0e10cSrcweir 
239cdf0e10cSrcweir                 // create RGB image (levels of gray) of alpha channel of original picture
240cdf0e10cSrcweir                 cairo_set_source_rgba( pAlphaCairo.get(), 1, 1, 1, 1 );
241cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_SOURCE );
242cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
243cdf0e10cSrcweir                 cairo_set_source_surface( pAlphaCairo.get(), mpBufferSurface->getCairoSurface().get(), 0, 0 );
244cdf0e10cSrcweir                 cairo_set_operator( pAlphaCairo.get(), CAIRO_OPERATOR_XOR );
245cdf0e10cSrcweir                 cairo_paint( pAlphaCairo.get() );
246cdf0e10cSrcweir                 pAlphaCairo.reset();
247cdf0e10cSrcweir 
24807a3d7f1SPedro Giffuni                 // can't seem to retrieve HBITMAP from cairo. copy content then
249cdf0e10cSrcweir                 uno::Sequence< uno::Any > args( 1 );
250cdf0e10cSrcweir                 args[1] = uno::Any( sal_Int64(surface2HBitmap(pAlphaSurface,maSize)) );
251cdf0e10cSrcweir 
252cdf0e10cSrcweir                 aRV = uno::Any( args );
253cdf0e10cSrcweir                 // caller frees the bitmap
254*906a4e93SYuri Dario #elif defined CAIRO_HAS_OS2_SURFACE
255*906a4e93SYuri Dario                 OSL_TRACE( "CanvasBitmap::getFastPropertyValue case 2");
256cdf0e10cSrcweir #else
257cdf0e10cSrcweir # error Please define fast prop retrieval for your platform!
258cdf0e10cSrcweir #endif
259cdf0e10cSrcweir                 break;
260cdf0e10cSrcweir             }
261cdf0e10cSrcweir         }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir         return aRV;
264cdf0e10cSrcweir     }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir #define IMPLEMENTATION_NAME "CairoCanvas.CanvasBitmap"
267cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.rendering.CanvasBitmap"
268cdf0e10cSrcweir 
getImplementationName()269cdf0e10cSrcweir     ::rtl::OUString SAL_CALL CanvasBitmap::getImplementationName(  ) throw (uno::RuntimeException)
270cdf0e10cSrcweir     {
271cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
272cdf0e10cSrcweir     }
273cdf0e10cSrcweir 
supportsService(const::rtl::OUString & ServiceName)274cdf0e10cSrcweir     sal_Bool SAL_CALL CanvasBitmap::supportsService( const ::rtl::OUString& ServiceName ) throw (uno::RuntimeException)
275cdf0e10cSrcweir     {
276cdf0e10cSrcweir         return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
277cdf0e10cSrcweir     }
278cdf0e10cSrcweir 
getSupportedServiceNames()279cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > SAL_CALL CanvasBitmap::getSupportedServiceNames(  ) throw (uno::RuntimeException)
280cdf0e10cSrcweir     {
281cdf0e10cSrcweir         uno::Sequence< ::rtl::OUString > aRet(1);
282cdf0e10cSrcweir         aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
283cdf0e10cSrcweir 
284cdf0e10cSrcweir         return aRet;
285cdf0e10cSrcweir     }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir }
288