blob: 702323652418795138f897a788a4f1766e7799bc [file] [log] [blame]
Hal Canary87515122019-03-15 14:22:51 -04001// 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.
Mike Kleinc0bd9f92019-04-23 12:05:21 -05003#include "tools/fiddle/examples.h"
Hal Canary87515122019-03-15 14:22:51 -04004// HASH=87f55e62ec4c3535e1a5d0f1415b20c6
Hal Canarya7181e7c2019-03-18 16:06:34 -04005REG_FIDDLE(Canvas_MakeRasterDirectN32, 256, 256, true, 0) {
Hal Canary87515122019-03-15 14:22:51 -04006void draw(SkCanvas* ) {
7 const int width = 3;
8 const int height = 3;
9 SkPMColor pixels[height][width]; // allocate a 3 by 3 Premultiplied bitmap on the stack
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::MakeRasterDirectN32(
13 width,
14 height,
15 pixels[0], // top-left of the bitmap
16 sizeof(pixels[0])); // byte width of the each row
17 // write a Premultiplied value for white into all pixels in the bitmap
18 canvas->clear(SK_ColorWHITE);
19 SkPMColor pmWhite = pixels[0][0]; // the Premultiplied format may vary
20 SkPaint paint; // by default, draws black
21 canvas->drawPoint(1, 1, paint); // draw in the center
22 canvas->flush(); // ensure that pixels is ready to be read
23 for (int y = 0; y < height; ++y) {
24 for (int x = 0; x < width; ++x) {
25 SkDebugf("%c", pixels[y][x] == pmWhite ? '-' : 'x');
26 }
27 SkDebugf("\n");
28 }
29}
30} // END FIDDLE