Avoid lossy time stamp conversions when caching metadata.
[chromium-blink-merge.git] / cc / test / test_web_graphics_context_3d.h
blob31182f81fd1eaa501ebbecaee71cb7ff64083234
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_
8 #include <vector>
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;
27 namespace cc {
28 class TestContextSupport;
30 class TestWebGraphicsContext3D {
31 public:
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,
41 int height,
42 float scale_factor);
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,
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) {}
92 virtual GLenum checkFramebufferStatus(GLenum target);
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);
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,
151 GLint levels,
152 GLuint internalformat,
153 GLint width,
154 GLint height) {}
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,
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) {}
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) {}
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);
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,
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() {}
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) {
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) {}
225 virtual void getIntegerv(GLenum pname, GLint* value);
227 virtual void getProgramiv(GLuint program, GLenum pname, GLint* value);
229 virtual void getRenderbufferParameteriv(GLenum target,
230 GLenum pname,
231 GLint* value) {}
233 virtual void getShaderiv(GLuint shader, GLenum pname, GLint* value);
235 virtual void getShaderPrecisionFormat(GLenum shadertype,
236 GLenum precisiontype,
237 GLint* range,
238 GLint* precision);
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,
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);
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);
266 virtual void texImageIOSurface2DCHROMIUM(GLenum target,
267 GLsizei width,
268 GLsizei height,
269 GLuint io_surface_id,
270 GLuint plane) {}
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 set_test_support(TestContextSupport* test_support) {
367 test_support_ = test_support;
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_; }
376 enum UpdateType {
377 NoUpdate = 0,
378 PrepareTexture,
379 PostSubBuffer
382 gfx::Rect update_rect() const { return update_rect_; }
384 UpdateType last_update_type() { return last_update_type_; }
386 protected:
387 struct TextureTargets {
388 TextureTargets();
389 ~TextureTargets();
391 void BindTexture(GLenum target, GLuint id);
392 void UnbindTexture(GLuint id);
394 GLuint BoundTexture(GLenum target);
396 private:
397 typedef base::hash_map<GLenum, GLuint> TargetTextureMap;
398 TargetTextureMap bound_textures_;
401 struct Buffer {
402 Buffer();
403 ~Buffer();
405 GLenum target;
406 scoped_ptr<uint8[]> pixels;
407 size_t size;
409 private:
410 DISALLOW_COPY_AND_ASSIGN(Buffer);
413 struct Image {
414 Image();
415 ~Image();
417 scoped_ptr<uint8[]> pixels;
419 private:
420 DISALLOW_COPY_AND_ASSIGN(Image);
423 struct Namespace : public base::RefCountedThreadSafe<Namespace> {
424 Namespace();
426 // Protects all fields.
427 base::Lock lock;
428 unsigned next_buffer_id;
429 unsigned next_image_id;
430 unsigned next_texture_id;
431 unsigned next_renderbuffer_id;
432 base::ScopedPtrHashMap<unsigned, Buffer> buffers;
433 base::hash_set<unsigned> images;
434 OrderedTextureMap textures;
435 base::hash_set<unsigned> renderbuffer_set;
437 private:
438 friend class base::RefCountedThreadSafe<Namespace>;
439 ~Namespace();
440 DISALLOW_COPY_AND_ASSIGN(Namespace);
443 TestWebGraphicsContext3D();
445 void CreateNamespace();
446 GLuint BoundTextureId(GLenum target);
447 scoped_refptr<TestTexture> BoundTexture(GLenum target);
448 void CheckTextureIsBound(GLenum target);
450 unsigned context_id_;
451 ContextProvider::Capabilities test_capabilities_;
452 int times_bind_texture_succeeds_;
453 int times_end_query_succeeds_;
454 bool context_lost_;
455 int times_map_buffer_chromium_succeeds_;
456 int current_used_transfer_buffer_usage_bytes_;
457 int max_used_transfer_buffer_usage_bytes_;
458 base::Closure context_lost_callback_;
459 base::hash_set<unsigned> used_textures_;
460 unsigned next_program_id_;
461 base::hash_set<unsigned> program_set_;
462 unsigned next_shader_id_;
463 base::hash_set<unsigned> shader_set_;
464 unsigned next_framebuffer_id_;
465 base::hash_set<unsigned> framebuffer_set_;
466 unsigned current_framebuffer_;
467 std::vector<TestWebGraphicsContext3D*> shared_contexts_;
468 int max_texture_size_;
469 bool reshape_called_;
470 int width_;
471 int height_;
472 float scale_factor_;
473 TestContextSupport* test_support_;
474 gfx::Rect update_rect_;
475 UpdateType last_update_type_;
476 unsigned next_insert_sync_point_;
477 unsigned last_waited_sync_point_;
478 int unpack_alignment_;
480 unsigned bound_buffer_;
481 TextureTargets texture_targets_;
483 scoped_refptr<Namespace> namespace_;
484 static Namespace* shared_namespace_;
486 base::WeakPtrFactory<TestWebGraphicsContext3D> weak_ptr_factory_;
489 } // namespace cc
491 #endif // CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_