xref: /aosp_15_r20/external/skia/docs/examples/Canvas_MakeRasterDirectN32.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 REG_FIDDLE(Canvas_MakeRasterDirectN32, 256, 256, true, 0) {
draw(SkCanvas *)5 void draw(SkCanvas* ) {
6     const int width = 3;
7     const int height = 3;
8     SkPMColor pixels[height][width];  // allocate a 3 by 3 Premultiplied bitmap on the stack
9     // create a SkCanvas backed by a raster device, and delete it when the
10     // function goes out of scope.
11     std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirectN32(
12             width,
13             height,
14             pixels[0],  // top-left of the bitmap
15             sizeof(pixels[0]));  // byte width of the each row
16     // write a Premultiplied value for white into all pixels in the bitmap
17     canvas->clear(SK_ColorWHITE);
18     SkPMColor pmWhite = pixels[0][0];  // the Premultiplied format may vary
19     SkPaint paint;  // by default, draws black
20     canvas->drawPoint(1, 1, paint);  // draw in the center
21     for (int y = 0; y < height; ++y) {
22         for (int x = 0; x < width; ++x) {
23             SkDebugf("%c", pixels[y][x] == pmWhite ? '-' : 'x');
24         }
25         SkDebugf("\n");
26     }
27 }
28 }  // END FIDDLE
29