1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef GPU_COMMAND_BUFFER_SERVICE_ASYNC_PIXEL_TRANSFER_MANAGER_H_
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define GPU_COMMAND_BUFFER_SERVICE_ASYNC_PIXEL_TRANSFER_MANAGER_H_
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <set>
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/basictypes.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/callback.h"
127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/containers/hash_tables.h"
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/memory/linked_ptr.h"
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/memory/ref_counted.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "gpu/command_buffer/service/texture_manager.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "gpu/gpu_export.h"
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(COMPILER_GCC)
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace BASE_HASH_NAMESPACE {
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <>
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  struct hash<gpu::gles2::TextureRef*> {
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  size_t operator()(gpu::gles2::TextureRef* ptr) const {
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return hash<size_t>()(reinterpret_cast<size_t>(ptr));
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace BASE_HASH_NAMESPACE
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif  // COMPILER
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace gfx {
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class GLContext;
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace gpu {
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class AsyncPixelTransferDelegate;
35effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochclass AsyncMemoryParams;
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)struct AsyncTexImage2DParams;
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass AsyncPixelTransferCompletionObserver
39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    : public base::RefCountedThreadSafe<AsyncPixelTransferCompletionObserver> {
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch public:
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  AsyncPixelTransferCompletionObserver();
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void DidComplete(const AsyncMemoryParams& mem_params) = 0;
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch protected:
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual ~AsyncPixelTransferCompletionObserver();
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch private:
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  friend class base::RefCountedThreadSafe<AsyncPixelTransferCompletionObserver>;
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DISALLOW_COPY_AND_ASSIGN(AsyncPixelTransferCompletionObserver);
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch};
53eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class GPU_EXPORT AsyncPixelTransferManager
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : public gles2::TextureManager::DestructionObserver {
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static AsyncPixelTransferManager* Create(gfx::GLContext* context);
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual ~AsyncPixelTransferManager();
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void Initialize(gles2::TextureManager* texture_manager);
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void BindCompletedAsyncTransfers() = 0;
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // There's no guarantee that callback will run on the caller thread.
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void AsyncNotifyCompletion(
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const AsyncMemoryParams& mem_params,
68eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      AsyncPixelTransferCompletionObserver* observer) = 0;
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual uint32 GetTextureUploadCount() = 0;
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual base::TimeDelta GetTotalTextureUploadTime() = 0;
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // ProcessMorePendingTransfers() will be called at a good time
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // to process a small amount of pending transfer work while
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // NeedsProcessMorePendingTransfers() returns true. Implementations
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // that can't dispatch work to separate threads should use
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // this to avoid blocking the caller thread inappropriately.
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void ProcessMorePendingTransfers() = 0;
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual bool NeedsProcessMorePendingTransfers() = 0;
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
814ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch  // Wait for all AsyncTex(Sub)Image2D uploads to finish before returning.
824ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch  virtual void WaitAllAsyncTexImage2D() = 0;
834ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch
847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  AsyncPixelTransferDelegate* CreatePixelTransferDelegate(
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      gles2::TextureRef* ref,
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const AsyncTexImage2DParams& define_params);
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  AsyncPixelTransferDelegate* GetPixelTransferDelegate(
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      gles2::TextureRef* ref);
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void ClearPixelTransferDelegateForTest(gles2::TextureRef* ref);
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool AsyncTransferIsInProgress(gles2::TextureRef* ref);
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // gles2::TextureRef::DestructionObserver implementation:
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void OnTextureManagerDestroying(gles2::TextureManager* manager)
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      OVERRIDE;
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void OnTextureRefDestroying(gles2::TextureRef* texture) OVERRIDE;
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) protected:
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AsyncPixelTransferManager();
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  gles2::TextureManager* manager_;
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef base::hash_map<gles2::TextureRef*,
1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                         linked_ptr<AsyncPixelTransferDelegate> >
1087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      TextureToDelegateMap;
1097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  TextureToDelegateMap delegate_map_;
1107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // A factory method called by CreatePixelTransferDelegate that is overriden
1127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // by each implementation.
1137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual AsyncPixelTransferDelegate* CreatePixelTransferDelegateImpl(
1147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      gles2::TextureRef* ref,
1157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const AsyncTexImage2DParams& define_params) = 0;
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AsyncPixelTransferManager);
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace gpu
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif  // GPU_COMMAND_BUFFER_SERVICE_ASYNC_PIXEL_TRANSFER_MANAGER_H_
123