summaryrefslogtreecommitdiffstats
path: root/ui/gl/async_pixel_transfer_delegate_stub.cc
blob: 86f068735a49c237fb88f23a3cdc8cdffd3a7fe4 (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
// 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 "ui/gl/async_pixel_transfer_delegate_stub.h"

#include "base/shared_memory.h"
#include "build/build_config.h"
#include "ui/gl/gl_bindings.h"

using base::SharedMemory;
using base::SharedMemoryHandle;

namespace {
// Gets the address of the data from shared memory.
void* GetAddress(SharedMemory* shared_memory,
                 uint32 shm_size,
                 uint32 shm_data_offset,
                 uint32 shm_data_size) {
  // Memory bounds have already been validated, so there
  // is just DCHECKS here.
  DCHECK(shared_memory);
  DCHECK(shared_memory->memory());
  DCHECK_LE(shm_data_offset + shm_data_size, shm_size);
  return static_cast<int8*>(shared_memory->memory()) + shm_data_offset;
}
} // namespace

namespace gfx {

#if !defined(OS_ANDROID)
scoped_ptr<AsyncPixelTransferDelegate>
    AsyncPixelTransferDelegate::Create(gfx::GLContext* context) {
  return AsyncPixelTransferDelegateStub::Create(context);
}
#endif

scoped_ptr<AsyncPixelTransferDelegate>
      AsyncPixelTransferDelegateStub::Create(gfx::GLContext* context) {
  return make_scoped_ptr(
      static_cast<AsyncPixelTransferDelegate*>(
          new AsyncPixelTransferDelegateStub()));
}

AsyncTransferStateStub::AsyncTransferStateStub(GLuint texture_id) {
  static const AsyncTexImage2DParams zero_params = {0, 0, 0, 0, 0, 0, 0, 0};
  late_bind_define_params_ = zero_params;
  needs_late_bind_ = false;
}

AsyncTransferStateStub::~AsyncTransferStateStub() {
}

bool AsyncTransferStateStub::TransferIsInProgress() {
  return false;
}

void AsyncTransferStateStub::BindTransfer(AsyncTexImage2DParams* out_params) {
  DCHECK(out_params);
  DCHECK(needs_late_bind_);
  *out_params = late_bind_define_params_;
  needs_late_bind_ = false;
}

AsyncPixelTransferDelegateStub::AsyncPixelTransferDelegateStub()
    : texture_upload_count_(0) {
}

AsyncPixelTransferDelegateStub::~AsyncPixelTransferDelegateStub() {
}

AsyncPixelTransferState*
    AsyncPixelTransferDelegateStub::CreateRawPixelTransferState(
        GLuint texture_id) {
  return static_cast<AsyncPixelTransferState*>(
      new AsyncTransferStateStub(texture_id));
}

void AsyncPixelTransferDelegateStub::AsyncNotifyCompletion(
    const base::Closure& task) {
  task.Run();
}

void AsyncPixelTransferDelegateStub::AsyncTexImage2D(
    AsyncPixelTransferState* transfer_state,
    const AsyncTexImage2DParams& tex_params,
    const AsyncMemoryParams& mem_params) {
  // Save the define params to return later during deferred
  // binding of the transfer texture.
  DCHECK(transfer_state);
  AsyncTransferStateStub* state =
      static_cast<AsyncTransferStateStub*>(transfer_state);
  // We don't actually need a late bind since this stub does
  // everything synchronously, but this tries to be similar
  // as an async implementation.
  state->needs_late_bind_ = true;
  state->late_bind_define_params_ = tex_params;
  void* data = GetAddress(mem_params.shared_memory,
                          mem_params.shm_size,
                          mem_params.shm_data_offset,
                          mem_params.shm_data_size);
  glTexImage2D(
      tex_params.target,
      tex_params.level,
      tex_params.internal_format,
      tex_params.width,
      tex_params.height,
      tex_params.border,
      tex_params.format,
      tex_params.type,
      data);
}

void AsyncPixelTransferDelegateStub::AsyncTexSubImage2D(
    AsyncPixelTransferState* transfer_state,
    const AsyncTexSubImage2DParams& tex_params,
    const AsyncMemoryParams& mem_params) {
  void* data = GetAddress(mem_params.shared_memory,
                          mem_params.shm_size,
                          mem_params.shm_data_offset,
                          mem_params.shm_data_size);
  DCHECK(transfer_state);
  AsyncTransferStateStub* state =
      static_cast<AsyncTransferStateStub*>(transfer_state);
  DCHECK(!state->needs_late_bind_);
  base::TimeTicks begin_time(base::TimeTicks::HighResNow());
  glTexSubImage2D(
      tex_params.target,
      tex_params.level,
      tex_params.xoffset,
      tex_params.yoffset,
      tex_params.width,
      tex_params.height,
      tex_params.format,
      tex_params.type,
      data);
  texture_upload_count_++;
  total_texture_upload_time_ += base::TimeTicks::HighResNow() - begin_time;
}

uint32 AsyncPixelTransferDelegateStub::GetTextureUploadCount() {
  return texture_upload_count_;
}

base::TimeDelta AsyncPixelTransferDelegateStub::GetTotalTextureUploadTime() {
  return total_texture_upload_time_;
}

}  // namespace gfx