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