SkRasterHandleAllocator.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkRasterHandleAllocator_DEFINED
  8. #define SkRasterHandleAllocator_DEFINED
  9. #include "include/core/SkImageInfo.h"
  10. class SkBitmap;
  11. class SkCanvas;
  12. class SkMatrix;
  13. /**
  14. * If a client wants to control the allocation of raster layers in a canvas, it should subclass
  15. * SkRasterHandleAllocator. This allocator performs two tasks:
  16. * 1. controls how the memory for the pixels is allocated
  17. * 2. associates a "handle" to a private object that can track the matrix/clip of the SkCanvas
  18. *
  19. * This example allocates a canvas, and defers to the allocator to create the base layer.
  20. *
  21. * std::unique_ptr<SkCanvas> canvas = SkRasterHandleAllocator::MakeCanvas(
  22. * SkImageInfo::Make(...),
  23. * skstd::make_unique<MySubclassRasterHandleAllocator>(...),
  24. * nullptr);
  25. *
  26. * If you have already allocated the base layer (and its handle, release-proc etc.) then you
  27. * can pass those in using the last parameter to MakeCanvas().
  28. *
  29. * Regardless of how the base layer is allocated, each time canvas->saveLayer() is called,
  30. * your allocator's allocHandle() will be called.
  31. */
  32. class SK_API SkRasterHandleAllocator {
  33. public:
  34. virtual ~SkRasterHandleAllocator() {}
  35. // The value that is returned to clients of the canvas that has this allocator installed.
  36. typedef void* Handle;
  37. struct Rec {
  38. // When the allocation goes out of scope, this proc is called to free everything associated
  39. // with it: the pixels, the "handle", etc. This is passed the pixel address and fReleaseCtx.
  40. void (*fReleaseProc)(void* pixels, void* ctx);
  41. void* fReleaseCtx; // context passed to fReleaseProc
  42. void* fPixels; // pixels for this allocation
  43. size_t fRowBytes; // rowbytes for these pixels
  44. Handle fHandle; // public handle returned by SkCanvas::accessTopRasterHandle()
  45. };
  46. /**
  47. * Given a requested info, allocate the corresponding pixels/rowbytes, and whatever handle
  48. * is desired to give clients access to those pixels. The rec also contains a proc and context
  49. * which will be called when this allocation goes out of scope.
  50. *
  51. * e.g.
  52. * when canvas->saveLayer() is called, the allocator will be called to allocate the pixels
  53. * for the layer. When canvas->restore() is called, the fReleaseProc will be called.
  54. */
  55. virtual bool allocHandle(const SkImageInfo&, Rec*) = 0;
  56. /**
  57. * Clients access the handle for a given layer by calling SkCanvas::accessTopRasterHandle().
  58. * To allow the handle to reflect the current matrix/clip in the canvs, updateHandle() is
  59. * is called. The subclass is responsible to update the handle as it sees fit.
  60. */
  61. virtual void updateHandle(Handle, const SkMatrix&, const SkIRect&) = 0;
  62. /**
  63. * This creates a canvas which will use the allocator to manage pixel allocations, including
  64. * all calls to saveLayer().
  65. *
  66. * If rec is non-null, then it will be used as the base-layer of pixels/handle.
  67. * If rec is null, then the allocator will be called for the base-layer as well.
  68. */
  69. static std::unique_ptr<SkCanvas> MakeCanvas(std::unique_ptr<SkRasterHandleAllocator>,
  70. const SkImageInfo&, const Rec* rec = nullptr);
  71. private:
  72. friend class SkBitmapDevice;
  73. Handle allocBitmap(const SkImageInfo&, SkBitmap*);
  74. };
  75. #endif