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 GPU_COMMAND_BUFFER_SERVICE_ASYNC_PIXEL_TRANSFER_MANAGER_H_
6#define GPU_COMMAND_BUFFER_SERVICE_ASYNC_PIXEL_TRANSFER_MANAGER_H_
7
8#include <set>
9
10#include "base/basictypes.h"
11#include "base/callback.h"
12#include "base/containers/hash_tables.h"
13#include "base/memory/linked_ptr.h"
14#include "base/memory/ref_counted.h"
15#include "gpu/command_buffer/service/texture_manager.h"
16#include "gpu/gpu_export.h"
17
18#if defined(COMPILER_GCC)
19namespace BASE_HASH_NAMESPACE {
20template <>
21  struct hash<gpu::gles2::TextureRef*> {
22  size_t operator()(gpu::gles2::TextureRef* ptr) const {
23    return hash<size_t>()(reinterpret_cast<size_t>(ptr));
24  }
25};
26}  // namespace BASE_HASH_NAMESPACE
27#endif  // COMPILER
28
29namespace gfx {
30class GLContext;
31}
32
33namespace gpu {
34class AsyncPixelTransferDelegate;
35class AsyncMemoryParams;
36struct AsyncTexImage2DParams;
37
38class AsyncPixelTransferCompletionObserver
39    : public base::RefCountedThreadSafe<AsyncPixelTransferCompletionObserver> {
40 public:
41  AsyncPixelTransferCompletionObserver();
42
43  virtual void DidComplete(const AsyncMemoryParams& mem_params) = 0;
44
45 protected:
46  virtual ~AsyncPixelTransferCompletionObserver();
47
48 private:
49  friend class base::RefCountedThreadSafe<AsyncPixelTransferCompletionObserver>;
50
51  DISALLOW_COPY_AND_ASSIGN(AsyncPixelTransferCompletionObserver);
52};
53
54class GPU_EXPORT AsyncPixelTransferManager
55    : public gles2::TextureManager::DestructionObserver {
56 public:
57  static AsyncPixelTransferManager* Create(gfx::GLContext* context);
58
59  virtual ~AsyncPixelTransferManager();
60
61  void Initialize(gles2::TextureManager* texture_manager);
62
63  virtual void BindCompletedAsyncTransfers() = 0;
64
65  // There's no guarantee that callback will run on the caller thread.
66  virtual void AsyncNotifyCompletion(
67      const AsyncMemoryParams& mem_params,
68      AsyncPixelTransferCompletionObserver* observer) = 0;
69
70  virtual uint32 GetTextureUploadCount() = 0;
71  virtual base::TimeDelta GetTotalTextureUploadTime() = 0;
72
73  // ProcessMorePendingTransfers() will be called at a good time
74  // to process a small amount of pending transfer work while
75  // NeedsProcessMorePendingTransfers() returns true. Implementations
76  // that can't dispatch work to separate threads should use
77  // this to avoid blocking the caller thread inappropriately.
78  virtual void ProcessMorePendingTransfers() = 0;
79  virtual bool NeedsProcessMorePendingTransfers() = 0;
80
81  // Wait for all AsyncTex(Sub)Image2D uploads to finish before returning.
82  virtual void WaitAllAsyncTexImage2D() = 0;
83
84  AsyncPixelTransferDelegate* CreatePixelTransferDelegate(
85      gles2::TextureRef* ref,
86      const AsyncTexImage2DParams& define_params);
87
88  AsyncPixelTransferDelegate* GetPixelTransferDelegate(
89      gles2::TextureRef* ref);
90
91  void ClearPixelTransferDelegateForTest(gles2::TextureRef* ref);
92
93  bool AsyncTransferIsInProgress(gles2::TextureRef* ref);
94
95  // gles2::TextureRef::DestructionObserver implementation:
96  virtual void OnTextureManagerDestroying(gles2::TextureManager* manager)
97      OVERRIDE;
98  virtual void OnTextureRefDestroying(gles2::TextureRef* texture) OVERRIDE;
99
100 protected:
101  AsyncPixelTransferManager();
102
103 private:
104  gles2::TextureManager* manager_;
105
106  typedef base::hash_map<gles2::TextureRef*,
107                         linked_ptr<AsyncPixelTransferDelegate> >
108      TextureToDelegateMap;
109  TextureToDelegateMap delegate_map_;
110
111  // A factory method called by CreatePixelTransferDelegate that is overriden
112  // by each implementation.
113  virtual AsyncPixelTransferDelegate* CreatePixelTransferDelegateImpl(
114      gles2::TextureRef* ref,
115      const AsyncTexImage2DParams& define_params) = 0;
116
117  DISALLOW_COPY_AND_ASSIGN(AsyncPixelTransferManager);
118};
119
120}  // namespace gpu
121
122#endif  // GPU_COMMAND_BUFFER_SERVICE_ASYNC_PIXEL_TRANSFER_MANAGER_H_
123