1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/gl_renderer.h"
#include "cc/compositor_frame_metadata.h"
#include "cc/draw_quad.h"
#include "cc/prioritized_resource_manager.h"
#include "cc/resource_provider.h"
#include "cc/test/fake_impl_proxy.h"
#include "cc/test/fake_layer_tree_host_impl.h"
#include "cc/test/fake_output_surface.h"
#include "cc/test/fake_web_graphics_context_3d.h"
#include "cc/test/render_pass_test_common.h"
#include "cc/test/render_pass_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "ui/gfx/transform.h"
using namespace WebKit;
using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
using testing::Expectation;
using testing::InSequence;
using testing::Mock;
namespace cc {
namespace {
class FrameCountingMemoryAllocationSettingContext : public FakeWebGraphicsContext3D {
public:
FrameCountingMemoryAllocationSettingContext() : m_frame(0) { }
// WebGraphicsContext3D methods.
// This method would normally do a glSwapBuffers under the hood.
virtual void prepareTexture() { m_frame++; }
virtual void setMemoryAllocationChangedCallbackCHROMIUM(WebGraphicsMemoryAllocationChangedCallbackCHROMIUM* callback) { m_memoryAllocationChangedCallback = callback; }
virtual WebString getString(WebKit::WGC3Denum name)
{
if (name == GL_EXTENSIONS)
return WebString("GL_CHROMIUM_set_visibility GL_CHROMIUM_gpu_memory_manager GL_CHROMIUM_discard_backbuffer");
return WebString();
}
// Methods added for test.
int frameCount() { return m_frame; }
void setMemoryAllocation(WebGraphicsMemoryAllocation allocation)
{
m_memoryAllocationChangedCallback->onMemoryAllocationChanged(allocation);
}
private:
int m_frame;
WebGraphicsMemoryAllocationChangedCallbackCHROMIUM* m_memoryAllocationChangedCallback;
};
class FakeRendererClient : public RendererClient {
public:
FakeRendererClient()
: m_hostImpl(&m_proxy)
, m_setFullRootLayerDamageCount(0)
, m_lastCallWasSetVisibility(0)
, m_rootLayer(LayerImpl::create(m_hostImpl.activeTree(), 1))
, m_memoryAllocationLimitBytes(PrioritizedResourceManager::defaultMemoryAllocationLimit())
{
m_rootLayer->createRenderSurface();
RenderPass::Id renderPassId = m_rootLayer->renderSurface()->renderPassId();
scoped_ptr<RenderPass> rootRenderPass = RenderPass::Create();
rootRenderPass->SetNew(renderPassId, gfx::Rect(), gfx::Rect(), gfx::Transform());
m_renderPassesInDrawOrder.append(rootRenderPass.Pass());
}
// RendererClient methods.
virtual const gfx::Size& deviceViewportSize() const OVERRIDE { static gfx::Size fakeSize(1, 1); return fakeSize; }
virtual const LayerTreeSettings& settings() const OVERRIDE { static LayerTreeSettings fakeSettings; return fakeSettings; }
virtual void didLoseOutputSurface() OVERRIDE { }
virtual void onSwapBuffersComplete() OVERRIDE { }
virtual void setFullRootLayerDamage() OVERRIDE { m_setFullRootLayerDamageCount++; }
virtual void setManagedMemoryPolicy(const ManagedMemoryPolicy& policy) OVERRIDE { m_memoryAllocationLimitBytes = policy.bytesLimitWhenVisible; }
virtual void enforceManagedMemoryPolicy(const ManagedMemoryPolicy& policy) OVERRIDE { if (m_lastCallWasSetVisibility) *m_lastCallWasSetVisibility = false; }
virtual bool hasImplThread() const OVERRIDE { return false; }
virtual bool shouldClearRootRenderPass() const OVERRIDE { return true; }
virtual CompositorFrameMetadata makeCompositorFrameMetadata() const
OVERRIDE { return CompositorFrameMetadata(); }
// Methods added for test.
int setFullRootLayerDamageCount() const { return m_setFullRootLayerDamageCount; }
void setLastCallWasSetVisibilityPointer(bool* lastCallWasSetVisibility) { m_lastCallWasSetVisibility = lastCallWasSetVisibility; }
RenderPass* rootRenderPass() { return m_renderPassesInDrawOrder.last(); }
RenderPassList& renderPassesInDrawOrder() { return m_renderPassesInDrawOrder; }
size_t memoryAllocationLimitBytes() const { return m_memoryAllocationLimitBytes; }
private:
FakeImplProxy m_proxy;
FakeLayerTreeHostImpl m_hostImpl;
int m_setFullRootLayerDamageCount;
bool* m_lastCallWasSetVisibility;
scoped_ptr<LayerImpl> m_rootLayer;
RenderPassList m_renderPassesInDrawOrder;
size_t m_memoryAllocationLimitBytes;
};
class FakeRendererGL : public GLRenderer {
public:
FakeRendererGL(RendererClient* client, OutputSurface* outputSurface, ResourceProvider* resourceProvider) : GLRenderer(client, outputSurface, resourceProvider) { }
// GLRenderer methods.
// Changing visibility to public.
using GLRenderer::initialize;
using GLRenderer::isBackbufferDiscarded;
using GLRenderer::drawQuad;
using GLRenderer::beginDrawingFrame;
using GLRenderer::finishDrawingQuadList;
};
class GLRendererTest : public testing::Test {
protected:
GLRendererTest()
: m_suggestHaveBackbufferYes(1, true)
, m_suggestHaveBackbufferNo(1, false)
, m_outputSurface(FakeOutputSurface::Create3d(scoped_ptr<WebKit::WebGraphicsContext3D>(new FrameCountingMemoryAllocationSettingContext())))
, m_resourceProvider(ResourceProvider::create(m_outputSurface.get()))
, m_renderer(&m_mockClient, m_outputSurface.get(), m_resourceProvider.get())
{
}
virtual void SetUp()
{
m_renderer.initialize();
}
void swapBuffers()
{
m_renderer.swapBuffers();
}
FrameCountingMemoryAllocationSettingContext* context() { return static_cast<FrameCountingMemoryAllocationSettingContext*>(m_outputSurface->Context3D()); }
WebGraphicsMemoryAllocation m_suggestHaveBackbufferYes;
WebGraphicsMemoryAllocation m_suggestHaveBackbufferNo;
scoped_ptr<OutputSurface> m_outputSurface;
FakeRendererClient m_mockClient;
scoped_ptr<ResourceProvider> m_resourceProvider;
FakeRendererGL m_renderer;
};
// Test GLRenderer discardBackbuffer functionality:
// Suggest recreating framebuffer when one already exists.
// Expected: it does nothing.
TEST_F(GLRendererTest, SuggestBackbufferYesWhenItAlreadyExistsShouldDoNothing)
{
context()->setMemoryAllocation(m_suggestHaveBackbufferYes);
EXPECT_EQ(0, m_mockClient.setFullRootLayerDamageCount());
EXPECT_FALSE(m_renderer.isBackbufferDiscarded());
swapBuffers();
EXPECT_EQ(1, context()->frameCount());
}
// Test GLRenderer discardBackbuffer functionality:
// Suggest discarding framebuffer when one exists and the renderer is not visible.
// Expected: it is discarded and damage tracker is reset.
TEST_F(GLRendererTest, SuggestBackbufferNoShouldDiscardBackbufferAndDamageRootLayerWhileNotVisible)
{
m_renderer.setVisible(false);
context()->setMemoryAllocation(m_suggestHaveBackbufferNo);
EXPECT_EQ(1, m_mockClient.setFullRootLayerDamageCount());
EXPECT_TRUE(m_renderer.isBackbufferDiscarded());
}
// Test GLRenderer discardBackbuffer functionality:
// Suggest discarding framebuffer when one exists and the renderer is visible.
// Expected: the allocation is ignored.
TEST_F(GLRendererTest, SuggestBackbufferNoDoNothingWhenVisible)
{
m_renderer.setVisible(true);
context()->setMemoryAllocation(m_suggestHaveBackbufferNo);
EXPECT_EQ(0, m_mockClient.setFullRootLayerDamageCount());
EXPECT_FALSE(m_renderer.isBackbufferDiscarded());
}
// Test GLRenderer discardBackbuffer functionality:
// Suggest discarding framebuffer when one does not exist.
// Expected: it does nothing.
TEST_F(GLRendererTest, SuggestBackbufferNoWhenItDoesntExistShouldDoNothing)
{
m_renderer.setVisible(false);
context()->setMemoryAllocation(m_suggestHaveBackbufferNo);
EXPECT_EQ(1, m_mockClient.setFullRootLayerDamageCount());
EXPECT_TRUE(m_renderer.isBackbufferDiscarded());
context()->setMemoryAllocation(m_suggestHaveBackbufferNo);
EXPECT_EQ(1, m_mockClient.setFullRootLayerDamageCount());
EXPECT_TRUE(m_renderer.isBackbufferDiscarded());
}
// Test GLRenderer discardBackbuffer functionality:
// Begin drawing a frame while a framebuffer is discarded.
// Expected: will recreate framebuffer.
TEST_F(GLRendererTest, DiscardedBackbufferIsRecreatedForScopeDuration)
{
m_renderer.setVisible(false);
context()->setMemoryAllocation(m_suggestHaveBackbufferNo);
EXPECT_TRUE(m_renderer.isBackbufferDiscarded());
EXPECT_EQ(1, m_mockClient.setFullRootLayerDamageCount());
m_renderer.setVisible(true);
m_renderer.drawFrame(m_mockClient.renderPassesInDrawOrder());
EXPECT_FALSE(m_renderer.isBackbufferDiscarded());
swapBuffers();
EXPECT_EQ(1, context()->frameCount());
}
TEST_F(GLRendererTest, FramebufferDiscardedAfterReadbackWhenNotVisible)
{
m_renderer.setVisible(false);
context()->setMemoryAllocation(m_suggestHaveBackbufferNo);
EXPECT_TRUE(m_renderer.isBackbufferDiscarded());
EXPECT_EQ(1, m_mockClient.setFullRootLayerDamageCount());
char pixels[4];
m_renderer.drawFrame(m_mockClient.renderPassesInDrawOrder());
EXPECT_FALSE(m_renderer.isBackbufferDiscarded());
m_renderer.getFramebufferPixels(pixels, gfx::Rect(0, 0, 1, 1));
EXPECT_TRUE(m_renderer.isBackbufferDiscarded());
EXPECT_EQ(2, m_mockClient.setFullRootLayerDamageCount());
}
class ForbidSynchronousCallContext : public FakeWebGraphicsContext3D {
public:
ForbidSynchronousCallContext() { }
virtual bool getActiveAttrib(WebGLId program, WGC3Duint index, ActiveInfo&) { ADD_FAILURE(); return false; }
virtual bool getActiveUniform(WebGLId program, WGC3Duint index, ActiveInfo&) { ADD_FAILURE(); return false; }
virtual void getAttachedShaders(WebGLId program, WGC3Dsizei maxCount, WGC3Dsizei* count, WebGLId* shaders) { ADD_FAILURE(); }
virtual WGC3Dint getAttribLocation(WebGLId program, const WGC3Dchar* name) { ADD_FAILURE(); return 0; }
virtual void getBooleanv(WGC3Denum pname, WGC3Dboolean* value) { ADD_FAILURE(); }
virtual void getBufferParameteriv(WGC3Denum target, WGC3Denum pname, WGC3Dint* value) { ADD_FAILURE(); }
virtual Attributes getContextAttributes() { ADD_FAILURE(); return attributes_; }
virtual WGC3Denum getError() { ADD_FAILURE(); return 0; }
virtual void getFloatv(WGC3Denum pname, WGC3Dfloat* value) { ADD_FAILURE(); }
virtual void getFramebufferAttachmentParameteriv(WGC3Denum target, WGC3Denum attachment, WGC3Denum pname, WGC3Dint* value) { ADD_FAILURE(); }
virtual void getIntegerv(WGC3Denum pname, WGC3Dint* value)
{
if (pname == GL_MAX_TEXTURE_SIZE)
*value = 1024; // MAX_TEXTURE_SIZE is cached client side, so it's OK to query.
else
ADD_FAILURE();
}
// We allow querying the shader compilation and program link status in debug mode, but not release.
virtual void getProgramiv(WebGLId program, WGC3Denum pname, WGC3Dint* value)
{
#ifndef NDEBUG
*value = 1;
#else
ADD_FAILURE();
#endif
}
virtual void getShaderiv(WebGLId shader, WGC3Denum pname, WGC3Dint* value)
{
#ifndef NDEBUG
*value = 1;
#else
ADD_FAILURE();
#endif
}
virtual WebString getString(WGC3Denum name)
{
// We allow querying the extension string.
// FIXME: It'd be better to check that we only do this before starting any other expensive work (like starting a compilation)
if (name != GL_EXTENSIONS)
ADD_FAILURE();
return WebString();
}
virtual WebString getProgramInfoLog(WebGLId program) { ADD_FAILURE(); return WebString(); }
virtual void getRenderbufferParameteriv(WGC3Denum target, WGC3Denum pname, WGC3Dint* value) { ADD_FAILURE(); }
virtual WebString getShaderInfoLog(WebGLId shader) { ADD_FAILURE(); return WebString(); }
virtual void getShaderPrecisionFormat(WGC3Denum shadertype, WGC3Denum precisiontype, WGC3Dint* range, WGC3Dint* precision) { ADD_FAILURE(); }
virtual WebString getShaderSource(WebGLId shader) { ADD_FAILURE(); return WebString(); }
virtual void getTexParameterfv(WGC3Denum target, WGC3Denum pname, WGC3Dfloat* value) { ADD_FAILURE(); }
virtual void getTexParameteriv(WGC3Denum target, WGC3Denum pname, WGC3Dint* value) { ADD_FAILURE(); }
virtual void getUniformfv(WebGLId program, WGC3Dint location, WGC3Dfloat* value) { ADD_FAILURE(); }
virtual void getUniformiv(WebGLId program, WGC3Dint location, WGC3Dint* value) { ADD_FAILURE(); }
virtual WGC3Dint getUniformLocation(WebGLId program, const WGC3Dchar* name) { ADD_FAILURE(); return 0; }
virtual void getVertexAttribfv(WGC3Duint index, WGC3Denum pname, WGC3Dfloat* value) { ADD_FAILURE(); }
virtual void getVertexAttribiv(WGC3Duint index, WGC3Denum pname, WGC3Dint* value) { ADD_FAILURE(); }
virtual WGC3Dsizeiptr getVertexAttribOffset(WGC3Duint index, WGC3Denum pname) { ADD_FAILURE(); return 0; }
};
// This test isn't using the same fixture as GLRendererTest, and you can't mix TEST() and TEST_F() with the same name, hence LRC2.
TEST(GLRendererTest2, initializationDoesNotMakeSynchronousCalls)
{
FakeRendererClient mockClient;
scoped_ptr<OutputSurface> outputSurface(FakeOutputSurface::Create3d(scoped_ptr<WebKit::WebGraphicsContext3D>(new ForbidSynchronousCallContext)));
scoped_ptr<ResourceProvider> resourceProvider(ResourceProvider::create(outputSurface.get()));
FakeRendererGL renderer(&mockClient, outputSurface.get(), resourceProvider.get());
EXPECT_TRUE(renderer.initialize());
}
class LoseContextOnFirstGetContext : public FakeWebGraphicsContext3D {
public:
LoseContextOnFirstGetContext()
: m_contextLost(false)
{
}
virtual bool makeContextCurrent() OVERRIDE
{
return !m_contextLost;
}
virtual void getProgramiv(WebGLId program, WGC3Denum pname, WGC3Dint* value) OVERRIDE
{
m_contextLost = true;
*value = 0;
}
virtual void getShaderiv(WebGLId shader, WGC3Denum pname, WGC3Dint* value) OVERRIDE
{
m_contextLost = true;
*value = 0;
}
virtual WGC3Denum getGraphicsResetStatusARB() OVERRIDE
{
return m_contextLost ? 1 : 0;
}
private:
bool m_contextLost;
};
TEST(GLRendererTest2, initializationWithQuicklyLostContextDoesNotAssert)
{
FakeRendererClient mockClient;
scoped_ptr<OutputSurface> outputSurface(FakeOutputSurface::Create3d(scoped_ptr<WebKit::WebGraphicsContext3D>(new LoseContextOnFirstGetContext)));
scoped_ptr<ResourceProvider> resourceProvider(ResourceProvider::create(outputSurface.get()));
FakeRendererGL renderer(&mockClient, outputSurface.get(), resourceProvider.get());
renderer.initialize();
}
class ContextThatDoesNotSupportMemoryManagmentExtensions : public FakeWebGraphicsContext3D {
public:
ContextThatDoesNotSupportMemoryManagmentExtensions() { }
// WebGraphicsContext3D methods.
// This method would normally do a glSwapBuffers under the hood.
virtual void prepareTexture() { }
virtual void setMemoryAllocationChangedCallbackCHROMIUM(WebGraphicsMemoryAllocationChangedCallbackCHROMIUM* callback) { }
virtual WebString getString(WebKit::WGC3Denum name) { return WebString(); }
};
TEST(GLRendererTest2, initializationWithoutGpuMemoryManagerExtensionSupportShouldDefaultToNonZeroAllocation)
{
FakeRendererClient mockClient;
scoped_ptr<OutputSurface> outputSurface(FakeOutputSurface::Create3d(scoped_ptr<WebKit::WebGraphicsContext3D>(new ContextThatDoesNotSupportMemoryManagmentExtensions)));
scoped_ptr<ResourceProvider> resourceProvider(ResourceProvider::create(outputSurface.get()));
FakeRendererGL renderer(&mockClient, outputSurface.get(), resourceProvider.get());
renderer.initialize();
EXPECT_GT(mockClient.memoryAllocationLimitBytes(), 0ul);
}
class ClearCountingContext : public FakeWebGraphicsContext3D {
public:
ClearCountingContext() : m_clear(0) { }
virtual void clear(WGC3Dbitfield)
{
m_clear++;
}
int clearCount() const { return m_clear; }
private:
int m_clear;
};
TEST(GLRendererTest2, opaqueBackground)
{
FakeRendererClient mockClient;
scoped_ptr<OutputSurface> outputSurface(FakeOutputSurface::Create3d(scoped_ptr<WebKit::WebGraphicsContext3D>(new ClearCountingContext)));
ClearCountingContext* context = static_cast<ClearCountingContext*>(outputSurface->Context3D());
scoped_ptr<ResourceProvider> resourceProvider(ResourceProvider::create(outputSurface.get()));
FakeRendererGL renderer(&mockClient, outputSurface.get(), resourceProvider.get());
mockClient.rootRenderPass()->has_transparent_background = false;
EXPECT_TRUE(renderer.initialize());
renderer.drawFrame(mockClient.renderPassesInDrawOrder());
// On DEBUG builds, render passes with opaque background clear to blue to
// easily see regions that were not drawn on the screen.
#ifdef NDEBUG
EXPECT_EQ(0, context->clearCount());
#else
EXPECT_EQ(1, context->clearCount());
#endif
}
TEST(GLRendererTest2, transparentBackground)
{
FakeRendererClient mockClient;
scoped_ptr<OutputSurface> outputSurface(FakeOutputSurface::Create3d(scoped_ptr<WebKit::WebGraphicsContext3D>(new ClearCountingContext)));
ClearCountingContext* context = static_cast<ClearCountingContext*>(outputSurface->Context3D());
scoped_ptr<ResourceProvider> resourceProvider(ResourceProvider::create(outputSurface.get()));
FakeRendererGL renderer(&mockClient, outputSurface.get(), resourceProvider.get());
mockClient.rootRenderPass()->has_transparent_background = true;
EXPECT_TRUE(renderer.initialize());
renderer.drawFrame(mockClient.renderPassesInDrawOrder());
EXPECT_EQ(1, context->clearCount());
}
class VisibilityChangeIsLastCallTrackingContext : public FakeWebGraphicsContext3D {
public:
VisibilityChangeIsLastCallTrackingContext()
: m_lastCallWasSetVisibility(0)
{
}
// WebGraphicsContext3D methods.
virtual void setVisibilityCHROMIUM(bool visible) {
if (!m_lastCallWasSetVisibility)
return;
DCHECK(*m_lastCallWasSetVisibility == false);
*m_lastCallWasSetVisibility = true;
}
virtual void flush() { if (m_lastCallWasSetVisibility) *m_lastCallWasSetVisibility = false; }
virtual void deleteTexture(WebGLId) { if (m_lastCallWasSetVisibility) *m_lastCallWasSetVisibility = false; }
virtual void deleteFramebuffer(WebGLId) { if (m_lastCallWasSetVisibility) *m_lastCallWasSetVisibility = false; }
virtual void deleteRenderbuffer(WebGLId) { if (m_lastCallWasSetVisibility) *m_lastCallWasSetVisibility = false; }
// This method would normally do a glSwapBuffers under the hood.
virtual WebString getString(WebKit::WGC3Denum name)
{
if (name == GL_EXTENSIONS)
return WebString("GL_CHROMIUM_set_visibility GL_CHROMIUM_gpu_memory_manager GL_CHROMIUM_discard_backbuffer");
return WebString();
}
// Methods added for test.
void setLastCallWasSetVisibilityPointer(bool* lastCallWasSetVisibility) { m_lastCallWasSetVisibility = lastCallWasSetVisibility; }
private:
bool* m_lastCallWasSetVisibility;
};
TEST(GLRendererTest2, visibilityChangeIsLastCall)
{
FakeRendererClient mockClient;
scoped_ptr<OutputSurface> outputSurface(FakeOutputSurface::Create3d(scoped_ptr<WebKit::WebGraphicsContext3D>(new VisibilityChangeIsLastCallTrackingContext)));
VisibilityChangeIsLastCallTrackingContext* context = static_cast<VisibilityChangeIsLastCallTrackingContext*>(outputSurface->Context3D());
scoped_ptr<ResourceProvider> resourceProvider(ResourceProvider::create(outputSurface.get()));
FakeRendererGL renderer(&mockClient, outputSurface.get(), resourceProvider.get());
EXPECT_TRUE(renderer.initialize());
bool lastCallWasSetVisiblity = false;
// Ensure that the call to setVisibilityCHROMIUM is the last call issue to the GPU
// process, after glFlush is called, and after the RendererClient's enforceManagedMemoryPolicy
// is called. Plumb this tracking between both the RenderClient and the Context by giving
// them both a pointer to a variable on the stack.
context->setLastCallWasSetVisibilityPointer(&lastCallWasSetVisiblity);
mockClient.setLastCallWasSetVisibilityPointer(&lastCallWasSetVisiblity);
renderer.setVisible(true);
renderer.drawFrame(mockClient.renderPassesInDrawOrder());
renderer.setVisible(false);
EXPECT_TRUE(lastCallWasSetVisiblity);
}
class TextureStateTrackingContext : public FakeWebGraphicsContext3D {
public:
TextureStateTrackingContext()
: m_activeTexture(GL_INVALID_ENUM)
{
}
virtual WebString getString(WGC3Denum name)
{
if (name == GL_EXTENSIONS)
return WebString("GL_OES_EGL_image_external");
return WebString();
}
MOCK_METHOD3(texParameteri, void(WGC3Denum target, WGC3Denum pname, WGC3Dint param));
MOCK_METHOD4(drawElements, void(WGC3Denum mode, WGC3Dsizei count, WGC3Denum type, WGC3Dintptr offset));
virtual void activeTexture(WGC3Denum texture)
{
EXPECT_NE(texture, m_activeTexture);
m_activeTexture = texture;
}
WGC3Denum activeTexture() const { return m_activeTexture; }
private:
WGC3Denum m_activeTexture;
};
TEST(GLRendererTest2, activeTextureState)
{
FakeRendererClient fakeClient;
scoped_ptr<OutputSurface> outputSurface(FakeOutputSurface::Create3d(scoped_ptr<WebKit::WebGraphicsContext3D>(new TextureStateTrackingContext)));
TextureStateTrackingContext* context = static_cast<TextureStateTrackingContext*>(outputSurface->Context3D());
scoped_ptr<ResourceProvider> resourceProvider(ResourceProvider::create(outputSurface.get()));
FakeRendererGL renderer(&fakeClient, outputSurface.get(), resourceProvider.get());
// During initialization we are allowed to set any texture parameters.
EXPECT_CALL(*context, texParameteri(_, _, _)).Times(AnyNumber());
EXPECT_TRUE(renderer.initialize());
cc::RenderPass::Id id(1, 1);
scoped_ptr<TestRenderPass> pass = TestRenderPass::Create();
pass->SetNew(id, gfx::Rect(0, 0, 100, 100), gfx::Rect(0, 0, 100, 100), gfx::Transform());
pass->AppendOneOfEveryQuadType(resourceProvider.get());
// Set up expected texture filter state transitions that match the quads
// created in AppendOneOfEveryQuadType().
Mock::VerifyAndClearExpectations(context);
{
InSequence sequence;
// yuv_quad is drawn with the default filter.
EXPECT_CALL(*context, drawElements(_, _, _, _));
// tile_quad is drawn with GL_NEAREST because it is not transformed or
// scaled.
EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
EXPECT_CALL(*context, drawElements(_, _, _, _));
// transformed_tile_quad uses GL_LINEAR.
EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
EXPECT_CALL(*context, drawElements(_, _, _, _));
// scaled_tile_quad also uses GL_LINEAR.
EXPECT_CALL(*context, drawElements(_, _, _, _));
// The remaining quads also use GL_LINEAR because nearest neighbor
// filtering is currently only used with tile quads.
EXPECT_CALL(*context, drawElements(_, _, _, _)).Times(6);
}
cc::DirectRenderer::DrawingFrame drawingFrame;
renderer.beginDrawingFrame(drawingFrame);
EXPECT_EQ(context->activeTexture(), GL_TEXTURE0);
for (cc::QuadList::backToFrontIterator it = pass->quad_list.backToFrontBegin();
it != pass->quad_list.backToFrontEnd(); ++it) {
renderer.drawQuad(drawingFrame, *it);
}
renderer.finishDrawingQuadList();
EXPECT_EQ(context->activeTexture(), GL_TEXTURE0);
Mock::VerifyAndClearExpectations(context);
}
class NoClearRootRenderPassFakeClient : public FakeRendererClient {
public:
virtual bool shouldClearRootRenderPass() const { return false; }
};
class NoClearRootRenderPassMockContext : public FakeWebGraphicsContext3D {
public:
MOCK_METHOD1(clear, void(WGC3Dbitfield mask));
MOCK_METHOD4(drawElements, void(WGC3Denum mode, WGC3Dsizei count, WGC3Denum type, WGC3Dintptr offset));
};
TEST(GLRendererTest2, shouldClearRootRenderPass)
{
NoClearRootRenderPassFakeClient mockClient;
scoped_ptr<OutputSurface> outputSurface(FakeOutputSurface::Create3d(scoped_ptr<WebKit::WebGraphicsContext3D>(new NoClearRootRenderPassMockContext)));
NoClearRootRenderPassMockContext* mockContext = static_cast<NoClearRootRenderPassMockContext*>(outputSurface->Context3D());
scoped_ptr<ResourceProvider> resourceProvider(ResourceProvider::create(outputSurface.get()));
FakeRendererGL renderer(&mockClient, outputSurface.get(), resourceProvider.get());
EXPECT_TRUE(renderer.initialize());
gfx::Rect viewportRect(mockClient.deviceViewportSize());
ScopedPtrVector<RenderPass>& renderPasses = mockClient.renderPassesInDrawOrder();
renderPasses.clear();
RenderPass::Id rootPassId(1, 0);
TestRenderPass* rootPass = addRenderPass(renderPasses, rootPassId, viewportRect, gfx::Transform());
addQuad(rootPass, viewportRect, SK_ColorGREEN);
RenderPass::Id childPassId(2, 0);
TestRenderPass* childPass = addRenderPass(renderPasses, childPassId, viewportRect, gfx::Transform());
addQuad(childPass, viewportRect, SK_ColorBLUE);
addRenderPassQuad(rootPass, childPass);
// First render pass is not the root one, clearing should happen.
EXPECT_CALL(*mockContext, clear(GL_COLOR_BUFFER_BIT))
.Times(AtLeast(1));
Expectation firstRenderPass = EXPECT_CALL(*mockContext, drawElements(_, _, _, _))
.Times(1);
// The second render pass is the root one, clearing should be prevented.
EXPECT_CALL(*mockContext, clear(GL_COLOR_BUFFER_BIT))
.Times(0)
.After(firstRenderPass);
EXPECT_CALL(*mockContext, drawElements(_, _, _, _))
.Times(AnyNumber())
.After(firstRenderPass);
renderer.decideRenderPassAllocationsForFrame(mockClient.renderPassesInDrawOrder());
renderer.drawFrame(mockClient.renderPassesInDrawOrder());
// In multiple render passes all but the root pass should clear the framebuffer.
Mock::VerifyAndClearExpectations(&mockContext);
}
} // namespace
} // namespace cc
|