1 // Copyright (c) 2012 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 GPU_COMMAND_BUFFER_CLIENT_SHARE_GROUP_H_
6 #define GPU_COMMAND_BUFFER_CLIENT_SHARE_GROUP_H_
9 #include "base/memory/scoped_ptr.h"
10 #include "gles2_impl_export.h"
11 #include "gpu/command_buffer/client/ref_counted.h"
12 #include "gpu/command_buffer/common/gles2_cmd_format.h"
17 class GLES2Implementation
;
18 class GLES2ImplementationTest
;
19 class ProgramInfoManager
;
21 typedef void (GLES2Implementation::*DeleteFn
)(GLsizei n
, const GLuint
* ids
);
22 typedef void (GLES2Implementation::*DeleteRangeFn
)(const GLuint first_id
,
24 typedef void (GLES2Implementation::*BindFn
)(GLenum target
, GLuint id
);
25 typedef void (GLES2Implementation::*BindIndexedFn
)( \
26 GLenum target
, GLuint index
, GLuint id
);
27 typedef void (GLES2Implementation::*BindIndexedRangeFn
)( \
28 GLenum target
, GLuint index
, GLuint id
, GLintptr offset
, GLsizeiptr size
);
30 class ShareGroupContextData
{
32 struct IdHandlerData
{
36 std::vector
<GLuint
> freed_ids_
;
37 uint32 flush_generation_
;
40 IdHandlerData
* id_handler_data(int namespace_id
) {
41 return &id_handler_data_
[namespace_id
];
45 IdHandlerData id_handler_data_
[id_namespaces::kNumIdNamespaces
];
48 // Base class for IdHandlers
49 class IdHandlerInterface
{
51 IdHandlerInterface() { }
52 virtual ~IdHandlerInterface() { }
54 // Makes some ids at or above id_offset.
56 GLES2Implementation
* gl_impl
,
57 GLuint id_offset
, GLsizei n
, GLuint
* ids
) = 0;
61 GLES2Implementation
* gl_impl
, GLsizei n
, const GLuint
* ids
,
62 DeleteFn delete_fn
) = 0;
64 // Marks an id as used for glBind functions. id = 0 does nothing.
65 virtual bool MarkAsUsedForBind(
66 GLES2Implementation
* gl_impl
,
70 // This is for glBindBufferBase.
71 virtual bool MarkAsUsedForBind(
72 GLES2Implementation
* gl_impl
,
76 BindIndexedFn bind_fn
) = 0;
77 // This is for glBindBufferRange.
78 virtual bool MarkAsUsedForBind(
79 GLES2Implementation
* gl_impl
,
85 BindIndexedRangeFn bind_fn
) = 0;
87 // Called when a context in the share group is destructed.
88 virtual void FreeContext(GLES2Implementation
* gl_impl
) = 0;
91 class RangeIdHandlerInterface
{
93 RangeIdHandlerInterface() {}
94 virtual ~RangeIdHandlerInterface() {}
96 // Makes a continuous range of ids. Stores the first allocated id to
97 // |first_id| or 0 if allocation failed.
98 virtual void MakeIdRange(GLES2Implementation
* gl_impl
,
100 GLuint
* first_id
) = 0;
102 // Frees a continuous |range| of ids beginning at |first_id|.
103 virtual void FreeIdRange(GLES2Implementation
* gl_impl
,
104 const GLuint first_id
,
106 DeleteRangeFn delete_fn
) = 0;
108 // Called when a context in the share group is destructed.
109 virtual void FreeContext(GLES2Implementation
* gl_impl
) = 0;
112 // ShareGroup manages shared resources for contexts that are sharing resources.
113 class GLES2_IMPL_EXPORT ShareGroup
114 : public gpu::RefCountedThreadSafe
<ShareGroup
> {
116 ShareGroup(bool bind_generates_resource
);
118 bool bind_generates_resource() const {
119 return bind_generates_resource_
;
122 IdHandlerInterface
* GetIdHandler(int namespace_id
) const {
123 return id_handlers_
[namespace_id
].get();
126 RangeIdHandlerInterface
* GetRangeIdHandler(int range_namespace_id
) const {
127 return range_id_handlers_
[range_namespace_id
].get();
130 ProgramInfoManager
* program_info_manager() {
131 return program_info_manager_
.get();
134 void FreeContext(GLES2Implementation
* gl_impl
) {
135 for (int i
= 0; i
< id_namespaces::kNumIdNamespaces
; ++i
) {
136 id_handlers_
[i
]->FreeContext(gl_impl
);
138 for (auto& range_id_handler
: range_id_handlers_
) {
139 range_id_handler
->FreeContext(gl_impl
);
144 friend class gpu::RefCountedThreadSafe
<ShareGroup
>;
145 friend class gpu::gles2::GLES2ImplementationTest
;
148 // Install a new program info manager. Used for testing only;
149 void set_program_info_manager(ProgramInfoManager
* manager
);
151 scoped_ptr
<IdHandlerInterface
> id_handlers_
[id_namespaces::kNumIdNamespaces
];
152 scoped_ptr
<RangeIdHandlerInterface
>
153 range_id_handlers_
[id_namespaces::kNumRangeIdNamespaces
];
154 scoped_ptr
<ProgramInfoManager
> program_info_manager_
;
156 bool bind_generates_resource_
;
158 DISALLOW_COPY_AND_ASSIGN(ShareGroup
);
164 #endif // GPU_COMMAND_BUFFER_CLIENT_SHARE_GROUP_H_