1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef PlatformInstrumentation_h
32#define PlatformInstrumentation_h
33
34#include "platform/PlatformExport.h"
35#include "platform/TraceEvent.h"
36#include "wtf/MainThread.h"
37#include "wtf/text/WTFString.h"
38
39namespace WebCore {
40
41class PLATFORM_EXPORT PlatformInstrumentationClient {
42public:
43    virtual ~PlatformInstrumentationClient();
44
45    virtual void willDecodeImage(const String& imageType) = 0;
46    virtual void didDecodeImage() = 0;
47    virtual void willResizeImage(bool shouldCache) = 0;
48    virtual void didResizeImage() = 0;
49};
50
51class PLATFORM_EXPORT PlatformInstrumentation {
52public:
53    class LazyPixelRefTracker: TraceEvent::TraceScopedTrackableObject<void*> {
54    public:
55        LazyPixelRefTracker(void* instance)
56            : TraceEvent::TraceScopedTrackableObject<void*>(CategoryName, LazyPixelRef, instance)
57        {
58        }
59    };
60
61    static const char ImageDecodeEvent[];
62    static const char ImageResizeEvent[];
63    static const char DrawLazyPixelRefEvent[];
64    static const char DecodeLazyPixelRefEvent[];
65
66    static const char ImageTypeArgument[];
67    static const char CachedArgument[];
68
69    static const char LazyPixelRef[];
70
71    static void setClient(PlatformInstrumentationClient*);
72    static bool hasClient() { return m_client; }
73
74    static void willDecodeImage(const String& imageType);
75    static void didDecodeImage();
76    static void willResizeImage(bool shouldCache);
77    static void didResizeImage();
78    static void didDrawLazyPixelRef(unsigned long long lazyPixelRefId);
79    static void willDecodeLazyPixelRef(unsigned long long lazyPixelRefId);
80    static void didDecodeLazyPixelRef(unsigned long long lazyPixelRefId);
81
82private:
83    static const char CategoryName[];
84
85    static PlatformInstrumentationClient* m_client;
86};
87
88#define FAST_RETURN_IF_NO_CLIENT_OR_NOT_MAIN_THREAD() if (!m_client || !isMainThread()) return;
89
90inline void PlatformInstrumentation::willDecodeImage(const String& imageType)
91{
92    TRACE_EVENT_BEGIN1(CategoryName, ImageDecodeEvent, ImageTypeArgument, TRACE_STR_COPY(imageType.ascii().data()));
93    FAST_RETURN_IF_NO_CLIENT_OR_NOT_MAIN_THREAD();
94    m_client->willDecodeImage(imageType);
95}
96
97inline void PlatformInstrumentation::didDecodeImage()
98{
99    TRACE_EVENT_END0(CategoryName, ImageDecodeEvent);
100    FAST_RETURN_IF_NO_CLIENT_OR_NOT_MAIN_THREAD();
101    m_client->didDecodeImage();
102}
103
104inline void PlatformInstrumentation::willResizeImage(bool shouldCache)
105{
106    TRACE_EVENT_BEGIN1(CategoryName, ImageResizeEvent, CachedArgument, shouldCache);
107    FAST_RETURN_IF_NO_CLIENT_OR_NOT_MAIN_THREAD();
108    m_client->willResizeImage(shouldCache);
109}
110
111inline void PlatformInstrumentation::didResizeImage()
112{
113    TRACE_EVENT_END0(CategoryName, ImageResizeEvent);
114    FAST_RETURN_IF_NO_CLIENT_OR_NOT_MAIN_THREAD();
115    m_client->didResizeImage();
116}
117
118inline void PlatformInstrumentation::didDrawLazyPixelRef(unsigned long long lazyPixelRefId)
119{
120    TRACE_EVENT_INSTANT1(CategoryName, DrawLazyPixelRefEvent, LazyPixelRef, lazyPixelRefId);
121}
122
123inline void PlatformInstrumentation::willDecodeLazyPixelRef(unsigned long long lazyPixelRefId)
124{
125    TRACE_EVENT_BEGIN1(CategoryName, DecodeLazyPixelRefEvent, LazyPixelRef, lazyPixelRefId);
126}
127
128inline void PlatformInstrumentation::didDecodeLazyPixelRef(unsigned long long lazyPixelRefId)
129{
130    TRACE_EVENT_END1(CategoryName, DecodeLazyPixelRefEvent, LazyPixelRef, lazyPixelRefId);
131}
132
133} // namespace WebCore
134
135#endif // PlatformInstrumentation_h
136