| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_GPU_SCHEDULER_H_ | 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_GPU_SCHEDULER_H_ |
| 6 #define GPU_COMMAND_BUFFER_SERVICE_GPU_SCHEDULER_H_ | 6 #define GPU_COMMAND_BUFFER_SERVICE_GPU_SCHEDULER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/shared_memory.h" | 15 #include "base/shared_memory.h" |
| 16 #include "base/task.h" | 16 #include "base/task.h" |
| 17 #include "gpu/command_buffer/common/command_buffer.h" | 17 #include "gpu/command_buffer/common/command_buffer.h" |
| 18 #include "gpu/command_buffer/service/cmd_buffer_engine.h" | 18 #include "gpu/command_buffer/service/cmd_buffer_engine.h" |
| 19 #include "gpu/command_buffer/service/cmd_parser.h" | 19 #include "gpu/command_buffer/service/cmd_parser.h" |
| 20 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | 20 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
| 21 #include "ui/gfx/native_widget_types.h" | 21 #include "ui/gfx/native_widget_types.h" |
| 22 #include "ui/gfx/size.h" | 22 #include "ui/gfx/size.h" |
| 23 #include "ui/gfx/surface/transport_dib.h" | 23 #include "ui/gfx/surface/transport_dib.h" |
| 24 | 24 |
| 25 #if defined(OS_MACOSX) | 25 #if defined(OS_MACOSX) |
| 26 #include "ui/gfx/surface/accelerated_surface_mac.h" | 26 #include "ui/gfx/surface/accelerated_surface_mac.h" |
| 27 #elif defined(TOUCH_UI) | |
| 28 #include "ui/gfx/surface/accelerated_surface_linux.h" | |
| 29 #endif | 27 #endif |
| 30 | 28 |
| 31 namespace gfx { | 29 namespace gfx { |
| 32 class GLContext; | 30 class GLContext; |
| 33 class GLShareGroup; | 31 class GLShareGroup; |
| 34 class GLSurface; | 32 class GLSurface; |
| 35 } | 33 } |
| 36 | 34 |
| 37 namespace gpu { | 35 namespace gpu { |
| 38 namespace gles2 { | 36 namespace gles2 { |
| 39 class ContextGroup; | 37 class ContextGroup; |
| 40 } | 38 } |
| 41 | 39 |
| 42 // This class processes commands in a command buffer. It is event driven and | 40 // This class processes commands in a command buffer. It is event driven and |
| 43 // posts tasks to the current message loop to do additional work. | 41 // posts tasks to the current message loop to do additional work. |
| 44 class GpuScheduler : public CommandBufferEngine { | 42 class GpuScheduler : public CommandBufferEngine { |
| 45 public: | 43 public: |
| 46 // If a group is not passed in one will be created. | 44 // If a group is not passed in one will be created. |
| 47 static GpuScheduler* Create(CommandBuffer* command_buffer, | 45 static GpuScheduler* Create(CommandBuffer* command_buffer, |
| 48 SurfaceManager* surface_manager, | 46 SurfaceManager* surface_manager, |
| 49 gles2::ContextGroup* group); | 47 gles2::ContextGroup* group); |
| 50 | 48 |
| 51 // This constructor is for unit tests. | 49 // This constructor is for unit tests. |
| 52 static GpuScheduler* CreateForTests(CommandBuffer* command_buffer, | 50 static GpuScheduler* CreateForTests(CommandBuffer* command_buffer, |
| 53 gles2::GLES2Decoder* decoder, | 51 gles2::GLES2Decoder* decoder, |
| 54 CommandParser* parser); | 52 CommandParser* parser); |
| 55 | 53 |
| 56 virtual ~GpuScheduler(); | 54 virtual ~GpuScheduler(); |
| 57 | 55 |
| 58 // Perform platform specific and common initialization. | 56 // Platform specific code to create GLContexts and GLSurfaces that are |
| 57 // handed off to the next function. |
| 59 bool Initialize(gfx::PluginWindowHandle hwnd, | 58 bool Initialize(gfx::PluginWindowHandle hwnd, |
| 60 const gfx::Size& size, | 59 const gfx::Size& size, |
| 61 bool software, | 60 bool software, |
| 62 const gles2::DisallowedExtensions& disallowed_extensions, | 61 const gles2::DisallowedExtensions& disallowed_extensions, |
| 63 const char* allowed_extensions, | 62 const char* allowed_extensions, |
| 64 const std::vector<int32>& attribs, | 63 const std::vector<int32>& attribs, |
| 65 gfx::GLShareGroup* share_group); | 64 gfx::GLShareGroup* share_group); |
| 66 | 65 |
| 66 // Takes ownership of GLSurface and GLContext. |
| 67 bool InitializeCommon( |
| 68 const scoped_refptr<gfx::GLSurface>& surface, |
| 69 const scoped_refptr<gfx::GLContext>& context, |
| 70 const gfx::Size& size, |
| 71 const gles2::DisallowedExtensions& disallowed_extensions, |
| 72 const char* allowed_extensions, |
| 73 const std::vector<int32>& attribs); |
| 74 |
| 67 void Destroy(); | 75 void Destroy(); |
| 68 void DestroyCommon(); | 76 void DestroyCommon(); |
| 69 | 77 |
| 70 bool SetParent(GpuScheduler* parent_scheduler, uint32 parent_texture_id); | 78 bool SetParent(GpuScheduler* parent_scheduler, uint32 parent_texture_id); |
| 71 | 79 |
| 72 void PutChanged(); | 80 void PutChanged(); |
| 73 | 81 |
| 74 // Sets whether commands should be processed by this scheduler. Setting to | 82 // Sets whether commands should be processed by this scheduler. Setting to |
| 75 // false unschedules. Setting to true reschedules. Whether or not the | 83 // false unschedules. Setting to true reschedules. Whether or not the |
| 76 // scheduler is currently scheduled is "reference counted". Every call with | 84 // scheduler is currently scheduled is "reference counted". Every call with |
| 77 // false must eventually be paired by a call with true. | 85 // false must eventually be paired by a call with true. |
| 78 void SetScheduled(bool is_scheduled); | 86 void SetScheduled(bool is_scheduled); |
| 79 | 87 |
| 80 // Returns whether the scheduler is currently scheduled to process commands. | 88 // Returns whether the scheduler is currently scheduled to process commands. |
| 81 bool IsScheduled(); | 89 bool IsScheduled(); |
| 82 | 90 |
| 83 // Sets a callback that is invoked just before scheduler is rescheduled. | 91 // Sets a callback that is invoked just before scheduler is rescheduled. |
| 84 // Takes ownership of callback object. | 92 // Takes ownership of callback object. |
| 85 void SetScheduledCallback(Callback0::Type* scheduled_callback); | 93 void SetScheduledCallback(Callback0::Type* scheduled_callback); |
| 86 | 94 |
| 87 // Implementation of CommandBufferEngine. | 95 // Implementation of CommandBufferEngine. |
| 88 virtual Buffer GetSharedMemoryBuffer(int32 shm_id); | 96 virtual Buffer GetSharedMemoryBuffer(int32 shm_id); |
| 89 virtual void set_token(int32 token); | 97 virtual void set_token(int32 token); |
| 90 virtual bool SetGetOffset(int32 offset); | 98 virtual bool SetGetOffset(int32 offset); |
| 91 virtual int32 GetGetOffset(); | 99 virtual int32 GetGetOffset(); |
| 92 | 100 |
| 93 // Asynchronously resizes an offscreen frame buffer. | 101 // Asynchronously resizes an offscreen frame buffer. |
| 94 void ResizeOffscreenFrameBuffer(const gfx::Size& size); | 102 void ResizeOffscreenFrameBuffer(const gfx::Size& size); |
| 95 | 103 |
| 96 #if defined(OS_MACOSX) || defined(TOUCH_UI) | 104 #if defined(OS_MACOSX) |
| 97 // To prevent the GPU process from overloading the browser process, | 105 // To prevent the GPU process from overloading the browser process, |
| 98 // we need to track the number of swap buffers calls issued and | 106 // we need to track the number of swap buffers calls issued and |
| 99 // acknowledged per on-screen context, and keep the GPU from getting | 107 // acknowledged per on-screen context, and keep the GPU from getting |
| 100 // too far ahead of the browser. Note that this | 108 // too far ahead of the browser. Note that this |
| 101 // is also predicated on a flow control mechanism between the | 109 // is also predicated on a flow control mechanism between the |
| 102 // renderer and GPU processes. | 110 // renderer and GPU processes. |
| 103 uint64 swap_buffers_count() const; | 111 uint64 swap_buffers_count() const; |
| 104 uint64 acknowledged_swap_buffers_count() const; | 112 uint64 acknowledged_swap_buffers_count() const; |
| 105 void set_acknowledged_swap_buffers_count( | 113 void set_acknowledged_swap_buffers_count( |
| 106 uint64 acknowledged_swap_buffers_count); | 114 uint64 acknowledged_swap_buffers_count); |
| 107 #endif | |
| 108 | 115 |
| 109 #if defined(OS_MACOSX) | |
| 110 // Needed only on Mac OS X, which does not render into an on-screen | 116 // Needed only on Mac OS X, which does not render into an on-screen |
| 111 // window and therefore requires the backing store to be resized | 117 // window and therefore requires the backing store to be resized |
| 112 // manually. Returns an opaque identifier for the new backing store. | 118 // manually. Returns an opaque identifier for the new backing store. |
| 113 // There are two versions of this method: one for use with the IOSurface | 119 // There are two versions of this method: one for use with the IOSurface |
| 114 // available in Mac OS X 10.6; and, one for use with the | 120 // available in Mac OS X 10.6; and, one for use with the |
| 115 // TransportDIB-based version used on Mac OS X 10.5. | 121 // TransportDIB-based version used on Mac OS X 10.5. |
| 116 virtual uint64 SetWindowSizeForIOSurface(const gfx::Size& size); | 122 virtual uint64 SetWindowSizeForIOSurface(const gfx::Size& size); |
| 117 virtual TransportDIB::Handle SetWindowSizeForTransportDIB( | 123 virtual TransportDIB::Handle SetWindowSizeForTransportDIB( |
| 118 const gfx::Size& size); | 124 const gfx::Size& size); |
| 119 virtual void SetTransportDIBAllocAndFree( | 125 virtual void SetTransportDIBAllocAndFree( |
| 120 Callback2<size_t, TransportDIB::Handle*>::Type* allocator, | 126 Callback2<size_t, TransportDIB::Handle*>::Type* allocator, |
| 121 Callback1<TransportDIB::Id>::Type* deallocator); | 127 Callback1<TransportDIB::Id>::Type* deallocator); |
| 122 // Returns the id of the current surface that is being rendered to | 128 // Returns the id of the current surface that is being rendered to |
| 123 // (or 0 if no such surface has been created). | 129 // (or 0 if no such surface has been created). |
| 124 virtual uint64 GetSurfaceId(); | 130 virtual uint64 GetSurfaceId(); |
| 125 | 131 |
| 126 void DidDestroySurface(); | 132 void DidDestroySurface(); |
| 127 #endif | 133 #endif |
| 128 | 134 |
| 129 #if defined(TOUCH_UI) | |
| 130 virtual void CreateBackSurface(const gfx::Size& size); | |
| 131 // Should not be back_surface_ or front_surface_. | |
| 132 virtual void ReleaseSurface(uint64 surface_id); | |
| 133 | |
| 134 // Returns the id of the surface (or 0 if no such surface has been created). | |
| 135 virtual uint64 GetBackSurfaceId(); | |
| 136 virtual uint64 GetFrontSurfaceId(); | |
| 137 #endif | |
| 138 | |
| 139 // Sets a callback that is called when a glResizeCHROMIUM command | 135 // Sets a callback that is called when a glResizeCHROMIUM command |
| 140 // is processed. | 136 // is processed. |
| 141 void SetResizeCallback(Callback1<gfx::Size>::Type* callback); | 137 void SetResizeCallback(Callback1<gfx::Size>::Type* callback); |
| 142 | 138 |
| 143 // Sets a callback which is called when a SwapBuffers command is processed. | 139 // Sets a callback which is called when a SwapBuffers command is processed. |
| 144 // Must be called after Initialize(). | 140 // Must be called after Initialize(). |
| 145 // It is not defined on which thread this callback is called. | 141 // It is not defined on which thread this callback is called. |
| 146 void SetSwapBuffersCallback(Callback0::Type* callback); | 142 void SetSwapBuffersCallback(Callback0::Type* callback); |
| 147 | 143 |
| 148 void SetCommandProcessedCallback(Callback0::Type* callback); | 144 void SetCommandProcessedCallback(Callback0::Type* callback); |
| 149 | 145 |
| 150 // Sets a callback which is called when set_token() is called, and passes the | 146 // Sets a callback which is called when set_token() is called, and passes the |
| 151 // just-set token to the callback. DCHECKs that no callback has previously | 147 // just-set token to the callback. DCHECKs that no callback has previously |
| 152 // been registered for this notification. | 148 // been registered for this notification. |
| 153 void SetTokenCallback(const base::Callback<void(int32)>& callback); | 149 void SetTokenCallback(const base::Callback<void(int32)>& callback); |
| 154 | 150 |
| 155 // Get the GLES2Decoder associated with this scheduler. | 151 // Get the GLES2Decoder associated with this scheduler. |
| 156 gles2::GLES2Decoder* decoder() const { return decoder_.get(); } | 152 gles2::GLES2Decoder* decoder() const { return decoder_.get(); } |
| 157 | 153 |
| 158 protected: | |
| 159 // Perform common initialization. Takes ownership of GLSurface and GLContext. | |
| 160 bool InitializeCommon( | |
| 161 const scoped_refptr<gfx::GLSurface>& surface, | |
| 162 const scoped_refptr<gfx::GLContext>& context, | |
| 163 const gfx::Size& size, | |
| 164 const gles2::DisallowedExtensions& disallowed_extensions, | |
| 165 const char* allowed_extensions, | |
| 166 const std::vector<int32>& attribs); | |
| 167 | |
| 168 | |
| 169 private: | 154 private: |
| 170 // If a group is not passed in one will be created. | 155 // If a group is not passed in one will be created. |
| 171 GpuScheduler(CommandBuffer* command_buffer, | 156 GpuScheduler(CommandBuffer* command_buffer, |
| 172 gles2::GLES2Decoder* decoder, | 157 gles2::GLES2Decoder* decoder, |
| 173 CommandParser* parser); | 158 CommandParser* parser); |
| 174 | 159 |
| 175 // Called via a callback just before we are supposed to call the | 160 // Called via a callback just before we are supposed to call the |
| 176 // user's resize callback. | |
| 177 void WillResize(gfx::Size size); | |
| 178 | |
| 179 // Called via a callback just before we are supposed to call the | |
| 180 // user's swap buffers callback. | 161 // user's swap buffers callback. |
| 181 void WillSwapBuffers(); | 162 void WillSwapBuffers(); |
| 182 | 163 |
| 183 // The GpuScheduler holds a weak reference to the CommandBuffer. The | 164 // The GpuScheduler holds a weak reference to the CommandBuffer. The |
| 184 // CommandBuffer owns the GpuScheduler and holds a strong reference to it | 165 // CommandBuffer owns the GpuScheduler and holds a strong reference to it |
| 185 // through the ProcessCommands callback. | 166 // through the ProcessCommands callback. |
| 186 CommandBuffer* command_buffer_; | 167 CommandBuffer* command_buffer_; |
| 187 | 168 |
| 188 scoped_ptr<gles2::GLES2Decoder> decoder_; | 169 scoped_ptr<gles2::GLES2Decoder> decoder_; |
| 189 scoped_ptr<CommandParser> parser_; | 170 scoped_ptr<CommandParser> parser_; |
| 190 | 171 |
| 191 // Greater than zero if this is waiting to be rescheduled before continuing. | 172 // Greater than zero if this is waiting to be rescheduled before continuing. |
| 192 int unscheduled_count_; | 173 int unscheduled_count_; |
| 193 | 174 |
| 194 scoped_ptr<Callback0::Type> scheduled_callback_; | 175 scoped_ptr<Callback0::Type> scheduled_callback_; |
| 195 | 176 |
| 196 #if defined(OS_MACOSX) || defined(TOUCH_UI) | 177 #if defined(OS_MACOSX) |
| 197 uint64 swap_buffers_count_; | 178 uint64 swap_buffers_count_; |
| 198 uint64 acknowledged_swap_buffers_count_; | 179 uint64 acknowledged_swap_buffers_count_; |
| 199 #endif | |
| 200 | |
| 201 #if defined(OS_MACOSX) | |
| 202 scoped_ptr<AcceleratedSurface> surface_; | 180 scoped_ptr<AcceleratedSurface> surface_; |
| 203 #elif defined(TOUCH_UI) | |
| 204 std::map<uint64, scoped_refptr<AcceleratedSurface> > | |
| 205 surfaces_; | |
| 206 scoped_refptr<AcceleratedSurface> back_surface_; | |
| 207 scoped_refptr<AcceleratedSurface> front_surface_; | |
| 208 #endif | 181 #endif |
| 209 | 182 |
| 210 ScopedRunnableMethodFactory<GpuScheduler> method_factory_; | 183 ScopedRunnableMethodFactory<GpuScheduler> method_factory_; |
| 211 scoped_ptr<Callback1<gfx::Size>::Type> wrapped_resize_callback_; | |
| 212 scoped_ptr<Callback0::Type> wrapped_swap_buffers_callback_; | 184 scoped_ptr<Callback0::Type> wrapped_swap_buffers_callback_; |
| 213 scoped_ptr<Callback0::Type> command_processed_callback_; | 185 scoped_ptr<Callback0::Type> command_processed_callback_; |
| 214 base::Callback<void(int32)> set_token_callback_; | 186 base::Callback<void(int32)> set_token_callback_; |
| 215 }; | 187 }; |
| 216 | 188 |
| 217 } // namespace gpu | 189 } // namespace gpu |
| 218 | 190 |
| 219 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_SCHEDULER_H_ | 191 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_SCHEDULER_H_ |
| OLD | NEW |