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.
38 #ifndef __DEV_PIXELPUMP_HH__
39 #define __DEV_PIXELPUMP_HH__
41 #include "base/framebuffer.hh"
42 #include "sim/clocked_object.hh"
44 struct BasePixelPumpParams;
46 struct DisplayTimings : public Serializable
49 * Create a display timing configuration struct
51 * @param width Width of the visible area of the screen.
52 * @param height Height of the visible area of the screen.
53 * @param hfp Horizontal front porch in pixel clocks.
54 * @param h_sync Horizontal sync in pixel clocks.
55 * @param hbp Horizontal back porch in pixel clocks.
56 * @param vfp Vertical front porch in scan lines.
57 * @param v_sync Vertical sync in scan lines.
58 * @param vbp Vertical back porch in scan lines.
60 DisplayTimings(unsigned width, unsigned height,
61 unsigned hbp, unsigned h_sync, unsigned hfp,
62 unsigned vbp, unsigned v_sync, unsigned vfp);
64 void serialize(CheckpointOut &cp) const override;
65 void unserialize(CheckpointIn &cp) override;
67 /** How many pixel clocks are required for one line? */
68 Cycles cyclesPerLine() const {
69 return Cycles(hSync + hBackPorch + width + hBackPorch);
72 /** How many pixel clocks are required for one frame? */
73 Cycles cyclesPerFrame() const {
74 return Cycles(cyclesPerLine() * linesPerFrame());
77 /** Calculate the first line of the vsync signal */
78 unsigned lineVSyncStart() const {
82 /** Calculate the first line of the vertical back porch */
83 unsigned lineVBackPorchStart() const {
84 return lineVSyncStart() + vSync;
87 /** Calculate the first line of the visible region */
88 unsigned lineFirstVisible() const {
89 return lineVBackPorchStart() + vBackPorch;
92 /** Calculate the first line of the back porch */
93 unsigned lineFrontPorchStart() const {
94 return lineFirstVisible() + height;
97 /** Calculate the total number of lines in a frame */
98 unsigned linesPerFrame() const {
99 return lineFrontPorchStart() + vFrontPorch;
102 /** Display width in pixels */
104 /** Display height in pixels */
107 /** Horizontal back porch in pixels */
109 /** Horizontal front porch in pixels */
110 unsigned hFrontPorch;
111 /** Horizontal sync signal length in pixels */
114 /** Vertical back porch in lines */
116 /** Vertical front porch in lines */
117 unsigned vFrontPorch;
118 /** Vertical sync signal in lines */
121 static const DisplayTimings vga;
125 * Timing generator for a pixel-based display.
127 * Pixels are ordered relative to the top left corner of the
128 * display. Scan lines appear in the following order:
130 * <li>Vertical Sync (starting at line 0)
131 * <li>Vertical back porch
133 * <li>Vertical front porch
136 * Pixel order within a scan line:
138 * <li>Horizontal Sync
139 * <li>Horizontal Back Porch
141 * <li>Horizontal Front Porch
145 : public EventManager, public Clocked,
149 BasePixelPump(EventManager &em, ClockDomain &pxl_clk,
150 unsigned pixel_chunk);
151 virtual ~BasePixelPump();
153 void serialize(CheckpointOut &cp) const override;
154 void unserialize(CheckpointIn &cp) override;
156 public: // Public API
157 /** Update frame size using display timing */
158 void updateTimings(const DisplayTimings &timings);
160 /** Render an entire frame in KVM execution mode */
163 /** Starting pushing pixels in timing mode */
166 /** Immediately stop pushing pixels */
169 /** Get a constant reference of the current display timings */
170 const DisplayTimings &timings() const { return _timings; }
172 /** Is the pixel pump active and refreshing the display? */
173 bool active() const { return evBeginLine.active(); }
175 /** Did a buffer underrun occur within this refresh interval? */
176 bool underrun() const { return _underrun; }
178 /** Is the current line within the visible range? */
179 bool visibleLine() const {
180 return line >= _timings.lineFirstVisible() &&
181 line < _timings.lineFrontPorchStart();
184 /** Current pixel position within the visible area */
185 unsigned posX() const { return _posX; }
187 /** Current pixel position within the visible area */
188 unsigned posY() const {
189 return visibleLine() ? line - _timings.lineFirstVisible() : 0;
192 /** Output frame buffer */
195 protected: // Callbacks
197 * Get the next pixel from the scan line buffer.
199 * @param p Output pixel value, undefined on underrun
200 * @return true on success, false on buffer underrun
202 virtual bool nextPixel(Pixel &p) = 0;
204 /** First pixel clock of the first VSync line. */
205 virtual void onVSyncBegin() {};
208 * Callback on the first pixel of the line after the end VSync
209 * region (typically the first pixel of the vertical back porch).
211 virtual void onVSyncEnd() {};
214 * Start of the HSync region.
216 * @note This is called even for scan lines outside of the visible
219 virtual void onHSyncBegin() {};
222 * Start of the first pixel after the HSync region.
224 * @note This is called even for scan lines outside of the visible
227 virtual void onHSyncEnd() {};
230 * Buffer underrun occurred on a frame.
232 * This method is called once if there is buffer underrun while
233 * refreshing the display. The underrun state is reset on the next
236 * @param x Coordinate within the visible region.
237 * @param y Coordinate within the visible region.
239 virtual void onUnderrun(unsigned x, unsigned y) {};
241 /** Finished displaying the visible region of a frame */
242 virtual void onFrameDone() {};
245 /** Maximum number of pixels to handle per render callback */
246 const unsigned pixelChunk;
250 * Callback helper class with suspend support.
252 * Unlike a normal EventWrapper, this class suspends an event on
253 * drain() and restarts it at drainResume(). The suspend operation
254 * stores the tick relative to curTick() and then deschedules the
255 * event. The resume operation schedules the event at curTick()
256 * plus the relative tick stored when the event was suspended.
258 class PixelEvent : public Event, public Drainable
260 typedef void (BasePixelPump::* CallbackType)();
263 PixelEvent(const char *name, BasePixelPump *parent, CallbackType func);
265 DrainState drain() override;
266 void drainResume() override;
268 void serialize(CheckpointOut &cp) const override;
269 void unserialize(CheckpointIn &cp) override;
271 const std::string name() const override { return _name; }
272 void process() override {
276 bool active() const { return scheduled() || suspended; }
282 const std::string _name;
283 BasePixelPump &parent;
284 const CallbackType func;
293 /** Fast and event-free line rendering function */
296 /** Convenience vector when doing operations on all events */
297 std::vector<PixelEvent *> pixelEvents;
299 PixelEvent evVSyncBegin;
300 PixelEvent evVSyncEnd;
301 PixelEvent evHSyncBegin;
302 PixelEvent evHSyncEnd;
303 PixelEvent evBeginLine;
304 PixelEvent evRenderPixels;
306 DisplayTimings _timings;
309 * Current line (including back porch, front porch, and vsync)
313 /** X-coordinate within the visible region of a frame */
316 /** Did a buffer underrun occur within this refresh interval? */
320 #endif // __DEV_PIXELPUMP_HH__