File: gpu_fence.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (101 lines) | stat: -rw-r--r-- 2,792 bytes parent folder | download | duplicates (5)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/gpu_fence.h"

#include "base/logging.h"
#include "base/notreached.h"
#include "base/time/time.h"
#include "build/build_config.h"

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
#include <sync/sync.h>
#endif

namespace gfx {

GpuFence::GpuFence(GpuFenceHandle fence_handle)
    : fence_handle_(std::move(fence_handle)) {}

GpuFence::~GpuFence() = default;

GpuFence::GpuFence(GpuFence&& other) = default;

GpuFence& GpuFence::operator=(GpuFence&& other) = default;

const GpuFenceHandle& GpuFence::GetGpuFenceHandle() const {
  return fence_handle_;
}

ClientGpuFence GpuFence::AsClientGpuFence() {
  return reinterpret_cast<ClientGpuFence>(this);
}

// static
GpuFence* GpuFence::FromClientGpuFence(ClientGpuFence gpu_fence) {
  return reinterpret_cast<GpuFence*>(gpu_fence);
}

void GpuFence::Wait() {
  if (fence_handle_.is_null()) {
    return;
  }

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  static const int kInfiniteSyncWaitTimeout = -1;
  DCHECK_GE(fence_handle_.Peek(), 0);
  if (sync_wait(fence_handle_.Peek(), kInfiniteSyncWaitTimeout) < 0) {
    LOG(FATAL) << "Failed while waiting for gpu fence fd";
  }
#else
  NOTREACHED();
#endif
}

// static
GpuFence::FenceStatus GpuFence::GetStatusChangeTime(int fd,
                                                    base::TimeTicks* time) {
  DCHECK_NE(fd, -1);
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  auto info =
      std::unique_ptr<sync_fence_info_data, void (*)(sync_fence_info_data*)>{
          sync_fence_info(fd), sync_fence_info_free};
  if (!info) {
    LOG(ERROR) << "sync_fence_info returned null for fd : " << fd;
    return FenceStatus::kInvalid;
  }

  // Not signalled yet.
  if (info->status != 1) {
    return FenceStatus::kNotSignaled;
  }

  uint64_t timestamp_ns = 0u;
  struct sync_pt_info* pt_info = nullptr;
  while ((pt_info = sync_pt_info(info.get(), pt_info)))
    timestamp_ns = std::max(timestamp_ns, pt_info->timestamp_ns);

  if (timestamp_ns == 0u) {
    LOG(ERROR) << "No timestamp provided from sync_pt_info for fd : " << fd;
    return FenceStatus::kInvalid;
  }
  *time = base::TimeTicks() + base::Nanoseconds(timestamp_ns);
  return FenceStatus::kSignaled;
#else
  NOTREACHED();
#endif
}

base::TimeTicks GpuFence::GetMaxTimestamp() const {
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  base::TimeTicks timestamp;
  FenceStatus status = GetStatusChangeTime(fence_handle_.Peek(), &timestamp);
  DCHECK_EQ(status, FenceStatus::kSignaled);
  return timestamp;
#else
  NOTREACHED();
#endif
}

}  // namespace gfx