forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl_renderer_unittest.cc
1859 lines (1585 loc) · 68.3 KB
/
gl_renderer_unittest.cc
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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/output/gl_renderer.h"
#include <set>
#include "cc/base/math_util.h"
#include "cc/output/compositor_frame_metadata.h"
#include "cc/resources/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_output_surface_client.h"
#include "cc/test/fake_renderer_client.h"
#include "cc/test/pixel_test.h"
#include "cc/test/render_pass_test_common.h"
#include "cc/test/render_pass_test_utils.h"
#include "cc/test/test_shared_bitmap_manager.h"
#include "cc/test/test_web_graphics_context_3d.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "gpu/command_buffer/client/context_support.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkImageFilter.h"
#include "third_party/skia/include/core/SkMatrix.h"
#include "third_party/skia/include/effects/SkColorFilterImageFilter.h"
#include "third_party/skia/include/effects/SkColorMatrixFilter.h"
#include "ui/gfx/transform.h"
using testing::_;
using testing::AnyNumber;
using testing::Args;
using testing::AtLeast;
using testing::ElementsAre;
using testing::Expectation;
using testing::InSequence;
using testing::Mock;
using testing::Return;
using testing::StrictMock;
namespace cc {
class GLRendererTest : public testing::Test {
protected:
RenderPass* root_render_pass() { return render_passes_in_draw_order_.back(); }
RenderPassList render_passes_in_draw_order_;
};
#define EXPECT_PROGRAM_VALID(program_binding) \
do { \
EXPECT_TRUE((program_binding)->program()); \
EXPECT_TRUE((program_binding)->initialized()); \
} while (false)
// Explicitly named to be a friend in GLRenderer for shader access.
class GLRendererShaderPixelTest : public GLRendererPixelTest {
public:
void TestShaders() {
ASSERT_FALSE(renderer()->IsContextLost());
EXPECT_PROGRAM_VALID(renderer()->GetTileCheckerboardProgram());
EXPECT_PROGRAM_VALID(renderer()->GetDebugBorderProgram());
EXPECT_PROGRAM_VALID(renderer()->GetSolidColorProgram());
EXPECT_PROGRAM_VALID(renderer()->GetSolidColorProgramAA());
TestShadersWithTexCoordPrecision(TexCoordPrecisionMedium);
TestShadersWithTexCoordPrecision(TexCoordPrecisionHigh);
ASSERT_FALSE(renderer()->IsContextLost());
}
void TestShadersWithTexCoordPrecision(TexCoordPrecision precision) {
EXPECT_PROGRAM_VALID(renderer()->GetRenderPassProgram(precision));
EXPECT_PROGRAM_VALID(renderer()->GetRenderPassProgramAA(precision));
EXPECT_PROGRAM_VALID(renderer()->GetRenderPassMaskProgram(precision));
EXPECT_PROGRAM_VALID(renderer()->GetRenderPassMaskProgramAA(precision));
EXPECT_PROGRAM_VALID(
renderer()->GetRenderPassColorMatrixProgram(precision));
EXPECT_PROGRAM_VALID(
renderer()->GetRenderPassMaskColorMatrixProgramAA(precision));
EXPECT_PROGRAM_VALID(
renderer()->GetRenderPassColorMatrixProgramAA(precision));
EXPECT_PROGRAM_VALID(
renderer()->GetRenderPassMaskColorMatrixProgram(precision));
EXPECT_PROGRAM_VALID(renderer()->GetTextureProgram(precision));
EXPECT_PROGRAM_VALID(
renderer()->GetNonPremultipliedTextureProgram(precision));
EXPECT_PROGRAM_VALID(renderer()->GetTextureBackgroundProgram(precision));
EXPECT_PROGRAM_VALID(
renderer()->GetNonPremultipliedTextureBackgroundProgram(precision));
EXPECT_PROGRAM_VALID(renderer()->GetTextureIOSurfaceProgram(precision));
EXPECT_PROGRAM_VALID(renderer()->GetVideoYUVProgram(precision));
EXPECT_PROGRAM_VALID(renderer()->GetVideoYUVAProgram(precision));
// This is unlikely to be ever true in tests due to usage of osmesa.
if (renderer()->Capabilities().using_egl_image)
EXPECT_PROGRAM_VALID(renderer()->GetVideoStreamTextureProgram(precision));
else
EXPECT_FALSE(renderer()->GetVideoStreamTextureProgram(precision));
TestShadersWithSamplerType(precision, SamplerType2D);
TestShadersWithSamplerType(precision, SamplerType2DRect);
// This is unlikely to be ever true in tests due to usage of osmesa.
if (renderer()->Capabilities().using_egl_image)
TestShadersWithSamplerType(precision, SamplerTypeExternalOES);
}
void TestShadersWithSamplerType(TexCoordPrecision precision,
SamplerType sampler) {
EXPECT_PROGRAM_VALID(renderer()->GetTileProgram(precision, sampler));
EXPECT_PROGRAM_VALID(renderer()->GetTileProgramOpaque(precision, sampler));
EXPECT_PROGRAM_VALID(renderer()->GetTileProgramAA(precision, sampler));
EXPECT_PROGRAM_VALID(renderer()->GetTileProgramSwizzle(precision, sampler));
EXPECT_PROGRAM_VALID(
renderer()->GetTileProgramSwizzleOpaque(precision, sampler));
EXPECT_PROGRAM_VALID(
renderer()->GetTileProgramSwizzleAA(precision, sampler));
}
};
namespace {
#if !defined(OS_ANDROID)
TEST_F(GLRendererShaderPixelTest, AllShadersCompile) { TestShaders(); }
#endif
class FakeRendererGL : public GLRenderer {
public:
FakeRendererGL(RendererClient* client,
const LayerTreeSettings* settings,
OutputSurface* output_surface,
ResourceProvider* resource_provider)
: GLRenderer(client,
settings,
output_surface,
resource_provider,
NULL,
0) {}
// GLRenderer methods.
// Changing visibility to public.
using GLRenderer::IsBackbufferDiscarded;
using GLRenderer::DoDrawQuad;
using GLRenderer::BeginDrawingFrame;
using GLRenderer::FinishDrawingQuadList;
using GLRenderer::stencil_enabled;
};
class GLRendererWithDefaultHarnessTest : public GLRendererTest {
protected:
GLRendererWithDefaultHarnessTest() {
output_surface_ =
FakeOutputSurface::Create3d(TestWebGraphicsContext3D::Create()).Pass();
CHECK(output_surface_->BindToClient(&output_surface_client_));
shared_bitmap_manager_.reset(new TestSharedBitmapManager());
resource_provider_ = ResourceProvider::Create(output_surface_.get(),
shared_bitmap_manager_.get(),
NULL,
NULL,
0,
false,
1,
false).Pass();
renderer_ = make_scoped_ptr(new FakeRendererGL(&renderer_client_,
&settings_,
output_surface_.get(),
resource_provider_.get()));
}
void SwapBuffers() { renderer_->SwapBuffers(CompositorFrameMetadata()); }
LayerTreeSettings settings_;
FakeOutputSurfaceClient output_surface_client_;
scoped_ptr<FakeOutputSurface> output_surface_;
FakeRendererClient renderer_client_;
scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
scoped_ptr<ResourceProvider> resource_provider_;
scoped_ptr<FakeRendererGL> renderer_;
};
// Closing the namespace here so that GLRendererShaderTest can take advantage
// of the friend relationship with GLRenderer and all of the mock classes
// declared above it.
} // namespace
class GLRendererShaderTest : public GLRendererTest {
protected:
GLRendererShaderTest() {
output_surface_ = FakeOutputSurface::Create3d().Pass();
CHECK(output_surface_->BindToClient(&output_surface_client_));
shared_bitmap_manager_.reset(new TestSharedBitmapManager());
resource_provider_ = ResourceProvider::Create(output_surface_.get(),
shared_bitmap_manager_.get(),
NULL,
NULL,
0,
false,
1,
false).Pass();
renderer_.reset(new FakeRendererGL(&renderer_client_,
&settings_,
output_surface_.get(),
resource_provider_.get()));
}
void TestRenderPassProgram(TexCoordPrecision precision) {
EXPECT_PROGRAM_VALID(&renderer_->render_pass_program_[precision]);
EXPECT_EQ(renderer_->render_pass_program_[precision].program(),
renderer_->program_shadow_);
}
void TestRenderPassColorMatrixProgram(TexCoordPrecision precision) {
EXPECT_PROGRAM_VALID(
&renderer_->render_pass_color_matrix_program_[precision]);
EXPECT_EQ(renderer_->render_pass_color_matrix_program_[precision].program(),
renderer_->program_shadow_);
}
void TestRenderPassMaskProgram(TexCoordPrecision precision) {
EXPECT_PROGRAM_VALID(&renderer_->render_pass_mask_program_[precision]);
EXPECT_EQ(renderer_->render_pass_mask_program_[precision].program(),
renderer_->program_shadow_);
}
void TestRenderPassMaskColorMatrixProgram(TexCoordPrecision precision) {
EXPECT_PROGRAM_VALID(
&renderer_->render_pass_mask_color_matrix_program_[precision]);
EXPECT_EQ(
renderer_->render_pass_mask_color_matrix_program_[precision].program(),
renderer_->program_shadow_);
}
void TestRenderPassProgramAA(TexCoordPrecision precision) {
EXPECT_PROGRAM_VALID(&renderer_->render_pass_program_aa_[precision]);
EXPECT_EQ(renderer_->render_pass_program_aa_[precision].program(),
renderer_->program_shadow_);
}
void TestRenderPassColorMatrixProgramAA(TexCoordPrecision precision) {
EXPECT_PROGRAM_VALID(
&renderer_->render_pass_color_matrix_program_aa_[precision]);
EXPECT_EQ(
renderer_->render_pass_color_matrix_program_aa_[precision].program(),
renderer_->program_shadow_);
}
void TestRenderPassMaskProgramAA(TexCoordPrecision precision) {
EXPECT_PROGRAM_VALID(&renderer_->render_pass_mask_program_aa_[precision]);
EXPECT_EQ(renderer_->render_pass_mask_program_aa_[precision].program(),
renderer_->program_shadow_);
}
void TestRenderPassMaskColorMatrixProgramAA(TexCoordPrecision precision) {
EXPECT_PROGRAM_VALID(
&renderer_->render_pass_mask_color_matrix_program_aa_[precision]);
EXPECT_EQ(renderer_->render_pass_mask_color_matrix_program_aa_[precision]
.program(),
renderer_->program_shadow_);
}
void TestSolidColorProgramAA() {
EXPECT_PROGRAM_VALID(&renderer_->solid_color_program_aa_);
EXPECT_EQ(renderer_->solid_color_program_aa_.program(),
renderer_->program_shadow_);
}
LayerTreeSettings settings_;
FakeOutputSurfaceClient output_surface_client_;
scoped_ptr<FakeOutputSurface> output_surface_;
FakeRendererClient renderer_client_;
scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
scoped_ptr<ResourceProvider> resource_provider_;
scoped_ptr<FakeRendererGL> renderer_;
};
namespace {
// 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(
GLRendererWithDefaultHarnessTest,
SuggestBackbufferNoShouldDiscardBackbufferAndDamageRootLayerIfNotVisible) {
renderer_->SetVisible(false);
EXPECT_EQ(1, renderer_client_.set_full_root_layer_damage_count());
EXPECT_TRUE(renderer_->IsBackbufferDiscarded());
}
// Test GLRenderer DiscardBackbuffer functionality:
// Suggest discarding framebuffer when one exists and the renderer is visible.
// Expected: the allocation is ignored.
TEST_F(GLRendererWithDefaultHarnessTest,
SuggestBackbufferNoDoNothingWhenVisible) {
renderer_->SetVisible(true);
EXPECT_EQ(0, renderer_client_.set_full_root_layer_damage_count());
EXPECT_FALSE(renderer_->IsBackbufferDiscarded());
}
// Test GLRenderer DiscardBackbuffer functionality:
// Suggest discarding framebuffer when one does not exist.
// Expected: it does nothing.
TEST_F(GLRendererWithDefaultHarnessTest,
SuggestBackbufferNoWhenItDoesntExistShouldDoNothing) {
renderer_->SetVisible(false);
EXPECT_EQ(1, renderer_client_.set_full_root_layer_damage_count());
EXPECT_TRUE(renderer_->IsBackbufferDiscarded());
EXPECT_EQ(1, renderer_client_.set_full_root_layer_damage_count());
EXPECT_TRUE(renderer_->IsBackbufferDiscarded());
}
// Test GLRenderer DiscardBackbuffer functionality:
// Begin drawing a frame while a framebuffer is discarded.
// Expected: will recreate framebuffer.
TEST_F(GLRendererWithDefaultHarnessTest,
DiscardedBackbufferIsRecreatedForScopeDuration) {
gfx::Rect viewport_rect(1, 1);
renderer_->SetVisible(false);
EXPECT_TRUE(renderer_->IsBackbufferDiscarded());
EXPECT_EQ(1, renderer_client_.set_full_root_layer_damage_count());
AddRenderPass(&render_passes_in_draw_order_,
RenderPassId(1, 0),
viewport_rect,
gfx::Transform());
renderer_->SetVisible(true);
renderer_->DrawFrame(&render_passes_in_draw_order_,
1.f,
viewport_rect,
viewport_rect,
false);
EXPECT_FALSE(renderer_->IsBackbufferDiscarded());
SwapBuffers();
EXPECT_EQ(1u, output_surface_->num_sent_frames());
}
TEST_F(GLRendererWithDefaultHarnessTest, ExternalStencil) {
gfx::Rect viewport_rect(1, 1);
EXPECT_FALSE(renderer_->stencil_enabled());
output_surface_->set_has_external_stencil_test(true);
TestRenderPass* root_pass = AddRenderPass(&render_passes_in_draw_order_,
RenderPassId(1, 0),
viewport_rect,
gfx::Transform());
root_pass->has_transparent_background = false;
renderer_->DrawFrame(&render_passes_in_draw_order_,
1.f,
viewport_rect,
viewport_rect,
false);
EXPECT_TRUE(renderer_->stencil_enabled());
}
class ForbidSynchronousCallContext : public TestWebGraphicsContext3D {
public:
ForbidSynchronousCallContext() {}
virtual void getAttachedShaders(GLuint program,
GLsizei max_count,
GLsizei* count,
GLuint* shaders) override {
ADD_FAILURE();
}
virtual GLint getAttribLocation(GLuint program, const GLchar* name) override {
ADD_FAILURE();
return 0;
}
virtual void getBooleanv(GLenum pname, GLboolean* value) override {
ADD_FAILURE();
}
virtual void getBufferParameteriv(GLenum target,
GLenum pname,
GLint* value) override {
ADD_FAILURE();
}
virtual GLenum getError() override {
ADD_FAILURE();
return GL_NO_ERROR;
}
virtual void getFloatv(GLenum pname, GLfloat* value) override {
ADD_FAILURE();
}
virtual void getFramebufferAttachmentParameteriv(GLenum target,
GLenum attachment,
GLenum pname,
GLint* value) override {
ADD_FAILURE();
}
virtual void getIntegerv(GLenum pname, GLint* value) override {
if (pname == GL_MAX_TEXTURE_SIZE) {
// MAX_TEXTURE_SIZE is cached client side, so it's OK to query.
*value = 1024;
} else {
ADD_FAILURE();
}
}
// We allow querying the shader compilation and program link status in debug
// mode, but not release.
virtual void getProgramiv(GLuint program,
GLenum pname,
GLint* value) override {
#ifndef NDEBUG
*value = 1;
#else
ADD_FAILURE();
#endif
}
virtual void getShaderiv(GLuint shader, GLenum pname, GLint* value) override {
#ifndef NDEBUG
*value = 1;
#else
ADD_FAILURE();
#endif
}
virtual void getRenderbufferParameteriv(GLenum target,
GLenum pname,
GLint* value) override {
ADD_FAILURE();
}
virtual void getShaderPrecisionFormat(GLenum shadertype,
GLenum precisiontype,
GLint* range,
GLint* precision) override {
ADD_FAILURE();
}
virtual void getTexParameterfv(GLenum target,
GLenum pname,
GLfloat* value) override {
ADD_FAILURE();
}
virtual void getTexParameteriv(GLenum target,
GLenum pname,
GLint* value) override {
ADD_FAILURE();
}
virtual void getUniformfv(GLuint program,
GLint location,
GLfloat* value) override {
ADD_FAILURE();
}
virtual void getUniformiv(GLuint program,
GLint location,
GLint* value) override {
ADD_FAILURE();
}
virtual GLint getUniformLocation(GLuint program,
const GLchar* name) override {
ADD_FAILURE();
return 0;
}
virtual void getVertexAttribfv(GLuint index,
GLenum pname,
GLfloat* value) override {
ADD_FAILURE();
}
virtual void getVertexAttribiv(GLuint index,
GLenum pname,
GLint* value) override {
ADD_FAILURE();
}
virtual GLsizeiptr getVertexAttribOffset(GLuint index,
GLenum pname) override {
ADD_FAILURE();
return 0;
}
};
TEST_F(GLRendererTest, InitializationDoesNotMakeSynchronousCalls) {
FakeOutputSurfaceClient output_surface_client;
scoped_ptr<OutputSurface> output_surface(FakeOutputSurface::Create3d(
scoped_ptr<TestWebGraphicsContext3D>(new ForbidSynchronousCallContext)));
CHECK(output_surface->BindToClient(&output_surface_client));
scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
new TestSharedBitmapManager());
scoped_ptr<ResourceProvider> resource_provider(
ResourceProvider::Create(output_surface.get(),
shared_bitmap_manager.get(),
NULL,
NULL,
0,
false,
1,
false));
LayerTreeSettings settings;
FakeRendererClient renderer_client;
FakeRendererGL renderer(&renderer_client,
&settings,
output_surface.get(),
resource_provider.get());
}
class LoseContextOnFirstGetContext : public TestWebGraphicsContext3D {
public:
LoseContextOnFirstGetContext() {}
virtual void getProgramiv(GLuint program,
GLenum pname,
GLint* value) override {
context_lost_ = true;
*value = 0;
}
virtual void getShaderiv(GLuint shader, GLenum pname, GLint* value) override {
context_lost_ = true;
*value = 0;
}
};
TEST_F(GLRendererTest, InitializationWithQuicklyLostContextDoesNotAssert) {
FakeOutputSurfaceClient output_surface_client;
scoped_ptr<OutputSurface> output_surface(FakeOutputSurface::Create3d(
scoped_ptr<TestWebGraphicsContext3D>(new LoseContextOnFirstGetContext)));
CHECK(output_surface->BindToClient(&output_surface_client));
scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
new TestSharedBitmapManager());
scoped_ptr<ResourceProvider> resource_provider(
ResourceProvider::Create(output_surface.get(),
shared_bitmap_manager.get(),
NULL,
NULL,
0,
false,
1,
false));
LayerTreeSettings settings;
FakeRendererClient renderer_client;
FakeRendererGL renderer(&renderer_client,
&settings,
output_surface.get(),
resource_provider.get());
}
class ClearCountingContext : public TestWebGraphicsContext3D {
public:
ClearCountingContext() { test_capabilities_.gpu.discard_framebuffer = true; }
MOCK_METHOD3(discardFramebufferEXT,
void(GLenum target,
GLsizei numAttachments,
const GLenum* attachments));
MOCK_METHOD1(clear, void(GLbitfield mask));
};
TEST_F(GLRendererTest, OpaqueBackground) {
scoped_ptr<ClearCountingContext> context_owned(new ClearCountingContext);
ClearCountingContext* context = context_owned.get();
FakeOutputSurfaceClient output_surface_client;
scoped_ptr<OutputSurface> output_surface(
FakeOutputSurface::Create3d(context_owned.Pass()));
CHECK(output_surface->BindToClient(&output_surface_client));
scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
new TestSharedBitmapManager());
scoped_ptr<ResourceProvider> resource_provider(
ResourceProvider::Create(output_surface.get(),
shared_bitmap_manager.get(),
NULL,
NULL,
0,
false,
1,
false));
LayerTreeSettings settings;
FakeRendererClient renderer_client;
FakeRendererGL renderer(&renderer_client,
&settings,
output_surface.get(),
resource_provider.get());
gfx::Rect viewport_rect(1, 1);
TestRenderPass* root_pass = AddRenderPass(&render_passes_in_draw_order_,
RenderPassId(1, 0),
viewport_rect,
gfx::Transform());
root_pass->has_transparent_background = false;
// On DEBUG builds, render passes with opaque background clear to blue to
// easily see regions that were not drawn on the screen.
EXPECT_CALL(*context, discardFramebufferEXT(GL_FRAMEBUFFER, _, _))
.With(Args<2, 1>(ElementsAre(GL_COLOR_EXT)))
.Times(1);
#ifdef NDEBUG
EXPECT_CALL(*context, clear(_)).Times(0);
#else
EXPECT_CALL(*context, clear(_)).Times(1);
#endif
renderer.DrawFrame(&render_passes_in_draw_order_,
1.f,
viewport_rect,
viewport_rect,
false);
Mock::VerifyAndClearExpectations(context);
}
TEST_F(GLRendererTest, TransparentBackground) {
scoped_ptr<ClearCountingContext> context_owned(new ClearCountingContext);
ClearCountingContext* context = context_owned.get();
FakeOutputSurfaceClient output_surface_client;
scoped_ptr<OutputSurface> output_surface(
FakeOutputSurface::Create3d(context_owned.Pass()));
CHECK(output_surface->BindToClient(&output_surface_client));
scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
new TestSharedBitmapManager());
scoped_ptr<ResourceProvider> resource_provider(
ResourceProvider::Create(output_surface.get(),
shared_bitmap_manager.get(),
NULL,
NULL,
0,
false,
1,
false));
LayerTreeSettings settings;
FakeRendererClient renderer_client;
FakeRendererGL renderer(&renderer_client,
&settings,
output_surface.get(),
resource_provider.get());
gfx::Rect viewport_rect(1, 1);
TestRenderPass* root_pass = AddRenderPass(&render_passes_in_draw_order_,
RenderPassId(1, 0),
viewport_rect,
gfx::Transform());
root_pass->has_transparent_background = true;
EXPECT_CALL(*context, discardFramebufferEXT(GL_FRAMEBUFFER, 1, _)).Times(1);
EXPECT_CALL(*context, clear(_)).Times(1);
renderer.DrawFrame(&render_passes_in_draw_order_,
1.f,
viewport_rect,
viewport_rect,
false);
Mock::VerifyAndClearExpectations(context);
}
TEST_F(GLRendererTest, OffscreenOutputSurface) {
scoped_ptr<ClearCountingContext> context_owned(new ClearCountingContext);
ClearCountingContext* context = context_owned.get();
FakeOutputSurfaceClient output_surface_client;
scoped_ptr<OutputSurface> output_surface(
FakeOutputSurface::CreateOffscreen(context_owned.Pass()));
CHECK(output_surface->BindToClient(&output_surface_client));
scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
new TestSharedBitmapManager());
scoped_ptr<ResourceProvider> resource_provider(
ResourceProvider::Create(output_surface.get(),
shared_bitmap_manager.get(),
NULL,
NULL,
0,
false,
1,
false));
LayerTreeSettings settings;
FakeRendererClient renderer_client;
FakeRendererGL renderer(&renderer_client,
&settings,
output_surface.get(),
resource_provider.get());
gfx::Rect viewport_rect(1, 1);
AddRenderPass(&render_passes_in_draw_order_,
RenderPassId(1, 0),
viewport_rect,
gfx::Transform());
EXPECT_CALL(*context, discardFramebufferEXT(GL_FRAMEBUFFER, _, _))
.With(Args<2, 1>(ElementsAre(GL_COLOR_ATTACHMENT0)))
.Times(1);
EXPECT_CALL(*context, clear(_)).Times(AnyNumber());
renderer.DrawFrame(&render_passes_in_draw_order_,
1.f,
viewport_rect,
viewport_rect,
false);
Mock::VerifyAndClearExpectations(context);
}
class VisibilityChangeIsLastCallTrackingContext
: public TestWebGraphicsContext3D {
public:
VisibilityChangeIsLastCallTrackingContext()
: last_call_was_set_visibility_(false) {}
// TestWebGraphicsContext3D methods.
virtual void flush() override { last_call_was_set_visibility_ = false; }
virtual void deleteTexture(GLuint) override {
last_call_was_set_visibility_ = false;
}
virtual void deleteFramebuffer(GLuint) override {
last_call_was_set_visibility_ = false;
}
virtual void deleteQueryEXT(GLuint) override {
last_call_was_set_visibility_ = false;
}
virtual void deleteRenderbuffer(GLuint) override {
last_call_was_set_visibility_ = false;
}
// Methods added for test.
void set_last_call_was_visibility(bool visible) {
DCHECK(last_call_was_set_visibility_ == false);
last_call_was_set_visibility_ = true;
}
bool last_call_was_set_visibility() const {
return last_call_was_set_visibility_;
}
private:
bool last_call_was_set_visibility_;
};
TEST_F(GLRendererTest, VisibilityChangeIsLastCall) {
scoped_ptr<VisibilityChangeIsLastCallTrackingContext> context_owned(
new VisibilityChangeIsLastCallTrackingContext);
VisibilityChangeIsLastCallTrackingContext* context = context_owned.get();
scoped_refptr<TestContextProvider> provider =
TestContextProvider::Create(context_owned.Pass());
provider->support()->SetSurfaceVisibleCallback(base::Bind(
&VisibilityChangeIsLastCallTrackingContext::set_last_call_was_visibility,
base::Unretained(context)));
FakeOutputSurfaceClient output_surface_client;
scoped_ptr<OutputSurface> output_surface(
FakeOutputSurface::Create3d(provider));
CHECK(output_surface->BindToClient(&output_surface_client));
scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
new TestSharedBitmapManager());
scoped_ptr<ResourceProvider> resource_provider(
ResourceProvider::Create(output_surface.get(),
shared_bitmap_manager.get(),
NULL,
NULL,
0,
false,
1,
false));
LayerTreeSettings settings;
FakeRendererClient renderer_client;
FakeRendererGL renderer(&renderer_client,
&settings,
output_surface.get(),
resource_provider.get());
gfx::Rect viewport_rect(1, 1);
AddRenderPass(&render_passes_in_draw_order_,
RenderPassId(1, 0),
viewport_rect,
gfx::Transform());
// Ensure that the call to SetSurfaceVisible is the last call issue to the
// GPU process, after glFlush is called, and after the RendererClient's
// SetManagedMemoryPolicy is called. Plumb this tracking between both the
// RenderClient and the Context by giving them both a pointer to a variable on
// the stack.
renderer.SetVisible(true);
renderer.DrawFrame(&render_passes_in_draw_order_,
1.f,
viewport_rect,
viewport_rect,
false);
renderer.SetVisible(false);
EXPECT_TRUE(context->last_call_was_set_visibility());
}
class TextureStateTrackingContext : public TestWebGraphicsContext3D {
public:
TextureStateTrackingContext() : active_texture_(GL_INVALID_ENUM) {
test_capabilities_.gpu.egl_image_external = true;
}
MOCK_METHOD1(waitSyncPoint, void(unsigned sync_point));
MOCK_METHOD3(texParameteri, void(GLenum target, GLenum pname, GLint param));
MOCK_METHOD4(drawElements,
void(GLenum mode, GLsizei count, GLenum type, GLintptr offset));
virtual void activeTexture(GLenum texture) {
EXPECT_NE(texture, active_texture_);
active_texture_ = texture;
}
GLenum active_texture() const { return active_texture_; }
private:
GLenum active_texture_;
};
TEST_F(GLRendererTest, ActiveTextureState) {
scoped_ptr<TextureStateTrackingContext> context_owned(
new TextureStateTrackingContext);
TextureStateTrackingContext* context = context_owned.get();
FakeOutputSurfaceClient output_surface_client;
scoped_ptr<OutputSurface> output_surface(
FakeOutputSurface::Create3d(context_owned.Pass()));
CHECK(output_surface->BindToClient(&output_surface_client));
scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
new TestSharedBitmapManager());
scoped_ptr<ResourceProvider> resource_provider(
ResourceProvider::Create(output_surface.get(),
shared_bitmap_manager.get(),
NULL,
NULL,
0,
false,
1,
false));
LayerTreeSettings settings;
FakeRendererClient renderer_client;
FakeRendererGL renderer(&renderer_client,
&settings,
output_surface.get(),
resource_provider.get());
// During initialization we are allowed to set any texture parameters.
EXPECT_CALL(*context, texParameteri(_, _, _)).Times(AnyNumber());
RenderPassId id(1, 1);
TestRenderPass* root_pass = AddRenderPass(
&render_passes_in_draw_order_, id, gfx::Rect(100, 100), gfx::Transform());
root_pass->AppendOneOfEveryQuadType(resource_provider.get(),
RenderPassId(2, 1));
renderer.DecideRenderPassAllocationsForFrame(render_passes_in_draw_order_);
// Set up expected texture filter state transitions that match the quads
// created in AppendOneOfEveryQuadType().
Mock::VerifyAndClearExpectations(context);
{
InSequence sequence;
// The sync points for all quads are waited on first. This sync point is
// for a texture quad drawn later in the frame.
EXPECT_CALL(*context,
waitSyncPoint(TestRenderPass::kSyncPointForMailboxTextureQuad))
.Times(1);
// yuv_quad is drawn with the default linear 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, 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(7);
}
gfx::Rect viewport_rect(100, 100);
renderer.DrawFrame(&render_passes_in_draw_order_,
1.f,
viewport_rect,
viewport_rect,
false);
Mock::VerifyAndClearExpectations(context);
}
class NoClearRootRenderPassMockContext : public TestWebGraphicsContext3D {
public:
MOCK_METHOD1(clear, void(GLbitfield mask));
MOCK_METHOD4(drawElements,
void(GLenum mode, GLsizei count, GLenum type, GLintptr offset));
};
TEST_F(GLRendererTest, ShouldClearRootRenderPass) {
scoped_ptr<NoClearRootRenderPassMockContext> mock_context_owned(
new NoClearRootRenderPassMockContext);
NoClearRootRenderPassMockContext* mock_context = mock_context_owned.get();
FakeOutputSurfaceClient output_surface_client;
scoped_ptr<OutputSurface> output_surface(
FakeOutputSurface::Create3d(mock_context_owned.Pass()));
CHECK(output_surface->BindToClient(&output_surface_client));
scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
new TestSharedBitmapManager());
scoped_ptr<ResourceProvider> resource_provider(
ResourceProvider::Create(output_surface.get(),
shared_bitmap_manager.get(),
NULL,
NULL,
0,
false,
1,
false));
LayerTreeSettings settings;
settings.should_clear_root_render_pass = false;
FakeRendererClient renderer_client;
FakeRendererGL renderer(&renderer_client,
&settings,
output_surface.get(),
resource_provider.get());
gfx::Rect viewport_rect(10, 10);
RenderPassId root_pass_id(1, 0);
TestRenderPass* root_pass = AddRenderPass(&render_passes_in_draw_order_,
root_pass_id,
viewport_rect,
gfx::Transform());
AddQuad(root_pass, viewport_rect, SK_ColorGREEN);
RenderPassId child_pass_id(2, 0);
TestRenderPass* child_pass = AddRenderPass(&render_passes_in_draw_order_,
child_pass_id,
viewport_rect,
gfx::Transform());
AddQuad(child_pass, viewport_rect, SK_ColorBLUE);
AddRenderPassQuad(root_pass, child_pass);
#ifdef NDEBUG
GLint clear_bits = GL_COLOR_BUFFER_BIT;
#else
GLint clear_bits = GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
#endif
// First render pass is not the root one, clearing should happen.
EXPECT_CALL(*mock_context, clear(clear_bits)).Times(AtLeast(1));
Expectation first_render_pass =
EXPECT_CALL(*mock_context, drawElements(_, _, _, _)).Times(1);
// The second render pass is the root one, clearing should be prevented.
EXPECT_CALL(*mock_context, clear(clear_bits)).Times(0).After(
first_render_pass);
EXPECT_CALL(*mock_context, drawElements(_, _, _, _)).Times(AnyNumber()).After(
first_render_pass);
renderer.DecideRenderPassAllocationsForFrame(render_passes_in_draw_order_);
renderer.DrawFrame(&render_passes_in_draw_order_,
1.f,
viewport_rect,
viewport_rect,
false);
// In multiple render passes all but the root pass should clear the
// framebuffer.
Mock::VerifyAndClearExpectations(&mock_context);
}
class ScissorTestOnClearCheckingContext : public TestWebGraphicsContext3D {
public:
ScissorTestOnClearCheckingContext() : scissor_enabled_(false) {}
virtual void clear(GLbitfield) override { EXPECT_FALSE(scissor_enabled_); }
virtual void enable(GLenum cap) override {
if (cap == GL_SCISSOR_TEST)
scissor_enabled_ = true;
}
virtual void disable(GLenum cap) override {
if (cap == GL_SCISSOR_TEST)