SkAutoPixmapStorage.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 SkAutoPixmapStorage_DEFINED
  8. #define SkAutoPixmapStorage_DEFINED
  9. #include "include/core/SkPixmap.h"
  10. #include "include/private/SkMalloc.h"
  11. class SkAutoPixmapStorage : public SkPixmap {
  12. public:
  13. SkAutoPixmapStorage();
  14. ~SkAutoPixmapStorage();
  15. /**
  16. * Leave the moved-from object in a free-but-valid state.
  17. */
  18. SkAutoPixmapStorage& operator=(SkAutoPixmapStorage&& other);
  19. /**
  20. * Try to allocate memory for the pixels needed to match the specified Info. On success
  21. * return true and fill out the pixmap to point to that memory. The storage will be freed
  22. * when this object is destroyed, or if another call to tryAlloc() or alloc() is made.
  23. *
  24. * On failure, return false and reset() the pixmap to empty.
  25. */
  26. bool tryAlloc(const SkImageInfo&);
  27. /**
  28. * Allocate memory for the pixels needed to match the specified Info and fill out the pixmap
  29. * to point to that memory. The storage will be freed when this object is destroyed,
  30. * or if another call to tryAlloc() or alloc() is made.
  31. *
  32. * If the memory cannot be allocated, calls SK_ABORT().
  33. */
  34. void alloc(const SkImageInfo&);
  35. /**
  36. * Gets the size and optionally the rowBytes that would be allocated by SkAutoPixmapStorage if
  37. * alloc/tryAlloc was called.
  38. */
  39. static size_t AllocSize(const SkImageInfo& info, size_t* rowBytes);
  40. /**
  41. * Returns an SkData object wrapping the allocated pixels memory, and resets the pixmap.
  42. * If the storage hasn't been allocated, the result is NULL.
  43. */
  44. sk_sp<SkData> SK_WARN_UNUSED_RESULT detachPixelsAsData();
  45. // We wrap these so we can clear our internal storage
  46. void reset() {
  47. this->freeStorage();
  48. this->INHERITED::reset();
  49. }
  50. void reset(const SkImageInfo& info, const void* addr, size_t rb) {
  51. this->freeStorage();
  52. this->INHERITED::reset(info, addr, rb);
  53. }
  54. bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask) {
  55. this->freeStorage();
  56. return this->INHERITED::reset(mask);
  57. }
  58. private:
  59. void* fStorage;
  60. void freeStorage() {
  61. sk_free(fStorage);
  62. fStorage = nullptr;
  63. }
  64. typedef SkPixmap INHERITED;
  65. };
  66. #endif