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_CONTEXT_PROVIDER_H_
6#define CC_TEST_TEST_CONTEXT_PROVIDER_H_
7
8#include "base/callback.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/weak_ptr.h"
11#include "base/synchronization/lock.h"
12#include "base/threading/thread_checker.h"
13#include "cc/output/context_provider.h"
14#include "cc/test/test_context_support.h"
15#include "gpu/command_buffer/client/gles2_interface_stub.h"
16
17namespace blink { class WebGraphicsContext3D; }
18
19namespace cc {
20class TestWebGraphicsContext3D;
21class TestGLES2Interface;
22
23class TestContextProvider : public ContextProvider {
24 public:
25  typedef base::Callback<scoped_ptr<TestWebGraphicsContext3D>(void)>
26    CreateCallback;
27
28  static scoped_refptr<TestContextProvider> Create();
29  static scoped_refptr<TestContextProvider> Create(
30      scoped_ptr<TestWebGraphicsContext3D> context);
31
32  virtual bool BindToCurrentThread() OVERRIDE;
33  virtual Capabilities ContextCapabilities() OVERRIDE;
34  virtual blink::WebGraphicsContext3D* Context3d() OVERRIDE;
35  virtual gpu::gles2::GLES2Interface* ContextGL() OVERRIDE;
36  virtual gpu::ContextSupport* ContextSupport() OVERRIDE;
37  virtual class GrContext* GrContext() OVERRIDE;
38  virtual void MakeGrContextCurrent() OVERRIDE;
39  virtual bool IsContextLost() OVERRIDE;
40  virtual void VerifyContexts() OVERRIDE;
41  virtual bool DestroyedOnMainThread() OVERRIDE;
42  virtual void SetLostContextCallback(const LostContextCallback& cb) OVERRIDE;
43  virtual void SetMemoryPolicyChangedCallback(
44      const MemoryPolicyChangedCallback& cb) OVERRIDE;
45
46  TestWebGraphicsContext3D* TestContext3d();
47
48  // This returns the TestWebGraphicsContext3D but is valid to call
49  // before the context is bound to a thread. This is needed to set up
50  // state on the test context before binding. Don't call
51  // makeContextCurrent on the context returned from this method.
52  TestWebGraphicsContext3D* UnboundTestContext3d();
53
54  TestContextSupport* support() { return &support_; }
55
56  void SetMemoryAllocation(const ManagedMemoryPolicy& policy);
57
58  void SetMaxTransferBufferUsageBytes(size_t max_transfer_buffer_usage_bytes);
59
60 protected:
61  explicit TestContextProvider(scoped_ptr<TestWebGraphicsContext3D> context);
62  virtual ~TestContextProvider();
63
64 private:
65  void OnLostContext();
66
67  TestContextSupport support_;
68
69  scoped_ptr<TestWebGraphicsContext3D> context3d_;
70  scoped_ptr<TestGLES2Interface> context_gl_;
71  bool bound_;
72
73  base::ThreadChecker main_thread_checker_;
74  base::ThreadChecker context_thread_checker_;
75
76  base::Lock destroyed_lock_;
77  bool destroyed_;
78
79  LostContextCallback lost_context_callback_;
80  MemoryPolicyChangedCallback memory_policy_changed_callback_;
81
82  class LostContextCallbackProxy;
83  scoped_ptr<LostContextCallbackProxy> lost_context_callback_proxy_;
84
85  base::WeakPtrFactory<TestContextProvider> weak_ptr_factory_;
86
87  DISALLOW_COPY_AND_ASSIGN(TestContextProvider);
88};
89
90}  // namespace cc
91
92#endif  // CC_TEST_TEST_CONTEXT_PROVIDER_H_
93
94