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.
5 #ifndef CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_
6 #define CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/stl_util.h"
18 #include "base/synchronization/lock.h"
19 #include "cc/output/context_provider.h"
20 #include "cc/test/ordered_texture_map.h"
21 #include "cc/test/test_texture.h"
22 #include "third_party/khronos/GLES2/gl2.h"
23 #include "ui/gfx/geometry/rect.h"
25 extern "C" typedef struct _ClientBuffer
* ClientBuffer
;
28 class TestContextSupport
;
30 class TestWebGraphicsContext3D
{
32 static scoped_ptr
<TestWebGraphicsContext3D
> Create();
34 virtual ~TestWebGraphicsContext3D();
36 void set_context_lost_callback(const base::Closure
& callback
) {
37 context_lost_callback_
= callback
;
40 virtual void reshapeWithScaleFactor(int width
,
44 virtual bool isContextLost();
46 virtual void discardFramebufferEXT(GLenum target
,
47 GLsizei num_attachments
,
48 const GLenum
* attachments
) {}
50 virtual void activeTexture(GLenum texture
) {}
51 virtual void attachShader(GLuint program
, GLuint shader
);
52 virtual void bindFramebuffer(GLenum target
, GLuint framebuffer
);
53 virtual void bindRenderbuffer(GLenum target
, GLuint renderbuffer
);
54 virtual void bindTexture(GLenum target
, GLuint texture_id
);
56 virtual void texParameteri(GLenum target
, GLenum pname
, GLint param
);
57 virtual void getTexParameteriv(GLenum target
, GLenum pname
, GLint
* value
);
58 virtual void asyncTexImage2DCHROMIUM(GLenum target
,
60 GLenum internalformat
,
66 const void* pixels
) {}
67 virtual void asyncTexSubImage2DCHROMIUM(GLenum target
,
75 const void* pixels
) {}
76 virtual void waitAsyncTexImage2DCHROMIUM(GLenum target
) {}
77 virtual void releaseTexImage2DCHROMIUM(GLenum target
, GLint image_id
) {}
78 virtual void framebufferRenderbuffer(GLenum target
,
80 GLenum renderbuffertarget
,
81 GLuint renderbuffer
) {}
82 virtual void framebufferTexture2D(GLenum target
,
87 virtual void renderbufferStorage(GLenum target
,
88 GLenum internalformat
,
92 virtual GLenum
checkFramebufferStatus(GLenum target
);
94 virtual void clear(GLbitfield mask
) {}
95 virtual void clearColor(GLclampf red
,
99 virtual void clearStencil(GLint s
) {}
100 virtual void compressedTexImage2D(GLenum target
,
102 GLenum internal_format
,
108 virtual GLint
getUniformLocation(GLuint program
, const GLchar
* name
);
109 virtual GLsizeiptr
getVertexAttribOffset(GLuint index
, GLenum pname
);
111 virtual GLboolean
isBuffer(GLuint buffer
);
112 virtual GLboolean
isEnabled(GLenum cap
);
113 virtual GLboolean
isFramebuffer(GLuint framebuffer
);
114 virtual GLboolean
isProgram(GLuint program
);
115 virtual GLboolean
isRenderbuffer(GLuint renderbuffer
);
116 virtual GLboolean
isShader(GLuint shader
);
117 virtual GLboolean
isTexture(GLuint texture
);
119 virtual void useProgram(GLuint program
);
121 virtual void viewport(GLint x
, GLint y
, GLsizei width
, GLsizei height
) {}
123 virtual void genBuffers(GLsizei count
, GLuint
* ids
);
124 virtual void genFramebuffers(GLsizei count
, GLuint
* ids
);
125 virtual void genRenderbuffers(GLsizei count
, GLuint
* ids
);
126 virtual void genTextures(GLsizei count
, GLuint
* ids
);
128 virtual void deleteBuffers(GLsizei count
, GLuint
* ids
);
129 virtual void deleteFramebuffers(GLsizei count
, GLuint
* ids
);
130 virtual void deleteRenderbuffers(GLsizei count
, GLuint
* ids
);
131 virtual void deleteTextures(GLsizei count
, GLuint
* ids
);
133 virtual GLuint
createBuffer();
134 virtual GLuint
createFramebuffer();
135 virtual GLuint
createRenderbuffer();
136 virtual GLuint
createTexture();
138 virtual void deleteBuffer(GLuint id
);
139 virtual void deleteFramebuffer(GLuint id
);
140 virtual void deleteRenderbuffer(GLuint id
);
141 virtual void deleteTexture(GLuint id
);
143 virtual GLuint
createProgram();
144 virtual GLuint
createShader(GLenum
);
145 virtual GLuint
createExternalTexture();
147 virtual void deleteProgram(GLuint id
);
148 virtual void deleteShader(GLuint id
);
150 virtual void texStorage2DEXT(GLenum target
,
152 GLuint internalformat
,
156 virtual GLuint
createQueryEXT();
157 virtual void deleteQueryEXT(GLuint query
) {}
158 virtual void beginQueryEXT(GLenum target
, GLuint query
) {}
159 virtual void endQueryEXT(GLenum target
);
160 virtual void getQueryObjectuivEXT(GLuint query
, GLenum pname
, GLuint
* params
);
162 virtual void scissor(GLint x
, GLint y
, GLsizei width
, GLsizei height
) {}
164 virtual void texImage2D(GLenum target
,
166 GLenum internalformat
,
172 const void* pixels
) {}
174 virtual void texSubImage2D(GLenum target
,
182 const void* pixels
) {}
184 virtual void genMailboxCHROMIUM(GLbyte
* mailbox
);
185 virtual void produceTextureCHROMIUM(GLenum target
,
186 const GLbyte
* mailbox
) { }
187 virtual void produceTextureDirectCHROMIUM(GLuint texture
,
189 const GLbyte
* mailbox
) {}
190 virtual void consumeTextureCHROMIUM(GLenum target
,
191 const GLbyte
* mailbox
) { }
192 virtual GLuint
createAndConsumeTextureCHROMIUM(GLenum target
,
193 const GLbyte
* mailbox
);
195 virtual void loseContextCHROMIUM(GLenum current
, GLenum other
);
197 virtual void bindTexImage2DCHROMIUM(GLenum target
, GLint image_id
) {}
199 virtual void drawArrays(GLenum mode
, GLint first
, GLsizei count
) {}
200 virtual void drawElements(GLenum mode
,
204 virtual void disable(GLenum cap
) {}
205 virtual void enable(GLenum cap
) {}
206 virtual void finish();
207 virtual void flush();
208 virtual void shallowFinishCHROMIUM();
209 virtual void shallowFlushCHROMIUM() {}
211 virtual void getAttachedShaders(GLuint program
,
215 virtual GLint
getAttribLocation(GLuint program
, const GLchar
* name
);
216 virtual void getBooleanv(GLenum pname
, GLboolean
* value
) {}
217 virtual void getBufferParameteriv(GLenum target
, GLenum pname
, GLint
* value
) {
219 virtual GLenum
getError();
220 virtual void getFloatv(GLenum pname
, GLfloat
* value
) {}
221 virtual void getFramebufferAttachmentParameteriv(GLenum target
,
226 virtual void getIntegerv(GLenum pname
, GLint
* value
);
228 virtual void getProgramiv(GLuint program
, GLenum pname
, GLint
* value
);
230 virtual void getRenderbufferParameteriv(GLenum target
,
234 virtual void getShaderiv(GLuint shader
, GLenum pname
, GLint
* value
);
236 virtual void getShaderPrecisionFormat(GLenum shadertype
,
237 GLenum precisiontype
,
241 virtual void getTexParameterfv(GLenum target
, GLenum pname
, GLfloat
* value
) {}
242 virtual void getUniformfv(GLuint program
, GLint location
, GLfloat
* value
) {}
243 virtual void getUniformiv(GLuint program
, GLint location
, GLint
* value
) {}
244 virtual void getVertexAttribfv(GLuint index
, GLenum pname
, GLfloat
* value
) {}
245 virtual void getVertexAttribiv(GLuint index
, GLenum pname
, GLint
* value
) {}
247 virtual void bindBuffer(GLenum target
, GLuint buffer
);
248 virtual void bufferData(GLenum target
,
252 virtual void pixelStorei(GLenum pname
, GLint param
);
253 virtual void* mapBufferCHROMIUM(GLenum target
,
255 virtual GLboolean
unmapBufferCHROMIUM(GLenum target
);
257 virtual GLuint
createImageCHROMIUM(ClientBuffer buffer
,
260 GLenum internalformat
);
261 virtual void destroyImageCHROMIUM(GLuint image_id
);
262 virtual GLuint
createGpuMemoryBufferImageCHROMIUM(GLsizei width
,
264 GLenum internalformat
,
267 virtual void texImageIOSurface2DCHROMIUM(GLenum target
,
270 GLuint io_surface_id
,
273 virtual unsigned insertSyncPoint();
274 virtual void waitSyncPoint(unsigned sync_point
);
276 unsigned last_waited_sync_point() const { return last_waited_sync_point_
; }
278 const ContextProvider::Capabilities
& test_capabilities() const {
279 return test_capabilities_
;
282 void set_context_lost(bool context_lost
) { context_lost_
= context_lost
; }
283 void set_times_bind_texture_succeeds(int times
) {
284 times_bind_texture_succeeds_
= times
;
286 void set_times_end_query_succeeds(int times
) {
287 times_end_query_succeeds_
= times
;
290 // When set, mapBufferCHROMIUM will return NULL after this many times.
291 void set_times_map_buffer_chromium_succeeds(int times
) {
292 times_map_buffer_chromium_succeeds_
= times
;
295 size_t NumTextures() const;
296 GLuint
TextureAt(int i
) const;
298 size_t NumUsedTextures() const { return used_textures_
.size(); }
299 bool UsedTexture(int texture
) const {
300 return ContainsKey(used_textures_
, texture
);
302 void ResetUsedTextures() { used_textures_
.clear(); }
304 void set_have_extension_io_surface(bool have
) {
305 test_capabilities_
.gpu
.iosurface
= have
;
306 test_capabilities_
.gpu
.texture_rectangle
= have
;
308 void set_have_extension_egl_image(bool have
) {
309 test_capabilities_
.gpu
.egl_image_external
= have
;
311 void set_have_post_sub_buffer(bool have
) {
312 test_capabilities_
.gpu
.post_sub_buffer
= have
;
314 void set_have_discard_framebuffer(bool have
) {
315 test_capabilities_
.gpu
.discard_framebuffer
= have
;
317 void set_support_compressed_texture_etc1(bool support
) {
318 test_capabilities_
.gpu
.texture_format_etc1
= support
;
320 void set_support_texture_format_bgra8888(bool support
) {
321 test_capabilities_
.gpu
.texture_format_bgra8888
= support
;
323 void set_support_texture_storage(bool support
) {
324 test_capabilities_
.gpu
.texture_storage
= support
;
326 void set_support_texture_usage(bool support
) {
327 test_capabilities_
.gpu
.texture_usage
= support
;
329 void set_support_sync_query(bool support
) {
330 test_capabilities_
.gpu
.sync_query
= support
;
332 void set_support_image(bool support
) {
333 test_capabilities_
.gpu
.image
= support
;
335 void set_support_texture_rectangle(bool support
) {
336 test_capabilities_
.gpu
.texture_rectangle
= support
;
339 // When this context is lost, all contexts in its share group are also lost.
340 void add_share_group_context(TestWebGraphicsContext3D
* context3d
) {
341 shared_contexts_
.push_back(context3d
);
344 void set_max_texture_size(int size
) { max_texture_size_
= size
; }
346 static const GLuint kExternalTextureId
;
347 virtual GLuint
NextTextureId();
348 virtual void RetireTextureId(GLuint id
);
350 virtual GLuint
NextBufferId();
351 virtual void RetireBufferId(GLuint id
);
353 virtual GLuint
NextImageId();
354 virtual void RetireImageId(GLuint id
);
356 virtual GLuint
NextFramebufferId();
357 virtual void RetireFramebufferId(GLuint id
);
359 virtual GLuint
NextRenderbufferId();
360 virtual void RetireRenderbufferId(GLuint id
);
362 void SetMaxTransferBufferUsageBytes(size_t max_transfer_buffer_usage_bytes
);
363 size_t max_used_transfer_buffer_usage_bytes() const {
364 return max_used_transfer_buffer_usage_bytes_
;
367 void SetMaxSamples(int max_samples
);
368 void set_test_support(TestContextSupport
* test_support
) {
369 test_support_
= test_support
;
372 int width() const { return width_
; }
373 int height() const { return height_
; }
374 bool reshape_called() const { return reshape_called_
; }
375 void clear_reshape_called() { reshape_called_
= false; }
376 float scale_factor() const { return scale_factor_
; }
378 enum UpdateType
{ NO_UPDATE
= 0, PREPARE_TEXTURE
, POST_SUB_BUFFER
};
380 gfx::Rect
update_rect() const { return update_rect_
; }
382 UpdateType
last_update_type() { return last_update_type_
; }
385 struct TextureTargets
{
389 void BindTexture(GLenum target
, GLuint id
);
390 void UnbindTexture(GLuint id
);
392 GLuint
BoundTexture(GLenum target
);
395 typedef base::hash_map
<GLenum
, GLuint
> TargetTextureMap
;
396 TargetTextureMap bound_textures_
;
404 scoped_ptr
<uint8
[]> pixels
;
408 DISALLOW_COPY_AND_ASSIGN(Buffer
);
415 scoped_ptr
<uint8
[]> pixels
;
418 DISALLOW_COPY_AND_ASSIGN(Image
);
421 struct Namespace
: public base::RefCountedThreadSafe
<Namespace
> {
424 // Protects all fields.
426 unsigned next_buffer_id
;
427 unsigned next_image_id
;
428 unsigned next_texture_id
;
429 unsigned next_renderbuffer_id
;
430 base::ScopedPtrHashMap
<unsigned, scoped_ptr
<Buffer
>> buffers
;
431 base::hash_set
<unsigned> images
;
432 OrderedTextureMap textures
;
433 base::hash_set
<unsigned> renderbuffer_set
;
436 friend class base::RefCountedThreadSafe
<Namespace
>;
438 DISALLOW_COPY_AND_ASSIGN(Namespace
);
441 TestWebGraphicsContext3D();
443 void CreateNamespace();
444 GLuint
BoundTextureId(GLenum target
);
445 scoped_refptr
<TestTexture
> BoundTexture(GLenum target
);
446 scoped_refptr
<TestTexture
> UnboundTexture(GLuint texture
);
447 void CheckTextureIsBound(GLenum target
);
449 unsigned context_id_
;
450 ContextProvider::Capabilities test_capabilities_
;
451 int times_bind_texture_succeeds_
;
452 int times_end_query_succeeds_
;
454 int times_map_buffer_chromium_succeeds_
;
455 int current_used_transfer_buffer_usage_bytes_
;
456 int max_used_transfer_buffer_usage_bytes_
;
457 base::Closure context_lost_callback_
;
458 base::hash_set
<unsigned> used_textures_
;
459 unsigned next_program_id_
;
460 base::hash_set
<unsigned> program_set_
;
461 unsigned next_shader_id_
;
462 base::hash_set
<unsigned> shader_set_
;
463 unsigned next_framebuffer_id_
;
464 base::hash_set
<unsigned> framebuffer_set_
;
465 unsigned current_framebuffer_
;
466 std::vector
<TestWebGraphicsContext3D
*> shared_contexts_
;
467 int max_texture_size_
;
468 bool reshape_called_
;
472 TestContextSupport
* test_support_
;
473 gfx::Rect update_rect_
;
474 UpdateType last_update_type_
;
475 unsigned next_insert_sync_point_
;
476 unsigned last_waited_sync_point_
;
477 int unpack_alignment_
;
479 unsigned bound_buffer_
;
480 TextureTargets texture_targets_
;
482 scoped_refptr
<Namespace
> namespace_
;
483 static Namespace
* shared_namespace_
;
485 base::WeakPtrFactory
<TestWebGraphicsContext3D
> weak_ptr_factory_
;
490 #endif // CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_