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. |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 3 | #include <memory> |
| 4 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 5 | #include "tools/fiddle/examples.h" |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 6 | // HASH=4486d0c0b22ad2931db130f42da4c80c |
Hal Canary | a7181e7c | 2019-03-18 16:06:34 -0400 | [diff] [blame] | 7 | REG_FIDDLE(Canvas_accessTopRasterHandle, 256, 256, true, 0) { |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 8 | static void DeleteCallback(void*, void* context) { |
| 9 | delete (char*) context; |
| 10 | } |
| 11 | class CustomAllocator : public SkRasterHandleAllocator { |
| 12 | public: |
| 13 | bool allocHandle(const SkImageInfo& info, Rec* rec) override { |
| 14 | char* context = new char[4]{'s', 'k', 'i', 'a'}; |
| 15 | rec->fReleaseProc = DeleteCallback; |
| 16 | rec->fReleaseCtx = context; |
| 17 | rec->fHandle = context; |
| 18 | rec->fPixels = context; |
| 19 | rec->fRowBytes = 4; |
| 20 | return true; |
| 21 | } |
| 22 | void updateHandle(Handle handle, const SkMatrix& ctm, const SkIRect& clip_bounds) override { |
| 23 | // apply canvas matrix and clip to custom environment |
| 24 | } |
| 25 | }; |
| 26 | |
| 27 | void draw(SkCanvas* canvas) { |
| 28 | const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 29 | std::unique_ptr<SkCanvas> c2 = |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 30 | SkRasterHandleAllocator::MakeCanvas(std::make_unique<CustomAllocator>(), info); |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 31 | char* context = (char*) c2->accessTopRasterHandle(); |
| 32 | SkDebugf("context = %.4s\n", context); |
| 33 | } |
| 34 | } // END FIDDLE |