[chromium] Move deferral-related logic out of Canvas2DLayerChromium
[WebKit.git] / Source / WebKit / chromium / tests / Canvas2DLayerBridgeTest.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 "Canvas2DLayerBridge.h"
28
29 #include "FakeWebGraphicsContext3D.h"
30 #include "GraphicsContext3DPrivate.h"
31 #include "ImageBuffer.h"
32 #include "TextureCopier.h"
33 #include "TextureManager.h"
34 #include "WebCompositor.h"
35 #include "WebKit.h"
36 #include "cc/CCGraphicsContext.h"
37 #include "cc/CCTextureUpdater.h"
38 #include "platform/WebKitPlatformSupport.h"
39 #include "platform/WebThread.h"
40
41 #include <gmock/gmock.h>
42 #include <gtest/gtest.h>
43 #include <wtf/RefPtr.h>
44
45 using namespace WebCore;
46 using namespace WebKit;
47 using testing::InSequence;
48 using testing::Return;
49 using testing::Test;
50
51 namespace {
52
53 class MockCanvasContext : public FakeWebGraphicsContext3D {
54 public:
55     MOCK_METHOD0(flush, void(void));
56     MOCK_METHOD0(createTexture, unsigned(void));
57     MOCK_METHOD1(deleteTexture, void(unsigned));
58
59     virtual GrGLInterface* onCreateGrGLInterface() OVERRIDE { return 0; }
60 };
61
62 class MockTextureCopier : public TextureCopier {
63 public:
64     MOCK_METHOD4(copyTexture, void(CCGraphicsContext*, unsigned, unsigned, const IntSize&));
65 };
66
67 } // namespace
68
69 enum ThreadMode {
70     SingleThreaded, Threaded
71 };
72
73 class Canvas2DLayerBridgeTest : public Test {
74 protected:
75     void fullLifecycleTest(ThreadMode threadMode, DeferralMode deferralMode)
76     {
77         GraphicsContext3D::Attributes attrs;
78
79         RefPtr<GraphicsContext3D> mainContext = GraphicsContext3DPrivate::createGraphicsContextFromWebContext(adoptPtr(new MockCanvasContext()), GraphicsContext3D::RenderDirectlyToHostWindow);
80         RefPtr<CCGraphicsContext> ccMainContext = CCGraphicsContext::create3D(mainContext);
81         RefPtr<GraphicsContext3D> implContext = GraphicsContext3DPrivate::createGraphicsContextFromWebContext(adoptPtr(new MockCanvasContext()), GraphicsContext3D::RenderDirectlyToHostWindow);
82         RefPtr<CCGraphicsContext> ccImplContext = CCGraphicsContext::create3D(implContext);
83
84         MockCanvasContext& mainMock = *static_cast<MockCanvasContext*>(GraphicsContext3DPrivate::extractWebGraphicsContext3D(mainContext.get()));
85         MockCanvasContext& implMock = *static_cast<MockCanvasContext*>(GraphicsContext3DPrivate::extractWebGraphicsContext3D(implContext.get()));
86
87         MockTextureCopier copierMock;
88         CCTextureUpdater updater;
89
90         const IntSize size(300, 150);
91
92         OwnPtr<WebThread> thread;
93         if (threadMode == Threaded)
94             thread = adoptPtr(WebKit::Platform::current()->createThread("Canvas2DLayerBridgeTest"));
95         WebCompositor::initialize(thread.get());
96
97         WebGLId backTextureId = 1;
98         WebGLId frontTextureId = 1;
99
100         // Threaded and non deferred canvases are double buffered.
101         if (threadMode == Threaded && deferralMode == NonDeferred) {
102             frontTextureId = 2;
103             // Create texture (on the main thread) and do the copy (on the impl thread).
104             EXPECT_CALL(mainMock, createTexture()).WillOnce(Return(frontTextureId));
105         }
106
107         OwnPtr<Canvas2DLayerBridge> bridge = Canvas2DLayerBridge::create(mainContext.get(), size, deferralMode, backTextureId);
108
109         ::testing::Mock::VerifyAndClearExpectations(&mainMock);
110
111         EXPECT_CALL(mainMock, flush());
112         EXPECT_EQ(frontTextureId, bridge->prepareTexture(updater));
113         ::testing::Mock::VerifyAndClearExpectations(&mainMock);
114
115         if (threadMode == Threaded && deferralMode == NonDeferred) {
116             EXPECT_CALL(copierMock, copyTexture(ccImplContext.get(), backTextureId, frontTextureId, size));
117             EXPECT_CALL(implMock, flush());
118         }
119         updater.update(ccImplContext.get(), 0, &copierMock, 0, 100);
120         ::testing::Mock::VerifyAndClearExpectations(&implMock);
121
122         if (threadMode == Threaded && deferralMode == NonDeferred) {
123             EXPECT_CALL(mainMock, deleteTexture(frontTextureId));
124             EXPECT_CALL(mainMock, flush());
125         }
126         bridge.clear();
127         ::testing::Mock::VerifyAndClearExpectations(&implMock);
128
129         WebCompositor::shutdown();
130     }
131 };
132
133 namespace {
134
135 TEST_F(Canvas2DLayerBridgeTest, testFullLifecycleSingleThreadedDeferred)
136 {
137     fullLifecycleTest(SingleThreaded, NonDeferred);
138 }
139
140 TEST_F(Canvas2DLayerBridgeTest, testFullLifecycleSingleThreadedNonDeferred)
141 {
142     fullLifecycleTest(SingleThreaded, Deferred);
143 }
144
145 TEST_F(Canvas2DLayerBridgeTest, testFullLifecycleThreadedNonDeferred)
146 {
147     fullLifecycleTest(Threaded, NonDeferred);
148 }
149
150 TEST_F(Canvas2DLayerBridgeTest, testFullLifecycleThreadedDeferred)
151 {
152     fullLifecycleTest(Threaded, Deferred);
153 }
154
155 } // namespace
156