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"
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* mapBufferCHROMIUM(GLenum target
,
253 virtual GLboolean
unmapBufferCHROMIUM(GLenum target
);
255 virtual GLuint
createImageCHROMIUM(ClientBuffer buffer
,
258 GLenum internalformat
);
259 virtual void destroyImageCHROMIUM(GLuint image_id
);
260 virtual GLuint
createGpuMemoryBufferImageCHROMIUM(GLsizei width
,
262 GLenum internalformat
,
265 virtual void texImageIOSurface2DCHROMIUM(GLenum target
,
268 GLuint io_surface_id
,
271 virtual unsigned insertSyncPoint();
272 virtual void waitSyncPoint(unsigned sync_point
);
274 unsigned last_waited_sync_point() const { return last_waited_sync_point_
; }
276 const ContextProvider::Capabilities
& test_capabilities() const {
277 return test_capabilities_
;
280 void set_context_lost(bool context_lost
) { context_lost_
= context_lost
; }
281 void set_times_bind_texture_succeeds(int times
) {
282 times_bind_texture_succeeds_
= times
;
284 void set_times_end_query_succeeds(int times
) {
285 times_end_query_succeeds_
= times
;
288 // When set, mapBufferCHROMIUM will return NULL after this many times.
289 void set_times_map_buffer_chromium_succeeds(int times
) {
290 times_map_buffer_chromium_succeeds_
= times
;
293 size_t NumTextures() const;
294 GLuint
TextureAt(int i
) const;
296 size_t NumUsedTextures() const { return used_textures_
.size(); }
297 bool UsedTexture(int texture
) const {
298 return ContainsKey(used_textures_
, texture
);
300 void ResetUsedTextures() { used_textures_
.clear(); }
302 void set_have_extension_io_surface(bool have
) {
303 test_capabilities_
.gpu
.iosurface
= have
;
304 test_capabilities_
.gpu
.texture_rectangle
= have
;
306 void set_have_extension_egl_image(bool have
) {
307 test_capabilities_
.gpu
.egl_image_external
= have
;
309 void set_have_post_sub_buffer(bool have
) {
310 test_capabilities_
.gpu
.post_sub_buffer
= have
;
312 void set_have_discard_framebuffer(bool have
) {
313 test_capabilities_
.gpu
.discard_framebuffer
= have
;
315 void set_support_compressed_texture_etc1(bool support
) {
316 test_capabilities_
.gpu
.texture_format_etc1
= support
;
318 void set_support_texture_format_bgra8888(bool support
) {
319 test_capabilities_
.gpu
.texture_format_bgra8888
= support
;
321 void set_support_texture_storage(bool support
) {
322 test_capabilities_
.gpu
.texture_storage
= support
;
324 void set_support_texture_usage(bool support
) {
325 test_capabilities_
.gpu
.texture_usage
= support
;
327 void set_support_sync_query(bool support
) {
328 test_capabilities_
.gpu
.sync_query
= support
;
331 // When this context is lost, all contexts in its share group are also lost.
332 void add_share_group_context(TestWebGraphicsContext3D
* context3d
) {
333 shared_contexts_
.push_back(context3d
);
336 void set_max_texture_size(int size
) { max_texture_size_
= size
; }
338 static const GLuint kExternalTextureId
;
339 virtual GLuint
NextTextureId();
340 virtual void RetireTextureId(GLuint id
);
342 virtual GLuint
NextBufferId();
343 virtual void RetireBufferId(GLuint id
);
345 virtual GLuint
NextImageId();
346 virtual void RetireImageId(GLuint id
);
348 void SetMaxTransferBufferUsageBytes(size_t max_transfer_buffer_usage_bytes
);
349 size_t max_used_transfer_buffer_usage_bytes() const {
350 return max_used_transfer_buffer_usage_bytes_
;
353 void set_test_support(TestContextSupport
* test_support
) {
354 test_support_
= test_support
;
357 int width() const { return width_
; }
358 int height() const { return height_
; }
359 bool reshape_called() const { return reshape_called_
; }
360 void clear_reshape_called() { reshape_called_
= false; }
361 float scale_factor() const { return scale_factor_
; }
369 gfx::Rect
update_rect() const { return update_rect_
; }
371 UpdateType
last_update_type() { return last_update_type_
; }
374 struct TextureTargets
{
378 void BindTexture(GLenum target
, GLuint id
);
379 void UnbindTexture(GLuint id
);
381 GLuint
BoundTexture(GLenum target
);
384 typedef base::hash_map
<GLenum
, GLuint
> TargetTextureMap
;
385 TargetTextureMap bound_textures_
;
393 scoped_ptr
<uint8
[]> pixels
;
397 DISALLOW_COPY_AND_ASSIGN(Buffer
);
404 scoped_ptr
<uint8
[]> pixels
;
407 DISALLOW_COPY_AND_ASSIGN(Image
);
410 struct Namespace
: public base::RefCountedThreadSafe
<Namespace
> {
413 // Protects all fields.
415 unsigned next_buffer_id
;
416 unsigned next_image_id
;
417 unsigned next_texture_id
;
418 base::ScopedPtrHashMap
<unsigned, Buffer
> buffers
;
419 base::hash_set
<unsigned> images
;
420 OrderedTextureMap textures
;
423 friend class base::RefCountedThreadSafe
<Namespace
>;
425 DISALLOW_COPY_AND_ASSIGN(Namespace
);
428 TestWebGraphicsContext3D();
430 void CreateNamespace();
431 GLuint
BoundTextureId(GLenum target
);
432 scoped_refptr
<TestTexture
> BoundTexture(GLenum target
);
433 void CheckTextureIsBound(GLenum target
);
435 unsigned context_id_
;
436 ContextProvider::Capabilities test_capabilities_
;
437 int times_bind_texture_succeeds_
;
438 int times_end_query_succeeds_
;
440 int times_map_buffer_chromium_succeeds_
;
441 int current_used_transfer_buffer_usage_bytes_
;
442 int max_used_transfer_buffer_usage_bytes_
;
443 base::Closure context_lost_callback_
;
444 base::hash_set
<unsigned> used_textures_
;
445 unsigned next_program_id_
;
446 base::hash_set
<unsigned> program_set_
;
447 unsigned next_shader_id_
;
448 base::hash_set
<unsigned> shader_set_
;
449 std::vector
<TestWebGraphicsContext3D
*> shared_contexts_
;
450 int max_texture_size_
;
451 bool reshape_called_
;
455 TestContextSupport
* test_support_
;
456 gfx::Rect update_rect_
;
457 UpdateType last_update_type_
;
458 unsigned next_insert_sync_point_
;
459 unsigned last_waited_sync_point_
;
461 unsigned bound_buffer_
;
462 TextureTargets texture_targets_
;
464 scoped_refptr
<Namespace
> namespace_
;
465 static Namespace
* shared_namespace_
;
467 base::WeakPtrFactory
<TestWebGraphicsContext3D
> weak_ptr_factory_
;
472 #endif // CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_