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=1b2800d23c9ea249b45c2c21a34b6d14 |
Hal Canary | a7181e7c | 2019-03-18 16:06:34 -0400 | [diff] [blame] | 5 | REG_FIDDLE(Bitmap_allocPixels_4, 256, 32, false, 0) { |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 6 | class TinyAllocator : public SkBitmap::Allocator { |
| 7 | public: |
| 8 | bool allocPixelRef(SkBitmap* bitmap) override { |
| 9 | const SkImageInfo& info = bitmap->info(); |
| 10 | if (info.height() * info.minRowBytes() > sizeof(storage)) { |
| 11 | return false; |
| 12 | } |
| 13 | sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>( |
| 14 | new SkPixelRef(info.width(), info.height(), storage, info.minRowBytes())); |
| 15 | bitmap->setPixelRef(std::move(pr), 0, 0); |
| 16 | return true; |
| 17 | } |
| 18 | char storage[16]; |
| 19 | }; |
| 20 | |
| 21 | void draw(SkCanvas* canvas) { |
| 22 | TinyAllocator tinyAllocator; |
| 23 | SkBitmap bitmap; |
| 24 | bitmap.setInfo(SkImageInfo::MakeN32(2, 2, kOpaque_SkAlphaType)); |
| 25 | if (bitmap.tryAllocPixels(&tinyAllocator)) { |
| 26 | bitmap.eraseColor(0xff55aa33); |
| 27 | bitmap.erase(0xffaa3355, SkIRect::MakeXYWH(1, 1, 1, 1)); |
| 28 | canvas->scale(16, 16); |
| 29 | canvas->drawBitmap(bitmap, 0, 0); |
| 30 | } |
| 31 | } |
| 32 | } // END FIDDLE |