Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ui / gl / async_pixel_transfer_delegate.h
blob3f9f4346e00873a54539c3251f09aa87fcff356d
1 // Copyright (c) 2011 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.
5 #ifndef UI_GL_ASYNC_TASK_DELEGATE_H_
6 #define UI_GL_ASYNC_TASK_DELEGATE_H_
8 #include "base/basictypes.h"
9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/time.h"
13 #include "build/build_config.h"
14 #include "ui/gl/gl_bindings.h"
15 #include "ui/gl/gl_export.h"
17 namespace base {
18 class SharedMemory;
21 namespace gfx {
23 struct AsyncTexImage2DParams {
24 GLenum target;
25 GLint level;
26 GLenum internal_format;
27 GLsizei width;
28 GLsizei height;
29 GLint border;
30 GLenum format;
31 GLenum type;
34 struct AsyncTexSubImage2DParams {
35 GLenum target;
36 GLint level;
37 GLint xoffset;
38 GLint yoffset;
39 GLsizei width;
40 GLsizei height;
41 GLenum format;
42 GLenum type;
45 struct AsyncMemoryParams {
46 base::SharedMemory* shared_memory;
47 uint32 shm_size;
48 uint32 shm_data_offset;
49 uint32 shm_data_size;
52 // AsyncPixelTransferState holds the resources required to do async
53 // transfers on one texture. It should stay alive for the lifetime
54 // of the texture to allow multiple transfers.
55 class GL_EXPORT AsyncPixelTransferState :
56 public base::SupportsWeakPtr<AsyncPixelTransferState> {
57 public:
58 virtual ~AsyncPixelTransferState() {}
60 // Returns true if there is a transfer in progress.
61 virtual bool TransferIsInProgress() = 0;
63 protected:
64 friend class base::RefCounted<AsyncPixelTransferState>;
65 AsyncPixelTransferState() {}
67 private:
68 DISALLOW_COPY_AND_ASSIGN(AsyncPixelTransferState);
71 class GL_EXPORT AsyncPixelTransferDelegate {
72 public:
73 typedef base::Callback<void(const AsyncMemoryParams&)> CompletionCallback;
75 static scoped_ptr<AsyncPixelTransferDelegate>
76 Create(gfx::GLContext* context);
77 virtual ~AsyncPixelTransferDelegate() {}
79 // This wouldn't work with MOCK_METHOD, so it routes through
80 // a virtual protected version which returns a raw pointer.
81 scoped_ptr<AsyncPixelTransferState>
82 CreatePixelTransferState(GLuint tex_id,
83 const AsyncTexImage2DParams& define_params) {
84 return make_scoped_ptr(CreateRawPixelTransferState(tex_id, define_params));
87 // Returns true iff a texture was bound to texture-unit zero.
88 virtual bool BindCompletedAsyncTransfers() = 0;
90 // There's no guarantee that callback will run on the caller thread.
91 virtual void AsyncNotifyCompletion(
92 const AsyncMemoryParams& mem_params,
93 const CompletionCallback& callback) = 0;
95 // The callback occurs on the caller thread, once the texture is
96 // safe/ready to be used.
97 virtual void AsyncTexImage2D(
98 AsyncPixelTransferState* state,
99 const AsyncTexImage2DParams& tex_params,
100 const AsyncMemoryParams& mem_params,
101 const base::Closure& bind_callback) = 0;
103 virtual void AsyncTexSubImage2D(
104 AsyncPixelTransferState* state,
105 const AsyncTexSubImage2DParams& tex_params,
106 const AsyncMemoryParams& mem_params) = 0;
108 // Block until the specified transfer completes.
109 virtual void WaitForTransferCompletion(
110 AsyncPixelTransferState* state) = 0;
112 virtual uint32 GetTextureUploadCount() = 0;
113 virtual base::TimeDelta GetTotalTextureUploadTime() = 0;
115 // ProcessMorePendingTransfers() will be called at a good time
116 // to process a small amount of pending transfer work while
117 // NeedsProcessMorePendingTransfers() returns true. Implementations
118 // that can't dispatch work to separate threads should use
119 // this to avoid blocking the caller thread inappropriately.
120 // ProcessMorePendingTransfers() returns true iff a texture was
121 // bound to texture-unit zero.
122 virtual bool ProcessMorePendingTransfers() = 0;
123 virtual bool NeedsProcessMorePendingTransfers() = 0;
125 protected:
126 AsyncPixelTransferDelegate() {}
127 // For testing, as returning scoped_ptr wouldn't work with MOCK_METHOD.
128 virtual AsyncPixelTransferState*
129 CreateRawPixelTransferState(GLuint tex_id,
130 const AsyncTexImage2DParams& define_params) = 0;
132 private:
133 DISALLOW_COPY_AND_ASSIGN(AsyncPixelTransferDelegate);
136 } // namespace gfx
138 #endif // UI_GL_ASYNC_TASK_DELEGATE_H_