xref: /aoo41x/main/canvas/source/tools/page.hxx (revision 91c99ff4)
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 #ifndef INCLUDED_CANVAS_PAGE_HXX
25 #define INCLUDED_CANVAS_PAGE_HXX
26 
27 #include <basegfx/vector/b2isize.hxx>
28 #include <basegfx/range/b2irectangle.hxx>
29 #include <canvas/rendering/icolorbuffer.hxx>
30 #include <canvas/rendering/irendermodule.hxx>
31 #include <canvas/rendering/isurface.hxx>
32 
33 #include <list>
34 #include <vector>
35 #include <boost/shared_ptr.hpp>
36 #include "surfacerect.hxx"
37 
38 namespace canvas
39 {
40 	class PageFragment;
41 
42 	typedef ::boost::shared_ptr< PageFragment > FragmentSharedPtr;
43 
44     /** One page of IRenderModule-provided texture space
45      */
46 	class Page
47 	{
48     public:
49         Page( const IRenderModuleSharedPtr& rRenderModule );
50 
51         FragmentSharedPtr        allocateSpace( const ::basegfx::B2ISize& rSize );
52         bool                     nakedFragment( const FragmentSharedPtr& pFragment );
53         void                     free( const FragmentSharedPtr& pFragment );
getSurface() const54         const ISurfaceSharedPtr& getSurface() const { return mpSurface; }
55         bool                     isValid() const;
56         void                     validate();
57 
58     private:
59         typedef std::list<FragmentSharedPtr> FragmentContainer_t;
60 
61         IRenderModuleSharedPtr  mpRenderModule;
62         ISurfaceSharedPtr       mpSurface;
63         FragmentContainer_t     mpFragments;
64 
65         bool insert( SurfaceRect& r );
66         bool isValidLocation( const SurfaceRect& r ) const;
67 	};
68 
69 	typedef ::boost::shared_ptr< Page > PageSharedPtr;
70 
71 
72     /** A part of a page, which gets allocated to a surface
73      */
74 	class PageFragment
75 	{
76     public:
PageFragment(const SurfaceRect & r,Page * pPage)77         PageFragment( const SurfaceRect& r,
78                       Page*              pPage ) :
79             mpPage(pPage),
80             maRect(r),
81             mpBuffer(),
82             maSourceOffset()
83         {
84         }
85 
86         /// Creates a 'naked' fragment.
PageFragment(const::basegfx::B2ISize & rSize)87         PageFragment( const ::basegfx::B2ISize& rSize ) :
88             mpPage(NULL),
89             maRect(rSize),
90             mpBuffer(),
91             maSourceOffset()
92         {
93         }
94 
isNaked() const95         bool                        isNaked() const { return (mpPage == NULL); }
getRect() const96         const SurfaceRect&          getRect() const { return maRect; }
getPos() const97         const ::basegfx::B2IPoint&  getPos() const { return maRect.maPos; }
getSize() const98         const ::basegfx::B2ISize&   getSize() const { return maRect.maSize; }
setColorBuffer(const IColorBufferSharedPtr & pColorBuffer)99         void                        setColorBuffer( const IColorBufferSharedPtr& pColorBuffer ) { mpBuffer=pColorBuffer; }
setSourceOffset(const::basegfx::B2IPoint & rOffset)100         void                        setSourceOffset( const ::basegfx::B2IPoint& rOffset ) { maSourceOffset=rOffset; }
setPage(Page * pPage)101         void                        setPage( Page* pPage ) { mpPage=pPage; }
102 
free(const FragmentSharedPtr & pFragment)103         void free( const FragmentSharedPtr& pFragment )
104         {
105             if(mpPage)
106                 mpPage->free(pFragment);
107 
108             mpPage=NULL;
109         }
110 
select(bool bRefresh)111         bool select( bool bRefresh )
112         {
113             // request was made to select this fragment,
114             // but this fragment has not been located on any
115             // of the available pages, we need to hurry now.
116             if(!(mpPage))
117                 return false;
118 
119             ISurfaceSharedPtr pSurface(mpPage->getSurface());
120 
121             // select this surface before wiping the contents
122             // since a specific implementation could trigger
123             // a rendering operation here...
124             if(!(pSurface->selectTexture()))
125                 return false;
126 
127             // call refresh() if requested, otherwise we're up to date...
128             return bRefresh ? refresh() : true;
129         }
130 
refresh()131         bool refresh()
132         {
133             if(!(mpPage))
134                 return false;
135 
136             ISurfaceSharedPtr pSurface(mpPage->getSurface());
137 
138             return pSurface->update( maRect.maPos,
139                                      ::basegfx::B2IRectangle(
140                                          maSourceOffset,
141                                          maSourceOffset + maRect.maSize ),
142                                      *mpBuffer );
143         }
144 
145     private:
146         Page*                 mpPage;
147         SurfaceRect           maRect;
148         IColorBufferSharedPtr mpBuffer;
149         ::basegfx::B2IPoint   maSourceOffset;
150 	};
151 }
152 
153 #endif
154