File: test_gles2_interface.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (547 lines) | stat: -rw-r--r-- 16,725 bytes parent folder | download | duplicates (6)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "components/viz/test/test_gles2_interface.h"

#include "base/containers/contains.h"
#include "base/containers/heap_array.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/numerics/safe_conversions.h"
#include "components/viz/test/test_context_support.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "gpu/command_buffer/common/gles2_cmd_utils.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace viz {

static unsigned NextContextId() {
  static uint16_t s_context_id = 1;
  // We need to ensure that the context_id fits in 16 bits since it is placed on
  // the top 16 bits of the 32 bit identifiers (program_id, framebuffer_id,
  // shader_id, etc.) generated by the context.
  if (s_context_id == std::numeric_limits<uint16_t>::max()) {
    LOG(ERROR) << "Exceeded max context id count; wrapping around";
    s_context_id = 1;
  }
  return s_context_id++;
}

TestGLES2Interface::TestGLES2Interface() : context_id_(NextContextId()) {
  // For stream textures.
  test_capabilities_.egl_image_external = true;
  set_max_texture_size(2048);
}

TestGLES2Interface::~TestGLES2Interface() = default;

void TestGLES2Interface::GenTextures(GLsizei n, GLuint* textures) {
  for (int i = 0; i < n; ++i) {
    textures[i] = NextTextureId();
    textures_.insert(textures[i]);
  }
}

void TestGLES2Interface::GenBuffers(GLsizei n, GLuint* buffers) {
  for (int i = 0; i < n; ++i)
    buffers[i] = NextBufferId();
}

void TestGLES2Interface::GenFramebuffers(GLsizei n, GLuint* framebuffers) {
  for (int i = 0; i < n; ++i)
    framebuffers[i] = NextFramebufferId();
}

void TestGLES2Interface::GenRenderbuffers(GLsizei n, GLuint* renderbuffers) {
  for (int i = 0; i < n; ++i)
    renderbuffers[i] = NextRenderbufferId();
}

void TestGLES2Interface::GenQueriesEXT(GLsizei n, GLuint* queries) {
  for (GLsizei i = 0; i < n; ++i) {
    queries[i] = 1u;
  }
}

void TestGLES2Interface::DeleteTextures(GLsizei n, const GLuint* textures) {
  for (int i = 0; i < n; ++i) {
    RetireTextureId(textures[i]);
    textures_.erase(textures[i]);
  }
}

void TestGLES2Interface::DeleteBuffers(GLsizei n, const GLuint* buffers) {
  for (int i = 0; i < n; ++i)
    RetireBufferId(buffers[i]);
}

void TestGLES2Interface::DeleteFramebuffers(GLsizei n,
                                            const GLuint* framebuffers) {
  for (int i = 0; i < n; ++i) {
    if (framebuffers[i]) {
      RetireFramebufferId(framebuffers[i]);
      if (framebuffers[i] == current_framebuffer_)
        current_framebuffer_ = 0;
    }
  }
}

void TestGLES2Interface::DeleteQueriesEXT(GLsizei n, const GLuint* queries) {
}

GLuint TestGLES2Interface::CreateShader(GLenum type) {
  unsigned shader = next_shader_id_++ | context_id_ << 16;
  shader_set_.insert(shader);
  return shader;
}

GLuint TestGLES2Interface::CreateProgram() {
  unsigned program = next_program_id_++ | context_id_ << 16;
  program_set_.insert(program);
  return program;
}

void TestGLES2Interface::BindTexture(GLenum target, GLuint texture) {
  if (!texture)
    return;
  DCHECK(base::Contains(textures_, texture));
  used_textures_.insert(texture);
}

void TestGLES2Interface::GetIntegerv(GLenum pname, GLint* params) {
  if (pname == GL_MAX_TEXTURE_SIZE)
    *params = test_gl_capabilities_.max_texture_size;
  else if (pname == GL_ACTIVE_TEXTURE)
    *params = GL_TEXTURE0;
  else if (pname == GL_UNPACK_ALIGNMENT)
    *params = unpack_alignment_;
  else if (pname == GL_FRAMEBUFFER_BINDING)
    *params = current_framebuffer_;
  else if (pname == GL_MAX_SAMPLES)
    *params = test_gl_capabilities_.max_samples;
}

void TestGLES2Interface::GetShaderiv(GLuint shader,
                                     GLenum pname,
                                     GLint* params) {
  if (pname == GL_COMPILE_STATUS)
    *params = 1;
}

void TestGLES2Interface::GetProgramiv(GLuint program,
                                      GLenum pname,
                                      GLint* params) {
  if (pname == GL_LINK_STATUS)
    *params = 1;
}

void TestGLES2Interface::GetShaderPrecisionFormat(GLenum shadertype,
                                                  GLenum precisiontype,
                                                  GLint* range,
                                                  GLint* precision) {
  // Return the minimum precision requirements of the GLES2
  // specification.
  switch (precisiontype) {
    case GL_LOW_INT:
      range[0] = 8;
      range[1] = 8;
      *precision = 0;
      break;
    case GL_MEDIUM_INT:
      range[0] = 10;
      range[1] = 10;
      *precision = 0;
      break;
    case GL_HIGH_INT:
      range[0] = 16;
      range[1] = 16;
      *precision = 0;
      break;
    case GL_LOW_FLOAT:
      range[0] = 8;
      range[1] = 8;
      *precision = 8;
      break;
    case GL_MEDIUM_FLOAT:
      range[0] = 14;
      range[1] = 14;
      *precision = 10;
      break;
    case GL_HIGH_FLOAT:
      range[0] = 62;
      range[1] = 62;
      *precision = 16;
      break;
    default:
      NOTREACHED();
  }
}

void TestGLES2Interface::UseProgram(GLuint program) {
  if (!program)
    return;
  if (!program_set_.count(program))
    ADD_FAILURE() << "useProgram called on unknown program " << program;
}

GLenum TestGLES2Interface::CheckFramebufferStatus(GLenum target) {
  if (context_lost_)
    return GL_FRAMEBUFFER_UNDEFINED_OES;
  return GL_FRAMEBUFFER_COMPLETE;
}

void TestGLES2Interface::Flush() {
  test_support_->CallAllSyncPointCallbacks();
}

void TestGLES2Interface::Finish() {
  test_support_->CallAllSyncPointCallbacks();
}

void TestGLES2Interface::ShallowFinishCHROMIUM() {
  test_support_->CallAllSyncPointCallbacks();
}

void TestGLES2Interface::BindRenderbuffer(GLenum target, GLuint renderbuffer) {
  if (!renderbuffer)
    return;
  if (renderbuffer != 0 &&
      renderbuffer_set_.find(renderbuffer) == renderbuffer_set_.end()) {
    ADD_FAILURE() << "bindRenderbuffer called with unknown renderbuffer";
  } else if ((renderbuffer >> 16) != context_id_) {
    ADD_FAILURE()
        << "bindRenderbuffer called with renderbuffer from other context";
  }
}

void TestGLES2Interface::BindFramebuffer(GLenum target, GLuint framebuffer) {
  if (framebuffer != 0 &&
      framebuffer_set_.find(framebuffer) == framebuffer_set_.end()) {
    ADD_FAILURE() << "bindFramebuffer called with unknown framebuffer";
  } else if (framebuffer != 0 && (framebuffer >> 16) != context_id_) {
    ADD_FAILURE()
        << "bindFramebuffer called with framebuffer from other context";
  } else {
    current_framebuffer_ = framebuffer;
  }
}

void TestGLES2Interface::BindBuffer(GLenum target, GLuint buffer) {
  bound_buffer_[target] = buffer;
  if (!buffer)
    return;
  unsigned context_id = buffer >> 16;
  unsigned buffer_id = buffer & 0xffff;
  DCHECK(buffer_id);
  DCHECK_LT(buffer_id, next_buffer_id_);
  DCHECK_EQ(context_id, context_id_);

  if (buffers_.count(bound_buffer_[target]) == 0)
    buffers_[bound_buffer_[target]] = std::make_unique<Buffer>();

  buffers_[bound_buffer_[target]]->target = target;
}

void TestGLES2Interface::PixelStorei(GLenum pname, GLint param) {
  switch (pname) {
    case GL_UNPACK_ALIGNMENT:
      // Param should be a power of two <= 8.
      EXPECT_EQ(0, param & (param - 1));
      EXPECT_GE(8, param);
      switch (param) {
        case 1:
        case 2:
        case 4:
        case 8:
          unpack_alignment_ = param;
          break;
        default:
          break;
      }
      break;
    default:
      break;
  }
}

void* TestGLES2Interface::MapBufferCHROMIUM(GLuint target, GLenum access) {
  DCHECK_GT(bound_buffer_.count(target), 0u);
  DCHECK_GT(buffers_.count(bound_buffer_[target]), 0u);
  DCHECK_EQ(target, buffers_[bound_buffer_[target]]->target);
  if (times_map_buffer_chromium_succeeds_ >= 0) {
    if (!times_map_buffer_chromium_succeeds_) {
      return nullptr;
    }
    --times_map_buffer_chromium_succeeds_;
  }

  return buffers_[bound_buffer_[target]]->pixels.data();
}

GLboolean TestGLES2Interface::UnmapBufferCHROMIUM(GLuint target) {
  DCHECK_GT(bound_buffer_.count(target), 0u);
  DCHECK_GT(buffers_.count(bound_buffer_[target]), 0u);
  DCHECK_EQ(target, buffers_[bound_buffer_[target]]->target);
  buffers_[bound_buffer_[target]]->pixels = {};
  return true;
}

void TestGLES2Interface::BufferData(GLenum target,
                                    GLsizeiptr size,
                                    const void* data,
                                    GLenum usage) {
  DCHECK_GT(bound_buffer_.count(target), 0u);
  DCHECK_GT(buffers_.count(bound_buffer_[target]), 0u);
  DCHECK_EQ(target, buffers_[bound_buffer_[target]]->target);
  Buffer* buffer = buffers_[bound_buffer_[target]].get();
  if (context_lost_) {
    buffer->pixels = {};
    return;
  }

  buffer->pixels = base::HeapArray<uint8_t>::Uninit(size);
  if (data != nullptr) {
    buffer->pixels.as_span().copy_from(
        base::span<const uint8_t>(reinterpret_cast<const uint8_t*>(data),
                                  base::checked_cast<size_t>(size)));
  }
}

void TestGLES2Interface::GenSyncTokenCHROMIUM(GLbyte* sync_token) {
  // Don't return a valid sync token if context is lost. This matches behavior
  // of CommandBufferProxyImpl.
  if (context_lost_)
    return;
  gpu::SyncToken sync_token_data(gpu::CommandBufferNamespace::GPU_IO,
                                 gpu::CommandBufferId(),
                                 next_insert_fence_sync_++);
  sync_token_data.SetVerifyFlush();
  memcpy(sync_token, &sync_token_data, sizeof(sync_token_data));
}

void TestGLES2Interface::GenUnverifiedSyncTokenCHROMIUM(GLbyte* sync_token) {
  // Don't return a valid sync token if context is lost. This matches behavior
  // of CommandBufferProxyImpl.
  if (context_lost_)
    return;
  gpu::SyncToken sync_token_data(gpu::CommandBufferNamespace::GPU_IO,
                                 gpu::CommandBufferId(),
                                 next_insert_fence_sync_++);
  memcpy(sync_token, &sync_token_data, sizeof(sync_token_data));
}

void TestGLES2Interface::VerifySyncTokensCHROMIUM(GLbyte** sync_tokens,
                                                  GLsizei count) {
  for (GLsizei i = 0; i < count; ++i) {
    gpu::SyncToken sync_token_data;
    memcpy(sync_token_data.GetData(), sync_tokens[i], sizeof(sync_token_data));
    sync_token_data.SetVerifyFlush();
    memcpy(sync_tokens[i], &sync_token_data, sizeof(sync_token_data));
  }
}

void TestGLES2Interface::WaitSyncTokenCHROMIUM(const GLbyte* sync_token) {
  gpu::SyncToken sync_token_data;
  if (sync_token)
    memcpy(&sync_token_data, sync_token, sizeof(sync_token_data));

  if (sync_token_data.release_count() >
      last_waited_sync_token_.release_count()) {
    last_waited_sync_token_ = sync_token_data;
  }
}

void TestGLES2Interface::BeginQueryEXT(GLenum target, GLuint id) {}

void TestGLES2Interface::EndQueryEXT(GLenum target) {
  if (times_end_query_succeeds_ >= 0) {
    if (!times_end_query_succeeds_) {
      LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
                          GL_INNOCENT_CONTEXT_RESET_ARB);
    }
    --times_end_query_succeeds_;
  }
}

void TestGLES2Interface::GetQueryObjectuivEXT(GLuint id,
                                              GLenum pname,
                                              GLuint* params) {
  // If the context is lost, behave as if result is available.
  if (pname == GL_QUERY_RESULT_AVAILABLE_EXT ||
      pname == GL_QUERY_RESULT_AVAILABLE_NO_FLUSH_CHROMIUM_EXT) {
    *params = 1;
  }
}

GLuint TestGLES2Interface::CreateAndTexStorage2DSharedImageCHROMIUM(
    const GLbyte* mailbox) {
  GLuint texture_id;
  GenTextures(1, &texture_id);
  return texture_id;
}

void TestGLES2Interface::LoseContextCHROMIUM(GLenum current, GLenum other) {
  if (context_lost_)
    return;
  context_lost_ = true;
  if (!context_lost_callback_.is_null())
    std::move(context_lost_callback_).Run();

  for (size_t i = 0; i < shared_contexts_.size(); ++i)
    shared_contexts_[i]->LoseContextCHROMIUM(current, other);
  shared_contexts_.clear();
}

GLenum TestGLES2Interface::GetGraphicsResetStatusKHR() {
  if (IsContextLost())
    return GL_UNKNOWN_CONTEXT_RESET_KHR;
  return GL_NO_ERROR;
}

void TestGLES2Interface::ReadPixels(GLint x,
                                    GLint y,
                                    GLsizei width,
                                    GLsizei height,
                                    GLenum format,
                                    GLenum type,
                                    void* pixels) {
  // Zero-initialize the destination buffer to appease MSAN. Note that we don't
  // support non-default alignment or ES3 pixel store parameters, but that's ok
  // since this is test-only code and MSAN will catch any uninitialized access.
  uint32_t pixels_size = 0;
  gpu::gles2::GLES2Util::ComputeImageDataSizes(
      width, height, /*depth=*/1, format, type, /*alignment=*/4, &pixels_size,
      /*opt_unpadded_row_size=*/nullptr, /*opt_padded_row_size=*/nullptr);
  memset(pixels, 0, pixels_size);
}

void TestGLES2Interface::set_support_texture_half_float_linear(bool support) {
  test_capabilities_.texture_half_float_linear = support;
}

void TestGLES2Interface::set_support_texture_norm16(bool support) {
  test_capabilities_.texture_norm16 = support;
}

void TestGLES2Interface::set_gpu_rasterization(bool gpu_rasterization) {
  test_capabilities_.gpu_rasterization = gpu_rasterization;
}

void TestGLES2Interface::set_max_texture_size(int size) {
  test_gl_capabilities_.max_texture_size = size;
  test_capabilities_.max_texture_size = size;
}

void TestGLES2Interface::set_supports_gpu_memory_buffer_format(
    gfx::BufferFormat format,
    bool support) {
  if (support) {
    test_capabilities_.gpu_memory_buffer_formats.Put(format);
  } else {
    test_capabilities_.gpu_memory_buffer_formats.Remove(format);
  }
}

void TestGLES2Interface::set_supports_texture_rg(bool support) {
  test_capabilities_.texture_rg = support;
}

size_t TestGLES2Interface::NumTextures() const {
  return textures_.size();
}

GLuint TestGLES2Interface::NextTextureId() {
  GLuint texture_id = next_texture_id_++;
  DCHECK(texture_id < (1 << 16));
  texture_id |= context_id_ << 16;
  return texture_id;
}

void TestGLES2Interface::RetireTextureId(GLuint id) {
  unsigned context_id = id >> 16;
  unsigned texture_id = id & 0xffff;
  DCHECK(texture_id);
  DCHECK_LT(texture_id, next_texture_id_);
  DCHECK_EQ(context_id, context_id_);
}

GLuint TestGLES2Interface::NextBufferId() {
  GLuint buffer_id = next_buffer_id_++;
  DCHECK(buffer_id < (1 << 16));
  buffer_id |= context_id_ << 16;
  return buffer_id;
}

void TestGLES2Interface::RetireBufferId(GLuint id) {
  unsigned context_id = id >> 16;
  unsigned buffer_id = id & 0xffff;
  DCHECK(buffer_id);
  DCHECK_LT(buffer_id, next_buffer_id_);
  DCHECK_EQ(context_id, context_id_);
}

GLuint TestGLES2Interface::NextImageId() {
  GLuint image_id = next_image_id_++;
  DCHECK(image_id < (1 << 16));
  image_id |= context_id_ << 16;
  return image_id;
}

void TestGLES2Interface::RetireImageId(GLuint id) {
  unsigned context_id = id >> 16;
  unsigned image_id = id & 0xffff;
  DCHECK(image_id);
  DCHECK_LT(image_id, next_image_id_);
  DCHECK_EQ(context_id, context_id_);
}

GLuint TestGLES2Interface::NextFramebufferId() {
  GLuint id = next_framebuffer_id_++;
  DCHECK(id < (1 << 16));
  id |= context_id_ << 16;
  framebuffer_set_.insert(id);
  return id;
}

void TestGLES2Interface::RetireFramebufferId(GLuint id) {
  DCHECK(base::Contains(framebuffer_set_, id));
  framebuffer_set_.erase(id);
}

GLuint TestGLES2Interface::NextRenderbufferId() {
  GLuint id = next_renderbuffer_id_++;
  DCHECK(id < (1 << 16));
  id |= context_id_ << 16;
  renderbuffer_set_.insert(id);
  return id;
}

void TestGLES2Interface::RetireRenderbufferId(GLuint id) {
  DCHECK(base::Contains(renderbuffer_set_, id));
  renderbuffer_set_.erase(id);
}

size_t TestGLES2Interface::NumFramebuffers() const {
  return framebuffer_set_.size();
}

size_t TestGLES2Interface::NumRenderbuffers() const {
  return renderbuffer_set_.size();
}

TestGLES2Interface::Buffer::Buffer() : target(0) {}

TestGLES2Interface::Buffer::~Buffer() = default;

}  // namespace viz