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 // Create ObjectRenderers - new's match delete's in View::~View
24 //TODO: Don't forget to put new renderers here or things will be segfaultastic
25 m_object_renderers[RECT_FILLED] = new RectFilledRenderer();
26 m_object_renderers[RECT_OUTLINE] = new RectOutlineRenderer();
27 m_object_renderers[CIRCLE_FILLED] = new CircleFilledRenderer();
28 m_object_renderers[BEZIER] = new BezierRenderer();
30 // To add rendering for a new type of object;
31 // 1. Add enum to ObjectType in ipdf.h
32 // 2. Implement class inheriting from ObjectRenderer using that type in objectrenderer.h and objectrenderer.cpp
37 #ifndef QUADTREE_DISABLED
38 m_quadtree_max_depth = 1;
39 m_current_quadtree_node = document.GetQuadTree().root_id;
45 * Frees memory used by ObjectRenderers
49 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
51 delete m_object_renderers[i]; // delete's match new's in constructor
53 m_object_renderers.clear();
54 delete [] m_cpu_rendering_pixels;
59 * @param x, y - Amount to translate
61 void View::Translate(Real x, Real y)
67 Debug("View Bounds => %s", m_bounds.Str().c_str());
68 if (!m_use_gpu_transform)
69 m_buffer_dirty = true;
70 m_bounds_dirty = true;
74 * Scale the View at a point
75 * @param x, y - Coordinates to scale at (eg: Mouse cursor position)
76 * @param scale_amount - Amount to scale by
78 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
80 // x and y are coordinates in the window
81 // Convert to local coords.
87 Real top = y - m_bounds.y;
88 Real left = x - m_bounds.x;
93 m_bounds.x = x - left;
95 m_bounds.w *= scale_amount;
96 m_bounds.h *= scale_amount;
97 //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());
98 if (!m_use_gpu_transform)
99 m_buffer_dirty = true;
100 m_bounds_dirty = true;
104 * Transform a point in the document to a point relative to the top left corner of the view
105 * This is the CPU coordinate transform code; used only if the CPU is doing coordinate transforms
106 * @param inp - Input Rect {x,y,w,h} in the document
107 * @returns output Rect {x,y,w,h} in the View
109 Rect View::TransformToViewCoords(const Rect& inp) const
112 out.x = (inp.x - m_bounds.x) / m_bounds.w;
113 out.y = (inp.y - m_bounds.y) / m_bounds.h;
114 out.w = inp.w / m_bounds.w;
115 out.h = inp.h / m_bounds.h;
121 * Updates FrameBuffer if the document, object bounds, or view bounds have changed, then Blits it
122 * Otherwise just Blits the cached FrameBuffer
123 * @param width - Width of View to render
124 * @param height - Height of View to render
126 void View::Render(int width, int height)
128 #ifdef QUADTREE_DISABLED
129 RenderRange(width, height, 0, m_document.ObjectCount());
131 RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
135 #ifndef QUADTREE_DISABLED
136 void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
138 Rect old_bounds = m_bounds;
139 if (node == QUADTREE_EMPTY) return;
140 if (!remaining_depth) return;
141 RenderRange(width, height, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
143 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_LEFT);
144 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_left, remaining_depth-1);
145 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_RIGHT);
146 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_right, remaining_depth-1);
147 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_LEFT);
148 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_left, remaining_depth-1);
149 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_RIGHT);
150 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_right, remaining_depth-1);
151 m_bounds = old_bounds;
155 void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
157 // View dimensions have changed (ie: Window was resized)
158 int prev_width = m_cached_display.GetWidth();
159 int prev_height = m_cached_display.GetHeight();
160 if (width != prev_width || height != prev_height)
162 m_cached_display.Create(width, height);
163 m_bounds_dirty = true;
166 // View bounds have not changed; blit the FrameBuffer as it is
169 m_cached_display.UnBind();
170 m_cached_display.Blit();
174 // Bind FrameBuffer for rendering, and clear it
177 if (m_render_dirty) // document has changed
180 if (m_buffer_dirty) // object bounds have changed
181 UpdateObjBoundsVBO();
183 if (m_use_gpu_transform)
185 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)),
186 0.0, 0.0, 640.0, 480.0};
187 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
191 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
192 0.0f, 0.0f, 640.0f, 480.0f};
193 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
195 m_bounds_dirty = false;
197 m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
198 m_cached_display.Clear();
201 if (m_use_gpu_rendering)
203 if (m_colour.a < 1.0f)
206 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
208 m_objbounds_vbo.Bind();
210 glEnableVertexAttribArray(0);
211 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
213 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
215 m_object_renderers[i]->RenderUsingGPU(first_obj, last_obj);
218 glDisableVertexAttribArray(0);
219 if (m_colour.a < 1.0f)
224 else // Rasterise on CPU then blit texture to GPU
226 // Dynamically resize CPU rendering target pixels if needed
227 if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
229 delete [] m_cpu_rendering_pixels;
230 m_cpu_rendering_pixels = new uint8_t[width*height*4];
231 if (m_cpu_rendering_pixels == NULL)
232 Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
234 // Clear CPU rendering pixels
235 for (int i = 0; i < width*height*4; ++i)
236 m_cpu_rendering_pixels[i] = 255;
238 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
240 m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height}, first_obj, last_obj);
242 m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
243 // Debug for great victory (do something similar for GPU and compare?)
244 ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
246 m_cached_display.UnBind(); // resets render target to the screen
247 m_cached_display.Blit(); // blit FrameBuffer to screen
250 void View::UpdateObjBoundsVBO()
252 m_objbounds_vbo.Invalidate();
253 m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
254 if (m_use_gpu_transform)
256 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
260 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
262 m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
264 BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.Map(false, true, true), m_objbounds_vbo.GetSize());
266 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
269 if (m_use_gpu_transform)
271 obj_bounds = m_document.m_objects.bounds[id];
275 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
277 GPUObjBounds gpu_bounds = {
278 (float)Float(obj_bounds.x),
279 (float)Float(obj_bounds.y),
280 (float)Float(obj_bounds.x + obj_bounds.w),
281 (float)Float(obj_bounds.y + obj_bounds.h)
283 obj_bounds_builder.Add(gpu_bounds);
286 m_objbounds_vbo.UnMap();
287 m_buffer_dirty = false;
290 * Prepare the document for rendering
291 * Will be called on View::Render if m_render_dirty is set
292 * (Called at least once, on the first Render)
294 void View::PrepareRender()
296 Debug("Recreate buffers with %u objects", m_document.ObjectCount());
297 // Prepare bounds vbo
298 m_bounds_ubo.Invalidate();
299 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
300 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
302 // Instead of having each ObjectRenderer go through the whole document
303 // we initialise them, go through the document once adding to the appropriate Renderers
304 // and then finalise them
305 // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
307 // Prepare the buffers
308 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
310 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
313 // Add objects from Document to buffers
314 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
316 ObjectType type = m_document.m_objects.types[id];
317 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
318 // (Also, Wow I just actually used std::vector::at())
319 // (Also, I just managed to make it throw an exception because I'm a moron)
320 Debug("Object of type %d", type);
323 // Finish the buffers
324 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
326 m_object_renderers[i]->FinaliseBuffers();
328 dynamic_cast<BezierRenderer*>(m_object_renderers[BEZIER])->PrepareBezierGPUBuffer(m_document.m_objects);
329 m_render_dirty = false;