xref: /aosp_15_r20/external/skia/docs/examples/Canvas_accessTopRasterHandle.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 <memory>
4 
5 #include "tools/fiddle/examples.h"
6 REG_FIDDLE(Canvas_accessTopRasterHandle, 256, 256, true, 0) {
DeleteCallback(void *,void * context)7 static void DeleteCallback(void*, void* context) {
8     delete (char*) context;
9 }
10 class CustomAllocator : public SkRasterHandleAllocator {
11 public:
allocHandle(const SkImageInfo & info,Rec * rec)12     bool allocHandle(const SkImageInfo& info, Rec* rec) override {
13         char* context = new char[4]{'s', 'k', 'i', 'a'};
14         rec->fReleaseProc = DeleteCallback;
15         rec->fReleaseCtx = context;
16         rec->fHandle = context;
17         rec->fPixels = context;
18         rec->fRowBytes = 4;
19         return true;
20     }
updateHandle(Handle handle,const SkMatrix & ctm,const SkIRect & clip_bounds)21     void updateHandle(Handle handle, const SkMatrix& ctm, const SkIRect& clip_bounds) override {
22         // apply canvas matrix and clip to custom environment
23     }
24 };
25 
draw(SkCanvas * canvas)26 void draw(SkCanvas* canvas) {
27     const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
28     std::unique_ptr<SkCanvas> c2 =
29             SkRasterHandleAllocator::MakeCanvas(std::make_unique<CustomAllocator>(), info);
30     char* context = (char*) c2->accessTopRasterHandle();
31     SkDebugf("context = %.4s\n", context);
32 }
33 }  // END FIDDLE
34