summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/paint/raster_invalidation_tracking.cc
blob: a5a9e132de113582db44b930cd3c21b51f01fa25 (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
// Copyright 2016 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 "third_party/blink/renderer/platform/graphics/paint/raster_invalidation_tracking.h"

#include "SkImageFilter.h"
#include "third_party/blink/renderer/platform/geometry/geometry_as_json.h"
#include "third_party/blink/renderer/platform/geometry/layout_rect.h"
#include "third_party/blink/renderer/platform/graphics/color.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_canvas.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_recorder.h"

namespace blink {

static bool g_simulate_raster_under_invalidations = false;

void RasterInvalidationTracking::SimulateRasterUnderInvalidations(bool enable) {
  g_simulate_raster_under_invalidations = enable;
}

void RasterInvalidationTracking::AddInvalidation(
    const DisplayItemClient* client,
    const String& debug_name,
    const IntRect& rect,
    PaintInvalidationReason reason) {
  if (rect.IsEmpty())
    return;

  RasterInvalidationInfo info;
  info.client = client;
  info.client_debug_name = debug_name;
  info.rect = rect;
  info.reason = reason;
  invalidations_.push_back(info);

  // TODO(crbug.com/496260): Some antialiasing effects overflow the paint
  // invalidation rect.
  IntRect r = rect;
  r.Inflate(1);
  invalidation_region_since_last_paint_.Unite(r);
}

static bool CompareRasterInvalidationInfo(const RasterInvalidationInfo& a,
                                          const RasterInvalidationInfo& b) {
  // Sort by rect first, bigger rects before smaller ones.
  if (a.rect.Width() != b.rect.Width())
    return a.rect.Width() > b.rect.Width();
  if (a.rect.Height() != b.rect.Height())
    return a.rect.Height() > b.rect.Height();
  if (a.rect.X() != b.rect.X())
    return a.rect.X() > b.rect.X();
  if (a.rect.Y() != b.rect.Y())
    return a.rect.Y() > b.rect.Y();

  // Then compare clientDebugName, in alphabetic order.
  int name_compare_result =
      CodePointCompare(a.client_debug_name, b.client_debug_name);
  if (name_compare_result != 0)
    return name_compare_result < 0;

  return a.reason < b.reason;
}

void RasterInvalidationTracking::AsJSON(JSONObject* json) {
  if (!invalidations_.IsEmpty()) {
    std::sort(invalidations_.begin(), invalidations_.end(),
              &CompareRasterInvalidationInfo);
    std::unique_ptr<JSONArray> paint_invalidations_json = JSONArray::Create();
    for (auto& info : invalidations_) {
      std::unique_ptr<JSONObject> info_json = JSONObject::Create();
      info_json->SetString("object", info.client_debug_name);
      if (!info.rect.IsEmpty()) {
        if (info.rect == LayoutRect::InfiniteIntRect())
          info_json->SetString("rect", "infinite");
        else
          info_json->SetArray("rect", RectAsJSONArray(info.rect));
      }
      info_json->SetString("reason",
                           PaintInvalidationReasonToString(info.reason));
      paint_invalidations_json->PushObject(std::move(info_json));
    }
    json->SetArray("paintInvalidations", std::move(paint_invalidations_json));
  }

  if (!under_invalidations_.IsEmpty()) {
    std::unique_ptr<JSONArray> under_paint_invalidations_json =
        JSONArray::Create();
    for (auto& under_paint_invalidation : under_invalidations_) {
      std::unique_ptr<JSONObject> under_paint_invalidation_json =
          JSONObject::Create();
      under_paint_invalidation_json->SetDouble("x", under_paint_invalidation.x);
      under_paint_invalidation_json->SetDouble("y", under_paint_invalidation.y);
      under_paint_invalidation_json->SetString(
          "oldPixel",
          Color(under_paint_invalidation.old_pixel).NameForLayoutTreeAsText());
      under_paint_invalidation_json->SetString(
          "newPixel",
          Color(under_paint_invalidation.new_pixel).NameForLayoutTreeAsText());
      under_paint_invalidations_json->PushObject(
          std::move(under_paint_invalidation_json));
    }
    json->SetArray("underPaintInvalidations",
                   std::move(under_paint_invalidations_json));
  }
}

static bool PixelComponentsDiffer(int c1, int c2) {
  // Compare strictly for saturated values.
  if (c1 == 0 || c1 == 255 || c2 == 0 || c2 == 255)
    return c1 != c2;
  // Tolerate invisible differences that may occur in gradients etc.
  return abs(c1 - c2) > 2;
}

static bool PixelsDiffer(SkColor p1, SkColor p2) {
  return PixelComponentsDiffer(SkColorGetA(p1), SkColorGetA(p2)) ||
         PixelComponentsDiffer(SkColorGetR(p1), SkColorGetR(p2)) ||
         PixelComponentsDiffer(SkColorGetG(p1), SkColorGetG(p2)) ||
         PixelComponentsDiffer(SkColorGetB(p1), SkColorGetB(p2));
}

void RasterInvalidationTracking::CheckUnderInvalidations(
    const String& layer_debug_name,
    sk_sp<PaintRecord> new_record,
    const IntRect& new_interest_rect) {
  auto old_interest_rect = last_interest_rect_;
  Region invalidation_region;
  if (!g_simulate_raster_under_invalidations)
    invalidation_region = invalidation_region_since_last_paint_;
  auto old_record = std::move(last_painted_record_);

  last_painted_record_ = new_record;
  last_interest_rect_ = new_interest_rect;
  invalidation_region_since_last_paint_ = Region();

  if (!old_record)
    return;

  IntRect rect = Intersection(old_interest_rect, new_interest_rect);
  // Avoid too big area as the following code is slow.
  rect.Intersect(IntRect(rect.X(), rect.Y(), 1200, 6000));
  if (rect.IsEmpty())
    return;

  SkBitmap old_bitmap;
  old_bitmap.allocPixels(
      SkImageInfo::MakeN32Premul(rect.Width(), rect.Height()));
  {
    SkiaPaintCanvas canvas(old_bitmap);
    canvas.clear(SK_ColorTRANSPARENT);
    canvas.translate(-rect.X(), -rect.Y());
    canvas.drawPicture(std::move(old_record));
  }

  SkBitmap new_bitmap;
  new_bitmap.allocPixels(
      SkImageInfo::MakeN32Premul(rect.Width(), rect.Height()));
  {
    SkiaPaintCanvas canvas(new_bitmap);
    canvas.clear(SK_ColorTRANSPARENT);
    canvas.translate(-rect.X(), -rect.Y());
    canvas.drawPicture(std::move(new_record));
  }

  int mismatching_pixels = 0;
  static const int kMaxMismatchesToReport = 50;
  for (int bitmap_y = 0; bitmap_y < rect.Height(); ++bitmap_y) {
    int layer_y = bitmap_y + rect.Y();
    for (int bitmap_x = 0; bitmap_x < rect.Width(); ++bitmap_x) {
      int layer_x = bitmap_x + rect.X();
      SkColor old_pixel = old_bitmap.getColor(bitmap_x, bitmap_y);
      SkColor new_pixel = new_bitmap.getColor(bitmap_x, bitmap_y);
      if (PixelsDiffer(old_pixel, new_pixel) &&
          !invalidation_region.Contains(IntPoint(layer_x, layer_y))) {
        if (mismatching_pixels < kMaxMismatchesToReport) {
          RasterUnderInvalidation under_invalidation = {layer_x, layer_y,
                                                        old_pixel, new_pixel};
          under_invalidations_.push_back(under_invalidation);
          LOG(ERROR) << layer_debug_name
                     << " Uninvalidated old/new pixels mismatch at " << layer_x
                     << "," << layer_y << " old:" << std::hex << old_pixel
                     << " new:" << new_pixel;
        } else if (mismatching_pixels == kMaxMismatchesToReport) {
          LOG(ERROR) << "and more...";
        }
        ++mismatching_pixels;
        *new_bitmap.getAddr32(bitmap_x, bitmap_y) =
            SkColorSetARGB(0xFF, 0xA0, 0, 0);  // Dark red.
      } else {
        *new_bitmap.getAddr32(bitmap_x, bitmap_y) = SK_ColorTRANSPARENT;
      }
    }
  }

  if (!mismatching_pixels)
    return;

  PaintRecorder recorder;
  recorder.beginRecording(rect);
  auto* canvas = recorder.getRecordingCanvas();
  if (under_invalidation_record_)
    canvas->drawPicture(std::move(under_invalidation_record_));
  canvas->drawBitmap(new_bitmap, rect.X(), rect.Y());
  under_invalidation_record_ = recorder.finishRecordingAsPicture();
}

}  // namespace blink