summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/buffer_manager.cc
blob: dbc382cf50105ea0cf8260397315381ed881c7bf (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
// Copyright (c) 2010 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 "gpu/command_buffer/service/buffer_manager.h"
#include "base/logging.h"
#include "gpu/command_buffer/common/gles2_cmd_utils.h"
#include "gpu/command_buffer/service/gles2_cmd_decoder.h"

namespace gpu {
namespace gles2 {

void BufferManager::CreateBufferInfo(GLuint client_id, GLuint service_id) {
  std::pair<BufferInfoMap::iterator, bool> result =
      buffer_infos_.insert(
          std::make_pair(client_id,
                         BufferInfo::Ref(new BufferInfo(service_id))));
  DCHECK(result.second);
}

BufferManager::BufferInfo* BufferManager::GetBufferInfo(
    GLuint client_id) {
  BufferInfoMap::iterator it = buffer_infos_.find(client_id);
  return it != buffer_infos_.end() ? it->second : NULL;
}

void BufferManager::RemoveBufferInfo(GLuint client_id) {
  BufferInfoMap::iterator it = buffer_infos_.find(client_id);
  if (it != buffer_infos_.end()) {
    it->second->MarkAsDeleted();
    buffer_infos_.erase(it);
  }
}

void BufferManager::BufferInfo::SetSize(GLsizeiptr size) {
  DCHECK(!IsDeleted());
  if (size != size_) {
    size_ = size;
    ClearCache();
    if (target_ == GL_ELEMENT_ARRAY_BUFFER) {
      shadow_.reset(new int8[size]);
      memset(shadow_.get(), 0, size);
    }
  }
}

bool BufferManager::BufferInfo::SetRange(
    GLintptr offset, GLsizeiptr size, const GLvoid * data) {
  DCHECK(!IsDeleted());
  if (offset + size < offset ||
        offset + size > size_) {
    return false;
  }
  if (target_ == GL_ELEMENT_ARRAY_BUFFER) {
    memcpy(shadow_.get() + offset, data, size);
    ClearCache();
  }
  return true;
}

void BufferManager::BufferInfo::ClearCache() {
  range_set_.clear();
}

template <typename T>
GLuint GetMaxValue(const void* data, GLuint offset, GLsizei count) {
  GLuint max_value = 0;
  const T* element = reinterpret_cast<const T*>(
      static_cast<const int8*>(data) + offset);
  const T* end = element + count;
  for (; element < end; ++element) {
    if (*element > max_value) {
      max_value = *element;
    }
  }
  return max_value;
}

bool BufferManager::BufferInfo::GetMaxValueForRange(
    GLuint offset, GLsizei count, GLenum type, GLuint* max_value) {
  DCHECK_EQ(target_, static_cast<GLenum>(GL_ELEMENT_ARRAY_BUFFER));
  DCHECK(!IsDeleted());
  Range range(offset, count, type);
  RangeToMaxValueMap::iterator it = range_set_.find(range);
  if (it != range_set_.end()) {
    *max_value = it->second;
    return true;
  }

  uint32 size;
  if (!SafeMultiplyUint32(
      count, GLES2Util::GetGLTypeSizeForTexturesAndBuffers(type), &size)) {
    return false;
  }

  if (!SafeAddUint32(offset, size, &size)) {
    return false;
  }

  if (size > static_cast<uint32>(size_)) {
    return false;
  }

  // Scan the range for the max value and store
  GLuint max_v = 0;
  switch (type) {
    case GL_UNSIGNED_BYTE:
      max_v = GetMaxValue<uint8>(shadow_.get(), offset, count);
      break;
    case GL_UNSIGNED_SHORT:
      // Check we are not accessing an odd byte for a 2 byte value.
      if ((offset & 1) != 0) {
        return false;
      }
      max_v = GetMaxValue<uint16>(shadow_.get(), offset, count);
      break;
    default:
      NOTREACHED();  // should never get here by validation.
      break;
  }
  std::pair<RangeToMaxValueMap::iterator, bool> result =
      range_set_.insert(std::make_pair(range, max_v));
  *max_value = max_v;
  return true;
}

}  // namespace gles2
}  // namespace gpu