2 #include "bufferbuilder.h"
6 #ifndef CONTROLPANEL_DISABLED
7 #include "controlpanel.h"
8 #endif //CONTROLPANEL_DISABLED
15 * Allocates memory for ObjectRenderers
16 * @param document - The document to associate the View with
17 * @param bounds - Initial bounds of the View
18 * @param colour - Colour to use for rendering this view. TODO: Make sure this actually works, or just remove it
20 View::View(Document & document, Screen & screen, const Rect & bounds, const Colour & colour)
21 : m_use_gpu_transform(USE_GPU_TRANSFORM), m_use_gpu_rendering(USE_GPU_RENDERING), m_bounds_dirty(true), m_buffer_dirty(true),
22 m_render_dirty(true), m_document(document), m_screen(screen), m_cached_display(), m_bounds(bounds), m_colour(colour), m_bounds_ubo(),
23 m_objbounds_vbo(), m_object_renderers(NUMBER_OF_OBJECT_TYPES), m_cpu_rendering_pixels(NULL),
24 m_perform_shading(USE_SHADING), m_show_bezier_bounds(false), m_show_bezier_type(false),
25 m_show_fill_points(false), m_show_fill_bounds(false), m_lazy_rendering(true)
27 Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str());
29 screen.SetView(this); // oh dear...
33 // Create ObjectRenderers - new's match delete's in View::~View
34 //TODO: Don't forget to put new renderers here or things will be segfaultastic
37 m_object_renderers[RECT_FILLED] = new RectFilledRenderer();
38 m_object_renderers[RECT_OUTLINE] = new RectOutlineRenderer();
39 m_object_renderers[CIRCLE_FILLED] = new CircleFilledRenderer();
40 m_object_renderers[BEZIER] = new BezierRenderer();
41 m_object_renderers[PATH] = new PathRenderer();
45 for (int i = RECT_FILLED; i <= PATH; ++i)
46 m_object_renderers[i] = new FakeRenderer();
49 // To add rendering for a new type of object;
50 // 1. Add enum to ObjectType in ipdf.h
51 // 2. Implement class inheriting from ObjectRenderer using that type in objectrenderer.h and objectrenderer.cpp
56 #ifndef QUADTREE_DISABLED
57 m_quadtree_max_depth = 2;
58 m_current_quadtree_node = document.GetQuadTree().root_id;
64 * Frees memory used by ObjectRenderers
68 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
70 delete m_object_renderers[i]; // delete's match new's in constructor
72 m_object_renderers.clear();
73 delete [] m_cpu_rendering_pixels;
78 * @param x, y - Amount to translate
80 void View::Translate(Real x, Real y)
82 if (!m_use_gpu_transform)
83 m_buffer_dirty = true;
84 m_bounds_dirty = true;
85 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
86 m_document.TranslateObjects(-x, -y);
92 //Debug("View Bounds => %s", m_bounds.Str().c_str());
99 * @param bounds - New bounds
101 void View::SetBounds(const Rect & bounds)
103 m_bounds.x = bounds.x;
104 m_bounds.y = bounds.y;
105 m_bounds.w = bounds.w;
106 m_bounds.h = bounds.h;
107 if (!m_use_gpu_transform)
108 m_buffer_dirty = true;
109 m_bounds_dirty = true;
113 * Scale the View at a point
114 * @param x, y - Coordinates to scale at (eg: Mouse cursor position)
115 * @param scale_amount - Amount to scale by
117 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
120 // (x0, y0, w, h) -> (x*w - (x*w - x0)*s, y*h - (y*h - y0)*s, w*s, h*s)
121 // x and y are coordinates in the window
122 // Convert to local coords.
123 if (!m_use_gpu_transform)
124 m_buffer_dirty = true;
125 m_bounds_dirty = true;
128 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
129 m_document.ScaleObjectsAboutPoint(x, y, scale_amount);
136 Real top = y - m_bounds.y;
137 Real left = x - m_bounds.x;
140 left *= scale_amount;
142 m_bounds.x = x - left;
143 m_bounds.y = y - top;
144 m_bounds.w *= scale_amount;
145 m_bounds.h *= scale_amount;
146 //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());
152 * Transform a point in the document to a point relative to the top left corner of the view
153 * This is the CPU coordinate transform code; used only if the CPU is doing coordinate transforms
154 * @param inp - Input Rect {x,y,w,h} in the document
155 * @returns output Rect {x,y,w,h} in the View
157 Rect View::TransformToViewCoords(const Rect& inp) const
159 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
163 out.x = (inp.x - m_bounds.x) / m_bounds.w;
164 out.y = (inp.y - m_bounds.y) / m_bounds.h;
165 out.w = inp.w / m_bounds.w;
166 out.h = inp.h / m_bounds.h;
172 * Updates FrameBuffer if the document, object bounds, or view bounds have changed, then Blits it
173 * Otherwise just Blits the cached FrameBuffer
174 * @param width - Width of View to render
175 * @param height - Height of View to render
177 void View::Render(int width, int height)
179 if (!m_screen.Valid()) return;
180 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION,42,-1, "Beginning View::Render()");
181 // View dimensions have changed (ie: Window was resized)
182 int prev_width = m_cached_display.GetWidth();
183 int prev_height = m_cached_display.GetHeight();
184 if (width != prev_width || height != prev_height)
186 m_cached_display.Create(width, height);
187 m_bounds_dirty = true;
190 // View bounds have not changed; blit the FrameBuffer as it is
191 if (!m_bounds_dirty && m_lazy_rendering)
193 m_cached_display.UnBind();
194 m_cached_display.Blit();
198 m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
199 m_cached_display.Clear();
201 #ifndef QUADTREE_DISABLED
202 if (m_bounds_dirty || !m_lazy_rendering)
204 if ( (m_bounds.x > 1.0 || m_bounds.x < 0.0 || m_bounds.y > 1.0 || m_bounds.y < 0.0 || m_bounds.w > 1.0 || m_bounds.h > 1.0))
206 //TODO: Generate a new parent node.
207 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
209 m_bounds = TransformFromQuadChild(m_bounds, m_document.GetQuadTree().nodes[m_current_quadtree_node].child_type);
210 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].parent;
213 if (ContainedInQuadChild(m_bounds, QTC_TOP_LEFT))
215 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left == QUADTREE_EMPTY)
217 // We want to reparent into a child node, but none exist. Get the document to create one.
218 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_LEFT);
219 m_render_dirty = true;
221 m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_LEFT);
222 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left;
224 if (ContainedInQuadChild(m_bounds, QTC_TOP_RIGHT))
226 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right == QUADTREE_EMPTY)
228 // We want to reparent into a child node, but none exist. Get the document to create one.
229 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_RIGHT);
230 m_render_dirty = true;
232 m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_RIGHT);
233 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right;
235 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_LEFT))
237 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left == QUADTREE_EMPTY)
239 // We want to reparent into a child node, but none exist. Get the document to create one.
240 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_LEFT);
241 m_render_dirty = true;
243 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_LEFT);
244 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left;
246 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_RIGHT))
248 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
250 // We want to reparent into a child node, but none exist. Get the document to create one.
251 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
252 m_render_dirty = true;
254 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
255 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
259 m_screen.DebugFontPrintF("Current View QuadTree");
260 QuadTreeIndex overlay = m_current_quadtree_node;
261 while (overlay != -1)
263 m_screen.DebugFontPrintF(" Node: %d (objs: %d -> %d)", overlay, m_document.GetQuadTree().nodes[overlay].object_begin,
264 m_document.GetQuadTree().nodes[overlay].object_end);
265 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
267 m_screen.DebugFontPrintF("\n");
269 Rect view_top_bounds = m_bounds;
270 QuadTreeIndex tmp = m_current_quadtree_node;
273 view_top_bounds = TransformFromQuadChild(view_top_bounds, m_document.GetQuadTree().nodes[tmp].child_type);
274 tmp = m_document.GetQuadTree().nodes[tmp].parent;
276 m_screen.DebugFontPrintF("Equivalent View Bounds: %s\n", view_top_bounds.Str().c_str());
279 if (!m_use_gpu_rendering)
281 // Dynamically resize CPU rendering target pixels if needed
282 if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
284 delete [] m_cpu_rendering_pixels;
285 m_cpu_rendering_pixels = new uint8_t[width*height*4];
286 if (m_cpu_rendering_pixels == NULL)
287 Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
289 // Clear CPU rendering pixels
290 for (int i = 0; i < width*height*4; ++i)
291 m_cpu_rendering_pixels[i] = 255;
293 #ifdef QUADTREE_DISABLED
294 RenderRange(width, height, 0, m_document.ObjectCount());
296 RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
298 if (!m_use_gpu_rendering)
300 m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
301 // Debug for great victory (do something similar for GPU and compare?)
302 //ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
304 m_cached_display.UnBind(); // resets render target to the screen
305 m_cached_display.Blit(); // blit FrameBuffer to screen
306 m_buffer_dirty = false;
309 #ifndef CONTROLPANEL_DISABLED
310 ControlPanel::Update();
311 #endif //CONTROLPANEL_DISABLED
312 //Debug("Completed Render");
316 #ifndef QUADTREE_DISABLED
317 void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
319 Rect old_bounds = m_bounds;
320 if (node == QUADTREE_EMPTY) return;
321 if (!remaining_depth) return;
322 //Debug("Rendering QT node %d, (objs: %d -- %d)\n", node, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
323 m_bounds_dirty = true;
324 QuadTreeIndex overlay = node;
327 RenderRange(width, height, m_document.GetQuadTree().nodes[overlay].object_begin, m_document.GetQuadTree().nodes[overlay].object_end);
328 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
331 if (m_bounds.Intersects(Rect(-1,-1,1,1)))
333 m_bounds = Rect(m_bounds.x - 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
334 m_bounds_dirty = true;
335 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, -1), remaining_depth - 1);
337 if (m_bounds.Intersects(Rect(-1,0,1,1)))
339 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
340 m_bounds_dirty = true;
341 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 0), remaining_depth - 1);
343 if (m_bounds.Intersects(Rect(-1,1,1,1)))
345 m_bounds = Rect(m_bounds.x - 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
346 m_bounds_dirty = true;
347 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 1), remaining_depth - 1);
349 if (m_bounds.Intersects(Rect(0,-1,1,1)))
351 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
352 m_bounds_dirty = true;
353 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, -1), remaining_depth - 1);
355 if (m_bounds.Intersects(Rect(0,1,1,1)))
357 m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
358 m_bounds_dirty = true;
359 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, 1), remaining_depth - 1);
361 if (m_bounds.Intersects(Rect(1,-1,1,1)))
363 m_bounds = Rect(m_bounds.x + 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
364 m_bounds_dirty = true;
365 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, -1), remaining_depth - 1);
367 if (m_bounds.Intersects(Rect(1,0,1,1)))
369 m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
370 m_bounds_dirty = true;
371 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 0), remaining_depth - 1);
373 if (m_bounds.Intersects(Rect(1,1,1,1)))
375 m_bounds = Rect(m_bounds.x + 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
376 m_bounds_dirty = true;
377 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 1), remaining_depth - 1);
379 m_bounds = old_bounds;
380 m_bounds_dirty = true;
383 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_LEFT);
384 m_bounds_dirty = true;
385 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_left, remaining_depth-1);
386 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_RIGHT);
387 m_bounds_dirty = true;
388 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_right, remaining_depth-1);
389 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_LEFT);
390 m_bounds_dirty = true;
391 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_left, remaining_depth-1);
392 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_RIGHT);
393 m_bounds_dirty = true;
394 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_right, remaining_depth-1);
395 m_bounds = old_bounds;
396 m_bounds_dirty = true;
401 void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
403 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 43, -1, "View::RenderRange()");
404 if (m_render_dirty) // document has changed
407 if (m_buffer_dirty || m_bounds_dirty || !m_lazy_rendering) // object bounds have changed
409 if (m_use_gpu_rendering)
410 UpdateObjBoundsVBO(first_obj, last_obj);
413 if (m_use_gpu_transform)
415 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
416 //Debug("Transform objects, not view");
417 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
418 0.0f, 0.0f, float(width), float(height)};
420 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)),
421 0.0, 0.0, static_cast<GLfloat>(width), static_cast<GLfloat>(height)};
423 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
427 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
428 0.0f, 0.0f, float(width), float(height)};
429 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
431 m_bounds_dirty = false;
435 if (m_use_gpu_rendering)
437 if (m_colour.a < 1.0f)
440 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
442 m_objbounds_vbo.Bind();
444 glEnableVertexAttribArray(0);
445 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
447 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
449 m_object_renderers[i]->RenderUsingGPU(first_obj, last_obj);
452 glDisableVertexAttribArray(0);
453 if (m_colour.a < 1.0f)
458 else // Rasterise on CPU then blit texture to GPU
461 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
463 m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height}, first_obj, last_obj);
469 void View::UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj)
471 //m_objbounds_vbo.Invalidate();
472 m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
473 m_objbounds_vbo.SetName("Object Bounds VBO");
475 #ifndef TRANSFORM_OBJECTS_NOT_VIEW
476 if (m_use_gpu_transform)
478 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
481 #endif //TRANSFORM_OBJECTS_NOT_VIEW
483 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicCopy);
485 m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
487 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());
489 for (unsigned id = first_obj; id < last_obj; ++id)
492 if (m_use_gpu_transform)
494 obj_bounds = m_document.m_objects.bounds[id];
498 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
500 GPUObjBounds gpu_bounds = {
501 (float)Float(obj_bounds.x),
502 (float)Float(obj_bounds.y),
503 (float)Float(obj_bounds.x + obj_bounds.w),
504 (float)Float(obj_bounds.y + obj_bounds.h)
507 obj_bounds_builder.Add(gpu_bounds);
510 m_objbounds_vbo.UnMap();
513 * Prepare the document for rendering
514 * Will be called on View::Render if m_render_dirty is set
515 * (Called at least once, on the first Render)
517 void View::PrepareRender()
519 Debug("Recreate buffers with %u objects", m_document.ObjectCount());
520 // Prepare bounds vbo
521 if (UsingGPURendering())
523 m_bounds_ubo.Invalidate();
524 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
525 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
526 m_bounds_ubo.SetName("m_bounds_ubo: Screen bounds.");
529 // Instead of having each ObjectRenderer go through the whole document
530 // we initialise them, go through the document once adding to the appropriate Renderers
531 // and then finalise them
532 // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
534 // Prepare the buffers
535 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
537 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
540 // Add objects from Document to buffers
541 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
543 ObjectType type = m_document.m_objects.types[id];
544 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
545 // (Also, Wow I just actually used std::vector::at())
546 // (Also, I just managed to make it throw an exception because I'm a moron)
547 //Debug("Object of type %d", type);
551 // Finish the buffers
552 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
554 m_object_renderers[i]->FinaliseBuffers();
556 if (UsingGPURendering())
557 dynamic_cast<BezierRenderer*>(m_object_renderers[BEZIER])->PrepareBezierGPUBuffer(m_document.m_objects);
558 m_render_dirty = false;
561 void View::SaveCPUBMP(const char * filename)
563 bool prev = UsingGPURendering();
564 SetGPURendering(false);
566 ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, 800, 600}, filename);
567 SetGPURendering(prev);
570 void View::SaveGPUBMP(const char * filename)
572 bool prev = UsingGPURendering();
573 SetGPURendering(true);
575 m_screen.ScreenShot(filename);
576 SetGPURendering(prev);