summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/tests/gl_virtual_contexts_unittest.cc
blob: d161ff664a3e5b70072d316c5577083a27224084 (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <GLES2/gl2extchromium.h>
#include <stdint.h>

#include "gpu/command_buffer/tests/gl_manager.h"
#include "gpu/command_buffer/tests/gl_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#define SHADER(Src) #Src

namespace gpu {

class GLVirtualContextsTest : public testing::Test {
 protected:
  static const int kSize0 = 4;
  static const int kSize1 = 8;
  static const int kSize2 = 16;

  static const GLfloat kFloatRed[4];
  static const GLfloat kFloatGreen[4];
  static const uint8_t kExpectedRed[4];
  static const uint8_t kExpectedGreen[4];

  void SetUp() override {
    GLManager::Options options;
    options.size = gfx::Size(kSize0, kSize0);
    gl_real_.Initialize(options);
    gl_real_shared_.Initialize(options);
    options.virtual_manager = &gl_real_shared_;
    options.size = gfx::Size(kSize1, kSize1);
    gl1_.Initialize(options);
    options.size = gfx::Size(kSize2, kSize2);
    gl2_.Initialize(options);
  }

  void TearDown() override {
    gl1_.Destroy();
    gl2_.Destroy();
    gl_real_shared_.Destroy();
    gl_real_.Destroy();
  }

  GLuint SetupColoredVertexProgram() {
    static const char* v_shader_str = SHADER(
        attribute vec4 a_position;
        attribute vec4 a_color;
        varying vec4 v_color;
        void main()
        {
           gl_Position = a_position;
           v_color = a_color;
        }
     );

    static const char* f_shader_str = SHADER(
        precision mediump float;
        varying vec4 v_color;
        void main()
        {
          gl_FragColor = v_color;
        }
    );

    GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
    glUseProgram(program);
    return program;
  }

  void SetUpColoredUnitQuad(const GLfloat* color) {
    GLuint program1 = SetupColoredVertexProgram();
    GLuint position_loc1 = glGetAttribLocation(program1, "a_position");
    GLuint color_loc1 = glGetAttribLocation(program1, "a_color");
    GLTestHelper::SetupUnitQuad(position_loc1);
    GLTestHelper::SetupColorsForUnitQuad(color_loc1, color, GL_STATIC_DRAW);
  }

  GLManager gl_real_;
  GLManager gl_real_shared_;
  GLManager gl1_;
  GLManager gl2_;
};

const GLfloat GLVirtualContextsTest::kFloatRed[4] = {
    1.0f, 0.0f, 0.0f, 1.0f,
};
const GLfloat GLVirtualContextsTest::kFloatGreen[4] = {
    0.0f, 1.0f, 0.0f, 1.0f,
};
const uint8_t GLVirtualContextsTest::kExpectedRed[4] = {
    255, 0, 0, 255,
};
const uint8_t GLVirtualContextsTest::kExpectedGreen[4] = {
    0, 255, 0, 255,
};

namespace {

void SetupSimpleShader(const uint8_t* color) {
  static const char* v_shader_str = SHADER(
      attribute vec4 a_Position;
      void main()
      {
         gl_Position = a_Position;
      }
   );

  static const char* f_shader_str = SHADER(
      precision mediump float;
      uniform vec4 u_color;
      void main()
      {
        gl_FragColor = u_color;
      }
  );

  GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
  glUseProgram(program);

  GLuint position_loc = glGetAttribLocation(program, "a_Position");

  GLTestHelper::SetupUnitQuad(position_loc);

  GLuint color_loc = glGetUniformLocation(program, "u_color");
  glUniform4f(
      color_loc,
      color[0] / 255.0f,
      color[1] / 255.0f,
      color[2] / 255.0f,
      color[3] / 255.0f);
}

void TestDraw(int size) {
  uint8_t expected_clear[] = {
      127, 0, 255, 0,
  };
  glClearColor(0.5f, 0.0f, 1.0f, 0.0f);
  glClear(GL_COLOR_BUFFER_BIT);
  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, size, size, 1, expected_clear));
  glDrawArrays(GL_TRIANGLES, 0, 6);
}

}  // anonymous namespace

// http://crbug.com/281565
TEST_F(GLVirtualContextsTest, Basic) {
  struct TestInfo {
    int size;
    uint8_t color[4];
    GLManager* manager;
  };
  const int kNumTests = 3;
  TestInfo tests[] = {
    { kSize0, { 255, 0, 0, 0, }, &gl_real_, },
    { kSize1, { 0, 255, 0, 0, }, &gl1_, },
    { kSize2, { 0, 0, 255, 0, }, &gl2_, },
  };

  for (int ii = 0; ii < kNumTests; ++ii) {
    const TestInfo& test = tests[ii];
    GLManager* gl_manager = test.manager;
    gl_manager->MakeCurrent();
    SetupSimpleShader(test.color);
  }

  for (int ii = 0; ii < kNumTests; ++ii) {
    const TestInfo& test = tests[ii];
    GLManager* gl_manager = test.manager;
    gl_manager->MakeCurrent();
    TestDraw(test.size);
  }

  for (int ii = 0; ii < kNumTests; ++ii) {
    const TestInfo& test = tests[ii];
    GLManager* gl_manager = test.manager;
    gl_manager->MakeCurrent();
    EXPECT_TRUE(GLTestHelper::CheckPixels(
        0, 0, test.size, test.size, 0, test.color));
  }

  for (int ii = 0; ii < kNumTests; ++ii) {
    const TestInfo& test = tests[ii];
    GLManager* gl_manager = test.manager;
    gl_manager->MakeCurrent();
    GLTestHelper::CheckGLError("no errors", __LINE__);
  }
}

// http://crbug.com/363407
TEST_F(GLVirtualContextsTest, VertexArrayObjectRestore) {
  GLuint vao1 = 0, vao2 = 0;

  gl1_.MakeCurrent();
  // Set up red quad in vao1.
  glGenVertexArraysOES(1, &vao1);
  glBindVertexArrayOES(vao1);
  SetUpColoredUnitQuad(kFloatRed);
  glFinish();

  gl2_.MakeCurrent();
  // Set up green quad in vao2.
  glGenVertexArraysOES(1, &vao2);
  glBindVertexArrayOES(vao2);
  SetUpColoredUnitQuad(kFloatGreen);
  glFinish();

  gl1_.MakeCurrent();
  // Test to ensure that vao1 is still the active VAO for this context.
  glDrawArrays(GL_TRIANGLES, 0, 6);
  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize1, kSize1, 0, kExpectedRed));
  glFinish();
  GLTestHelper::CheckGLError("no errors", __LINE__);

  gl2_.MakeCurrent();
  // Test to ensure that vao2 is still the active VAO for this context.
  glDrawArrays(GL_TRIANGLES, 0, 6);
  EXPECT_TRUE(
      GLTestHelper::CheckPixels(0, 0, kSize2, kSize2, 0, kExpectedGreen));
  glFinish();
  GLTestHelper::CheckGLError("no errors", __LINE__);
}

// http://crbug.com/363407
TEST_F(GLVirtualContextsTest, VertexArrayObjectRestoreRebind) {
  GLuint vao1 = 0, vao2 = 0;

  gl1_.MakeCurrent();
  // Set up red quad in vao1.
  glGenVertexArraysOES(1, &vao1);
  glBindVertexArrayOES(vao1);
  SetUpColoredUnitQuad(kFloatRed);
  glFinish();

  gl2_.MakeCurrent();
  // Set up green quad in new vao2.
  glGenVertexArraysOES(1, &vao2);
  glBindVertexArrayOES(vao2);
  SetUpColoredUnitQuad(kFloatGreen);
  glFinish();

  gl1_.MakeCurrent();
  // Test to ensure that vao1 hasn't been corrupted after rebinding.
  // Bind 0 is required so that bind vao1 is not optimized away in the service.
  glBindVertexArrayOES(0);
  glBindVertexArrayOES(vao1);
  glDrawArrays(GL_TRIANGLES, 0, 6);
  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize1, kSize1, 0, kExpectedRed));
  glFinish();
  GLTestHelper::CheckGLError("no errors", __LINE__);

  gl2_.MakeCurrent();
  // Test to ensure that vao1 hasn't been corrupted after rebinding.
  // Bind 0 is required so that bind vao2 is not optimized away in the service.
  glBindVertexArrayOES(0);
  glBindVertexArrayOES(vao2);
  glDrawArrays(GL_TRIANGLES, 0, 6);
  EXPECT_TRUE(
      GLTestHelper::CheckPixels(0, 0, kSize2, kSize2, 0, kExpectedGreen));
  glFinish();

  GLTestHelper::CheckGLError("no errors", __LINE__);
}

// http://crbug.com/363407
TEST_F(GLVirtualContextsTest, VertexArrayObjectRestoreDefault) {
  gl1_.MakeCurrent();
  // Set up red quad in default VAO.
  SetUpColoredUnitQuad(kFloatRed);
  glFinish();

  gl2_.MakeCurrent();
  // Set up green quad in default VAO.
  SetUpColoredUnitQuad(kFloatGreen);
  glFinish();

  // Gen & bind a non-default VAO.
  GLuint vao;
  glGenVertexArraysOES(1, &vao);
  glBindVertexArrayOES(vao);
  glFinish();

  gl1_.MakeCurrent();
  // Test to ensure that default VAO on gl1_ is still valid.
  glDrawArrays(GL_TRIANGLES, 0, 6);
  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize1, kSize1, 0, kExpectedRed));
  glFinish();

  gl2_.MakeCurrent();
  // Test to ensure that default VAO on gl2_ is still valid.
  // This tests that a default VAO is restored even when it's not currently
  // bound during the context switch.
  glBindVertexArrayOES(0);
  glDrawArrays(GL_TRIANGLES, 0, 6);
  EXPECT_TRUE(
      GLTestHelper::CheckPixels(0, 0, kSize2, kSize2, 0, kExpectedGreen));
  glFinish();

  GLTestHelper::CheckGLError("no errors", __LINE__);
}

TEST_F(GLVirtualContextsTest, VirtualQueries) {
  const GLenum query_targets[] = {
    GL_ANY_SAMPLES_PASSED_EXT,
    GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
    GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM,
    GL_COMMANDS_COMPLETED_CHROMIUM,
    GL_COMMANDS_ISSUED_CHROMIUM,
    GL_GET_ERROR_QUERY_CHROMIUM,
    GL_LATENCY_QUERY_CHROMIUM,
    GL_TIME_ELAPSED_EXT,
  };

  for (GLenum query_target : query_targets) {
    GLuint query1 = 0;
    gl1_.MakeCurrent();
    glGenQueriesEXT(1, &query1);
    glBeginQueryEXT(query_target, query1);
    const GLenum begin_error = glGetError();
    if (GL_INVALID_OPERATION == begin_error) {
      // Not supported, simply skip.
      glDeleteQueriesEXT(1, &query1);
      continue;
    }
    ASSERT_TRUE(GL_NO_ERROR == begin_error);

    GLuint query2 = 0;
    gl2_.MakeCurrent();
    glGenQueriesEXT(1, &query2);
    glBeginQueryEXT(query_target, query2);
    EXPECT_TRUE(GL_NO_ERROR == glGetError())
        << "Virtualized Query " << query_target << " failed.";

    gl1_.MakeCurrent();
    glEndQueryEXT(query_target);
    glFinish();

    GLuint query1_available = 0;
    glGetQueryObjectuivEXT(query1, GL_QUERY_RESULT_AVAILABLE_EXT,
                           &query1_available);
    EXPECT_TRUE(query1_available);

    glDeleteQueriesEXT(1, &query1);
    GLTestHelper::CheckGLError("no errors", __LINE__);

    gl2_.MakeCurrent();
    glEndQueryEXT(query_target);
    glFinish();

    GLuint query2_available = 0;
    glGetQueryObjectuivEXT(query2, GL_QUERY_RESULT_AVAILABLE_EXT,
                           &query2_available);
    EXPECT_TRUE(query2_available);

    glDeleteQueriesEXT(1, &query2);
    GLTestHelper::CheckGLError("no errors", __LINE__);
  }
}

}  // namespace gpu