test_shared_bitmap_manager.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2014 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 "components/viz/test/test_shared_bitmap_manager.h"
  5. #include <stdint.h>
  6. #include <utility>
  7. #include "base/memory/read_only_shared_memory_region.h"
  8. #include "base/notreached.h"
  9. #include "components/viz/common/resources/bitmap_allocation.h"
  10. #include "components/viz/common/resources/resource_format_utils.h"
  11. #include "mojo/public/cpp/system/platform_handle.h"
  12. namespace viz {
  13. TestSharedBitmapManager::TestSharedBitmapManager() = default;
  14. TestSharedBitmapManager::~TestSharedBitmapManager() {
  15. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  16. }
  17. std::unique_ptr<SharedBitmap> TestSharedBitmapManager::GetSharedBitmapFromId(
  18. const gfx::Size&,
  19. ResourceFormat,
  20. const SharedBitmapId& id) {
  21. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  22. const auto it = mapping_map_.find(id);
  23. if (it == mapping_map_.end())
  24. return nullptr;
  25. // NOTE: pixels needs to be writable for legacy reasons, but SharedBitmap
  26. // instances returned by a SharedBitmapManager are always read-only.
  27. auto* pixels = static_cast<uint8_t*>(const_cast<void*>(it->second.memory()));
  28. return std::make_unique<SharedBitmap>(pixels);
  29. }
  30. base::UnguessableToken
  31. TestSharedBitmapManager::GetSharedBitmapTracingGUIDFromId(
  32. const SharedBitmapId& id) {
  33. const auto it = mapping_map_.find(id);
  34. if (it == mapping_map_.end())
  35. return {};
  36. return it->second.guid();
  37. }
  38. bool TestSharedBitmapManager::ChildAllocatedSharedBitmap(
  39. base::ReadOnlySharedMemoryMapping mapping,
  40. const SharedBitmapId& id) {
  41. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  42. // TestSharedBitmapManager is both the client and service side. So the
  43. // notification here should be about a bitmap that was previously allocated
  44. // with AllocateSharedBitmap().
  45. if (mapping_map_.find(id) == mapping_map_.end()) {
  46. mapping_map_.emplace(id, std::move(mapping));
  47. }
  48. // The same bitmap id should not be notified more than once.
  49. DCHECK_EQ(notified_set_.count(id), 0u);
  50. notified_set_.insert(id);
  51. return true;
  52. }
  53. bool TestSharedBitmapManager::LocalAllocatedSharedBitmap(
  54. SkBitmap bitmap,
  55. const SharedBitmapId& id) {
  56. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  57. NOTIMPLEMENTED();
  58. return false;
  59. }
  60. void TestSharedBitmapManager::ChildDeletedSharedBitmap(
  61. const SharedBitmapId& id) {
  62. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  63. notified_set_.erase(id);
  64. mapping_map_.erase(id);
  65. }
  66. } // namespace viz