SkMallocPixelRef.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2008 The Android Open Source Project
  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 SkMallocPixelRef_DEFINED
  8. #define SkMallocPixelRef_DEFINED
  9. #include "SkPixelRef.h"
  10. #include "SkRefCnt.h"
  11. #include "SkTypes.h"
  12. class SkData;
  13. struct SkImageInfo;
  14. /** We explicitly use the same allocator for our pixels that SkMask does,
  15. so that we can freely assign memory allocated by one class to the other.
  16. */
  17. class SK_API SkMallocPixelRef : public SkPixelRef {
  18. public:
  19. /**
  20. * Return a new SkMallocPixelRef with the provided pixel storage, rowBytes,
  21. * and optional colortable. The caller is responsible for managing the
  22. * lifetime of the pixel storage buffer, as this pixelref will not try
  23. * to delete it.
  24. *
  25. * Returns NULL on failure.
  26. */
  27. static sk_sp<SkPixelRef> MakeDirect(const SkImageInfo&, void* addr, size_t rowBytes);
  28. /**
  29. * Return a new SkMallocPixelRef, automatically allocating storage for the
  30. * pixels. If rowBytes are 0, an optimal value will be chosen automatically.
  31. * If rowBytes is > 0, then it will be respected, or NULL will be returned
  32. * if rowBytes is invalid for the specified info.
  33. *
  34. * This pixelref will ref() the specified colortable (if not NULL).
  35. *
  36. * Returns NULL on failure.
  37. */
  38. static sk_sp<SkPixelRef> MakeAllocate(const SkImageInfo&, size_t rowBytes);
  39. /**
  40. * Identical to MakeAllocate, except all pixel bytes are zeroed.
  41. */
  42. static sk_sp<SkPixelRef> MakeZeroed(const SkImageInfo&, size_t rowBytes);
  43. /**
  44. * Return a new SkMallocPixelRef with the provided pixel storage,
  45. * rowBytes, and optional colortable. On destruction, ReleaseProc
  46. * will be called.
  47. *
  48. * If ReleaseProc is NULL, the pixels will never be released. This
  49. * can be useful if the pixels were stack allocated. However, such an
  50. * SkMallocPixelRef must not live beyond its pixels (e.g. by copying
  51. * an SkBitmap pointing to it, or drawing to an SkPicture).
  52. *
  53. * Returns NULL on failure.
  54. */
  55. typedef void (*ReleaseProc)(void* addr, void* context);
  56. static sk_sp<SkPixelRef> MakeWithProc(const SkImageInfo& info, size_t rowBytes, void* addr,
  57. ReleaseProc proc, void* context);
  58. /**
  59. * Return a new SkMallocPixelRef that will use the provided
  60. * SkData, rowBytes, and optional colortable as pixel storage.
  61. * The SkData will be ref()ed and on destruction of the PielRef,
  62. * the SkData will be unref()ed.
  63. *
  64. * Returns NULL on failure.
  65. */
  66. static sk_sp<SkPixelRef> MakeWithData(const SkImageInfo&, size_t rowBytes, sk_sp<SkData> data);
  67. protected:
  68. ~SkMallocPixelRef() override;
  69. private:
  70. // Uses alloc to implement NewAllocate or NewZeroed.
  71. static sk_sp<SkPixelRef> MakeUsing(void*(*alloc)(size_t),
  72. const SkImageInfo&,
  73. size_t rowBytes);
  74. ReleaseProc fReleaseProc;
  75. void* fReleaseProcContext;
  76. SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, ReleaseProc proc, void* context);
  77. typedef SkPixelRef INHERITED;
  78. };
  79. #endif