Home | History | Annotate | Download | only in test
      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/compiler_specific.h"
     11 #include "base/containers/hash_tables.h"
     12 #include "base/containers/scoped_ptr_hash_map.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "base/stl_util.h"
     17 #include "base/synchronization/lock.h"
     18 #include "cc/output/context_provider.h"
     19 #include "cc/test/fake_web_graphics_context_3d.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"
     24 
     25 namespace cc {
     26 class TestContextSupport;
     27 
     28 class TestWebGraphicsContext3D : public FakeWebGraphicsContext3D {
     29  public:
     30   static scoped_ptr<TestWebGraphicsContext3D> Create();
     31 
     32   virtual ~TestWebGraphicsContext3D();
     33 
     34   virtual void reshapeWithScaleFactor(
     35       int width, int height, float scale_factor);
     36 
     37   virtual bool isContextLost();
     38 
     39   virtual void attachShader(blink::WebGLId program, blink::WebGLId shader);
     40   virtual void bindFramebuffer(
     41       blink::WGC3Denum target, blink::WebGLId framebuffer);
     42   virtual void bindRenderbuffer(
     43       blink::WGC3Denum target, blink::WebGLId renderbuffer);
     44   virtual void bindTexture(
     45       blink::WGC3Denum target,
     46       blink::WebGLId texture_id);
     47 
     48   virtual void texParameteri(blink::WGC3Denum target,
     49                              blink::WGC3Denum pname,
     50                              blink::WGC3Dint param);
     51   virtual void getTexParameteriv(blink::WGC3Denum target,
     52                                  blink::WGC3Denum pname,
     53                                  blink::WGC3Dint* value);
     54 
     55   virtual blink::WGC3Denum checkFramebufferStatus(blink::WGC3Denum target);
     56 
     57   virtual Attributes getContextAttributes();
     58 
     59   virtual blink::WebString getString(blink::WGC3Denum name);
     60   virtual blink::WGC3Dint getUniformLocation(
     61       blink::WebGLId program,
     62       const blink::WGC3Dchar* name);
     63   virtual blink::WGC3Dsizeiptr getVertexAttribOffset(
     64       blink::WGC3Duint index,
     65       blink::WGC3Denum pname);
     66 
     67   virtual blink::WGC3Dboolean isBuffer(blink::WebGLId buffer);
     68   virtual blink::WGC3Dboolean isEnabled(blink::WGC3Denum cap);
     69   virtual blink::WGC3Dboolean isFramebuffer(blink::WebGLId framebuffer);
     70   virtual blink::WGC3Dboolean isProgram(blink::WebGLId program);
     71   virtual blink::WGC3Dboolean isRenderbuffer(blink::WebGLId renderbuffer);
     72   virtual blink::WGC3Dboolean isShader(blink::WebGLId shader);
     73   virtual blink::WGC3Dboolean isTexture(blink::WebGLId texture);
     74 
     75   virtual void useProgram(blink::WebGLId program);
     76 
     77   virtual void genBuffers(blink::WGC3Dsizei count, blink::WebGLId* ids);
     78   virtual void genFramebuffers(blink::WGC3Dsizei count, blink::WebGLId* ids);
     79   virtual void genRenderbuffers(blink::WGC3Dsizei count, blink::WebGLId* ids);
     80   virtual void genTextures(blink::WGC3Dsizei count, blink::WebGLId* ids);
     81 
     82   virtual void deleteBuffers(blink::WGC3Dsizei count, blink::WebGLId* ids);
     83   virtual void deleteFramebuffers(
     84       blink::WGC3Dsizei count, blink::WebGLId* ids);
     85   virtual void deleteRenderbuffers(
     86       blink::WGC3Dsizei count, blink::WebGLId* ids);
     87   virtual void deleteTextures(blink::WGC3Dsizei count, blink::WebGLId* ids);
     88 
     89   virtual blink::WebGLId createBuffer();
     90   virtual blink::WebGLId createFramebuffer();
     91   virtual blink::WebGLId createRenderbuffer();
     92   virtual blink::WebGLId createTexture();
     93 
     94   virtual void deleteBuffer(blink::WebGLId id);
     95   virtual void deleteFramebuffer(blink::WebGLId id);
     96   virtual void deleteRenderbuffer(blink::WebGLId id);
     97   virtual void deleteTexture(blink::WebGLId id);
     98 
     99   virtual blink::WebGLId createProgram();
    100   virtual blink::WebGLId createShader(blink::WGC3Denum);
    101   virtual blink::WebGLId createExternalTexture();
    102 
    103   virtual void deleteProgram(blink::WebGLId id);
    104   virtual void deleteShader(blink::WebGLId id);
    105 
    106   virtual void endQueryEXT(blink::WGC3Denum target);
    107   virtual void getQueryObjectuivEXT(
    108       blink::WebGLId query,
    109       blink::WGC3Denum pname,
    110       blink::WGC3Duint* params);
    111 
    112   virtual void getIntegerv(
    113       blink::WGC3Denum pname,
    114       blink::WGC3Dint* value);
    115 
    116   virtual void genMailboxCHROMIUM(blink::WGC3Dbyte* mailbox);
    117   virtual void produceTextureCHROMIUM(blink::WGC3Denum target,
    118                                       const blink::WGC3Dbyte* mailbox) { }
    119   virtual void consumeTextureCHROMIUM(blink::WGC3Denum target,
    120                                       const blink::WGC3Dbyte* mailbox) { }
    121 
    122   virtual void setContextLostCallback(
    123       WebGraphicsContextLostCallback* callback);
    124 
    125   virtual void loseContextCHROMIUM(blink::WGC3Denum current,
    126                                    blink::WGC3Denum other);
    127 
    128   virtual void finish();
    129   virtual void flush();
    130 
    131   virtual void bindBuffer(blink::WGC3Denum target, blink::WebGLId buffer);
    132   virtual void bufferData(blink::WGC3Denum target,
    133                           blink::WGC3Dsizeiptr size,
    134                           const void* data,
    135                           blink::WGC3Denum usage);
    136   virtual void* mapBufferCHROMIUM(blink::WGC3Denum target,
    137                                   blink::WGC3Denum access);
    138   virtual blink::WGC3Dboolean unmapBufferCHROMIUM(blink::WGC3Denum target);
    139 
    140   virtual blink::WGC3Duint createImageCHROMIUM(
    141       blink::WGC3Dsizei width,
    142       blink::WGC3Dsizei height,
    143       blink::WGC3Denum internalformat);
    144   virtual void destroyImageCHROMIUM(blink::WGC3Duint image_id);
    145   virtual void getImageParameterivCHROMIUM(
    146       blink::WGC3Duint image_id,
    147       blink::WGC3Denum pname,
    148       blink::WGC3Dint* params);
    149   virtual void* mapImageCHROMIUM(
    150       blink::WGC3Duint image_id,
    151       blink::WGC3Denum access);
    152   virtual void unmapImageCHROMIUM(blink::WGC3Duint image_id);
    153 
    154   virtual unsigned insertSyncPoint() OVERRIDE;
    155   virtual void waitSyncPoint(unsigned sync_point) OVERRIDE;
    156 
    157   unsigned last_waited_sync_point() const { return last_waited_sync_point_; }
    158 
    159   const ContextProvider::Capabilities& test_capabilities() const {
    160     return test_capabilities_;
    161   }
    162 
    163   void set_context_lost(bool context_lost) { context_lost_ = context_lost; }
    164   void set_times_bind_texture_succeeds(int times) {
    165     times_bind_texture_succeeds_ = times;
    166   }
    167   void set_times_end_query_succeeds(int times) {
    168     times_end_query_succeeds_ = times;
    169   }
    170   void set_times_gen_mailbox_succeeds(int times) {
    171     times_gen_mailbox_succeeds_ = times;
    172   }
    173 
    174   // When set, mapImageCHROMIUM and mapBufferCHROMIUM will return NULL after
    175   // this many times.
    176   void set_times_map_image_chromium_succeeds(int times) {
    177     times_map_image_chromium_succeeds_ = times;
    178   }
    179   void set_times_map_buffer_chromium_succeeds(int times) {
    180     times_map_buffer_chromium_succeeds_ = times;
    181   }
    182 
    183   size_t NumTextures() const;
    184   blink::WebGLId TextureAt(int i) const;
    185 
    186   size_t NumUsedTextures() const { return used_textures_.size(); }
    187   bool UsedTexture(int texture) const {
    188     return ContainsKey(used_textures_, texture);
    189   }
    190   void ResetUsedTextures() { used_textures_.clear(); }
    191 
    192   void set_have_extension_io_surface(bool have) {
    193     test_capabilities_.iosurface = have;
    194     test_capabilities_.texture_rectangle = have;
    195   }
    196   void set_have_extension_egl_image(bool have) {
    197     test_capabilities_.egl_image_external = have;
    198   }
    199   void set_have_post_sub_buffer(bool have) {
    200     test_capabilities_.post_sub_buffer = have;
    201   }
    202   void set_have_discard_framebuffer(bool have) {
    203     test_capabilities_.discard_framebuffer = have;
    204   }
    205   void set_support_compressed_texture_etc1(bool support) {
    206     test_capabilities_.texture_format_etc1 = support;
    207   }
    208   void set_support_texture_storage(bool support) {
    209     test_capabilities_.texture_storage = support;
    210   }
    211 
    212   // When this context is lost, all contexts in its share group are also lost.
    213   void add_share_group_context(blink::WebGraphicsContext3D* context3d) {
    214     shared_contexts_.push_back(context3d);
    215   }
    216 
    217   void set_max_texture_size(int size) { max_texture_size_ = size; }
    218 
    219   static const blink::WebGLId kExternalTextureId;
    220   virtual blink::WebGLId NextTextureId();
    221   virtual void RetireTextureId(blink::WebGLId id);
    222 
    223   virtual blink::WebGLId NextBufferId();
    224   virtual void RetireBufferId(blink::WebGLId id);
    225 
    226   virtual blink::WebGLId NextImageId();
    227   virtual void RetireImageId(blink::WebGLId id);
    228 
    229   size_t GetTransferBufferMemoryUsedBytes() const;
    230   void SetMaxTransferBufferUsageBytes(size_t max_transfer_buffer_usage_bytes);
    231   size_t GetPeakTransferBufferMemoryUsedBytes() const {
    232     return peak_transfer_buffer_memory_used_bytes_;
    233   }
    234 
    235   void set_test_support(TestContextSupport* test_support) {
    236     test_support_ = test_support;
    237   }
    238 
    239   int width() const { return width_; }
    240   int height() const { return height_; }
    241   bool reshape_called() const { return reshape_called_; }
    242   void clear_reshape_called() { reshape_called_ = false; }
    243   float scale_factor() const { return scale_factor_; }
    244 
    245   enum UpdateType {
    246     NoUpdate = 0,
    247     PrepareTexture,
    248     PostSubBuffer
    249   };
    250 
    251   gfx::Rect update_rect() const { return update_rect_; }
    252 
    253   UpdateType last_update_type() {
    254     return last_update_type_;
    255   }
    256 
    257  protected:
    258   struct TextureTargets {
    259     TextureTargets();
    260     ~TextureTargets();
    261 
    262     void BindTexture(blink::WGC3Denum target, blink::WebGLId id);
    263     void UnbindTexture(blink::WebGLId id);
    264 
    265     blink::WebGLId BoundTexture(blink::WGC3Denum target);
    266 
    267    private:
    268     typedef base::hash_map<blink::WGC3Denum, blink::WebGLId> TargetTextureMap;
    269     TargetTextureMap bound_textures_;
    270   };
    271 
    272   struct Buffer {
    273     Buffer();
    274     ~Buffer();
    275 
    276     blink::WGC3Denum target;
    277     scoped_ptr<uint8[]> pixels;
    278     size_t size;
    279 
    280    private:
    281     DISALLOW_COPY_AND_ASSIGN(Buffer);
    282   };
    283 
    284   struct Image {
    285     Image();
    286     ~Image();
    287 
    288     scoped_ptr<uint8[]> pixels;
    289 
    290    private:
    291     DISALLOW_COPY_AND_ASSIGN(Image);
    292   };
    293 
    294   struct Namespace : public base::RefCountedThreadSafe<Namespace> {
    295     Namespace();
    296 
    297     // Protects all fields.
    298     base::Lock lock;
    299     unsigned next_buffer_id;
    300     unsigned next_image_id;
    301     unsigned next_texture_id;
    302     base::ScopedPtrHashMap<unsigned, Buffer> buffers;
    303     base::ScopedPtrHashMap<unsigned, Image> images;
    304     OrderedTextureMap textures;
    305 
    306    private:
    307     friend class base::RefCountedThreadSafe<Namespace>;
    308     ~Namespace();
    309     DISALLOW_COPY_AND_ASSIGN(Namespace);
    310   };
    311 
    312   TestWebGraphicsContext3D();
    313 
    314   void CreateNamespace();
    315   blink::WebGLId BoundTextureId(blink::WGC3Denum target);
    316   scoped_refptr<TestTexture> BoundTexture(blink::WGC3Denum target);
    317   void CheckTextureIsBound(blink::WGC3Denum target);
    318 
    319   unsigned context_id_;
    320   Attributes attributes_;
    321   ContextProvider::Capabilities test_capabilities_;
    322   int times_bind_texture_succeeds_;
    323   int times_end_query_succeeds_;
    324   int times_gen_mailbox_succeeds_;
    325   bool context_lost_;
    326   int times_map_image_chromium_succeeds_;
    327   int times_map_buffer_chromium_succeeds_;
    328   WebGraphicsContextLostCallback* context_lost_callback_;
    329   base::hash_set<unsigned> used_textures_;
    330   unsigned next_program_id_;
    331   base::hash_set<unsigned> program_set_;
    332   unsigned next_shader_id_;
    333   base::hash_set<unsigned> shader_set_;
    334   std::vector<blink::WebGraphicsContext3D*> shared_contexts_;
    335   int max_texture_size_;
    336   bool reshape_called_;
    337   int width_;
    338   int height_;
    339   float scale_factor_;
    340   TestContextSupport* test_support_;
    341   gfx::Rect update_rect_;
    342   UpdateType last_update_type_;
    343   unsigned next_insert_sync_point_;
    344   unsigned last_waited_sync_point_;
    345 
    346   unsigned bound_buffer_;
    347   TextureTargets texture_targets_;
    348 
    349   size_t peak_transfer_buffer_memory_used_bytes_;
    350 
    351   scoped_refptr<Namespace> namespace_;
    352   static Namespace* shared_namespace_;
    353 
    354   base::WeakPtrFactory<TestWebGraphicsContext3D> weak_ptr_factory_;
    355 };
    356 
    357 }  // namespace cc
    358 
    359 #endif  // CC_TEST_TEST_WEB_GRAPHICS_CONTEXT_3D_H_
    360