Debian Codegraph @

docs/examples/Surface_MakeRasterDirectReleaseProc.cpp

raw
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 REG_FIDDLE(Surface_WrapPixels_WithReleaseProc, 256, 256, true, 0) {
5 static void release_direct_surface_storage(void* pixels, void* context) {
6 if (pixels == context) {
7 SkDebugf("expected release context\n");
8 }
9 sk_free(pixels);
10 }
11
12 void draw(SkCanvas*) {
13 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
14 const size_t rowBytes = info.minRowBytes();
15 void* pixels = sk_malloc_throw(info.computeByteSize(rowBytes));
16 sk_sp<SkSurface> surface(SkSurfaces::WrapPixels(
17 info, pixels, rowBytes, release_direct_surface_storage, pixels));
18 SkCanvas* canvas = surface->getCanvas();
19 canvas->clear(SK_ColorWHITE);
20 SkPMColor* colorPtr = (SkPMColor*)pixels;
21 SkPMColor pmWhite = colorPtr[0];
22 SkPaint paint;
23 canvas->drawPoint(1, 1, paint);
24 for (int y = 0; y < info.height(); ++y) {
25 for (int x = 0; x < info.width(); ++x) {
26 SkDebugf("%c", *colorPtr++ == pmWhite ? '-' : 'x');
27 }
28 SkDebugf("\n");
29 }
30 }
31 } // END FIDDLE