SkAutoPixmapStorage.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "include/core/SkData.h"
  8. #include "src/core/SkAutoPixmapStorage.h"
  9. SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(nullptr) {}
  10. SkAutoPixmapStorage::~SkAutoPixmapStorage() {
  11. this->freeStorage();
  12. }
  13. SkAutoPixmapStorage& SkAutoPixmapStorage::operator=(SkAutoPixmapStorage&& other) {
  14. this->fStorage = other.fStorage;
  15. this->INHERITED::reset(other.info(), this->fStorage, other.rowBytes());
  16. other.fStorage = nullptr;
  17. other.INHERITED::reset();
  18. return *this;
  19. }
  20. size_t SkAutoPixmapStorage::AllocSize(const SkImageInfo& info, size_t* rowBytes) {
  21. size_t rb = info.minRowBytes();
  22. if (rowBytes) {
  23. *rowBytes = rb;
  24. }
  25. return info.computeByteSize(rb);
  26. }
  27. bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) {
  28. this->freeStorage();
  29. size_t rb;
  30. size_t size = AllocSize(info, &rb);
  31. if (SkImageInfo::ByteSizeOverflowed(size)) {
  32. return false;
  33. }
  34. void* pixels = sk_malloc_canfail(size);
  35. if (nullptr == pixels) {
  36. return false;
  37. }
  38. this->reset(info, pixels, rb);
  39. fStorage = pixels;
  40. return true;
  41. }
  42. void SkAutoPixmapStorage::alloc(const SkImageInfo& info) {
  43. SkASSERT_RELEASE(this->tryAlloc(info));
  44. }
  45. sk_sp<SkData> SkAutoPixmapStorage::detachPixelsAsData() {
  46. if (!fStorage) {
  47. return nullptr;
  48. }
  49. sk_sp<SkData> data = SkData::MakeFromMalloc(fStorage, this->computeByteSize());
  50. fStorage = nullptr;
  51. this->INHERITED::reset();
  52. return data;
  53. }