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

gpu / command_buffer / client / buffer_tracker.cc [blame]

// 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/buffer_tracker.h"

#include <stdint.h>

#include <utility>
#include "gpu/command_buffer/client/mapped_memory.h"

namespace gpu {
namespace gles2 {

BufferTracker::BufferTracker(MappedMemoryManager* manager)
    : mapped_memory_(manager) {
}

BufferTracker::~BufferTracker() {
  while (!buffers_.empty()) {
    RemoveBuffer(buffers_.begin()->first);
  }
}

BufferTracker::Buffer* BufferTracker::CreateBuffer(
    GLuint id, GLsizeiptr size) {
  DCHECK_NE(0u, id);
  DCHECK_LE(0, size);
  int32_t shm_id = -1;
  uint32_t shm_offset = 0;
  void* address = nullptr;
  if (size)
    address = mapped_memory_->Alloc(size, &shm_id, &shm_offset);

  Buffer* buffer = new Buffer(id, size, shm_id, shm_offset, address);
  std::pair<BufferMap::iterator, bool> result =
      buffers_.insert(std::make_pair(id, buffer));
  DCHECK(result.second);
  return buffer;
}

BufferTracker::Buffer* BufferTracker::GetBuffer(GLuint client_id) {
  BufferMap::iterator it = buffers_.find(client_id);
  return it != buffers_.end() ? it->second : nullptr;
}

void BufferTracker::RemoveBuffer(GLuint client_id) {
  BufferMap::iterator it = buffers_.find(client_id);
  if (it != buffers_.end()) {
    Buffer* buffer = it->second;
    buffers_.erase(it);
    if (buffer->address_)
      mapped_memory_->Free(buffer->address_);
    delete buffer;
  }
}

void BufferTracker::FreePendingToken(Buffer* buffer, int32_t token) {
  if (buffer->address_)
    mapped_memory_->FreePendingToken(buffer->address_, token);
  buffer->size_ = 0;
  buffer->shm_id_ = 0;
  buffer->shm_offset_ = 0;
  buffer->address_ = nullptr;
  buffer->last_usage_token_ = 0;
  buffer->last_async_upload_token_ = 0;
}

void BufferTracker::Unmanage(Buffer* buffer) {
  buffer->size_ = 0;
  buffer->shm_id_ = 0;
  buffer->shm_offset_ = 0;
  buffer->address_ = nullptr;
  buffer->last_usage_token_ = 0;
  buffer->last_async_upload_token_ = 0;
}

void BufferTracker::Free(Buffer* buffer) {
  if (buffer->address_)
    mapped_memory_->Free(buffer->address_);

  buffer->size_ = 0;
  buffer->shm_id_ = 0;
  buffer->shm_offset_ = 0;
  buffer->address_ = nullptr;
  buffer->last_usage_token_ = 0;
  buffer->last_async_upload_token_ = 0;
}

}  // namespace gles2
}  // namespace gpu