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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/client/share_group.h"
#include <stdint.h>
#include <memory>
#include <vector>
#include "base/containers/stack.h"
#include "base/logging.h"
#include "base/synchronization/lock.h"
#include "gpu/command_buffer/client/gles2_cmd_helper.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "gpu/command_buffer/client/program_info_manager.h"
#include "gpu/command_buffer/common/id_allocator.h"
namespace gpu {
namespace gles2 {
ShareGroupContextData::IdHandlerData::IdHandlerData() : flush_generation_(0) {}
ShareGroupContextData::IdHandlerData::~IdHandlerData() = default;
static_assert(gpu::kInvalidResource == 0,
"GL expects kInvalidResource to be 0");
// The standard id handler.
class IdHandler : public IdHandlerInterface {
public:
IdHandler() = default;
~IdHandler() override = default;
// Overridden from IdHandlerInterface.
void MakeIds(GLES2Implementation* /* gl_impl */,
GLuint id_offset,
GLsizei n,
GLuint* ids) override {
base::AutoLock auto_lock(lock_);
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.
bool FreeIds(GLES2Implementation* gl_impl,
GLsizei n,
const GLuint* ids,
DeleteFn delete_fn) override {
base::AutoLock auto_lock(lock_);
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::OrderingBarrier();
return true;
}
// Overridden from IdHandlerInterface.
bool MarkAsUsedForBind(GLES2Implementation* gl_impl,
GLenum target,
GLuint id,
BindFn bind_fn) override {
base::AutoLock auto_lock(lock_);
bool result = id ? id_allocator_.MarkAsUsed(id) : true;
(gl_impl->*bind_fn)(target, id);
return result;
}
bool MarkAsUsedForBind(GLES2Implementation* gl_impl,
GLenum target,
GLuint index,
GLuint id,
BindIndexedFn bind_fn) override {
base::AutoLock auto_lock(lock_);
bool result = id ? id_allocator_.MarkAsUsed(id) : true;
(gl_impl->*bind_fn)(target, index, id);
return result;
}
bool MarkAsUsedForBind(GLES2Implementation* gl_impl,
GLenum target,
GLuint index,
GLuint id,
GLintptr offset,
GLsizeiptr size,
BindIndexedRangeFn bind_fn) override {
base::AutoLock auto_lock(lock_);
bool result = id ? id_allocator_.MarkAsUsed(id) : true;
(gl_impl->*bind_fn)(target, index, id, offset, size);
return result;
}
void FreeContext(GLES2Implementation* gl_impl) override {}
private:
base::Lock lock_;
IdAllocator id_allocator_ GUARDED_BY(lock_);
};
// An id handler that requires Gen before Bind.
class StrictIdHandler : public IdHandlerInterface {
public:
explicit StrictIdHandler(int id_namespace) : id_namespace_(id_namespace) {}
~StrictIdHandler() override = default;
// Overridden from IdHandler.
void MakeIds(GLES2Implementation* gl_impl,
GLuint /* id_offset */,
GLsizei n,
GLuint* ids) override {
base::AutoLock auto_lock(lock_);
// Collect pending FreeIds from other flush_generation.
CollectPendingFreeIds(gl_impl);
for (GLsizei ii = 0; ii < n; ++ii) {
if (!free_ids_.empty()) {
// Allocate a previously freed Id.
ids[ii] = free_ids_.top();
free_ids_.pop();
// Record kIdInUse state.
DCHECK(id_states_[ids[ii] - 1] == kIdFree);
id_states_[ids[ii] - 1] = kIdInUse;
} else {
// Allocate a new Id.
id_states_.push_back(kIdInUse);
ids[ii] = id_states_.size();
}
}
}
// Overridden from IdHandler.
bool FreeIds(GLES2Implementation* gl_impl,
GLsizei n,
const GLuint* ids,
DeleteFn delete_fn) override {
// Delete stub must run before CollectPendingFreeIds.
(gl_impl->*delete_fn)(n, ids);
bool return_value = true;
{
base::AutoLock auto_lock(lock_);
// Collect pending FreeIds from other flush_generation.
CollectPendingFreeIds(gl_impl);
// Save Ids to free in a later flush_generation.
ShareGroupContextData::IdHandlerData* ctxt_data =
gl_impl->share_group_context_data()->id_handler_data(id_namespace_);
GLuint max_valid_id = id_states_.size();
for (GLsizei ii = 0; ii < n; ++ii) {
GLuint id = ids[ii];
if (id != 0) {
if (id > max_valid_id) {
// Caller will generate an error.
return_value = false;
continue;
}
// Save freed Id for later.
if (id_states_[id - 1] != kIdInUse) {
DVLOG(1) << "Already freed id " << id;
return_value = false;
continue;
}
id_states_[id - 1] = kIdPendingFree;
ctxt_data->freed_ids_.push_back(id);
}
}
}
return return_value;
}
// Overridden from IdHandler.
bool MarkAsUsedForBind(GLES2Implementation* gl_impl,
GLenum target,
GLuint id,
BindFn bind_fn) override {
#ifndef NDEBUG
if (id != 0) {
base::AutoLock auto_lock(lock_);
DCHECK(id_states_[id - 1] == kIdInUse);
}
#endif
// StrictIdHandler is used if |bind_generates_resource| is false. In that
// case, |bind_fn| will not use Flush() after helper->Bind*(), so it is OK
// to call |bind_fn| without holding the lock.
(gl_impl->*bind_fn)(target, id);
return true;
}
bool MarkAsUsedForBind(GLES2Implementation* gl_impl,
GLenum target,
GLuint index,
GLuint id,
BindIndexedFn bind_fn) override {
#ifndef NDEBUG
if (id != 0) {
base::AutoLock auto_lock(lock_);
DCHECK(id_states_[id - 1] == kIdInUse);
}
#endif
// StrictIdHandler is used if |bind_generates_resource| is false. In that
// case, |bind_fn| will not use Flush() after helper->Bind*(), so it is OK
// to call |bind_fn| without holding the lock.
(gl_impl->*bind_fn)(target, index, id);
return true;
}
bool MarkAsUsedForBind(GLES2Implementation* gl_impl,
GLenum target,
GLuint index,
GLuint id,
GLintptr offset,
GLsizeiptr size,
BindIndexedRangeFn bind_fn) override {
#ifndef NDEBUG
if (id != 0) {
base::AutoLock auto_lock(lock_);
DCHECK(id_states_[id - 1] == kIdInUse);
}
#endif
// StrictIdHandler is used if |bind_generates_resource| is false. In that
// case, |bind_fn| will not use Flush() after helper->Bind*(), so it is OK
// to call |bind_fn| without holding the lock.
(gl_impl->*bind_fn)(target, index, id, offset, size);
return true;
}
// Overridden from IdHandlerInterface.
void FreeContext(GLES2Implementation* gl_impl) override {
base::AutoLock auto_lock(lock_);
CollectPendingFreeIds(gl_impl);
}
private:
enum IdState { kIdFree, kIdPendingFree, kIdInUse };
void CollectPendingFreeIds(GLES2Implementation* gl_impl)
EXCLUSIVE_LOCKS_REQUIRED(lock_) {
uint32_t flush_generation = gl_impl->helper()->flush_generation();
ShareGroupContextData::IdHandlerData* ctxt_data =
gl_impl->share_group_context_data()->id_handler_data(id_namespace_);
if (ctxt_data->flush_generation_ != flush_generation) {
ctxt_data->flush_generation_ = flush_generation;
for (uint32_t ii = 0; ii < ctxt_data->freed_ids_.size(); ++ii) {
const GLuint id = ctxt_data->freed_ids_[ii];
DCHECK(id_states_[id - 1] == kIdPendingFree);
id_states_[id - 1] = kIdFree;
free_ids_.push(id);
}
ctxt_data->freed_ids_.clear();
}
}
int id_namespace_;
base::Lock lock_;
std::vector<uint8_t> id_states_ GUARDED_BY(lock_);
base::stack<uint32_t> free_ids_ GUARDED_BY(lock_);
};
// An id handler for ids that are never reused.
class NonReusedIdHandler : public IdHandlerInterface {
public:
NonReusedIdHandler() : last_id_(0) {}
~NonReusedIdHandler() override = default;
// Overridden from IdHandlerInterface.
void MakeIds(GLES2Implementation* /* gl_impl */,
GLuint id_offset,
GLsizei n,
GLuint* ids) override {
base::AutoLock auto_lock(lock_);
for (GLsizei ii = 0; ii < n; ++ii) {
ids[ii] = ++last_id_ + id_offset;
}
}
// Overridden from IdHandlerInterface.
bool FreeIds(GLES2Implementation* gl_impl,
GLsizei n,
const GLuint* ids,
DeleteFn delete_fn) override {
// Ids are never freed.
(gl_impl->*delete_fn)(n, ids);
return true;
}
// Overridden from IdHandlerInterface.
bool MarkAsUsedForBind(GLES2Implementation* /* gl_impl */,
GLenum /* target */,
GLuint /* id */,
BindFn /* bind_fn */) override {
// This is only used for Shaders and Programs which have no bind.
return false;
}
bool MarkAsUsedForBind(GLES2Implementation* /* gl_impl */,
GLenum /* target */,
GLuint /* index */,
GLuint /* id */,
BindIndexedFn /* bind_fn */) override {
// This is only used for Shaders and Programs which have no bind.
return false;
}
bool MarkAsUsedForBind(GLES2Implementation* /* gl_impl */,
GLenum /* target */,
GLuint /* index */,
GLuint /* id */,
GLintptr /* offset */,
GLsizeiptr /* size */,
BindIndexedRangeFn /* bind_fn */) override {
// This is only used for Shaders and Programs which have no bind.
return false;
}
void FreeContext(GLES2Implementation* gl_impl) override {}
private:
base::Lock lock_;
GLuint last_id_ GUARDED_BY(lock_);
};
class RangeIdHandler : public RangeIdHandlerInterface {
public:
RangeIdHandler() = default;
void MakeIdRange(GLES2Implementation* /*gl_impl*/,
GLsizei n,
GLuint* first_id) override {
base::AutoLock auto_lock(lock_);
*first_id = id_allocator_.AllocateIDRange(n);
}
void FreeIdRange(GLES2Implementation* gl_impl,
const GLuint first_id,
GLsizei range,
DeleteRangeFn delete_fn) override {
base::AutoLock auto_lock(lock_);
DCHECK(range > 0);
id_allocator_.FreeIDRange(first_id, range);
(gl_impl->*delete_fn)(first_id, range);
gl_impl->helper()->CommandBufferHelper::OrderingBarrier();
}
void FreeContext(GLES2Implementation* gl_impl) override {}
private:
base::Lock lock_;
IdAllocator id_allocator_ GUARDED_BY(lock_);
};
ShareGroup::ShareGroup(bool bind_generates_resource, uint64_t tracing_guid)
: bind_generates_resource_(bind_generates_resource),
tracing_guid_(tracing_guid) {
if (bind_generates_resource) {
for (int i = 0;
i < static_cast<int>(SharedIdNamespaces::kNumSharedIdNamespaces);
++i) {
if (i == static_cast<int>(SharedIdNamespaces::kProgramsAndShaders)) {
id_handlers_[i] = std::make_unique<NonReusedIdHandler>();
} else {
id_handlers_[i] = std::make_unique<IdHandler>();
}
}
} else {
for (int i = 0;
i < static_cast<int>(SharedIdNamespaces::kNumSharedIdNamespaces);
++i) {
if (i == static_cast<int>(SharedIdNamespaces::kProgramsAndShaders)) {
id_handlers_[i] = std::make_unique<NonReusedIdHandler>();
} else {
id_handlers_[i] = std::make_unique<StrictIdHandler>(i);
}
}
}
program_info_manager_ = std::make_unique<ProgramInfoManager>();
for (auto& range_id_handler : range_id_handlers_) {
range_id_handler = std::make_unique<RangeIdHandler>();
}
}
void ShareGroup::Lose() {
base::AutoLock hold(lost_lock_);
lost_ = true;
}
bool ShareGroup::IsLost() const {
base::AutoLock hold(lost_lock_);
return lost_;
}
void ShareGroup::SetProgramInfoManagerForTesting(ProgramInfoManager* manager) {
program_info_manager_.reset(manager);
}
ShareGroup::~ShareGroup() = default;
} // namespace gles2
} // namespace gpu
|