framebuffer.cc revision 10839
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 125void 126FrameBuffer::resize(unsigned width, unsigned height) 127{ 128 _width = width; 129 _height = height; 130 131 pixels.resize(width * height); 132} 133 134void 135FrameBuffer::fill(const Pixel &pixel) 136{ 137 for (auto &p : pixels) 138 p = pixel; 139} 140 141void 142FrameBuffer::clear() 143{ 144 static const Pixel black(0, 0, 0); 145 146 fill(black); 147} 148 149void 150FrameBuffer::copyIn(const uint8_t *fb, const PixelConverter &conv) 151{ 152 for (auto &p : pixels) { 153 p = conv.toPixel(fb); 154 fb += conv.length; 155 } 156} 157 158void 159FrameBuffer::copyOut(uint8_t *fb, const PixelConverter &conv) const 160{ 161 for (auto &p : pixels) { 162 conv.fromPixel(fb, p); 163 fb += conv.length; 164 } 165} 166 167uint64_t 168FrameBuffer::getHash() const 169{ 170 return adler32(0UL, 171 reinterpret_cast<const Bytef *>(pixels.data()), 172 area() * sizeof(Pixel)); 173} 174