transport_dib.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2012 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. #ifndef UI_SURFACE_TRANSPORT_DIB_H_
  5. #define UI_SURFACE_TRANSPORT_DIB_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include "base/memory/shared_memory_mapping.h"
  9. #include "base/memory/unsafe_shared_memory_region.h"
  10. #include "build/build_config.h"
  11. #include "ui/surface/surface_export.h"
  12. class SkCanvas;
  13. // -----------------------------------------------------------------------------
  14. // A TransportDIB is a block of memory that is used to transport pixels
  15. // between processes: from the renderer process to the browser, and
  16. // between renderer and plugin processes.
  17. // -----------------------------------------------------------------------------
  18. class SURFACE_EXPORT TransportDIB {
  19. public:
  20. TransportDIB(const TransportDIB&) = delete;
  21. TransportDIB& operator=(const TransportDIB&) = delete;
  22. ~TransportDIB();
  23. // Creates and maps a new TransportDIB with a shared memory region.
  24. // Returns nullptr on failure.
  25. static std::unique_ptr<TransportDIB> Map(
  26. base::UnsafeSharedMemoryRegion region);
  27. // Creates a new TransportDIB with a shared memory region. This always returns
  28. // a valid pointer. The DIB is not mapped.
  29. static std::unique_ptr<TransportDIB> CreateWithHandle(
  30. base::UnsafeSharedMemoryRegion region);
  31. // Returns a canvas using the memory of this TransportDIB. The returned
  32. // pointer will be owned by the caller. The bitmap will be of the given size,
  33. // which should fit inside this memory. Bitmaps returned will be either
  34. // opaque or have premultiplied alpha.
  35. //
  36. // On POSIX, this |TransportDIB| will be mapped if not already. On Windows,
  37. // this |TransportDIB| will NOT be mapped and should not be mapped prior,
  38. // because PlatformCanvas will map the file internally.
  39. //
  40. // Will return NULL on allocation failure. This could be because the image
  41. // is too large to map into the current process' address space.
  42. std::unique_ptr<SkCanvas> GetPlatformCanvas(int w, int h, bool opaque);
  43. // Map the DIB into the current process if it is not already. This is used to
  44. // map a DIB that has already been created. Returns true if the DIB is mapped.
  45. bool Map();
  46. // Return a pointer to the shared memory.
  47. void* memory() const;
  48. // Return the maximum size of the shared memory. This is not the amount of
  49. // data which is valid, you have to know that via other means, this is simply
  50. // the maximum amount that /could/ be valid.
  51. size_t size() const { return size_; }
  52. // Returns a pointer to the UnsafeSharedMemoryRegion object that backs the
  53. // transport dib.
  54. base::UnsafeSharedMemoryRegion* shared_memory_region();
  55. private:
  56. // Verifies that the dib can hold a canvas of the requested dimensions.
  57. bool VerifyCanvasSize(int w, int h);
  58. explicit TransportDIB(base::UnsafeSharedMemoryRegion region);
  59. base::UnsafeSharedMemoryRegion shm_region_;
  60. base::WritableSharedMemoryMapping shm_mapping_;
  61. size_t size_ = 0;
  62. };
  63. #endif // UI_SURFACE_TRANSPORT_DIB_H_