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
gpu / command_buffer / service / context_state.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "gpu/command_buffer/service/context_state.h"
#include <stddef.h>
#include <algorithm>
#include <cmath>
#include <optional>
#include "gpu/command_buffer/common/gles2_cmd_utils.h"
#include "gpu/command_buffer/service/buffer_manager.h"
#include "gpu/command_buffer/service/framebuffer_manager.h"
#include "gpu/command_buffer/service/program_manager.h"
#include "gpu/command_buffer/service/renderbuffer_manager.h"
#include "gpu/command_buffer/service/transform_feedback_manager.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_version_info.h"
namespace gpu {
namespace gles2 {
namespace {
GLuint Get2dServiceId(const TextureUnit& unit) {
return unit.bound_texture_2d.get() ? unit.bound_texture_2d->service_id() : 0;
}
GLuint Get2dArrayServiceId(const TextureUnit& unit) {
return unit.bound_texture_2d_array.get()
? unit.bound_texture_2d_array->service_id()
: 0;
}
GLuint Get3dServiceId(const TextureUnit& unit) {
return unit.bound_texture_3d.get() ? unit.bound_texture_3d->service_id() : 0;
}
GLuint GetCubeServiceId(const TextureUnit& unit) {
return unit.bound_texture_cube_map.get()
? unit.bound_texture_cube_map->service_id()
: 0;
}
GLuint GetOesServiceId(const TextureUnit& unit) {
return unit.bound_texture_external_oes.get()
? unit.bound_texture_external_oes->service_id()
: 0;
}
GLuint GetArbServiceId(const TextureUnit& unit) {
return unit.bound_texture_rectangle_arb.get()
? unit.bound_texture_rectangle_arb->service_id()
: 0;
}
GLuint GetServiceId(const TextureUnit& unit, GLuint target) {
switch (target) {
case GL_TEXTURE_2D:
return Get2dServiceId(unit);
case GL_TEXTURE_CUBE_MAP:
return GetCubeServiceId(unit);
case GL_TEXTURE_RECTANGLE_ARB:
return GetArbServiceId(unit);
case GL_TEXTURE_EXTERNAL_OES:
return GetOesServiceId(unit);
default:
NOTREACHED();
}
}
bool TargetIsSupported(const FeatureInfo* feature_info, GLuint target) {
switch (target) {
case GL_TEXTURE_2D:
return true;
case GL_TEXTURE_CUBE_MAP:
return true;
case GL_TEXTURE_RECTANGLE_ARB:
return feature_info->feature_flags().arb_texture_rectangle;
case GL_TEXTURE_EXTERNAL_OES:
return feature_info->feature_flags().oes_egl_image_external ||
feature_info->feature_flags().nv_egl_stream_consumer_external;
default:
NOTREACHED();
}
}
GLuint GetBufferId(const Buffer* buffer) {
if (buffer)
return buffer->service_id();
return 0;
}
} // anonymous namespace.
TextureUnit::TextureUnit() : bind_target(GL_TEXTURE_2D) {}
TextureUnit::TextureUnit(const TextureUnit& other) = default;
TextureUnit::~TextureUnit() = default;
bool Vec4::Equal(const Vec4& other) const {
if (type_ != other.type_)
return false;
switch (type_) {
case SHADER_VARIABLE_FLOAT:
for (size_t ii = 0; ii < 4; ++ii) {
if (v_[ii].float_value != other.v_[ii].float_value)
return false;
}
break;
case SHADER_VARIABLE_INT:
for (size_t ii = 0; ii < 4; ++ii) {
if (v_[ii].int_value != other.v_[ii].int_value)
return false;
}
break;
case SHADER_VARIABLE_UINT:
for (size_t ii = 0; ii < 4; ++ii) {
if (v_[ii].uint_value != other.v_[ii].uint_value)
return false;
}
break;
default:
NOTREACHED();
}
return true;
}
template <>
void Vec4::GetValues<GLfloat>(GLfloat* values) const {
DCHECK(values);
switch (type_) {
case SHADER_VARIABLE_FLOAT:
for (size_t ii = 0; ii < 4; ++ii)
values[ii] = v_[ii].float_value;
break;
case SHADER_VARIABLE_INT:
for (size_t ii = 0; ii < 4; ++ii)
values[ii] = static_cast<GLfloat>(v_[ii].int_value);
break;
case SHADER_VARIABLE_UINT:
for (size_t ii = 0; ii < 4; ++ii)
values[ii] = static_cast<GLfloat>(v_[ii].uint_value);
break;
default:
NOTREACHED();
}
}
template <>
void Vec4::GetValues<GLint>(GLint* values) const {
DCHECK(values);
switch (type_) {
case SHADER_VARIABLE_FLOAT:
for (size_t ii = 0; ii < 4; ++ii)
values[ii] = static_cast<GLint>(v_[ii].float_value);
break;
case SHADER_VARIABLE_INT:
for (size_t ii = 0; ii < 4; ++ii)
values[ii] = v_[ii].int_value;
break;
case SHADER_VARIABLE_UINT:
for (size_t ii = 0; ii < 4; ++ii)
values[ii] = static_cast<GLint>(v_[ii].uint_value);
break;
default:
NOTREACHED();
}
}
template <>
void Vec4::GetValues<GLuint>(GLuint* values) const {
DCHECK(values);
switch (type_) {
case SHADER_VARIABLE_FLOAT:
for (size_t ii = 0; ii < 4; ++ii)
values[ii] = static_cast<GLuint>(v_[ii].float_value);
break;
case SHADER_VARIABLE_INT:
for (size_t ii = 0; ii < 4; ++ii)
values[ii] = static_cast<GLuint>(v_[ii].int_value);
break;
case SHADER_VARIABLE_UINT:
for (size_t ii = 0; ii < 4; ++ii)
values[ii] = v_[ii].uint_value;
break;
default:
NOTREACHED();
}
}
template <>
void Vec4::SetValues<GLfloat>(const GLfloat* values) {
DCHECK(values);
for (size_t ii = 0; ii < 4; ++ii)
v_[ii].float_value = values[ii];
type_ = SHADER_VARIABLE_FLOAT;
}
template <>
void Vec4::SetValues<GLint>(const GLint* values) {
DCHECK(values);
for (size_t ii = 0; ii < 4; ++ii)
v_[ii].int_value = values[ii];
type_ = SHADER_VARIABLE_INT;
}
template <>
void Vec4::SetValues<GLuint>(const GLuint* values) {
DCHECK(values);
for (size_t ii = 0; ii < 4; ++ii)
v_[ii].uint_value = values[ii];
type_ = SHADER_VARIABLE_UINT;
}
ContextState::ContextState(FeatureInfo* feature_info,
bool track_texture_and_sampler_units)
: track_texture_and_sampler_units(track_texture_and_sampler_units),
feature_info_(feature_info) {
Initialize();
}
ContextState::~ContextState() = default;
void ContextState::SetLineWidthBounds(GLfloat min, GLfloat max) {
line_width_min_ = min;
line_width_max_ = max;
}
void ContextState::RestoreTextureUnitBindings(
GLuint unit,
const ContextState* prev_state) const {
DCHECK(unit < texture_units.size() ||
(unit == 0 && !track_texture_and_sampler_units));
GLuint service_id_2d = 0u;
GLuint service_id_2d_array = 0u;
GLuint service_id_3d = 0u;
GLuint service_id_cube = 0u;
GLuint service_id_oes = 0u;
GLuint service_id_arb = 0u;
if (track_texture_and_sampler_units) {
const TextureUnit& texture_unit = texture_units[unit];
service_id_2d = Get2dServiceId(texture_unit);
service_id_2d_array = Get2dArrayServiceId(texture_unit);
service_id_3d = Get3dServiceId(texture_unit);
service_id_cube = GetCubeServiceId(texture_unit);
service_id_oes = GetOesServiceId(texture_unit);
service_id_arb = GetArbServiceId(texture_unit);
}
bool bind_texture_2d = true;
bool bind_texture_cube = true;
bool bind_texture_oes =
feature_info_->feature_flags().oes_egl_image_external ||
feature_info_->feature_flags().nv_egl_stream_consumer_external;
bool bind_texture_arb = feature_info_->feature_flags().arb_texture_rectangle;
// TEXTURE_2D_ARRAY and TEXTURE_3D are only applicable from ES3 version.
// So set it to FALSE by default.
bool bind_texture_2d_array = false;
bool bind_texture_3d = false;
// set the variables to true only if the application is ES3 or newer
if (feature_info_->IsES3Capable()) {
bind_texture_2d_array = true;
bind_texture_3d = true;
}
if (prev_state) {
if (prev_state->track_texture_and_sampler_units) {
const TextureUnit& prev_unit = prev_state->texture_units[unit];
bind_texture_2d = service_id_2d != Get2dServiceId(prev_unit);
bind_texture_2d_array =
bind_texture_2d_array &&
service_id_2d_array != Get2dArrayServiceId(prev_unit);
bind_texture_3d =
bind_texture_3d && service_id_3d != Get3dServiceId(prev_unit);
bind_texture_cube = service_id_cube != GetCubeServiceId(prev_unit);
bind_texture_oes =
bind_texture_oes && service_id_oes != GetOesServiceId(prev_unit);
bind_texture_arb =
bind_texture_arb && service_id_arb != GetArbServiceId(prev_unit);
} else if (prev_state->texture_units_in_ground_state) {
bind_texture_2d = service_id_2d;
bind_texture_2d_array = bind_texture_2d_array && service_id_2d_array;
bind_texture_3d = bind_texture_3d && service_id_3d;
bind_texture_cube = service_id_cube;
bind_texture_oes = bind_texture_oes && service_id_oes;
bind_texture_arb = bind_texture_arb && service_id_arb;
} else {
// We need bind all restore target binding, if texture units is not in
// ground state.
}
}
// Early-out if nothing has changed from the previous state.
if (!bind_texture_2d && !bind_texture_2d_array && !bind_texture_3d &&
!bind_texture_cube && !bind_texture_oes && !bind_texture_arb) {
return;
}
api()->glActiveTextureFn(GL_TEXTURE0 + unit);
if (bind_texture_2d) {
api()->glBindTextureFn(GL_TEXTURE_2D, service_id_2d);
}
if (bind_texture_cube) {
api()->glBindTextureFn(GL_TEXTURE_CUBE_MAP, service_id_cube);
}
if (bind_texture_oes) {
api()->glBindTextureFn(GL_TEXTURE_EXTERNAL_OES, service_id_oes);
}
if (bind_texture_arb) {
api()->glBindTextureFn(GL_TEXTURE_RECTANGLE_ARB, service_id_arb);
}
if (bind_texture_2d_array) {
api()->glBindTextureFn(GL_TEXTURE_2D_ARRAY, service_id_2d_array);
}
if (bind_texture_3d) {
api()->glBindTextureFn(GL_TEXTURE_3D, service_id_3d);
}
} // namespace gles2
void ContextState::RestoreSamplerBinding(GLuint unit,
const ContextState* prev_state) const {
if (!feature_info_->IsES3Capable())
return;
GLuint cur_id = 0u;
if (const auto& cur_sampler = sampler_units[unit])
cur_id = cur_sampler->service_id();
std::optional<GLuint> prev_id;
if (prev_state) {
const auto& prev_sampler = prev_state->sampler_units[unit];
prev_id.emplace(prev_sampler ? prev_sampler->service_id() : 0);
}
if (!prev_id || cur_id != *prev_id)
api()->glBindSamplerFn(unit, cur_id);
}
void ContextState::PushTextureUnpackState() const {
api()->glPixelStoreiFn(GL_UNPACK_ALIGNMENT, 1);
if (bound_pixel_unpack_buffer.get()) {
api()->glBindBufferFn(GL_PIXEL_UNPACK_BUFFER, 0);
api()->glPixelStoreiFn(GL_UNPACK_ROW_LENGTH, 0);
api()->glPixelStoreiFn(GL_UNPACK_IMAGE_HEIGHT, 0);
DCHECK_EQ(0, unpack_skip_pixels);
DCHECK_EQ(0, unpack_skip_rows);
DCHECK_EQ(0, unpack_skip_images);
}
}
void ContextState::RestoreUnpackState() const {
api()->glPixelStoreiFn(GL_UNPACK_ALIGNMENT, unpack_alignment);
if (bound_pixel_unpack_buffer.get()) {
api()->glBindBufferFn(GL_PIXEL_UNPACK_BUFFER,
GetBufferId(bound_pixel_unpack_buffer.get()));
api()->glPixelStoreiFn(GL_UNPACK_ROW_LENGTH, unpack_row_length);
api()->glPixelStoreiFn(GL_UNPACK_IMAGE_HEIGHT, unpack_image_height);
}
}
void ContextState::DoLineWidth(GLfloat width) const {
api()->glLineWidthFn(std::clamp(width, line_width_min_, line_width_max_));
}
void ContextState::RestoreBufferBindings() const {
if (vertex_attrib_manager.get()) {
Buffer* element_array_buffer =
vertex_attrib_manager->element_array_buffer();
api()->glBindBufferFn(GL_ELEMENT_ARRAY_BUFFER,
GetBufferId(element_array_buffer));
}
api()->glBindBufferFn(GL_ARRAY_BUFFER, GetBufferId(bound_array_buffer.get()));
if (feature_info_->IsES3Capable()) {
api()->glBindBufferFn(GL_COPY_READ_BUFFER,
GetBufferId(bound_copy_read_buffer.get()));
api()->glBindBufferFn(GL_COPY_WRITE_BUFFER,
GetBufferId(bound_copy_write_buffer.get()));
api()->glBindBufferFn(GL_PIXEL_PACK_BUFFER,
GetBufferId(bound_pixel_pack_buffer.get()));
UpdatePackParameters();
api()->glBindBufferFn(GL_PIXEL_UNPACK_BUFFER,
GetBufferId(bound_pixel_unpack_buffer.get()));
UpdateUnpackParameters();
api()->glBindBufferFn(GL_TRANSFORM_FEEDBACK_BUFFER,
GetBufferId(bound_transform_feedback_buffer.get()));
api()->glBindBufferFn(GL_UNIFORM_BUFFER,
GetBufferId(bound_uniform_buffer.get()));
}
}
void ContextState::RestoreRenderbufferBindings() {
// Require Renderbuffer rebind.
bound_renderbuffer_valid = false;
}
void ContextState::RestoreProgramSettings(
const ContextState* prev_state,
bool restore_transform_feedback_bindings) const {
bool flag =
(restore_transform_feedback_bindings && feature_info_->IsES3Capable());
if (flag && prev_state) {
if (prev_state->bound_transform_feedback.get() &&
prev_state->bound_transform_feedback->active() &&
!prev_state->bound_transform_feedback->paused()) {
api()->glPauseTransformFeedbackFn();
}
}
api()->glUseProgramFn(current_program.get() ? current_program->service_id()
: 0);
if (flag) {
if (bound_transform_feedback.get()) {
bound_transform_feedback->DoBindTransformFeedback(
GL_TRANSFORM_FEEDBACK, bound_transform_feedback.get(),
bound_transform_feedback_buffer.get());
} else {
api()->glBindTransformFeedbackFn(GL_TRANSFORM_FEEDBACK, 0);
}
}
}
void ContextState::RestoreIndexedUniformBufferBindings(
const ContextState* prev_state) {
if (!feature_info_->IsES3Capable())
return;
indexed_uniform_buffer_bindings->RestoreBindings(
prev_state ? prev_state->indexed_uniform_buffer_bindings.get() : nullptr);
}
void ContextState::RestoreActiveTexture() const {
api()->glActiveTextureFn(GL_TEXTURE0 + active_texture_unit);
}
void ContextState::RestoreAllTextureUnitAndSamplerBindings(
const ContextState* prev_state) const {
if (!track_texture_and_sampler_units) {
if (prev_state) {
if (!prev_state->track_texture_and_sampler_units) {
texture_units_in_ground_state =
prev_state->texture_units_in_ground_state;
return;
}
texture_units_in_ground_state = true;
for (size_t i = 1; i < prev_state->texture_units.size(); ++i) {
if (prev_state->texture_units[i].AnyTargetBound()) {
texture_units_in_ground_state = false;
break;
}
}
// Make sure all texture units are in ground state, we need to reset the
// 0th texture units. If some of non zero textures aren't in ground state,
// when another context is being make current, we will restore all texture
// units, then it is not necessary to reset the 0th texture units anymore.
if (texture_units_in_ground_state)
RestoreTextureUnitBindings(0, prev_state);
} else {
texture_units_in_ground_state = false;
}
// GrContext is not aware of sampler objects and skia will not restore them,
// so we need to reset them to ground state.
// TODO(penghuang): Remove it when GrContext is created for ES 3.0.
for (size_t i = 0; i < sampler_units.size(); ++i)
RestoreSamplerBinding(i, prev_state);
} else {
// Restore Texture state.
for (size_t i = 0; i < texture_units.size(); ++i) {
RestoreTextureUnitBindings(i, prev_state);
RestoreSamplerBinding(i, prev_state);
}
RestoreActiveTexture();
}
}
void ContextState::RestoreActiveTextureUnitBinding(unsigned int target) const {
DCHECK(active_texture_unit < texture_units.size() ||
(active_texture_unit == 0 && !track_texture_and_sampler_units));
GLuint service_id = 0;
if (track_texture_and_sampler_units) {
const TextureUnit& texture_unit = texture_units[active_texture_unit];
service_id = GetServiceId(texture_unit, target);
}
if (TargetIsSupported(feature_info_, target))
api()->glBindTextureFn(target, service_id);
}
void ContextState::RestoreVertexAttribValues() const {
for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs();
++attrib) {
switch (attrib_values[attrib].type()) {
case SHADER_VARIABLE_FLOAT: {
GLfloat v[4];
attrib_values[attrib].GetValues(v);
api()->glVertexAttrib4fvFn(attrib, v);
} break;
case SHADER_VARIABLE_INT: {
GLint v[4];
attrib_values[attrib].GetValues(v);
api()->glVertexAttribI4ivFn(attrib, v);
} break;
case SHADER_VARIABLE_UINT: {
GLuint v[4];
attrib_values[attrib].GetValues(v);
api()->glVertexAttribI4uivFn(attrib, v);
} break;
default:
NOTREACHED();
}
}
}
void ContextState::RestoreVertexAttribArrays(
const scoped_refptr<VertexAttribManager> attrib_manager) const {
// This is expected to be called only for VAO with service_id 0,
// either to restore the default VAO or a virtual VAO with service_id 0.
GLuint vao_service_id = attrib_manager->service_id();
DCHECK(vao_service_id == 0);
// Bind VAO if supported.
if (feature_info_->feature_flags().native_vertex_array_object)
api()->glBindVertexArrayOESFn(vao_service_id);
// Restore vertex attrib arrays.
for (size_t attrib_index = 0; attrib_index < attrib_manager->num_attribs();
++attrib_index) {
const VertexAttrib* attrib = attrib_manager->GetVertexAttrib(attrib_index);
// Restore vertex array.
Buffer* buffer = attrib->buffer();
GLuint buffer_service_id = buffer ? buffer->service_id() : 0;
api()->glBindBufferFn(GL_ARRAY_BUFFER, buffer_service_id);
const void* ptr = reinterpret_cast<const void*>(attrib->offset());
api()->glVertexAttribPointerFn(attrib_index, attrib->size(), attrib->type(),
attrib->normalized(), attrib->gl_stride(),
ptr);
// Restore attrib divisor if supported.
if (feature_info_->feature_flags().angle_instanced_arrays)
api()->glVertexAttribDivisorANGLEFn(attrib_index, attrib->divisor());
if (attrib->enabled_in_driver()) {
api()->glEnableVertexAttribArrayFn(attrib_index);
} else {
api()->glDisableVertexAttribArrayFn(attrib_index);
}
}
}
void ContextState::RestoreVertexAttribs(const ContextState* prev_state) const {
// Restore Vertex Attrib Arrays
DCHECK(vertex_attrib_manager.get());
// Restore VAOs.
if (feature_info_->feature_flags().native_vertex_array_object) {
// If default VAO is still using shared id 0 instead of unique ids
// per-context, default VAO state must be restored.
GLuint default_vao_service_id = default_vertex_attrib_manager->service_id();
if (default_vao_service_id == 0)
RestoreVertexAttribArrays(default_vertex_attrib_manager);
// Restore the current VAO binding, unless it's the same as the
// default above.
GLuint curr_vao_service_id = vertex_attrib_manager->service_id();
if (curr_vao_service_id != 0)
api()->glBindVertexArrayOESFn(curr_vao_service_id);
} else {
if (prev_state &&
prev_state->feature_info_->feature_flags().native_vertex_array_object &&
feature_info_->workarounds()
.use_client_side_arrays_for_stream_buffers) {
// In order to use client side arrays, the driver's default VAO has to be
// bound.
api()->glBindVertexArrayOESFn(0);
}
// If native VAO isn't supported, emulated VAOs are used.
// Restore to the currently bound VAO.
RestoreVertexAttribArrays(vertex_attrib_manager);
}
// glVertexAttrib4fv aren't part of VAO state and must be restored.
RestoreVertexAttribValues();
}
void ContextState::RestoreGlobalState(const ContextState* prev_state) const {
InitCapabilities(prev_state);
InitState(prev_state);
}
void ContextState::RestoreState(const ContextState* prev_state) {
RestoreAllTextureUnitAndSamplerBindings(prev_state);
// For RasterDecoder, |vertex_attrib_manager| will be nullptr, and we don't
// need restore vertex attribs for them.
if (vertex_attrib_manager)
RestoreVertexAttribs(prev_state);
// RestoreIndexedUniformBufferBindings must be called before
// RestoreBufferBindings. This is because setting the indexed uniform buffer
// bindings via glBindBuffer{Base,Range} also sets the general uniform buffer
// bindings (glBindBuffer), but not vice versa.
// For RasterDecoder, |indexed_uniform_buffer_bindings| will be nullptr, and
// we don't need restore indexed uniform buffer for them.
if (indexed_uniform_buffer_bindings)
RestoreIndexedUniformBufferBindings(prev_state);
RestoreBufferBindings();
RestoreRenderbufferBindings();
RestoreProgramSettings(prev_state, true);
RestoreGlobalState(prev_state);
// FRAMEBUFFER_SRGB will be restored lazily at render time.
framebuffer_srgb_valid_ = false;
}
void ContextState::EnableDisable(GLenum pname, bool enable) const {
if (pname == GL_PRIMITIVE_RESTART_FIXED_INDEX &&
feature_info_->feature_flags().emulate_primitive_restart_fixed_index) {
// GLES2DecoderImpl::DoDrawElements can handle this situation
return;
}
if (enable) {
api()->glEnableFn(pname);
} else {
api()->glDisableFn(pname);
}
}
void ContextState::UpdatePackParameters() const {
if (!feature_info_->IsES3Capable())
return;
if (bound_pixel_pack_buffer.get()) {
api()->glPixelStoreiFn(GL_PACK_ROW_LENGTH, pack_row_length);
} else {
api()->glPixelStoreiFn(GL_PACK_ROW_LENGTH, 0);
}
}
void ContextState::SetMaxWindowRectangles(size_t max) {
window_rectangles_ = std::vector<GLint>(max * 4, 0);
}
size_t ContextState::GetMaxWindowRectangles() const {
size_t size = window_rectangles_.size();
DCHECK_EQ(0ull, size % 4);
return size / 4;
}
void ContextState::SetWindowRectangles(GLenum mode,
size_t count,
const volatile GLint* box) {
window_rectangles_mode = mode;
num_window_rectangles = count;
DCHECK_LE(count, GetMaxWindowRectangles());
if (count) {
std::copy(box, &box[count * 4], window_rectangles_.begin());
}
}
void ContextState::UpdateWindowRectangles() const {
if (!feature_info_->feature_flags().ext_window_rectangles) {
return;
}
if (current_draw_framebuffer_client_id == 0) {
// Window rectangles must not take effect for client_id 0 (backbuffer).
api()->glWindowRectanglesEXTFn(GL_EXCLUSIVE_EXT, 0, nullptr);
} else {
DCHECK_LE(static_cast<size_t>(num_window_rectangles),
GetMaxWindowRectangles());
const GLint* data =
num_window_rectangles ? window_rectangles_.data() : nullptr;
api()->glWindowRectanglesEXTFn(window_rectangles_mode,
num_window_rectangles, data);
}
}
void ContextState::UpdateWindowRectanglesForBoundDrawFramebufferClientID(
GLuint client_id) {
bool old_id_nonzero = current_draw_framebuffer_client_id != 0;
bool new_id_nonzero = client_id != 0;
current_draw_framebuffer_client_id = client_id;
// If switching from FBO to backbuffer, or vice versa, update driver state.
if (old_id_nonzero ^ new_id_nonzero) {
UpdateWindowRectangles();
}
}
void ContextState::UpdateUnpackParameters() const {
if (!feature_info_->IsES3Capable())
return;
if (bound_pixel_unpack_buffer.get()) {
api()->glPixelStoreiFn(GL_UNPACK_ROW_LENGTH, unpack_row_length);
api()->glPixelStoreiFn(GL_UNPACK_IMAGE_HEIGHT, unpack_image_height);
} else {
api()->glPixelStoreiFn(GL_UNPACK_ROW_LENGTH, 0);
api()->glPixelStoreiFn(GL_UNPACK_IMAGE_HEIGHT, 0);
}
}
void ContextState::SetBoundBuffer(GLenum target, Buffer* buffer) {
bool do_refcounting = feature_info_->IsWebGL2OrES3Context();
switch (target) {
case GL_ARRAY_BUFFER:
if (do_refcounting && bound_array_buffer)
bound_array_buffer->OnUnbind(target, false);
bound_array_buffer = buffer;
if (do_refcounting && buffer)
buffer->OnBind(target, false);
break;
case GL_ELEMENT_ARRAY_BUFFER:
vertex_attrib_manager->SetElementArrayBuffer(buffer);
break;
case GL_COPY_READ_BUFFER:
if (do_refcounting && bound_copy_read_buffer)
bound_copy_read_buffer->OnUnbind(target, false);
bound_copy_read_buffer = buffer;
if (do_refcounting && buffer)
buffer->OnBind(target, false);
break;
case GL_COPY_WRITE_BUFFER:
if (do_refcounting && bound_copy_write_buffer)
bound_copy_write_buffer->OnUnbind(target, false);
bound_copy_write_buffer = buffer;
if (do_refcounting && buffer)
buffer->OnBind(target, false);
break;
case GL_PIXEL_PACK_BUFFER:
if (do_refcounting && bound_pixel_pack_buffer)
bound_pixel_pack_buffer->OnUnbind(target, false);
bound_pixel_pack_buffer = buffer;
if (do_refcounting && buffer)
buffer->OnBind(target, false);
UpdatePackParameters();
break;
case GL_PIXEL_UNPACK_BUFFER:
if (do_refcounting && bound_pixel_unpack_buffer)
bound_pixel_unpack_buffer->OnUnbind(target, false);
bound_pixel_unpack_buffer = buffer;
if (do_refcounting && buffer)
buffer->OnBind(target, false);
UpdateUnpackParameters();
break;
case GL_TRANSFORM_FEEDBACK_BUFFER:
if (do_refcounting && bound_transform_feedback_buffer)
bound_transform_feedback_buffer->OnUnbind(target, false);
bound_transform_feedback_buffer = buffer;
if (do_refcounting && buffer)
buffer->OnBind(target, false);
break;
case GL_UNIFORM_BUFFER:
if (do_refcounting && bound_uniform_buffer)
bound_uniform_buffer->OnUnbind(target, false);
bound_uniform_buffer = buffer;
if (do_refcounting && buffer)
buffer->OnBind(target, false);
break;
default:
NOTREACHED();
}
}
void ContextState::RemoveBoundBuffer(Buffer* buffer) {
DCHECK(buffer);
bool do_refcounting = feature_info_->IsWebGL2OrES3Context();
if (bound_array_buffer.get() == buffer) {
bound_array_buffer = nullptr;
if (do_refcounting)
buffer->OnUnbind(GL_ARRAY_BUFFER, false);
if (!context_lost_)
api()->glBindBufferFn(GL_ARRAY_BUFFER, 0);
}
// Needs to be called after bound_array_buffer handled.
vertex_attrib_manager->Unbind(buffer, bound_array_buffer.get());
if (bound_copy_read_buffer.get() == buffer) {
bound_copy_read_buffer = nullptr;
if (do_refcounting)
buffer->OnUnbind(GL_COPY_READ_BUFFER, false);
if (!context_lost_)
api()->glBindBufferFn(GL_COPY_READ_BUFFER, 0);
}
if (bound_copy_write_buffer.get() == buffer) {
bound_copy_write_buffer = nullptr;
if (do_refcounting)
buffer->OnUnbind(GL_COPY_WRITE_BUFFER, false);
if (!context_lost_)
api()->glBindBufferFn(GL_COPY_WRITE_BUFFER, 0);
}
if (bound_pixel_pack_buffer.get() == buffer) {
bound_pixel_pack_buffer = nullptr;
if (do_refcounting)
buffer->OnUnbind(GL_PIXEL_PACK_BUFFER, false);
if (!context_lost_)
api()->glBindBufferFn(GL_PIXEL_PACK_BUFFER, 0);
UpdatePackParameters();
}
if (bound_pixel_unpack_buffer.get() == buffer) {
bound_pixel_unpack_buffer = nullptr;
if (do_refcounting)
buffer->OnUnbind(GL_PIXEL_UNPACK_BUFFER, false);
if (!context_lost_)
api()->glBindBufferFn(GL_PIXEL_UNPACK_BUFFER, 0);
UpdateUnpackParameters();
}
if (bound_transform_feedback_buffer.get() == buffer) {
bound_transform_feedback_buffer = nullptr;
if (do_refcounting)
buffer->OnUnbind(GL_TRANSFORM_FEEDBACK_BUFFER, false);
if (!context_lost_)
api()->glBindBufferFn(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
}
// Needs to be called after bound_transform_feedback_buffer handled.
if (bound_transform_feedback.get()) {
bound_transform_feedback->RemoveBoundBuffer(
GL_TRANSFORM_FEEDBACK_BUFFER, buffer,
bound_transform_feedback_buffer.get(), !context_lost_);
}
if (bound_uniform_buffer.get() == buffer) {
bound_uniform_buffer = nullptr;
if (do_refcounting)
buffer->OnUnbind(GL_UNIFORM_BUFFER, false);
if (!context_lost_)
api()->glBindBufferFn(GL_UNIFORM_BUFFER, 0);
}
// Needs to be called after bound_uniform_buffer handled.
if (indexed_uniform_buffer_bindings) {
indexed_uniform_buffer_bindings->RemoveBoundBuffer(
GL_UNIFORM_BUFFER, buffer, bound_uniform_buffer.get(), !context_lost_);
}
}
void ContextState::UnbindTexture(TextureRef* texture) {
GLuint active_unit = active_texture_unit;
for (size_t jj = 0; jj < texture_units.size(); ++jj) {
TextureUnit& unit = texture_units[jj];
if (unit.bound_texture_2d.get() == texture) {
unit.bound_texture_2d = nullptr;
if (active_unit != jj) {
api()->glActiveTextureFn(GL_TEXTURE0 + jj);
active_unit = jj;
}
api()->glBindTextureFn(GL_TEXTURE_2D, 0);
} else if (unit.bound_texture_cube_map.get() == texture) {
unit.bound_texture_cube_map = nullptr;
if (active_unit != jj) {
api()->glActiveTextureFn(GL_TEXTURE0 + jj);
active_unit = jj;
}
api()->glBindTextureFn(GL_TEXTURE_CUBE_MAP, 0);
} else if (unit.bound_texture_external_oes.get() == texture) {
unit.bound_texture_external_oes = nullptr;
if (active_unit != jj) {
api()->glActiveTextureFn(GL_TEXTURE0 + jj);
active_unit = jj;
}
api()->glBindTextureFn(GL_TEXTURE_EXTERNAL_OES, 0);
} else if (unit.bound_texture_rectangle_arb.get() == texture) {
unit.bound_texture_rectangle_arb = nullptr;
if (active_unit != jj) {
api()->glActiveTextureFn(GL_TEXTURE0 + jj);
active_unit = jj;
}
api()->glBindTextureFn(GL_TEXTURE_RECTANGLE_ARB, 0);
} else if (unit.bound_texture_3d.get() == texture) {
unit.bound_texture_3d = nullptr;
if (active_unit != jj) {
api()->glActiveTextureFn(GL_TEXTURE0 + jj);
active_unit = jj;
}
api()->glBindTextureFn(GL_TEXTURE_3D, 0);
} else if (unit.bound_texture_2d_array.get() == texture) {
unit.bound_texture_2d_array = nullptr;
if (active_unit != jj) {
api()->glActiveTextureFn(GL_TEXTURE0 + jj);
active_unit = jj;
}
api()->glBindTextureFn(GL_TEXTURE_2D_ARRAY, 0);
}
}
if (active_unit != active_texture_unit) {
api()->glActiveTextureFn(GL_TEXTURE0 + active_texture_unit);
}
}
void ContextState::UnbindSampler(Sampler* sampler) {
for (size_t jj = 0; jj < sampler_units.size(); ++jj) {
if (sampler_units[jj].get() == sampler) {
sampler_units[jj] = nullptr;
api()->glBindSamplerFn(jj, 0);
}
}
}
PixelStoreParams ContextState::GetPackParams() {
DCHECK_EQ(0, pack_skip_pixels);
DCHECK_EQ(0, pack_skip_rows);
PixelStoreParams params;
params.alignment = pack_alignment;
params.row_length = pack_row_length;
return params;
}
PixelStoreParams ContextState::GetUnpackParams(Dimension dimension) {
DCHECK_EQ(0, unpack_skip_pixels);
DCHECK_EQ(0, unpack_skip_rows);
DCHECK_EQ(0, unpack_skip_images);
PixelStoreParams params;
params.alignment = unpack_alignment;
params.row_length = unpack_row_length;
if (dimension == k3D) {
params.image_height = unpack_image_height;
}
return params;
}
void ContextState::EnableDisableFramebufferSRGB(bool enable) {
if (framebuffer_srgb_valid_ && framebuffer_srgb_ == enable)
return;
EnableDisable(GL_FRAMEBUFFER_SRGB, enable);
framebuffer_srgb_ = enable;
framebuffer_srgb_valid_ = true;
}
void ContextState::InitStateManual(const ContextState*) const {
// Here we always reset the states whether it's different from previous ones.
// We have very limited states here; also, once we switch to MANGLE, MANGLE
// will opmitize this.
UpdatePackParameters();
UpdateUnpackParameters();
UpdateWindowRectangles();
}
// Include the auto-generated part of this file. We split this because it means
// we can easily edit the non-auto generated parts right here in this file
// instead of having to edit some template or the code generator.
#include "gpu/command_buffer/service/context_state_impl_autogen.h"
} // namespace gles2
} // namespace gpu