blob: b622ecf035e364528563e2bc40a2838d573f3951 [file] [log] [blame]
Hal Canary83c2f702019-03-07 14:53:03 -05001// 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 "fiddle/examples.h"
4// HASH=1b2800d23c9ea249b45c2c21a34b6d14
5REG_FIDDLE(Bitmap_053, 256, 32, false, 0) {
6class TinyAllocator : public SkBitmap::Allocator {
7public:
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
21void 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