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/rect.h"
26 class TestContextSupport
;
28 class TestWebGraphicsContext3D
{
30 static scoped_ptr
<TestWebGraphicsContext3D
> Create();
32 virtual ~TestWebGraphicsContext3D();
34 void set_context_lost_callback(const base::Closure
& callback
) {
35 context_lost_callback_
= callback
;
38 virtual void reshapeWithScaleFactor(int width
,
42 virtual bool isContextLost();
44 virtual void discardFramebufferEXT(GLenum target
,
45 GLsizei num_attachments
,
46 const GLenum
* attachments
) {}
48 virtual void activeTexture(GLenum texture
) {}
49 virtual void attachShader(GLuint program
, GLuint shader
);
50 virtual void bindFramebuffer(GLenum target
, GLuint framebuffer
);
51 virtual void bindRenderbuffer(GLenum target
, GLuint renderbuffer
);
52 virtual void bindTexture(GLenum target
, GLuint texture_id
);
54 virtual void texParameteri(GLenum target
, GLenum pname
, GLint param
);
55 virtual void getTexParameteriv(GLenum target
, GLenum pname
, GLint
* value
);
56 virtual void asyncTexImage2DCHROMIUM(GLenum target
,
58 GLenum internalformat
,
64 const void* pixels
) {}
65 virtual void asyncTexSubImage2DCHROMIUM(GLenum target
,
73 const void* pixels
) {}
74 virtual void waitAsyncTexImage2DCHROMIUM(GLenum target
) {}
75 virtual void releaseTexImage2DCHROMIUM(GLenum target
, GLint image_id
) {}
77 virtual GLenum
checkFramebufferStatus(GLenum target
);
79 virtual void clear(GLbitfield mask
) {}
80 virtual void clearColor(GLclampf red
,
84 virtual void clearStencil(GLint s
) {}
85 virtual void compressedTexImage2D(GLenum target
,
87 GLenum internal_format
,
93 virtual GLint
getUniformLocation(GLuint program
, const GLchar
* name
);
94 virtual GLsizeiptr
getVertexAttribOffset(GLuint index
, GLenum pname
);
96 virtual GLboolean
isBuffer(GLuint buffer
);
97 virtual GLboolean
isEnabled(GLenum cap
);
98 virtual GLboolean
isFramebuffer(GLuint framebuffer
);
99 virtual GLboolean
isProgram(GLuint program
);
100 virtual GLboolean
isRenderbuffer(GLuint renderbuffer
);
101 virtual GLboolean
isShader(GLuint shader
);
102 virtual GLboolean
isTexture(GLuint texture
);
104 virtual void useProgram(GLuint program
);
106 virtual void viewport(GLint x
, GLint y
, GLsizei width
, GLsizei height
) {}
108 virtual void genBuffers(GLsizei count
, GLuint
* ids
);
109 virtual void genFramebuffers(GLsizei count
, GLuint
* ids
);
110 virtual void genRenderbuffers(GLsizei count
, GLuint
* ids
);
111 virtual void genTextures(GLsizei count
, GLuint
* ids
);
113 virtual void deleteBuffers(GLsizei count
, GLuint
* ids
);
114 virtual void deleteFramebuffers(GLsizei count
, GLuint
* ids
);
115 virtual void deleteRenderbuffers(GLsizei count
, GLuint
* ids
);
116 virtual void deleteTextures(GLsizei count
, GLuint
* ids
);
118 virtual GLuint
createBuffer();
119 virtual GLuint
createFramebuffer();
120 virtual GLuint
createRenderbuffer();
121 virtual GLuint
createTexture();
123 virtual void deleteBuffer(GLuint id
);
124 virtual void deleteFramebuffer(GLuint id
);
125 virtual void deleteRenderbuffer(GLuint id
);
126 virtual void deleteTexture(GLuint id
);
128 virtual GLuint
createProgram();
129 virtual GLuint
createShader(GLenum
);
130 virtual GLuint
createExternalTexture();
132 virtual void deleteProgram(GLuint id
);
133 virtual void deleteShader(GLuint id
);
135 virtual void texStorage2DEXT(GLenum target
,
137 GLuint internalformat
,
141 virtual GLuint
createQueryEXT();
142 virtual void deleteQueryEXT(GLuint query
) {}
143 virtual void beginQueryEXT(GLenum target
, GLuint query
) {}
144 virtual void endQueryEXT(GLenum target
);
145 virtual void getQueryObjectuivEXT(GLuint query
, GLenum pname
, GLuint
* params
);
147 virtual void scissor(GLint x
, GLint y
, GLsizei width
, GLsizei height
) {}
149 virtual void texImage2D(GLenum target
,
151 GLenum internalformat
,
157 const void* pixels
) {}
159 virtual void texSubImage2D(GLenum target
,
167 const void* pixels
) {}
169 virtual void genMailboxCHROMIUM(GLbyte
* mailbox
);
170 virtual void produceTextureCHROMIUM(GLenum target
,
171 const GLbyte
* mailbox
) { }
172 virtual void produceTextureDirectCHROMIUM(GLuint texture
,
174 const GLbyte
* mailbox
) {}
175 virtual void consumeTextureCHROMIUM(GLenum target
,
176 const GLbyte
* mailbox
) { }
177 virtual GLuint
createAndConsumeTextureCHROMIUM(GLenum target
,
178 const GLbyte
* mailbox
);
180 virtual void loseContextCHROMIUM(GLenum current
, GLenum other
);
182 virtual void bindTexImage2DCHROMIUM(GLenum target
, GLint image_id
) {}
184 virtual void drawArrays(GLenum mode
, GLint first
, GLsizei count
) {}
185 virtual void drawElements(GLenum mode
,
189 virtual void disable(GLenum cap
) {}
190 virtual void enable(GLenum cap
) {}
191 virtual void finish();
192 virtual void flush();
193 virtual void shallowFlushCHROMIUM() {}
195 virtual void getAttachedShaders(GLuint program
,
199 virtual GLint
getAttribLocation(GLuint program
, const GLchar
* name
);
200 virtual void getBooleanv(GLenum pname
, GLboolean
* value
) {}
201 virtual void getBufferParameteriv(GLenum target
, GLenum pname
, GLint
* value
) {
203 virtual GLenum
getError();
204 virtual void getFloatv(GLenum pname
, GLfloat
* value
) {}
205 virtual void getFramebufferAttachmentParameteriv(GLenum target
,
210 virtual void getIntegerv(GLenum pname
, GLint
* value
);
212 virtual void getProgramiv(GLuint program
, GLenum pname
, GLint
* value
);
214 virtual void getRenderbufferParameteriv(GLenum target
,
218 virtual void getShaderiv(GLuint shader
, GLenum pname
, GLint
* value
);
220 virtual void getShaderPrecisionFormat(GLenum shadertype
,
221 GLenum precisiontype
,
225 virtual void getTexParameterfv(GLenum target
, GLenum pname
, GLfloat
* value
) {}
226 virtual void getUniformfv(GLuint program
, GLint location
, GLfloat
* value
) {}
227 virtual void getUniformiv(GLuint program
, GLint location
, GLint
* value
) {}
228 virtual void getVertexAttribfv(GLuint index
, GLenum pname
, GLfloat
* value
) {}
229 virtual void getVertexAttribiv(GLuint index
, GLenum pname
, GLint
* value
) {}
231 virtual void bindBuffer(GLenum target
, GLuint buffer
);
232 virtual void bufferData(GLenum target
,
236 virtual void* mapBufferCHROMIUM(GLenum target
,
238 virtual GLboolean
unmapBufferCHROMIUM(GLenum target
);
240 virtual GLuint
createImageCHROMIUM(GLsizei width
,
242 GLenum internalformat
,
244 virtual void destroyImageCHROMIUM(GLuint image_id
);
245 virtual void getImageParameterivCHROMIUM(GLuint image_id
,
248 virtual void* mapImageCHROMIUM(GLuint image_id
);
249 virtual void unmapImageCHROMIUM(GLuint image_id
);
250 virtual void texImageIOSurface2DCHROMIUM(GLenum target
,
253 GLuint io_surface_id
,
256 virtual unsigned insertSyncPoint();
257 virtual void waitSyncPoint(unsigned sync_point
);
259 unsigned last_waited_sync_point() const { return last_waited_sync_point_
; }
261 const ContextProvider::Capabilities
& test_capabilities() const {
262 return test_capabilities_
;
265 void set_context_lost(bool context_lost
) { context_lost_
= context_lost
; }
266 void set_times_bind_texture_succeeds(int times
) {
267 times_bind_texture_succeeds_
= times
;
269 void set_times_end_query_succeeds(int times
) {
270 times_end_query_succeeds_
= times
;
273 // When set, mapImageCHROMIUM and mapBufferCHROMIUM will return NULL after
275 void set_times_map_image_chromium_succeeds(int times
) {
276 times_map_image_chromium_succeeds_
= times
;
278 void set_times_map_buffer_chromium_succeeds(int times
) {
279 times_map_buffer_chromium_succeeds_
= times
;
282 size_t NumTextures() const;
283 GLuint
TextureAt(int i
) const;
285 size_t NumUsedTextures() const { return used_textures_
.size(); }
286 bool UsedTexture(int texture
) const {
287 return ContainsKey(used_textures_
, texture
);
289 void ResetUsedTextures() { used_textures_
.clear(); }
291 void set_have_extension_io_surface(bool have
) {
292 test_capabilities_
.gpu
.iosurface
= have
;
293 test_capabilities_
.gpu
.texture_rectangle
= have
;
295 void set_have_extension_egl_image(bool have
) {
296 test_capabilities_
.gpu
.egl_image_external
= have
;
298 void set_have_post_sub_buffer(bool have
) {
299 test_capabilities_
.gpu
.post_sub_buffer
= have
;
301 void set_have_discard_framebuffer(bool have
) {
302 test_capabilities_
.gpu
.discard_framebuffer
= have
;
304 void set_support_compressed_texture_etc1(bool support
) {
305 test_capabilities_
.gpu
.texture_format_etc1
= support
;
307 void set_support_texture_format_bgra8888(bool support
) {
308 test_capabilities_
.gpu
.texture_format_bgra8888
= support
;
310 void set_support_texture_storage(bool support
) {
311 test_capabilities_
.gpu
.texture_storage
= support
;
313 void set_support_texture_usage(bool support
) {
314 test_capabilities_
.gpu
.texture_usage
= support
;
316 void set_support_sync_query(bool support
) {
317 test_capabilities_
.gpu
.sync_query
= support
;
320 // When this context is lost, all contexts in its share group are also lost.
321 void add_share_group_context(TestWebGraphicsContext3D
* context3d
) {
322 shared_contexts_
.push_back(context3d
);
325 void set_max_texture_size(int size
) { max_texture_size_
= size
; }
327 static const GLuint kExternalTextureId
;
328 virtual GLuint
NextTextureId();
329 virtual void RetireTextureId(GLuint id
);
331 virtual GLuint
NextBufferId();
332 virtual void RetireBufferId(GLuint id
);
334 virtual GLuint
NextImageId();
335 virtual void RetireImageId(GLuint id
);
337 void SetMaxTransferBufferUsageBytes(size_t max_transfer_buffer_usage_bytes
);
338 size_t max_used_transfer_buffer_usage_bytes() const {
339 return max_used_transfer_buffer_usage_bytes_
;
342 void set_test_support(TestContextSupport
* test_support
) {
343 test_support_
= test_support
;
346 int width() const { return width_
; }
347 int height() const { return height_
; }
348 bool reshape_called() const { return reshape_called_
; }
349 void clear_reshape_called() { reshape_called_
= false; }
350 float scale_factor() const { return scale_factor_
; }
358 gfx::Rect
update_rect() const { return update_rect_
; }
360 UpdateType
last_update_type() { return last_update_type_
; }
363 struct TextureTargets
{
367 void BindTexture(GLenum target
, GLuint id
);
368 void UnbindTexture(GLuint id
);
370 GLuint
BoundTexture(GLenum target
);
373 typedef base::hash_map
<GLenum
, GLuint
> TargetTextureMap
;
374 TargetTextureMap bound_textures_
;
382 scoped_ptr
<uint8
[]> pixels
;
386 DISALLOW_COPY_AND_ASSIGN(Buffer
);
393 scoped_ptr
<uint8
[]> pixels
;
396 DISALLOW_COPY_AND_ASSIGN(Image
);
399 struct Namespace
: public base::RefCountedThreadSafe
<Namespace
> {
402 // Protects all fields.
404 unsigned next_buffer_id
;
405 unsigned next_image_id
;
406 unsigned next_texture_id
;
407 base::ScopedPtrHashMap
<unsigned, Buffer
> buffers
;
408 base::ScopedPtrHashMap
<unsigned, Image
> images
;
409 OrderedTextureMap textures
;
412 friend class base::RefCountedThreadSafe
<Namespace
>;
414 DISALLOW_COPY_AND_ASSIGN(Namespace
);
417 TestWebGraphicsContext3D();
419 void CreateNamespace();
420 GLuint
BoundTextureId(GLenum target
);
421 scoped_refptr
<TestTexture
> BoundTexture(GLenum target
);
422 void CheckTextureIsBound(GLenum target
);
424 unsigned context_id_
;
425 ContextProvider::Capabilities test_capabilities_
;
426 int times_bind_texture_succeeds_
;
427 int times_end_query_succeeds_
;
429 int times_map_image_chromium_succeeds_
;
430 int times_map_buffer_chromium_succeeds_
;
431 int current_used_transfer_buffer_usage_bytes_
;
432 int max_used_transfer_buffer_usage_bytes_
;
433 base::Closure context_lost_callback_
;
434 base::hash_set
<unsigned> used_textures_
;
435 unsigned next_program_id_
;
436 base::hash_set
<unsigned> program_set_
;
437 unsigned next_shader_id_
;
438 base::hash_set
<unsigned> shader_set_
;
439 std::vector
<TestWebGraphicsContext3D
*> shared_contexts_
;
440 int max_texture_size_
;
441 bool reshape_called_
;
445 TestContextSupport
* test_support_
;
446 gfx::Rect update_rect_
;
447 UpdateType last_update_type_
;
448 unsigned next_insert_sync_point_
;
449 unsigned last_waited_sync_point_
;
451 unsigned bound_buffer_
;
452 TextureTargets texture_targets_
;
454 scoped_refptr
<Namespace
> namespace_
;
455 static Namespace
* shared_namespace_
;
457 base::WeakPtrFactory
<TestWebGraphicsContext3D
> weak_ptr_factory_
;
462 #endif // CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_