xref: /aosp_15_r20/external/skia/docs/examples/Bitmap_allocN32Pixels.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 // Copyright 2019 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #include "tools/fiddle/examples.h"
4 
5 #include <climits>
6 #include <random>
7 
8 REG_FIDDLE(Bitmap_allocN32Pixels, 256, 256, false, 0) {
draw(SkCanvas * canvas)9 void draw(SkCanvas* canvas) {
10     SkBitmap bitmap;
11     bitmap.allocN32Pixels(64, 64);
12     bitmap.eraseColor(SK_ColorTRANSPARENT);
13     std::default_random_engine rng;
14     const auto randUint = [&rng](uint32_t min, uint32_t max) -> uint32_t {
15         return std::uniform_int_distribution<uint32_t>(min, max)(rng);
16     };
17 
18     for (int y = 0; y < 256; y += 64) {
19         for (int x = 0; x < 256; x += 64) {
20             SkColor color = randUint(0, UINT_MAX);
21             uint32_t w = randUint(4, 32);
22             uint32_t cx = randUint(0, 64 - w);
23             uint32_t h = randUint(4, 32);
24             uint32_t cy = randUint(0, 64 - h);
25             bitmap.erase(color, SkIRect::MakeXYWH(cx, cy, w, h));
26             canvas->drawImage(bitmap.asImage(), x, y);
27         }
28     }
29 }
30 }  // END FIDDLE
31