1/*
2 * Copyright (c) 2015 ARM Limited
3 * All rights reserved
4 *
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.
13 *
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.
24 *
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.
36 *
37 * Authors: Andreas Sandberg
38 */
39
40#ifndef __BASE_FRAMEBUFFER_HH__
41#define __BASE_FRAMEBUFFER_HH__
42
43#include <cmath>
44#include <cstdint>
45
46#include <string>
47#include <vector>
48
49#include "base/compiler.hh"
50#include "base/cprintf.hh"
51#include "base/pixel.hh"
52#include "base/str.hh"
53#include "base/types.hh"
54#include "sim/serialize.hh"
55
56/**
57 * Internal gem5 representation of a frame buffer
58 *
59 * Display controllers and other devices producing images are expected
60 * to use this class to represent the final image.
61 *
62 * Pixels are indexed relative to the upper left corner of the
63 * image. That is, the pixel at position (0, 0) is the upper left
64 * corner. The backing store is a linear vector of Pixels ordered left
65 * to right starting in the upper left corner.
66 */
67class FrameBuffer : public Serializable
68{
69  public:
70    /**
71     * Create a frame buffer of a given size.
72     *
73     * @param width Width in pixels
74     * @param height Height in pixels
75     */
76    FrameBuffer(unsigned width, unsigned height);
77    /** Create an empty (0x0) frame buffer */
78    FrameBuffer();
79
80    virtual ~FrameBuffer();
81
82    void serialize(CheckpointOut &cp) const override;
83    void unserialize(CheckpointIn &cp) override;
84
85    /**
86     * Resize the frame buffer.
87     *
88     * This method resizes frame buffer including the backing
89     * store. The contents of the backing store are undefined after
90     * this operation.
91     *
92     * @param with Width in pixels.
93     * @param height Height in pixels.
94     */
95    void resize(unsigned width, unsigned height);
96
97    /** Frame buffer width in pixels */
98    unsigned width() const { return _width; }
99    /** Frame buffer height in pixels */
100    unsigned height() const { return _height; }
101    /** Total number of pixels in frame buffer */
102    unsigned area() const { return _width * _height; }
103
104    /**
105     * Fill the frame buffer with a single pixel value
106     *
107     * @param pixel Pixel value to fill with.
108     */
109    void fill(const Pixel &pixel);
110    /**
111     * Fill the frame buffer with black pixels
112     */
113    void clear();
114
115    /**
116     * Fill the frame buffer with pixel data from an external buffer
117     * of the same width and height as this frame buffer.
118     *
119     * @param fb External frame buffer
120     * @param conv Pixel conversion helper
121     */
122    void copyIn(const uint8_t *fb, const PixelConverter &conv);
123    /**
124     * Fill the frame buffer with pixel data from an external buffer
125     * of the same width and height as this frame buffer.
126     *
127     * @param fb External frame buffer
128     * @param conv Pixel conversion helper
129     */
130    void copyIn(const std::vector<uint8_t> &fb, const PixelConverter &conv) {
131        copyIn(fb.data(), conv);
132    }
133
134    /**
135     * Store the contents of this frame buffer in an external buffer
136     * of the same width and height as this frame buffer.
137     *
138     * @param fb External frame buffer
139     * @param conv Pixel conversion helper
140     */
141    void copyOut(uint8_t *fb, const PixelConverter &conv) const;
142    /**
143     * Store the contents of this frame buffer in an external buffer
144     * of the same width and height as this frame buffer.
145     *
146     * @param fb External frame buffer
147     * @param conv Pixel conversion helper
148     */
149    void copyOut(std::vector<uint8_t> &fb, const PixelConverter &conv) const {
150        copyOut(fb.data(), conv);
151    }
152
153    /**
154     * Get a pixel from an (x, y) coordinate
155     *
156     * @param x Distance from the left margin.
157     * @param y Distance from the top of the frame.
158     */
159    const Pixel &pixel(unsigned x, unsigned y) const {
160        assert(x < _width);
161        assert(y < _height);
162
163        return pixels[y * _width + x];
164    }
165
166    /**
167     * Get a pixel from an (x, y) coordinate
168     *
169     * @param x Distance from the left margin.
170     * @param y Distance from the top of the frame.
171     */
172    Pixel &pixel(unsigned x, unsigned y) {
173        assert(x < _width);
174        assert(y < _height);
175
176        return pixels[y * _width + x];
177    }
178
179    /**
180     * Create a hash of the image that can be used for quick
181     * comparisons.
182     */
183    uint64_t getHash() const;
184
185    /**
186     * Static "dummy" frame buffer.
187     *
188     * This is a dummy frame buffer that can be used as a place holder
189     * for devices that always expect a frame buffer to be present.
190     */
191    static const FrameBuffer dummy;
192
193    /** Frame buffer backing store */
194    std::vector<Pixel> pixels;
195
196  protected:
197    /** Width in pixels */
198    unsigned _width;
199    /** Height in pixels */
200    unsigned _height;
201};
202
203#endif // __BASE_FRAMEBUFFER_HH__
204