RemoteLayerTreeTransaction.mm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2012 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 "RemoteLayerTreeTransaction.h"
  27. #include "ArgumentCoders.h"
  28. #include "MessageDecoder.h"
  29. #include "MessageEncoder.h"
  30. #include "RemoteGraphicsLayer.h"
  31. #include "WebCoreArgumentCoders.h"
  32. #include <wtf/text/CString.h>
  33. #include <wtf/text/StringBuilder.h>
  34. using namespace WebCore;
  35. namespace WebKit {
  36. RemoteLayerTreeTransaction::LayerProperties::LayerProperties()
  37. : changedProperties(NoChange)
  38. {
  39. }
  40. void RemoteLayerTreeTransaction::LayerProperties::encode(CoreIPC::ArgumentEncoder& encoder) const
  41. {
  42. encoder << changedProperties;
  43. if (changedProperties & NameChanged)
  44. encoder << name;
  45. if (changedProperties & ChildrenChanged)
  46. encoder << children;
  47. if (changedProperties & PositionChanged)
  48. encoder << position;
  49. if (changedProperties & SizeChanged)
  50. encoder << size;
  51. }
  52. bool RemoteLayerTreeTransaction::LayerProperties::decode(CoreIPC::ArgumentDecoder& decoder, LayerProperties& result)
  53. {
  54. if (!decoder.decode(result.changedProperties))
  55. return false;
  56. if (result.changedProperties & NameChanged) {
  57. if (!decoder.decode(result.name))
  58. return false;
  59. }
  60. if (result.changedProperties & ChildrenChanged) {
  61. if (!decoder.decode(result.children))
  62. return false;
  63. for (auto layerID: result.children) {
  64. if (!layerID)
  65. return false;
  66. }
  67. }
  68. if (result.changedProperties & PositionChanged) {
  69. if (!decoder.decode(result.position))
  70. return false;
  71. }
  72. if (result.changedProperties & SizeChanged) {
  73. if (!decoder.decode(result.size))
  74. return false;
  75. }
  76. return true;
  77. }
  78. RemoteLayerTreeTransaction::RemoteLayerTreeTransaction()
  79. {
  80. }
  81. RemoteLayerTreeTransaction::~RemoteLayerTreeTransaction()
  82. {
  83. }
  84. void RemoteLayerTreeTransaction::encode(CoreIPC::ArgumentEncoder& encoder) const
  85. {
  86. encoder << m_rootLayerID;
  87. encoder << m_changedLayerProperties;
  88. encoder << m_destroyedLayerIDs;
  89. }
  90. bool RemoteLayerTreeTransaction::decode(CoreIPC::ArgumentDecoder& decoder, RemoteLayerTreeTransaction& result)
  91. {
  92. if (!decoder.decode(result.m_rootLayerID))
  93. return false;
  94. if (!result.m_rootLayerID)
  95. return false;
  96. if (!decoder.decode(result.m_changedLayerProperties))
  97. return false;
  98. if (!decoder.decode(result.m_destroyedLayerIDs))
  99. return false;
  100. for (uint64_t layerID: result.m_destroyedLayerIDs) {
  101. if (!layerID)
  102. return false;
  103. }
  104. return true;
  105. }
  106. void RemoteLayerTreeTransaction::setRootLayerID(uint64_t rootLayerID)
  107. {
  108. ASSERT_ARG(rootLayerID, rootLayerID);
  109. m_rootLayerID = rootLayerID;
  110. }
  111. void RemoteLayerTreeTransaction::layerPropertiesChanged(const RemoteGraphicsLayer* graphicsLayer, unsigned changedProperties)
  112. {
  113. LayerProperties& layerProperties = m_changedLayerProperties.add(graphicsLayer->layerID(), LayerProperties()).iterator->value;
  114. layerProperties.changedProperties |= changedProperties;
  115. if (changedProperties & NameChanged)
  116. layerProperties.name = graphicsLayer->name();
  117. if (changedProperties & ChildrenChanged) {
  118. const Vector<GraphicsLayer*>& children = graphicsLayer->children();
  119. ASSERT(layerProperties.children.isEmpty());
  120. layerProperties.children.reserveCapacity(children.size());
  121. for (size_t i = 0; i < children.size(); ++i) {
  122. RemoteGraphicsLayer* childLayer = static_cast<RemoteGraphicsLayer*>(children[i]);
  123. layerProperties.children.uncheckedAppend(childLayer->layerID());
  124. }
  125. }
  126. if (changedProperties & PositionChanged)
  127. layerProperties.position = graphicsLayer->position();
  128. if (changedProperties & SizeChanged)
  129. layerProperties.size = graphicsLayer->size();
  130. }
  131. void RemoteLayerTreeTransaction::setDestroyedLayerIDs(Vector<uint64_t> destroyedLayerIDs)
  132. {
  133. m_destroyedLayerIDs = std::move(destroyedLayerIDs);
  134. }
  135. #ifndef NDEBUG
  136. static void writeIndent(StringBuilder& builder, int indent)
  137. {
  138. for (int i = 0; i < indent; ++i)
  139. builder.append(' ');
  140. }
  141. static void dumpChangedLayers(StringBuilder& builder, const HashMap<uint64_t, RemoteLayerTreeTransaction::LayerProperties>& changedLayerProperties)
  142. {
  143. if (changedLayerProperties.isEmpty())
  144. return;
  145. writeIndent(builder, 1);
  146. builder.append("(changed-layers\n");
  147. // Dump the layer properties sorted by layer ID.
  148. Vector<uint64_t> layerIDs;
  149. copyKeysToVector(changedLayerProperties, layerIDs);
  150. std::sort(layerIDs.begin(), layerIDs.end());
  151. for (uint64_t layerID: layerIDs) {
  152. const RemoteLayerTreeTransaction::LayerProperties& layerProperties = changedLayerProperties.get(layerID);
  153. writeIndent(builder, 2);
  154. builder.append("(layer ");
  155. builder.appendNumber(layerID);
  156. if (layerProperties.changedProperties & RemoteLayerTreeTransaction::NameChanged) {
  157. builder.append('\n');
  158. writeIndent(builder, 3);
  159. builder.append("(name \"");
  160. builder.append(layerProperties.name);
  161. builder.append("\")");
  162. }
  163. if (layerProperties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged) {
  164. builder.append('\n');
  165. writeIndent(builder, 3);
  166. builder.append("(children (");
  167. for (size_t i = 0; i < layerProperties.children.size(); ++i) {
  168. if (i)
  169. builder.append(' ');
  170. builder.appendNumber(layerProperties.children[i]);
  171. }
  172. builder.append(")");
  173. }
  174. if (layerProperties.changedProperties & RemoteLayerTreeTransaction::PositionChanged) {
  175. builder.append('\n');
  176. writeIndent(builder, 3);
  177. builder.append("(position ");
  178. builder.append(String::number(layerProperties.position.x()));
  179. builder.append(" ");
  180. builder.append(String::number(layerProperties.position.y()));
  181. builder.append(')');
  182. }
  183. if (layerProperties.changedProperties & RemoteLayerTreeTransaction::SizeChanged) {
  184. builder.append('\n');
  185. writeIndent(builder, 3);
  186. builder.append("(size ");
  187. builder.append(String::number(layerProperties.size.width()));
  188. builder.append(" ");
  189. builder.append(String::number(layerProperties.size.height()));
  190. builder.append(')');
  191. }
  192. builder.append(")\n");
  193. }
  194. }
  195. void RemoteLayerTreeTransaction::dump() const
  196. {
  197. StringBuilder builder;
  198. builder.append("(\n");
  199. writeIndent(builder, 1);
  200. builder.append("(root-layer ");
  201. builder.appendNumber(m_rootLayerID);
  202. builder.append(")\n");
  203. dumpChangedLayers(builder, m_changedLayerProperties);
  204. if (!m_destroyedLayerIDs.isEmpty()) {
  205. writeIndent(builder, 1);
  206. builder.append("(destroyed-layers ");
  207. for (size_t i = 0; i < m_destroyedLayerIDs.size(); ++i) {
  208. if (i)
  209. builder.append(' ');
  210. builder.appendNumber(m_destroyedLayerIDs[i]);
  211. }
  212. builder.append(")\n");
  213. }
  214. builder.append(")\n");
  215. fprintf(stderr, "%s", builder.toString().utf8().data());
  216. }
  217. #endif // NDEBUG
  218. } // namespace WebKit