Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / client / share_group.cc
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.
4
5 #include "gpu/command_buffer/client/share_group.h"
6
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"
12
13 namespace gpu {
14 namespace gles2 {
15
16 COMPILE_ASSERT(gpu::kInvalidResource == 0,
17                INVALID_RESOURCE_NOT_0_AS_GL_EXPECTS);
18
19 // The standard id handler.
20 class IdHandler : public IdHandlerInterface {
21  public:
22   IdHandler() { }
23   virtual ~IdHandler() { }
24
25   // Overridden from IdHandlerInterface.
26   virtual void MakeIds(
27       GLES2Implementation* /* gl_impl */,
28       GLuint id_offset, GLsizei n, GLuint* ids) OVERRIDE {
29     base::AutoLock auto_lock(lock_);
30     if (id_offset == 0) {
31       for (GLsizei ii = 0; ii < n; ++ii) {
32         ids[ii] = id_allocator_.AllocateID();
33       }
34     } else {
35       for (GLsizei ii = 0; ii < n; ++ii) {
36         ids[ii] = id_allocator_.AllocateIDAtOrAbove(id_offset);
37         id_offset = ids[ii] + 1;
38       }
39     }
40   }
41
42   // Overridden from IdHandlerInterface.
43   virtual bool FreeIds(
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]);
49     }
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();
54     return true;
55   }
56
57   // Overridden from IdHandlerInterface.
58   virtual bool MarkAsUsedForBind(GLuint id) OVERRIDE {
59     if (id == 0)
60       return true;
61     base::AutoLock auto_lock(lock_);
62     return id_allocator_.MarkAsUsed(id);
63   }
64
65  protected:
66   base::Lock lock_;
67   IdAllocator id_allocator_;
68 };
69
70 // An id handler that requires Gen before Bind.
71 class StrictIdHandler : public IdHandler {
72  public:
73   StrictIdHandler() {}
74   virtual ~StrictIdHandler() {}
75
76   // Overridden from IdHandler.
77   virtual bool MarkAsUsedForBind(GLuint id) OVERRIDE {
78 #ifndef NDEBUG
79     {
80       base::AutoLock auto_lock(lock_);
81       DCHECK(id == 0 || id_allocator_.InUse(id));
82     }
83 #endif
84     return true;
85   }
86 };
87
88 // An id handler for ids that are never reused.
89 class NonReusedIdHandler : public IdHandlerInterface {
90  public:
91   NonReusedIdHandler() : last_id_(0) {}
92   virtual ~NonReusedIdHandler() {}
93
94   // Overridden from IdHandlerInterface.
95   virtual void MakeIds(
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;
101     }
102   }
103
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);
110     return true;
111   }
112
113   // Overridden from IdHandlerInterface.
114   virtual bool MarkAsUsedForBind(GLuint /* id */) OVERRIDE {
115     // This is only used for Shaders and Programs which have no bind.
116     return false;
117   }
118
119  private:
120   base::Lock lock_;
121   GLuint last_id_;
122 };
123
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());
130       } else {
131         id_handlers_[i].reset(new IdHandler());
132       }
133     }
134   } else {
135     for (int i = 0; i < id_namespaces::kNumIdNamespaces; ++i) {
136       if (i == id_namespaces::kProgramsAndShaders) {
137         id_handlers_[i].reset(new NonReusedIdHandler());
138       } else {
139         id_handlers_[i].reset(new StrictIdHandler());
140       }
141     }
142   }
143   program_info_manager_.reset(ProgramInfoManager::Create(false));
144 }
145
146 void ShareGroup::set_program_info_manager(ProgramInfoManager* manager) {
147   program_info_manager_.reset(manager);
148 }
149
150 ShareGroup::~ShareGroup() {}
151
152 }  // namespace gles2
153 }  // namespace gpu