Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: Source/web/tests/Canvas2DLayerManagerTest.cpp

Issue 104023007: Refactoring ImageBuffer to decouple it from Canvas2DLayerBridge (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: rebase mayhem Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/web/tests/Canvas2DLayerBridgeTest.cpp ('k') | Source/web/tests/TransparencyWinTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 26 matching lines...) Expand all
37 #include <gtest/gtest.h> 37 #include <gtest/gtest.h>
38 38
39 using namespace WebCore; 39 using namespace WebCore;
40 using testing::InSequence; 40 using testing::InSequence;
41 using testing::Return; 41 using testing::Return;
42 using testing::Test; 42 using testing::Test;
43 43
44 44
45 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge { 45 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge {
46 public: 46 public:
47 FakeCanvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, PassRefPtr<Sk DeferredCanvas> canvas) 47 FakeCanvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, PassOwnPtr<Sk DeferredCanvas> canvas)
48 : Canvas2DLayerBridge(context, canvas, 0, NonOpaque) 48 : Canvas2DLayerBridge(context, canvas, 0, NonOpaque)
49 , m_freeableBytes(0) 49 , m_freeableBytes(0)
50 , m_freeMemoryIfPossibleCount(0) 50 , m_freeMemoryIfPossibleCount(0)
51 , m_flushCount(0) 51 , m_flushCount(0)
52 { 52 {
53 } 53 }
54 54
55 virtual size_t storageAllocatedForRecording() OVERRIDE 55 virtual size_t storageAllocatedForRecording() OVERRIDE
56 { 56 {
57 // Because the fake layer has no canvas to query, just 57 // Because the fake layer has no canvas to query, just
(...skipping 24 matching lines...) Expand all
82 flushedDrawCommands(); 82 flushedDrawCommands();
83 m_flushCount++; 83 m_flushCount++;
84 } 84 }
85 85
86 public: 86 public:
87 size_t m_freeableBytes; 87 size_t m_freeableBytes;
88 int m_freeMemoryIfPossibleCount; 88 int m_freeMemoryIfPossibleCount;
89 int m_flushCount; 89 int m_flushCount;
90 }; 90 };
91 91
92 static PassRefPtr<SkDeferredCanvas> createCanvas(GraphicsContext3D* context) 92 class FakeCanvas2DLayerBridgePtr {
93 public:
94 FakeCanvas2DLayerBridgePtr(PassRefPtr<FakeCanvas2DLayerBridge> layerBridge)
95 : m_layerBridge(layerBridge) { }
96
97 ~FakeCanvas2DLayerBridgePtr()
98 {
99 m_layerBridge->beginDestruction();
100 }
101
102 FakeCanvas2DLayerBridge* operator->() { return m_layerBridge.get(); }
103 FakeCanvas2DLayerBridge* get() { return m_layerBridge.get(); }
104
105 private:
106 RefPtr<FakeCanvas2DLayerBridge> m_layerBridge;
107 };
108
109 static PassOwnPtr<SkDeferredCanvas> createCanvas(GraphicsContext3D* context)
93 { 110 {
94 return adoptRef(SkDeferredCanvas::Create(SkSurface::NewRasterPMColor(1, 1))) ; 111 return adoptPtr(SkDeferredCanvas::Create(SkSurface::NewRasterPMColor(1, 1))) ;
95 }
96
97 FakeCanvas2DLayerBridge* fake(const Canvas2DLayerBridgePtr& layer)
98 {
99 return static_cast<FakeCanvas2DLayerBridge*>(layer.get());
100 } 112 }
101 113
102 class Canvas2DLayerManagerTest : public Test { 114 class Canvas2DLayerManagerTest : public Test {
103 protected: 115 protected:
104 void storageAllocationTrackingTest() 116 void storageAllocationTrackingTest()
105 { 117 {
106 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); 118 Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
107 manager.init(10, 10); 119 manager.init(10, 10);
108 { 120 {
109 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphic sContextFromWebContext(adoptPtr(new blink::MockWebGraphicsContext3D)); 121 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphic sContextFromWebContext(adoptPtr(new blink::MockWebGraphicsContext3D));
110 RefPtr<SkDeferredCanvas> canvas1(createCanvas(context.get())); 122 OwnPtr<SkDeferredCanvas> canvas1 = createCanvas(context.get());
111 Canvas2DLayerBridgePtr layer1(adoptRef(new FakeCanvas2DLayerBridge(c ontext, canvas1.release()))); 123 FakeCanvas2DLayerBridgePtr layer1(adoptRef(new FakeCanvas2DLayerBrid ge(context, canvas1.release())));
112 EXPECT_EQ((size_t)0, manager.m_bytesAllocated); 124 EXPECT_EQ((size_t)0, manager.m_bytesAllocated);
113 layer1->storageAllocatedForRecordingChanged(1); 125 layer1->storageAllocatedForRecordingChanged(1);
114 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); 126 EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
115 // Test allocation increase 127 // Test allocation increase
116 layer1->storageAllocatedForRecordingChanged(2); 128 layer1->storageAllocatedForRecordingChanged(2);
117 EXPECT_EQ((size_t)2, manager.m_bytesAllocated); 129 EXPECT_EQ((size_t)2, manager.m_bytesAllocated);
118 // Test allocation decrease 130 // Test allocation decrease
119 layer1->storageAllocatedForRecordingChanged(1); 131 layer1->storageAllocatedForRecordingChanged(1);
120 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); 132 EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
121 { 133 {
122 RefPtr<SkDeferredCanvas> canvas2(createCanvas(context.get())); 134 OwnPtr<SkDeferredCanvas> canvas2 = createCanvas(context.get());
123 Canvas2DLayerBridgePtr layer2(adoptRef(new FakeCanvas2DLayerBrid ge(context, canvas2.release()))); 135 FakeCanvas2DLayerBridgePtr layer2(adoptRef(new FakeCanvas2DLayer Bridge(context, canvas2.release())));
124 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); 136 EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
125 // verify multi-layer allocation tracking 137 // verify multi-layer allocation tracking
126 layer2->storageAllocatedForRecordingChanged(2); 138 layer2->storageAllocatedForRecordingChanged(2);
127 EXPECT_EQ((size_t)3, manager.m_bytesAllocated); 139 EXPECT_EQ((size_t)3, manager.m_bytesAllocated);
128 } 140 }
129 // Verify tracking after destruction 141 // Verify tracking after destruction
130 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); 142 EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
131 } 143 }
132 } 144 }
133 145
134 void evictionTest() 146 void evictionTest()
135 { 147 {
136 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::MockWebGraphicsContext3D)); 148 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::MockWebGraphicsContext3D));
137 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); 149 Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
138 manager.init(10, 5); 150 manager.init(10, 5);
139 RefPtr<SkDeferredCanvas> canvas(createCanvas(context.get())); 151 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get());
140 Canvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(contex t, canvas.release()))); 152 FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(co ntext, canvas.release())));
141 fake(layer)->fakeFreeableBytes(10); 153 layer->fakeFreeableBytes(10);
142 layer->storageAllocatedForRecordingChanged(8); // under the max 154 layer->storageAllocatedForRecordingChanged(8); // under the max
143 EXPECT_EQ(0, fake(layer)->m_freeMemoryIfPossibleCount); 155 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount);
144 layer->storageAllocatedForRecordingChanged(12); // over the max 156 layer->storageAllocatedForRecordingChanged(12); // over the max
145 EXPECT_EQ(1, fake(layer)->m_freeMemoryIfPossibleCount); 157 EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount);
146 EXPECT_EQ((size_t)3, fake(layer)->m_freeableBytes); 158 EXPECT_EQ((size_t)3, layer->m_freeableBytes);
147 EXPECT_EQ(0, fake(layer)->m_flushCount); // eviction succeeded without t riggering a flush 159 EXPECT_EQ(0, layer->m_flushCount); // eviction succeeded without trigger ing a flush
148 EXPECT_EQ((size_t)5, layer->bytesAllocated()); 160 EXPECT_EQ((size_t)5, layer->bytesAllocated());
149 } 161 }
150 162
151 void flushEvictionTest() 163 void flushEvictionTest()
152 { 164 {
153 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::MockWebGraphicsContext3D)); 165 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::MockWebGraphicsContext3D));
154 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); 166 Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
155 manager.init(10, 5); 167 manager.init(10, 5);
156 RefPtr<SkDeferredCanvas> canvas(createCanvas(context.get())); 168 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get());
157 Canvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(contex t, canvas.release()))); 169 FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(co ntext, canvas.release())));
158 fake(layer)->fakeFreeableBytes(1); // Not enough freeable bytes, will ca use aggressive eviction by flushing 170 layer->fakeFreeableBytes(1); // Not enough freeable bytes, will cause ag gressive eviction by flushing
159 layer->storageAllocatedForRecordingChanged(8); // under the max 171 layer->storageAllocatedForRecordingChanged(8); // under the max
160 EXPECT_EQ(0, fake(layer)->m_freeMemoryIfPossibleCount); 172 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount);
161 layer->storageAllocatedForRecordingChanged(12); // over the max 173 layer->storageAllocatedForRecordingChanged(12); // over the max
162 EXPECT_EQ(2, fake(layer)->m_freeMemoryIfPossibleCount); // Two tries, on e before flush, one after flush 174 EXPECT_EQ(2, layer->m_freeMemoryIfPossibleCount); // Two tries, one befo re flush, one after flush
163 EXPECT_EQ((size_t)0, fake(layer)->m_freeableBytes); 175 EXPECT_EQ((size_t)0, layer->m_freeableBytes);
164 EXPECT_EQ(1, fake(layer)->m_flushCount); // flush was attempted 176 EXPECT_EQ(1, layer->m_flushCount); // flush was attempted
165 EXPECT_EQ((size_t)11, layer->bytesAllocated()); // flush drops the layer from manager's tracking list 177 EXPECT_EQ((size_t)11, layer->bytesAllocated()); // flush drops the layer from manager's tracking list
166 EXPECT_FALSE(manager.isInList(layer.get())); 178 EXPECT_FALSE(manager.isInList(layer.get()));
167 } 179 }
168 180
169 void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipComman ds) 181 void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipComman ds)
170 { 182 {
171 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); 183 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
172 layer->contextAcquired(); 184 layer->willUse();
173 layer->storageAllocatedForRecordingChanged(1); 185 layer->storageAllocatedForRecordingChanged(1);
174 EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive); 186 EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive);
175 if (skipCommands) { 187 if (skipCommands) {
176 layer->contextAcquired(); 188 layer->willUse();
177 layer->storageAllocatedForRecordingChanged(0); 189 layer->storageAllocatedForRecordingChanged(0);
178 layer->skippedPendingDrawCommands(); 190 layer->skippedPendingDrawCommands();
179 } 191 }
180 blink::Platform::current()->currentThread()->exitRunLoop(); 192 blink::Platform::current()->currentThread()->exitRunLoop();
181 } 193 }
182 194
183 class DeferredFrameTestTask : public blink::WebThread::Task { 195 class DeferredFrameTestTask : public blink::WebThread::Task {
184 public: 196 public:
185 DeferredFrameTestTask(Canvas2DLayerManagerTest* test, FakeCanvas2DLayerB ridge* layer, bool skipCommands) 197 DeferredFrameTestTask(Canvas2DLayerManagerTest* test, FakeCanvas2DLayerB ridge* layer, bool skipCommands)
186 { 198 {
187 m_test = test; 199 m_test = test;
188 m_layer = layer; 200 m_layer = layer;
189 m_skipCommands = skipCommands; 201 m_skipCommands = skipCommands;
190 } 202 }
191 203
192 virtual void run() OVERRIDE 204 virtual void run() OVERRIDE
193 { 205 {
194 m_test->doDeferredFrameTestTask(m_layer, m_skipCommands); 206 m_test->doDeferredFrameTestTask(m_layer, m_skipCommands);
195 } 207 }
196 private: 208 private:
197 Canvas2DLayerManagerTest* m_test; 209 Canvas2DLayerManagerTest* m_test;
198 FakeCanvas2DLayerBridge* m_layer; 210 FakeCanvas2DLayerBridge* m_layer;
199 bool m_skipCommands; 211 bool m_skipCommands;
200 }; 212 };
201 213
202 void deferredFrameTest() 214 void deferredFrameTest()
203 { 215 {
204 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::MockWebGraphicsContext3D)); 216 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::MockWebGraphicsContext3D));
205 Canvas2DLayerManager::get().init(10, 10); 217 Canvas2DLayerManager::get().init(10, 10);
206 RefPtr<SkDeferredCanvas> canvas(createCanvas(context.get())); 218 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get());
207 Canvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(contex t, canvas.release()))); 219 FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(co ntext, canvas.release())));
208 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, fake(layer), true)); 220 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, layer.get(), true));
209 blink::Platform::current()->currentThread()->enterRunLoop(); 221 blink::Platform::current()->currentThread()->enterRunLoop();
210 // Verify that didProcessTask was called upon completion 222 // Verify that didProcessTask was called upon completion
211 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); 223 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
212 // Verify that no flush was performed because frame is fresh 224 // Verify that no flush was performed because frame is fresh
213 EXPECT_EQ(0, fake(layer)->m_flushCount); 225 EXPECT_EQ(0, layer->m_flushCount);
214 226
215 // Verify that no flushes are triggered as long as frame are fresh 227 // Verify that no flushes are triggered as long as frame are fresh
216 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, fake(layer), true)); 228 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, layer.get(), true));
217 blink::Platform::current()->currentThread()->enterRunLoop(); 229 blink::Platform::current()->currentThread()->enterRunLoop();
218 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); 230 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
219 EXPECT_EQ(0, fake(layer)->m_flushCount); 231 EXPECT_EQ(0, layer->m_flushCount);
220 232
221 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, fake(layer), true)); 233 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, layer.get(), true));
222 blink::Platform::current()->currentThread()->enterRunLoop(); 234 blink::Platform::current()->currentThread()->enterRunLoop();
223 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); 235 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
224 EXPECT_EQ(0, fake(layer)->m_flushCount); 236 EXPECT_EQ(0, layer->m_flushCount);
225 237
226 // Verify that a flush is triggered when queue is accumulating a multi-f rame backlog. 238 // Verify that a flush is triggered when queue is accumulating a multi-f rame backlog.
227 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, fake(layer), false)); 239 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, layer.get(), false));
228 blink::Platform::current()->currentThread()->enterRunLoop(); 240 blink::Platform::current()->currentThread()->enterRunLoop();
229 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); 241 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
230 EXPECT_EQ(1, fake(layer)->m_flushCount); 242 EXPECT_EQ(1, layer->m_flushCount);
231 243
232 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, fake(layer), false)); 244 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, layer.get(), false));
233 blink::Platform::current()->currentThread()->enterRunLoop(); 245 blink::Platform::current()->currentThread()->enterRunLoop();
234 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); 246 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
235 EXPECT_EQ(2, fake(layer)->m_flushCount); 247 EXPECT_EQ(2, layer->m_flushCount);
236 } 248 }
237 }; 249 };
238 250
239 namespace { 251 namespace {
240 252
241 TEST_F(Canvas2DLayerManagerTest, testStorageAllocationTracking) 253 TEST_F(Canvas2DLayerManagerTest, testStorageAllocationTracking)
242 { 254 {
243 storageAllocationTrackingTest(); 255 storageAllocationTrackingTest();
244 } 256 }
245 257
246 TEST_F(Canvas2DLayerManagerTest, testEviction) 258 TEST_F(Canvas2DLayerManagerTest, testEviction)
247 { 259 {
248 evictionTest(); 260 evictionTest();
249 } 261 }
250 262
251 TEST_F(Canvas2DLayerManagerTest, testFlushEviction) 263 TEST_F(Canvas2DLayerManagerTest, testFlushEviction)
252 { 264 {
253 flushEvictionTest(); 265 flushEvictionTest();
254 } 266 }
255 267
256 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame) 268 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame)
257 { 269 {
258 deferredFrameTest(); 270 deferredFrameTest();
259 } 271 }
260 272
261 } // namespace 273 } // namespace
262 274
OLDNEW
« no previous file with comments | « Source/web/tests/Canvas2DLayerBridgeTest.cpp ('k') | Source/web/tests/TransparencyWinTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698