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.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;
214 // TODO: Support generating new parent nodes.
215 if (false && m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
217 if (m_bounds.x < -0.5)
219 m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
220 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, -1, 0, &m_document);
222 if (m_bounds.y < -0.5)
224 m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
225 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, -1, &m_document);
227 if (m_bounds.w + m_bounds.x > 0.5)
229 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
230 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 1, 0, &m_document);
232 if (m_bounds.h + m_bounds.y > 0.5)
234 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
235 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, 1, &m_document);
239 if (ContainedInQuadChild(m_bounds, QTC_TOP_LEFT))
241 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left == QUADTREE_EMPTY)
243 // We want to reparent into a child node, but none exist. Get the document to create one.
244 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_LEFT);
245 m_render_dirty = true;
247 m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_LEFT);
248 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left;
250 if (ContainedInQuadChild(m_bounds, QTC_TOP_RIGHT))
252 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right == QUADTREE_EMPTY)
254 // We want to reparent into a child node, but none exist. Get the document to create one.
255 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_RIGHT);
256 m_render_dirty = true;
258 m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_RIGHT);
259 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right;
261 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_LEFT))
263 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left == QUADTREE_EMPTY)
265 // We want to reparent into a child node, but none exist. Get the document to create one.
266 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_LEFT);
267 m_render_dirty = true;
269 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_LEFT);
270 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left;
272 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_RIGHT))
274 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
276 // We want to reparent into a child node, but none exist. Get the document to create one.
277 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
278 m_render_dirty = true;
280 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
281 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
285 m_screen.DebugFontPrintF("Current View QuadTree");
286 QuadTreeIndex overlay = m_current_quadtree_node;
287 while (overlay != -1)
289 m_screen.DebugFontPrintF(" Node: %d (objs: %d -> %d)", overlay, m_document.GetQuadTree().nodes[overlay].object_begin,
290 m_document.GetQuadTree().nodes[overlay].object_end);
291 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
293 m_screen.DebugFontPrintF("\n");
295 Rect view_top_bounds = m_bounds;
296 QuadTreeIndex tmp = m_current_quadtree_node;
299 view_top_bounds = TransformFromQuadChild(view_top_bounds, m_document.GetQuadTree().nodes[tmp].child_type);
300 tmp = m_document.GetQuadTree().nodes[tmp].parent;
302 m_screen.DebugFontPrintF("Equivalent View Bounds: %s\n", view_top_bounds.Str().c_str());
305 if (!m_use_gpu_rendering)
307 // Dynamically resize CPU rendering target pixels if needed
308 if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
310 delete [] m_cpu_rendering_pixels;
311 m_cpu_rendering_pixels = new uint8_t[width*height*4];
312 if (m_cpu_rendering_pixels == NULL)
313 Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
315 // Clear CPU rendering pixels
316 for (int i = 0; i < width*height*4; ++i)
317 m_cpu_rendering_pixels[i] = 255;
319 #ifdef QUADTREE_DISABLED
320 RenderRange(width, height, 0, m_document.ObjectCount());
322 RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
324 if (!m_use_gpu_rendering)
326 m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
327 // Debug for great victory (do something similar for GPU and compare?)
328 //ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
330 m_cached_display.UnBind(); // resets render target to the screen
331 m_cached_display.Blit(); // blit FrameBuffer to screen
332 m_buffer_dirty = false;
335 #ifndef CONTROLPANEL_DISABLED
336 ControlPanel::Update();
337 #endif //CONTROLPANEL_DISABLED
338 //Debug("Completed Render");
342 #ifndef QUADTREE_DISABLED
343 void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
345 Rect old_bounds = m_bounds;
346 if (node == QUADTREE_EMPTY) return;
347 if (!remaining_depth) return;
348 //Debug("Rendering QT node %d, (objs: %d -- %d)\n", node, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
349 m_bounds_dirty = true;
350 QuadTreeIndex overlay = node;
353 RenderRange(width, height, m_document.GetQuadTree().nodes[overlay].object_begin, m_document.GetQuadTree().nodes[overlay].object_end);
354 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
357 if (m_bounds.Intersects(Rect(-1,-1,1,1)))
359 m_bounds = Rect(m_bounds.x - 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
360 m_bounds_dirty = true;
361 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, -1, &m_document), remaining_depth - 1);
363 m_bounds = old_bounds;
364 if (m_bounds.Intersects(Rect(-1,0,1,1)))
366 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
367 m_bounds_dirty = true;
368 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 0, &m_document), remaining_depth - 1);
370 m_bounds = old_bounds;
371 if (m_bounds.Intersects(Rect(-1,1,1,1)))
373 m_bounds = Rect(m_bounds.x - 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
374 m_bounds_dirty = true;
375 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 1, &m_document), remaining_depth - 1);
377 m_bounds = old_bounds;
378 if (m_bounds.Intersects(Rect(0,-1,1,1)))
380 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
381 m_bounds_dirty = true;
382 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, -1, &m_document), remaining_depth - 1);
384 m_bounds = old_bounds;
385 if (m_bounds.Intersects(Rect(0,1,1,1)))
387 m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
388 m_bounds_dirty = true;
389 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, 1, &m_document), remaining_depth - 1);
391 m_bounds = old_bounds;
392 if (m_bounds.Intersects(Rect(1,-1,1,1)))
394 m_bounds = Rect(m_bounds.x + 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
395 m_bounds_dirty = true;
396 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, -1, &m_document), remaining_depth - 1);
398 m_bounds = old_bounds;
399 if (m_bounds.Intersects(Rect(1,0,1,1)))
401 m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
402 m_bounds_dirty = true;
403 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 0, &m_document), remaining_depth - 1);
405 m_bounds = old_bounds;
406 if (m_bounds.Intersects(Rect(1,1,1,1)))
408 m_bounds = Rect(m_bounds.x + 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
409 m_bounds_dirty = true;
410 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 1, &m_document), remaining_depth - 1);
412 m_bounds = old_bounds;
413 m_bounds_dirty = true;
416 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_LEFT);
417 m_bounds_dirty = true;
418 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_left, remaining_depth-1);
419 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_RIGHT);
420 m_bounds_dirty = true;
421 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_right, remaining_depth-1);
422 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_LEFT);
423 m_bounds_dirty = true;
424 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_left, remaining_depth-1);
425 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_RIGHT);
426 m_bounds_dirty = true;
427 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_right, remaining_depth-1);
428 m_bounds = old_bounds;
429 m_bounds_dirty = true;
434 void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
436 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 43, -1, "View::RenderRange()");
437 if (m_render_dirty) // document has changed
440 if (m_buffer_dirty || m_bounds_dirty || !m_lazy_rendering) // object bounds have changed
442 if (m_use_gpu_rendering)
443 UpdateObjBoundsVBO(first_obj, last_obj);
446 if (m_use_gpu_transform)
448 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
449 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
450 0.0f, 0.0f, float(width), float(height)};
452 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)),
453 0.0, 0.0, static_cast<GLfloat>(width), static_cast<GLfloat>(height)};
455 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
459 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
460 0.0f, 0.0f, float(width), float(height)};
461 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
463 m_bounds_dirty = false;
467 if (m_use_gpu_rendering)
469 if (m_colour.a < 1.0f)
472 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
474 m_objbounds_vbo.Bind();
476 glEnableVertexAttribArray(0);
477 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
479 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
481 m_object_renderers[i]->RenderUsingGPU(first_obj, last_obj);
484 glDisableVertexAttribArray(0);
485 if (m_colour.a < 1.0f)
490 else // Rasterise on CPU then blit texture to GPU
493 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
495 m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height}, first_obj, last_obj);
501 void View::UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj)
503 //m_objbounds_vbo.Invalidate();
504 m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
505 m_objbounds_vbo.SetName("Object Bounds VBO");
506 if (m_use_gpu_transform)
508 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
512 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicCopy);
514 m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
516 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());
518 for (unsigned id = first_obj; id < last_obj; ++id)
521 if (m_use_gpu_transform)
523 obj_bounds = m_document.m_objects.bounds[id];
527 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
529 GPUObjBounds gpu_bounds = {
530 (float)Float(obj_bounds.x),
531 (float)Float(obj_bounds.y),
532 (float)Float(obj_bounds.x + obj_bounds.w),
533 (float)Float(obj_bounds.y + obj_bounds.h)
535 obj_bounds_builder.Add(gpu_bounds);
538 m_objbounds_vbo.UnMap();
541 * Prepare the document for rendering
542 * Will be called on View::Render if m_render_dirty is set
543 * (Called at least once, on the first Render)
545 void View::PrepareRender()
547 Debug("Recreate buffers with %u objects", m_document.ObjectCount());
548 // Prepare bounds vbo
549 if (UsingGPURendering())
551 m_bounds_ubo.Invalidate();
552 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
553 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
554 m_bounds_ubo.SetName("m_bounds_ubo: Screen bounds.");
557 // Instead of having each ObjectRenderer go through the whole document
558 // we initialise them, go through the document once adding to the appropriate Renderers
559 // and then finalise them
560 // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
562 // Prepare the buffers
563 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
565 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
568 // Add objects from Document to buffers
569 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
571 ObjectType type = m_document.m_objects.types[id];
572 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
573 // (Also, Wow I just actually used std::vector::at())
574 // (Also, I just managed to make it throw an exception because I'm a moron)
575 //Debug("Object of type %d", type);
579 // Finish the buffers
580 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
582 m_object_renderers[i]->FinaliseBuffers();
584 if (UsingGPURendering())
585 dynamic_cast<BezierRenderer*>(m_object_renderers[BEZIER])->PrepareBezierGPUBuffer(m_document.m_objects);
586 m_render_dirty = false;
589 void View::SaveCPUBMP(const char * filename)
591 bool prev = UsingGPURendering();
592 SetGPURendering(false);
594 ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, 800, 600}, filename);
595 SetGPURendering(prev);
598 void View::SaveGPUBMP(const char * filename)
600 bool prev = UsingGPURendering();
601 SetGPURendering(true);
603 m_screen.ScreenShot(filename);
604 SetGPURendering(prev);