OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_ | |
6 #define CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_ | |
7 | |
8 #include <vector> | |
9 | |
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" | |
24 | |
25 extern "C" typedef struct _ClientBuffer* ClientBuffer; | |
26 | |
27 namespace cc { | |
28 class TestContextSupport; | |
29 | |
30 class TestWebGraphicsContext3D { | |
31 public: | |
32 static scoped_ptr<TestWebGraphicsContext3D> Create(); | |
33 | |
34 virtual ~TestWebGraphicsContext3D(); | |
35 | |
36 void set_context_lost_callback(const base::Closure& callback) { | |
37 context_lost_callback_ = callback; | |
38 } | |
39 | |
40 virtual void reshapeWithScaleFactor(int width, | |
41 int height, | |
42 float scale_factor); | |
43 | |
44 virtual bool isContextLost(); | |
45 | |
46 virtual void discardFramebufferEXT(GLenum target, | |
47 GLsizei num_attachments, | |
48 const GLenum* attachments) {} | |
49 | |
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); | |
55 | |
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, | |
59 GLint level, | |
60 GLenum internalformat, | |
61 GLsizei width, | |
62 GLsizei height, | |
63 GLint border, | |
64 GLenum format, | |
65 GLenum type, | |
66 const void* pixels) {} | |
67 virtual void asyncTexSubImage2DCHROMIUM(GLenum target, | |
68 GLint level, | |
69 GLint xoffset, | |
70 GLint yoffset, | |
71 GLsizei width, | |
72 GLsizei height, | |
73 GLenum format, | |
74 GLenum type, | |
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, | |
79 GLenum attachment, | |
80 GLenum renderbuffertarget, | |
81 GLuint renderbuffer) {} | |
82 virtual void framebufferTexture2D(GLenum target, | |
83 GLenum attachment, | |
84 GLenum textarget, | |
85 GLuint texture, | |
86 GLint level) {} | |
87 virtual void renderbufferStorage(GLenum target, | |
88 GLenum internalformat, | |
89 GLsizei width, | |
90 GLsizei height) {} | |
91 | |
92 virtual GLenum checkFramebufferStatus(GLenum target); | |
93 | |
94 virtual void clear(GLbitfield mask) {} | |
95 virtual void clearColor(GLclampf red, | |
96 GLclampf green, | |
97 GLclampf blue, | |
98 GLclampf alpha) {} | |
99 virtual void clearStencil(GLint s) {} | |
100 virtual void compressedTexImage2D(GLenum target, | |
101 GLint level, | |
102 GLenum internal_format, | |
103 GLsizei width, | |
104 GLsizei height, | |
105 GLint border, | |
106 GLsizei image_size, | |
107 const void* data) {} | |
108 virtual GLint getUniformLocation(GLuint program, const GLchar* name); | |
109 virtual GLsizeiptr getVertexAttribOffset(GLuint index, GLenum pname); | |
110 | |
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); | |
118 | |
119 virtual void useProgram(GLuint program); | |
120 | |
121 virtual void viewport(GLint x, GLint y, GLsizei width, GLsizei height) {} | |
122 | |
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); | |
127 | |
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); | |
132 | |
133 virtual GLuint createBuffer(); | |
134 virtual GLuint createFramebuffer(); | |
135 virtual GLuint createRenderbuffer(); | |
136 virtual GLuint createTexture(); | |
137 | |
138 virtual void deleteBuffer(GLuint id); | |
139 virtual void deleteFramebuffer(GLuint id); | |
140 virtual void deleteRenderbuffer(GLuint id); | |
141 virtual void deleteTexture(GLuint id); | |
142 | |
143 virtual GLuint createProgram(); | |
144 virtual GLuint createShader(GLenum); | |
145 virtual GLuint createExternalTexture(); | |
146 | |
147 virtual void deleteProgram(GLuint id); | |
148 virtual void deleteShader(GLuint id); | |
149 | |
150 virtual void texStorage2DEXT(GLenum target, | |
151 GLint levels, | |
152 GLuint internalformat, | |
153 GLint width, | |
154 GLint height) {} | |
155 | |
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); | |
161 | |
162 virtual void scissor(GLint x, GLint y, GLsizei width, GLsizei height) {} | |
163 | |
164 virtual void texImage2D(GLenum target, | |
165 GLint level, | |
166 GLenum internalformat, | |
167 GLsizei width, | |
168 GLsizei height, | |
169 GLint border, | |
170 GLenum format, | |
171 GLenum type, | |
172 const void* pixels) {} | |
173 | |
174 virtual void texSubImage2D(GLenum target, | |
175 GLint level, | |
176 GLint xoffset, | |
177 GLint yoffset, | |
178 GLsizei width, | |
179 GLsizei height, | |
180 GLenum format, | |
181 GLenum type, | |
182 const void* pixels) {} | |
183 | |
184 virtual void genMailboxCHROMIUM(GLbyte* mailbox); | |
185 virtual void produceTextureCHROMIUM(GLenum target, | |
186 const GLbyte* mailbox) { } | |
187 virtual void produceTextureDirectCHROMIUM(GLuint texture, | |
188 GLenum target, | |
189 const GLbyte* mailbox) {} | |
190 virtual void consumeTextureCHROMIUM(GLenum target, | |
191 const GLbyte* mailbox) { } | |
192 virtual GLuint createAndConsumeTextureCHROMIUM(GLenum target, | |
193 const GLbyte* mailbox); | |
194 | |
195 virtual void loseContextCHROMIUM(GLenum current, GLenum other); | |
196 | |
197 virtual void bindTexImage2DCHROMIUM(GLenum target, GLint image_id) {} | |
198 | |
199 virtual void drawArrays(GLenum mode, GLint first, GLsizei count) {} | |
200 virtual void drawElements(GLenum mode, | |
201 GLsizei count, | |
202 GLenum type, | |
203 GLintptr offset) {} | |
204 virtual void disable(GLenum cap) {} | |
205 virtual void enable(GLenum cap) {} | |
206 virtual void finish(); | |
207 virtual void flush(); | |
208 virtual void shallowFlushCHROMIUM() {} | |
209 | |
210 virtual void getAttachedShaders(GLuint program, | |
211 GLsizei max_count, | |
212 GLsizei* count, | |
213 GLuint* shaders) {} | |
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) { | |
217 } | |
218 virtual GLenum getError(); | |
219 virtual void getFloatv(GLenum pname, GLfloat* value) {} | |
220 virtual void getFramebufferAttachmentParameteriv(GLenum target, | |
221 GLenum attachment, | |
222 GLenum pname, | |
223 GLint* value) {} | |
224 | |
225 virtual void getIntegerv(GLenum pname, GLint* value); | |
226 | |
227 virtual void getProgramiv(GLuint program, GLenum pname, GLint* value); | |
228 | |
229 virtual void getRenderbufferParameteriv(GLenum target, | |
230 GLenum pname, | |
231 GLint* value) {} | |
232 | |
233 virtual void getShaderiv(GLuint shader, GLenum pname, GLint* value); | |
234 | |
235 virtual void getShaderPrecisionFormat(GLenum shadertype, | |
236 GLenum precisiontype, | |
237 GLint* range, | |
238 GLint* precision); | |
239 | |
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) {} | |
245 | |
246 virtual void bindBuffer(GLenum target, GLuint buffer); | |
247 virtual void bufferData(GLenum target, | |
248 GLsizeiptr size, | |
249 const void* data, | |
250 GLenum usage); | |
251 virtual void pixelStorei(GLenum pname, GLint param); | |
252 virtual void* mapBufferCHROMIUM(GLenum target, | |
253 GLenum access); | |
254 virtual GLboolean unmapBufferCHROMIUM(GLenum target); | |
255 | |
256 virtual GLuint createImageCHROMIUM(ClientBuffer buffer, | |
257 GLsizei width, | |
258 GLsizei height, | |
259 GLenum internalformat); | |
260 virtual void destroyImageCHROMIUM(GLuint image_id); | |
261 virtual GLuint createGpuMemoryBufferImageCHROMIUM(GLsizei width, | |
262 GLsizei height, | |
263 GLenum internalformat, | |
264 GLenum usage); | |
265 | |
266 virtual void texImageIOSurface2DCHROMIUM(GLenum target, | |
267 GLsizei width, | |
268 GLsizei height, | |
269 GLuint io_surface_id, | |
270 GLuint plane) {} | |
271 | |
272 virtual unsigned insertSyncPoint(); | |
273 virtual void waitSyncPoint(unsigned sync_point); | |
274 | |
275 unsigned last_waited_sync_point() const { return last_waited_sync_point_; } | |
276 | |
277 const ContextProvider::Capabilities& test_capabilities() const { | |
278 return test_capabilities_; | |
279 } | |
280 | |
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; | |
284 } | |
285 void set_times_end_query_succeeds(int times) { | |
286 times_end_query_succeeds_ = times; | |
287 } | |
288 | |
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; | |
292 } | |
293 | |
294 size_t NumTextures() const; | |
295 GLuint TextureAt(int i) const; | |
296 | |
297 size_t NumUsedTextures() const { return used_textures_.size(); } | |
298 bool UsedTexture(int texture) const { | |
299 return ContainsKey(used_textures_, texture); | |
300 } | |
301 void ResetUsedTextures() { used_textures_.clear(); } | |
302 | |
303 void set_have_extension_io_surface(bool have) { | |
304 test_capabilities_.gpu.iosurface = have; | |
305 test_capabilities_.gpu.texture_rectangle = have; | |
306 } | |
307 void set_have_extension_egl_image(bool have) { | |
308 test_capabilities_.gpu.egl_image_external = have; | |
309 } | |
310 void set_have_post_sub_buffer(bool have) { | |
311 test_capabilities_.gpu.post_sub_buffer = have; | |
312 } | |
313 void set_have_discard_framebuffer(bool have) { | |
314 test_capabilities_.gpu.discard_framebuffer = have; | |
315 } | |
316 void set_support_compressed_texture_etc1(bool support) { | |
317 test_capabilities_.gpu.texture_format_etc1 = support; | |
318 } | |
319 void set_support_texture_format_bgra8888(bool support) { | |
320 test_capabilities_.gpu.texture_format_bgra8888 = support; | |
321 } | |
322 void set_support_texture_storage(bool support) { | |
323 test_capabilities_.gpu.texture_storage = support; | |
324 } | |
325 void set_support_texture_usage(bool support) { | |
326 test_capabilities_.gpu.texture_usage = support; | |
327 } | |
328 void set_support_sync_query(bool support) { | |
329 test_capabilities_.gpu.sync_query = support; | |
330 } | |
331 void set_support_image(bool support) { | |
332 test_capabilities_.gpu.image = support; | |
333 } | |
334 void set_support_texture_rectangle(bool support) { | |
335 test_capabilities_.gpu.texture_rectangle = support; | |
336 } | |
337 | |
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); | |
341 } | |
342 | |
343 void set_max_texture_size(int size) { max_texture_size_ = size; } | |
344 | |
345 static const GLuint kExternalTextureId; | |
346 virtual GLuint NextTextureId(); | |
347 virtual void RetireTextureId(GLuint id); | |
348 | |
349 virtual GLuint NextBufferId(); | |
350 virtual void RetireBufferId(GLuint id); | |
351 | |
352 virtual GLuint NextImageId(); | |
353 virtual void RetireImageId(GLuint id); | |
354 | |
355 virtual GLuint NextFramebufferId(); | |
356 virtual void RetireFramebufferId(GLuint id); | |
357 | |
358 virtual GLuint NextRenderbufferId(); | |
359 virtual void RetireRenderbufferId(GLuint id); | |
360 | |
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_; | |
364 } | |
365 | |
366 void set_test_support(TestContextSupport* test_support) { | |
367 test_support_ = test_support; | |
368 } | |
369 | |
370 int width() const { return width_; } | |
371 int height() const { return height_; } | |
372 bool reshape_called() const { return reshape_called_; } | |
373 void clear_reshape_called() { reshape_called_ = false; } | |
374 float scale_factor() const { return scale_factor_; } | |
375 | |
376 enum UpdateType { NO_UPDATE = 0, PREPARE_TEXTURE, POST_SUB_BUFFER }; | |
377 | |
378 gfx::Rect update_rect() const { return update_rect_; } | |
379 | |
380 UpdateType last_update_type() { return last_update_type_; } | |
381 | |
382 protected: | |
383 struct TextureTargets { | |
384 TextureTargets(); | |
385 ~TextureTargets(); | |
386 | |
387 void BindTexture(GLenum target, GLuint id); | |
388 void UnbindTexture(GLuint id); | |
389 | |
390 GLuint BoundTexture(GLenum target); | |
391 | |
392 private: | |
393 typedef base::hash_map<GLenum, GLuint> TargetTextureMap; | |
394 TargetTextureMap bound_textures_; | |
395 }; | |
396 | |
397 struct Buffer { | |
398 Buffer(); | |
399 ~Buffer(); | |
400 | |
401 GLenum target; | |
402 scoped_ptr<uint8[]> pixels; | |
403 size_t size; | |
404 | |
405 private: | |
406 DISALLOW_COPY_AND_ASSIGN(Buffer); | |
407 }; | |
408 | |
409 struct Image { | |
410 Image(); | |
411 ~Image(); | |
412 | |
413 scoped_ptr<uint8[]> pixels; | |
414 | |
415 private: | |
416 DISALLOW_COPY_AND_ASSIGN(Image); | |
417 }; | |
418 | |
419 struct Namespace : public base::RefCountedThreadSafe<Namespace> { | |
420 Namespace(); | |
421 | |
422 // Protects all fields. | |
423 base::Lock lock; | |
424 unsigned next_buffer_id; | |
425 unsigned next_image_id; | |
426 unsigned next_texture_id; | |
427 unsigned next_renderbuffer_id; | |
428 base::ScopedPtrHashMap<unsigned, Buffer> buffers; | |
429 base::hash_set<unsigned> images; | |
430 OrderedTextureMap textures; | |
431 base::hash_set<unsigned> renderbuffer_set; | |
432 | |
433 private: | |
434 friend class base::RefCountedThreadSafe<Namespace>; | |
435 ~Namespace(); | |
436 DISALLOW_COPY_AND_ASSIGN(Namespace); | |
437 }; | |
438 | |
439 TestWebGraphicsContext3D(); | |
440 | |
441 void CreateNamespace(); | |
442 GLuint BoundTextureId(GLenum target); | |
443 scoped_refptr<TestTexture> BoundTexture(GLenum target); | |
444 scoped_refptr<TestTexture> UnboundTexture(GLuint texture); | |
445 void CheckTextureIsBound(GLenum target); | |
446 | |
447 unsigned context_id_; | |
448 ContextProvider::Capabilities test_capabilities_; | |
449 int times_bind_texture_succeeds_; | |
450 int times_end_query_succeeds_; | |
451 bool context_lost_; | |
452 int times_map_buffer_chromium_succeeds_; | |
453 int current_used_transfer_buffer_usage_bytes_; | |
454 int max_used_transfer_buffer_usage_bytes_; | |
455 base::Closure context_lost_callback_; | |
456 base::hash_set<unsigned> used_textures_; | |
457 unsigned next_program_id_; | |
458 base::hash_set<unsigned> program_set_; | |
459 unsigned next_shader_id_; | |
460 base::hash_set<unsigned> shader_set_; | |
461 unsigned next_framebuffer_id_; | |
462 base::hash_set<unsigned> framebuffer_set_; | |
463 unsigned current_framebuffer_; | |
464 std::vector<TestWebGraphicsContext3D*> shared_contexts_; | |
465 int max_texture_size_; | |
466 bool reshape_called_; | |
467 int width_; | |
468 int height_; | |
469 float scale_factor_; | |
470 TestContextSupport* test_support_; | |
471 gfx::Rect update_rect_; | |
472 UpdateType last_update_type_; | |
473 unsigned next_insert_sync_point_; | |
474 unsigned last_waited_sync_point_; | |
475 int unpack_alignment_; | |
476 | |
477 unsigned bound_buffer_; | |
478 TextureTargets texture_targets_; | |
479 | |
480 scoped_refptr<Namespace> namespace_; | |
481 static Namespace* shared_namespace_; | |
482 | |
483 base::WeakPtrFactory<TestWebGraphicsContext3D> weak_ptr_factory_; | |
484 }; | |
485 | |
486 } // namespace cc | |
487 | |
488 #endif // CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_ | |
OLD | NEW |