summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/client/share_group.cc
blob: 39d9b85f512a7052b3e25f981e1ce317716bb9c4 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) 2012 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.

#include "../client/atomicops.h"
#include "../client/share_group.h"
#include "../client/gles2_implementation.h"
#include "../client/program_info_manager.h"
#include "../common/id_allocator.h"
#include "../common/logging.h"

namespace gpu {
namespace gles2 {

COMPILE_ASSERT(gpu::kInvalidResource == 0,
               INVALID_RESOURCE_NOT_0_AS_GL_EXPECTS);

// The standard id handler.
class IdHandler : public IdHandlerInterface {
 public:
  IdHandler() { }
  virtual ~IdHandler() { }

  // Overridden from IdHandlerInterface.
  virtual void Destroy(GLES2Implementation* /* gl_impl */) {
  }

  // Overridden from IdHandlerInterface.
  virtual void MakeIds(
      GLES2Implementation* /* gl_impl */,
      GLuint id_offset, GLsizei n, GLuint* ids) {
    if (id_offset == 0) {
      for (GLsizei ii = 0; ii < n; ++ii) {
        ids[ii] = id_allocator_.AllocateID();
      }
    } else {
      for (GLsizei ii = 0; ii < n; ++ii) {
        ids[ii] = id_allocator_.AllocateIDAtOrAbove(id_offset);
        id_offset = ids[ii] + 1;
      }
    }
  }

  // Overridden from IdHandlerInterface.
  virtual bool FreeIds(
      GLES2Implementation* gl_impl,
      GLsizei n, const GLuint* ids, DeleteFn delete_fn) {
    for (GLsizei ii = 0; ii < n; ++ii) {
      id_allocator_.FreeID(ids[ii]);
    }
    (gl_impl->*delete_fn)(n, ids);
    // We need to ensure that the delete call is evaluated on the service side
    // before any other contexts issue commands using these client ids.
    gl_impl->helper()->CommandBufferHelper::Flush();
    return true;
  }

  // Overridden from IdHandlerInterface.
  virtual bool MarkAsUsedForBind(GLuint id) {
    return id == 0 ? true : id_allocator_.MarkAsUsed(id);
  }
 protected:
  IdAllocator id_allocator_;
};

// An id handler that require Gen before Bind.
class StrictIdHandler : public IdHandler {
 public:
  StrictIdHandler() { }
  virtual ~StrictIdHandler() { }

  // Overridden from IdHandler.
  virtual bool MarkAsUsedForBind(GLuint id) {
    GPU_DCHECK(id == 0 || id_allocator_.InUse(id));
    return IdHandler::MarkAsUsedForBind(id);
  }
};

// An id handler for ids that are never reused.
class NonReusedIdHandler : public IdHandlerInterface {
 public:
  NonReusedIdHandler() : last_id_(0) { }
  virtual ~NonReusedIdHandler() { }

  // Overridden from IdHandlerInterface.
  virtual void Destroy(GLES2Implementation* /* gl_impl */) {
  }

  // Overridden from IdHandlerInterface.
  virtual void MakeIds(
      GLES2Implementation* /* gl_impl */,
      GLuint id_offset, GLsizei n, GLuint* ids) {
    for (GLsizei ii = 0; ii < n; ++ii) {
      ids[ii] = ++last_id_ + id_offset;
    }
  }

  // Overridden from IdHandlerInterface.
  virtual bool FreeIds(
      GLES2Implementation* gl_impl,
      GLsizei n, const GLuint* ids, DeleteFn delete_fn) {
    // Ids are never freed.
    (gl_impl->*delete_fn)(n, ids);
    return true;
  }

  // Overridden from IdHandlerInterface.
  virtual bool MarkAsUsedForBind(GLuint /* id */) {
    // This is only used for Shaders and Programs which have no bind.
    return false;
  }

 private:
  GLuint last_id_;
};

// An id handler for shared ids.
class SharedIdHandler : public IdHandlerInterface {
 public:
  SharedIdHandler(
      id_namespaces::IdNamespaces id_namespace)
      : id_namespace_(id_namespace) {
  }

  virtual ~SharedIdHandler() { }

  // Overridden from IdHandlerInterface.
  virtual void Destroy(GLES2Implementation* /* gl_impl */) {
  }

  virtual void MakeIds(
      GLES2Implementation* gl_impl,
      GLuint id_offset, GLsizei n, GLuint* ids) {
    gl_impl->GenSharedIdsCHROMIUM(id_namespace_, id_offset, n, ids);
  }

  virtual bool FreeIds(
      GLES2Implementation* gl_impl,
      GLsizei n, const GLuint* ids, DeleteFn delete_fn) {
    gl_impl->DeleteSharedIdsCHROMIUM(id_namespace_, n, ids);
    (gl_impl->*delete_fn)(n, ids);
    // We need to ensure that the delete call is evaluated on the service side
    // before any other contexts issue commands using these client ids.
    gl_impl->helper()->CommandBufferHelper::Flush();
    return true;
  }

  virtual bool MarkAsUsedForBind(GLuint id) {
    // This has no meaning for shared resources.
    return true;
  }

 private:
  id_namespaces::IdNamespaces id_namespace_;
};

class ThreadSafeIdHandlerWrapper : public IdHandlerInterface {
 public:
  ThreadSafeIdHandlerWrapper(IdHandlerInterface* id_handler)
      : id_handler_(id_handler) {
  }
  virtual ~ThreadSafeIdHandlerWrapper() { }

  // Overridden from IdHandlerInterface.
  virtual void Destroy(GLES2Implementation* gl_impl) {
    AutoLock auto_lock(lock_);
    id_handler_->Destroy(gl_impl);
  }

  // Overridden from IdHandlerInterface.
  virtual void MakeIds(
      GLES2Implementation* gl_impl, GLuint id_offset, GLsizei n, GLuint* ids) {
    AutoLock auto_lock(lock_);
    id_handler_->MakeIds(gl_impl, id_offset, n, ids);
  }

  // Overridden from IdHandlerInterface.
  virtual bool FreeIds(
      GLES2Implementation* gl_impl, GLsizei n, const GLuint* ids,
      DeleteFn delete_fn) {
    AutoLock auto_lock(lock_);
    return id_handler_->FreeIds(gl_impl, n, ids, delete_fn);
  }

  // Overridden from IdHandlerInterface.
  virtual bool MarkAsUsedForBind(GLuint id) {
    AutoLock auto_lock(lock_);
    return id_handler_->MarkAsUsedForBind(id);
  }

 private:
   IdHandlerInterface* id_handler_;
   Lock lock_;
};

ShareGroup::ShareGroup(bool share_resources, bool bind_generates_resource)
    : sharing_resources_(share_resources),
      bind_generates_resource_(bind_generates_resource),
      gles2_(NULL) {
  GPU_CHECK(ShareGroup::ImplementsThreadSafeReferenceCounting());

  if (bind_generates_resource) {
    for (int i = 0; i < id_namespaces::kNumIdNamespaces; ++i) {
      if (i == id_namespaces::kProgramsAndShaders) {
        id_handlers_[i].reset(new ThreadSafeIdHandlerWrapper(
            new NonReusedIdHandler()));
      } else {
        id_handlers_[i].reset(new ThreadSafeIdHandlerWrapper(
            new IdHandler()));
      }
    }
  } else {
    for (int i = 0; i < id_namespaces::kNumIdNamespaces; ++i) {
      if (i == id_namespaces::kProgramsAndShaders) {
        id_handlers_[i].reset(new ThreadSafeIdHandlerWrapper(
            new NonReusedIdHandler()));
      } else {
        id_handlers_[i].reset(new ThreadSafeIdHandlerWrapper(
            new StrictIdHandler()));
      }
    }
  }
  program_info_manager_.reset(ProgramInfoManager::Create(false));
}

void ShareGroup::SetGLES2ImplementationForDestruction(
    GLES2Implementation* gl_impl) {
  gles2_ = gl_impl;
}

ShareGroup::~ShareGroup() {
  for (int i = 0; i < id_namespaces::kNumIdNamespaces; ++i) {
    id_handlers_[i]->Destroy(gles2_);
    id_handlers_[i].reset();
  }
}

}  // namespace gles2
}  // namespace gpu