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  SkSamplingOptions());
25 }
26 
27 } // anonymous namespace
28 
29 void DrawCheckerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
30  SkPaint paint;
31  paint.setShader(CreateCheckerboardShader(c1, c2, size));
32  canvas->drawPaint(paint);
33 }
34 
35 void DrawCheckerboard(SkCanvas* canvas, const SkRect& rect) {
36  // Draw a checkerboard
37  canvas->save();
38  canvas->clipRect(rect);
39 
40  auto checkerboard_color =
41  SkColorSetARGB(64, rand() % 256, rand() % 256, rand() % 256);
42 
43  DrawCheckerboard(canvas, checkerboard_color, 0x00000000, 12);
44  canvas->restore();
45 
46  // Stroke the drawn area
47  SkPaint debugPaint;
48  debugPaint.setStrokeWidth(8);
49  debugPaint.setColor(SkColorSetA(checkerboard_color, 255));
50  debugPaint.setStyle(SkPaint::kStroke_Style);
51  canvas->drawRect(rect, debugPaint);
52 }
53 
54 } // 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:29