AcagiLayer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
  3. Copyright (C) 2014 Sony Computer Entertainment Inc.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include "config.h"
  18. #include "AcagiLayer.h"
  19. #include "FloatQuad.h"
  20. #include "Region.h"
  21. #include <wtf/MathExtras.h>
  22. #if USE(ACCELERATED_COMPOSITING) && USE(ACAGI)
  23. namespace WebCore {
  24. class AcagiPaintOptions {
  25. public:
  26. RefPtr<AcagiBitmapTexture> surface;
  27. float opacity;
  28. TransformationMatrix transform;
  29. IntSize offset;
  30. AcagiCompositor* compositor;
  31. AcagiPaintOptions()
  32. : opacity(1)
  33. , compositor(0)
  34. { }
  35. };
  36. const AcagiLayer* AcagiLayer::rootLayer() const
  37. {
  38. if (m_effectTarget)
  39. return m_effectTarget->rootLayer();
  40. if (m_parent)
  41. return m_parent->rootLayer();
  42. return this;
  43. }
  44. void AcagiLayer::computeTransformsRecursive()
  45. {
  46. if (m_state.size.isEmpty() && m_state.masksToBounds)
  47. return;
  48. // Compute transforms recursively on the way down to leafs.
  49. TransformationMatrix parentTransform;
  50. if (m_parent)
  51. parentTransform = m_parent->m_currentTransform.combinedForChildren();
  52. else if (m_effectTarget)
  53. parentTransform = m_effectTarget->m_currentTransform.combined();
  54. m_currentTransform.combineTransforms(parentTransform);
  55. m_state.visible = m_state.backfaceVisibility || !m_currentTransform.combined().isBackFaceVisible();
  56. if (m_parent && m_parent->m_state.preserves3D)
  57. m_centerZ = m_currentTransform.combined().mapPoint(FloatPoint3D(m_state.size.width() / 2, m_state.size.height() / 2, 0)).z();
  58. if (m_state.maskLayer)
  59. m_state.maskLayer->computeTransformsRecursive();
  60. if (m_state.replicaLayer)
  61. m_state.replicaLayer->computeTransformsRecursive();
  62. for (size_t i = 0; i < m_children.size(); ++i)
  63. m_children[i]->computeTransformsRecursive();
  64. // Reorder children if needed on the way back up.
  65. if (m_state.preserves3D)
  66. sortByZOrder(m_children);
  67. }
  68. void AcagiLayer::paint()
  69. {
  70. computeTransformsRecursive();
  71. AcagiPaintOptions options;
  72. options.compositor = m_compositor;
  73. options.compositor->bindSurface(0);
  74. paintRecursive(options);
  75. }
  76. static Color blendWithOpacity(const Color& color, float opacity)
  77. {
  78. RGBA32 rgba = color.rgb();
  79. // See Color::getRGBA() to know how to extract alpha from color.
  80. float alpha = alphaChannel(rgba) / 255.;
  81. float effectiveAlpha = alpha * opacity;
  82. return Color(colorWithOverrideAlpha(rgba, effectiveAlpha));
  83. }
  84. void AcagiLayer::computePatternTransformIfNeeded()
  85. {
  86. if (!m_patternTransformDirty)
  87. return;
  88. m_patternTransformDirty = false;
  89. m_patternTransform =
  90. TransformationMatrix::rectToRect(FloatRect(FloatPoint::zero(), m_state.contentsTileSize), m_state.contentsRect)
  91. .multiply(TransformationMatrix().translate(m_state.contentsTilePhase.x() / m_state.contentsRect.width(), m_state.contentsTilePhase.y() / m_state.contentsRect.height()));
  92. }
  93. void AcagiLayer::paintSelf(const AcagiPaintOptions& options)
  94. {
  95. if (!m_state.visible || !m_state.contentsVisible)
  96. return;
  97. // We apply the following transform to compensate for painting into a surface, and then apply the offset so that the painting fits in the target rect.
  98. TransformationMatrix transform;
  99. transform.translate(options.offset.width(), options.offset.height());
  100. transform.multiply(options.transform);
  101. transform.multiply(m_currentTransform.combined());
  102. if (m_state.solidColor.isValid() && !m_state.contentsRect.isEmpty() && m_state.solidColor.alpha()) {
  103. options.compositor->drawSolidColor(m_state.contentsRect, transform, blendWithOpacity(m_state.solidColor, options.opacity));
  104. if (m_state.showDebugBorders)
  105. options.compositor->drawBorder(m_state.debugBorderColor, m_state.debugBorderWidth, layerRect(), transform);
  106. return;
  107. }
  108. options.compositor->setWrapMode(AcagiCompositor::StretchWrap);
  109. options.compositor->setPatternTransform(TransformationMatrix());
  110. if (m_backingStore) {
  111. FloatRect targetRect = layerRect();
  112. ASSERT(!targetRect.isEmpty());
  113. m_backingStore->compositeContents(options.compositor, targetRect, transform, options.opacity);
  114. if (m_state.showDebugBorders)
  115. m_backingStore->drawBorder(options.compositor, m_state.debugBorderColor, m_state.debugBorderWidth, targetRect, transform);
  116. // Only draw repaint count for the main backing store.
  117. if (m_state.showRepaintCounter)
  118. m_backingStore->drawRepaintCounter(options.compositor, m_state.repaintCount, m_state.debugBorderColor, targetRect, transform);
  119. }
  120. if (!m_contentsLayer)
  121. return;
  122. if (!m_state.contentsTileSize.isEmpty()) {
  123. computePatternTransformIfNeeded();
  124. options.compositor->setWrapMode(AcagiCompositor::RepeatWrap);
  125. options.compositor->setPatternTransform(m_patternTransform);
  126. }
  127. ASSERT(!layerRect().isEmpty());
  128. m_contentsLayer->compositeContents(options.compositor, m_state.contentsRect, transform, options.opacity);
  129. if (m_state.showDebugBorders)
  130. m_contentsLayer->drawBorder(options.compositor, m_state.debugBorderColor, m_state.debugBorderWidth, m_state.contentsRect, transform);
  131. }
  132. int AcagiLayer::compareGraphicsLayersZValue(const void* a, const void* b)
  133. {
  134. AcagiLayer* const* layerA = static_cast<AcagiLayer* const*>(a);
  135. AcagiLayer* const* layerB = static_cast<AcagiLayer* const*>(b);
  136. return int(((*layerA)->m_centerZ - (*layerB)->m_centerZ) * 1000);
  137. }
  138. void AcagiLayer::sortByZOrder(Vector<AcagiLayer* >& array)
  139. {
  140. qsort(array.data(), array.size(), sizeof(AcagiLayer*), compareGraphicsLayersZValue);
  141. }
  142. void AcagiLayer::paintSelfAndChildren(const AcagiPaintOptions& options)
  143. {
  144. paintSelf(options);
  145. if (m_children.isEmpty())
  146. return;
  147. bool shouldClip = m_state.masksToBounds && !m_state.preserves3D;
  148. if (shouldClip) {
  149. TransformationMatrix clipTransform;
  150. clipTransform.translate(options.offset.width(), options.offset.height());
  151. clipTransform.multiply(options.transform);
  152. clipTransform.multiply(m_currentTransform.combined());
  153. options.compositor->beginClip(clipTransform, layerRect());
  154. }
  155. for (size_t i = 0; i < m_children.size(); ++i)
  156. m_children[i]->paintRecursive(options);
  157. if (shouldClip)
  158. options.compositor->endClip();
  159. }
  160. bool AcagiLayer::shouldBlend() const
  161. {
  162. if (m_state.preserves3D)
  163. return false;
  164. return m_currentOpacity < 1
  165. || hasFilters()
  166. || m_state.maskLayer
  167. || (m_state.replicaLayer && m_state.replicaLayer->m_state.maskLayer);
  168. }
  169. bool AcagiLayer::isVisible() const
  170. {
  171. if (m_state.size.isEmpty() && (m_state.masksToBounds || m_state.maskLayer || m_children.isEmpty()))
  172. return false;
  173. if (!m_state.visible && m_children.isEmpty())
  174. return false;
  175. if (!m_state.contentsVisible && m_children.isEmpty())
  176. return false;
  177. if (m_currentOpacity < 0.01)
  178. return false;
  179. return true;
  180. }
  181. void AcagiLayer::paintSelfAndChildrenWithReplica(const AcagiPaintOptions& options)
  182. {
  183. if (m_state.replicaLayer) {
  184. AcagiPaintOptions replicaOptions(options);
  185. replicaOptions.transform
  186. .multiply(m_state.replicaLayer->m_currentTransform.combined())
  187. .multiply(m_currentTransform.combined().inverse());
  188. paintSelfAndChildren(replicaOptions);
  189. }
  190. paintSelfAndChildren(options);
  191. }
  192. void AcagiLayer::setAnimatedTransform(const TransformationMatrix& matrix)
  193. {
  194. m_currentTransform.setLocalTransform(matrix);
  195. }
  196. void AcagiLayer::setAnimatedOpacity(float opacity)
  197. {
  198. m_currentOpacity = opacity;
  199. }
  200. TransformationMatrix AcagiLayer::replicaTransform()
  201. {
  202. return TransformationMatrix(m_state.replicaLayer->m_currentTransform.combined()).multiply(m_currentTransform.combined().inverse());
  203. }
  204. static void resolveOverlaps(Region newRegion, Region& overlapRegion, Region& nonOverlapRegion)
  205. {
  206. Region newOverlapRegion(newRegion);
  207. newOverlapRegion.intersect(nonOverlapRegion);
  208. nonOverlapRegion.subtract(newOverlapRegion);
  209. overlapRegion.unite(newOverlapRegion);
  210. newRegion.subtract(overlapRegion);
  211. nonOverlapRegion.unite(newRegion);
  212. }
  213. void AcagiLayer::computeOverlapRegions(Region& overlapRegion, Region& nonOverlapRegion, ResolveSelfOverlapMode mode)
  214. {
  215. if (!m_state.visible || !m_state.contentsVisible)
  216. return;
  217. FloatRect boundingRect;
  218. if (m_backingStore || m_state.masksToBounds || m_state.maskLayer || hasFilters())
  219. boundingRect = layerRect();
  220. else if (m_contentsLayer || m_state.solidColor.alpha())
  221. boundingRect = m_state.contentsRect;
  222. TransformationMatrix replicaMatrix;
  223. if (m_state.replicaLayer) {
  224. replicaMatrix = replicaTransform();
  225. boundingRect.unite(replicaMatrix.mapRect(boundingRect));
  226. }
  227. boundingRect = m_currentTransform.combined().mapRect(boundingRect);
  228. // Count all masks and filters as overlap layers.
  229. if (hasFilters() || m_state.maskLayer || (m_state.replicaLayer && m_state.replicaLayer->m_state.maskLayer)) {
  230. Region newOverlapRegion(enclosingIntRect(boundingRect));
  231. nonOverlapRegion.subtract(newOverlapRegion);
  232. overlapRegion.unite(newOverlapRegion);
  233. return;
  234. }
  235. Region newOverlapRegion;
  236. Region newNonOverlapRegion(enclosingIntRect(boundingRect));
  237. if (!m_state.masksToBounds) {
  238. for (size_t i = 0; i < m_children.size(); ++i) {
  239. AcagiLayer* child = m_children[i];
  240. child->computeOverlapRegions(newOverlapRegion, newNonOverlapRegion, ResolveSelfOverlapIfNeeded);
  241. }
  242. }
  243. if (m_state.replicaLayer) {
  244. newOverlapRegion.unite(replicaMatrix.mapRect(newOverlapRegion.bounds()));
  245. Region replicaRegion(replicaMatrix.mapRect(newNonOverlapRegion.bounds()));
  246. resolveOverlaps(replicaRegion, newOverlapRegion, newNonOverlapRegion);
  247. }
  248. if ((mode != ResolveSelfOverlapAlways) && shouldBlend()) {
  249. newNonOverlapRegion.unite(newOverlapRegion);
  250. newOverlapRegion = Region();
  251. }
  252. overlapRegion.unite(newOverlapRegion);
  253. resolveOverlaps(newNonOverlapRegion, overlapRegion, nonOverlapRegion);
  254. }
  255. void AcagiLayer::paintUsingOverlapRegions(const AcagiPaintOptions& options)
  256. {
  257. Region overlapRegion;
  258. Region nonOverlapRegion;
  259. computeOverlapRegions(overlapRegion, nonOverlapRegion, ResolveSelfOverlapAlways);
  260. if (overlapRegion.isEmpty()) {
  261. paintSelfAndChildrenWithReplica(options);
  262. return;
  263. }
  264. // Having both overlap and non-overlap regions carries some overhead. Avoid it if the overlap area
  265. // is big anyway.
  266. if (overlapRegion.bounds().size().area() > nonOverlapRegion.bounds().size().area()) {
  267. overlapRegion.unite(nonOverlapRegion);
  268. nonOverlapRegion = Region();
  269. }
  270. nonOverlapRegion.translate(options.offset);
  271. Vector<IntRect> rects = nonOverlapRegion.rects();
  272. for (size_t i = 0; i < rects.size(); ++i) {
  273. IntRect rect = rects[i];
  274. if (!rect.intersects(options.compositor->clipBounds()))
  275. continue;
  276. options.compositor->beginClip(TransformationMatrix(), rects[i]);
  277. paintSelfAndChildrenWithReplica(options);
  278. options.compositor->endClip();
  279. }
  280. rects = overlapRegion.rects();
  281. static const size_t OverlapRegionConsolidationThreshold = 4;
  282. if (nonOverlapRegion.isEmpty() && rects.size() > OverlapRegionConsolidationThreshold) {
  283. rects.clear();
  284. rects.append(overlapRegion.bounds());
  285. }
  286. IntSize maxTextureSize = options.compositor->maxTextureSize();
  287. IntRect adjustedClipBounds(options.compositor->clipBounds());
  288. adjustedClipBounds.move(-options.offset);
  289. for (size_t i = 0; i < rects.size(); ++i) {
  290. IntRect rect = rects[i];
  291. for (int x = rect.x(); x < rect.maxX(); x += maxTextureSize.width()) {
  292. for (int y = rect.y(); y < rect.maxY(); y += maxTextureSize.height()) {
  293. IntRect tileRect(IntPoint(x, y), maxTextureSize);
  294. tileRect.intersect(rect);
  295. if (!tileRect.intersects(adjustedClipBounds))
  296. continue;
  297. paintWithIntermediateSurface(options, tileRect);
  298. }
  299. }
  300. }
  301. }
  302. void AcagiLayer::applyMask(const AcagiPaintOptions& options)
  303. {
  304. options.compositor->setMaskMode(true);
  305. paintSelf(options);
  306. options.compositor->setMaskMode(false);
  307. }
  308. PassRefPtr<AcagiBitmapTexture> AcagiLayer::paintIntoSurface(const AcagiPaintOptions& options, const IntSize& size)
  309. {
  310. RefPtr<AcagiBitmapTexture> surface = options.compositor->acquireTextureFromPool(size);
  311. AcagiPaintOptions paintOptions(options);
  312. paintOptions.surface = surface;
  313. options.compositor->bindSurface(surface.get());
  314. paintSelfAndChildren(paintOptions);
  315. if (m_state.maskLayer)
  316. m_state.maskLayer->applyMask(options);
  317. options.compositor->bindSurface(surface.get());
  318. return surface;
  319. }
  320. static void commitSurface(const AcagiPaintOptions& options, PassRefPtr<AcagiBitmapTexture> surface, const IntRect& rect, float opacity)
  321. {
  322. options.compositor->bindSurface(options.surface.get());
  323. TransformationMatrix targetTransform;
  324. targetTransform.translate(options.offset.width(), options.offset.height());
  325. targetTransform.multiply(options.transform);
  326. options.compositor->drawTexture(*surface.get(), rect, targetTransform, opacity);
  327. }
  328. void AcagiLayer::paintWithIntermediateSurface(const AcagiPaintOptions& options, const IntRect& rect)
  329. {
  330. RefPtr<AcagiBitmapTexture> replicaSurface;
  331. RefPtr<AcagiBitmapTexture> mainSurface;
  332. AcagiPaintOptions paintOptions(options);
  333. paintOptions.offset = -IntSize(rect.x(), rect.y());
  334. paintOptions.opacity = 1;
  335. paintOptions.transform = TransformationMatrix();
  336. if (m_state.replicaLayer) {
  337. paintOptions.transform = replicaTransform();
  338. replicaSurface = paintIntoSurface(paintOptions, rect.size());
  339. paintOptions.transform = TransformationMatrix();
  340. if (m_state.replicaLayer->m_state.maskLayer)
  341. m_state.replicaLayer->m_state.maskLayer->applyMask(paintOptions);
  342. }
  343. if (replicaSurface && options.opacity == 1) {
  344. commitSurface(options, replicaSurface, rect, 1);
  345. replicaSurface.clear();
  346. }
  347. mainSurface = paintIntoSurface(paintOptions, rect.size());
  348. if (replicaSurface) {
  349. options.compositor->bindSurface(replicaSurface.get());
  350. options.compositor->drawTexture(*mainSurface.get(), FloatRect(FloatPoint::zero(), rect.size()));
  351. mainSurface = replicaSurface;
  352. }
  353. commitSurface(options, mainSurface, rect, options.opacity);
  354. }
  355. void AcagiLayer::paintRecursive(const AcagiPaintOptions& options)
  356. {
  357. if (!isVisible())
  358. return;
  359. AcagiPaintOptions paintOptions(options);
  360. paintOptions.opacity *= m_currentOpacity;
  361. if (!shouldBlend()) {
  362. paintSelfAndChildrenWithReplica(paintOptions);
  363. return;
  364. }
  365. paintUsingOverlapRegions(paintOptions);
  366. }
  367. AcagiLayer::~AcagiLayer()
  368. {
  369. for (int i = m_children.size() - 1; i >= 0; --i)
  370. m_children[i]->m_parent = 0;
  371. if (m_parent)
  372. m_parent->m_children.remove(m_parent->m_children.find(this));
  373. }
  374. AcagiCompositor* AcagiLayer::compositor() const
  375. {
  376. return rootLayer()->m_compositor;
  377. }
  378. void AcagiLayer::setChildren(const Vector<AcagiLayer*>& newChildren)
  379. {
  380. removeAllChildren();
  381. for (size_t i = 0; i < newChildren.size(); ++i)
  382. addChild(newChildren[i]);
  383. }
  384. void AcagiLayer::addChild(AcagiLayer* childLayer)
  385. {
  386. ASSERT(childLayer != this);
  387. if (childLayer->m_parent)
  388. childLayer->removeFromParent();
  389. childLayer->m_parent = this;
  390. m_children.append(childLayer);
  391. }
  392. void AcagiLayer::removeFromParent()
  393. {
  394. if (m_parent) {
  395. unsigned i;
  396. for (i = 0; i < m_parent->m_children.size(); i++) {
  397. if (this == m_parent->m_children[i]) {
  398. m_parent->m_children.remove(i);
  399. break;
  400. }
  401. }
  402. m_parent = 0;
  403. }
  404. }
  405. void AcagiLayer::removeAllChildren()
  406. {
  407. while (m_children.size()) {
  408. AcagiLayer* curLayer = m_children[0];
  409. ASSERT(curLayer->m_parent);
  410. curLayer->removeFromParent();
  411. }
  412. }
  413. void AcagiLayer::setMaskLayer(AcagiLayer* maskLayer)
  414. {
  415. if (maskLayer)
  416. maskLayer->m_effectTarget = this;
  417. m_state.maskLayer = maskLayer;
  418. }
  419. void AcagiLayer::setReplicaLayer(AcagiLayer* replicaLayer)
  420. {
  421. if (replicaLayer)
  422. replicaLayer->m_effectTarget = this;
  423. m_state.replicaLayer = replicaLayer;
  424. }
  425. void AcagiLayer::setPosition(const FloatPoint& position)
  426. {
  427. m_state.pos = position;
  428. m_currentTransform.setPosition(adjustedPosition());
  429. }
  430. void AcagiLayer::setSize(const FloatSize& size)
  431. {
  432. m_state.size = size;
  433. m_currentTransform.setSize(size);
  434. }
  435. void AcagiLayer::setAnchorPoint(const FloatPoint3D& anchorPoint)
  436. {
  437. m_state.anchorPoint = anchorPoint;
  438. m_currentTransform.setAnchorPoint(anchorPoint);
  439. }
  440. void AcagiLayer::setPreserves3D(bool preserves3D)
  441. {
  442. m_state.preserves3D = preserves3D;
  443. m_currentTransform.setFlattening(!preserves3D);
  444. }
  445. void AcagiLayer::setTransform(const TransformationMatrix& transform)
  446. {
  447. m_state.transform = transform;
  448. m_currentTransform.setLocalTransform(transform);
  449. }
  450. void AcagiLayer::setChildrenTransform(const TransformationMatrix& childrenTransform)
  451. {
  452. m_state.childrenTransform = childrenTransform;
  453. m_currentTransform.setChildrenTransform(childrenTransform);
  454. }
  455. void AcagiLayer::setContentsRect(const IntRect& contentsRect)
  456. {
  457. if (contentsRect == m_state.contentsRect)
  458. return;
  459. m_state.contentsRect = contentsRect;
  460. m_patternTransformDirty = true;
  461. }
  462. void AcagiLayer::setContentsTileSize(const IntSize& size)
  463. {
  464. if (size == m_state.contentsTileSize)
  465. return;
  466. m_state.contentsTileSize = size;
  467. m_patternTransformDirty = true;
  468. }
  469. void AcagiLayer::setContentsTilePhase(const IntPoint& phase)
  470. {
  471. if (phase == m_state.contentsTilePhase)
  472. return;
  473. m_state.contentsTilePhase = phase;
  474. m_patternTransformDirty = true;
  475. }
  476. void AcagiLayer::setMasksToBounds(bool masksToBounds)
  477. {
  478. m_state.masksToBounds = masksToBounds;
  479. }
  480. void AcagiLayer::setDrawsContent(bool drawsContent)
  481. {
  482. m_state.drawsContent = drawsContent;
  483. }
  484. void AcagiLayer::setContentsVisible(bool contentsVisible)
  485. {
  486. m_state.contentsVisible = contentsVisible;
  487. }
  488. void AcagiLayer::setContentsOpaque(bool contentsOpaque)
  489. {
  490. m_state.contentsOpaque = contentsOpaque;
  491. }
  492. void AcagiLayer::setBackfaceVisibility(bool backfaceVisibility)
  493. {
  494. m_state.backfaceVisibility = backfaceVisibility;
  495. }
  496. void AcagiLayer::setOpacity(float opacity)
  497. {
  498. m_state.opacity = opacity;
  499. }
  500. void AcagiLayer::setSolidColor(const Color& color)
  501. {
  502. m_state.solidColor = color;
  503. }
  504. void AcagiLayer::setDebugVisuals(bool showDebugBorders, const Color& debugBorderColor, float debugBorderWidth, bool showRepaintCounter)
  505. {
  506. m_state.showDebugBorders = showDebugBorders;
  507. m_state.debugBorderColor = debugBorderColor;
  508. m_state.debugBorderWidth = debugBorderWidth;
  509. m_state.showRepaintCounter = showRepaintCounter;
  510. }
  511. void AcagiLayer::setRepaintCount(int repaintCount)
  512. {
  513. m_state.repaintCount = repaintCount;
  514. }
  515. void AcagiLayer::setContentsLayer(PlatformLayerAcagi* platformLayer)
  516. {
  517. m_contentsLayer = platformLayer;
  518. }
  519. void AcagiLayer::setAnimations(const GraphicsLayerAnimations& animations)
  520. {
  521. m_animations = animations;
  522. }
  523. void AcagiLayer::setFixedToViewport(bool fixedToViewport)
  524. {
  525. m_fixedToViewport = fixedToViewport;
  526. }
  527. void AcagiLayer::setBackingStore(PassRefPtr<AcagiBackingStore> backingStore)
  528. {
  529. m_backingStore = backingStore;
  530. }
  531. bool AcagiLayer::descendantsOrSelfHaveRunningAnimations() const
  532. {
  533. if (m_animations.hasRunningAnimations())
  534. return true;
  535. for (size_t i = 0; i < m_children.size(); ++i) {
  536. if (m_children[i]->descendantsOrSelfHaveRunningAnimations())
  537. return true;
  538. }
  539. return false;
  540. }
  541. void AcagiLayer::applyAnimationsRecursively()
  542. {
  543. syncAnimations();
  544. for (size_t i = 0; i < m_children.size(); ++i)
  545. m_children[i]->applyAnimationsRecursively();
  546. }
  547. void AcagiLayer::syncAnimations()
  548. {
  549. m_animations.apply(this);
  550. if (!m_animations.hasActiveAnimationsOfType(AnimatedPropertyWebkitTransform))
  551. m_currentTransform.setLocalTransform(m_state.transform);
  552. if (!m_animations.hasActiveAnimationsOfType(AnimatedPropertyOpacity))
  553. m_currentOpacity = m_state.opacity;
  554. }
  555. bool AcagiLayer::isAncestorFixedToViewport() const
  556. {
  557. for (AcagiLayer* parent = m_parent; parent; parent = parent->m_parent) {
  558. if (parent->m_fixedToViewport)
  559. return true;
  560. }
  561. return false;
  562. }
  563. void AcagiLayer::setScrollPositionDeltaIfNeeded(const FloatSize& delta)
  564. {
  565. // delta is the difference between the scroll offset in the ui process and the scroll offset
  566. // in the web process. We add this delta to the position of fixed layers, to make
  567. // sure that they do not move while scrolling. We need to reset this delta to fixed layers
  568. // that have an ancestor which is also a fixed layer, because the delta will be added to the ancestor.
  569. if (isAncestorFixedToViewport())
  570. m_scrollPositionDelta = FloatSize();
  571. else
  572. m_scrollPositionDelta = delta;
  573. m_currentTransform.setPosition(adjustedPosition());
  574. }
  575. template<class HitTestCondition> AcagiLayer* AcagiLayer::hitTest(const FloatPoint& point, HitTestCondition condition)
  576. {
  577. if (!m_state.visible || !m_state.contentsVisible)
  578. return 0;
  579. AcagiLayer* result = 0;
  580. for (int i = m_children.size() - 1; !result && i >= 0; --i)
  581. result = m_children[i]->hitTest(point, condition);
  582. if (result)
  583. return result;
  584. return condition(this, point) ? this : 0;
  585. }
  586. bool AcagiLayer::scrollableLayerHitTestCondition(AcagiLayer* layer, const FloatPoint& point)
  587. {
  588. // scrolling layer's m_parent->m_parent, the parent of the scrolling layes, is the one that defines the
  589. // rectangle to be used for hit testing.
  590. if (!layer->isScrollable() || !layer->m_parent || !layer->m_parent->m_parent)
  591. return false;
  592. AcagiLayer* parentLayer = layer->m_parent->m_parent;
  593. FloatRect rect = parentLayer->layerRect();
  594. return parentLayer->m_currentTransform.combined().mapQuad(rect).containsPoint(point);
  595. }
  596. AcagiLayer* AcagiLayer::findScrollableContentsLayerAt(const FloatPoint& point)
  597. {
  598. return hitTest(point, &AcagiLayer::scrollableLayerHitTestCondition);
  599. }
  600. FloatSize AcagiLayer::mapScrollOffset(const FloatSize& offset)
  601. {
  602. double zeroX, zeroY, offsetX, offsetY;
  603. TransformationMatrix transform = m_currentTransform.combined().inverse();
  604. transform.map(0, 0, zeroX, zeroY);
  605. transform.map(offset.width(), offset.height(), offsetX, offsetY);
  606. return FloatSize(offsetX - zeroX, offsetY - zeroY);
  607. }
  608. void AcagiLayer::commitScrollOffset(const FloatSize& offset)
  609. {
  610. FloatSize fullOffset = m_accumulatedScrollOffsetFractionalPart + offset;
  611. int intWidth = round(fullOffset.width());
  612. int intHeight = round(fullOffset.height());
  613. // m_accumulatedScrollOffsetFractionalPart holds the fractional part of the user scroll offset that
  614. // has not yet been synced with the web process because the web process expects an IntSize.
  615. m_accumulatedScrollOffsetFractionalPart = FloatSize(fullOffset.width() - intWidth, fullOffset.height() - intHeight);
  616. m_scrollClient->commitScrollOffset(m_id, IntSize(intWidth, intHeight));
  617. }
  618. void AcagiLayer::scrollBy(const FloatSize& offset)
  619. {
  620. if (!isScrollable() || !m_scrollClient || offset.isZero())
  621. return;
  622. FloatSize scrollOffset = mapScrollOffset(offset);
  623. m_userScrollOffset += scrollOffset;
  624. m_currentTransform.setPosition(adjustedPosition());
  625. commitScrollOffset(scrollOffset);
  626. }
  627. void AcagiLayer::didCommitScrollOffset(const IntSize& offset)
  628. {
  629. m_userScrollOffset = FloatSize(m_userScrollOffset.width() - offset.width(), m_userScrollOffset.height() - offset.height());
  630. m_currentTransform.setPosition(adjustedPosition());
  631. }
  632. }
  633. #endif