Flutter Impeller
gpu_tracer_gles.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 
6 #include <thread>
7 #include "fml/trace_event.h"
8 
9 namespace impeller {
10 
11 GPUTracerGLES::GPUTracerGLES(const ProcTableGLES& gl, bool enable_tracing) {
12 #ifdef IMPELLER_DEBUG
13  auto desc = gl.GetDescription();
14  enabled_ =
15  enable_tracing && desc->HasExtension("GL_EXT_disjoint_timer_query");
16 #endif // IMPELLER_DEBUG
17 }
18 
20  if (!enabled_ || active_frame_.has_value() ||
21  std::this_thread::get_id() != raster_thread_) {
22  return;
23  }
24 
25  // At the beginning of a frame, check the status of all pending
26  // previous queries.
27  ProcessQueries(gl);
28 
29  uint32_t query = 0;
30  gl.GenQueriesEXT(1, &query);
31  if (query == 0) {
32  return;
33  }
34 
35  active_frame_ = query;
36  gl.BeginQueryEXT(GL_TIME_ELAPSED_EXT, query);
37 }
38 
40  raster_thread_ = std::this_thread::get_id();
41 }
42 
44  pending_traces_.clear();
45  active_frame_ = std::nullopt;
46 }
47 
48 void GPUTracerGLES::ProcessQueries(const ProcTableGLES& gl) {
49  // For reasons unknown to me, querying the state of more than
50  // one query object per frame causes crashes on a Pixel 6 pro.
51  // It does not crash on an S10.
52  while (!pending_traces_.empty()) {
53  auto query = pending_traces_.front();
54 
55  // First check if the query is complete without blocking
56  // on the result. Incomplete results are left in the pending
57  // trace vector and will not be checked again for another
58  // frame.
59  GLuint available = GL_FALSE;
60  gl.GetQueryObjectuivEXT(query, GL_QUERY_RESULT_AVAILABLE_EXT, &available);
61 
62  if (available != GL_TRUE) {
63  // If a query is not available, then all subsequent queries will be
64  // unavailable.
65  return;
66  }
67  // Return the timer resolution in nanoseconds.
68  uint64_t duration = 0;
69  gl.GetQueryObjectui64vEXT(query, GL_QUERY_RESULT_EXT, &duration);
70  auto gpu_ms = duration / 1000000.0;
71 
72  FML_TRACE_COUNTER("flutter", "GPUTracer",
73  reinterpret_cast<int64_t>(this), // Trace Counter ID
74  "FrameTimeMS", gpu_ms);
75  gl.DeleteQueriesEXT(1, &query);
76  pending_traces_.pop_front();
77  }
78 }
79 
81  if (!enabled_ || std::this_thread::get_id() != raster_thread_ ||
82  !active_frame_.has_value()) {
83  return;
84  }
85 
86  auto query = active_frame_.value();
87  gl.EndQueryEXT(GL_TIME_ELAPSED_EXT);
88 
89  pending_traces_.push_back(query);
90  active_frame_ = std::nullopt;
91 }
92 
93 } // namespace impeller
void MarkFrameEnd(const ProcTableGLES &gl)
Record the end of a frame workload.
void MarkFrameStart(const ProcTableGLES &gl)
Record the start of a frame workload, if one hasn't already been started.
GPUTracerGLES(const ProcTableGLES &gl, bool enable_tracing)
void RecordRasterThread()
Record the thread id of the raster thread.
void Reset()
Reset query tracking. Must be called when reusing reusing GLES context in a new window (e....
const DescriptionGLES * GetDescription() const