1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "gpu/command_buffer/service/async_pixel_transfer_delegate.h"
6
7#include "base/memory/shared_memory.h"
8#include "gpu/command_buffer/service/safe_shared_memory_pool.h"
9
10namespace gpu {
11
12namespace {
13
14void* GetAddressImpl(base::SharedMemory* shared_memory,
15                     uint32 shm_size,
16                     uint32 shm_data_offset,
17                     uint32 shm_data_size) {
18  // Memory bounds have already been validated, so there
19  // are just DCHECKS here.
20  DCHECK(shared_memory);
21  DCHECK(shared_memory->memory());
22  DCHECK_LE(shm_data_offset + shm_data_size, shm_size);
23  return static_cast<int8*>(shared_memory->memory()) + shm_data_offset;
24}
25
26}  // namespace
27
28AsyncPixelTransferUploadStats::AsyncPixelTransferUploadStats()
29    : texture_upload_count_(0) {}
30
31AsyncPixelTransferUploadStats::~AsyncPixelTransferUploadStats() {}
32
33void AsyncPixelTransferUploadStats::AddUpload(base::TimeDelta transfer_time) {
34  base::AutoLock scoped_lock(lock_);
35  texture_upload_count_++;
36  total_texture_upload_time_ += transfer_time;
37}
38
39int AsyncPixelTransferUploadStats::GetStats(
40    base::TimeDelta* total_texture_upload_time) {
41  base::AutoLock scoped_lock(lock_);
42  if (total_texture_upload_time)
43    *total_texture_upload_time = total_texture_upload_time_;
44  return texture_upload_count_;
45}
46
47AsyncPixelTransferDelegate::AsyncPixelTransferDelegate(){}
48
49AsyncPixelTransferDelegate::~AsyncPixelTransferDelegate(){}
50
51// static
52void* AsyncPixelTransferDelegate::GetAddress(
53    const AsyncMemoryParams& mem_params) {
54  return GetAddressImpl(mem_params.shared_memory,
55                        mem_params.shm_size,
56                        mem_params.shm_data_offset,
57                        mem_params.shm_data_size);
58}
59
60// static
61void* AsyncPixelTransferDelegate::GetAddress(
62    ScopedSafeSharedMemory* safe_shared_memory,
63    const AsyncMemoryParams& mem_params) {
64  return GetAddressImpl(safe_shared_memory->shared_memory(),
65                        mem_params.shm_size,
66                        mem_params.shm_data_offset,
67                        mem_params.shm_data_size);
68}
69
70}  // namespace gpu
71