SkDiscardableMemory_chrome.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "skia/ext/SkDiscardableMemory_chrome.h"
  5. #include <stddef.h>
  6. #include <utility>
  7. #include "base/callback_helpers.h"
  8. #include "base/memory/discardable_memory.h"
  9. #include "base/memory/discardable_memory_allocator.h"
  10. SkDiscardableMemoryChrome::~SkDiscardableMemoryChrome() = default;
  11. bool SkDiscardableMemoryChrome::lock() {
  12. return discardable_->Lock();
  13. }
  14. void* SkDiscardableMemoryChrome::data() {
  15. return discardable_->data();
  16. }
  17. void SkDiscardableMemoryChrome::unlock() {
  18. discardable_->Unlock();
  19. }
  20. SkDiscardableMemoryChrome::SkDiscardableMemoryChrome(
  21. std::unique_ptr<base::DiscardableMemory> memory)
  22. : discardable_(std::move(memory)) {}
  23. base::trace_event::MemoryAllocatorDump*
  24. SkDiscardableMemoryChrome::CreateMemoryAllocatorDump(
  25. const char* name,
  26. base::trace_event::ProcessMemoryDump* pmd) const {
  27. return discardable_->CreateMemoryAllocatorDump(name, pmd);
  28. }
  29. SkDiscardableMemory* SkDiscardableMemory::Create(size_t bytes) {
  30. // TODO(crbug.com/1034271): Make the caller handle a nullptr return value,
  31. // and do not die when the allocation fails.
  32. auto discardable = base::DiscardableMemoryAllocator::GetInstance()
  33. ->AllocateLockedDiscardableMemoryWithRetryOrDie(
  34. bytes, base::DoNothing());
  35. return new SkDiscardableMemoryChrome(std::move(discardable));
  36. }