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
|
// Copyright (c) 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 "gpu/command_buffer/service/framebuffer_manager.h"
#include "base/logging.h"
#include "base/stringprintf.h"
#include "gpu/command_buffer/common/gles2_cmd_utils.h"
#include "gpu/command_buffer/service/renderbuffer_manager.h"
#include "gpu/command_buffer/service/texture_manager.h"
#include "ui/gl/gl_bindings.h"
namespace gpu {
namespace gles2 {
Framebuffer::FramebufferComboCompleteMap*
Framebuffer::framebuffer_combo_complete_map_;
// Framebuffer completeness is not cacheable on OS X because of dynamic
// graphics switching.
// http://crbug.com/180876
#if defined(OS_MACOSX)
bool Framebuffer::allow_framebuffer_combo_complete_map_ = false;
#else
bool Framebuffer::allow_framebuffer_combo_complete_map_ = true;
#endif
void Framebuffer::ClearFramebufferCompleteComboMap() {
if (framebuffer_combo_complete_map_) {
framebuffer_combo_complete_map_->clear();
}
}
class RenderbufferAttachment
: public Framebuffer::Attachment {
public:
explicit RenderbufferAttachment(
Renderbuffer* renderbuffer)
: renderbuffer_(renderbuffer) {
}
virtual GLsizei width() const OVERRIDE {
return renderbuffer_->width();
}
virtual GLsizei height() const OVERRIDE {
return renderbuffer_->height();
}
virtual GLenum internal_format() const OVERRIDE {
return renderbuffer_->internal_format();
}
virtual GLsizei samples() const OVERRIDE {
return renderbuffer_->samples();
}
virtual bool cleared() const OVERRIDE {
return renderbuffer_->cleared();
}
virtual void SetCleared(
RenderbufferManager* renderbuffer_manager,
TextureManager* /* texture_manager */,
bool cleared) OVERRIDE {
renderbuffer_manager->SetCleared(renderbuffer_, cleared);
}
virtual bool IsTexture(
TextureRef* /* texture */) const OVERRIDE {
return false;
}
virtual bool IsRenderbuffer(
Renderbuffer* renderbuffer) const OVERRIDE {
return renderbuffer_ == renderbuffer;
}
virtual bool CanRenderTo() const OVERRIDE {
return true;
}
virtual void DetachFromFramebuffer() const OVERRIDE {
// Nothing to do for renderbuffers.
}
virtual bool ValidForAttachmentType(
GLenum attachment_type, uint32 max_color_attachments) OVERRIDE {
uint32 need = GLES2Util::GetChannelsNeededForAttachmentType(
attachment_type, max_color_attachments);
uint32 have = GLES2Util::GetChannelsForFormat(internal_format());
return (need & have) != 0;
}
Renderbuffer* renderbuffer() const {
return renderbuffer_.get();
}
virtual void AddToSignature(
TextureManager* texture_manager, std::string* signature) const OVERRIDE {
DCHECK(signature);
renderbuffer_->AddToSignature(signature);
}
protected:
virtual ~RenderbufferAttachment() { }
private:
scoped_refptr<Renderbuffer> renderbuffer_;
DISALLOW_COPY_AND_ASSIGN(RenderbufferAttachment);
};
class TextureAttachment
: public Framebuffer::Attachment {
public:
TextureAttachment(
TextureRef* texture_ref, GLenum target, GLint level)
: texture_ref_(texture_ref),
target_(target),
level_(level) {
}
virtual GLsizei width() const OVERRIDE {
GLsizei temp_width = 0;
GLsizei temp_height = 0;
texture_ref_->texture()->GetLevelSize(
target_, level_, &temp_width, &temp_height);
return temp_width;
}
virtual GLsizei height() const OVERRIDE {
GLsizei temp_width = 0;
GLsizei temp_height = 0;
texture_ref_->texture()->GetLevelSize(
target_, level_, &temp_width, &temp_height);
return temp_height;
}
virtual GLenum internal_format() const OVERRIDE {
GLenum temp_type = 0;
GLenum temp_internal_format = 0;
texture_ref_->texture()->GetLevelType(
target_, level_, &temp_type, &temp_internal_format);
return temp_internal_format;
}
virtual GLsizei samples() const OVERRIDE {
return 0;
}
virtual bool cleared() const OVERRIDE {
return texture_ref_->texture()->IsLevelCleared(target_, level_);
}
virtual void SetCleared(
RenderbufferManager* /* renderbuffer_manager */,
TextureManager* texture_manager,
bool cleared) OVERRIDE {
texture_manager->SetLevelCleared(texture_ref_, target_, level_, cleared);
}
virtual bool IsTexture(TextureRef* texture) const OVERRIDE {
return texture == texture_ref_.get();
}
virtual bool IsRenderbuffer(
Renderbuffer* /* renderbuffer */)
const OVERRIDE {
return false;
}
TextureRef* texture() const {
return texture_ref_.get();
}
virtual bool CanRenderTo() const OVERRIDE {
return texture_ref_->texture()->CanRenderTo();
}
virtual void DetachFromFramebuffer() const OVERRIDE {
texture_ref_->texture()->DetachFromFramebuffer();
}
virtual bool ValidForAttachmentType(
GLenum attachment_type, uint32 max_color_attachments) OVERRIDE {
GLenum type = 0;
GLenum internal_format = 0;
if (!texture_ref_->texture()->GetLevelType(
target_, level_, &type, &internal_format)) {
return false;
}
uint32 need = GLES2Util::GetChannelsNeededForAttachmentType(
attachment_type, max_color_attachments);
uint32 have = GLES2Util::GetChannelsForFormat(internal_format);
return (need & have) != 0;
}
virtual void AddToSignature(
TextureManager* texture_manager, std::string* signature) const OVERRIDE {
DCHECK(signature);
texture_manager->AddToSignature(texture_ref_, target_, level_, signature);
}
protected:
virtual ~TextureAttachment() {}
private:
scoped_refptr<TextureRef> texture_ref_;
GLenum target_;
GLint level_;
DISALLOW_COPY_AND_ASSIGN(TextureAttachment);
};
FramebufferManager::FramebufferManager(
uint32 max_draw_buffers, uint32 max_color_attachments)
: framebuffer_state_change_count_(1),
framebuffer_count_(0),
have_context_(true),
max_draw_buffers_(max_draw_buffers),
max_color_attachments_(max_color_attachments) {
DCHECK_GT(max_draw_buffers_, 0u);
DCHECK_GT(max_color_attachments_, 0u);
}
FramebufferManager::~FramebufferManager() {
DCHECK(framebuffers_.empty());
// If this triggers, that means something is keeping a reference to a
// Framebuffer belonging to this.
CHECK_EQ(framebuffer_count_, 0u);
}
void Framebuffer::MarkAsDeleted() {
deleted_ = true;
while (!attachments_.empty()) {
Attachment* attachment = attachments_.begin()->second.get();
attachment->DetachFromFramebuffer();
attachments_.erase(attachments_.begin());
}
}
void FramebufferManager::Destroy(bool have_context) {
have_context_ = have_context;
framebuffers_.clear();
}
void FramebufferManager::StartTracking(
Framebuffer* /* framebuffer */) {
++framebuffer_count_;
}
void FramebufferManager::StopTracking(
Framebuffer* /* framebuffer */) {
--framebuffer_count_;
}
void FramebufferManager::CreateFramebuffer(
GLuint client_id, GLuint service_id) {
std::pair<FramebufferMap::iterator, bool> result =
framebuffers_.insert(
std::make_pair(
client_id,
scoped_refptr<Framebuffer>(
new Framebuffer(this, service_id))));
DCHECK(result.second);
}
Framebuffer::Framebuffer(
FramebufferManager* manager, GLuint service_id)
: manager_(manager),
deleted_(false),
service_id_(service_id),
has_been_bound_(false),
framebuffer_complete_state_count_id_(0) {
manager->StartTracking(this);
DCHECK_GT(manager->max_draw_buffers_, 0u);
draw_buffers_.reset(new GLenum[manager->max_draw_buffers_]);
draw_buffers_[0] = GL_COLOR_ATTACHMENT0;
for (uint32 i = 1; i < manager->max_draw_buffers_; ++i)
draw_buffers_[i] = GL_NONE;
}
Framebuffer::~Framebuffer() {
if (manager_) {
if (manager_->have_context_) {
GLuint id = service_id();
glDeleteFramebuffersEXT(1, &id);
}
manager_->StopTracking(this);
manager_ = NULL;
}
}
bool Framebuffer::HasUnclearedAttachment(
GLenum attachment) const {
AttachmentMap::const_iterator it =
attachments_.find(attachment);
if (it != attachments_.end()) {
const Attachment* attachment = it->second;
return !attachment->cleared();
}
return false;
}
void Framebuffer::MarkAttachmentAsCleared(
RenderbufferManager* renderbuffer_manager,
TextureManager* texture_manager,
GLenum attachment,
bool cleared) {
AttachmentMap::iterator it = attachments_.find(attachment);
if (it != attachments_.end()) {
Attachment* a = it->second;
if (a->cleared() != cleared) {
a->SetCleared(renderbuffer_manager,
texture_manager,
cleared);
}
}
}
void Framebuffer::MarkAttachmentsAsCleared(
RenderbufferManager* renderbuffer_manager,
TextureManager* texture_manager,
bool cleared) {
for (AttachmentMap::iterator it = attachments_.begin();
it != attachments_.end(); ++it) {
Attachment* attachment = it->second;
if (attachment->cleared() != cleared) {
attachment->SetCleared(renderbuffer_manager, texture_manager, cleared);
}
}
}
bool Framebuffer::HasDepthAttachment() const {
return attachments_.find(GL_DEPTH_STENCIL_ATTACHMENT) != attachments_.end() ||
attachments_.find(GL_DEPTH_ATTACHMENT) != attachments_.end();
}
bool Framebuffer::HasStencilAttachment() const {
return attachments_.find(GL_DEPTH_STENCIL_ATTACHMENT) != attachments_.end() ||
attachments_.find(GL_STENCIL_ATTACHMENT) != attachments_.end();
}
GLenum Framebuffer::GetColorAttachmentFormat() const {
AttachmentMap::const_iterator it = attachments_.find(GL_COLOR_ATTACHMENT0);
if (it == attachments_.end()) {
return 0;
}
const Attachment* attachment = it->second;
return attachment->internal_format();
}
GLenum Framebuffer::IsPossiblyComplete() const {
if (attachments_.empty()) {
return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
}
GLsizei width = -1;
GLsizei height = -1;
for (AttachmentMap::const_iterator it = attachments_.begin();
it != attachments_.end(); ++it) {
GLenum attachment_type = it->first;
Attachment* attachment = it->second;
if (!attachment->ValidForAttachmentType(
attachment_type, manager_->max_color_attachments_)) {
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
}
if (width < 0) {
width = attachment->width();
height = attachment->height();
if (width == 0 || height == 0) {
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
}
} else {
if (attachment->width() != width || attachment->height() != height) {
return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
}
}
if (!attachment->CanRenderTo()) {
return GL_FRAMEBUFFER_UNSUPPORTED;
}
}
// This does not mean the framebuffer is actually complete. It just means our
// checks passed.
return GL_FRAMEBUFFER_COMPLETE;
}
GLenum Framebuffer::GetStatus(
TextureManager* texture_manager, GLenum target) const {
// Check if we have this combo already.
std::string signature;
if (allow_framebuffer_combo_complete_map_) {
signature = base::StringPrintf("|FBO|target=%04x", target);
for (AttachmentMap::const_iterator it = attachments_.begin();
it != attachments_.end(); ++it) {
Attachment* attachment = it->second;
signature += base::StringPrintf(
"|Attachment|attachmentpoint=%04x", it->first);
attachment->AddToSignature(texture_manager, &signature);
}
if (!framebuffer_combo_complete_map_) {
framebuffer_combo_complete_map_ = new FramebufferComboCompleteMap();
}
FramebufferComboCompleteMap::const_iterator it =
framebuffer_combo_complete_map_->find(signature);
if (it != framebuffer_combo_complete_map_->end()) {
return GL_FRAMEBUFFER_COMPLETE;
}
}
GLenum result = glCheckFramebufferStatusEXT(target);
// Insert the new result into the combo map.
if (allow_framebuffer_combo_complete_map_ &&
result == GL_FRAMEBUFFER_COMPLETE) {
framebuffer_combo_complete_map_->insert(std::make_pair(signature, true));
}
return result;
}
bool Framebuffer::IsCleared() const {
// are all the attachments cleaared?
for (AttachmentMap::const_iterator it = attachments_.begin();
it != attachments_.end(); ++it) {
Attachment* attachment = it->second;
if (!attachment->cleared()) {
return false;
}
}
return true;
}
GLenum Framebuffer::GetDrawBuffer(GLenum draw_buffer) const {
GLsizei index = static_cast<GLsizei>(
draw_buffer - GL_DRAW_BUFFER0_ARB);
CHECK(index >= 0 &&
index < static_cast<GLsizei>(manager_->max_draw_buffers_));
return draw_buffers_[index];
}
void Framebuffer::SetDrawBuffers(GLsizei n, const GLenum* bufs) {
DCHECK(n <= static_cast<GLsizei>(manager_->max_draw_buffers_));
for (GLsizei i = 0; i < n; ++i)
draw_buffers_[i] = bufs[i];
}
void Framebuffer::UnbindRenderbuffer(
GLenum target, Renderbuffer* renderbuffer) {
bool done;
do {
done = true;
for (AttachmentMap::const_iterator it = attachments_.begin();
it != attachments_.end(); ++it) {
Attachment* attachment = it->second;
if (attachment->IsRenderbuffer(renderbuffer)) {
// TODO(gman): manually detach renderbuffer.
// glFramebufferRenderbufferEXT(target, it->first, GL_RENDERBUFFER, 0);
AttachRenderbuffer(it->first, NULL);
done = false;
break;
}
}
} while (!done);
}
void Framebuffer::UnbindTexture(
GLenum target, TextureRef* texture_ref) {
bool done;
do {
done = true;
for (AttachmentMap::const_iterator it = attachments_.begin();
it != attachments_.end(); ++it) {
Attachment* attachment = it->second;
if (attachment->IsTexture(texture_ref)) {
// TODO(gman): manually detach texture.
// glFramebufferTexture2DEXT(target, it->first, GL_TEXTURE_2D, 0, 0);
AttachTexture(it->first, NULL, GL_TEXTURE_2D, 0);
done = false;
break;
}
}
} while (!done);
}
Framebuffer* FramebufferManager::GetFramebuffer(
GLuint client_id) {
FramebufferMap::iterator it = framebuffers_.find(client_id);
return it != framebuffers_.end() ? it->second : NULL;
}
void FramebufferManager::RemoveFramebuffer(GLuint client_id) {
FramebufferMap::iterator it = framebuffers_.find(client_id);
if (it != framebuffers_.end()) {
it->second->MarkAsDeleted();
framebuffers_.erase(it);
}
}
void Framebuffer::AttachRenderbuffer(
GLenum attachment, Renderbuffer* renderbuffer) {
const Attachment* a = GetAttachment(attachment);
if (a)
a->DetachFromFramebuffer();
if (renderbuffer) {
attachments_[attachment] = scoped_refptr<Attachment>(
new RenderbufferAttachment(renderbuffer));
} else {
attachments_.erase(attachment);
}
framebuffer_complete_state_count_id_ = 0;
}
void Framebuffer::AttachTexture(
GLenum attachment, TextureRef* texture_ref, GLenum target,
GLint level) {
const Attachment* a = GetAttachment(attachment);
if (a)
a->DetachFromFramebuffer();
if (texture_ref) {
attachments_[attachment] = scoped_refptr<Attachment>(
new TextureAttachment(texture_ref, target, level));
texture_ref->texture()->AttachToFramebuffer();
} else {
attachments_.erase(attachment);
}
framebuffer_complete_state_count_id_ = 0;
}
const Framebuffer::Attachment*
Framebuffer::GetAttachment(
GLenum attachment) const {
AttachmentMap::const_iterator it = attachments_.find(attachment);
if (it != attachments_.end()) {
return it->second;
}
return NULL;
}
bool FramebufferManager::GetClientId(
GLuint service_id, GLuint* client_id) const {
// This doesn't need to be fast. It's only used during slow queries.
for (FramebufferMap::const_iterator it = framebuffers_.begin();
it != framebuffers_.end(); ++it) {
if (it->second->service_id() == service_id) {
*client_id = it->first;
return true;
}
}
return false;
}
void FramebufferManager::MarkAttachmentsAsCleared(
Framebuffer* framebuffer,
RenderbufferManager* renderbuffer_manager,
TextureManager* texture_manager) {
DCHECK(framebuffer);
framebuffer->MarkAttachmentsAsCleared(renderbuffer_manager,
texture_manager,
true);
MarkAsComplete(framebuffer);
}
void FramebufferManager::MarkAsComplete(
Framebuffer* framebuffer) {
DCHECK(framebuffer);
framebuffer->MarkAsComplete(framebuffer_state_change_count_);
}
bool FramebufferManager::IsComplete(
Framebuffer* framebuffer) {
DCHECK(framebuffer);
return framebuffer->framebuffer_complete_state_count_id() ==
framebuffer_state_change_count_;
}
} // namespace gles2
} // namespace gpu
|