initial import
[vuplus_webkit] / 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 "LayerPainterChromium.h"
38 #include "LayerRendererChromium.h"
39 #include "LayerTextureUpdaterCanvas.h"
40 #include "PlatformSupport.h"
41 #include "cc/CCLayerTreeHost.h"
42 #include <wtf/CurrentTime.h>
43
44 namespace WebCore {
45
46 class ContentLayerPainter : public LayerPainterChromium {
47     WTF_MAKE_NONCOPYABLE(ContentLayerPainter);
48 public:
49     static PassOwnPtr<ContentLayerPainter> create(CCLayerDelegate* delegate)
50     {
51         return adoptPtr(new ContentLayerPainter(delegate));
52     }
53
54     virtual void paint(GraphicsContext& context, const IntRect& contentRect)
55     {
56         double paintStart = currentTime();
57         context.clearRect(contentRect);
58         context.clip(contentRect);
59         m_delegate->paintContents(context, contentRect);
60         double paintEnd = currentTime();
61         double pixelsPerSec = (contentRect.width() * contentRect.height()) / (paintEnd - paintStart);
62         PlatformSupport::histogramCustomCounts("Renderer4.AccelContentPaintDurationMS", (paintEnd - paintStart) * 1000, 0, 120, 30);
63         PlatformSupport::histogramCustomCounts("Renderer4.AccelContentPaintMegapixPerSecond", pixelsPerSec / 1000000, 10, 210, 30);
64     }
65 private:
66     explicit ContentLayerPainter(CCLayerDelegate* delegate)
67         : m_delegate(delegate)
68     {
69     }
70
71     CCLayerDelegate* m_delegate;
72 };
73
74 PassRefPtr<ContentLayerChromium> ContentLayerChromium::create(CCLayerDelegate* delegate)
75 {
76     return adoptRef(new ContentLayerChromium(delegate));
77 }
78
79 ContentLayerChromium::ContentLayerChromium(CCLayerDelegate* delegate)
80     : TiledLayerChromium(delegate)
81 {
82 }
83
84 ContentLayerChromium::~ContentLayerChromium()
85 {
86     cleanupResources();
87 }
88
89 void ContentLayerChromium::cleanupResources()
90 {
91     m_textureUpdater.clear();
92     TiledLayerChromium::cleanupResources();
93 }
94
95 void ContentLayerChromium::paintContentsIfDirty()
96 {
97     ASSERT(drawsContent());
98
99     updateTileSizeAndTilingOption();
100
101     const IntRect& layerRect = visibleLayerRect();
102     if (layerRect.isEmpty())
103         return;
104
105     IntRect dirty = enclosingIntRect(m_dirtyRect);
106     dirty.intersect(IntRect(IntPoint(), contentBounds()));
107     invalidateRect(dirty);
108
109     if (!drawsContent())
110         return;
111
112     prepareToUpdate(layerRect);
113     m_dirtyRect = FloatRect();
114 }
115
116 bool ContentLayerChromium::drawsContent() const
117 {
118     return m_delegate && m_delegate->drawsContent() && TiledLayerChromium::drawsContent();
119 }
120
121 void ContentLayerChromium::createTextureUpdater(const CCLayerTreeHost* host)
122 {
123 #if USE(SKIA)
124     // Note that host->skiaContext() will crash if called while in threaded
125     // mode. This thus depends on CCLayerTreeHost::initialize turning off
126     // acceleratePainting to prevent this from crashing.
127     if (host->settings().acceleratePainting) {
128         m_textureUpdater = LayerTextureUpdaterSkPicture::create(ContentLayerPainter::create(m_delegate));
129         return;
130     }
131 #endif // SKIA
132
133     m_textureUpdater = LayerTextureUpdaterBitmap::create(ContentLayerPainter::create(m_delegate), host->layerRendererCapabilities().usingMapSub);
134 }
135
136 }
137 #endif // USE(ACCELERATED_COMPOSITING)