• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkDebugfTracer.h"
9 #include "SkTraceEvent.h"
10 
addTraceEvent(char phase,const uint8_t * categoryEnabledFlag,const char * name,uint64_t id,int numArgs,const char ** argNames,const uint8_t * argTypes,const uint64_t * argValues,uint8_t flags)11 SkEventTracer::Handle SkDebugfTracer::addTraceEvent(char phase,
12                                                     const uint8_t* categoryEnabledFlag,
13                                                     const char* name,
14                                                     uint64_t id,
15                                                     int numArgs,
16                                                     const char** argNames,
17                                                     const uint8_t* argTypes,
18                                                     const uint64_t* argValues,
19                                                     uint8_t flags) {
20     SkString args;
21     for (int i = 0; i < numArgs; ++i) {
22         if (i > 0) {
23             args.append(", ");
24         } else {
25             args.append(" ");
26         }
27         skia::tracing_internals::TraceValueUnion value;
28         value.as_uint = argValues[i];
29         switch (argTypes[i]) {
30             case TRACE_VALUE_TYPE_BOOL:
31                 args.appendf("%s=%s", argNames[i], value.as_bool ? "true" : "false");
32                 break;
33             case TRACE_VALUE_TYPE_UINT:
34                 args.appendf("%s=%u", argNames[i], static_cast<uint32_t>(argValues[i]));
35                 break;
36             case TRACE_VALUE_TYPE_INT:
37                 args.appendf("%s=%d", argNames[i], static_cast<int32_t>(argValues[i]));
38                 break;
39             case TRACE_VALUE_TYPE_DOUBLE:
40                 args.appendf("%s=%g", argNames[i], value.as_double);
41                 break;
42             case TRACE_VALUE_TYPE_POINTER:
43                 args.appendf("%s=0x%p", argNames[i], value.as_pointer);
44                 break;
45             case TRACE_VALUE_TYPE_STRING:
46             case TRACE_VALUE_TYPE_COPY_STRING: {
47                 static constexpr size_t kMaxLen = 20;
48                 SkString string(value.as_string);
49                 size_t truncAt = string.size();
50                 size_t newLineAt = SkStrFind(string.c_str(), "\n");
51                 if (newLineAt > 0) {
52                     truncAt = newLineAt;
53                 }
54                 truncAt = SkTMin(truncAt, kMaxLen);
55                 if (truncAt < string.size()) {
56                     string.resize(truncAt);
57                     string.append("...");
58                 }
59                 args.appendf("%s=\"%s\"", argNames[i], string.c_str());
60                 break;
61             }
62             default:
63                 args.appendf("%s=<unknown type>", argNames[i]);
64                 break;
65         }
66     }
67     bool open = (phase == TRACE_EVENT_PHASE_COMPLETE);
68     if (open) {
69         const char* category = this->getCategoryGroupName(categoryEnabledFlag);
70         SkDebugf("[% 2d]%s <%s> %s%s #%d {\n", fIndent.size(), fIndent.c_str(), category, name,
71                  args.c_str(), fCnt);
72         fIndent.append(" ");
73     } else {
74         SkDebugf("%s%s #%d\n", name, args.c_str(), fCnt);
75     }
76     ++fCnt;
77     return 0;
78 }
79 
updateTraceEventDuration(const uint8_t * categoryEnabledFlag,const char * name,SkEventTracer::Handle handle)80 void SkDebugfTracer::updateTraceEventDuration(const uint8_t* categoryEnabledFlag,
81                                               const char* name,
82                                               SkEventTracer::Handle handle) {
83     fIndent.resize(fIndent.size() - 1);
84     SkDebugf("[% 2d]%s } %s\n", fIndent.size(), fIndent.c_str(), name);
85 }
86