xref: /aosp_15_r20/external/skia/docs/examples/Canvas_MakeRasterDirect.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_MakeRasterDirect, 256, 256, true, 0) {
draw(SkCanvas *)5 void draw(SkCanvas*) {
6     SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);  // device aligned, 32 bpp, Premultiplied
7     const size_t minRowBytes = info.minRowBytes();  // bytes used by one bitmap row
8     const size_t size = info.computeMinByteSize();  // bytes used by all rows
9     SkPMColor* pixels = new SkPMColor[size]; // allocate storage for pixels
10     // create a SkCanvas backed by a raster device, and delete it when the
11     // function goes out of scope.
12     std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, pixels, minRowBytes);
13     canvas->clear(SK_ColorWHITE);  // white is Unpremultiplied, in ARGB order
14     SkPMColor pmWhite = pixels[0];  // the Premultiplied format may vary
15     SkPaint paint;  // by default, draws black
16     canvas->drawPoint(1, 1, paint);  // draw in the center
17     for (int y = 0; y < info.height(); ++y) {
18         for (int x = 0; x < info.width(); ++x) {
19             SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
20         }
21         SkDebugf("\n");
22     }
23     delete[] pixels;
24 }
25 }  // END FIDDLE
26