1 // Copyright 2011 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/compiler_specific.h" 11 #include "base/containers/hash_tables.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/stl_util.h" 16 #include "base/synchronization/lock.h" 17 #include "cc/base/scoped_ptr_hash_map.h" 18 #include "cc/debug/fake_web_graphics_context_3d.h" 19 #include "third_party/khronos/GLES2/gl2.h" 20 21 namespace WebKit { struct WebGraphicsMemoryAllocation; } 22 23 namespace cc { 24 25 class TestWebGraphicsContext3D : public FakeWebGraphicsContext3D { 26 public: 27 static scoped_ptr<TestWebGraphicsContext3D> Create() { 28 return make_scoped_ptr(new TestWebGraphicsContext3D()); 29 } 30 static scoped_ptr<TestWebGraphicsContext3D> Create( 31 const WebKit::WebGraphicsContext3D::Attributes& attributes) { 32 return make_scoped_ptr(new TestWebGraphicsContext3D(attributes)); 33 } 34 virtual ~TestWebGraphicsContext3D(); 35 36 virtual bool makeContextCurrent(); 37 38 virtual int width(); 39 virtual int height(); 40 41 virtual void reshapeWithScaleFactor( 42 int width, int height, float scale_factor); 43 44 virtual bool isContextLost(); 45 virtual WebKit::WGC3Denum getGraphicsResetStatusARB(); 46 47 virtual void attachShader(WebKit::WebGLId program, WebKit::WebGLId shader); 48 virtual void bindFramebuffer( 49 WebKit::WGC3Denum target, WebKit::WebGLId framebuffer); 50 virtual void bindRenderbuffer( 51 WebKit::WGC3Denum target, WebKit::WebGLId renderbuffer); 52 virtual void bindTexture( 53 WebKit::WGC3Denum target, 54 WebKit::WebGLId texture_id); 55 56 virtual WebKit::WGC3Denum checkFramebufferStatus(WebKit::WGC3Denum target); 57 58 virtual Attributes getContextAttributes(); 59 60 virtual WebKit::WebString getString(WebKit::WGC3Denum name); 61 virtual WebKit::WGC3Dint getUniformLocation( 62 WebKit::WebGLId program, 63 const WebKit::WGC3Dchar* name); 64 virtual WebKit::WGC3Dsizeiptr getVertexAttribOffset( 65 WebKit::WGC3Duint index, 66 WebKit::WGC3Denum pname); 67 68 virtual WebKit::WGC3Dboolean isBuffer(WebKit::WebGLId buffer); 69 virtual WebKit::WGC3Dboolean isEnabled(WebKit::WGC3Denum cap); 70 virtual WebKit::WGC3Dboolean isFramebuffer(WebKit::WebGLId framebuffer); 71 virtual WebKit::WGC3Dboolean isProgram(WebKit::WebGLId program); 72 virtual WebKit::WGC3Dboolean isRenderbuffer(WebKit::WebGLId renderbuffer); 73 virtual WebKit::WGC3Dboolean isShader(WebKit::WebGLId shader); 74 virtual WebKit::WGC3Dboolean isTexture(WebKit::WebGLId texture); 75 76 virtual void useProgram(WebKit::WebGLId program); 77 78 virtual WebKit::WebGLId createBuffer(); 79 virtual WebKit::WebGLId createFramebuffer(); 80 virtual WebKit::WebGLId createProgram(); 81 virtual WebKit::WebGLId createRenderbuffer(); 82 virtual WebKit::WebGLId createShader(WebKit::WGC3Denum); 83 virtual WebKit::WebGLId createTexture(); 84 85 virtual void deleteBuffer(WebKit::WebGLId id); 86 virtual void deleteFramebuffer(WebKit::WebGLId id); 87 virtual void deleteProgram(WebKit::WebGLId id); 88 virtual void deleteRenderbuffer(WebKit::WebGLId id); 89 virtual void deleteShader(WebKit::WebGLId id); 90 virtual void deleteTexture(WebKit::WebGLId texture_id); 91 92 virtual void endQueryEXT(WebKit::WGC3Denum target); 93 virtual void getQueryObjectuivEXT( 94 WebKit::WebGLId query, 95 WebKit::WGC3Denum pname, 96 WebKit::WGC3Duint* params); 97 98 virtual void getIntegerv( 99 WebKit::WGC3Denum pname, 100 WebKit::WGC3Dint* value); 101 102 virtual void genMailboxCHROMIUM(WebKit::WGC3Dbyte* mailbox); 103 virtual void produceTextureCHROMIUM(WebKit::WGC3Denum target, 104 const WebKit::WGC3Dbyte* mailbox) { } 105 virtual void consumeTextureCHROMIUM(WebKit::WGC3Denum target, 106 const WebKit::WGC3Dbyte* mailbox) { } 107 108 virtual void setContextLostCallback( 109 WebGraphicsContextLostCallback* callback); 110 111 virtual void loseContextCHROMIUM(WebKit::WGC3Denum current, 112 WebKit::WGC3Denum other); 113 114 // Takes ownership of the |callback|. 115 virtual void signalSyncPoint(unsigned sync_point, 116 WebGraphicsSyncPointCallback* callback); 117 virtual void signalQuery(WebKit::WebGLId query, 118 WebGraphicsSyncPointCallback* callback); 119 120 virtual void setSwapBuffersCompleteCallbackCHROMIUM( 121 WebGraphicsSwapBuffersCompleteCallbackCHROMIUM* callback); 122 123 virtual void setMemoryAllocationChangedCallbackCHROMIUM( 124 WebGraphicsMemoryAllocationChangedCallbackCHROMIUM* callback); 125 126 virtual void prepareTexture(); 127 virtual void finish(); 128 virtual void flush(); 129 130 virtual void bindBuffer(WebKit::WGC3Denum target, WebKit::WebGLId buffer); 131 virtual void bufferData(WebKit::WGC3Denum target, 132 WebKit::WGC3Dsizeiptr size, 133 const void* data, 134 WebKit::WGC3Denum usage); 135 virtual void* mapBufferCHROMIUM(WebKit::WGC3Denum target, 136 WebKit::WGC3Denum access); 137 virtual WebKit::WGC3Dboolean unmapBufferCHROMIUM(WebKit::WGC3Denum target); 138 139 virtual void bindTexImage2DCHROMIUM(WebKit::WGC3Denum target, 140 WebKit::WGC3Dint image_id); 141 virtual WebKit::WGC3Duint createImageCHROMIUM( 142 WebKit::WGC3Dsizei width, 143 WebKit::WGC3Dsizei height, 144 WebKit::WGC3Denum internalformat); 145 virtual void destroyImageCHROMIUM(WebKit::WGC3Duint image_id); 146 virtual void getImageParameterivCHROMIUM( 147 WebKit::WGC3Duint image_id, 148 WebKit::WGC3Denum pname, 149 WebKit::WGC3Dint* params); 150 virtual void* mapImageCHROMIUM( 151 WebKit::WGC3Duint image_id, 152 WebKit::WGC3Denum access); 153 virtual void unmapImageCHROMIUM(WebKit::WGC3Duint image_id); 154 155 // When set, MakeCurrent() will fail after this many times. 156 void set_times_make_current_succeeds(int times) { 157 times_make_current_succeeds_ = times; 158 } 159 void set_times_bind_texture_succeeds(int times) { 160 times_bind_texture_succeeds_ = times; 161 } 162 void set_times_end_query_succeeds(int times) { 163 times_end_query_succeeds_ = times; 164 } 165 void set_times_gen_mailbox_succeeds(int times) { 166 times_gen_mailbox_succeeds_ = times; 167 } 168 169 // When set, mapImageCHROMIUM and mapBufferCHROMIUM will return NULL after 170 // this many times. 171 void set_times_map_image_chromium_succeeds(int times) { 172 times_map_image_chromium_succeeds_ = times; 173 } 174 void set_times_map_buffer_chromium_succeeds(int times) { 175 times_map_buffer_chromium_succeeds_ = times; 176 } 177 178 size_t NumTextures() const; 179 WebKit::WebGLId TextureAt(int i) const; 180 181 size_t NumUsedTextures() const { return used_textures_.size(); } 182 bool UsedTexture(int texture) const { 183 return ContainsKey(used_textures_, texture); 184 } 185 void ResetUsedTextures() { used_textures_.clear(); } 186 187 void set_support_swapbuffers_complete_callback(bool support) { 188 support_swapbuffers_complete_callback_ = support; 189 } 190 void set_have_extension_io_surface(bool have) { 191 have_extension_io_surface_ = have; 192 } 193 void set_have_extension_egl_image(bool have) { 194 have_extension_egl_image_ = have; 195 } 196 197 // When this context is lost, all contexts in its share group are also lost. 198 void add_share_group_context(WebKit::WebGraphicsContext3D* context3d) { 199 shared_contexts_.push_back(context3d); 200 } 201 202 void set_max_texture_size(int size) { max_texture_size_ = size; } 203 204 static const WebKit::WebGLId kExternalTextureId; 205 virtual WebKit::WebGLId NextTextureId(); 206 207 virtual WebKit::WebGLId NextBufferId(); 208 209 virtual WebKit::WebGLId NextImageId(); 210 211 void SetMemoryAllocation(WebKit::WebGraphicsMemoryAllocation allocation); 212 213 protected: 214 struct Buffer { 215 Buffer(); 216 ~Buffer(); 217 218 WebKit::WGC3Denum target; 219 scoped_ptr<uint8[]> pixels; 220 221 private: 222 DISALLOW_COPY_AND_ASSIGN(Buffer); 223 }; 224 225 struct Image { 226 Image(); 227 ~Image(); 228 229 scoped_ptr<uint8[]> pixels; 230 231 private: 232 DISALLOW_COPY_AND_ASSIGN(Image); 233 }; 234 235 struct Namespace : public base::RefCountedThreadSafe<Namespace> { 236 Namespace(); 237 238 // Protects all fields. 239 base::Lock lock; 240 unsigned next_buffer_id; 241 unsigned next_image_id; 242 unsigned next_texture_id; 243 std::vector<WebKit::WebGLId> textures; 244 ScopedPtrHashMap<unsigned, Buffer> buffers; 245 ScopedPtrHashMap<unsigned, Image> images; 246 247 private: 248 friend class base::RefCountedThreadSafe<Namespace>; 249 ~Namespace(); 250 DISALLOW_COPY_AND_ASSIGN(Namespace); 251 }; 252 253 TestWebGraphicsContext3D(); 254 TestWebGraphicsContext3D( 255 const WebKit::WebGraphicsContext3D::Attributes& attributes); 256 257 void CallAllSyncPointCallbacks(); 258 void SwapBuffersComplete(); 259 void CreateNamespace(); 260 261 unsigned context_id_; 262 Attributes attributes_; 263 bool support_swapbuffers_complete_callback_; 264 bool have_extension_io_surface_; 265 bool have_extension_egl_image_; 266 int times_make_current_succeeds_; 267 int times_bind_texture_succeeds_; 268 int times_end_query_succeeds_; 269 int times_gen_mailbox_succeeds_; 270 bool context_lost_; 271 int times_map_image_chromium_succeeds_; 272 int times_map_buffer_chromium_succeeds_; 273 WebGraphicsContextLostCallback* context_lost_callback_; 274 WebGraphicsSwapBuffersCompleteCallbackCHROMIUM* swap_buffers_callback_; 275 WebGraphicsMemoryAllocationChangedCallbackCHROMIUM* 276 memory_allocation_changed_callback_; 277 std::vector<WebGraphicsSyncPointCallback*> sync_point_callbacks_; 278 base::hash_set<WebKit::WebGLId> used_textures_; 279 std::vector<WebKit::WebGraphicsContext3D*> shared_contexts_; 280 int max_texture_size_; 281 int width_; 282 int height_; 283 284 unsigned bound_buffer_; 285 286 scoped_refptr<Namespace> namespace_; 287 static Namespace* shared_namespace_; 288 289 base::WeakPtrFactory<TestWebGraphicsContext3D> weak_ptr_factory_; 290 }; 291 292 } // namespace cc 293 294 #endif // CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_ 295