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) 19 namespace BASE_HASH_NAMESPACE { 20 template <> 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 29 namespace gfx { 30 class GLContext; 31 } 32 33 namespace gpu { 34 class AsyncPixelTransferDelegate; 35 struct AsyncMemoryParams; 36 struct AsyncTexImage2DParams; 37 38 class 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 54 class 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 AsyncPixelTransferDelegate* CreatePixelTransferDelegate( 82 gles2::TextureRef* ref, 83 const AsyncTexImage2DParams& define_params); 84 85 AsyncPixelTransferDelegate* GetPixelTransferDelegate( 86 gles2::TextureRef* ref); 87 88 void ClearPixelTransferDelegateForTest(gles2::TextureRef* ref); 89 90 bool AsyncTransferIsInProgress(gles2::TextureRef* ref); 91 92 // gles2::TextureRef::DestructionObserver implementation: 93 virtual void OnTextureManagerDestroying(gles2::TextureManager* manager) 94 OVERRIDE; 95 virtual void OnTextureRefDestroying(gles2::TextureRef* texture) OVERRIDE; 96 97 protected: 98 AsyncPixelTransferManager(); 99 100 private: 101 gles2::TextureManager* manager_; 102 103 typedef base::hash_map<gles2::TextureRef*, 104 linked_ptr<AsyncPixelTransferDelegate> > 105 TextureToDelegateMap; 106 TextureToDelegateMap delegate_map_; 107 108 // A factory method called by CreatePixelTransferDelegate that is overriden 109 // by each implementation. 110 virtual AsyncPixelTransferDelegate* CreatePixelTransferDelegateImpl( 111 gles2::TextureRef* ref, 112 const AsyncTexImage2DParams& define_params) = 0; 113 114 DISALLOW_COPY_AND_ASSIGN(AsyncPixelTransferManager); 115 }; 116 117 } // namespace gpu 118 119 #endif // GPU_COMMAND_BUFFER_SERVICE_ASYNC_PIXEL_TRANSFER_MANAGER_H_ 120