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 shallowFlushCHROMIUM() {}
210 virtual void getAttachedShaders(GLuint program
,
214 virtual GLint
getAttribLocation(GLuint program
, const GLchar
* name
);
215 virtual void getBooleanv(GLenum pname
, GLboolean
* value
) {}
216 virtual void getBufferParameteriv(GLenum target
, GLenum pname
, GLint
* value
) {
218 virtual GLenum
getError();
219 virtual void getFloatv(GLenum pname
, GLfloat
* value
) {}
220 virtual void getFramebufferAttachmentParameteriv(GLenum target
,
225 virtual void getIntegerv(GLenum pname
, GLint
* value
);
227 virtual void getProgramiv(GLuint program
, GLenum pname
, GLint
* value
);
229 virtual void getRenderbufferParameteriv(GLenum target
,
233 virtual void getShaderiv(GLuint shader
, GLenum pname
, GLint
* value
);
235 virtual void getShaderPrecisionFormat(GLenum shadertype
,
236 GLenum precisiontype
,
240 virtual void getTexParameterfv(GLenum target
, GLenum pname
, GLfloat
* value
) {}
241 virtual void getUniformfv(GLuint program
, GLint location
, GLfloat
* value
) {}
242 virtual void getUniformiv(GLuint program
, GLint location
, GLint
* value
) {}
243 virtual void getVertexAttribfv(GLuint index
, GLenum pname
, GLfloat
* value
) {}
244 virtual void getVertexAttribiv(GLuint index
, GLenum pname
, GLint
* value
) {}
246 virtual void bindBuffer(GLenum target
, GLuint buffer
);
247 virtual void bufferData(GLenum target
,
251 virtual void pixelStorei(GLenum pname
, GLint param
);
252 virtual void* mapBufferCHROMIUM(GLenum target
,
254 virtual GLboolean
unmapBufferCHROMIUM(GLenum target
);
256 virtual GLuint
createImageCHROMIUM(ClientBuffer buffer
,
259 GLenum internalformat
);
260 virtual void destroyImageCHROMIUM(GLuint image_id
);
261 virtual GLuint
createGpuMemoryBufferImageCHROMIUM(GLsizei width
,
263 GLenum internalformat
,
266 virtual void texImageIOSurface2DCHROMIUM(GLenum target
,
269 GLuint io_surface_id
,
272 virtual unsigned insertSyncPoint();
273 virtual void waitSyncPoint(unsigned sync_point
);
275 unsigned last_waited_sync_point() const { return last_waited_sync_point_
; }
277 const ContextProvider::Capabilities
& test_capabilities() const {
278 return test_capabilities_
;
281 void set_context_lost(bool context_lost
) { context_lost_
= context_lost
; }
282 void set_times_bind_texture_succeeds(int times
) {
283 times_bind_texture_succeeds_
= times
;
285 void set_times_end_query_succeeds(int times
) {
286 times_end_query_succeeds_
= times
;
289 // When set, mapBufferCHROMIUM will return NULL after this many times.
290 void set_times_map_buffer_chromium_succeeds(int times
) {
291 times_map_buffer_chromium_succeeds_
= times
;
294 size_t NumTextures() const;
295 GLuint
TextureAt(int i
) const;
297 size_t NumUsedTextures() const { return used_textures_
.size(); }
298 bool UsedTexture(int texture
) const {
299 return ContainsKey(used_textures_
, texture
);
301 void ResetUsedTextures() { used_textures_
.clear(); }
303 void set_have_extension_io_surface(bool have
) {
304 test_capabilities_
.gpu
.iosurface
= have
;
305 test_capabilities_
.gpu
.texture_rectangle
= have
;
307 void set_have_extension_egl_image(bool have
) {
308 test_capabilities_
.gpu
.egl_image_external
= have
;
310 void set_have_post_sub_buffer(bool have
) {
311 test_capabilities_
.gpu
.post_sub_buffer
= have
;
313 void set_have_discard_framebuffer(bool have
) {
314 test_capabilities_
.gpu
.discard_framebuffer
= have
;
316 void set_support_compressed_texture_etc1(bool support
) {
317 test_capabilities_
.gpu
.texture_format_etc1
= support
;
319 void set_support_texture_format_bgra8888(bool support
) {
320 test_capabilities_
.gpu
.texture_format_bgra8888
= support
;
322 void set_support_texture_storage(bool support
) {
323 test_capabilities_
.gpu
.texture_storage
= support
;
325 void set_support_texture_usage(bool support
) {
326 test_capabilities_
.gpu
.texture_usage
= support
;
328 void set_support_sync_query(bool support
) {
329 test_capabilities_
.gpu
.sync_query
= support
;
331 void set_support_image(bool support
) {
332 test_capabilities_
.gpu
.image
= support
;
334 void set_support_texture_rectangle(bool support
) {
335 test_capabilities_
.gpu
.texture_rectangle
= support
;
338 // When this context is lost, all contexts in its share group are also lost.
339 void add_share_group_context(TestWebGraphicsContext3D
* context3d
) {
340 shared_contexts_
.push_back(context3d
);
343 void set_max_texture_size(int size
) { max_texture_size_
= size
; }
345 static const GLuint kExternalTextureId
;
346 virtual GLuint
NextTextureId();
347 virtual void RetireTextureId(GLuint id
);
349 virtual GLuint
NextBufferId();
350 virtual void RetireBufferId(GLuint id
);
352 virtual GLuint
NextImageId();
353 virtual void RetireImageId(GLuint id
);
355 virtual GLuint
NextFramebufferId();
356 virtual void RetireFramebufferId(GLuint id
);
358 virtual GLuint
NextRenderbufferId();
359 virtual void RetireRenderbufferId(GLuint id
);
361 void SetMaxTransferBufferUsageBytes(size_t max_transfer_buffer_usage_bytes
);
362 size_t max_used_transfer_buffer_usage_bytes() const {
363 return max_used_transfer_buffer_usage_bytes_
;
366 void SetMaxSamples(int max_samples
);
367 void set_test_support(TestContextSupport
* test_support
) {
368 test_support_
= test_support
;
371 int width() const { return width_
; }
372 int height() const { return height_
; }
373 bool reshape_called() const { return reshape_called_
; }
374 void clear_reshape_called() { reshape_called_
= false; }
375 float scale_factor() const { return scale_factor_
; }
377 enum UpdateType
{ NO_UPDATE
= 0, PREPARE_TEXTURE
, POST_SUB_BUFFER
};
379 gfx::Rect
update_rect() const { return update_rect_
; }
381 UpdateType
last_update_type() { return last_update_type_
; }
384 struct TextureTargets
{
388 void BindTexture(GLenum target
, GLuint id
);
389 void UnbindTexture(GLuint id
);
391 GLuint
BoundTexture(GLenum target
);
394 typedef base::hash_map
<GLenum
, GLuint
> TargetTextureMap
;
395 TargetTextureMap bound_textures_
;
403 scoped_ptr
<uint8
[]> pixels
;
407 DISALLOW_COPY_AND_ASSIGN(Buffer
);
414 scoped_ptr
<uint8
[]> pixels
;
417 DISALLOW_COPY_AND_ASSIGN(Image
);
420 struct Namespace
: public base::RefCountedThreadSafe
<Namespace
> {
423 // Protects all fields.
425 unsigned next_buffer_id
;
426 unsigned next_image_id
;
427 unsigned next_texture_id
;
428 unsigned next_renderbuffer_id
;
429 base::ScopedPtrHashMap
<unsigned, scoped_ptr
<Buffer
>> buffers
;
430 base::hash_set
<unsigned> images
;
431 OrderedTextureMap textures
;
432 base::hash_set
<unsigned> renderbuffer_set
;
435 friend class base::RefCountedThreadSafe
<Namespace
>;
437 DISALLOW_COPY_AND_ASSIGN(Namespace
);
440 TestWebGraphicsContext3D();
442 void CreateNamespace();
443 GLuint
BoundTextureId(GLenum target
);
444 scoped_refptr
<TestTexture
> BoundTexture(GLenum target
);
445 scoped_refptr
<TestTexture
> UnboundTexture(GLuint texture
);
446 void CheckTextureIsBound(GLenum target
);
448 unsigned context_id_
;
449 ContextProvider::Capabilities test_capabilities_
;
450 int times_bind_texture_succeeds_
;
451 int times_end_query_succeeds_
;
453 int times_map_buffer_chromium_succeeds_
;
454 int current_used_transfer_buffer_usage_bytes_
;
455 int max_used_transfer_buffer_usage_bytes_
;
456 base::Closure context_lost_callback_
;
457 base::hash_set
<unsigned> used_textures_
;
458 unsigned next_program_id_
;
459 base::hash_set
<unsigned> program_set_
;
460 unsigned next_shader_id_
;
461 base::hash_set
<unsigned> shader_set_
;
462 unsigned next_framebuffer_id_
;
463 base::hash_set
<unsigned> framebuffer_set_
;
464 unsigned current_framebuffer_
;
465 std::vector
<TestWebGraphicsContext3D
*> shared_contexts_
;
466 int max_texture_size_
;
467 bool reshape_called_
;
471 TestContextSupport
* test_support_
;
472 gfx::Rect update_rect_
;
473 UpdateType last_update_type_
;
474 unsigned next_insert_sync_point_
;
475 unsigned last_waited_sync_point_
;
476 int unpack_alignment_
;
478 unsigned bound_buffer_
;
479 TextureTargets texture_targets_
;
481 scoped_refptr
<Namespace
> namespace_
;
482 static Namespace
* shared_namespace_
;
484 base::WeakPtrFactory
<TestWebGraphicsContext3D
> weak_ptr_factory_
;
489 #endif // CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_