2 #include "bufferbuilder.h"
11 * Allocates memory for ObjectRenderers
12 * @param document - The document to associate the View with
13 * @param bounds - Initial bounds of the View
14 * @param colour - Colour to use for rendering this view. TODO: Make sure this actually works, or just remove it
16 View::View(Document & document, Screen & screen, const Rect & bounds, const Colour & colour)
17 : m_use_gpu_transform(USE_GPU_TRANSFORM), m_use_gpu_rendering(USE_GPU_RENDERING), m_bounds_dirty(true), m_buffer_dirty(true),
18 m_render_dirty(true), m_document(document), m_screen(screen), m_cached_display(), m_bounds(bounds), m_colour(colour), m_bounds_ubo(),
19 m_objbounds_vbo(), m_object_renderers(NUMBER_OF_OBJECT_TYPES), m_cpu_rendering_pixels(NULL)
21 Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str());
23 screen.SetView(this); // oh dear...
25 // Create ObjectRenderers - new's match delete's in View::~View
26 //TODO: Don't forget to put new renderers here or things will be segfaultastic
27 m_object_renderers[RECT_FILLED] = new RectFilledRenderer();
28 m_object_renderers[RECT_OUTLINE] = new RectOutlineRenderer();
29 m_object_renderers[CIRCLE_FILLED] = new CircleFilledRenderer();
30 m_object_renderers[BEZIER] = new BezierRenderer();
32 // To add rendering for a new type of object;
33 // 1. Add enum to ObjectType in ipdf.h
34 // 2. Implement class inheriting from ObjectRenderer using that type in objectrenderer.h and objectrenderer.cpp
39 #ifndef QUADTREE_DISABLED
40 m_quadtree_max_depth = 2;
41 m_current_quadtree_node = document.GetQuadTree().root_id;
47 * Frees memory used by ObjectRenderers
51 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
53 delete m_object_renderers[i]; // delete's match new's in constructor
55 m_object_renderers.clear();
56 delete [] m_cpu_rendering_pixels;
61 * @param x, y - Amount to translate
63 void View::Translate(Real x, Real y)
69 Debug("View Bounds => %s", m_bounds.Str().c_str());
70 if (!m_use_gpu_transform)
71 m_buffer_dirty = true;
72 m_bounds_dirty = true;
76 * Scale the View at a point
77 * @param x, y - Coordinates to scale at (eg: Mouse cursor position)
78 * @param scale_amount - Amount to scale by
80 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
82 // x and y are coordinates in the window
83 // Convert to local coords.
89 Real top = y - m_bounds.y;
90 Real left = x - m_bounds.x;
95 m_bounds.x = x - left;
97 m_bounds.w *= scale_amount;
98 m_bounds.h *= scale_amount;
99 //Debug("Scale at {%s, %s} by %s View Bounds => %s", x.Str().c_str(), y.Str().c_str(), scale_amount.Str().c_str(), m_bounds.Str().c_str());
100 if (!m_use_gpu_transform)
101 m_buffer_dirty = true;
102 m_bounds_dirty = true;
106 * Transform a point in the document to a point relative to the top left corner of the view
107 * This is the CPU coordinate transform code; used only if the CPU is doing coordinate transforms
108 * @param inp - Input Rect {x,y,w,h} in the document
109 * @returns output Rect {x,y,w,h} in the View
111 Rect View::TransformToViewCoords(const Rect& inp) const
114 out.x = (inp.x - m_bounds.x) / m_bounds.w;
115 out.y = (inp.y - m_bounds.y) / m_bounds.h;
116 out.w = inp.w / m_bounds.w;
117 out.h = inp.h / m_bounds.h;
123 * Updates FrameBuffer if the document, object bounds, or view bounds have changed, then Blits it
124 * Otherwise just Blits the cached FrameBuffer
125 * @param width - Width of View to render
126 * @param height - Height of View to render
128 void View::Render(int width, int height)
130 // View dimensions have changed (ie: Window was resized)
131 int prev_width = m_cached_display.GetWidth();
132 int prev_height = m_cached_display.GetHeight();
133 if (width != prev_width || height != prev_height)
135 m_cached_display.Create(width, height);
136 m_bounds_dirty = true;
139 // View bounds have not changed; blit the FrameBuffer as it is
142 m_cached_display.UnBind();
143 m_cached_display.Blit();
146 m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
147 m_cached_display.Clear();
150 if (!m_use_gpu_rendering)
152 // Dynamically resize CPU rendering target pixels if needed
153 if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
155 delete [] m_cpu_rendering_pixels;
156 m_cpu_rendering_pixels = new uint8_t[width*height*4];
157 if (m_cpu_rendering_pixels == NULL)
158 Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
160 // Clear CPU rendering pixels
161 for (int i = 0; i < width*height*4; ++i)
162 m_cpu_rendering_pixels[i] = 255;
164 #ifdef QUADTREE_DISABLED
165 RenderRange(width, height, 0, m_document.ObjectCount());
167 RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
169 if (!m_use_gpu_rendering)
171 m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
172 // Debug for great victory (do something similar for GPU and compare?)
173 ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
175 m_cached_display.UnBind(); // resets render target to the screen
176 m_cached_display.Blit(); // blit FrameBuffer to screen
177 m_buffer_dirty = false;
180 #ifndef QUADTREE_DISABLED
181 void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
183 Rect old_bounds = m_bounds;
184 if (node == QUADTREE_EMPTY) return;
185 if (!remaining_depth) return;
186 //Debug("Rendering QT node %d, (objs: %d -- %d)\n", node, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
187 RenderRange(width, height, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
189 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_LEFT);
190 m_bounds_dirty = true;
191 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_left, remaining_depth-1);
192 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_RIGHT);
193 m_bounds_dirty = true;
194 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_right, remaining_depth-1);
195 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_LEFT);
196 m_bounds_dirty = true;
197 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_left, remaining_depth-1);
198 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_RIGHT);
199 m_bounds_dirty = true;
200 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_right, remaining_depth-1);
201 m_bounds = old_bounds;
202 m_bounds_dirty = true;
206 void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
209 if (m_render_dirty) // document has changed
212 if (m_buffer_dirty) // object bounds have changed
213 UpdateObjBoundsVBO(first_obj, last_obj);
215 if (m_use_gpu_transform)
217 GLfloat glbounds[] = {static_cast<GLfloat>(Float(m_bounds.x)), static_cast<GLfloat>(Float(m_bounds.y)), static_cast<GLfloat>(Float(m_bounds.w)), static_cast<GLfloat>(Float(m_bounds.h)),
218 0.0, 0.0, static_cast<GLfloat>(width), static_cast<GLfloat>(height)};
219 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
223 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
224 0.0f, 0.0f, float(width), float(height)};
225 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
227 m_bounds_dirty = false;
231 if (m_use_gpu_rendering)
233 if (m_colour.a < 1.0f)
236 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
238 m_objbounds_vbo.Bind();
240 glEnableVertexAttribArray(0);
241 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
243 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
245 m_object_renderers[i]->RenderUsingGPU(first_obj, last_obj);
248 glDisableVertexAttribArray(0);
249 if (m_colour.a < 1.0f)
254 else // Rasterise on CPU then blit texture to GPU
257 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
259 m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height}, first_obj, last_obj);
264 void View::UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj)
266 //m_objbounds_vbo.Invalidate();
267 m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
268 if (m_use_gpu_transform)
270 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
274 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
276 m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
278 BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.MapRange(first_obj*sizeof(GPUObjBounds), (last_obj-first_obj)*sizeof(GPUObjBounds), false, true, true), m_objbounds_vbo.GetSize());
280 for (unsigned id = first_obj; id < last_obj; ++id)
283 if (m_use_gpu_transform)
285 obj_bounds = m_document.m_objects.bounds[id];
289 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
291 GPUObjBounds gpu_bounds = {
292 (float)Float(obj_bounds.x),
293 (float)Float(obj_bounds.y),
294 (float)Float(obj_bounds.x + obj_bounds.w),
295 (float)Float(obj_bounds.y + obj_bounds.h)
297 obj_bounds_builder.Add(gpu_bounds);
300 m_objbounds_vbo.UnMap();
303 * Prepare the document for rendering
304 * Will be called on View::Render if m_render_dirty is set
305 * (Called at least once, on the first Render)
307 void View::PrepareRender()
309 Debug("Recreate buffers with %u objects", m_document.ObjectCount());
310 // Prepare bounds vbo
311 m_bounds_ubo.Invalidate();
312 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
313 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
315 // Instead of having each ObjectRenderer go through the whole document
316 // we initialise them, go through the document once adding to the appropriate Renderers
317 // and then finalise them
318 // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
320 // Prepare the buffers
321 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
323 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
326 // Add objects from Document to buffers
327 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
329 ObjectType type = m_document.m_objects.types[id];
330 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
331 // (Also, Wow I just actually used std::vector::at())
332 // (Also, I just managed to make it throw an exception because I'm a moron)
333 Debug("Object of type %d", type);
336 // Finish the buffers
337 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
339 m_object_renderers[i]->FinaliseBuffers();
341 dynamic_cast<BezierRenderer*>(m_object_renderers[BEZIER])->PrepareBezierGPUBuffer(m_document.m_objects);
342 m_render_dirty = false;