Unreviewed. Build fix for !ENABLE(JIT) after r119441.
[WebKit.git] / Source / WebKit / chromium / tests / Canvas2DLayerChromiumTest.cpp
1 /*
2  * Copyright (C) 2011 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
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'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26
27 #include "Canvas2DLayerChromium.h"
28
29 #include "CCSchedulerTestCommon.h"
30 #include "FakeCCLayerTreeHostClient.h"
31 #include "FakeWebGraphicsContext3D.h"
32 #include "GraphicsContext3DPrivate.h"
33 #include "Region.h"
34 #include "TextureCopier.h"
35 #include "TextureManager.h"
36 #include "WebCompositor.h"
37 #include "WebKit.h"
38 #include "cc/CCSingleThreadProxy.h"
39 #include "cc/CCTextureLayerImpl.h"
40 #include "cc/CCTextureUpdater.h"
41 #include "platform/WebKitPlatformSupport.h"
42 #include "platform/WebThread.h"
43
44 #include <gmock/gmock.h>
45 #include <gtest/gtest.h>
46 #include <wtf/RefPtr.h>
47
48 using namespace WebCore;
49 using namespace WebKit;
50 using namespace WebKitTests;
51 using testing::InSequence;
52 using testing::Return;
53 using testing::Test;
54
55 namespace {
56
57 class FakeCCLayerTreeHost : public CCLayerTreeHost {
58 public:
59     static PassOwnPtr<FakeCCLayerTreeHost> create()
60     {
61         OwnPtr<FakeCCLayerTreeHost> host(adoptPtr(new FakeCCLayerTreeHost));
62         host->initialize();
63         return host.release();
64     }
65
66 private:
67     FakeCCLayerTreeHost()
68         : CCLayerTreeHost(&m_client, CCSettings())
69     {
70     }
71
72     FakeCCLayerTreeHostClient m_client;
73 };
74
75 class MockCanvasContext : public FakeWebGraphicsContext3D {
76 public:
77     MOCK_METHOD0(flush, void(void));
78 };
79
80 class MockTextureAllocator : public TextureAllocator {
81 public:
82     MOCK_METHOD2(createTexture, unsigned(const IntSize&, GC3Denum));
83     MOCK_METHOD3(deleteTexture, void(unsigned, const IntSize&, GC3Denum));
84 };
85
86 class MockTextureCopier : public TextureCopier {
87 public:
88     MOCK_METHOD4(copyTexture, void(CCGraphicsContext*, unsigned, unsigned, const IntSize&));
89 };
90
91 class MockTextureUploader : public TextureUploader {
92 public:
93     MOCK_METHOD0(isBusy, bool());
94     MOCK_METHOD0(beginUploads, void());
95     MOCK_METHOD0(endUploads, void());
96     MOCK_METHOD5(uploadTexture, void(CCGraphicsContext*, LayerTextureUpdater::Texture*, TextureAllocator*, const IntRect, const IntRect));
97 };
98
99 } // namespace
100
101 class Canvas2DLayerChromiumTest : public Test {
102 protected:
103     void fullLifecycleTest(bool threaded, bool deferred)
104     {
105         GraphicsContext3D::Attributes attrs;
106
107         RefPtr<GraphicsContext3D> mainContext = GraphicsContext3DPrivate::createGraphicsContextFromWebContext(adoptPtr(new MockCanvasContext()), GraphicsContext3D::RenderDirectlyToHostWindow);
108         RefPtr<CCGraphicsContext> ccMainContext = CCGraphicsContext::create3D(mainContext);
109         RefPtr<GraphicsContext3D> implContext = GraphicsContext3DPrivate::createGraphicsContextFromWebContext(adoptPtr(new MockCanvasContext()), GraphicsContext3D::RenderDirectlyToHostWindow);
110         RefPtr<CCGraphicsContext> ccImplContext = CCGraphicsContext::create3D(implContext);
111
112         MockCanvasContext& mainMock = *static_cast<MockCanvasContext*>(GraphicsContext3DPrivate::extractWebGraphicsContext3D(mainContext.get()));
113         MockCanvasContext& implMock = *static_cast<MockCanvasContext*>(GraphicsContext3DPrivate::extractWebGraphicsContext3D(implContext.get()));
114
115         MockTextureAllocator allocatorMock;
116         MockTextureCopier copierMock;
117         MockTextureUploader uploaderMock;
118         CCTextureUpdater updater;
119
120         const IntSize size(300, 150);
121
122         OwnPtr<WebThread> thread;
123         if (threaded)
124             thread = adoptPtr(WebKit::Platform::current()->createThread("Canvas2DLayerChromiumTest"));
125         WebCompositor::initialize(thread.get());
126
127         OwnPtr<FakeCCLayerTreeHost> layerTreeHost(FakeCCLayerTreeHost::create());
128         // Force an update, so that we get a valid TextureManager.
129         layerTreeHost->updateLayers(updater);
130
131         const WebGLId backTextureId = 1;
132         const WebGLId frontTextureId = 2;
133         {
134             InSequence sequence;
135
136             // Paint canvas contents on the main thread.
137             EXPECT_CALL(mainMock, flush());
138
139             // Note that the canvas backing texture is doublebuffered only when using the threaded
140             // compositor and not using deferred canvas rendering
141             if (threaded && !deferred) {
142                 // Create texture and do the copy (on the impl thread).
143                 EXPECT_CALL(allocatorMock, createTexture(size, GraphicsContext3D::RGBA))
144                     .WillOnce(Return(frontTextureId));
145                 EXPECT_CALL(copierMock, copyTexture(ccImplContext.get(), backTextureId, frontTextureId, size));
146                 EXPECT_CALL(implMock, flush());
147
148                 // Teardown TextureManager.
149                 EXPECT_CALL(allocatorMock, deleteTexture(frontTextureId, size, GraphicsContext3D::RGBA));
150             }
151         }
152
153         RefPtr<Canvas2DLayerChromium> canvas = Canvas2DLayerChromium::create(mainContext.get(), size, deferred ? Deferred : NonDeferred);
154         canvas->setIsDrawable(true);
155         canvas->setLayerTreeHost(layerTreeHost.get());
156         canvas->setBounds(IntSize(600, 300));
157         canvas->setTextureId(backTextureId);
158
159         canvas->setNeedsDisplay();
160         EXPECT_TRUE(canvas->needsDisplay());
161         canvas->update(updater, 0);
162         EXPECT_FALSE(canvas->needsDisplay());
163         {
164             DebugScopedSetImplThread scopedImplThread;
165
166             OwnPtr<CCLayerImpl> layerImpl = canvas->createCCLayerImpl();
167             EXPECT_EQ(0u, static_cast<CCTextureLayerImpl*>(layerImpl.get())->textureId());
168
169             updater.update(ccImplContext.get(), &allocatorMock, &copierMock, &uploaderMock, 1);
170             canvas->pushPropertiesTo(layerImpl.get());
171
172             if (threaded && !deferred)
173                 EXPECT_EQ(frontTextureId, static_cast<CCTextureLayerImpl*>(layerImpl.get())->textureId());
174             else
175                 EXPECT_EQ(backTextureId, static_cast<CCTextureLayerImpl*>(layerImpl.get())->textureId());
176         }
177         canvas.clear();
178         layerTreeHost->contentsTextureManager()->reduceMemoryToLimit(0);
179         layerTreeHost->contentsTextureManager()->deleteEvictedTextures(&allocatorMock);
180         layerTreeHost.clear();
181         WebCompositor::shutdown();
182     }
183 };
184
185 namespace {
186
187 TEST_F(Canvas2DLayerChromiumTest, testFullLifecycleSingleThread)
188 {
189     fullLifecycleTest(false, false);
190 }
191
192 TEST_F(Canvas2DLayerChromiumTest, testFullLifecycleThreaded)
193 {
194     fullLifecycleTest(true, false);
195 }
196
197 TEST_F(Canvas2DLayerChromiumTest, testFullLifecycleSingleThreadDeferred)
198 {
199     fullLifecycleTest(false, true);
200 }
201
202 TEST_F(Canvas2DLayerChromiumTest, testFullLifecycleThreadedDeferred)
203 {
204     fullLifecycleTest(true, true);
205 }
206
207 } // namespace