Home | History | Annotate | Download | only in test
      1 // Copyright 2012 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_FAKE_OUTPUT_SURFACE_H_
      6 #define CC_TEST_FAKE_OUTPUT_SURFACE_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/logging.h"
     10 #include "base/time/time.h"
     11 #include "cc/output/begin_frame_args.h"
     12 #include "cc/output/compositor_frame.h"
     13 #include "cc/output/managed_memory_policy.h"
     14 #include "cc/output/output_surface.h"
     15 #include "cc/output/software_output_device.h"
     16 #include "cc/test/test_context_provider.h"
     17 #include "cc/test/test_web_graphics_context_3d.h"
     18 
     19 namespace cc {
     20 
     21 class FakeOutputSurface : public OutputSurface {
     22  public:
     23   virtual ~FakeOutputSurface();
     24 
     25   static scoped_ptr<FakeOutputSurface> Create3d() {
     26     return make_scoped_ptr(new FakeOutputSurface(
     27         TestContextProvider::Create(), false));
     28   }
     29 
     30   static scoped_ptr<FakeOutputSurface> Create3d(
     31       scoped_refptr<TestContextProvider> context_provider) {
     32     return make_scoped_ptr(new FakeOutputSurface(context_provider, false));
     33   }
     34 
     35   static scoped_ptr<FakeOutputSurface> Create3d(
     36       scoped_ptr<TestWebGraphicsContext3D> context) {
     37     return make_scoped_ptr(new FakeOutputSurface(
     38         TestContextProvider::Create(context.Pass()), false));
     39   }
     40 
     41   static scoped_ptr<FakeOutputSurface> CreateSoftware(
     42       scoped_ptr<SoftwareOutputDevice> software_device) {
     43     return make_scoped_ptr(new FakeOutputSurface(software_device.Pass(),
     44                                                  false));
     45   }
     46 
     47   static scoped_ptr<FakeOutputSurface> CreateDelegating3d() {
     48     return make_scoped_ptr(new FakeOutputSurface(
     49         TestContextProvider::Create(), true));
     50   }
     51 
     52   static scoped_ptr<FakeOutputSurface> CreateDelegating3d(
     53       scoped_refptr<TestContextProvider> context_provider) {
     54     return make_scoped_ptr(new FakeOutputSurface(context_provider, true));
     55   }
     56 
     57   static scoped_ptr<FakeOutputSurface> CreateDelegating3d(
     58       scoped_ptr<TestWebGraphicsContext3D> context) {
     59     return make_scoped_ptr(new FakeOutputSurface(
     60         TestContextProvider::Create(context.Pass()), true));
     61   }
     62 
     63   static scoped_ptr<FakeOutputSurface> CreateDelegatingSoftware(
     64       scoped_ptr<SoftwareOutputDevice> software_device) {
     65     return make_scoped_ptr(
     66         new FakeOutputSurface(software_device.Pass(), true));
     67   }
     68 
     69   // TODO(boliu): Use a general factory that takes Capabilities arg.
     70   static scoped_ptr<FakeOutputSurface> CreateDeferredGL(
     71       scoped_ptr<SoftwareOutputDevice> software_device) {
     72     scoped_ptr<FakeOutputSurface> result(
     73         new FakeOutputSurface(software_device.Pass(), false));
     74     result->capabilities_.deferred_gl_initialization = true;
     75     return result.Pass();
     76   }
     77 
     78   static scoped_ptr<FakeOutputSurface> CreateAlwaysDrawAndSwap3d() {
     79     scoped_ptr<FakeOutputSurface> surface(Create3d());
     80     surface->capabilities_.draw_and_swap_full_viewport_every_frame = true;
     81     return surface.Pass();
     82   }
     83 
     84   static scoped_ptr<FakeOutputSurface> CreateOffscreen(
     85       scoped_ptr<TestWebGraphicsContext3D> context) {
     86     scoped_ptr<FakeOutputSurface> surface(new FakeOutputSurface(
     87         TestContextProvider::Create(context.Pass()), false));
     88     surface->capabilities_.uses_default_gl_framebuffer = false;
     89     return surface.Pass();
     90   }
     91 
     92   CompositorFrame& last_sent_frame() { return last_sent_frame_; }
     93   size_t num_sent_frames() { return num_sent_frames_; }
     94 
     95   virtual void SwapBuffers(CompositorFrame* frame) OVERRIDE;
     96 
     97   virtual void SetNeedsBeginImplFrame(bool enable) OVERRIDE;
     98   bool needs_begin_impl_frame() const {
     99     return needs_begin_impl_frame_;
    100   }
    101 
    102   void set_forced_draw_to_software_device(bool forced) {
    103     forced_draw_to_software_device_ = forced;
    104   }
    105   virtual bool ForcedDrawToSoftwareDevice() const OVERRIDE;
    106 
    107   virtual bool BindToClient(OutputSurfaceClient* client) OVERRIDE;
    108 
    109   using OutputSurface::ReleaseGL;
    110   using OutputSurface::InitializeAndSetContext3d;
    111 
    112   void SetTreeActivationCallback(const base::Closure& callback);
    113 
    114   const TransferableResourceArray& resources_held_by_parent() {
    115     return resources_held_by_parent_;
    116   }
    117 
    118   void ReturnResource(unsigned id, CompositorFrameAck* ack);
    119 
    120   virtual bool HasExternalStencilTest() const OVERRIDE;
    121 
    122   void set_has_external_stencil_test(bool has_test) {
    123     has_external_stencil_test_ = has_test;
    124   }
    125 
    126   void SetMemoryPolicyToSetAtBind(
    127       scoped_ptr<ManagedMemoryPolicy> memory_policy_to_set_at_bind);
    128 
    129  protected:
    130   FakeOutputSurface(
    131       scoped_refptr<ContextProvider> context_provider,
    132       bool delegated_rendering);
    133 
    134   FakeOutputSurface(
    135       scoped_ptr<SoftwareOutputDevice> software_device,
    136       bool delegated_rendering);
    137 
    138   FakeOutputSurface(
    139       scoped_refptr<ContextProvider> context_provider,
    140       scoped_ptr<SoftwareOutputDevice> software_device,
    141       bool delegated_rendering);
    142 
    143   void OnBeginImplFrame();
    144 
    145   OutputSurfaceClient* client_;
    146   CompositorFrame last_sent_frame_;
    147   size_t num_sent_frames_;
    148   bool needs_begin_impl_frame_;
    149   bool forced_draw_to_software_device_;
    150   bool has_external_stencil_test_;
    151   TransferableResourceArray resources_held_by_parent_;
    152   base::WeakPtrFactory<FakeOutputSurface> fake_weak_ptr_factory_;
    153   scoped_ptr<ManagedMemoryPolicy> memory_policy_to_set_at_bind_;
    154 };
    155 
    156 static inline scoped_ptr<OutputSurface> CreateFakeOutputSurface() {
    157   return FakeOutputSurface::Create3d().PassAs<OutputSurface>();
    158 }
    159 
    160 }  // namespace cc
    161 
    162 #endif  // CC_TEST_FAKE_OUTPUT_SURFACE_H_
    163