framebuffer.cc revision 10907:94d5a1476c5b
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#include "base/framebuffer.hh"
41
42#include <zlib.h>
43
44#include <cassert>
45
46#include "base/bitfield.hh"
47
48const PixelConverter PixelConverter::rgba8888_le(4, 0, 8, 16, 8, 8, 8);
49const PixelConverter PixelConverter::rgba8888_be(4, 0, 8, 16, 8, 8, 8,
50                                                 BigEndianByteOrder);
51const PixelConverter PixelConverter::rgb565_le(2,  0, 5, 11, 5, 6, 5);
52const PixelConverter PixelConverter::rgb565_be(2,  0, 5, 11, 5, 6, 5,
53                                               BigEndianByteOrder);
54
55const FrameBuffer FrameBuffer::dummy(320, 240);
56
57PixelConverter::PixelConverter(unsigned _length,
58                               unsigned ro, unsigned go, unsigned bo,
59                               unsigned rw, unsigned gw, unsigned bw,
60                               ByteOrder _byte_order)
61    : length(_length),
62      depth(rw + gw + bw),
63      byte_order(_byte_order),
64      ch_r(ro, rw),
65      ch_g(go, gw),
66      ch_b(bo, bw)
67{
68    assert(length > 1);
69}
70
71PixelConverter::Channel::Channel(unsigned _offset, unsigned width)
72    : offset(_offset),
73      mask(::mask(width)),
74      factor(255.0 / mask)
75{
76}
77
78uint32_t
79PixelConverter::readWord(const uint8_t *p) const
80{
81    uint32_t word(0);
82
83    if (byte_order == LittleEndianByteOrder) {
84        for (int i = 0; i < length; ++i)
85            word |= p[i] << (8 * i);
86    } else {
87        for (int i = 0; i < length; ++i)
88            word |= p[i] << (8 * (length - i - 1));
89    }
90
91    return word;
92}
93
94void
95PixelConverter::writeWord(uint8_t *p, uint32_t word) const
96{
97    if (byte_order == LittleEndianByteOrder) {
98        for (int i = 0; i < length; ++i)
99            p[i] = (word >> (8 * i)) & 0xFF;
100    } else {
101        for (int i = 0; i < length; ++i)
102            p[i] = (word >> (8 * (length - i - 1))) & 0xFF;
103    }
104}
105
106
107
108
109FrameBuffer::FrameBuffer(unsigned width, unsigned height)
110    : pixels(width * height),
111      _width(width), _height(height)
112{
113    clear();
114}
115
116FrameBuffer::FrameBuffer()
117    : _width(0), _height(0)
118{
119}
120
121FrameBuffer::~FrameBuffer()
122{
123}
124
125
126void
127FrameBuffer::serialize(CheckpointOut &cp) const
128{
129    SERIALIZE_SCALAR(_width);
130    SERIALIZE_SCALAR(_height);
131    SERIALIZE_CONTAINER(pixels);
132}
133
134void
135FrameBuffer::unserialize(CheckpointIn &cp)
136{
137    UNSERIALIZE_SCALAR(_width);
138    UNSERIALIZE_SCALAR(_height);
139    UNSERIALIZE_CONTAINER(pixels);
140}
141
142void
143FrameBuffer::resize(unsigned width, unsigned height)
144{
145    _width = width;
146    _height = height;
147
148    pixels.resize(width * height);
149}
150
151void
152FrameBuffer::fill(const Pixel &pixel)
153{
154    for (auto &p : pixels)
155        p = pixel;
156}
157
158void
159FrameBuffer::clear()
160{
161    static const Pixel black(0, 0, 0);
162
163    fill(black);
164}
165
166void
167FrameBuffer::copyIn(const uint8_t *fb, const PixelConverter &conv)
168{
169    for (auto &p : pixels) {
170        p = conv.toPixel(fb);
171        fb += conv.length;
172    }
173}
174
175void
176FrameBuffer::copyOut(uint8_t *fb, const PixelConverter &conv) const
177{
178    for (auto &p : pixels) {
179        conv.fromPixel(fb, p);
180        fb += conv.length;
181    }
182}
183
184uint64_t
185FrameBuffer::getHash() const
186{
187    return adler32(0UL,
188                   reinterpret_cast<const Bytef *>(pixels.data()),
189                   area() * sizeof(Pixel));
190}
191