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/time/time.h" 10 #include "cc/output/begin_frame_args.h" 11 #include "cc/output/compositor_frame.h" 12 #include "cc/output/output_surface.h" 13 #include "cc/output/software_output_device.h" 14 #include "cc/test/test_web_graphics_context_3d.h" 15 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" 16 17 namespace cc { 18 19 class FakeOutputSurface : public OutputSurface { 20 public: 21 virtual ~FakeOutputSurface(); 22 23 static scoped_ptr<FakeOutputSurface> Create3d( 24 scoped_ptr<WebKit::WebGraphicsContext3D> context3d) { 25 return make_scoped_ptr(new FakeOutputSurface(context3d.Pass(), false)); 26 } 27 28 static scoped_ptr<FakeOutputSurface> Create3d() { 29 scoped_ptr<WebKit::WebGraphicsContext3D> context3d = 30 TestWebGraphicsContext3D::Create() 31 .PassAs<WebKit::WebGraphicsContext3D>(); 32 return make_scoped_ptr(new FakeOutputSurface(context3d.Pass(), false)); 33 } 34 35 static scoped_ptr<FakeOutputSurface> CreateSoftware( 36 scoped_ptr<SoftwareOutputDevice> software_device) { 37 return make_scoped_ptr( 38 new FakeOutputSurface(software_device.Pass(), false)); 39 } 40 41 static scoped_ptr<FakeOutputSurface> CreateDelegating3d( 42 scoped_ptr<WebKit::WebGraphicsContext3D> context3d) { 43 return make_scoped_ptr(new FakeOutputSurface(context3d.Pass(), true)); 44 } 45 46 static scoped_ptr<FakeOutputSurface> CreateDelegating3d() { 47 scoped_ptr<WebKit::WebGraphicsContext3D> context3d = 48 TestWebGraphicsContext3D::Create() 49 .PassAs<WebKit::WebGraphicsContext3D>(); 50 return make_scoped_ptr(new FakeOutputSurface(context3d.Pass(), true)); 51 } 52 53 static scoped_ptr<FakeOutputSurface> CreateDelegatingSoftware( 54 scoped_ptr<SoftwareOutputDevice> software_device) { 55 return make_scoped_ptr( 56 new FakeOutputSurface(software_device.Pass(), true)); 57 } 58 59 // TODO(boliu): Use a general factory that takes Capabilities arg. 60 static scoped_ptr<FakeOutputSurface> CreateDeferredGL( 61 scoped_ptr<SoftwareOutputDevice> software_device) { 62 scoped_ptr<FakeOutputSurface> result( 63 new FakeOutputSurface(software_device.Pass(), false)); 64 result->capabilities_.deferred_gl_initialization = true; 65 return result.Pass(); 66 } 67 68 static scoped_ptr<FakeOutputSurface> CreateAlwaysDrawAndSwap3d() { 69 scoped_ptr<FakeOutputSurface> result(Create3d()); 70 result->capabilities_.draw_and_swap_full_viewport_every_frame = true; 71 return result.Pass(); 72 } 73 74 CompositorFrame& last_sent_frame() { return last_sent_frame_; } 75 size_t num_sent_frames() { return num_sent_frames_; } 76 77 virtual void SwapBuffers(CompositorFrame* frame) OVERRIDE; 78 79 virtual void SetNeedsBeginFrame(bool enable) OVERRIDE; 80 bool needs_begin_frame() const { 81 return needs_begin_frame_; 82 } 83 84 void set_forced_draw_to_software_device(bool forced) { 85 forced_draw_to_software_device_ = forced; 86 } 87 virtual bool ForcedDrawToSoftwareDevice() const OVERRIDE; 88 89 virtual bool BindToClient(OutputSurfaceClient* client) OVERRIDE; 90 91 bool SetAndInitializeContext3D( 92 scoped_ptr<WebKit::WebGraphicsContext3D> context3d); 93 94 using OutputSurface::ReleaseGL; 95 96 void SetTreeActivationCallback(const base::Closure& callback); 97 98 const TransferableResourceArray& resources_held_by_parent() { 99 return resources_held_by_parent_; 100 } 101 102 void ReturnResource(unsigned id, CompositorFrameAck* ack); 103 104 protected: 105 FakeOutputSurface( 106 scoped_ptr<WebKit::WebGraphicsContext3D> context3d, 107 bool delegated_rendering); 108 109 FakeOutputSurface( 110 scoped_ptr<SoftwareOutputDevice> software_device, 111 bool delegated_rendering); 112 113 FakeOutputSurface( 114 scoped_ptr<WebKit::WebGraphicsContext3D> context3d, 115 scoped_ptr<SoftwareOutputDevice> software_device, 116 bool delegated_rendering); 117 118 void OnBeginFrame(); 119 120 OutputSurfaceClient* client_; 121 CompositorFrame last_sent_frame_; 122 size_t num_sent_frames_; 123 bool needs_begin_frame_; 124 bool forced_draw_to_software_device_; 125 base::WeakPtrFactory<FakeOutputSurface> fake_weak_ptr_factory_; 126 TransferableResourceArray resources_held_by_parent_; 127 }; 128 129 static inline scoped_ptr<cc::OutputSurface> CreateFakeOutputSurface() { 130 return FakeOutputSurface::Create3d().PassAs<cc::OutputSurface>(); 131 } 132 133 } // namespace cc 134 135 #endif // CC_TEST_FAKE_OUTPUT_SURFACE_H_ 136