2 * Copyright (c) 2015, 2017 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Authors: Andreas Sandberg
40 #ifndef __DEV_PIXELPUMP_HH__
41 #define __DEV_PIXELPUMP_HH__
43 #include "base/framebuffer.hh"
44 #include "sim/clocked_object.hh"
46 struct BasePixelPumpParams;
48 struct DisplayTimings : public Serializable
51 * Create a display timing configuration struct
53 * @param width Width of the visible area of the screen.
54 * @param height Height of the visible area of the screen.
55 * @param hfp Horizontal front porch in pixel clocks.
56 * @param h_sync Horizontal sync in pixel clocks.
57 * @param hbp Horizontal back porch in pixel clocks.
58 * @param vfp Vertical front porch in scan lines.
59 * @param v_sync Vertical sync in scan lines.
60 * @param vbp Vertical back porch in scan lines.
62 DisplayTimings(unsigned width, unsigned height,
63 unsigned hbp, unsigned h_sync, unsigned hfp,
64 unsigned vbp, unsigned v_sync, unsigned vfp);
66 void serialize(CheckpointOut &cp) const override;
67 void unserialize(CheckpointIn &cp) override;
69 /** How many pixel clocks are required for one line? */
70 Cycles cyclesPerLine() const {
71 return Cycles(hSync + hBackPorch + width + hBackPorch);
74 /** How many pixel clocks are required for one frame? */
75 Cycles cyclesPerFrame() const {
76 return Cycles(cyclesPerLine() * linesPerFrame());
79 /** Calculate the first line of the vsync signal */
80 unsigned lineVSyncStart() const {
84 /** Calculate the first line of the vertical back porch */
85 unsigned lineVBackPorchStart() const {
86 return lineVSyncStart() + vSync;
89 /** Calculate the first line of the visible region */
90 unsigned lineFirstVisible() const {
91 return lineVBackPorchStart() + vBackPorch;
94 /** Calculate the first line of the back porch */
95 unsigned lineFrontPorchStart() const {
96 return lineFirstVisible() + height;
99 /** Calculate the total number of lines in a frame */
100 unsigned linesPerFrame() const {
101 return lineFrontPorchStart() + vFrontPorch;
104 /** Display width in pixels */
106 /** Display height in pixels */
109 /** Horizontal back porch in pixels */
111 /** Horizontal front porch in pixels */
112 unsigned hFrontPorch;
113 /** Horizontal sync signal length in pixels */
116 /** Vertical back porch in lines */
118 /** Vertical front porch in lines */
119 unsigned vFrontPorch;
120 /** Vertical sync signal in lines */
123 static const DisplayTimings vga;
127 * Timing generator for a pixel-based display.
129 * Pixels are ordered relative to the top left corner of the
130 * display. Scan lines appear in the following order:
132 * <li>Vertical Sync (starting at line 0)
133 * <li>Vertical back porch
135 * <li>Vertical front porch
138 * Pixel order within a scan line:
140 * <li>Horizontal Sync
141 * <li>Horizontal Back Porch
143 * <li>Horizontal Front Porch
147 : public EventManager, public Clocked,
151 BasePixelPump(EventManager &em, ClockDomain &pxl_clk,
152 unsigned pixel_chunk);
153 virtual ~BasePixelPump();
155 void serialize(CheckpointOut &cp) const override;
156 void unserialize(CheckpointIn &cp) override;
158 public: // Public API
159 /** Update frame size using display timing */
160 void updateTimings(const DisplayTimings &timings);
162 /** Render an entire frame in KVM execution mode */
165 /** Starting pushing pixels in timing mode */
168 /** Immediately stop pushing pixels */
171 /** Get a constant reference of the current display timings */
172 const DisplayTimings &timings() const { return _timings; }
174 /** Is the pixel pump active and refreshing the display? */
175 bool active() const { return evBeginLine.active(); }
177 /** Did a buffer underrun occur within this refresh interval? */
178 bool underrun() const { return _underrun; }
180 /** Is the current line within the visible range? */
181 bool visibleLine() const {
182 return line >= _timings.lineFirstVisible() &&
183 line < _timings.lineFrontPorchStart();
186 /** Current pixel position within the visible area */
187 unsigned posX() const { return _posX; }
189 /** Current pixel position within the visible area */
190 unsigned posY() const {
191 return visibleLine() ? line - _timings.lineFirstVisible() : 0;
194 /** Output frame buffer */
197 protected: // Callbacks
199 * Get the next pixel from the scan line buffer.
201 * @param p Output pixel value, undefined on underrun
202 * @return true on success, false on buffer underrun
204 virtual bool nextPixel(Pixel &p) = 0;
206 /** First pixel clock of the first VSync line. */
207 virtual void onVSyncBegin() {};
210 * Callback on the first pixel of the line after the end VSync
211 * region (typically the first pixel of the vertical back porch).
213 virtual void onVSyncEnd() {};
216 * Start of the HSync region.
218 * @note This is called even for scan lines outside of the visible
221 virtual void onHSyncBegin() {};
224 * Start of the first pixel after the HSync region.
226 * @note This is called even for scan lines outside of the visible
229 virtual void onHSyncEnd() {};
232 * Buffer underrun occurred on a frame.
234 * This method is called once if there is buffer underrun while
235 * refreshing the display. The underrun state is reset on the next
238 * @param x Coordinate within the visible region.
239 * @param y Coordinate within the visible region.
241 virtual void onUnderrun(unsigned x, unsigned y) {};
243 /** Finished displaying the visible region of a frame */
244 virtual void onFrameDone() {};
247 /** Maximum number of pixels to handle per render callback */
248 const unsigned pixelChunk;
252 * Callback helper class with suspend support.
254 * Unlike a normal EventWrapper, this class suspends an event on
255 * drain() and restarts it at drainResume(). The suspend operation
256 * stores the tick relative to curTick() and then deschedules the
257 * event. The resume operation schedules the event at curTick()
258 * plus the relative tick stored when the event was suspended.
260 class PixelEvent : public Event, public Drainable
262 typedef void (BasePixelPump::* CallbackType)();
265 PixelEvent(const char *name, BasePixelPump *parent, CallbackType func);
267 DrainState drain() override;
268 void drainResume() override;
270 void serialize(CheckpointOut &cp) const override;
271 void unserialize(CheckpointIn &cp) override;
273 const std::string name() const override { return _name; }
274 void process() override {
278 bool active() const { return scheduled() || suspended; }
284 const std::string _name;
285 BasePixelPump &parent;
286 const CallbackType func;
295 /** Fast and event-free line rendering function */
298 /** Convenience vector when doing operations on all events */
299 std::vector<PixelEvent *> pixelEvents;
301 PixelEvent evVSyncBegin;
302 PixelEvent evVSyncEnd;
303 PixelEvent evHSyncBegin;
304 PixelEvent evHSyncEnd;
305 PixelEvent evBeginLine;
306 PixelEvent evRenderPixels;
308 DisplayTimings _timings;
311 * Current line (including back porch, front porch, and vsync)
315 /** X-coordinate within the visible region of a frame */
318 /** Did a buffer underrun occur within this refresh interval? */
322 #endif // __DEV_PIXELPUMP_HH__