From 12fb1ca0b5f4ba139889e6005a4aed6d03467864 Mon Sep 17 00:00:00 2001 From: Giacomo Travaglini Date: Thu, 28 Sep 2017 13:01:08 +0100 Subject: base: Introducing utility for writing raw data in png format Originally it was possible to use a Bitmap writer class for dumping a framebuffer snapshot in a .bmp file. This patch enables you to choose another format. In particular it implements the writing of PNG Images using libpng library. The latter has to be already installed in your machine, otherwise gem5 will default to the Bitmap format. This configurable writer has been introduced in the VNC frame dumping mechanism, which is storing changed frame buffers from the VNC server Change-Id: Id7e5763c82235f1ce90381c8486b85a7cce734ce Reviewed-by: Andreas Sandberg Reviewed-on: https://gem5-review.googlesource.com/5181 Reviewed-by: Jason Lowe-Power Maintainer: Andreas Sandberg --- src/base/Graphics.py | 43 ++++++++++++ src/base/SConscript | 6 +- src/base/bitmap.cc | 114 ------------------------------ src/base/bitmap.hh | 126 --------------------------------- src/base/bmpwriter.cc | 113 ++++++++++++++++++++++++++++++ src/base/bmpwriter.hh | 134 +++++++++++++++++++++++++++++++++++ src/base/imgwriter.cc | 72 +++++++++++++++++++ src/base/imgwriter.hh | 91 ++++++++++++++++++++++++ src/base/pngwriter.cc | 175 ++++++++++++++++++++++++++++++++++++++++++++++ src/base/pngwriter.hh | 109 +++++++++++++++++++++++++++++ src/base/vnc/Vnc.py | 5 ++ src/base/vnc/vncinput.cc | 23 +++--- src/base/vnc/vncinput.hh | 9 ++- src/base/vnc/vncserver.cc | 1 - src/base/vnc/vncserver.hh | 1 - 15 files changed, 767 insertions(+), 255 deletions(-) create mode 100644 src/base/Graphics.py delete mode 100644 src/base/bitmap.cc delete mode 100644 src/base/bitmap.hh create mode 100644 src/base/bmpwriter.cc create mode 100644 src/base/bmpwriter.hh create mode 100644 src/base/imgwriter.cc create mode 100644 src/base/imgwriter.hh create mode 100644 src/base/pngwriter.cc create mode 100644 src/base/pngwriter.hh (limited to 'src/base') diff --git a/src/base/Graphics.py b/src/base/Graphics.py new file mode 100644 index 000000000..c42882ea4 --- /dev/null +++ b/src/base/Graphics.py @@ -0,0 +1,43 @@ +# Copyright (c) 2017 ARM Limited +# All rights reserved +# +# The license below extends only to copyright in the software and shall +# not be construed as granting a license to any other intellectual +# property including but not limited to intellectual property relating +# to a hardware implementation of the functionality of the software +# licensed hereunder. You may use the software subject to the license +# terms below provided that you ensure that this notice is replicated +# unmodified and in its entirety in all distributions of the software, +# modified or unmodified, in source code or in binary form. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer; +# redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution; +# neither the name of the copyright holders nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# Authors: Giacomo Travaglini + +from m5.SimObject import SimObject +from m5.params import * + +# Image Formats: +# Auto option will let gem5 to choose the image format it prefers. +class ImageFormat(Enum): vals = ['Auto', 'Bitmap', 'Png'] diff --git a/src/base/SConscript b/src/base/SConscript index 1ca0e35bc..7aa93f504 100644 --- a/src/base/SConscript +++ b/src/base/SConscript @@ -33,15 +33,19 @@ Import('*') if env['CP_ANNOTATE']: SimObject('CPA.py') Source('cp_annotate.cc') +SimObject('Graphics.py') Source('atomicio.cc') Source('bitfield.cc') Source('bigint.cc') -Source('bitmap.cc') +Source('imgwriter.cc') +Source('bmpwriter.cc') Source('callback.cc') Source('cprintf.cc') Source('debug.cc') if env['USE_FENV']: Source('fenv.c') +if env['USE_PNG']: + Source('pngwriter.cc') Source('framebuffer.cc') Source('hostinfo.cc') Source('inet.cc') diff --git a/src/base/bitmap.cc b/src/base/bitmap.cc deleted file mode 100644 index 0052503a4..000000000 --- a/src/base/bitmap.cc +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2010, 2015 ARM Limited - * All rights reserved - * - * The license below extends only to copyright in the software and shall - * not be construed as granting a license to any other intellectual - * property including but not limited to intellectual property relating - * to a hardware implementation of the functionality of the software - * licensed hereunder. You may use the software subject to the license - * terms below provided that you ensure that this notice is replicated - * unmodified and in its entirety in all distributions of the software, - * modified or unmodified, in source code or in binary form. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer; - * redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution; - * neither the name of the copyright holders nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Authors: William Wang - * Ali Saidi - * Chris Emmons - * Andreas Sandberg - */ - -#include "base/bitmap.hh" - -#include - -#include "base/misc.hh" - -// bitmap class ctor -Bitmap::Bitmap(const FrameBuffer *_fb) - : fb(*_fb) -{ -} - -Bitmap::~Bitmap() -{ -} - -const Bitmap::CompleteV1Header -Bitmap::getCompleteHeader() const -{ - const uint32_t pixel_array_size(sizeof(PixelType) * fb.area()); - const uint32_t file_size(sizeof(CompleteV1Header) + pixel_array_size); - - const CompleteV1Header header = { - // File header - { - {'B','M'}, /* Magic */ - file_size, - 0, 0, /* Reserved */ - sizeof(CompleteV1Header) /* Offset to pixel array */ - }, - // Info/DIB header - { - sizeof(InfoHeaderV1), - fb.width(), - fb.height(), - 1, /* Color planes */ - 32, /* Bits per pixel */ - 0, /* No compression */ - pixel_array_size, /* Image size in bytes */ - 2835, /* x pixels per meter (assume 72 DPI) */ - 2835, /* y pixels per meter (assume 72 DPI) */ - 0, /* Colors in color table */ - 0 /* Important color count (0 == all are important) */ - } - }; - - return header; -} - -void -Bitmap::write(std::ostream &bmp) const -{ - const CompleteV1Header header(getCompleteHeader()); - - // 1. write the header - bmp.write(reinterpret_cast(&header), sizeof(header)); - - // 2. write the bitmap data - // BMP start store data left to right starting with the bottom row - // so we need to do some creative flipping - std::vector line_buffer(fb.width()); - for (int y = 0; y < fb.height(); ++y) { - for (unsigned x = 0; x < fb.width(); ++x) - line_buffer[x] = fb.pixel(x, fb.height() - y - 1); - - bmp.write(reinterpret_cast(line_buffer.data()), - line_buffer.size() * sizeof(line_buffer[0])); - } - - bmp.flush(); -} - diff --git a/src/base/bitmap.hh b/src/base/bitmap.hh deleted file mode 100644 index 0797a26a7..000000000 --- a/src/base/bitmap.hh +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (c) 2010, 2015 ARM Limited - * All rights reserved - * - * The license below extends only to copyright in the software and shall - * not be construed as granting a license to any other intellectual - * property including but not limited to intellectual property relating - * to a hardware implementation of the functionality of the software - * licensed hereunder. You may use the software subject to the license - * terms below provided that you ensure that this notice is replicated - * unmodified and in its entirety in all distributions of the software, - * modified or unmodified, in source code or in binary form. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer; - * redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution; - * neither the name of the copyright holders nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Authors: William Wang - * Ali Saidi - * Chris Emmons - * Andreas Sandberg - */ -#ifndef __BASE_BITMAP_HH__ -#define __BASE_BITMAP_HH__ - -#include - -#include "base/compiler.hh" -#include "base/framebuffer.hh" - -/** - * @file Declaration of a class that writes a frame buffer to a bitmap - */ - - -// write frame buffer into a bitmap picture -class Bitmap -{ - public: - /** - * Create a bitmap that takes data in a given mode & size and - * outputs to an ostream. - */ - Bitmap(const FrameBuffer *fb); - - ~Bitmap(); - - /** - * Write the frame buffer data into the provided ostream - * - * @param bmp stream to write to - */ - void write(std::ostream &bmp) const; - - - private: - struct FileHeader { - unsigned char magic_number[2]; - uint32_t size; - uint16_t reserved1; - uint16_t reserved2; - uint32_t offset; - } M5_ATTR_PACKED; - - struct InfoHeaderV1 { /* Aka DIB header */ - uint32_t Size; - uint32_t Width; - uint32_t Height; - uint16_t Planes; - uint16_t BitCount; - uint32_t Compression; - uint32_t SizeImage; - uint32_t XPelsPerMeter; - uint32_t YPelsPerMeter; - uint32_t ClrUsed; - uint32_t ClrImportant; - } M5_ATTR_PACKED; - - struct CompleteV1Header { - FileHeader file; - InfoHeaderV1 info; - } M5_ATTR_PACKED; - - struct BmpPixel32 { - BmpPixel32 &operator=(const Pixel &rhs) { - red = rhs.red; - green = rhs.green; - blue = rhs.blue; - padding = 0; - - return *this; - } - uint8_t blue; - uint8_t green; - uint8_t red; - uint8_t padding; - } M5_ATTR_PACKED; - - typedef BmpPixel32 PixelType; - - const CompleteV1Header getCompleteHeader() const; - - const FrameBuffer &fb; -}; - -#endif // __BASE_BITMAP_HH__ - diff --git a/src/base/bmpwriter.cc b/src/base/bmpwriter.cc new file mode 100644 index 000000000..f650074ab --- /dev/null +++ b/src/base/bmpwriter.cc @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2010, 2015, 2017 ARM Limited + * All rights reserved + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: William Wang + * Ali Saidi + * Chris Emmons + * Andreas Sandberg + */ + +#include "base/bmpwriter.hh" + +#include + +#include "base/misc.hh" + +const char* BmpWriter::_imgExtension = "bmp"; + +// bitmap class ctor +BmpWriter::BmpWriter(const FrameBuffer *_fb) + : ImgWriter(_fb) +{ +} + +const BmpWriter::CompleteV1Header +BmpWriter::getCompleteHeader() const +{ + const uint32_t pixel_array_size(sizeof(PixelType) * fb.area()); + const uint32_t file_size(sizeof(CompleteV1Header) + pixel_array_size); + + const CompleteV1Header header = { + // File header + { + {'B','M'}, /* Magic */ + file_size, + 0, 0, /* Reserved */ + sizeof(CompleteV1Header) /* Offset to pixel array */ + }, + // Info/DIB header + { + sizeof(InfoHeaderV1), + fb.width(), + fb.height(), + 1, /* Color planes */ + 32, /* Bits per pixel */ + 0, /* No compression */ + pixel_array_size, /* Image size in bytes */ + 2835, /* x pixels per meter (assume 72 DPI) */ + 2835, /* y pixels per meter (assume 72 DPI) */ + 0, /* Colors in color table */ + 0 /* Important color count (0 == all are important) */ + } + }; + + return header; +} + +void +BmpWriter::write(std::ostream &bmp) const +{ + const CompleteV1Header header(getCompleteHeader()); + + // 1. write the header + bmp.write(reinterpret_cast(&header), sizeof(header)); + + // 2. write the bitmap data + // BMP start store data left to right starting with the bottom row + // so we need to do some creative flipping + std::vector line_buffer(fb.width()); + for (int y = 0; y < fb.height(); ++y) { + for (unsigned x = 0; x < fb.width(); ++x) + line_buffer[x] = fb.pixel(x, fb.height() - y - 1); + + bmp.write(reinterpret_cast(line_buffer.data()), + line_buffer.size() * sizeof(line_buffer[0])); + } + + bmp.flush(); +} + + diff --git a/src/base/bmpwriter.hh b/src/base/bmpwriter.hh new file mode 100644 index 000000000..f78b994fd --- /dev/null +++ b/src/base/bmpwriter.hh @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2010, 2015, 2017 ARM Limited + * All rights reserved + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: William Wang + * Ali Saidi + * Chris Emmons + * Andreas Sandberg + */ +#ifndef __BASE_BITMAP_HH__ +#define __BASE_BITMAP_HH__ + +#include + +#include "base/compiler.hh" +#include "base/framebuffer.hh" +#include "base/imgwriter.hh" + +/** + * @file Declaration of a class that writes a frame buffer to a bitmap + */ + +// write frame buffer into a bitmap picture +class BmpWriter : public ImgWriter +{ + public: + /** + * Create a bitmap that takes data in a given mode & size and + * outputs to an ostream. + */ + BmpWriter(const FrameBuffer *fb); + + ~BmpWriter() {}; + + /* + * Return Image format as a string + * + * @return img extension (e.g. bmp for Bitmap) + */ + const char* getImgExtension() const override + { return _imgExtension; } + + /** + * Write the frame buffer data into the provided ostream + * + * @param bmp stream to write to + */ + void write(std::ostream &bmp) const override; + + private: + struct FileHeader { + unsigned char magic_number[2]; + uint32_t size; + uint16_t reserved1; + uint16_t reserved2; + uint32_t offset; + } M5_ATTR_PACKED; + + struct InfoHeaderV1 { /* Aka DIB header */ + uint32_t Size; + uint32_t Width; + uint32_t Height; + uint16_t Planes; + uint16_t BitCount; + uint32_t Compression; + uint32_t SizeImage; + uint32_t XPelsPerMeter; + uint32_t YPelsPerMeter; + uint32_t ClrUsed; + uint32_t ClrImportant; + } M5_ATTR_PACKED; + + struct CompleteV1Header { + FileHeader file; + InfoHeaderV1 info; + } M5_ATTR_PACKED; + + struct BmpPixel32 { + BmpPixel32 &operator=(const Pixel &rhs) { + red = rhs.red; + green = rhs.green; + blue = rhs.blue; + padding = 0; + + return *this; + } + uint8_t blue; + uint8_t green; + uint8_t red; + uint8_t padding; + } M5_ATTR_PACKED; + + typedef BmpPixel32 PixelType; + + static const char* _imgExtension; + + const CompleteV1Header getCompleteHeader() const; +}; + + +#endif // __BASE_BITMAP_HH__ + diff --git a/src/base/imgwriter.cc b/src/base/imgwriter.cc new file mode 100644 index 000000000..ba9b58c02 --- /dev/null +++ b/src/base/imgwriter.cc @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2017 ARM Limited + * All rights reserved + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Giacomo Travaglini + */ + +#include "base/imgwriter.hh" +#include "base/bmpwriter.hh" +#include "base/misc.hh" + +#include "config/use_png.hh" + +#if USE_PNG +#include "base/pngwriter.hh" +#endif + +std::unique_ptr +createImgWriter(Enums::ImageFormat type, const FrameBuffer *fb) +{ + switch (type) { + case Enums::Auto: + // The Auto option allows gem5 to choose automatically the + // writer type, and it will choose for the best fit in + // performance. + // gem5 will try PNG first, and it will fallback to BMP if not + // available. + + /* FALLTHROUGH */ +#if USE_PNG + case Enums::Png: + return std::unique_ptr(new PngWriter(fb)); +#endif + case Enums::Bitmap: + return std::unique_ptr(new BmpWriter(fb)); + default: + warn("Invalid Image Type specified, defaulting to Bitmap\n"); + return std::unique_ptr(new BmpWriter(fb)); + } +} diff --git a/src/base/imgwriter.hh b/src/base/imgwriter.hh new file mode 100644 index 000000000..8bfb92064 --- /dev/null +++ b/src/base/imgwriter.hh @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2017 ARM Limited + * All rights reserved + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Giacomo Travaglini + */ +#ifndef __BASE_IMGWRITER_HH__ +#define __BASE_IMGWRITER_HH__ + +#include + +#include "base/compiler.hh" +#include "base/framebuffer.hh" + +#include "enums/ImageFormat.hh" + +// write frame buffer to an image +class ImgWriter +{ + public: + ImgWriter(const FrameBuffer *_fb) + : fb(*_fb) + {} + + virtual ~ImgWriter() {}; + /** + * Write the frame buffer data into the provided ostream + * + * @param out output stream to write to + */ + virtual void write(std::ostream &out) const = 0; + /* + * Return Image format as a string + * + * @return img extension (e.g. bmp for Bitmap) + */ + virtual const char* getImgExtension() const = 0; + + protected: + const FrameBuffer &fb; +}; + +/** + * Factory Function which allocates a ImgWriter object and returns + * a smart pointer to it. The dynamic type of the object being pointed + * depends upon the enum type passed as a first parameter. + * If the enum contains an invalid value, the function will produce a warning + * and will default to Bitamp. + * + * @param type Image writer type (e.g. Bitamp, Png) + * @param fb Pointer to a FrameBuffer object + * This contains the raw data which will be stored as an image + * when calling the appropriate object method + * @return smart pointer to the allocated Image Writer + */ +std::unique_ptr +createImgWriter(Enums::ImageFormat type, const FrameBuffer *fb); + +#endif //__BASE_IMGWRITER_HH__ diff --git a/src/base/pngwriter.cc b/src/base/pngwriter.cc new file mode 100644 index 000000000..3b49d479d --- /dev/null +++ b/src/base/pngwriter.cc @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2017 ARM Limited + * All rights reserved + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Giacomo Travaglini + */ + +/** + * @file Definition of a class that writes a frame buffer to a png + */ + +#include "base/pngwriter.hh" + +extern "C" +{ +#include +} + +#include +#include + +#include "base/misc.hh" + +const char* PngWriter::_imgExtension = "png"; + +/** + * Write callback to use with libpng APIs + * + * @param pngPtr pointer to the png_struct structure + * @param data pointer to the data being written + * @param length number of bytes being written + */ +static void +writePng(png_structp pngPtr, png_bytep data, png_size_t length) +{ + // Here we get our IO pointer back from the write struct + // and we cast it into a ostream* type. + std::ostream* strmPtr = reinterpret_cast( + png_get_io_ptr(pngPtr) + ); + + // Write length bytes to data + strmPtr->write(reinterpret_cast(data), length); +} + +struct PngWriter::PngStructHandle { + private: + // Make PngStructHandle uncopyable + PngStructHandle(const PngStructHandle&) = delete; + PngStructHandle& operator=(const PngStructHandle&) = delete; + public: + + PngStructHandle() : + pngWriteP(NULL), pngInfoP(NULL) + { + // Creating write structure + pngWriteP = png_create_write_struct( + PNG_LIBPNG_VER_STRING, NULL, NULL, NULL + ); + + if (pngWriteP) { + // Creating info structure + pngInfoP = png_create_info_struct(pngWriteP); + } + } + + ~PngStructHandle() + { + if (pngWriteP) { + png_destroy_write_struct(&pngWriteP, &pngInfoP); + } + } + + /** Pointer to PNG Write struct */ + png_structp pngWriteP; + + /** Pointer to PNG Info struct */ + png_infop pngInfoP; +}; + +void +PngWriter::write(std::ostream &png) const +{ + + // Height of the frame buffer + unsigned height = fb.height(); + unsigned width = fb.width(); + + // Do not write if frame buffer is empty + if (!fb.area()) { + png.flush(); + return; + } + + // Initialize Png structures + PngStructHandle handle; + + // Png info/write pointers. + png_structp pngPtr = handle.pngWriteP; + png_infop infoPtr = handle.pngInfoP; + + if (!pngPtr) { + warn("Frame buffer dump aborted: Unable to create" + "Png Write Struct\n"); + return; + } + + if (!infoPtr) { + warn("Frame buffer dump aborted: Unable to create" + "Png Info Struct\n"); + return; + } + + // We cannot use default libpng write function since it requires + // a file pointer (FILE*), whereas we want to use the ostream. + // The following function replaces the write function with a custom + // one provided by us (writePng) + png_set_write_fn(pngPtr, (png_voidp)&png, writePng, NULL); + + png_set_IHDR(pngPtr, infoPtr, width, height, 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(pngPtr, infoPtr); + + // libpng requires an array of pointers to the frame buffer's rows. + std::vector rowPacked(width); + for (unsigned y=0; y < height; ++y) { + for (unsigned x=0; x < height; ++x) { + rowPacked[x] = fb.pixel(x, y); + } + + png_write_row(pngPtr, + reinterpret_cast(rowPacked.data()) + ); + } + + // End of write + png_write_end(pngPtr, NULL); +} + diff --git a/src/base/pngwriter.hh b/src/base/pngwriter.hh new file mode 100644 index 000000000..984f2f62f --- /dev/null +++ b/src/base/pngwriter.hh @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2017 ARM Limited + * All rights reserved + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Giacomo Travaglini + */ + +/** + * @file Declaration of a class that writes a frame buffer to a png + */ + +#ifndef __BASE_PNG_HH__ +#define __BASE_PNG_HH__ + +#include "base/compiler.hh" +#include "base/framebuffer.hh" +#include "base/imgwriter.hh" + +/** Image writer implementing support for PNG */ +class PngWriter : public ImgWriter +{ + public: + /** + * Create a png that takes data in a given mode & size and + * outputs to an ostream. + */ + PngWriter(const FrameBuffer *_fb) + : ImgWriter(_fb) + {} + + ~PngWriter() {}; + + /** + * Return Image format as a string + * + * @return img extension (e.g. .png for Png) + */ + const char* getImgExtension() const override + { return _imgExtension; } + + /** + * Write the frame buffer data into the provided ostream + * + * @param png stream to write to + */ + void write(std::ostream &png) const override; + private: + /** Png Pixel type: not containing padding */ + struct PngPixel24 { + PngPixel24 &operator=(const Pixel &rhs) { + red = rhs.red; + green = rhs.green; + blue = rhs.blue; + + return *this; + } + uint8_t red; + uint8_t green; + uint8_t blue; + } M5_ATTR_PACKED; + + /** + * Handle to resources used by libpng: + * - png_struct: Structure holding write informations + * - png_info : Structure holding image informations + * + * The class is automatically taking care of struct + * allocation/deallocation + */ + struct PngStructHandle; + + typedef PngPixel24 PixelType; + + static const char* _imgExtension; +}; + +#endif // __BASE_PNG_HH__ diff --git a/src/base/vnc/Vnc.py b/src/base/vnc/Vnc.py index a7faefb41..0aed0dc84 100644 --- a/src/base/vnc/Vnc.py +++ b/src/base/vnc/Vnc.py @@ -37,11 +37,16 @@ from m5.SimObject import SimObject from m5.params import * +from Graphics import * + class VncInput(SimObject): type = 'VncInput' cxx_header = "base/vnc/vncinput.hh" frame_capture = Param.Bool(False, "capture changed frames to files") + img_format = Param.ImageFormat( + "Bitmap", "Format of the dumped Framebuffer" + ) class VncServer(VncInput): type = 'VncServer' diff --git a/src/base/vnc/vncinput.cc b/src/base/vnc/vncinput.cc index 541b77143..b9d1d2546 100644 --- a/src/base/vnc/vncinput.cc +++ b/src/base/vnc/vncinput.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2015 ARM Limited + * Copyright (c) 2010, 2015, 2017 ARM Limited * All rights reserved * * The license below extends only to copyright in the software and shall @@ -48,6 +48,7 @@ #include "base/misc.hh" #include "base/output.hh" + #include "base/trace.hh" #include "debug/VNC.hh" @@ -58,7 +59,8 @@ VncInput::VncInput(const Params *p) fb(&FrameBuffer::dummy), _videoWidth(fb->width()), _videoHeight(fb->height()), captureEnabled(p->frame_capture), - captureCurrentFrame(0), captureLastHash(0) + captureCurrentFrame(0), captureLastHash(0), + imgFormat(p->img_format) { if (captureEnabled) { // remove existing frame output directory if it exists, then create a @@ -78,9 +80,11 @@ VncInput::setFrameBuffer(const FrameBuffer *rfb) fb = rfb; - // create bitmap of the frame with new attributes - if (captureEnabled) - captureBitmap.reset(new Bitmap(rfb)); + // Create the Image Writer object in charge of dumping + // the frame buffer raw data into a file in a specific format. + if (captureEnabled) { + captureImage = createImgWriter(imgFormat, rfb); + } // Setting a new frame buffer means that we need to send an update // to the client. Mark the internal buffers as dirty to do so. @@ -110,7 +114,7 @@ VncInput::setDirty() void VncInput::captureFrameBuffer() { - assert(captureBitmap); + assert(captureImage); // skip identical frames uint64_t new_hash = fb->getHash(); @@ -120,13 +124,14 @@ VncInput::captureFrameBuffer() // get the filename for the current frame char frameFilenameBuffer[64]; - snprintf(frameFilenameBuffer, 64, "fb.%06d.%lld.bmp.gz", - captureCurrentFrame, static_cast(curTick())); + snprintf(frameFilenameBuffer, 64, "fb.%06d.%lld.%s.gz", + captureCurrentFrame, static_cast(curTick()), + captureImage->getImgExtension()); const string frameFilename(frameFilenameBuffer); // create the compressed framebuffer file OutputStream *fb_out(captureOutputDirectory->create(frameFilename, true)); - captureBitmap->write(*fb_out->stream()); + captureImage->write(*fb_out->stream()); captureOutputDirectory->close(fb_out); ++captureCurrentFrame; diff --git a/src/base/vnc/vncinput.hh b/src/base/vnc/vncinput.hh index 15ddc5c58..38f42459f 100644 --- a/src/base/vnc/vncinput.hh +++ b/src/base/vnc/vncinput.hh @@ -48,7 +48,7 @@ #include #include -#include "base/bitmap.hh" +#include "base/imgwriter.hh" #include "params/VncInput.hh" #include "sim/sim_object.hh" @@ -226,8 +226,11 @@ class VncInput : public SimObject /** Computed hash of the last captured frame */ uint64_t captureLastHash; - /** Cached bitmap object for writing out frame buffers to file */ - std::unique_ptr captureBitmap; + /** Cached ImgWriter object for writing out frame buffers to file */ + std::unique_ptr captureImage; + + /** image format */ + Enums::ImageFormat imgFormat; /** Captures the current frame buffer to a file */ void captureFrameBuffer(); diff --git a/src/base/vnc/vncserver.cc b/src/base/vnc/vncserver.cc index 9cf38dc2d..00ff5602f 100644 --- a/src/base/vnc/vncserver.cc +++ b/src/base/vnc/vncserver.cc @@ -64,7 +64,6 @@ #include #include "base/atomicio.hh" -#include "base/bitmap.hh" #include "base/misc.hh" #include "base/output.hh" #include "base/socket.hh" diff --git a/src/base/vnc/vncserver.hh b/src/base/vnc/vncserver.hh index 99f4b5fe1..f64ccd7cd 100644 --- a/src/base/vnc/vncserver.hh +++ b/src/base/vnc/vncserver.hh @@ -48,7 +48,6 @@ #include #include "base/vnc/vncinput.hh" -#include "base/bitmap.hh" #include "base/circlebuf.hh" #include "base/pollevent.hh" #include "base/socket.hh" -- cgit v1.2.3