summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/client/gles2_implementation.cc
blob: e60e68b5a12aa11b29d66b67c11535ea1dd1e71e (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
// Copyright (c) 2006-2009 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.

// A class to emluate GLES2 over command buffers.

#include "gpu/command_buffer/client/gles2_implementation.h"
// TODO(gman): remove when all functions have been implemented.
#include "gpu/command_buffer/client/gles2_implementation_gen.h"
#include "gpu/command_buffer/common/gles2_cmd_utils.h"

namespace command_buffer {
namespace gles2 {

GLES2Implementation::GLES2Implementation(
      GLES2CmdHelper* helper,
      void* transfer_buffer,
      int transfer_buffer_id)
    : util_(0),  // TODO(gman): Get real number of compressed texture formats.
      helper_(helper),
      shared_memory_(transfer_buffer, transfer_buffer_id),
      pack_alignment_(4),
      unpack_alignment_(4) {
}

void GLES2Implementation::MakeIds(GLsizei n, GLuint* ids) {
  for (GLsizei ii = 0; ii < n; ++ii) {
    ids[ii] = id_allocator_.AllocateID();
  }
}

void GLES2Implementation::FreeIds(GLsizei n, const GLuint* ids) {
  for (GLsizei ii = 0; ii < n; ++ii) {
    id_allocator_.FreeID(ids[ii]);
  }
}

void GLES2Implementation::DrawElements(
    GLenum mode, GLsizei count, GLenum type, const void* indices) {
  helper_->DrawElements(mode, count, type, reinterpret_cast<GLuint>(indices));
}

void GLES2Implementation::VertexAttribPointer(
    GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride,
    const void* ptr) {
  helper_->VertexAttribPointer(index, size, type, normalized, stride,
                               reinterpret_cast<GLuint>(ptr));
}

void GLES2Implementation::ShaderSource(
    GLuint shader, GLsizei count, const char** string, const GLint* length) {
  // TODO(gman): change to use buckets and check that there is enough room.
  uint32* offsets = shared_memory_.GetAddressAs<uint32*>(0);
  char* strings = reinterpret_cast<char*>(offsets + count);

  uint32 offset = count * sizeof(*offsets);
  for (GLsizei ii = 0; ii < count; ++ii) {
    uint32 len = length ? length[ii] : strlen(string[ii]);
    memcpy(strings + offset, string[ii], len);
    offset += len;
    offsets[ii] = offset;
  }

  helper_->ShaderSource(shader, count, shared_memory_.GetId(), 0, offset);
  // TODO(gman): Should insert token but not wait until we need shared memory
  //     again. Really, I should implement a shared memory manager that puts
  //     things in the next unused part of shared memory and only blocks
  //     when it needs more memory.
  int32 token = helper_->InsertToken();
  helper_->WaitForToken(token);
}

void GLES2Implementation::BufferData(
    GLenum target, GLsizeiptr size, const void* data, GLenum usage) {
  // TODO(gman): Switch to use buckets alwayst or at least if no room in shared
  //    memory.
  memcpy(shared_memory_.GetAddress(0), data, size);
  helper_->BufferData(target, size, shared_memory_.GetId(), 0, usage);
  int32 token = helper_->InsertToken();
  helper_->WaitForToken(token);
}

void GLES2Implementation::BufferSubData(
    GLenum target, GLintptr offset, GLsizeiptr size, const void* data) {
  // TODO(gman): Switch to use buckets alwayst or at least if no room in shared
  //    memory.
  memcpy(shared_memory_.GetAddress(0), data, size);
  helper_->BufferSubData(target, offset, size, shared_memory_.GetId(), 0);
  int32 token = helper_->InsertToken();
  helper_->WaitForToken(token);
}

void GLES2Implementation::CompressedTexImage2D(
    GLenum target, GLint level, GLenum internalformat, GLsizei width,
    GLsizei height, GLint border, GLsizei imageSize, const void* data) {
  // TODO(gman): Switch to use buckets alwayst or at least if no room in shared
  //    memory.
  memcpy(shared_memory_.GetAddress(0), data, imageSize);
  helper_->CompressedTexImage2D(
      target, level, internalformat, width, height, border, imageSize,
      shared_memory_.GetId(), 0);
  int32 token = helper_->InsertToken();
  helper_->WaitForToken(token);
}

void GLES2Implementation::CompressedTexSubImage2D(
    GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width,
    GLsizei height, GLenum format, GLsizei imageSize, const void* data) {
  // TODO(gman): Switch to use buckets alwayst or at least if no room in shared
  //    memory.
  memcpy(shared_memory_.GetAddress(0), data, imageSize);
  helper_->CompressedTexSubImage2D(
      target, level, xoffset, yoffset, width, height, format, imageSize,
      shared_memory_.GetId(), 0);
  int32 token = helper_->InsertToken();
  helper_->WaitForToken(token);
}

void GLES2Implementation::TexImage2D(
    GLenum target, GLint level, GLint internalformat, GLsizei width,
    GLsizei height, GLint border, GLenum format, GLenum type,
    const void* pixels) {
  // TODO(gman): Switch to use buckets alwayst or at least if no room in shared
  //    memory.
  uint32 pixels_size = GLES2Util::ComputeImageDataSize(
      width, height, format, type, unpack_alignment_);
  memcpy(shared_memory_.GetAddress(0), pixels, pixels_size);
  helper_->TexImage2D(
      target, level, internalformat, width, height, border, format, type,
      shared_memory_.GetId(), 0);
  int32 token = helper_->InsertToken();
  helper_->WaitForToken(token);
}

void GLES2Implementation::TexSubImage2D(
    GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width,
    GLsizei height, GLenum format, GLenum type, const void* pixels) {
  // TODO(gman): Switch to use buckets alwayst or at least if no room in shared
  //    memory.
  uint32 pixels_size = GLES2Util::ComputeImageDataSize(
      width, height, format, type, unpack_alignment_);
  memcpy(shared_memory_.GetAddress(0), pixels, pixels_size);
  helper_->TexSubImage2D(
      target, level, xoffset, yoffset, width, height, format, type,
      shared_memory_.GetId(), 0);
  int32 token = helper_->InsertToken();
  helper_->WaitForToken(token);
}


}  // namespace gles2
}  // namespace command_buffer