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