Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / tests / Canvas2DLayerManagerTest.cpp
1 /*
2  * Copyright (C) 2012 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 "platform/graphics/Canvas2DLayerManager.h"
28
29 #include "SkDevice.h"
30 #include "SkSurface.h"
31 #include "public/platform/Platform.h"
32 #include "public/platform/WebGraphicsContext3DProvider.h"
33 #include "public/platform/WebThread.h"
34 #include "web/tests/MockWebGraphicsContext3D.h"
35
36 #include <gmock/gmock.h>
37 #include <gtest/gtest.h>
38
39 using namespace WebCore;
40 using testing::InSequence;
41 using testing::Return;
42 using testing::Test;
43
44 namespace {
45
46 class MockWebGraphicsContext3DProvider : public blink::WebGraphicsContext3DProvider {
47 public:
48     MockWebGraphicsContext3DProvider(blink::WebGraphicsContext3D* context3d)
49         : m_context3d(context3d) { }
50
51     blink::WebGraphicsContext3D* context3d()
52     {
53         return m_context3d;
54     }
55
56     GrContext* grContext()
57     {
58         return 0;
59     }
60
61 private:
62     blink::WebGraphicsContext3D* m_context3d;
63 };
64
65 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge {
66 public:
67     FakeCanvas2DLayerBridge(blink::WebGraphicsContext3D* context, PassOwnPtr<SkDeferredCanvas> canvas)
68         : Canvas2DLayerBridge(adoptPtr(new MockWebGraphicsContext3DProvider(context)), canvas, 0, NonOpaque)
69         , m_freeableBytes(0)
70         , m_freeMemoryIfPossibleCount(0)
71         , m_flushCount(0)
72     {
73     }
74
75     virtual size_t storageAllocatedForRecording() OVERRIDE
76     {
77         // Because the fake layer has no canvas to query, just
78         // return status quo. Allocation changes that would normally be
79         // initiated by the canvas can be faked by invoking
80         // storageAllocatedForRecordingChanged directly from the test code.
81         return m_bytesAllocated;
82     }
83
84     void fakeFreeableBytes(size_t size)
85     {
86         m_freeableBytes = size;
87     }
88
89     virtual size_t freeMemoryIfPossible(size_t size) OVERRIDE
90     {
91         m_freeMemoryIfPossibleCount++;
92         size_t bytesFreed = size < m_freeableBytes ? size : m_freeableBytes;
93         m_freeableBytes -= bytesFreed;
94         if (bytesFreed)
95             storageAllocatedForRecordingChanged(m_bytesAllocated - bytesFreed);
96         return bytesFreed;
97     }
98
99     virtual void flush() OVERRIDE
100     {
101         flushedDrawCommands();
102         m_freeableBytes = bytesAllocated();
103         m_flushCount++;
104     }
105
106 public:
107     size_t m_freeableBytes;
108     int m_freeMemoryIfPossibleCount;
109     int m_flushCount;
110 };
111
112 class FakeCanvas2DLayerBridgePtr {
113 public:
114     FakeCanvas2DLayerBridgePtr(PassRefPtr<FakeCanvas2DLayerBridge> layerBridge)
115         : m_layerBridge(layerBridge) { }
116
117     ~FakeCanvas2DLayerBridgePtr()
118     {
119         m_layerBridge->beginDestruction();
120     }
121
122     FakeCanvas2DLayerBridge* operator->() { return m_layerBridge.get(); }
123     FakeCanvas2DLayerBridge* get() { return m_layerBridge.get(); }
124
125 private:
126     RefPtr<FakeCanvas2DLayerBridge> m_layerBridge;
127 };
128
129 static PassOwnPtr<SkDeferredCanvas> createCanvas()
130 {
131     return adoptPtr(SkDeferredCanvas::Create(SkSurface::NewRasterPMColor(1, 1)));
132 }
133
134 } // unnamed namespace
135
136 class Canvas2DLayerManagerTest : public Test {
137 protected:
138     void storageAllocationTrackingTest()
139     {
140         Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
141         manager.init(10, 10);
142         {
143             OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
144             OwnPtr<SkDeferredCanvas> canvas1 = createCanvas();
145             FakeCanvas2DLayerBridgePtr layer1(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas1.release())));
146             EXPECT_EQ((size_t)0, manager.m_bytesAllocated);
147             layer1->storageAllocatedForRecordingChanged(1);
148             EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
149             // Test allocation increase
150             layer1->storageAllocatedForRecordingChanged(2);
151             EXPECT_EQ((size_t)2, manager.m_bytesAllocated);
152             // Test allocation decrease
153             layer1->storageAllocatedForRecordingChanged(1);
154             EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
155             {
156                 OwnPtr<SkDeferredCanvas> canvas2 = createCanvas();
157                 FakeCanvas2DLayerBridgePtr layer2(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas2.release())));
158                 EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
159                 // verify multi-layer allocation tracking
160                 layer2->storageAllocatedForRecordingChanged(2);
161                 EXPECT_EQ((size_t)3, manager.m_bytesAllocated);
162             }
163             // Verify tracking after destruction
164             EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
165         }
166     }
167
168     void evictionTest()
169     {
170         OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
171         Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
172         manager.init(10, 5);
173         OwnPtr<SkDeferredCanvas> canvas = createCanvas();
174         FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release())));
175         layer->fakeFreeableBytes(10);
176         layer->storageAllocatedForRecordingChanged(8); // under the max
177         EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount);
178         layer->storageAllocatedForRecordingChanged(12); // over the max
179         EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount);
180         EXPECT_EQ((size_t)3, layer->m_freeableBytes);
181         EXPECT_EQ(0, layer->m_flushCount); // eviction succeeded without triggering a flush
182         EXPECT_EQ((size_t)5, layer->bytesAllocated());
183     }
184
185     void hiddenCanvasTest()
186     {
187         OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
188         Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
189         manager.init(20, 5);
190         OwnPtr<SkDeferredCanvas> canvas = createCanvas();
191         FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release())));
192         layer->fakeFreeableBytes(5);
193         layer->storageAllocatedForRecordingChanged(10);
194         EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount);
195         EXPECT_EQ(0, layer->m_flushCount);
196         EXPECT_EQ((size_t)10, layer->bytesAllocated());
197         layer->setIsHidden(true);
198         EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount);
199         EXPECT_EQ((size_t)0, layer->m_freeableBytes);
200         EXPECT_EQ((size_t)0, layer->bytesAllocated());
201         EXPECT_EQ(1, layer->m_flushCount);
202     }
203
204     void addRemoveLayerTest()
205     {
206         OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
207         Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
208         manager.init(10, 5);
209         OwnPtr<SkDeferredCanvas> canvas = createCanvas();
210         FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release())));
211         EXPECT_FALSE(manager.isInList(layer.get()));
212         layer->storageAllocatedForRecordingChanged(5);
213         EXPECT_TRUE(manager.isInList(layer.get()));
214         layer->storageAllocatedForRecordingChanged(0);
215         EXPECT_FALSE(manager.isInList(layer.get()));
216     }
217
218     void flushEvictionTest()
219     {
220         OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
221         Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
222         manager.init(10, 5);
223         OwnPtr<SkDeferredCanvas> canvas = createCanvas();
224         FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release())));
225         layer->fakeFreeableBytes(1); // Not enough freeable bytes, will cause aggressive eviction by flushing
226         layer->storageAllocatedForRecordingChanged(8); // under the max
227         EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount);
228         layer->storageAllocatedForRecordingChanged(12); // over the max
229         EXPECT_EQ(2, layer->m_freeMemoryIfPossibleCount); // Two tries, one before flush, one after flush
230         EXPECT_EQ((size_t)5, layer->m_freeableBytes);
231         EXPECT_EQ(1, layer->m_flushCount); // flush was attempted
232         EXPECT_EQ((size_t)5, layer->bytesAllocated());
233         EXPECT_TRUE(manager.isInList(layer.get()));
234     }
235
236     void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipCommands)
237     {
238         EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
239         layer->willUse();
240         layer->storageAllocatedForRecordingChanged(1);
241         EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive);
242         if (skipCommands) {
243             layer->willUse();
244             layer->skippedPendingDrawCommands();
245         }
246         blink::Platform::current()->currentThread()->exitRunLoop();
247     }
248
249     class DeferredFrameTestTask : public blink::WebThread::Task {
250     public:
251         DeferredFrameTestTask(Canvas2DLayerManagerTest* test, FakeCanvas2DLayerBridge* layer, bool skipCommands)
252         {
253             m_test = test;
254             m_layer = layer;
255             m_skipCommands = skipCommands;
256         }
257
258         virtual void run() OVERRIDE
259         {
260             m_test->doDeferredFrameTestTask(m_layer, m_skipCommands);
261         }
262     private:
263         Canvas2DLayerManagerTest* m_test;
264         FakeCanvas2DLayerBridge* m_layer;
265         bool m_skipCommands;
266     };
267
268     void deferredFrameTest()
269     {
270         OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
271         Canvas2DLayerManager::get().init(10, 10);
272         OwnPtr<SkDeferredCanvas> canvas = createCanvas();
273         FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release())));
274         blink::Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), true));
275         blink::Platform::current()->currentThread()->enterRunLoop();
276         // Verify that didProcessTask was called upon completion
277         EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
278         // Verify that no flush was performed because frame is fresh
279         EXPECT_EQ(0, layer->m_flushCount);
280
281         // Verify that no flushes are triggered as long as frame are fresh
282         blink::Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), true));
283         blink::Platform::current()->currentThread()->enterRunLoop();
284         EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
285         EXPECT_EQ(0, layer->m_flushCount);
286
287         blink::Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), true));
288         blink::Platform::current()->currentThread()->enterRunLoop();
289         EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
290         EXPECT_EQ(0, layer->m_flushCount);
291
292         // Verify that a flush is triggered when queue is accumulating a multi-frame backlog.
293         blink::Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), false));
294         blink::Platform::current()->currentThread()->enterRunLoop();
295         EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
296         EXPECT_EQ(1, layer->m_flushCount);
297
298         blink::Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), false));
299         blink::Platform::current()->currentThread()->enterRunLoop();
300         EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
301         EXPECT_EQ(2, layer->m_flushCount);
302     }
303 };
304
305 namespace {
306
307 TEST_F(Canvas2DLayerManagerTest, testStorageAllocationTracking)
308 {
309     storageAllocationTrackingTest();
310 }
311
312 TEST_F(Canvas2DLayerManagerTest, testEviction)
313 {
314     evictionTest();
315 }
316
317 TEST_F(Canvas2DLayerManagerTest, testFlushEviction)
318 {
319     flushEvictionTest();
320 }
321
322 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame)
323 {
324     deferredFrameTest();
325 }
326
327 TEST_F(Canvas2DLayerManagerTest, testHiddenCanvas)
328 {
329     hiddenCanvasTest();
330 }
331
332 TEST_F(Canvas2DLayerManagerTest, testAddRemoveLayer)
333 {
334     addRemoveLayerTest();
335 }
336
337 } // unnamed namespace
338