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