1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/viz/test/test_gles2_interface.h"
6 
7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "components/viz/test/test_context_support.h"
13 #include "gpu/GLES2/gl2extchromium.h"
14 #include "gpu/command_buffer/common/mailbox.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 
17 namespace viz {
18 
NextContextId()19 static unsigned NextContextId() {
20   static uint16_t s_context_id = 1;
21   // We need to ensure that the context_id fits in 16 bits since it is placed on
22   // the top 16 bits of the 32 bit identifiers (program_id, framebuffer_id,
23   // shader_id, etc.) generated by the context.
24   if (s_context_id == std::numeric_limits<uint16_t>::max()) {
25     LOG(ERROR) << "Exceeded max context id count; wrapping around";
26     s_context_id = 1;
27   }
28   return s_context_id++;
29 }
30 
TestGLES2Interface()31 TestGLES2Interface::TestGLES2Interface() : context_id_(NextContextId()) {
32   // For stream textures.
33   set_have_extension_egl_image(true);
34   set_max_texture_size(2048);
35 }
36 
37 TestGLES2Interface::~TestGLES2Interface() = default;
38 
GenTextures(GLsizei n,GLuint * textures)39 void TestGLES2Interface::GenTextures(GLsizei n, GLuint* textures) {
40   for (int i = 0; i < n; ++i) {
41     textures[i] = NextTextureId();
42     textures_.insert(textures[i]);
43   }
44 }
45 
GenBuffers(GLsizei n,GLuint * buffers)46 void TestGLES2Interface::GenBuffers(GLsizei n, GLuint* buffers) {
47   for (int i = 0; i < n; ++i)
48     buffers[i] = NextBufferId();
49 }
50 
GenFramebuffers(GLsizei n,GLuint * framebuffers)51 void TestGLES2Interface::GenFramebuffers(GLsizei n, GLuint* framebuffers) {
52   for (int i = 0; i < n; ++i)
53     framebuffers[i] = NextFramebufferId();
54 }
55 
GenRenderbuffers(GLsizei n,GLuint * renderbuffers)56 void TestGLES2Interface::GenRenderbuffers(GLsizei n, GLuint* renderbuffers) {
57   for (int i = 0; i < n; ++i)
58     renderbuffers[i] = NextRenderbufferId();
59 }
60 
GenQueriesEXT(GLsizei n,GLuint * queries)61 void TestGLES2Interface::GenQueriesEXT(GLsizei n, GLuint* queries) {
62   for (GLsizei i = 0; i < n; ++i) {
63     queries[i] = 1u;
64   }
65 }
66 
DeleteTextures(GLsizei n,const GLuint * textures)67 void TestGLES2Interface::DeleteTextures(GLsizei n, const GLuint* textures) {
68   for (int i = 0; i < n; ++i) {
69     RetireTextureId(textures[i]);
70     textures_.erase(textures[i]);
71   }
72 }
73 
DeleteBuffers(GLsizei n,const GLuint * buffers)74 void TestGLES2Interface::DeleteBuffers(GLsizei n, const GLuint* buffers) {
75   for (int i = 0; i < n; ++i)
76     RetireBufferId(buffers[i]);
77 }
78 
DeleteFramebuffers(GLsizei n,const GLuint * framebuffers)79 void TestGLES2Interface::DeleteFramebuffers(GLsizei n,
80                                             const GLuint* framebuffers) {
81   for (int i = 0; i < n; ++i) {
82     if (framebuffers[i]) {
83       RetireFramebufferId(framebuffers[i]);
84       if (framebuffers[i] == current_framebuffer_)
85         current_framebuffer_ = 0;
86     }
87   }
88 }
89 
DeleteQueriesEXT(GLsizei n,const GLuint * queries)90 void TestGLES2Interface::DeleteQueriesEXT(GLsizei n, const GLuint* queries) {
91 }
92 
CreateShader(GLenum type)93 GLuint TestGLES2Interface::CreateShader(GLenum type) {
94   unsigned shader = next_shader_id_++ | context_id_ << 16;
95   shader_set_.insert(shader);
96   return shader;
97 }
98 
CreateProgram()99 GLuint TestGLES2Interface::CreateProgram() {
100   unsigned program = next_program_id_++ | context_id_ << 16;
101   program_set_.insert(program);
102   return program;
103 }
104 
BindTexture(GLenum target,GLuint texture)105 void TestGLES2Interface::BindTexture(GLenum target, GLuint texture) {
106   if (times_bind_texture_succeeds_ >= 0) {
107     if (!times_bind_texture_succeeds_) {
108       LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
109                           GL_INNOCENT_CONTEXT_RESET_ARB);
110     }
111     --times_bind_texture_succeeds_;
112   }
113 
114   if (!texture)
115     return;
116   DCHECK(base::Contains(textures_, texture));
117   used_textures_.insert(texture);
118 }
119 
GetIntegerv(GLenum pname,GLint * params)120 void TestGLES2Interface::GetIntegerv(GLenum pname, GLint* params) {
121   if (pname == GL_MAX_TEXTURE_SIZE)
122     *params = test_capabilities_.max_texture_size;
123   else if (pname == GL_ACTIVE_TEXTURE)
124     *params = GL_TEXTURE0;
125   else if (pname == GL_UNPACK_ALIGNMENT)
126     *params = unpack_alignment_;
127   else if (pname == GL_FRAMEBUFFER_BINDING)
128     *params = current_framebuffer_;
129   else if (pname == GL_MAX_SAMPLES)
130     *params = test_capabilities_.max_samples;
131 }
132 
GetShaderiv(GLuint shader,GLenum pname,GLint * params)133 void TestGLES2Interface::GetShaderiv(GLuint shader,
134                                      GLenum pname,
135                                      GLint* params) {
136   if (pname == GL_COMPILE_STATUS)
137     *params = 1;
138 }
139 
GetProgramiv(GLuint program,GLenum pname,GLint * params)140 void TestGLES2Interface::GetProgramiv(GLuint program,
141                                       GLenum pname,
142                                       GLint* params) {
143   if (pname == GL_LINK_STATUS)
144     *params = 1;
145 }
146 
GetShaderPrecisionFormat(GLenum shadertype,GLenum precisiontype,GLint * range,GLint * precision)147 void TestGLES2Interface::GetShaderPrecisionFormat(GLenum shadertype,
148                                                   GLenum precisiontype,
149                                                   GLint* range,
150                                                   GLint* precision) {
151   // Return the minimum precision requirements of the GLES2
152   // specification.
153   switch (precisiontype) {
154     case GL_LOW_INT:
155       range[0] = 8;
156       range[1] = 8;
157       *precision = 0;
158       break;
159     case GL_MEDIUM_INT:
160       range[0] = 10;
161       range[1] = 10;
162       *precision = 0;
163       break;
164     case GL_HIGH_INT:
165       range[0] = 16;
166       range[1] = 16;
167       *precision = 0;
168       break;
169     case GL_LOW_FLOAT:
170       range[0] = 8;
171       range[1] = 8;
172       *precision = 8;
173       break;
174     case GL_MEDIUM_FLOAT:
175       range[0] = 14;
176       range[1] = 14;
177       *precision = 10;
178       break;
179     case GL_HIGH_FLOAT:
180       range[0] = 62;
181       range[1] = 62;
182       *precision = 16;
183       break;
184     default:
185       NOTREACHED();
186       break;
187   }
188 }
189 
UseProgram(GLuint program)190 void TestGLES2Interface::UseProgram(GLuint program) {
191   if (!program)
192     return;
193   if (!program_set_.count(program))
194     ADD_FAILURE() << "useProgram called on unknown program " << program;
195 }
196 
CheckFramebufferStatus(GLenum target)197 GLenum TestGLES2Interface::CheckFramebufferStatus(GLenum target) {
198   if (context_lost_)
199     return GL_FRAMEBUFFER_UNDEFINED_OES;
200   return GL_FRAMEBUFFER_COMPLETE;
201 }
202 
Flush()203 void TestGLES2Interface::Flush() {
204   test_support_->CallAllSyncPointCallbacks();
205 }
206 
Finish()207 void TestGLES2Interface::Finish() {
208   test_support_->CallAllSyncPointCallbacks();
209 }
210 
ShallowFinishCHROMIUM()211 void TestGLES2Interface::ShallowFinishCHROMIUM() {
212   test_support_->CallAllSyncPointCallbacks();
213 }
214 
BindRenderbuffer(GLenum target,GLuint renderbuffer)215 void TestGLES2Interface::BindRenderbuffer(GLenum target, GLuint renderbuffer) {
216   if (!renderbuffer)
217     return;
218   if (renderbuffer != 0 &&
219       renderbuffer_set_.find(renderbuffer) == renderbuffer_set_.end()) {
220     ADD_FAILURE() << "bindRenderbuffer called with unknown renderbuffer";
221   } else if ((renderbuffer >> 16) != context_id_) {
222     ADD_FAILURE()
223         << "bindRenderbuffer called with renderbuffer from other context";
224   }
225 }
226 
BindFramebuffer(GLenum target,GLuint framebuffer)227 void TestGLES2Interface::BindFramebuffer(GLenum target, GLuint framebuffer) {
228   if (framebuffer != 0 &&
229       framebuffer_set_.find(framebuffer) == framebuffer_set_.end()) {
230     ADD_FAILURE() << "bindFramebuffer called with unknown framebuffer";
231   } else if (framebuffer != 0 && (framebuffer >> 16) != context_id_) {
232     ADD_FAILURE()
233         << "bindFramebuffer called with framebuffer from other context";
234   } else {
235     current_framebuffer_ = framebuffer;
236   }
237 }
238 
BindBuffer(GLenum target,GLuint buffer)239 void TestGLES2Interface::BindBuffer(GLenum target, GLuint buffer) {
240   bound_buffer_[target] = buffer;
241   if (!buffer)
242     return;
243   unsigned context_id = buffer >> 16;
244   unsigned buffer_id = buffer & 0xffff;
245   DCHECK(buffer_id);
246   DCHECK_LT(buffer_id, next_buffer_id_);
247   DCHECK_EQ(context_id, context_id_);
248 
249   if (buffers_.count(bound_buffer_[target]) == 0)
250     buffers_[bound_buffer_[target]] = std::make_unique<Buffer>();
251 
252   buffers_[bound_buffer_[target]]->target = target;
253 }
254 
PixelStorei(GLenum pname,GLint param)255 void TestGLES2Interface::PixelStorei(GLenum pname, GLint param) {
256   switch (pname) {
257     case GL_UNPACK_ALIGNMENT:
258       // Param should be a power of two <= 8.
259       EXPECT_EQ(0, param & (param - 1));
260       EXPECT_GE(8, param);
261       switch (param) {
262         case 1:
263         case 2:
264         case 4:
265         case 8:
266           unpack_alignment_ = param;
267           break;
268         default:
269           break;
270       }
271       break;
272     default:
273       break;
274   }
275 }
276 
CreateImageCHROMIUM(ClientBuffer buffer,GLsizei width,GLsizei height,GLenum internalformat)277 GLuint TestGLES2Interface::CreateImageCHROMIUM(ClientBuffer buffer,
278                                                GLsizei width,
279                                                GLsizei height,
280                                                GLenum internalformat) {
281   DCHECK(internalformat == GL_RGB || internalformat == GL_RGBA ||
282          (test_capabilities_.texture_format_bgra8888 &&
283           internalformat == GL_BGRA_EXT));
284   GLuint image_id = NextImageId();
285   images_.insert(image_id);
286   return image_id;
287 }
288 
DestroyImageCHROMIUM(GLuint image_id)289 void TestGLES2Interface::DestroyImageCHROMIUM(GLuint image_id) {
290   RetireImageId(image_id);
291   if (!images_.count(image_id)) {
292     ADD_FAILURE() << "destroyImageCHROMIUM called on unknown image "
293                   << image_id;
294   }
295   images_.erase(image_id);
296 }
297 
MapBufferCHROMIUM(GLuint target,GLenum access)298 void* TestGLES2Interface::MapBufferCHROMIUM(GLuint target, GLenum access) {
299   DCHECK_GT(bound_buffer_.count(target), 0u);
300   DCHECK_GT(buffers_.count(bound_buffer_[target]), 0u);
301   DCHECK_EQ(target, buffers_[bound_buffer_[target]]->target);
302   if (times_map_buffer_chromium_succeeds_ >= 0) {
303     if (!times_map_buffer_chromium_succeeds_) {
304       return nullptr;
305     }
306     --times_map_buffer_chromium_succeeds_;
307   }
308 
309   return buffers_[bound_buffer_[target]]->pixels.get();
310 }
311 
UnmapBufferCHROMIUM(GLuint target)312 GLboolean TestGLES2Interface::UnmapBufferCHROMIUM(GLuint target) {
313   DCHECK_GT(bound_buffer_.count(target), 0u);
314   DCHECK_GT(buffers_.count(bound_buffer_[target]), 0u);
315   DCHECK_EQ(target, buffers_[bound_buffer_[target]]->target);
316   buffers_[bound_buffer_[target]]->pixels = nullptr;
317   return true;
318 }
319 
BufferData(GLenum target,GLsizeiptr size,const void * data,GLenum usage)320 void TestGLES2Interface::BufferData(GLenum target,
321                                     GLsizeiptr size,
322                                     const void* data,
323                                     GLenum usage) {
324   DCHECK_GT(bound_buffer_.count(target), 0u);
325   DCHECK_GT(buffers_.count(bound_buffer_[target]), 0u);
326   DCHECK_EQ(target, buffers_[bound_buffer_[target]]->target);
327   Buffer* buffer = buffers_[bound_buffer_[target]].get();
328   if (context_lost_) {
329     buffer->pixels = nullptr;
330     return;
331   }
332 
333   buffer->pixels.reset(new uint8_t[size]);
334   buffer->size = size;
335   if (data != nullptr)
336     memcpy(buffer->pixels.get(), data, size);
337 }
338 
GenSyncTokenCHROMIUM(GLbyte * sync_token)339 void TestGLES2Interface::GenSyncTokenCHROMIUM(GLbyte* sync_token) {
340   // Don't return a valid sync token if context is lost. This matches behavior
341   // of CommandBufferProxyImpl.
342   if (context_lost_)
343     return;
344   gpu::SyncToken sync_token_data(gpu::CommandBufferNamespace::GPU_IO,
345                                  gpu::CommandBufferId(),
346                                  next_insert_fence_sync_++);
347   sync_token_data.SetVerifyFlush();
348   memcpy(sync_token, &sync_token_data, sizeof(sync_token_data));
349 }
350 
GenUnverifiedSyncTokenCHROMIUM(GLbyte * sync_token)351 void TestGLES2Interface::GenUnverifiedSyncTokenCHROMIUM(GLbyte* sync_token) {
352   // Don't return a valid sync token if context is lost. This matches behavior
353   // of CommandBufferProxyImpl.
354   if (context_lost_)
355     return;
356   gpu::SyncToken sync_token_data(gpu::CommandBufferNamespace::GPU_IO,
357                                  gpu::CommandBufferId(),
358                                  next_insert_fence_sync_++);
359   memcpy(sync_token, &sync_token_data, sizeof(sync_token_data));
360 }
361 
VerifySyncTokensCHROMIUM(GLbyte ** sync_tokens,GLsizei count)362 void TestGLES2Interface::VerifySyncTokensCHROMIUM(GLbyte** sync_tokens,
363                                                   GLsizei count) {
364   for (GLsizei i = 0; i < count; ++i) {
365     gpu::SyncToken sync_token_data;
366     memcpy(sync_token_data.GetData(), sync_tokens[i], sizeof(sync_token_data));
367     sync_token_data.SetVerifyFlush();
368     memcpy(sync_tokens[i], &sync_token_data, sizeof(sync_token_data));
369   }
370 }
371 
WaitSyncTokenCHROMIUM(const GLbyte * sync_token)372 void TestGLES2Interface::WaitSyncTokenCHROMIUM(const GLbyte* sync_token) {
373   gpu::SyncToken sync_token_data;
374   if (sync_token)
375     memcpy(&sync_token_data, sync_token, sizeof(sync_token_data));
376 
377   if (sync_token_data.release_count() >
378       last_waited_sync_token_.release_count()) {
379     last_waited_sync_token_ = sync_token_data;
380   }
381 }
382 
BeginQueryEXT(GLenum target,GLuint id)383 void TestGLES2Interface::BeginQueryEXT(GLenum target, GLuint id) {}
384 
EndQueryEXT(GLenum target)385 void TestGLES2Interface::EndQueryEXT(GLenum target) {
386   if (times_end_query_succeeds_ >= 0) {
387     if (!times_end_query_succeeds_) {
388       LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
389                           GL_INNOCENT_CONTEXT_RESET_ARB);
390     }
391     --times_end_query_succeeds_;
392   }
393 }
394 
GetQueryObjectuivEXT(GLuint id,GLenum pname,GLuint * params)395 void TestGLES2Interface::GetQueryObjectuivEXT(GLuint id,
396                                               GLenum pname,
397                                               GLuint* params) {
398   // If the context is lost, behave as if result is available.
399   if (pname == GL_QUERY_RESULT_AVAILABLE_EXT ||
400       pname == GL_QUERY_RESULT_AVAILABLE_NO_FLUSH_CHROMIUM_EXT) {
401     *params = 1;
402   }
403 }
404 
ProduceTextureDirectCHROMIUM(GLuint texture,GLbyte * mailbox)405 void TestGLES2Interface::ProduceTextureDirectCHROMIUM(GLuint texture,
406                                                       GLbyte* mailbox) {
407   gpu::Mailbox gpu_mailbox = gpu::Mailbox::Generate();
408   memcpy(mailbox, gpu_mailbox.name, sizeof(gpu_mailbox.name));
409 }
410 
CreateAndConsumeTextureCHROMIUM(const GLbyte * mailbox)411 GLuint TestGLES2Interface::CreateAndConsumeTextureCHROMIUM(
412     const GLbyte* mailbox) {
413   GLuint texture_id;
414   GenTextures(1, &texture_id);
415   return texture_id;
416 }
417 
CreateAndTexStorage2DSharedImageCHROMIUM(const GLbyte * mailbox)418 GLuint TestGLES2Interface::CreateAndTexStorage2DSharedImageCHROMIUM(
419     const GLbyte* mailbox) {
420   GLuint texture_id;
421   GenTextures(1, &texture_id);
422   return texture_id;
423 }
424 
ResizeCHROMIUM(GLuint width,GLuint height,float device_scale,GLcolorSpace color_space,GLboolean has_alpha)425 void TestGLES2Interface::ResizeCHROMIUM(GLuint width,
426                                         GLuint height,
427                                         float device_scale,
428                                         GLcolorSpace color_space,
429                                         GLboolean has_alpha) {
430   reshape_called_ = true;
431   width_ = width;
432   height_ = height;
433   scale_factor_ = device_scale;
434 }
435 
LoseContextCHROMIUM(GLenum current,GLenum other)436 void TestGLES2Interface::LoseContextCHROMIUM(GLenum current, GLenum other) {
437   if (context_lost_)
438     return;
439   context_lost_ = true;
440   if (!context_lost_callback_.is_null())
441     std::move(context_lost_callback_).Run();
442 
443   for (size_t i = 0; i < shared_contexts_.size(); ++i)
444     shared_contexts_[i]->LoseContextCHROMIUM(current, other);
445   shared_contexts_.clear();
446 }
447 
GetGraphicsResetStatusKHR()448 GLenum TestGLES2Interface::GetGraphicsResetStatusKHR() {
449   if (IsContextLost())
450     return GL_UNKNOWN_CONTEXT_RESET_KHR;
451   return GL_NO_ERROR;
452 }
453 
set_times_bind_texture_succeeds(int times)454 void TestGLES2Interface::set_times_bind_texture_succeeds(int times) {
455   times_bind_texture_succeeds_ = times;
456 }
457 
set_have_extension_io_surface(bool have)458 void TestGLES2Interface::set_have_extension_io_surface(bool have) {
459   test_capabilities_.iosurface = have;
460   test_capabilities_.texture_rectangle = have;
461 }
462 
set_have_extension_egl_image(bool have)463 void TestGLES2Interface::set_have_extension_egl_image(bool have) {
464   test_capabilities_.egl_image_external = have;
465 }
466 
set_have_post_sub_buffer(bool have)467 void TestGLES2Interface::set_have_post_sub_buffer(bool have) {
468   test_capabilities_.post_sub_buffer = have;
469 }
470 
set_have_swap_buffers_with_bounds(bool have)471 void TestGLES2Interface::set_have_swap_buffers_with_bounds(bool have) {
472   test_capabilities_.swap_buffers_with_bounds = have;
473 }
474 
set_have_commit_overlay_planes(bool have)475 void TestGLES2Interface::set_have_commit_overlay_planes(bool have) {
476   test_capabilities_.commit_overlay_planes = have;
477 }
478 
set_have_discard_framebuffer(bool have)479 void TestGLES2Interface::set_have_discard_framebuffer(bool have) {
480   test_capabilities_.discard_framebuffer = have;
481 }
482 
set_support_compressed_texture_etc1(bool support)483 void TestGLES2Interface::set_support_compressed_texture_etc1(bool support) {
484   test_capabilities_.texture_format_etc1 = support;
485 }
486 
set_support_texture_format_bgra8888(bool support)487 void TestGLES2Interface::set_support_texture_format_bgra8888(bool support) {
488   test_capabilities_.texture_format_bgra8888 = support;
489 }
490 
set_support_texture_storage(bool support)491 void TestGLES2Interface::set_support_texture_storage(bool support) {
492   test_capabilities_.texture_storage = support;
493 }
494 
set_support_texture_usage(bool support)495 void TestGLES2Interface::set_support_texture_usage(bool support) {
496   test_capabilities_.texture_usage = support;
497 }
498 
set_support_sync_query(bool support)499 void TestGLES2Interface::set_support_sync_query(bool support) {
500   test_capabilities_.sync_query = support;
501 }
502 
set_support_texture_rectangle(bool support)503 void TestGLES2Interface::set_support_texture_rectangle(bool support) {
504   test_capabilities_.texture_rectangle = support;
505 }
506 
set_support_texture_half_float_linear(bool support)507 void TestGLES2Interface::set_support_texture_half_float_linear(bool support) {
508   test_capabilities_.texture_half_float_linear = support;
509 }
510 
set_support_texture_norm16(bool support)511 void TestGLES2Interface::set_support_texture_norm16(bool support) {
512   test_capabilities_.texture_norm16 = support;
513 }
514 
set_msaa_is_slow(bool msaa_is_slow)515 void TestGLES2Interface::set_msaa_is_slow(bool msaa_is_slow) {
516   test_capabilities_.msaa_is_slow = msaa_is_slow;
517 }
518 
set_gpu_rasterization(bool gpu_rasterization)519 void TestGLES2Interface::set_gpu_rasterization(bool gpu_rasterization) {
520   test_capabilities_.gpu_rasterization = gpu_rasterization;
521 }
522 
set_avoid_stencil_buffers(bool avoid_stencil_buffers)523 void TestGLES2Interface::set_avoid_stencil_buffers(bool avoid_stencil_buffers) {
524   test_capabilities_.avoid_stencil_buffers = avoid_stencil_buffers;
525 }
526 
set_support_multisample_compatibility(bool support)527 void TestGLES2Interface::set_support_multisample_compatibility(bool support) {
528   test_capabilities_.multisample_compatibility = support;
529 }
530 
set_support_texture_storage_image(bool support)531 void TestGLES2Interface::set_support_texture_storage_image(bool support) {
532   test_capabilities_.texture_storage_image = support;
533 }
534 
set_support_texture_npot(bool support)535 void TestGLES2Interface::set_support_texture_npot(bool support) {
536   test_capabilities_.texture_npot = support;
537 }
538 
set_max_texture_size(int size)539 void TestGLES2Interface::set_max_texture_size(int size) {
540   test_capabilities_.max_texture_size = size;
541 }
542 
set_supports_oop_raster(bool support)543 void TestGLES2Interface::set_supports_oop_raster(bool support) {
544   test_capabilities_.supports_oop_raster = support;
545 }
546 
set_supports_shared_image_swap_chain(bool support)547 void TestGLES2Interface::set_supports_shared_image_swap_chain(bool support) {
548   test_capabilities_.shared_image_swap_chain = support;
549 }
550 
set_supports_gpu_memory_buffer_format(gfx::BufferFormat format,bool support)551 void TestGLES2Interface::set_supports_gpu_memory_buffer_format(
552     gfx::BufferFormat format,
553     bool support) {
554   if (support) {
555     test_capabilities_.gpu_memory_buffer_formats.Add(format);
556   } else {
557     test_capabilities_.gpu_memory_buffer_formats.Remove(format);
558   }
559 }
560 
NumTextures() const561 size_t TestGLES2Interface::NumTextures() const {
562   return textures_.size();
563 }
564 
NextTextureId()565 GLuint TestGLES2Interface::NextTextureId() {
566   GLuint texture_id = next_texture_id_++;
567   DCHECK(texture_id < (1 << 16));
568   texture_id |= context_id_ << 16;
569   return texture_id;
570 }
571 
RetireTextureId(GLuint id)572 void TestGLES2Interface::RetireTextureId(GLuint id) {
573   unsigned context_id = id >> 16;
574   unsigned texture_id = id & 0xffff;
575   DCHECK(texture_id);
576   DCHECK_LT(texture_id, next_texture_id_);
577   DCHECK_EQ(context_id, context_id_);
578 }
579 
NextBufferId()580 GLuint TestGLES2Interface::NextBufferId() {
581   GLuint buffer_id = next_buffer_id_++;
582   DCHECK(buffer_id < (1 << 16));
583   buffer_id |= context_id_ << 16;
584   return buffer_id;
585 }
586 
RetireBufferId(GLuint id)587 void TestGLES2Interface::RetireBufferId(GLuint id) {
588   unsigned context_id = id >> 16;
589   unsigned buffer_id = id & 0xffff;
590   DCHECK(buffer_id);
591   DCHECK_LT(buffer_id, next_buffer_id_);
592   DCHECK_EQ(context_id, context_id_);
593 }
594 
NextImageId()595 GLuint TestGLES2Interface::NextImageId() {
596   GLuint image_id = next_image_id_++;
597   DCHECK(image_id < (1 << 16));
598   image_id |= context_id_ << 16;
599   return image_id;
600 }
601 
RetireImageId(GLuint id)602 void TestGLES2Interface::RetireImageId(GLuint id) {
603   unsigned context_id = id >> 16;
604   unsigned image_id = id & 0xffff;
605   DCHECK(image_id);
606   DCHECK_LT(image_id, next_image_id_);
607   DCHECK_EQ(context_id, context_id_);
608 }
609 
NextFramebufferId()610 GLuint TestGLES2Interface::NextFramebufferId() {
611   GLuint id = next_framebuffer_id_++;
612   DCHECK(id < (1 << 16));
613   id |= context_id_ << 16;
614   framebuffer_set_.insert(id);
615   return id;
616 }
617 
RetireFramebufferId(GLuint id)618 void TestGLES2Interface::RetireFramebufferId(GLuint id) {
619   DCHECK(base::Contains(framebuffer_set_, id));
620   framebuffer_set_.erase(id);
621 }
622 
NextRenderbufferId()623 GLuint TestGLES2Interface::NextRenderbufferId() {
624   GLuint id = next_renderbuffer_id_++;
625   DCHECK(id < (1 << 16));
626   id |= context_id_ << 16;
627   renderbuffer_set_.insert(id);
628   return id;
629 }
630 
RetireRenderbufferId(GLuint id)631 void TestGLES2Interface::RetireRenderbufferId(GLuint id) {
632   DCHECK(base::Contains(renderbuffer_set_, id));
633   renderbuffer_set_.erase(id);
634 }
635 
SetMaxSamples(int max_samples)636 void TestGLES2Interface::SetMaxSamples(int max_samples) {
637   test_capabilities_.max_samples = max_samples;
638 }
639 
NumFramebuffers() const640 size_t TestGLES2Interface::NumFramebuffers() const {
641   return framebuffer_set_.size();
642 }
643 
NumRenderbuffers() const644 size_t TestGLES2Interface::NumRenderbuffers() const {
645   return renderbuffer_set_.size();
646 }
647 
Buffer()648 TestGLES2Interface::Buffer::Buffer() : target(0), size(0) {}
649 
650 TestGLES2Interface::Buffer::~Buffer() = default;
651 
652 }  // namespace viz
653