Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 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. |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 3 | #include "tools/fiddle/examples.h" |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 4 | // HASH=c26cfae4c42cb445240335cc12a50235 |
Hal Canary | a7181e7c | 2019-03-18 16:06:34 -0400 | [diff] [blame] | 5 | REG_FIDDLE(Canvas_const_SkBitmap_const_SkSurfaceProps, 256, 256, true, 0) { |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 6 | void draw(SkCanvas* ) { |
| 7 | SkBitmap bitmap; |
| 8 | // create a bitmap 5 wide and 11 high |
| 9 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(5, 11)); |
| 10 | SkCanvas canvas(bitmap, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); |
| 11 | canvas.clear(SK_ColorWHITE); // white is Unpremultiplied, in ARGB order |
| 12 | SkPixmap pixmap; // provides guaranteed access to the drawn pixels |
| 13 | if (!canvas.peekPixels(&pixmap)) { |
| 14 | SkDebugf("peekPixels should never fail.\n"); |
| 15 | } |
| 16 | const SkPMColor* pixels = pixmap.addr32(); // points to top-left of bitmap |
| 17 | SkPMColor pmWhite = pixels[0]; // the Premultiplied format may vary |
| 18 | SkPaint paint; // by default, draws black, 12 point text |
| 19 | SkFont font; |
| 20 | canvas.drawString("!", 1, 10, font, paint); // 1 char at baseline (1, 10) |
| 21 | for (int y = 0; y < bitmap.height(); ++y) { |
| 22 | for (int x = 0; x < bitmap.width(); ++x) { |
| 23 | SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x'); |
| 24 | } |
| 25 | SkDebugf("\n"); |
| 26 | } |
| 27 | } |
| 28 | } // END FIDDLE |