gpu-compute,mem-ruby: Refactor GPU coalescer
[gem5.git] / src / base / pngwriter.cc
1 /*
2 * Copyright (c) 2017 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
38 /**
39 * @file Definition of a class that writes a frame buffer to a png
40 */
41
42 #include "base/pngwriter.hh"
43
44 extern "C"
45 {
46 #include <png.h>
47 }
48
49 #include <cstdio>
50 #include <cstdlib>
51
52 #include "base/logging.hh"
53
54 const char* PngWriter::_imgExtension = "png";
55
56 /**
57 * Write callback to use with libpng APIs
58 *
59 * @param pngPtr pointer to the png_struct structure
60 * @param data pointer to the data being written
61 * @param length number of bytes being written
62 */
63 static void
64 writePng(png_structp pngPtr, png_bytep data, png_size_t length)
65 {
66 // Here we get our IO pointer back from the write struct
67 // and we cast it into a ostream* type.
68 std::ostream* strmPtr = reinterpret_cast<std::ostream*>(
69 png_get_io_ptr(pngPtr)
70 );
71
72 // Write length bytes to data
73 strmPtr->write(reinterpret_cast<const char *>(data), length);
74 }
75
76 struct PngWriter::PngStructHandle {
77 private:
78 // Make PngStructHandle uncopyable
79 PngStructHandle(const PngStructHandle&) = delete;
80 PngStructHandle& operator=(const PngStructHandle&) = delete;
81 public:
82
83 PngStructHandle() :
84 pngWriteP(NULL), pngInfoP(NULL)
85 {
86 // Creating write structure
87 pngWriteP = png_create_write_struct(
88 PNG_LIBPNG_VER_STRING, NULL, NULL, NULL
89 );
90
91 if (pngWriteP) {
92 // Creating info structure
93 pngInfoP = png_create_info_struct(pngWriteP);
94 }
95 }
96
97 ~PngStructHandle()
98 {
99 if (pngWriteP) {
100 png_destroy_write_struct(&pngWriteP, &pngInfoP);
101 }
102 }
103
104 /** Pointer to PNG Write struct */
105 png_structp pngWriteP;
106
107 /** Pointer to PNG Info struct */
108 png_infop pngInfoP;
109 };
110
111 void
112 PngWriter::write(std::ostream &png) const
113 {
114
115 // Height of the frame buffer
116 unsigned height = fb.height();
117 unsigned width = fb.width();
118
119 // Do not write if frame buffer is empty
120 if (!fb.area()) {
121 png.flush();
122 return;
123 }
124
125 // Initialize Png structures
126 PngStructHandle handle;
127
128 // Png info/write pointers.
129 png_structp pngPtr = handle.pngWriteP;
130 png_infop infoPtr = handle.pngInfoP;
131
132 if (!pngPtr) {
133 warn("Frame buffer dump aborted: Unable to create"
134 "Png Write Struct\n");
135 return;
136 }
137
138 if (!infoPtr) {
139 warn("Frame buffer dump aborted: Unable to create"
140 "Png Info Struct\n");
141 return;
142 }
143
144 // We cannot use default libpng write function since it requires
145 // a file pointer (FILE*), whereas we want to use the ostream.
146 // The following function replaces the write function with a custom
147 // one provided by us (writePng)
148 png_set_write_fn(pngPtr, (png_voidp)&png, writePng, NULL);
149
150 png_set_IHDR(pngPtr, infoPtr, width, height, 8,
151 PNG_COLOR_TYPE_RGB,
152 PNG_INTERLACE_NONE,
153 PNG_COMPRESSION_TYPE_DEFAULT,
154 PNG_FILTER_TYPE_DEFAULT);
155
156 png_write_info(pngPtr, infoPtr);
157
158 // libpng requires an array of pointers to the frame buffer's rows.
159 std::vector<PixelType> rowPacked(width);
160 for (unsigned y=0; y < height; ++y) {
161 for (unsigned x=0; x < width; ++x) {
162 rowPacked[x] = fb.pixel(x, y);
163 }
164
165 png_write_row(pngPtr,
166 reinterpret_cast<png_bytep>(rowPacked.data())
167 );
168 }
169
170 // End of write
171 png_write_end(pngPtr, NULL);
172 }
173