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/SkImage.h"
11
12namespace flutter {
13
14namespace {
15
16std::shared_ptr<DlColorSource> CreateCheckerboardShader(SkColor c1,
17 SkColor c2,
18 int size) {
19 SkBitmap bm;
20 bm.allocN32Pixels(width: 2 * size, height: 2 * size);
21 bm.eraseColor(c: c1);
22 bm.eraseArea(area: SkIRect::MakeLTRB(l: 0, t: 0, r: size, b: size), c: c2);
23 bm.eraseArea(area: SkIRect::MakeLTRB(l: size, t: size, r: 2 * size, b: 2 * size), c: c2);
24 auto image = DlImage::Make(image: SkImages::RasterFromBitmap(bitmap: bm));
25 return std::make_shared<DlImageColorSource>(
26 args&: image, args: DlTileMode::kRepeat, args: DlTileMode::kRepeat,
27 args: DlImageSampling::kNearestNeighbor);
28}
29
30} // anonymous namespace
31
32void DrawCheckerboard(DlCanvas* canvas, const SkRect& rect) {
33 // Draw a checkerboard
34 canvas->Save();
35 canvas->ClipRect(rect, clip_op: DlCanvas::ClipOp::kIntersect, is_aa: false);
36
37 // Secure random number generation isn't needed here.
38 // NOLINTBEGIN(clang-analyzer-security.insecureAPI.rand)
39 auto checkerboard_color =
40 SkColorSetARGB(a: 64, r: rand() % 256, g: rand() % 256, b: rand() % 256);
41 // NOLINTEND(clang-analyzer-security.insecureAPI.rand)
42
43 DlPaint paint;
44 paint.setColorSource(
45 CreateCheckerboardShader(c1: checkerboard_color, c2: 0x00000000, size: 12));
46 canvas->DrawPaint(paint);
47 canvas->Restore();
48
49 // Stroke the drawn area
50 DlPaint debug_paint;
51 debug_paint.setStrokeWidth(8);
52 debug_paint.setColor(SkColorSetA(c: checkerboard_color, a: 255));
53 debug_paint.setDrawStyle(DlDrawStyle::kStroke);
54 canvas->DrawRect(rect, paint: debug_paint);
55}
56
57} // namespace flutter
58

source code of flutter_engine/flutter/flow/paint_utils.cc