1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_canvas.hxx"
30 
31 #include <ctype.h> // don't ask. msdev breaks otherwise...
32 #include <canvas/debug.hxx>
33 #include <canvas/verbosetrace.hxx>
34 #include <tools/diagnose_ex.h>
35 
36 #include <canvas/canvastools.hxx>
37 
38 #include <osl/mutex.hxx>
39 
40 #include <com/sun/star/registry/XRegistryKey.hpp>
41 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
42 #include <com/sun/star/uno/XComponentContext.hpp>
43 #include <com/sun/star/lang/NoSupportException.hpp>
44 
45 #include <toolkit/helper/vclunohelper.hxx>
46 #include <cppuhelper/factory.hxx>
47 #include <cppuhelper/implementationentry.hxx>
48 #include <comphelper/servicedecl.hxx>
49 
50 #include <basegfx/matrix/b2dhommatrix.hxx>
51 #include <basegfx/point/b2dpoint.hxx>
52 #include <basegfx/tools/canvastools.hxx>
53 #include <basegfx/numeric/ftools.hxx>
54 
55 #include "dx_winstuff.hxx"
56 #include "dx_spritecanvas.hxx"
57 
58 #if DIRECTX_VERSION < 0x0900
59 # define CANVAS_TECH "DX5"
60 #else
61 # define CANVAS_TECH "DX9"
62 #endif
63 
64 #define SPRITECANVAS_SERVICE_NAME        "com.sun.star.rendering.SpriteCanvas."      CANVAS_TECH
65 #define SPRITECANVAS_IMPLEMENTATION_NAME "com.sun.star.comp.rendering.SpriteCanvas." CANVAS_TECH
66 
67 
68 using namespace ::com::sun::star;
69 
70 namespace dxcanvas
71 {
72     SpriteCanvas::SpriteCanvas( const uno::Sequence< uno::Any >&                aArguments,
73                                 const uno::Reference< uno::XComponentContext >& rxContext ) :
74         maArguments(aArguments),
75         mxComponentContext( rxContext )
76     {
77     }
78 
79     void SpriteCanvas::initialize()
80     {
81         // #i64742# Only call initialize when not in probe mode
82         if( maArguments.getLength() == 0 )
83             return;
84 
85         VERBOSE_TRACE( "SpriteCanvas::initialize called" );
86 
87         /* aArguments:
88            0: ptr to creating instance (Window or VirtualDevice)
89            1: SystemEnvData as a streamed Any (or empty for VirtualDevice)
90            2: current bounds of creating instance
91            3: bool, denoting always on top state for Window (always false for VirtualDevice)
92            4: XWindow for creating Window (or empty for VirtualDevice)
93            5: SystemGraphicsData as a streamed Any
94          */
95         ENSURE_ARG_OR_THROW( maArguments.getLength() >= 5 &&
96                              maArguments[4].getValueTypeClass() == uno::TypeClass_INTERFACE,
97                              "VCLSpriteCanvas::initialize: wrong number of arguments, or wrong types" );
98 
99         uno::Reference< awt::XWindow > xParentWindow;
100         maArguments[4] >>= xParentWindow;
101         Window* pParentWindow = VCLUnoHelper::GetWindow(xParentWindow);
102         if( !pParentWindow )
103             throw lang::NoSupportException(
104                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
105                                      "Parent window not VCL window, or canvas out-of-process!")),
106                 NULL);
107 
108         awt::Rectangle aRect;
109         maArguments[2] >>= aRect;
110 
111         sal_Bool bIsFullscreen( sal_False );
112         maArguments[3] >>= bIsFullscreen;
113 
114         // setup helper
115         maDeviceHelper.init( *pParentWindow,
116                              *this,
117                              aRect,
118                              bIsFullscreen );
119         maCanvasHelper.init( *this,
120                              maRedrawManager,
121                              maDeviceHelper.getRenderModule(),
122 							 maDeviceHelper.getSurfaceProxy(),
123                              maDeviceHelper.getBackBuffer(),
124                              ::basegfx::B2ISize() );
125         maArguments.realloc(0);
126     }
127 
128     void SAL_CALL SpriteCanvas::disposing()
129     {
130         ::osl::MutexGuard aGuard( m_aMutex );
131 
132         mxComponentContext.clear();
133 
134         // forward to parent
135         SpriteCanvasBaseT::disposing();
136     }
137 
138     ::sal_Bool SAL_CALL SpriteCanvas::showBuffer( ::sal_Bool bUpdateAll ) throw (uno::RuntimeException)
139     {
140         ::osl::MutexGuard aGuard( m_aMutex );
141 
142         // avoid repaints on hidden window (hidden: not mapped to
143         // screen). Return failure, since the screen really has _not_
144         // been updated (caller should try again later)
145         return !mbIsVisible ? false : SpriteCanvasBaseT::showBuffer( bUpdateAll );
146     }
147 
148     ::sal_Bool SAL_CALL SpriteCanvas::switchBuffer( ::sal_Bool bUpdateAll ) throw (uno::RuntimeException)
149     {
150         ::osl::MutexGuard aGuard( m_aMutex );
151 
152         // avoid repaints on hidden window (hidden: not mapped to
153         // screen). Return failure, since the screen really has _not_
154         // been updated (caller should try again later)
155         return !mbIsVisible ? false : SpriteCanvasBaseT::switchBuffer( bUpdateAll );
156     }
157 
158     sal_Bool SAL_CALL SpriteCanvas::updateScreen( sal_Bool bUpdateAll ) throw (uno::RuntimeException)
159     {
160         ::osl::MutexGuard aGuard( m_aMutex );
161 
162         // avoid repaints on hidden window (hidden: not mapped to
163         // screen). Return failure, since the screen really has _not_
164         // been updated (caller should try again later)
165         return !mbIsVisible ? false : maCanvasHelper.updateScreen(
166             ::basegfx::unotools::b2IRectangleFromAwtRectangle(maBounds),
167             bUpdateAll,
168             mbSurfaceDirty );
169     }
170 
171     ::rtl::OUString SAL_CALL SpriteCanvas::getServiceName(  ) throw (uno::RuntimeException)
172     {
173         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SPRITECANVAS_SERVICE_NAME ) );
174     }
175 
176     const IDXRenderModuleSharedPtr& SpriteCanvas::getRenderModule() const
177     {
178         ::osl::MutexGuard aGuard( m_aMutex );
179 
180         return maDeviceHelper.getRenderModule();
181     }
182 
183     const DXSurfaceBitmapSharedPtr& SpriteCanvas::getBackBuffer() const
184     {
185         ::osl::MutexGuard aGuard( m_aMutex );
186 
187         return maDeviceHelper.getBackBuffer();
188     }
189 
190     IBitmapSharedPtr SpriteCanvas::getBitmap() const
191     {
192         return maDeviceHelper.getBackBuffer();
193     }
194 
195     static uno::Reference<uno::XInterface> initCanvas( SpriteCanvas* pCanvas )
196     {
197         uno::Reference<uno::XInterface> xRet(static_cast<cppu::OWeakObject*>(pCanvas));
198         pCanvas->initialize();
199         return xRet;
200     }
201 
202     namespace sdecl = comphelper::service_decl;
203     sdecl::class_<SpriteCanvas, sdecl::with_args<true> > serviceImpl(&initCanvas);
204     const sdecl::ServiceDecl dxSpriteCanvasDecl(
205         serviceImpl,
206         SPRITECANVAS_IMPLEMENTATION_NAME,
207         SPRITECANVAS_SERVICE_NAME );
208 }
209 
210 // The C shared lib entry points
211 COMPHELPER_SERVICEDECL_EXPORTS1(dxcanvas::dxSpriteCanvasDecl);
212