~thomas-voss/glmark2/build-for-mir

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright © 2008 Ben Smith
 * Copyright © 2010-2011 Linaro Limited
 *
 * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
 *
 * glmark2 is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * glmark2.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *  Ben Smith (original glmark benchmark)
 *  Alexandros Frantzis (glmark2)
 *  Jesse Barker
 */
#ifndef GLMARK2_CANVAS_H_
#define GLMARK2_CANVAS_H_

#include "gl-headers.h"
#include "mat.h"
#include "gl-visual-config.h"

#include <stdint.h>
#include <string>
#include <stdio.h>
#include <cmath>

/**
 * Abstraction for a GL rendering target.
 */
class Canvas
{
public:
    virtual ~Canvas() {}

    /**
     * A pixel value.
     */
    struct Pixel {
        Pixel():
            r(0), g(0), b(0), a(0) {}
        Pixel(uint8_t r, uint8_t g, uint8_t b, uint8_t a):
            r(r), g(g), b(b), a(a) {}
        /**
         * Gets the pixel value as a 32-bit integer in 0xAABBGGRR format.
         *
         * @return the pixel value
         */
        uint32_t to_le32()
        {
            return static_cast<uint32_t>(r) +
                   (static_cast<uint32_t>(g) << 8) +
                   (static_cast<uint32_t>(b) << 16) +
                   (static_cast<uint32_t>(a) << 24);

        }

        /**
         * Gets the euclidian distance from this pixel in 3D RGB space.
         *
         * @param p the pixel to get the distance from
         *
         * @return the euclidian distance
         */
        double distance_rgb(const Canvas::Pixel &p)
        {
            // These work without casts because of integer promotion rules
            // (the uint8_ts are promoted to ints)
            double d = (r - p.r) * (r - p.r) +
                       (g - p.g) * (g - p.g) +
                       (b - p.b) * (b - p.b);
            return std::sqrt(d);
        }

        uint8_t r;
        uint8_t g;
        uint8_t b;
        uint8_t a;
    };


    /**
     * Initializes the canvas and makes it the target of GL operations.
     *
     * This method should be implemented in derived classes.
     *
     * @return whether initialization succeeded
     */
    virtual bool init() { return false; }

    /**
     * Resets the canvas, destroying and recreating resources to give each new
     * test scenario a fresh context for rendering.
     *
     * This method should be implemented in derived classes.
     *
     * @return whether reset succeeded
     */
    virtual bool reset() { return false; }

    /**
     * Changes the visibility of the canvas.
     *
     * The canvas is initially not visible.
     *
     * This method should be implemented in derived classes.
     *
     * @param visible true to make the Canvas visible, false otherwise
     */
    virtual void visible(bool visible) { static_cast<void>(visible); }

    /**
     * Clears the canvas.
     * This method should be implemented in derived classes.
     */
    virtual void clear() {}

    /**
     * Ensures that the canvas on-screen representation gets updated
     * with the latest canvas contents.
     *
     * This method should be implemented in derived classes.
     */
    virtual void update() {}

    /**
     * Prints information about the canvas.
     *
     * This method should be implemented in derived classes.
     */
    virtual void print_info() {}

    /**
     * Reads a pixel from the canvas.
     *
     * The (0, 0) point is the lower left corner. The X and Y coordinates
     * increase towards the right and top, respectively.
     *
     * This method should be implemented in derived classes.
     *
     * @param x the X coordinate
     * @param y the Y coordinate
     *
     * @return the pixel
     */
    virtual Pixel read_pixel(int x, int y)
    {
        static_cast<void>(x);
        static_cast<void>(y);
        return Pixel();
    }

    /**
     * Writes the canvas contents to a file.
     *
     * The pixel save order is  upper left to lower right. Each pixel value
     * is stored as four consecutive bytes R,G,B,A.
     *
     * This method should be implemented in derived classes.
     *
     * @param filename the name of the file to write to
     */
    virtual void write_to_file(std::string &filename) { static_cast<void>(filename); }

    /**
     * Whether we should quit the application.
     *
     * This method should be implemented in derived classes.
     *
     * @return true if we should quit, false otherwise
     */
    virtual bool should_quit() { return false; }

    /**
     * Resizes the canvas.
     *
     * This method should be implemented in derived classes.
     *
     * @param width the new width in pixels
     * @param height the new height in pixels
     *
     * @return true if we should quit, false otherwise
     */
    virtual void resize(int width, int height) { static_cast<void>(width); static_cast<void>(height); }

    /**
     * Gets the FBO associated with the canvas.
     *
     * @return the FBO
     */
    virtual unsigned int fbo() { return 0; }

    /**
     * Gets a dummy canvas object.
     *
     * @return the dummy canvas
     */
    static Canvas &dummy()
    {
        static Canvas dummy_canvas(0, 0);
        return dummy_canvas;
    }

    /**
     * Gets the width of the canvas.
     *
     * @return the width in pixels
     */
    int width() { return width_; }

    /**
     * Gets the height of the canvas.
     *
     * @return the height in pixels
     */
    int height() { return height_; }

    /**
     * Gets the projection matrix recommended for use with the canvas.
     *
     * It's not mandatory to use this projection matrix.
     *
     * @return the projection matrix
     */
    const LibMatrix::mat4 &projection() { return projection_; }

    /**
     * Sets whether the canvas should be backed by an off-screen surface.
     *
     * This takes effect after the next init()/reset().
     */
    void offscreen(bool offscreen) { offscreen_ = offscreen; }

    /**
     * Sets the preferred visual configuration.
     *
     * This takes effect after the next init()/reset().
     */
    void visual_config(GLVisualConfig &config) { visual_config_ = config; }

protected:
    Canvas(int width, int height) :
        width_(width), height_(height), offscreen_(false) {}

    int width_;
    int height_;
    LibMatrix::mat4 projection_;
    bool offscreen_;
    GLVisualConfig visual_config_;
};

#endif