[chromium] Allow us to disable overdraw metrics when tracing is off
[WebKit-https.git] / Source / WebCore / platform / graphics / chromium / cc / CCOverdrawMetrics.cpp
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
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26
27 #if USE(ACCELERATED_COMPOSITING)
28
29 #include "cc/CCOverdrawMetrics.h"
30
31 #include "FloatQuad.h"
32 #include "IntRect.h"
33 #include "TraceEvent.h"
34 #include "TransformationMatrix.h"
35 #include "cc/CCLayerTreeHost.h"
36 #include "cc/CCLayerTreeHostImpl.h"
37
38 namespace WebCore {
39
40 CCOverdrawMetrics::CCOverdrawMetrics(bool recordMetricsForFrame)
41     : m_recordMetricsForFrame(recordMetricsForFrame)
42     , m_pixelsDrawnOpaque(0)
43     , m_pixelsDrawnTranslucent(0)
44     , m_pixelsCulled(0)
45 {
46 }
47
48 static inline float wedgeProduct(const FloatPoint& p1, const FloatPoint& p2)
49 {
50     return p1.x() * p2.y() - p1.y() * p2.x();
51 }
52
53 // Computes area of quads that are possibly non-rectangular. Can be easily extended to polygons.
54 static inline float quadArea(const FloatQuad& quad)
55 {
56     return fabs(0.5 * (wedgeProduct(quad.p1(), quad.p2()) +
57                        wedgeProduct(quad.p2(), quad.p3()) +
58                        wedgeProduct(quad.p3(), quad.p4()) +
59                        wedgeProduct(quad.p4(), quad.p1())));
60 }
61
62 void CCOverdrawMetrics::didCull(const TransformationMatrix& transformToTarget, const IntRect& beforeCullRect, const IntRect& afterCullRect)
63 {
64     if (!m_recordMetricsForFrame)
65         return;
66
67     float beforeCullArea = quadArea(transformToTarget.mapQuad(FloatQuad(beforeCullRect)));
68     float afterCullArea = quadArea(transformToTarget.mapQuad(FloatQuad(afterCullRect)));
69
70     m_pixelsCulled += beforeCullArea - afterCullArea;
71 }
72
73 void CCOverdrawMetrics::didDraw(const TransformationMatrix& transformToTarget, const IntRect& afterCullRect, const IntRect& opaqueRect)
74 {
75     if (!m_recordMetricsForFrame)
76         return;
77
78     float afterCullArea = quadArea(transformToTarget.mapQuad(FloatQuad(afterCullRect)));
79     float afterCullOpaqueArea = quadArea(transformToTarget.mapQuad(FloatQuad(intersection(opaqueRect, afterCullRect))));
80
81     m_pixelsDrawnOpaque += afterCullOpaqueArea;
82     m_pixelsDrawnTranslucent += afterCullArea - afterCullOpaqueArea;
83 }
84
85 void CCOverdrawMetrics::recordMetrics(const CCLayerTreeHost* layerTreeHost) const
86 {
87     if (m_recordMetricsForFrame)
88         recordMetricsInternal<CCLayerTreeHost>(UPLOADING, layerTreeHost);
89 }
90
91 void CCOverdrawMetrics::recordMetrics(const CCLayerTreeHostImpl* layerTreeHost) const
92 {
93     if (m_recordMetricsForFrame)
94         recordMetricsInternal<CCLayerTreeHostImpl>(DRAWING, layerTreeHost);
95 }
96
97 template<typename LayerTreeHostType>
98 void CCOverdrawMetrics::recordMetricsInternal(MetricsType metricsType, const LayerTreeHostType* layerTreeHost) const
99 {
100     const char* histogramOpaqueName = 0;
101     const char* histogramTranslucentName = 0;
102     const char* histogramCulledName = 0;
103     const char* cullCounterName = 0;
104     const char* opaqueCounterName = 0;
105     const char* translucentCounterName = 0;
106     switch (metricsType) {
107     case DRAWING:
108         histogramOpaqueName = "Renderer4.drawPixelCountOpaque";
109         histogramTranslucentName = "Renderer4.drawPixelCountTranslucent";
110         histogramCulledName = "Renderer4.drawPixelCountCulled";
111         cullCounterName = "DrawPixelsCulled";
112         opaqueCounterName = "PixelsDrawnOpaque";
113         translucentCounterName = "PixelsDrawnTranslucent";
114         break;
115     case UPLOADING:
116         histogramOpaqueName = "Renderer4.uploadPixelCountOpaque";
117         histogramTranslucentName = "Renderer4.uploadPixelCountTranslucent";
118         histogramCulledName = "Renderer4.uploadPixelCountCulled";
119         cullCounterName = "UploadPixelsCulled";
120         opaqueCounterName = "PixelsUploadedOpaque";
121         translucentCounterName = "PixelsUploadedTranslucent";
122         break;
123     }
124     ASSERT(histogramOpaqueName);
125
126     float normalization = 1000.f / (layerTreeHost->viewportSize().width() * layerTreeHost->viewportSize().height());
127     PlatformSupport::histogramCustomCounts(histogramOpaqueName, static_cast<int>(normalization * m_pixelsDrawnOpaque), 100, 1000000, 50);
128     PlatformSupport::histogramCustomCounts(histogramTranslucentName, static_cast<int>(normalization * m_pixelsDrawnTranslucent), 100, 1000000, 50);
129     PlatformSupport::histogramCustomCounts(histogramCulledName, static_cast<int>(normalization * m_pixelsCulled), 100, 1000000, 50);
130
131     TRACE_COUNTER_ID1("webkit", cullCounterName, layerTreeHost, m_pixelsCulled);
132     TRACE_EVENT2("webkit", "CCOverdrawMetrics", opaqueCounterName, m_pixelsDrawnOpaque, translucentCounterName, m_pixelsDrawnTranslucent);
133 }
134
135 } // namespace WebCore
136
137 #endif