Flutter Engine
paint_utils.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/flow/paint_utils.h"
6 
7 #include <stdlib.h>
8 
9 #include "third_party/skia/include/core/SkBitmap.h"
10 #include "third_party/skia/include/core/SkPaint.h"
11 #include "third_party/skia/include/core/SkShader.h"
12 
13 namespace flutter {
14 
15 namespace {
16 
17 sk_sp<SkShader> CreateCheckerboardShader(SkColor c1, SkColor c2, int size) {
18  SkBitmap bm;
19  bm.allocN32Pixels(2 * size, 2 * size);
20  bm.eraseColor(c1);
21  bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
22  bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
23  return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
24 }
25 
26 } // anonymous namespace
27 
28 void DrawCheckerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
29  SkPaint paint;
30  paint.setShader(CreateCheckerboardShader(c1, c2, size));
31  canvas->drawPaint(paint);
32 }
33 
34 void DrawCheckerboard(SkCanvas* canvas, const SkRect& rect) {
35  // Draw a checkerboard
36  canvas->save();
37  canvas->clipRect(rect);
38 
39  auto checkerboard_color =
40  SkColorSetARGB(64, rand() % 256, rand() % 256, rand() % 256);
41 
42  DrawCheckerboard(canvas, checkerboard_color, 0x00000000, 12);
43  canvas->restore();
44 
45  // Stroke the drawn area
46  SkPaint debugPaint;
47  debugPaint.setStrokeWidth(8);
48  debugPaint.setColor(SkColorSetA(checkerboard_color, 255));
49  debugPaint.setStyle(SkPaint::kStroke_Style);
50  canvas->drawRect(rect, debugPaint);
51 }
52 
53 } // namespace flutter
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
void DrawCheckerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
Definition: paint_utils.cc:28