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=eb6f861ca1839146d26e40d56c2a001c |
Hal Canary | a7181e7c | 2019-03-18 16:06:34 -0400 | [diff] [blame] | 5 | REG_FIDDLE(Bitmap_tryAllocPixels_4, 256, 100, false, 0) { |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 6 | class LargePixelRef : public SkPixelRef { |
| 7 | public: |
| 8 | LargePixelRef(const SkImageInfo& info, char* storage, size_t rowBytes) |
| 9 | : SkPixelRef(info.width(), info.height(), storage, rowBytes) { |
| 10 | } |
| 11 | ~LargePixelRef() override { |
| 12 | delete[] (char* ) this->pixels(); |
| 13 | } |
| 14 | }; |
| 15 | class LargeAllocator : public SkBitmap::Allocator { |
| 16 | public: |
| 17 | bool allocPixelRef(SkBitmap* bitmap) override { |
| 18 | const SkImageInfo& info = bitmap->info(); |
| 19 | uint64_t rowBytes = info.minRowBytes64(); |
| 20 | uint64_t size = info.height() * rowBytes; |
| 21 | char* addr = new char[size]; |
| 22 | if (nullptr == addr) { |
| 23 | return false; |
| 24 | } |
| 25 | sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(new LargePixelRef(info, addr, rowBytes)); |
| 26 | if (!pr) { |
| 27 | return false; |
| 28 | } |
| 29 | bitmap->setPixelRef(std::move(pr), 0, 0); |
| 30 | return true; |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | void draw(SkCanvas* canvas) { |
| 35 | LargeAllocator largeAllocator; |
| 36 | SkBitmap bitmap; |
| 37 | int width = 100; // make this 20000 |
| 38 | int height = 100; // and this 100000 to allocate 8 gigs on a 64-bit platform |
| 39 | bitmap.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType)); |
| 40 | if (bitmap.tryAllocPixels(&largeAllocator)) { |
| 41 | bitmap.eraseColor(0xff55aa33); |
| 42 | canvas->drawBitmap(bitmap, 0, 0); |
| 43 | } |
| 44 | } |
| 45 | } // END FIDDLE |