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
39 * Frees memory used by ObjectRenderers
43 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
45 delete m_object_renderers[i]; // delete's match new's in constructor
47 m_object_renderers.clear();
48 delete [] m_cpu_rendering_pixels;
53 * @param x, y - Amount to translate
55 void View::Translate(Real x, Real y)
61 Debug("View Bounds => %s", m_bounds.Str().c_str());
62 if (!m_use_gpu_transform)
63 m_buffer_dirty = true;
64 m_bounds_dirty = true;
68 * Scale the View at a point
69 * @param x, y - Coordinates to scale at (eg: Mouse cursor position)
70 * @param scale_amount - Amount to scale by
72 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
74 // x and y are coordinates in the window
75 // Convert to local coords.
81 Real top = y - m_bounds.y;
82 Real left = x - m_bounds.x;
87 m_bounds.x = x - left;
89 m_bounds.w *= scale_amount;
90 m_bounds.h *= scale_amount;
91 Debug("View Bounds => %s", m_bounds.Str().c_str());
92 if (!m_use_gpu_transform)
93 m_buffer_dirty = true;
94 m_bounds_dirty = true;
98 * Transform a point in the document to a point relative to the top left corner of the view
99 * This is the CPU coordinate transform code; used only if the CPU is doing coordinate transforms
100 * @param inp - Input Rect {x,y,w,h} in the document
101 * @returns output Rect {x,y,w,h} in the View
103 Rect View::TransformToViewCoords(const Rect& inp) const
106 out.x = (inp.x - m_bounds.x) / m_bounds.w;
107 out.y = (inp.y - m_bounds.y) / m_bounds.h;
108 out.w = inp.w / m_bounds.w;
109 out.h = inp.h / m_bounds.h;
115 * Updates FrameBuffer if the document, object bounds, or view bounds have changed, then Blits it
116 * Otherwise just Blits the cached FrameBuffer
117 * @param width - Width of View to render
118 * @param height - Height of View to render
120 void View::Render(int width, int height)
122 // View dimensions have changed (ie: Window was resized)
123 int prev_width = m_cached_display.GetWidth();
124 int prev_height = m_cached_display.GetHeight();
125 if (width != prev_width || height != prev_height)
127 m_cached_display.Create(width, height);
128 m_bounds_dirty = true;
131 // View bounds have not changed; blit the FrameBuffer as it is
134 m_cached_display.UnBind();
135 m_cached_display.Blit();
139 // Bind FrameBuffer for rendering, and clear it
142 if (m_render_dirty) // document has changed
145 if (m_buffer_dirty) // object bounds have changed
146 UpdateObjBoundsVBO();
148 if (m_use_gpu_transform)
150 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))};
151 m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
155 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f};
156 m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
158 m_bounds_dirty = false;
160 m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
161 m_cached_display.Clear();
165 if (m_use_gpu_rendering)
167 if (m_colour.a < 1.0f)
170 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
172 m_objbounds_vbo.Bind();
174 glEnableVertexAttribArray(0);
175 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
177 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
179 m_object_renderers[i]->RenderUsingGPU();
182 glDisableVertexAttribArray(0);
183 if (m_colour.a < 1.0f)
188 else // Rasterise on CPU then blit texture to GPU
190 // Dynamically resize CPU rendering target pixels if needed
191 if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
193 delete [] m_cpu_rendering_pixels;
194 m_cpu_rendering_pixels = new uint8_t[width*height*4];
195 if (m_cpu_rendering_pixels == NULL)
196 Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
198 // Clear CPU rendering pixels
199 for (int i = 0; i < width*height*4; ++i)
200 m_cpu_rendering_pixels[i] = 255;
202 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
204 m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height});
206 m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
207 // Debug for great victory (do something similar for GPU and compare?)
208 ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
210 m_cached_display.UnBind(); // resets render target to the screen
211 m_cached_display.Blit(); // blit FrameBuffer to screen
214 void View::UpdateObjBoundsVBO()
216 m_objbounds_vbo.Invalidate();
217 m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
218 if (m_use_gpu_transform)
220 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
224 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
226 m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
228 BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.Map(false, true, true), m_objbounds_vbo.GetSize());
230 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
233 if (m_use_gpu_transform)
235 obj_bounds = m_document.m_objects.bounds[id];
239 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
241 GPUObjBounds gpu_bounds = {
242 (float)Float(obj_bounds.x),
243 (float)Float(obj_bounds.y),
244 (float)Float(obj_bounds.x + obj_bounds.w),
245 (float)Float(obj_bounds.y + obj_bounds.h)
247 obj_bounds_builder.Add(gpu_bounds);
250 m_objbounds_vbo.UnMap();
251 m_buffer_dirty = false;
254 * Prepare the document for rendering
255 * Will be called on View::Render if m_render_dirty is set
256 * (Called at least once, on the first Render)
258 void View::PrepareRender()
260 Debug("Recreate buffers with %u objects", m_document.ObjectCount());
261 // Prepare bounds vbo
262 m_bounds_ubo.Invalidate();
263 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
264 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
266 // Instead of having each ObjectRenderer go through the whole document
267 // we initialise them, go through the document once adding to the appropriate Renderers
268 // and then finalise them
269 // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
271 // Prepare the buffers
272 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
274 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
277 // Add objects from Document to buffers
278 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
280 ObjectType type = m_document.m_objects.types[id];
281 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
282 // (Also, Wow I just actually used std::vector::at())
283 // (Also, I just managed to make it throw an exception because I'm a moron)
284 Debug("Object of type %d", type);
287 // Finish the buffers
288 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
290 m_object_renderers[i]->FinaliseBuffers();
292 m_render_dirty = false;