gles2_cmd_decoder_unittest_base.h revision 558790d6acca3451cf3a6b497803a5f07d0bec58
1// Copyright (c) 2012 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#ifndef GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_
6#define GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_
7
8#include "gpu/command_buffer/common/gles2_cmd_format.h"
9#include "gpu/command_buffer/common/gles2_cmd_utils.h"
10#include "gpu/command_buffer/service/buffer_manager.h"
11#include "gpu/command_buffer/service/cmd_buffer_engine.h"
12#include "gpu/command_buffer/service/context_group.h"
13#include "gpu/command_buffer/service/framebuffer_manager.h"
14#include "gpu/command_buffer/service/gles2_cmd_decoder.h"
15#include "gpu/command_buffer/service/program_manager.h"
16#include "gpu/command_buffer/service/query_manager.h"
17#include "gpu/command_buffer/service/renderbuffer_manager.h"
18#include "gpu/command_buffer/service/shader_manager.h"
19#include "gpu/command_buffer/service/stream_texture_manager_mock.h"
20#include "gpu/command_buffer/service/test_helper.h"
21#include "gpu/command_buffer/service/texture_manager.h"
22#include "gpu/command_buffer/service/vertex_array_manager.h"
23#include "testing/gtest/include/gtest/gtest.h"
24#include "ui/gl/gl_context_stub.h"
25#include "ui/gl/gl_surface_stub.h"
26#include "ui/gl/gl_mock.h"
27
28namespace gpu {
29namespace gles2 {
30
31class MemoryTracker;
32
33class GLES2DecoderTestBase : public testing::Test {
34 public:
35  GLES2DecoderTestBase();
36  virtual ~GLES2DecoderTestBase();
37
38  // Template to call glGenXXX functions.
39  template <typename T>
40  void GenHelper(GLuint client_id) {
41    int8 buffer[sizeof(T) + sizeof(client_id)];
42    T& cmd = *reinterpret_cast<T*>(&buffer);
43    cmd.Init(1, &client_id);
44    EXPECT_EQ(error::kNoError,
45              ExecuteImmediateCmd(cmd, sizeof(client_id)));
46  }
47
48  // This template exists solely so we can specialize it for
49  // certain commands.
50  template <typename T, int id>
51  void SpecializedSetup(bool valid) {
52  }
53
54  template <typename T>
55  T* GetImmediateAs() {
56    return reinterpret_cast<T*>(immediate_buffer_);
57  }
58
59  template <typename T, typename Command>
60  T GetImmediateDataAs(Command* cmd) {
61    return reinterpret_cast<T>(ImmediateDataAddress(cmd));
62  }
63
64  void ClearSharedMemory() {
65    engine_->ClearSharedMemory();
66  }
67
68  virtual void SetUp() OVERRIDE;
69  virtual void TearDown() OVERRIDE;
70
71  template <typename T>
72  error::Error ExecuteCmd(const T& cmd) {
73    COMPILE_ASSERT(T::kArgFlags == cmd::kFixed, Cmd_kArgFlags_not_kFixed);
74    return decoder_->DoCommand(cmd.kCmdId,
75                               ComputeNumEntries(sizeof(cmd)) - 1,
76                               &cmd);
77  }
78
79  template <typename T>
80  error::Error ExecuteImmediateCmd(const T& cmd, size_t data_size) {
81    COMPILE_ASSERT(T::kArgFlags == cmd::kAtLeastN, Cmd_kArgFlags_not_kAtLeastN);
82    return decoder_->DoCommand(cmd.kCmdId,
83                               ComputeNumEntries(sizeof(cmd) + data_size) - 1,
84                               &cmd);
85  }
86
87  template <typename T>
88  T GetSharedMemoryAs() {
89    return reinterpret_cast<T>(shared_memory_address_);
90  }
91
92  template <typename T>
93  T GetSharedMemoryAsWithOffset(uint32 offset) {
94    void* ptr = reinterpret_cast<int8*>(shared_memory_address_) + offset;
95    return reinterpret_cast<T>(ptr);
96  }
97
98  IdAllocatorInterface* GetIdAllocator(GLuint namespace_id) {
99    return group_->GetIdAllocator(namespace_id);
100  }
101
102  Buffer* GetBuffer(GLuint service_id) {
103    return group_->buffer_manager()->GetBuffer(service_id);
104  }
105
106  Framebuffer* GetFramebuffer(GLuint service_id) {
107    return group_->framebuffer_manager()->GetFramebuffer(service_id);
108  }
109
110  Renderbuffer* GetRenderbuffer(
111      GLuint service_id) {
112    return group_->renderbuffer_manager()->GetRenderbuffer(service_id);
113  }
114
115  TextureRef* GetTexture(GLuint client_id) {
116    return group_->texture_manager()->GetTexture(client_id);
117  }
118
119  Shader* GetShader(GLuint client_id) {
120    return group_->shader_manager()->GetShader(client_id);
121  }
122
123  Program* GetProgram(GLuint client_id) {
124    return group_->program_manager()->GetProgram(client_id);
125  }
126
127  QueryManager::Query* GetQueryInfo(GLuint client_id) {
128    return decoder_->GetQueryManager()->GetQuery(client_id);
129  }
130
131  // This name doesn't match the underlying function, but doing it this way
132  // prevents the need to special-case the unit test generation
133  VertexAttribManager* GetVertexArrayInfo(GLuint client_id) {
134    return decoder_->GetVertexArrayManager()->GetVertexAttribManager(client_id);
135  }
136
137  ProgramManager* program_manager() {
138    return group_->program_manager();
139  }
140
141  ::testing::StrictMock<MockStreamTextureManager>*
142  stream_texture_manager() const {
143    return stream_texture_manager_.get();
144  }
145
146  void DoCreateProgram(GLuint client_id, GLuint service_id);
147  void DoCreateShader(GLenum shader_type, GLuint client_id, GLuint service_id);
148
149  void SetBucketAsCString(uint32 bucket_id, const char* str);
150
151  void set_memory_tracker(MemoryTracker* memory_tracker) {
152    memory_tracker_ = memory_tracker;
153  }
154
155  void InitDecoder(
156      const char* extensions,
157      bool has_alpha,
158      bool has_depth,
159      bool has_stencil,
160      bool request_alpha,
161      bool request_depth,
162      bool request_stencil,
163      bool bind_generates_resource);
164
165  const ContextGroup& group() const {
166    return *group_.get();
167  }
168
169  ::testing::StrictMock< ::gfx::MockGLInterface>* GetGLMock() const {
170    return gl_.get();
171  }
172
173  GLES2Decoder* GetDecoder() const {
174    return decoder_.get();
175  }
176
177  typedef TestHelper::AttribInfo AttribInfo;
178  typedef TestHelper::UniformInfo UniformInfo;
179
180  void SetupShader(
181      AttribInfo* attribs, size_t num_attribs,
182      UniformInfo* uniforms, size_t num_uniforms,
183      GLuint client_id, GLuint service_id,
184      GLuint vertex_shader_client_id, GLuint vertex_shader_service_id,
185      GLuint fragment_shader_client_id, GLuint fragment_shader_service_id);
186
187  void SetupExpectationsForClearingUniforms(
188      UniformInfo* uniforms, size_t num_uniforms) {
189    TestHelper::SetupExpectationsForClearingUniforms(
190        gl_.get(), uniforms, num_uniforms);
191  }
192
193  void SetupInitCapabilitiesExpectations();
194  void SetupInitStateExpectations();
195  void ExpectEnableDisable(GLenum cap, bool enable);
196
197  // Setups up a shader for testing glUniform.
198  void SetupShaderForUniform(GLenum uniform_type);
199  void SetupDefaultProgram();
200  void SetupCubemapProgram();
201  void SetupSamplerExternalProgram();
202  void SetupTexture();
203
204  // Note that the error is returned as GLint instead of GLenum.
205  // This is because there is a mismatch in the types of GLenum and
206  // the error values GL_NO_ERROR, GL_INVALID_ENUM, etc. GLenum is
207  // typedef'd as unsigned int while the error values are defined as
208  // integers. This is problematic for template functions such as
209  // EXPECT_EQ that expect both types to be the same.
210  GLint GetGLError();
211
212  void DoBindBuffer(GLenum target, GLuint client_id, GLuint service_id);
213  void DoBindFramebuffer(GLenum target, GLuint client_id, GLuint service_id);
214  void DoBindRenderbuffer(GLenum target, GLuint client_id, GLuint service_id);
215  void DoBindTexture(GLenum target, GLuint client_id, GLuint service_id);
216  void DoBindVertexArrayOES(GLuint client_id, GLuint service_id);
217
218  bool DoIsBuffer(GLuint client_id);
219  bool DoIsFramebuffer(GLuint client_id);
220  bool DoIsProgram(GLuint client_id);
221  bool DoIsRenderbuffer(GLuint client_id);
222  bool DoIsShader(GLuint client_id);
223  bool DoIsTexture(GLuint client_id);
224
225  void DoDeleteBuffer(GLuint client_id, GLuint service_id);
226  void DoDeleteFramebuffer(
227      GLuint client_id, GLuint service_id,
228      bool reset_draw, GLenum draw_target, GLuint draw_id,
229      bool reset_read, GLenum read_target, GLuint read_id);
230  void DoDeleteProgram(GLuint client_id, GLuint service_id);
231  void DoDeleteRenderbuffer(GLuint client_id, GLuint service_id);
232  void DoDeleteShader(GLuint client_id, GLuint service_id);
233  void DoDeleteTexture(GLuint client_id, GLuint service_id);
234
235  void DoCompressedTexImage2D(
236      GLenum target, GLint level, GLenum format,
237      GLsizei width, GLsizei height, GLint border,
238      GLsizei size, uint32 bucket_id);
239  void DoTexImage2D(
240      GLenum target, GLint level, GLenum internal_format,
241      GLsizei width, GLsizei height, GLint border,
242      GLenum format, GLenum type,
243      uint32 shared_memory_id, uint32 shared_memory_offset);
244  void DoTexImage2DSameSize(
245      GLenum target, GLint level, GLenum internal_format,
246      GLsizei width, GLsizei height, GLint border,
247      GLenum format, GLenum type,
248      uint32 shared_memory_id, uint32 shared_memory_offset);
249  void DoRenderbufferStorage(
250      GLenum target, GLenum internal_format, GLenum actual_format,
251      GLsizei width, GLsizei height, GLenum error);
252  void DoFramebufferRenderbuffer(
253      GLenum target,
254      GLenum attachment,
255      GLenum renderbuffer_target,
256      GLuint renderbuffer_client_id,
257      GLuint renderbuffer_service_id,
258      GLenum error);
259  void DoFramebufferTexture2D(
260      GLenum target, GLenum attachment, GLenum tex_target,
261      GLuint texture_client_id, GLuint texture_service_id,
262      GLint level, GLenum error);
263  void DoVertexAttribPointer(
264      GLuint index, GLint size, GLenum type, GLsizei stride, GLuint offset);
265  void DoVertexAttribDivisorANGLE(GLuint index, GLuint divisor);
266
267  void DoEnableVertexAttribArray(GLint index);
268
269  void DoBufferData(GLenum target, GLsizei size);
270
271  void DoBufferSubData(
272      GLenum target, GLint offset, GLsizei size, const void* data);
273
274  void SetupVertexBuffer();
275  void SetupAllNeededVertexBuffers();
276
277  void SetupIndexBuffer();
278
279  void DeleteVertexBuffer();
280
281  void DeleteIndexBuffer();
282
283  void SetupClearTextureExpections(
284      GLuint service_id,
285      GLuint old_service_id,
286      GLenum bind_target,
287      GLenum target,
288      GLint level,
289      GLenum format,
290      GLenum type,
291      GLsizei width,
292      GLsizei height);
293
294  void SetupExpectationsForRestoreClearState(
295      GLclampf restore_red,
296      GLclampf restore_green,
297      GLclampf restore_blue,
298      GLclampf restore_alpha,
299      GLuint restore_stencil,
300      GLclampf restore_depth,
301      bool restore_scissor_test);
302
303  void SetupExpectationsForFramebufferClearing(
304      GLenum target,
305      GLuint clear_bits,
306      GLclampf restore_red,
307      GLclampf restore_green,
308      GLclampf restore_blue,
309      GLclampf restore_alpha,
310      GLuint restore_stencil,
311      GLclampf restore_depth,
312      bool restore_scissor_test);
313
314  void SetupExpectationsForFramebufferClearingMulti(
315      GLuint read_framebuffer_service_id,
316      GLuint draw_framebuffer_service_id,
317      GLenum target,
318      GLuint clear_bits,
319      GLclampf restore_red,
320      GLclampf restore_green,
321      GLclampf restore_blue,
322      GLclampf restore_alpha,
323      GLuint restore_stencil,
324      GLclampf restore_depth,
325      bool restore_scissor_test);
326
327  void SetupExpectationsForApplyingDirtyState(
328      bool framebuffer_is_rgb,
329      bool framebuffer_has_depth,
330      bool framebuffer_has_stencil,
331      GLuint color_bits,  // NOTE! bits are 0x1000, 0x0100, 0x0010, and 0x0001
332      bool depth_mask,
333      bool depth_enabled,
334      GLuint front_stencil_mask,
335      GLuint back_stencil_mask,
336      bool stencil_enabled,
337      bool cull_face_enabled,
338      bool scissor_test_enabled,
339      bool blend_enabled);
340
341  void SetupExpectationsForApplyingDefaultDirtyState();
342
343  void AddExpectationsForSimulatedAttrib0WithError(
344      GLsizei num_vertices, GLuint buffer_id, GLenum error);
345
346  void AddExpectationsForSimulatedAttrib0(
347      GLsizei num_vertices, GLuint buffer_id);
348
349  void AddExpectationsForGenVertexArraysOES();
350  void AddExpectationsForDeleteVertexArraysOES();
351  void AddExpectationsForBindVertexArrayOES();
352  void AddExpectationsForRestoreAttribState(GLuint attrib);
353
354  GLvoid* BufferOffset(unsigned i) {
355    return static_cast<int8 *>(NULL)+(i);
356  }
357
358  template <typename Command, typename Result>
359  bool IsObjectHelper(GLuint client_id) {
360    Result* result = static_cast<Result*>(shared_memory_address_);
361    Command cmd;
362    cmd.Init(client_id, kSharedMemoryId, kSharedMemoryOffset);
363    EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
364    bool isObject = static_cast<bool>(*result);
365    EXPECT_EQ(GL_NO_ERROR, GetGLError());
366    return isObject;
367  }
368
369 protected:
370  static const int kBackBufferWidth = 128;
371  static const int kBackBufferHeight = 64;
372
373  static const GLint kMaxTextureSize = 2048;
374  static const GLint kMaxCubeMapTextureSize = 256;
375  static const GLint kNumVertexAttribs = 16;
376  static const GLint kNumTextureUnits = 8;
377  static const GLint kMaxTextureImageUnits = 8;
378  static const GLint kMaxVertexTextureImageUnits = 2;
379  static const GLint kMaxFragmentUniformVectors = 16;
380  static const GLint kMaxVaryingVectors = 8;
381  static const GLint kMaxVertexUniformVectors = 128;
382  static const GLint kMaxViewportWidth = 8192;
383  static const GLint kMaxViewportHeight = 8192;
384
385  static const GLint kViewportX = 0;
386  static const GLint kViewportY = 0;
387  static const GLint kViewportWidth = kBackBufferWidth;
388  static const GLint kViewportHeight = kBackBufferHeight;
389
390  static const GLuint kServiceAttrib0BufferId = 801;
391  static const GLuint kServiceFixedAttribBufferId = 802;
392
393  static const GLuint kServiceBufferId = 301;
394  static const GLuint kServiceFramebufferId = 302;
395  static const GLuint kServiceRenderbufferId = 303;
396  static const GLuint kServiceTextureId = 304;
397  static const GLuint kServiceProgramId = 305;
398  static const GLuint kServiceShaderId = 306;
399  static const GLuint kServiceElementBufferId = 308;
400  static const GLuint kServiceQueryId = 309;
401  static const GLuint kServiceVertexArrayId = 310;
402
403  static const int32 kSharedMemoryId = 401;
404  static const size_t kSharedBufferSize = 2048;
405  static const uint32 kSharedMemoryOffset = 132;
406  static const int32 kInvalidSharedMemoryId = 402;
407  static const uint32 kInvalidSharedMemoryOffset = kSharedBufferSize + 1;
408  static const uint32 kInitialResult = 0xBDBDBDBDu;
409  static const uint8 kInitialMemoryValue = 0xBDu;
410
411  static const uint32 kNewClientId = 501;
412  static const uint32 kNewServiceId = 502;
413  static const uint32 kInvalidClientId = 601;
414
415  static const GLuint kServiceVertexShaderId = 321;
416  static const GLuint kServiceFragmentShaderId = 322;
417
418  static const GLuint kServiceCopyTextureChromiumShaderId = 701;
419  static const GLuint kServiceCopyTextureChromiumProgramId = 721;
420
421  static const GLuint kServiceCopyTextureChromiumTextureBufferId = 751;
422  static const GLuint kServiceCopyTextureChromiumVertexBufferId = 752;
423  static const GLuint kServiceCopyTextureChromiumFBOId = 753;
424  static const GLuint kServiceCopyTextureChromiumPositionAttrib = 761;
425  static const GLuint kServiceCopyTextureChromiumTexAttrib = 762;
426  static const GLuint kServiceCopyTextureChromiumSamplerLocation = 763;
427
428  static const GLsizei kNumVertices = 100;
429  static const GLsizei kNumIndices = 10;
430  static const int kValidIndexRangeStart = 1;
431  static const int kValidIndexRangeCount = 7;
432  static const int kInvalidIndexRangeStart = 0;
433  static const int kInvalidIndexRangeCount = 7;
434  static const int kOutOfRangeIndexRangeEnd = 10;
435  static const GLuint kMaxValidIndex = 7;
436
437  static const GLint kMaxAttribLength = 10;
438  static const char* kAttrib1Name;
439  static const char* kAttrib2Name;
440  static const char* kAttrib3Name;
441  static const GLint kAttrib1Size = 1;
442  static const GLint kAttrib2Size = 1;
443  static const GLint kAttrib3Size = 1;
444  static const GLint kAttrib1Location = 0;
445  static const GLint kAttrib2Location = 1;
446  static const GLint kAttrib3Location = 2;
447  static const GLenum kAttrib1Type = GL_FLOAT_VEC4;
448  static const GLenum kAttrib2Type = GL_FLOAT_VEC2;
449  static const GLenum kAttrib3Type = GL_FLOAT_VEC3;
450  static const GLint kInvalidAttribLocation = 30;
451  static const GLint kBadAttribIndex = kNumVertexAttribs;
452
453  static const GLint kMaxUniformLength = 12;
454  static const char* kUniform1Name;
455  static const char* kUniform2Name;
456  static const char* kUniform3Name;
457  static const GLint kUniform1Size = 1;
458  static const GLint kUniform2Size = 3;
459  static const GLint kUniform3Size = 2;
460  static const GLint kUniform1RealLocation = 3;
461  static const GLint kUniform2RealLocation = 10;
462  static const GLint kUniform2ElementRealLocation = 12;
463  static const GLint kUniform3RealLocation = 20;
464  static const GLint kUniform1FakeLocation = 0;               // These are
465  static const GLint kUniform2FakeLocation = 1;               // hardcoded
466  static const GLint kUniform2ElementFakeLocation = 0x10001;  // to match
467  static const GLint kUniform3FakeLocation = 2;               // ProgramManager.
468  static const GLint kUniform1DesiredLocation = -1;
469  static const GLint kUniform2DesiredLocation = -1;
470  static const GLint kUniform3DesiredLocation = -1;
471  static const GLenum kUniform1Type = GL_SAMPLER_2D;
472  static const GLenum kUniform2Type = GL_INT_VEC2;
473  static const GLenum kUniform3Type = GL_FLOAT_VEC3;
474  static const GLenum kUniformSamplerExternalType = GL_SAMPLER_EXTERNAL_OES;
475  static const GLenum kUniformCubemapType = GL_SAMPLER_CUBE;
476  static const GLint kInvalidUniformLocation = 30;
477  static const GLint kBadUniformIndex = 1000;
478
479  // Use StrictMock to make 100% sure we know how GL will be called.
480  scoped_ptr< ::testing::StrictMock< ::gfx::MockGLInterface> > gl_;
481  scoped_refptr<gfx::GLSurfaceStub> surface_;
482  scoped_refptr<gfx::GLContextStub> context_;
483  scoped_ptr<GLES2Decoder> mock_decoder_;
484  scoped_ptr<GLES2Decoder> decoder_;
485  MemoryTracker* memory_tracker_;
486
487  GLuint client_buffer_id_;
488  GLuint client_framebuffer_id_;
489  GLuint client_program_id_;
490  GLuint client_renderbuffer_id_;
491  GLuint client_shader_id_;
492  GLuint client_texture_id_;
493  GLuint client_element_buffer_id_;
494  GLuint client_vertex_shader_id_;
495  GLuint client_fragment_shader_id_;
496  GLuint client_query_id_;
497  GLuint client_vertexarray_id_;
498
499  uint32 shared_memory_id_;
500  uint32 shared_memory_offset_;
501  void* shared_memory_address_;
502  void* shared_memory_base_;
503
504  int8 immediate_buffer_[256];
505
506 private:
507  class MockCommandBufferEngine : public CommandBufferEngine {
508   public:
509    MockCommandBufferEngine();
510
511    virtual ~MockCommandBufferEngine();
512
513    virtual gpu::Buffer GetSharedMemoryBuffer(int32 shm_id) OVERRIDE;
514
515    void ClearSharedMemory() {
516      memset(data_.get(), kInitialMemoryValue, kSharedBufferSize);
517    }
518
519    virtual void set_token(int32 token) OVERRIDE;
520
521    virtual bool SetGetBuffer(int32 /* transfer_buffer_id */) OVERRIDE;
522
523    // Overridden from CommandBufferEngine.
524    virtual bool SetGetOffset(int32 offset) OVERRIDE;
525
526    // Overridden from CommandBufferEngine.
527    virtual int32 GetGetOffset() OVERRIDE;
528
529   private:
530    scoped_ptr<int8[]> data_;
531    gpu::Buffer valid_buffer_;
532    gpu::Buffer invalid_buffer_;
533  };
534
535  void AddExpectationsForVertexAttribManager();
536
537  scoped_ptr< ::testing::StrictMock<MockCommandBufferEngine> > engine_;
538  scoped_ptr< ::testing::StrictMock<MockStreamTextureManager> >
539      stream_texture_manager_;
540  scoped_refptr<ContextGroup> group_;
541};
542
543class GLES2DecoderWithShaderTestBase : public GLES2DecoderTestBase {
544 public:
545  GLES2DecoderWithShaderTestBase()
546      : GLES2DecoderTestBase() {
547  }
548
549 protected:
550  virtual void SetUp() OVERRIDE;
551  virtual void TearDown() OVERRIDE;
552
553};
554
555}  // namespace gles2
556}  // namespace gpu
557
558#endif  // GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_
559