summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/gpu_processor.h
blob: 247c4c57d717271238d08241b303a63ecb8a0626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_COMMAND_BUFFER_SERVICE_GPU_PROCESSOR_H_
#define GPU_COMMAND_BUFFER_SERVICE_GPU_PROCESSOR_H_

#include <vector>

#include "app/surface/transport_dib.h"
#include "base/callback.h"
#include "base/ref_counted.h"
#include "base/scoped_ptr.h"
#include "base/shared_memory.h"
#include "base/task.h"
#include "gfx/native_widget_types.h"
#include "gfx/size.h"
#include "gpu/command_buffer/common/command_buffer.h"
#include "gpu/command_buffer/service/cmd_buffer_engine.h"
#include "gpu/command_buffer/service/cmd_parser.h"
#include "gpu/command_buffer/service/gles2_cmd_decoder.h"

#if defined(OS_MACOSX)
#include "app/surface/accelerated_surface_mac.h"
#endif

namespace gfx {
class GLContext;
}

namespace gpu {
namespace gles2 {
class ContextGroup;
}

// This class processes commands in a command buffer. It is event driven and
// posts tasks to the current message loop to do additional work.
class GPUProcessor : public CommandBufferEngine {
 public:
  // If a group is not passed in one will be created.
  GPUProcessor(CommandBuffer* command_buffer, gles2::ContextGroup* group);

  // This constructor is for unit tests.
  GPUProcessor(CommandBuffer* command_buffer,
               gles2::GLES2Decoder* decoder,
               CommandParser* parser,
               int commands_per_update);

  virtual ~GPUProcessor();

  // Perform platform specific and common initialization.
  bool Initialize(gfx::PluginWindowHandle hwnd,
                  const gfx::Size& size,
                  const char* allowed_extensions,
                  const std::vector<int32>& attribs,
                  GPUProcessor* parent,
                  uint32 parent_texture_id);

  void Destroy();
  void DestroyCommon();

  virtual void ProcessCommands();

  // Implementation of CommandBufferEngine.
  virtual Buffer GetSharedMemoryBuffer(int32 shm_id);
  virtual void set_token(int32 token);
  virtual bool SetGetOffset(int32 offset);
  virtual int32 GetGetOffset();

  // Asynchronously resizes an offscreen frame buffer.
  void ResizeOffscreenFrameBuffer(const gfx::Size& size);

#if defined(OS_MACOSX)
  // Needed only on Mac OS X, which does not render into an on-screen
  // window and therefore requires the backing store to be resized
  // manually. Returns an opaque identifier for the new backing store.
  // There are two versions of this method: one for use with the IOSurface
  // available in Mac OS X 10.6; and, one for use with the
  // TransportDIB-based version used on Mac OS X 10.5.
  virtual uint64 SetWindowSizeForIOSurface(const gfx::Size& size);
  virtual TransportDIB::Handle SetWindowSizeForTransportDIB(
      const gfx::Size& size);
  virtual void SetTransportDIBAllocAndFree(
      Callback2<size_t, TransportDIB::Handle*>::Type* allocator,
      Callback1<TransportDIB::Id>::Type* deallocator);
  // Returns the id of the current IOSurface, or 0.
  virtual uint64 GetSurfaceId();
#endif

  // Sets a callback that is called when a glResizeCHROMIUM command
  // is processed.
  virtual void SetResizeCallback(Callback1<gfx::Size>::Type* callback);

  // Sets a callback which is called when a SwapBuffers command is processed.
  // Must be called after Initialize().
  // It is not defined on which thread this callback is called.
  virtual void SetSwapBuffersCallback(Callback0::Type* callback);

  // Get the GLES2Decoder associated with this processor.
  gles2::GLES2Decoder* decoder() const { return decoder_.get(); }

 protected:
  // Perform common initialization. Takes ownership of GLContext.
  bool InitializeCommon(gfx::GLContext* context,
                        const gfx::Size& size,
                        const char* allowed_extensions,
                        const std::vector<int32>& attribs,
                        gles2::GLES2Decoder* parent_decoder,
                        uint32 parent_texture_id);


 private:
  // Called via a callback just before we are supposed to call the
  // user's swap buffers callback.
  virtual void WillSwapBuffers();

  // The GPUProcessor holds a weak reference to the CommandBuffer. The
  // CommandBuffer owns the GPUProcessor and holds a strong reference to it
  // through the ProcessCommands callback.
  CommandBuffer* command_buffer_;

  int commands_per_update_;

  scoped_ptr<gles2::GLES2Decoder> decoder_;
  scoped_ptr<CommandParser> parser_;

#if defined(OS_MACOSX)
  scoped_ptr<AcceleratedSurface> surface_;
#endif

  ScopedRunnableMethodFactory<GPUProcessor> method_factory_;
  scoped_ptr<Callback0::Type> wrapped_swap_buffers_callback_;
};

}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_SERVICE_GPU_PROCESSOR_H_