ShareableBitmap.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef ShareableBitmap_h
  26. #define ShareableBitmap_h
  27. #include "SharedMemory.h"
  28. #include <WebCore/IntRect.h>
  29. #include <wtf/PassOwnPtr.h>
  30. #include <wtf/PassRefPtr.h>
  31. #include <wtf/RefCounted.h>
  32. #include <wtf/RefPtr.h>
  33. #if USE(CG)
  34. #include <wtf/RetainPtr.h>
  35. #endif
  36. #if USE(CAIRO)
  37. #include <WebCore/RefPtrCairo.h>
  38. #endif
  39. #if PLATFORM(QT)
  40. #include <QImage>
  41. #ifdef Q_WS_X11
  42. // Avoid ambiguity caused by the Region typedef from qwindowdefs.h.
  43. namespace WebCore { class Region; }
  44. namespace WebKit { using WebCore::Region; }
  45. #endif
  46. #endif
  47. namespace WebCore {
  48. class Image;
  49. class GraphicsContext;
  50. }
  51. namespace WebKit {
  52. class ShareableBitmap : public RefCounted<ShareableBitmap> {
  53. public:
  54. enum Flag {
  55. NoFlags = 0,
  56. SupportsAlpha = 1 << 0,
  57. };
  58. typedef unsigned Flags;
  59. class Handle {
  60. WTF_MAKE_NONCOPYABLE(Handle);
  61. public:
  62. Handle();
  63. bool isNull() const { return m_handle.isNull(); }
  64. void encode(CoreIPC::ArgumentEncoder&) const;
  65. static bool decode(CoreIPC::ArgumentDecoder&, Handle&);
  66. private:
  67. friend class ShareableBitmap;
  68. mutable SharedMemory::Handle m_handle;
  69. WebCore::IntSize m_size;
  70. Flags m_flags;
  71. };
  72. // Create a shareable bitmap that uses malloced memory.
  73. static PassRefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags);
  74. // Create a shareable bitmap whose backing memory can be shared with another process.
  75. static PassRefPtr<ShareableBitmap> createShareable(const WebCore::IntSize&, Flags);
  76. // Create a shareable bitmap from an already existing shared memory block.
  77. static PassRefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags, PassRefPtr<SharedMemory>);
  78. // Create a shareable bitmap from a handle.
  79. static PassRefPtr<ShareableBitmap> create(const Handle&, SharedMemory::Protection = SharedMemory::ReadWrite);
  80. // Create a handle.
  81. bool createHandle(Handle&, SharedMemory::Protection = SharedMemory::ReadWrite);
  82. ~ShareableBitmap();
  83. const WebCore::IntSize& size() const { return m_size; }
  84. WebCore::IntRect bounds() const { return WebCore::IntRect(WebCore::IntPoint(), size()); }
  85. bool resize(const WebCore::IntSize& size);
  86. // Create a graphics context that can be used to paint into the backing store.
  87. PassOwnPtr<WebCore::GraphicsContext> createGraphicsContext();
  88. // Paint the backing store into the given context.
  89. void paint(WebCore::GraphicsContext&, const WebCore::IntPoint& destination, const WebCore::IntRect& source);
  90. void paint(WebCore::GraphicsContext&, float scaleFactor, const WebCore::IntPoint& destination, const WebCore::IntRect& source);
  91. bool isBackedBySharedMemory() const { return m_sharedMemory; }
  92. // This creates a bitmap image that directly references the shared bitmap data.
  93. // This is only safe to use when we know that the contents of the shareable bitmap won't change.
  94. PassRefPtr<WebCore::Image> createImage();
  95. #if USE(CG)
  96. // This creates a copied CGImageRef (most likely a copy-on-write) of the shareable bitmap.
  97. RetainPtr<CGImageRef> makeCGImageCopy();
  98. // This creates a CGImageRef that directly references the shared bitmap data.
  99. // This is only safe to use when we know that the contents of the shareable bitmap won't change.
  100. RetainPtr<CGImageRef> makeCGImage();
  101. #elif USE(CAIRO)
  102. // This creates a BitmapImage that directly references the shared bitmap data.
  103. // This is only safe to use when we know that the contents of the shareable bitmap won't change.
  104. PassRefPtr<cairo_surface_t> createCairoSurface();
  105. #elif PLATFORM(QT)
  106. // This creates a QImage that directly references the shared bitmap data.
  107. // This is only safe to use when we know that the contents of the shareable bitmap won't change.
  108. QImage createQImage();
  109. static void releaseSharedMemoryData(void* typelessBitmap);
  110. #endif
  111. private:
  112. ShareableBitmap(const WebCore::IntSize&, Flags, void*);
  113. ShareableBitmap(const WebCore::IntSize&, Flags, PassRefPtr<SharedMemory>);
  114. #if USE(CAIRO)
  115. static size_t numBytesForSize(const WebCore::IntSize&);
  116. #else
  117. static size_t numBytesForSize(const WebCore::IntSize& size) { return size.width() * size.height() * 4; }
  118. #endif
  119. #if USE(CG)
  120. RetainPtr<CGImageRef> createCGImage(CGDataProviderRef) const;
  121. static void releaseBitmapContextData(void* typelessBitmap, void* typelessData);
  122. static void releaseDataProviderData(void* typelessBitmap, const void* typelessData, size_t);
  123. #endif
  124. #if USE(CAIRO)
  125. static void releaseSurfaceData(void* typelessBitmap);
  126. #endif
  127. void* data() const;
  128. size_t sizeInBytes() const { return numBytesForSize(m_size); }
  129. WebCore::IntSize m_size;
  130. Flags m_flags;
  131. // If the shareable bitmap is backed by shared memory, this points to the shared memory object.
  132. RefPtr<SharedMemory> m_sharedMemory;
  133. // If the shareable bitmap is backed by fastMalloced memory, this points to the data.
  134. void* m_data;
  135. };
  136. } // namespace WebKit
  137. #endif // ShareableBitmap_h