2 #include "bufferbuilder.h"
7 #ifndef CONTROLPANEL_DISABLED
8 #include "controlpanel.h"
9 #endif //CONTROLPANEL_DISABLED
12 #ifdef TRANSFORM_BEZIERS_TO_PATH
13 #ifndef TRANSFORM_OBJECTS_NOT_VIEW
14 //#error Cannot TRANSFORM_BEZIERS_TO_PATH _without_ TRANSFORM_OBJECTS_NOT_VIEW
23 * Allocates memory for ObjectRenderers
24 * @param document - The document to associate the View with
25 * @param bounds - Initial bounds of the View
26 * @param colour - Colour to use for rendering this view. TODO: Make sure this actually works, or just remove it
28 View::View(Document & document, Screen & screen, const VRect & bounds, const Colour & colour)
29 : m_use_gpu_transform(false), m_use_gpu_rendering(USE_GPU_RENDERING), m_bounds_dirty(true), m_buffer_dirty(true),
30 m_render_dirty(true), m_document(document), m_screen(screen), m_cached_display(), m_bounds(bounds), m_colour(colour), m_bounds_ubo(),
31 m_objbounds_vbo(), m_object_renderers(NUMBER_OF_OBJECT_TYPES), m_cpu_rendering_pixels(NULL),
32 m_perform_shading(USE_SHADING), m_show_bezier_bounds(false), m_show_bezier_type(false),
33 m_show_fill_points(false), m_show_fill_bounds(false), m_lazy_rendering(true),
34 m_query_gpu_bounds_on_next_frame(NULL)
36 Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str());
38 screen.SetView(this); // oh dear...
42 // Create ObjectRenderers - new's match delete's in View::~View
43 //TODO: Don't forget to put new renderers here or things will be segfaultastic
46 m_object_renderers[RECT_FILLED] = new RectFilledRenderer();
47 m_object_renderers[RECT_OUTLINE] = new RectOutlineRenderer();
48 m_object_renderers[CIRCLE_FILLED] = new CircleFilledRenderer();
49 m_object_renderers[BEZIER] = new BezierRenderer();
50 m_object_renderers[PATH] = new PathRenderer();
54 for (int i = RECT_FILLED; i <= PATH; ++i)
55 m_object_renderers[i] = new FakeRenderer();
58 // To add rendering for a new type of object;
59 // 1. Add enum to ObjectType in ipdf.h
60 // 2. Implement class inheriting from ObjectRenderer using that type in objectrenderer.h and objectrenderer.cpp
65 #ifndef QUADTREE_DISABLED
66 m_quadtree_max_depth = 2;
67 m_current_quadtree_node = document.GetQuadTree().root_id;
73 * Frees memory used by ObjectRenderers
77 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
79 delete m_object_renderers[i]; // delete's match new's in constructor
81 m_object_renderers.clear();
82 delete [] m_cpu_rendering_pixels;
87 * @param x, y - Amount to translate
89 void View::Translate(Real x, Real y)
91 PROFILE_SCOPE("View::Translate");
92 if (!m_use_gpu_transform)
93 m_buffer_dirty = true;
94 m_bounds_dirty = true;
95 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
96 ObjectType type = NUMBER_OF_OBJECT_TYPES;
97 #ifdef TRANSFORM_BEZIERS_TO_PATH
100 m_document.TranslateObjects(-x, -y, type);
102 m_bounds.x += m_bounds.w*VReal(x);
103 m_bounds.y += m_bounds.h*VReal(y);
104 //Debug("View Bounds => %s", m_bounds.Str().c_str());
111 * @param bounds - New bounds
113 void View::SetBounds(const Rect & bounds)
115 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
116 ObjectType type = NUMBER_OF_OBJECT_TYPES;
117 #ifdef TRANSFORM_BEZIERS_TO_PATH
120 SVGMatrix transform = {Real(m_bounds.w)/bounds.w, 0, Real(m_bounds.x) - bounds.x, 0,Real(m_bounds.h)/bounds.h, Real(m_bounds.y) - bounds.y};
121 m_document.TransformObjectBounds(transform, type);
123 m_bounds.x = bounds.x;
124 m_bounds.y = bounds.y;
125 m_bounds.w = bounds.w;
126 m_bounds.h = bounds.h;
127 if (!m_use_gpu_transform)
128 m_buffer_dirty = true;
129 m_bounds_dirty = true;
133 * Scale the View at a point
134 * @param x, y - Coordinates to scale at (eg: Mouse cursor position)
135 * @param scale_amount - Amount to scale by
137 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
139 PROFILE_SCOPE("View::ScaleAroundPoint");
140 // (x0, y0, w, h) -> (x*w - (x*w - x0)*s, y*h - (y*h - y0)*s, w*s, h*s)
141 // x and y are coordinates in the window
142 // Convert to local coords.
143 if (!m_use_gpu_transform)
144 m_buffer_dirty = true;
145 m_bounds_dirty = true;
148 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
149 ObjectType type = NUMBER_OF_OBJECT_TYPES;
150 #ifdef TRANSFORM_BEZIERS_TO_PATH
153 m_document.ScaleObjectsAboutPoint(x, y, scale_amount, type);
155 VReal vx = m_bounds.w * VReal(x);
156 VReal vy = m_bounds.h * VReal(y);
160 VReal top = vy - m_bounds.y;
161 VReal left = vx - m_bounds.x;
164 left *= scale_amount;
166 m_bounds.x = vx - left;
167 m_bounds.y = vy - top;
168 m_bounds.w *= scale_amount;
169 m_bounds.h *= scale_amount;
170 //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());
176 * Transform a point in the document to a point relative to the top left corner of the view
177 * This is the CPU coordinate transform code; used only if the CPU is doing coordinate transforms
178 * @param inp - Input Rect {x,y,w,h} in the document
179 * @returns output Rect {x,y,w,h} in the View
181 Rect View::TransformToViewCoords(const Rect& inp) const
183 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
186 return TransformRectCoordinates(m_bounds.Convert<Real>(), inp);
191 * Updates FrameBuffer if the document, object bounds, or view bounds have changed, then Blits it
192 * Otherwise just Blits the cached FrameBuffer
193 * @param width - Width of View to render
194 * @param height - Height of View to render
196 void View::Render(int width, int height)
198 PROFILE_SCOPE("View::Render()");
199 if (!m_screen.Valid()) return;
200 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION,42,-1, "Beginning View::Render()");
201 // View dimensions have changed (ie: Window was resized)
202 int prev_width = m_cached_display.GetWidth();
203 int prev_height = m_cached_display.GetHeight();
204 if (width != prev_width || height != prev_height)
206 m_cached_display.Create(width, height);
207 m_bounds_dirty = true;
210 // View bounds have not changed; blit the FrameBuffer as it is
211 if (!m_bounds_dirty && m_lazy_rendering)
213 m_cached_display.UnBind();
214 m_cached_display.Blit();
218 m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
219 m_cached_display.Clear();
221 #ifndef QUADTREE_DISABLED
222 // I'm going to write this out in comments, so hopefully then I'll understand it. :/
224 // This code looks at the current bounds and tries to work out how they need to change
225 // to keep the view looking at the correct quadtree node.
227 // The idea is that the width/height of the view bounds are always 0.5<=wh<=1.0. We then always
228 // try to keep the bottom-right corner of the node on-screen, changing nodes to suit. Why bottom-right,
229 // you may ask. It's an excellent question, with a dubious, hand-wavey answer: because we're manipulating
230 // the bounds, it was easier to do it that way. (The top-left corner of the bounds are within the main
232 if (m_bounds_dirty || !m_lazy_rendering)
234 g_profiler.BeginZone("View::Render -- Quadtree view bounds management");
235 // If we're too far zoomed out, become the parent of the current node.
236 while ( m_bounds.w > 1.0 || m_bounds.h > 1.0)
238 // If a parent node exists, we'll become it.
239 //TODO: Generate a new parent node if none exists, and work out when to change child_type
240 // away from QTC_UNKNOWN
241 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
243 m_bounds = TransformFromQuadChild(m_bounds, m_document.GetQuadTree().nodes[m_current_quadtree_node].child_type);
244 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].parent;
249 // If we have a parent... (This prevents some crashes, but should disappear.)
250 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
252 // If the current node is off the left-hand side of the screen...
253 while (m_bounds.x > 1)
255 //... the current node becomes the node to its right.
256 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
257 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 1, 0, &m_document);
259 while (m_bounds.y > 1)
261 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
262 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, 1, &m_document);
264 while (m_bounds.x < 0)
266 m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
267 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, -1, 0, &m_document);
269 while (m_bounds.y < 0)
271 m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
272 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, -1, &m_document);
276 // Recurse into a node if we are completely within it. (If we're okay with having an invalid frame or two, we can remove this.)
277 if (ContainedInQuadChild(m_bounds, QTC_TOP_LEFT))
279 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left == QUADTREE_EMPTY)
281 // We want to reparent into a child node, but none exist. Get the document to create one.
282 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_LEFT);
283 m_render_dirty = true;
285 m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_LEFT);
286 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left;
288 if (ContainedInQuadChild(m_bounds, QTC_TOP_RIGHT))
290 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right == QUADTREE_EMPTY)
292 // We want to reparent into a child node, but none exist. Get the document to create one.
293 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_RIGHT);
294 m_render_dirty = true;
296 m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_RIGHT);
297 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right;
299 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_LEFT))
301 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left == QUADTREE_EMPTY)
303 // We want to reparent into a child node, but none exist. Get the document to create one.
304 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_LEFT);
305 m_render_dirty = true;
307 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_LEFT);
308 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left;
310 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_RIGHT))
312 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
314 // We want to reparent into a child node, but none exist. Get the document to create one.
315 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
316 m_render_dirty = true;
318 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
319 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
322 // Otherwise, we'll arbitrarily select the bottom-right.
323 // TODO: Perhaps select based on greatest area?
324 while (m_bounds.w < 0.5 || m_bounds.h < 0.5)
326 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
328 // We want to reparent into a child node, but none exist. Get the document to create one.
329 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
330 m_render_dirty = true;
332 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
333 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
335 g_profiler.EndZone();
338 m_screen.DebugFontPrintF("Current View QuadTree");
339 QuadTreeIndex overlay = m_current_quadtree_node;
340 while (overlay != -1)
342 m_screen.DebugFontPrintF(" Node: %d (objs: %d -> %d)", overlay, m_document.GetQuadTree().nodes[overlay].object_begin,
343 m_document.GetQuadTree().nodes[overlay].object_end);
344 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
346 m_screen.DebugFontPrintF("\n");
347 m_screen.DebugFontPrintF("Left: %d, Right: %d, Up: %d, Down: %d\n",
348 m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, -1, 0, 0),
349 m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 1, 0, 0),
350 m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, -1, 0),
351 m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, 1, 0));
354 Rect view_top_bounds = m_bounds;
355 QuadTreeIndex tmp = m_current_quadtree_node;
358 view_top_bounds = TransformFromQuadChild(view_top_bounds, m_document.GetQuadTree().nodes[tmp].child_type);
359 tmp = m_document.GetQuadTree().nodes[tmp].parent;
361 m_screen.DebugFontPrintF("Equivalent View Bounds: %s\n", view_top_bounds.Str().c_str());
364 if (!m_use_gpu_rendering)
366 // Dynamically resize CPU rendering target pixels if needed
367 if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
369 delete [] m_cpu_rendering_pixels;
370 m_cpu_rendering_pixels = new uint8_t[width*height*4];
371 if (m_cpu_rendering_pixels == NULL)
372 Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
374 // Clear CPU rendering pixels
375 for (int i = 0; i < width*height*4; ++i)
376 m_cpu_rendering_pixels[i] = 255;
378 #ifdef QUADTREE_DISABLED
379 RenderRange(width, height, 0, m_document.ObjectCount());
381 // Make sure we update the gpu buffers properly.
382 if (m_document.m_document_dirty)
384 m_render_dirty = m_buffer_dirty = true;
385 m_document.m_document_dirty = false;
387 RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
389 if (!m_use_gpu_rendering)
391 m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
392 // Debug for great victory (do something similar for GPU and compare?)
393 //ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
395 m_cached_display.UnBind(); // resets render target to the screen
396 m_cached_display.Blit(); // blit FrameBuffer to screen
397 m_buffer_dirty = false;
400 #ifndef CONTROLPANEL_DISABLED
401 // The powers that be suggest that this may be causing of the segfaults.
402 //ControlPanel::Update();
403 #endif //CONTROLPANEL_DISABLED
404 //Debug("Completed Render");
408 #ifndef QUADTREE_DISABLED
409 void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
411 Rect old_bounds = m_bounds;
412 if (node == QUADTREE_EMPTY) return;
413 if (!remaining_depth) return;
414 m_bounds_dirty = true;
415 QuadTreeIndex overlay = node;
418 //Debug("Rendering QT node %d, (overlay %d, objs: %d -- %d)\n", node, overlay, m_document.GetQuadTree().nodes[overlay].object_begin, m_document.GetQuadTree().nodes[overlay].object_end);
419 if (m_document.GetQuadTree().nodes[overlay].render_dirty)
420 m_buffer_dirty = m_render_dirty = true;
421 RenderRange(width, height, m_document.GetQuadTree().nodes[overlay].object_begin, m_document.GetQuadTree().nodes[overlay].object_end);
422 const_cast<bool&>(m_document.GetQuadTree().nodes[overlay].render_dirty) = false;
423 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
426 if (m_bounds.Intersects(Rect(1,1,1,1)))
428 m_bounds = Rect(m_bounds.x - 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
429 m_bounds_dirty = true;
430 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 1, &m_document), remaining_depth - 1);
432 m_bounds = old_bounds;
433 if (m_bounds.Intersects(Rect(1,0,1,1)))
435 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
436 m_bounds_dirty = true;
437 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 0, &m_document), remaining_depth - 1);
439 m_bounds = old_bounds;
440 if (m_bounds.Intersects(Rect(0,1,1,1)))
442 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
443 m_bounds_dirty = true;
444 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, 1, &m_document), remaining_depth - 1);
446 m_bounds = old_bounds;
447 m_bounds_dirty = true;
450 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_LEFT);
451 m_bounds_dirty = true;
452 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_left, remaining_depth-1);
453 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_RIGHT);
454 m_bounds_dirty = true;
455 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_right, remaining_depth-1);
456 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_LEFT);
457 m_bounds_dirty = true;
458 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_left, remaining_depth-1);
459 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_RIGHT);
460 m_bounds_dirty = true;
461 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_right, remaining_depth-1);
462 m_bounds = old_bounds;
463 m_bounds_dirty = true;
468 void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
470 // We don't want to render an empty range,
471 // so don't waste time setting up everything.
472 if (first_obj == last_obj) return;
473 PROFILE_SCOPE("View::RenderRange");
474 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 43, -1, "View::RenderRange()");
475 if (m_render_dirty) // document has changed
479 if (m_buffer_dirty || m_bounds_dirty || !m_lazy_rendering) // object bounds have changed
481 if (m_use_gpu_rendering)
482 UpdateObjBoundsVBO(first_obj, last_obj);
487 if (m_use_gpu_rendering)
490 if (m_use_gpu_transform)
492 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
493 //Debug("Transform objects, not view");
494 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
495 0.0f, 0.0f, float(width), float(height)};
497 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)),
498 0.0, 0.0, static_cast<GLfloat>(width), static_cast<GLfloat>(height)};
500 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
504 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
505 0.0f, 0.0f, float(width), float(height)};
506 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
508 m_bounds_dirty = false;
510 if (m_colour.a < 1.0f)
513 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
515 m_objbounds_vbo.Bind();
517 glEnableVertexAttribArray(0);
518 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
520 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
522 m_object_renderers[i]->RenderUsingGPU(first_obj, last_obj);
525 glDisableVertexAttribArray(0);
526 if (m_colour.a < 1.0f)
531 else // Rasterise on CPU then blit texture to GPU
534 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
536 m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height}, first_obj, last_obj);
542 void View::UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj)
544 PROFILE_SCOPE("View::UpdateObjBoundsVBO");
545 if (m_query_gpu_bounds_on_next_frame != NULL)
547 fprintf(m_query_gpu_bounds_on_next_frame,"# View: %s\t%s\t%s\t%s", Str(m_bounds.x).c_str(), Str(m_bounds.y).c_str(), Str(m_bounds.w).c_str(), Str(m_bounds.h).c_str());
550 //m_objbounds_vbo.Invalidate();
551 m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
552 m_objbounds_vbo.SetName("Object Bounds VBO");
554 #ifndef TRANSFORM_OBJECTS_NOT_VIEW
555 if (m_use_gpu_transform)
557 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
560 #endif //TRANSFORM_OBJECTS_NOT_VIEW
562 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicCopy);
564 m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
566 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());
568 #ifndef TRANSFORM_BEZIERS_TO_PATH
569 for (unsigned id = first_obj; id < last_obj; ++id)
572 if (m_use_gpu_transform)
574 obj_bounds = m_document.m_objects.bounds[id];
578 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
580 GPUObjBounds gpu_bounds = {
583 Float(obj_bounds.x + obj_bounds.w),
584 Float(obj_bounds.y + obj_bounds.h)
587 if (m_query_gpu_bounds_on_next_frame != NULL)
589 fprintf(m_query_gpu_bounds_on_next_frame,"%d\t%f\t%f\t%f\t%f\n", id, Float(obj_bounds.x), Float(obj_bounds.y), Float(obj_bounds.w), Float(obj_bounds.h));
592 obj_bounds_builder.Add(gpu_bounds);
595 for (unsigned i = 0; i < m_document.m_objects.paths.size(); ++i)
597 Path & path = m_document.m_objects.paths[i];
598 Rect & pbounds = path.GetBounds(m_document.m_objects); // Not very efficient...
599 //TODO: Add clipping here
600 //if (!pbounds.Intersects(Rect(0,0,1,1)) || pbounds.w < Real(1)/Real(800))
603 for (unsigned id = path.m_start; id <= path.m_end; ++id)
605 if (id < first_obj || id >= last_obj)
608 Rect obj_bounds = m_document.m_objects.bounds[id];
610 obj_bounds.x *= pbounds.w;
611 obj_bounds.x += pbounds.x;
612 obj_bounds.y *= pbounds.h;
613 obj_bounds.y += pbounds.y;
614 obj_bounds.w *= pbounds.w;
615 obj_bounds.h *= pbounds.h;
617 if (!m_use_gpu_transform)
618 obj_bounds = TransformToViewCoords(obj_bounds);
619 GPUObjBounds gpu_bounds = {
620 ClampFloat(obj_bounds.x),
621 ClampFloat(obj_bounds.y),
622 ClampFloat(obj_bounds.x + obj_bounds.w),
623 ClampFloat(obj_bounds.y + obj_bounds.h)
625 obj_bounds_builder.Add(gpu_bounds);
626 //Debug("Path %d %s -> %s via %s", id, m_document.m_objects.bounds[id].Str().c_str(), obj_bounds.Str().c_str(), pbounds.Str().c_str());
628 if (m_query_gpu_bounds_on_next_frame != NULL)
630 fprintf(m_query_gpu_bounds_on_next_frame,"%d\t%f\t%f\t%f\t%f\n", id, ClampFloat(obj_bounds.x), ClampFloat(obj_bounds.y), ClampFloat(obj_bounds.w), ClampFloat(obj_bounds.h));
633 GPUObjBounds p_gpu_bounds = {
634 ClampFloat(pbounds.x),
635 ClampFloat(pbounds.y),
636 ClampFloat(pbounds.x + pbounds.w),
637 ClampFloat(pbounds.y + pbounds.h)
639 obj_bounds_builder.Add(p_gpu_bounds);
642 if (m_query_gpu_bounds_on_next_frame != NULL)
644 if (m_query_gpu_bounds_on_next_frame != stdout && m_query_gpu_bounds_on_next_frame != stderr)
645 fclose(m_query_gpu_bounds_on_next_frame);
646 m_query_gpu_bounds_on_next_frame = NULL;
648 m_objbounds_vbo.UnMap();
651 * Prepare the document for rendering
652 * Will be called on View::Render if m_render_dirty is set
653 * (Called at least once, on the first Render)
655 void View::PrepareRender()
657 PROFILE_SCOPE("View::PrepareRender()");
658 Debug("Recreate buffers with %u objects", m_document.ObjectCount());
659 // Prepare bounds vbo
660 if (UsingGPURendering())
662 m_bounds_ubo.Invalidate();
663 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
664 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
665 m_bounds_ubo.SetName("m_bounds_ubo: Screen bounds.");
668 // Instead of having each ObjectRenderer go through the whole document
669 // we initialise them, go through the document once adding to the appropriate Renderers
670 // and then finalise them
671 // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
673 // Prepare the buffers
674 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
676 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
679 // Add objects from Document to buffers
680 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
682 ObjectType type = m_document.m_objects.types[id];
683 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
684 // (Also, Wow I just actually used std::vector::at())
685 // (Also, I just managed to make it throw an exception because I'm a moron)
686 //Debug("Object of type %d", type);
690 // Finish the buffers
691 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
693 m_object_renderers[i]->FinaliseBuffers();
695 if (UsingGPURendering())
697 dynamic_cast<BezierRenderer*>(m_object_renderers[BEZIER])->PrepareBezierGPUBuffer(m_document.m_objects);
699 m_render_dirty = false;
702 void View::SaveCPUBMP(const char * filename)
704 bool prev = UsingGPURendering();
705 SetGPURendering(false);
707 ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, 800, 600}, filename);
708 SetGPURendering(prev);
711 void View::SaveGPUBMP(const char * filename)
713 bool prev = UsingGPURendering();
714 SetGPURendering(true);
716 m_screen.ScreenShot(filename);
717 SetGPURendering(prev);
720 void View::QueryGPUBounds(const char * filename, const char * mode)
722 m_query_gpu_bounds_on_next_frame = fopen(filename, mode);
723 Debug("File: %s", filename);
724 if (m_query_gpu_bounds_on_next_frame == NULL)
725 Error("Couldn't open file \"%s\" : %s", filename, strerror(errno));