ShareableBitmap.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2010 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. #include "config.h"
  26. #include "ShareableBitmap.h"
  27. #include "SharedMemory.h"
  28. #include "WebCoreArgumentCoders.h"
  29. #include <WebCore/GraphicsContext.h>
  30. using namespace WebCore;
  31. namespace WebKit {
  32. ShareableBitmap::Handle::Handle()
  33. : m_flags(0)
  34. {
  35. }
  36. void ShareableBitmap::Handle::encode(CoreIPC::ArgumentEncoder& encoder) const
  37. {
  38. encoder << m_handle;
  39. encoder << m_size;
  40. encoder << m_flags;
  41. }
  42. bool ShareableBitmap::Handle::decode(CoreIPC::ArgumentDecoder& decoder, Handle& handle)
  43. {
  44. if (!decoder.decode(handle.m_handle))
  45. return false;
  46. if (!decoder.decode(handle.m_size))
  47. return false;
  48. if (!decoder.decode(handle.m_flags))
  49. return false;
  50. return true;
  51. }
  52. PassRefPtr<ShareableBitmap> ShareableBitmap::create(const IntSize& size, Flags flags)
  53. {
  54. size_t numBytes = numBytesForSize(size);
  55. void* data = 0;
  56. if (!tryFastMalloc(numBytes).getValue(data))
  57. return 0;
  58. return adoptRef(new ShareableBitmap(size, flags, data));
  59. }
  60. PassRefPtr<ShareableBitmap> ShareableBitmap::createShareable(const IntSize& size, Flags flags)
  61. {
  62. size_t numBytes = numBytesForSize(size);
  63. RefPtr<SharedMemory> sharedMemory = SharedMemory::create(numBytes);
  64. if (!sharedMemory)
  65. return 0;
  66. return adoptRef(new ShareableBitmap(size, flags, sharedMemory));
  67. }
  68. PassRefPtr<ShareableBitmap> ShareableBitmap::create(const IntSize& size, Flags flags, PassRefPtr<SharedMemory> sharedMemory)
  69. {
  70. ASSERT(sharedMemory);
  71. size_t numBytes = numBytesForSize(size);
  72. ASSERT_UNUSED(numBytes, sharedMemory->size() >= numBytes);
  73. return adoptRef(new ShareableBitmap(size, flags, sharedMemory));
  74. }
  75. PassRefPtr<ShareableBitmap> ShareableBitmap::create(const Handle& handle, SharedMemory::Protection protection)
  76. {
  77. // Create the shared memory.
  78. RefPtr<SharedMemory> sharedMemory = SharedMemory::create(handle.m_handle, protection);
  79. if (!sharedMemory)
  80. return 0;
  81. return create(handle.m_size, handle.m_flags, sharedMemory.release());
  82. }
  83. bool ShareableBitmap::createHandle(Handle& handle, SharedMemory::Protection protection)
  84. {
  85. ASSERT(isBackedBySharedMemory());
  86. if (!m_sharedMemory->createHandle(handle.m_handle, protection))
  87. return false;
  88. handle.m_size = m_size;
  89. handle.m_flags = m_flags;
  90. return true;
  91. }
  92. ShareableBitmap::ShareableBitmap(const IntSize& size, Flags flags, void* data)
  93. : m_size(size)
  94. , m_flags(flags)
  95. , m_data(data)
  96. {
  97. }
  98. ShareableBitmap::ShareableBitmap(const IntSize& size, Flags flags, PassRefPtr<SharedMemory> sharedMemory)
  99. : m_size(size)
  100. , m_flags(flags)
  101. , m_sharedMemory(sharedMemory)
  102. , m_data(0)
  103. {
  104. }
  105. ShareableBitmap::~ShareableBitmap()
  106. {
  107. if (!isBackedBySharedMemory())
  108. fastFree(m_data);
  109. }
  110. bool ShareableBitmap::resize(const IntSize& size)
  111. {
  112. // We can't resize backing stores that are backed by shared memory.
  113. ASSERT(!isBackedBySharedMemory());
  114. if (size == m_size)
  115. return true;
  116. size_t newNumBytes = numBytesForSize(size);
  117. // Try to resize.
  118. char* newData = 0;
  119. if (!tryFastRealloc(m_data, newNumBytes).getValue(newData)) {
  120. // We failed, but the backing store is still kept in a consistent state.
  121. return false;
  122. }
  123. m_size = size;
  124. m_data = newData;
  125. return true;
  126. }
  127. void* ShareableBitmap::data() const
  128. {
  129. if (isBackedBySharedMemory())
  130. return m_sharedMemory->data();
  131. ASSERT(m_data);
  132. return m_data;
  133. }
  134. } // namespace WebKit