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 // HASH=1b2800d23c9ea249b45c2c21a34b6d14
5 REG_FIDDLE(Bitmap_allocPixels_4, 256, 32, false, 0) {
6 class TinyAllocator : public SkBitmap::Allocator {
7 public:
allocPixelRef(SkBitmap * bitmap)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
draw(SkCanvas * canvas)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
33