1cb93a386Sopenharmony_ci// Copyright 2019 Google LLC.
2cb93a386Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3cb93a386Sopenharmony_ci#include "tools/fiddle/examples.h"
4cb93a386Sopenharmony_ci// HASH=525285073aae7e53eb8f454a398f880c
5cb93a386Sopenharmony_ciREG_FIDDLE(Canvas_MakeRasterDirect, 256, 256, true, 0) {
6cb93a386Sopenharmony_civoid draw(SkCanvas* ) {
7cb93a386Sopenharmony_ci    SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);  // device aligned, 32 bpp, Premultiplied
8cb93a386Sopenharmony_ci    const size_t minRowBytes = info.minRowBytes();  // bytes used by one bitmap row
9cb93a386Sopenharmony_ci    const size_t size = info.computeMinByteSize();  // bytes used by all rows
10cb93a386Sopenharmony_ci    SkAutoTMalloc<SkPMColor> storage(size);  // allocate storage for pixels
11cb93a386Sopenharmony_ci    SkPMColor* pixels = storage.get();  // get pointer to allocated storage
12cb93a386Sopenharmony_ci    // create a SkCanvas backed by a raster device, and delete it when the
13cb93a386Sopenharmony_ci    // function goes out of scope.
14cb93a386Sopenharmony_ci    std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, pixels, minRowBytes);
15cb93a386Sopenharmony_ci    canvas->clear(SK_ColorWHITE);  // white is Unpremultiplied, in ARGB order
16cb93a386Sopenharmony_ci    canvas->flush();  // ensure that pixels are cleared
17cb93a386Sopenharmony_ci    SkPMColor pmWhite = pixels[0];  // the Premultiplied format may vary
18cb93a386Sopenharmony_ci    SkPaint paint;  // by default, draws black
19cb93a386Sopenharmony_ci    canvas->drawPoint(1, 1, paint);  // draw in the center
20cb93a386Sopenharmony_ci    canvas->flush();  // ensure that point was drawn
21cb93a386Sopenharmony_ci    for (int y = 0; y < info.height(); ++y) {
22cb93a386Sopenharmony_ci        for (int x = 0; x < info.width(); ++x) {
23cb93a386Sopenharmony_ci            SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
24cb93a386Sopenharmony_ci        }
25cb93a386Sopenharmony_ci        SkDebugf("\n");
26cb93a386Sopenharmony_ci    }
27cb93a386Sopenharmony_ci}
28cb93a386Sopenharmony_ci}  // END FIDDLE
29