[chromium] Layers with animating transforms should prepaint even if they are not...
[WebKit-https.git] / Source / WebCore / platform / graphics / chromium / ContentLayerChromium.cpp
1 /*
2  * Copyright (C) 2010 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #if USE(ACCELERATED_COMPOSITING)
34
35 #include "ContentLayerChromium.h"
36
37 #include "BitmapCanvasLayerTextureUpdater.h"
38 #include "BitmapSkPictureCanvasLayerTextureUpdater.h"
39 #include "FrameBufferSkPictureCanvasLayerTextureUpdater.h"
40 #include "LayerPainterChromium.h"
41 #include "LayerRendererChromium.h"
42 #include "PlatformSupport.h"
43 #include "cc/CCLayerTreeHost.h"
44 #include <wtf/CurrentTime.h>
45
46 namespace WebCore {
47
48 class ContentLayerPainter : public LayerPainterChromium {
49     WTF_MAKE_NONCOPYABLE(ContentLayerPainter);
50 public:
51     static PassOwnPtr<ContentLayerPainter> create(ContentLayerDelegate* delegate)
52     {
53         return adoptPtr(new ContentLayerPainter(delegate));
54     }
55
56     virtual void paint(GraphicsContext& context, const IntRect& contentRect)
57     {
58         double paintStart = currentTime();
59         context.clearRect(contentRect);
60         context.clip(contentRect);
61         m_delegate->paintContents(context, contentRect);
62         double paintEnd = currentTime();
63         double pixelsPerSec = (contentRect.width() * contentRect.height()) / (paintEnd - paintStart);
64         PlatformSupport::histogramCustomCounts("Renderer4.AccelContentPaintDurationMS", (paintEnd - paintStart) * 1000, 0, 120, 30);
65         PlatformSupport::histogramCustomCounts("Renderer4.AccelContentPaintMegapixPerSecond", pixelsPerSec / 1000000, 10, 210, 30);
66     }
67 private:
68     explicit ContentLayerPainter(ContentLayerDelegate* delegate)
69         : m_delegate(delegate)
70     {
71     }
72
73     ContentLayerDelegate* m_delegate;
74 };
75
76 PassRefPtr<ContentLayerChromium> ContentLayerChromium::create(ContentLayerDelegate* delegate)
77 {
78     return adoptRef(new ContentLayerChromium(delegate));
79 }
80
81 ContentLayerChromium::ContentLayerChromium(ContentLayerDelegate* delegate)
82     : TiledLayerChromium()
83     , m_delegate(delegate)
84 {
85 }
86
87 ContentLayerChromium::~ContentLayerChromium()
88 {
89 }
90
91 bool ContentLayerChromium::drawsContent() const
92 {
93     return TiledLayerChromium::drawsContent() && m_delegate;
94 }
95
96 void ContentLayerChromium::paintContentsIfDirty(const CCOcclusionTracker* occlusion)
97 {
98     updateTileSizeAndTilingOption();
99     createTextureUpdaterIfNeeded();
100
101     IntRect layerRect;
102
103     // Always call prepareToUpdate() but with an empty layer rectangle when
104     // layer doesn't draw contents.
105     if (drawsContent())
106         layerRect = visibleLayerRect();
107
108     prepareToUpdate(layerRect, occlusion);
109     m_needsDisplay = false;
110 }
111
112 void ContentLayerChromium::idlePaintContentsIfDirty(const CCOcclusionTracker* occlusion)
113 {
114     if (!drawsContent())
115         return;
116
117     const IntRect layerRect = visibleLayerRect();
118     prepareToUpdateIdle(layerRect, occlusion);
119     if (needsIdlePaint(layerRect))
120         setNeedsCommit();
121 }
122
123 void ContentLayerChromium::createTextureUpdaterIfNeeded()
124 {
125     if (m_textureUpdater)
126         return;
127     if (layerTreeHost()->settings().acceleratePainting)
128         m_textureUpdater = FrameBufferSkPictureCanvasLayerTextureUpdater::create(ContentLayerPainter::create(m_delegate));
129     else if (layerTreeHost()->settings().perTilePainting)
130         m_textureUpdater = BitmapSkPictureCanvasLayerTextureUpdater::create(ContentLayerPainter::create(m_delegate), layerTreeHost()->layerRendererCapabilities().usingMapSub);
131     else
132         m_textureUpdater = BitmapCanvasLayerTextureUpdater::create(ContentLayerPainter::create(m_delegate), layerTreeHost()->layerRendererCapabilities().usingMapSub);
133     m_textureUpdater->setOpaque(opaque());
134
135     GC3Denum textureFormat = layerTreeHost()->layerRendererCapabilities().bestTextureFormat;
136     setTextureFormat(textureFormat);
137     setSampledTexelFormat(textureUpdater()->sampledTexelFormat(textureFormat));
138 }
139
140 void ContentLayerChromium::setOpaque(bool opaque)
141 {
142     LayerChromium::setOpaque(opaque);
143     if (m_textureUpdater)
144         m_textureUpdater->setOpaque(opaque);
145 }
146
147 }
148 #endif // USE(ACCELERATED_COMPOSITING)