1 // Copyright (c) 2012 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 // This file contains the GPUTrace class. 6 #ifndef GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_ 7 #define GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_ 8 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/threading/thread.h" 15 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" 16 #include "gpu/gpu_export.h" 17 #include "ui/gl/gl_bindings.h" 18 19 namespace gpu { 20 namespace gles2 { 21 22 // Id used to keep trace namespaces separate 23 enum GpuTracerSource { 24 kTraceGroupMarker = 0, 25 kTraceCHROMIUM = 1, 26 kTraceDecoder = 2, 27 }; 28 29 // Traces GPU Commands. 30 class GPUTracer { 31 public: 32 static scoped_ptr<GPUTracer> Create(gles2::GLES2Decoder* decoder); 33 34 GPUTracer(); 35 virtual ~GPUTracer(); 36 37 // Scheduled processing in decoder begins. 38 virtual bool BeginDecoding() = 0; 39 40 // Scheduled processing in decoder ends. 41 virtual bool EndDecoding() = 0; 42 43 // Begin a trace marker. 44 virtual bool Begin(const std::string& name, GpuTracerSource source) = 0; 45 46 // End the last started trace marker. 47 virtual bool End(GpuTracerSource source) = 0; 48 49 virtual bool IsTracing() = 0; 50 51 // Retrieve the name of the current open trace. 52 // Returns empty string if no current open trace. 53 virtual const std::string& CurrentName() const = 0; 54 55 private: 56 DISALLOW_COPY_AND_ASSIGN(GPUTracer); 57 }; 58 59 class Outputter : public base::RefCounted<Outputter> { 60 public: 61 virtual void Trace(const std::string& name, 62 int64 start_time, 63 int64 end_time) = 0; 64 65 protected: ~Outputter()66 virtual ~Outputter() {} 67 friend class base::RefCounted<Outputter>; 68 }; 69 70 class TraceOutputter : public Outputter { 71 public: 72 static scoped_refptr<TraceOutputter> Create(const std::string& name); 73 virtual void Trace(const std::string& name, 74 int64 start_time, 75 int64 end_time) OVERRIDE; 76 77 protected: 78 friend class base::RefCounted<Outputter>; 79 explicit TraceOutputter(const std::string& name); 80 virtual ~TraceOutputter(); 81 82 base::Thread named_thread_; 83 uint64 local_trace_id_; 84 85 DISALLOW_COPY_AND_ASSIGN(TraceOutputter); 86 }; 87 88 class GPU_EXPORT Trace : public base::RefCounted<Trace> { 89 public: Trace(const std::string & name)90 explicit Trace(const std::string& name) : name_(name) {} 91 92 virtual void Start() = 0; 93 virtual void End() = 0; 94 95 // True if the the results of this query are available. 96 virtual bool IsAvailable() = 0; 97 98 virtual bool IsProcessable(); 99 virtual void Process() = 0; 100 101 virtual const std::string& name(); 102 103 protected: ~Trace()104 virtual ~Trace() {} 105 106 private: 107 friend class base::RefCounted<Trace>; 108 109 std::string name_; 110 111 DISALLOW_COPY_AND_ASSIGN(Trace); 112 }; 113 114 class GPU_EXPORT GLARBTimerTrace : public Trace { 115 public: 116 GLARBTimerTrace(scoped_refptr<Outputter> outputter, 117 const std::string& name, 118 int64 offset); 119 120 // Implementation of Tracer 121 virtual void Start() OVERRIDE; 122 virtual void End() OVERRIDE; 123 virtual bool IsAvailable() OVERRIDE; 124 virtual void Process() OVERRIDE; 125 126 private: 127 virtual ~GLARBTimerTrace(); 128 129 void Output(); 130 131 scoped_refptr<Outputter> outputter_; 132 133 int64 offset_; 134 int64 start_time_; 135 int64 end_time_; 136 bool end_requested_; 137 138 GLuint queries_[2]; 139 140 DISALLOW_COPY_AND_ASSIGN(GLARBTimerTrace); 141 }; 142 143 } // namespace gles2 144 } // namespace gpu 145 146 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_TRACER_H_ 147