summaryrefslogtreecommitdiffstats
path: root/chromium/cc/debug/overdraw_metrics.cc
blob: a14284d800de785844271be79d9b57317eabb877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/debug/overdraw_metrics.h"

#include "base/debug/trace_event.h"
#include "base/metrics/histogram.h"
#include "cc/base/math_util.h"
#include "cc/trees/layer_tree_host.h"
#include "cc/trees/layer_tree_host_impl.h"
#include "ui/gfx/quad_f.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/transform.h"

namespace cc {

OverdrawMetrics::OverdrawMetrics(bool record_metrics_for_frame)
    : record_metrics_for_frame_(record_metrics_for_frame),
      pixels_painted_(0),
      pixels_uploaded_opaque_(0),
      pixels_uploaded_translucent_(0),
      tiles_culled_for_upload_(0),
      contents_texture_use_bytes_(0),
      render_surface_texture_use_bytes_(0),
      pixels_drawn_opaque_(0),
      pixels_drawn_translucent_(0),
      pixels_culled_for_drawing_(0) {}

static inline float WedgeProduct(gfx::PointF p1, gfx::PointF p2) {
  return p1.x() * p2.y() - p1.y() * p2.x();
}

// Calculates area of an arbitrary convex polygon with up to 8 points.
static inline float PolygonArea(gfx::PointF points[8], int num_points) {
  if (num_points < 3)
    return 0;

  float area = 0;
  for (int i = 0; i < num_points; ++i)
    area += WedgeProduct(points[i], points[(i+1)%num_points]);
  return std::abs(0.5f * area);
}

// Takes a given quad, maps it by the given transformation, and gives the area
// of the resulting polygon.
static inline float AreaOfMappedQuad(const gfx::Transform& transform,
                                     const gfx::QuadF& quad) {
  gfx::PointF clipped_quad[8];
  int num_vertices_in_clipped_quad = 0;
  MathUtil::MapClippedQuad(transform,
                           quad,
                           clipped_quad,
                           &num_vertices_in_clipped_quad);
  return PolygonArea(clipped_quad, num_vertices_in_clipped_quad);
}

void OverdrawMetrics::DidPaint(gfx::Rect painted_rect) {
  if (!record_metrics_for_frame_)
    return;

  pixels_painted_ +=
      static_cast<float>(painted_rect.width()) * painted_rect.height();
}

void OverdrawMetrics::DidCullTilesForUpload(int count) {
  if (record_metrics_for_frame_)
    tiles_culled_for_upload_ += count;
}

void OverdrawMetrics::DidUpload(const gfx::Transform& transform_to_target,
                                gfx::Rect upload_rect,
                                gfx::Rect opaque_rect) {
  if (!record_metrics_for_frame_)
    return;

  float upload_area =
      AreaOfMappedQuad(transform_to_target, gfx::QuadF(upload_rect));
  float upload_opaque_area =
      AreaOfMappedQuad(transform_to_target,
                       gfx::QuadF(gfx::IntersectRects(opaque_rect,
                                                      upload_rect)));

  pixels_uploaded_opaque_ += upload_opaque_area;
  pixels_uploaded_translucent_ += upload_area - upload_opaque_area;
}

void OverdrawMetrics::DidUseContentsTextureMemoryBytes(
    size_t contents_texture_use_bytes) {
  if (!record_metrics_for_frame_)
    return;

  contents_texture_use_bytes_ += contents_texture_use_bytes;
}

void OverdrawMetrics::DidUseRenderSurfaceTextureMemoryBytes(
    size_t render_surface_use_bytes) {
  if (!record_metrics_for_frame_)
    return;

  render_surface_texture_use_bytes_ += render_surface_use_bytes;
}

void OverdrawMetrics::DidCullForDrawing(
    const gfx::Transform& transform_to_target,
    gfx::Rect before_cull_rect,
    gfx::Rect after_cull_rect) {
  if (!record_metrics_for_frame_)
    return;

  float before_cull_area =
      AreaOfMappedQuad(transform_to_target, gfx::QuadF(before_cull_rect));
  float after_cull_area =
      AreaOfMappedQuad(transform_to_target, gfx::QuadF(after_cull_rect));

  pixels_culled_for_drawing_ += before_cull_area - after_cull_area;
}

void OverdrawMetrics::DidDraw(const gfx::Transform& transform_to_target,
                              gfx::Rect after_cull_rect,
                              gfx::Rect opaque_rect) {
  if (!record_metrics_for_frame_)
    return;

  float after_cull_area =
      AreaOfMappedQuad(transform_to_target, gfx::QuadF(after_cull_rect));
  float after_cull_opaque_area =
      AreaOfMappedQuad(transform_to_target,
                       gfx::QuadF(gfx::IntersectRects(opaque_rect,
                                                      after_cull_rect)));

  pixels_drawn_opaque_ += after_cull_opaque_area;
  pixels_drawn_translucent_ += after_cull_area - after_cull_opaque_area;
}

void OverdrawMetrics::RecordMetrics(
    const LayerTreeHost* layer_tree_host) const {
  if (record_metrics_for_frame_)
    RecordMetricsInternal<LayerTreeHost>(UpdateAndCommit, layer_tree_host);
}

void OverdrawMetrics::RecordMetrics(
    const LayerTreeHostImpl* layer_tree_host_impl) const {
  if (record_metrics_for_frame_) {
    RecordMetricsInternal<LayerTreeHostImpl>(DrawingToScreen,
                                             layer_tree_host_impl);
  }
}

static gfx::Size DrawViewportSize(const LayerTreeHost* host) {
  return host->device_viewport_size();
}
static gfx::Size DrawViewportSize(const LayerTreeHostImpl* host_impl) {
  return host_impl->DrawViewportSize();
}

template <typename LayerTreeHostType>
void OverdrawMetrics::RecordMetricsInternal(
    MetricsType metrics_type,
    const LayerTreeHostType* layer_tree_host) const {
  // This gives approximately 10x the percentage of pixels to fill the viewport
  // once.
  float normalization = 1000.f / (DrawViewportSize(layer_tree_host).width() *
                                  DrawViewportSize(layer_tree_host).height());
  // This gives approximately 100x the percentage of tiles to fill the viewport
  // once, if all tiles were 256x256.
  float tile_normalization =
      10000.f / (DrawViewportSize(layer_tree_host).width() / 256.f *
                 DrawViewportSize(layer_tree_host).height() / 256.f);
  // This gives approximately 10x the percentage of bytes to fill the viewport
  // once, assuming 4 bytes per pixel.
  float byte_normalization = normalization / 4;

  switch (metrics_type) {
    case DrawingToScreen: {
      UMA_HISTOGRAM_CUSTOM_COUNTS(
          "Renderer4.pixelCountOpaque_Draw",
          static_cast<int>(normalization * pixels_drawn_opaque_),
          100, 1000000, 50);
      UMA_HISTOGRAM_CUSTOM_COUNTS(
          "Renderer4.pixelCountTranslucent_Draw",
          static_cast<int>(normalization * pixels_drawn_translucent_),
          100, 1000000, 50);
      UMA_HISTOGRAM_CUSTOM_COUNTS(
          "Renderer4.pixelCountCulled_Draw",
          static_cast<int>(normalization * pixels_culled_for_drawing_),
          100, 1000000, 50);

      TRACE_COUNTER_ID1("cc",
                        "DrawPixelsCulled",
                        layer_tree_host,
                        pixels_culled_for_drawing_);
      TRACE_EVENT2("cc",
                   "OverdrawMetrics",
                   "PixelsDrawnOpaque",
                   pixels_drawn_opaque_,
                   "PixelsDrawnTranslucent",
                   pixels_drawn_translucent_);
      break;
    }
    case UpdateAndCommit: {
      UMA_HISTOGRAM_CUSTOM_COUNTS(
          "Renderer4.pixelCountPainted",
          static_cast<int>(normalization * pixels_painted_),
          100, 1000000, 50);
      UMA_HISTOGRAM_CUSTOM_COUNTS(
          "Renderer4.pixelCountOpaque_Upload",
          static_cast<int>(normalization * pixels_uploaded_opaque_),
          100, 1000000, 50);
      UMA_HISTOGRAM_CUSTOM_COUNTS(
          "Renderer4.pixelCountTranslucent_Upload",
          static_cast<int>(normalization * pixels_uploaded_translucent_),
          100, 1000000, 50);
      UMA_HISTOGRAM_CUSTOM_COUNTS(
          "Renderer4.tileCountCulled_Upload",
          static_cast<int>(tile_normalization * tiles_culled_for_upload_),
          100, 10000000, 50);
      UMA_HISTOGRAM_CUSTOM_COUNTS(
          "Renderer4.renderSurfaceTextureBytes_ViewportScaled",
          static_cast<int>(
              byte_normalization * render_surface_texture_use_bytes_),
          10, 1000000, 50);
      UMA_HISTOGRAM_CUSTOM_COUNTS(
          "Renderer4.renderSurfaceTextureBytes_Unscaled",
          static_cast<int>(render_surface_texture_use_bytes_ / 1000),
          1000, 100000000, 50);
      UMA_HISTOGRAM_CUSTOM_COUNTS(
          "Renderer4.contentsTextureBytes_ViewportScaled",
          static_cast<int>(byte_normalization * contents_texture_use_bytes_),
          10, 1000000, 50);
      UMA_HISTOGRAM_CUSTOM_COUNTS(
          "Renderer4.contentsTextureBytes_Unscaled",
          static_cast<int>(contents_texture_use_bytes_ / 1000),
          1000, 100000000, 50);
      {
        TRACE_COUNTER_ID1("cc",
                          "UploadTilesCulled",
                          layer_tree_host,
                          tiles_culled_for_upload_);
        TRACE_EVENT2("cc",
                     "OverdrawMetrics",
                     "PixelsUploadedOpaque",
                     pixels_uploaded_opaque_,
                     "PixelsUploadedTranslucent",
                     pixels_uploaded_translucent_);
      }
      {
        // This must be in a different scope than the TRACE_EVENT2 above.
        TRACE_EVENT1("cc",
                     "OverdrawPaintMetrics",
                     "PixelsPainted",
                     pixels_painted_);
      }
      {
        // This must be in a different scope than the TRACE_EVENTs above.
        TRACE_EVENT2("cc",
                     "OverdrawPaintMetrics",
                     "ContentsTextureBytes",
                     contents_texture_use_bytes_,
                     "RenderSurfaceTextureBytes",
                     render_surface_texture_use_bytes_);
      }
      break;
    }
  }
}

}  // namespace cc