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 #include "gpu/command_buffer/client/share_group.h"
7 #include "base/logging.h"
8 #include "base/synchronization/lock.h"
9 #include "gpu/command_buffer/client/gles2_implementation.h"
10 #include "gpu/command_buffer/client/program_info_manager.h"
11 #include "gpu/command_buffer/common/id_allocator.h"
16 COMPILE_ASSERT(gpu::kInvalidResource == 0,
17 INVALID_RESOURCE_NOT_0_AS_GL_EXPECTS);
19 // The standard id handler.
20 class IdHandler : public IdHandlerInterface {
23 virtual ~IdHandler() { }
25 // Overridden from IdHandlerInterface.
27 GLES2Implementation* /* gl_impl */,
28 GLuint id_offset, GLsizei n, GLuint* ids) OVERRIDE {
29 base::AutoLock auto_lock(lock_);
31 for (GLsizei ii = 0; ii < n; ++ii) {
32 ids[ii] = id_allocator_.AllocateID();
35 for (GLsizei ii = 0; ii < n; ++ii) {
36 ids[ii] = id_allocator_.AllocateIDAtOrAbove(id_offset);
37 id_offset = ids[ii] + 1;
42 // Overridden from IdHandlerInterface.
44 GLES2Implementation* gl_impl,
45 GLsizei n, const GLuint* ids, DeleteFn delete_fn) OVERRIDE {
46 base::AutoLock auto_lock(lock_);
47 for (GLsizei ii = 0; ii < n; ++ii) {
48 id_allocator_.FreeID(ids[ii]);
50 (gl_impl->*delete_fn)(n, ids);
51 // We need to ensure that the delete call is evaluated on the service side
52 // before any other contexts issue commands using these client ids.
53 gl_impl->helper()->CommandBufferHelper::Flush();
57 // Overridden from IdHandlerInterface.
58 virtual bool MarkAsUsedForBind(GLuint id) OVERRIDE {
61 base::AutoLock auto_lock(lock_);
62 return id_allocator_.MarkAsUsed(id);
67 IdAllocator id_allocator_;
70 // An id handler that requires Gen before Bind.
71 class StrictIdHandler : public IdHandler {
74 virtual ~StrictIdHandler() {}
76 // Overridden from IdHandler.
77 virtual bool MarkAsUsedForBind(GLuint id) OVERRIDE {
80 base::AutoLock auto_lock(lock_);
81 DCHECK(id == 0 || id_allocator_.InUse(id));
88 // An id handler for ids that are never reused.
89 class NonReusedIdHandler : public IdHandlerInterface {
91 NonReusedIdHandler() : last_id_(0) {}
92 virtual ~NonReusedIdHandler() {}
94 // Overridden from IdHandlerInterface.
96 GLES2Implementation* /* gl_impl */,
97 GLuint id_offset, GLsizei n, GLuint* ids) OVERRIDE {
98 base::AutoLock auto_lock(lock_);
99 for (GLsizei ii = 0; ii < n; ++ii) {
100 ids[ii] = ++last_id_ + id_offset;
104 // Overridden from IdHandlerInterface.
105 virtual bool FreeIds(
106 GLES2Implementation* gl_impl,
107 GLsizei n, const GLuint* ids, DeleteFn delete_fn) OVERRIDE {
108 // Ids are never freed.
109 (gl_impl->*delete_fn)(n, ids);
113 // Overridden from IdHandlerInterface.
114 virtual bool MarkAsUsedForBind(GLuint /* id */) OVERRIDE {
115 // This is only used for Shaders and Programs which have no bind.
124 ShareGroup::ShareGroup(bool bind_generates_resource)
125 : bind_generates_resource_(bind_generates_resource) {
126 if (bind_generates_resource) {
127 for (int i = 0; i < id_namespaces::kNumIdNamespaces; ++i) {
128 if (i == id_namespaces::kProgramsAndShaders) {
129 id_handlers_[i].reset(new NonReusedIdHandler());
131 id_handlers_[i].reset(new IdHandler());
135 for (int i = 0; i < id_namespaces::kNumIdNamespaces; ++i) {
136 if (i == id_namespaces::kProgramsAndShaders) {
137 id_handlers_[i].reset(new NonReusedIdHandler());
139 id_handlers_[i].reset(new StrictIdHandler());
143 program_info_manager_.reset(ProgramInfoManager::Create(false));
146 void ShareGroup::set_program_info_manager(ProgramInfoManager* manager) {
147 program_info_manager_.reset(manager);
150 ShareGroup::~ShareGroup() {}