blob: e12e68ebdd01d33dc2d07d54424b57958f4fcfdd [file] [log] [blame]
Hal Canary87515122019-03-15 14:22:51 -04001// 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 Kleinc0bd9f92019-04-23 12:05:21 -05003#include "tools/fiddle/examples.h"
Hal Canary87515122019-03-15 14:22:51 -04004// HASH=1b2800d23c9ea249b45c2c21a34b6d14
Hal Canarya7181e7c2019-03-18 16:06:34 -04005REG_FIDDLE(Bitmap_allocPixels_4, 256, 32, false, 0) {
Hal Canary87515122019-03-15 14:22:51 -04006class 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