~kdub/mir/mali-client-render-support

« back to all changes in this revision

Viewing changes to examples/image_renderer.cpp

  • Committer: Kevin DuBois
  • Date: 2013-11-05 21:44:14 UTC
  • mfrom: (1062.1.137 dev)
  • Revision ID: kevin.dubois@canonical.com-20131105214414-d32r52joqrs8tsmo
merge dev branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
87
87
    BOOST_THROW_EXCEPTION(std::runtime_error(object_info_err));
88
88
}
89
89
 
 
90
class GLState
 
91
{
 
92
public:
 
93
    GLState(GLint attrib_loc)
 
94
        : attrib_loc{attrib_loc}
 
95
    {
 
96
        glGetIntegerv(GL_CURRENT_PROGRAM, &program);
 
97
        glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture);
 
98
        glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &buffer);
 
99
        glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture_unit);
 
100
        if (attrib_loc >= 0)
 
101
        {
 
102
            glGetVertexAttribiv(attrib_loc, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &attrib_enabled);
 
103
            glGetVertexAttribiv(attrib_loc, GL_VERTEX_ATTRIB_ARRAY_SIZE, &attrib_size);
 
104
            glGetVertexAttribiv(attrib_loc, GL_VERTEX_ATTRIB_ARRAY_TYPE, &attrib_type);
 
105
            glGetVertexAttribiv(attrib_loc, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &attrib_normalized);
 
106
            glGetVertexAttribiv(attrib_loc, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &attrib_stride);
 
107
            glGetVertexAttribPointerv(attrib_loc, GL_VERTEX_ATTRIB_ARRAY_POINTER, &attrib_pointer);
 
108
        }
 
109
    }
 
110
 
 
111
    GLState() : GLState{invalid_attrib_loc} {}
 
112
 
 
113
    ~GLState()
 
114
    {
 
115
        glUseProgram(program);
 
116
        glBindTexture(GL_TEXTURE_2D, texture);
 
117
        glBindBuffer(GL_ARRAY_BUFFER, buffer);
 
118
        glActiveTexture(active_texture_unit);
 
119
        if (attrib_loc >= 0)
 
120
        {
 
121
            glVertexAttribPointer(attrib_loc, attrib_size, attrib_type,
 
122
                                  attrib_normalized, attrib_stride, attrib_pointer);
 
123
            if (attrib_enabled)
 
124
                glEnableVertexAttribArray(attrib_loc);
 
125
            else
 
126
                glDisableVertexAttribArray(attrib_loc);
 
127
        }
 
128
    }
 
129
 
 
130
private:
 
131
    static GLint const invalid_attrib_loc = -1;
 
132
    GLint program = 0;
 
133
    GLint texture = 0;
 
134
    GLint buffer = 0;
 
135
    GLint active_texture_unit = 0;
 
136
    GLint attrib_loc = invalid_attrib_loc;
 
137
    GLint attrib_enabled = 0;
 
138
    GLint attrib_size = 0;
 
139
    GLint attrib_type = 0;
 
140
    GLint attrib_normalized = 0;
 
141
    GLint attrib_stride = 0;
 
142
    GLvoid* attrib_pointer = nullptr;
 
143
};
 
144
 
90
145
}
91
146
 
92
147
mt::ImageRenderer::ImageRenderer(const uint8_t* pixel_data, mir::geometry::Size size,
93
148
                                 uint32_t bytes_per_pixel)
94
149
{
 
150
    GLState gl_state;
 
151
 
95
152
    resources.setup();
96
153
 
97
154
    /* Upload the texture */
173
230
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
174
231
 
175
232
    glUniform1i(tex_loc, 0);
176
 
    glBindTexture(GL_TEXTURE_2D, 0);
177
233
 
178
234
    /* Create VBO */
179
235
    glGenBuffers(1, &vertex_attribs_vbo);
181
237
    glBindBuffer(GL_ARRAY_BUFFER, vertex_attribs_vbo);
182
238
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_attribs),
183
239
                 glm::value_ptr(vertex_attribs[0]), GL_STATIC_DRAW);
184
 
 
185
 
    glBindBuffer(GL_ARRAY_BUFFER, 0);
186
 
    glUseProgram(0);
187
240
}
188
241
 
189
242
 
190
243
void mt::ImageRenderer::render()
191
244
{
 
245
    GLState gl_state(resources.position_attr_loc);
 
246
 
192
247
    glUseProgram(resources.program);
193
248
 
194
249
    glActiveTexture(GL_TEXTURE0);
202
257
    /* Draw */
203
258
    glEnableVertexAttribArray(resources.position_attr_loc);
204
259
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
205
 
    glDisableVertexAttribArray(resources.position_attr_loc);
206
260
}