Define for Transformations on Path only, also fixed segfault due to GraphicsBuffer
[ipdf/code.git] / src / view.h
1 #ifndef _VIEW_H
2 #define _VIEW_H
3
4 #include "ipdf.h"
5 #include "document.h"
6 #include "framebuffer.h"
7 #include "objectrenderer.h"
8
9 #define USE_GPU_TRANSFORM true 
10 #define USE_GPU_RENDERING true
11 #define USE_SHADING !(USE_GPU_RENDERING) && true
12
13 #ifdef QUADTREE_DISABLED
14
15 //#define TRANSFORM_OBJECTS_NOT_VIEW
16
17 #endif
18
19 namespace IPDF
20 {
21         class Screen;
22         /**
23          * The View class manages a rectangular view into the document.
24          * It is responsible for coordinate transforms and rendering the document.
25          * ObjectRenderer's for each type of Object should be created in the constructor.
26          */
27         class View
28         {
29                 public:
30                         View(Document & document, Screen & screen, const Rect & bounds = Rect(0,0,1,1), const Colour & colour = Colour(0.f,0.f,0.f,1.f));
31                         virtual ~View();
32
33                         void Render(int width = 0, int height = 0);
34                         
35                         void Translate(Real x, Real y);
36                         void ScaleAroundPoint(Real x, Real y, Real scale_amount);
37                         void SetBounds(const Rect & new_bounds);
38                         
39                         Rect TransformToViewCoords(const Rect& inp) const;
40                         
41                         const Rect& GetBounds() const { return m_bounds; }
42                         
43                         
44                         const bool UsingGPUTransform() const { return m_use_gpu_transform; } // whether view transform calculated on CPU or GPU
45                         const bool UsingGPURendering() const { return m_use_gpu_rendering; } // whether GPU shaders are used or CPU rendering
46                         void ToggleGPUTransform() { m_use_gpu_transform = (!m_use_gpu_transform); m_bounds_dirty = true; m_buffer_dirty = true; }
47                         void ToggleGPURendering() { m_use_gpu_rendering = (!m_use_gpu_rendering); m_bounds_dirty = true; m_buffer_dirty = true; }
48                         void SetGPUTransform(bool state) {m_use_gpu_transform = state; m_bounds_dirty = true; m_buffer_dirty = true;}
49                         
50                         void SetGPURendering(bool state) {m_use_gpu_rendering = state; m_bounds_dirty = true; m_buffer_dirty = true;}
51
52                         bool ShowingBezierBounds() const {return m_show_bezier_bounds;} // render bounds rectangles
53                         void ShowBezierBounds(bool state) {m_show_bezier_bounds = state; m_bounds_dirty = true; m_buffer_dirty = true;}
54                         bool ShowingBezierType() const {return m_show_bezier_type;}
55                         void ShowBezierType(bool state) {m_show_bezier_type = state; m_bounds_dirty = true; m_buffer_dirty = true;}
56                         bool ShowingFillPoints() const {return m_show_fill_points;}
57                         void ShowFillPoints(bool state) {m_show_fill_points = state; m_bounds_dirty = true; m_buffer_dirty = true;}
58                         bool ShowingFillBounds() const {return m_show_fill_bounds;}
59                         void ShowFillBounds(bool state) {m_show_fill_bounds = true;}
60                         
61                         bool PerformingShading() const {return m_perform_shading;}
62                         void PerformShading(bool state) {m_perform_shading = state; m_bounds_dirty = true; m_buffer_dirty = true;}
63
64                         void ForceBoundsDirty() {m_bounds_dirty = true;}                
65                         void ForceBufferDirty() {m_buffer_dirty = true;}                
66                         void ForceRenderDirty() {m_render_dirty = true;}
67                         
68                         void SetLazyRendering(bool state = true) {m_lazy_rendering = state;}
69                         bool UsingLazyRendering() const {return m_lazy_rendering;}
70                         
71                         void SaveBMP(const char * filename) {if (UsingGPURendering()) SaveGPUBMP(filename); else SaveCPUBMP(filename);}
72                         
73                         void SaveCPUBMP(const char * filename);
74                         void SaveGPUBMP(const char * filename);
75
76                         Document & Doc() {return m_document;}
77
78                 private:
79                         struct GPUObjBounds
80                         {
81                                 float x0, y0;
82                                 float x1, y1;
83                         } __attribute__((packed));
84
85                         void PrepareRender(); // call when m_render_dirty is true
86                         void UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj); // call when m_buffer_dirty is true
87
88                         void RenderRange(int width, int height, unsigned first_obj, unsigned last_obj);
89
90                         bool m_use_gpu_transform;
91                         bool m_use_gpu_rendering;
92                         bool m_bounds_dirty; // the view bounds has changed (occurs when changing view)
93                         bool m_buffer_dirty; // the object bounds have changed (also occurs when changing view, but only when not using GPU transforms)
94                         bool m_render_dirty; // the document has changed (occurs when document first loaded)
95                         Document & m_document;
96                         Screen & m_screen;
97                         FrameBuffer m_cached_display;
98                         Rect m_bounds;
99                         Colour m_colour;
100
101                         // Stores the view bounds.
102                         GraphicsBuffer m_bounds_ubo; //bounds_dirty means this one has changed
103                         // Stores the bounds for _all_ objects.
104                         GraphicsBuffer m_objbounds_vbo; //buffer_dirty means this one has changed
105
106                         // ObjectRenderers to be initialised in constructor
107                         // Trust me it will be easier to generalise things this way. Even though there are pointers.
108                         std::vector<ObjectRenderer*> m_object_renderers; 
109                         uint8_t * m_cpu_rendering_pixels; // pixels to be used for CPU rendering
110
111                         
112                         // shading
113                         bool m_perform_shading;
114                         
115                         // Debug rendering
116                         bool m_show_bezier_bounds;
117                         bool m_show_bezier_type;
118                         bool m_show_fill_points;
119                         bool m_show_fill_bounds;
120                         
121                         bool m_lazy_rendering;// don't redraw frames unless we need to
122
123
124 #ifndef QUADTREE_DISABLED
125                         QuadTreeIndex m_current_quadtree_node;  // The highest node we will traverse.
126                         int m_quadtree_max_depth;               // The maximum quadtree depth.
127                         void RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth);
128
129 #endif
130         };
131 }
132
133 #endif //_VIEW_H

UCC git Repository :: git.ucc.asn.au