File: gles2_external_framebuffer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (633 lines) | stat: -rw-r--r-- 21,246 bytes parent folder | download | duplicates (5)
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
// Copyright 2022 The Chromium Authors
// 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/gles2_external_framebuffer.h"

#include "gpu/command_buffer/service/feature_info.h"
#include "gpu/command_buffer/service/shared_image/shared_image_factory.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "gpu/command_buffer/service/texture_base.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gl/scoped_binders.h"
#include "ui/gl/scoped_restore_texture.h"

namespace gpu::gles2 {
namespace {
class ScopedRestoreRenderbuffer {
 public:
  explicit ScopedRestoreRenderbuffer(gl::GLApi* api) : api_(api) {
    api_->glGetIntegervFn(GL_RENDERBUFFER_BINDING, &renderbuffer_);
  }

  ~ScopedRestoreRenderbuffer() {
    api_->glBindRenderbufferEXTFn(GL_RENDERBUFFER, renderbuffer_);
  }

 private:
  const raw_ptr<gl::GLApi> api_;
  GLint renderbuffer_ = 0;
};

class ScopedRestoreWindowRectangles {
 public:
  explicit ScopedRestoreWindowRectangles(gl::GLApi* api) : api_(api) {
    api_->glGetIntegervFn(GL_WINDOW_RECTANGLE_MODE_EXT, &mode_);

    GLint num_windows = 0;
    api_->glGetIntegervFn(GL_NUM_WINDOW_RECTANGLES_EXT, &num_windows);

    windows_.resize(4 * num_windows);
    for (int i = 0; i < num_windows; ++i) {
      glGetIntegeri_v(GL_WINDOW_RECTANGLE_EXT, i, &windows_[i * 4]);
    }
  }

  ~ScopedRestoreWindowRectangles() {
    api_->glWindowRectanglesEXTFn(mode_, windows_.size() / 4, windows_.data());
  }

 private:
  const raw_ptr<gl::GLApi> api_;
  GLint mode_ = GL_EXCLUSIVE_EXT;
  std::vector<GLint> windows_;
};

class ScopedRestoreWriteMasks {
 public:
  explicit ScopedRestoreWriteMasks(gl::GLApi* api) : api_(api) {
    api_->glGetIntegervFn(GL_STENCIL_WRITEMASK, &stencil_front_mask_);
    api_->glGetIntegervFn(GL_STENCIL_BACK_WRITEMASK, &stencil_back_mask_);
    api_->glGetBooleanvFn(GL_DEPTH_WRITEMASK, &depth_mask_);
    api_->glGetBooleanvFn(GL_COLOR_WRITEMASK, color_mask_);
  }

  ~ScopedRestoreWriteMasks() {
    api_->glColorMaskFn(color_mask_[0], color_mask_[1], color_mask_[2],
                        color_mask_[3]);
    api_->glDepthMaskFn(depth_mask_);
    api_->glStencilMaskSeparateFn(GL_FRONT, stencil_front_mask_);
    api_->glStencilMaskSeparateFn(GL_BACK, stencil_back_mask_);
  }

 private:
  const raw_ptr<gl::GLApi> api_;
  GLboolean color_mask_[4] = {GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE};
  GLboolean depth_mask_ = GL_TRUE;
  GLint stencil_front_mask_ = 0xFF;
  GLint stencil_back_mask_ = 0xFF;
};

class ScopedRestoreClearValues {
 public:
  explicit ScopedRestoreClearValues(gl::GLApi* api) : api_(api) {
    api_->glGetFloatvFn(GL_COLOR_CLEAR_VALUE, clear_color_);
    api_->glGetFloatvFn(GL_DEPTH_CLEAR_VALUE, &clear_depth_);
    api_->glGetIntegervFn(GL_STENCIL_CLEAR_VALUE, &clear_stencil_);
  }
  ~ScopedRestoreClearValues() {
    api_->glClearColorFn(clear_color_[0], clear_color_[1], clear_color_[2],
                         clear_color_[3]);
    api_->glClearDepthFn(clear_depth_);
    api_->glClearStencilFn(clear_stencil_);
  }

 private:
  const raw_ptr<gl::GLApi> api_;
  GLfloat clear_color_[4] = {};
  GLfloat clear_depth_ = 0.0f;
  GLint clear_stencil_ = 0;
};

class ScopedRestoreFramebuffer {
 public:
  ScopedRestoreFramebuffer(gl::GLApi* api, bool supports_separate_fbo_bindings)
      : api_(api),
        supports_separate_fbo_bindings_(supports_separate_fbo_bindings) {
    if (supports_separate_fbo_bindings_) {
      api_->glGetIntegervFn(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer_);
      api_->glGetIntegervFn(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer_);
    } else {
      api_->glGetIntegervFn(GL_FRAMEBUFFER_BINDING, &draw_framebuffer_);
    }
  }

  ~ScopedRestoreFramebuffer() {
    if (supports_separate_fbo_bindings_) {
      api_->glBindFramebufferEXTFn(GL_DRAW_FRAMEBUFFER, draw_framebuffer_);
      api_->glBindFramebufferEXTFn(GL_READ_FRAMEBUFFER, read_framebuffer_);
    } else {
      api_->glBindFramebufferEXTFn(GL_FRAMEBUFFER, draw_framebuffer_);
    }
  }

 private:
  const raw_ptr<gl::GLApi> api_;
  const bool supports_separate_fbo_bindings_;
  GLint draw_framebuffer_ = 0;
  GLint read_framebuffer_ = 0;
};
}  // namespace

class GLES2ExternalFramebuffer::Attachment {
 public:
  static std::unique_ptr<Attachment> CreateTexture(const gfx::Size& size,
                                                   GLenum format) {
    gl::GLApi* const api = gl::g_current_gl_context;
    gl::ScopedRestoreTexture scoped_restore(api, GL_TEXTURE_2D);

    // Don't use sized formats for textures
    GLenum texture_format;
    switch (format) {
      case GL_RGBA8:
        texture_format = GL_RGBA;
        break;
      case GL_RGB8:
        texture_format = GL_RGB;
        break;
      default:
        texture_format = GL_RGBA;
        NOTREACHED();
    }

    GLuint texture;
    api->glGenTexturesFn(1, &texture);
    api->glBindTextureFn(GL_TEXTURE_2D, texture);
    api->glTexImage2DFn(GL_TEXTURE_2D, 0, texture_format, size.width(),
                        size.height(), 0, texture_format, GL_UNSIGNED_BYTE,
                        nullptr);

    return std::make_unique<Attachment>(size, /*samples_count=*/0, format,
                                        /*texture=*/texture,
                                        /*renderbuffer=*/0);
  }

  static std::unique_ptr<Attachment> CreateRenderbuffer(const gfx::Size& size,
                                                        int samples_count,
                                                        GLenum format) {
    gl::GLApi* const api = gl::g_current_gl_context;
    ScopedRestoreRenderbuffer rb_restore(api);

    GLuint renderbuffer;
    api->glGenRenderbuffersEXTFn(1, &renderbuffer);
    api->glBindRenderbufferEXTFn(GL_RENDERBUFFER, renderbuffer);

    if (samples_count > 0) {
      api->glRenderbufferStorageMultisampleFn(
          GL_RENDERBUFFER, samples_count, format, size.width(), size.height());
    } else {
      api->glRenderbufferStorageEXTFn(GL_RENDERBUFFER, format, size.width(),
                                      size.height());
    }

    return std::make_unique<Attachment>(size, samples_count, format,
                                        /*texture=*/0,
                                        /*renderbuffer=*/renderbuffer);
  }

  Attachment(const gfx::Size& size,
             int samples_count,
             GLenum format,
             GLuint texture,
             GLuint renderbuffer)
      : size_(size),
        samples_count_(samples_count),
        format_(format),
        texture_(texture),
        renderbuffer_(renderbuffer) {
    DCHECK_NE(!!texture, !!renderbuffer);
    DCHECK(!size.IsEmpty());
    DCHECK(format);
  }

  Attachment(const Attachment&) = delete;
  Attachment(Attachment&&) = delete;

  Attachment& operator=(const Attachment&) = delete;
  Attachment& operator=(Attachment&&) = delete;

  ~Attachment() {
    // No need to do anything if context was lost.
    if (context_lost_)
      return;

    DCHECK_EQ(attach_point_, 0u);
    if (texture_)
      glDeleteTextures(1, &texture_);
    else if (renderbuffer_)
      glDeleteRenderbuffersEXT(1, &renderbuffer_);
  }

  void Attach(GLenum attachment) {
    DCHECK_EQ(attach_point_, 0u);
    attach_point_ = attachment;

    if (texture_)
      AttachImpl(attach_point_, /*is_texture=*/true, texture_);
    else
      AttachImpl(attach_point_, /*is_texture=*/false, renderbuffer_);
  }

  void Detach() {
    DCHECK_NE(attach_point_, 0u);

    if (texture_)
      AttachImpl(attach_point_, /*is_texture=*/true, 0);
    else
      AttachImpl(attach_point_, /*is_texture=*/false, 0);
    attach_point_ = 0;
  }

  bool NeedsResolve() { return samples_count_ > 0; }

  bool Compatible(const gfx::Size size, int samples_count, GLenum format) {
    return size_ == size && samples_count_ == samples_count &&
           format_ == format;
  }

  void OnContextLost() { context_lost_ = true; }

  GLenum format() const { return format_; }
  int samples_count() const { return samples_count_; }

 private:
  void AttachImpl(GLenum attachment, bool is_texture, GLuint object) {
    if (is_texture) {
      glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                GL_TEXTURE_2D, object, 0);
    } else {
      if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                                     GL_RENDERBUFFER, object);
        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
                                     GL_RENDERBUFFER, object);
      } else {
        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, attachment,
                                     GL_RENDERBUFFER, object);
      }
    }
  }

  GLenum attach_point_ = 0;
  bool context_lost_ = false;

  const gfx::Size size_;
  const int samples_count_;
  const GLenum format_;
  const GLuint texture_;
  const GLuint renderbuffer_;
};

GLES2ExternalFramebuffer::GLES2ExternalFramebuffer(
    bool passthrough,
    const FeatureInfo& feature_info,
    SharedImageRepresentationFactory* shared_image_representation_factory)
    : passthrough_(passthrough),
      shared_image_representation_factory_(
          shared_image_representation_factory) {
  const bool multisampled_framebuffers_supported =
      feature_info.feature_flags().chromium_framebuffer_multisample;
  const bool rgb8_supported = feature_info.feature_flags().oes_rgb8_rgba8;
  // The only available default render buffer formats in GLES2 have very
  // little precision.  Don't enable multisampling unless 8-bit render
  // buffer formats are available--instead fall back to 8-bit textures.

  if (multisampled_framebuffers_supported && rgb8_supported) {
    glGetIntegerv(GL_MAX_SAMPLES_EXT, &max_sample_count_);
  }

  packed_depth_stencil_ = feature_info.feature_flags().packed_depth24_stencil8;
  supports_separate_fbo_bindings_ = multisampled_framebuffers_supported ||
                                    feature_info.IsWebGL2OrES3Context();
  supports_window_rectangles_ =
      feature_info.feature_flags().ext_window_rectangles;

  glGenFramebuffersEXT(1, &fbo_);
}

GLES2ExternalFramebuffer::~GLES2ExternalFramebuffer() {
  DCHECK_EQ(fbo_, 0u);
  DCHECK(attachments_.empty());
}

void GLES2ExternalFramebuffer::Destroy(bool have_context) {
  if (!have_context) {
    for (auto& attachment : attachments_)
      attachment.second->OnContextLost();

    if (shared_image_representation_)
      shared_image_representation_->OnContextLost();
  } else {
    gl::GLApi* const api = gl::g_current_gl_context;
    ScopedRestoreFramebuffer scoped_fbo_reset(api,
                                              supports_separate_fbo_bindings_);
    api->glBindFramebufferEXTFn(GL_FRAMEBUFFER, fbo_);
    for (auto& attachment : attachments_)
      attachment.second->Detach();
  }

  scoped_access_.reset();
  shared_image_representation_.reset();

  attachments_.clear();

  if (have_context)
    glDeleteFramebuffersEXT(1, &fbo_);
  fbo_ = 0;
}

bool GLES2ExternalFramebuffer::AttachSharedImage(const Mailbox& mailbox,
                                                 int samples,
                                                 bool preserve,
                                                 bool need_depth,
                                                 bool need_stencil) {
  ResolveAndDetach();

  if (mailbox.IsZero())
    return true;

  if (passthrough_) {
    shared_image_representation_ =
        shared_image_representation_factory_->ProduceGLTexturePassthrough(
            mailbox);
  } else {
    shared_image_representation_ =
        shared_image_representation_factory_->ProduceGLTexture(mailbox);
  }

  if (!shared_image_representation_) {
    LOG(ERROR) << "Can't produce representation";
    return false;
  }

  if (!shared_image_representation_->format().is_single_plane() ||
      (shared_image_representation_->format() !=
           viz::SinglePlaneFormat::kRGBA_8888 &&
       shared_image_representation_->format() !=
           viz::SinglePlaneFormat::kRGBX_8888)) {
    LOG(ERROR) << "Unsupported format";
    return false;
  }

  scoped_access_ = shared_image_representation_->BeginScopedAccess(
      GL_SHARED_IMAGE_ACCESS_MODE_READWRITE_CHROMIUM,
      GLTextureImageRepresentationBase::AllowUnclearedAccess::kYes);

  if (!scoped_access_) {
    LOG(ERROR) << "Can't BeginAccess";
    return false;
  }

  samples = std::min(samples, max_sample_count_);
  const bool can_attach_directly = !samples && !preserve;

  GLenum clear_flags = 0;
  const auto& size = shared_image_representation_->size();

  gl::GLApi* const api = gl::g_current_gl_context;
  ScopedRestoreFramebuffer scoped_fbo_reset(api,
                                            supports_separate_fbo_bindings_);
  api->glBindFramebufferEXTFn(GL_FRAMEBUFFER, fbo_);

  if (can_attach_directly) {
    glFramebufferTexture2DEXT(
        GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
        shared_image_representation_->GetTextureBase()->service_id(), 0);
    if (!shared_image_representation_->IsCleared())
      clear_flags |= GL_COLOR_BUFFER_BIT;
  } else {
    const bool has_alpha = shared_image_representation_->format() ==
                           viz::SinglePlaneFormat::kRGBA_8888;
    if (UpdateAttachment(GL_COLOR_ATTACHMENT0, size, samples,
                         has_alpha ? GL_RGBA8 : GL_RGB8)) {
      clear_flags |= GL_COLOR_BUFFER_BIT;
    }
  }

  // If GL_DEPTH24_STENCIL8 is supported, we prefer it.
  if (packed_depth_stencil_) {
    if (UpdateAttachment(
            GL_DEPTH_STENCIL_ATTACHMENT, size, samples,
            (need_depth || need_stencil) ? GL_DEPTH24_STENCIL8 : GL_NONE)) {
      clear_flags |= GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
    }
  } else {
    if (UpdateAttachment(GL_DEPTH_ATTACHMENT, size, samples,
                         need_depth ? GL_DEPTH_COMPONENT16 : GL_NONE)) {
      clear_flags |= GL_DEPTH_BUFFER_BIT;
    }
    if (UpdateAttachment(GL_STENCIL_ATTACHMENT, size, samples,
                         need_stencil ? GL_STENCIL_INDEX8 : GL_NONE)) {
      clear_flags |= GL_STENCIL_BUFFER_BIT;
    }
  }

  GLenum status = api->glCheckFramebufferStatusEXTFn(GL_FRAMEBUFFER);
  LOG_IF(DFATAL, status != GL_FRAMEBUFFER_COMPLETE)
      << "Framebuffer incomplete: " << status;

  if (clear_flags) {
    gl::ScopedCapability scoped_scissor(GL_SCISSOR_TEST, GL_FALSE);

    std::optional<ScopedRestoreWindowRectangles> window_rectangles_restore;
    if (supports_window_rectangles_) {
      window_rectangles_restore.emplace(api);
      api->glWindowRectanglesEXTFn(GL_EXCLUSIVE_EXT, 0, nullptr);
    }

    ScopedRestoreWriteMasks write_mask_restore(api);
    api->glColorMaskFn(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    api->glDepthMaskFn(GL_TRUE);
    api->glStencilMaskSeparateFn(GL_FRONT, 0xFF);
    api->glStencilMaskSeparateFn(GL_BACK, 0xFF);

    ScopedRestoreClearValues clear_values_restore(api);
    api->glClearColorFn(0, 0, 0, 0);
    api->glClearDepthFn(0.0f);
    api->glClearStencilFn(0);

    api->glClearFn(clear_flags);

    // If we attached SharedImage directly and did clear color attachment, mark
    // it as cleared.
    if (attachments_.find(GL_COLOR_ATTACHMENT0) == attachments_.end() &&
        (clear_flags & GL_COLOR_BUFFER_BIT))
      shared_image_representation_->SetCleared();
  }

  return true;
}

bool GLES2ExternalFramebuffer::UpdateAttachment(GLenum attachment,
                                                const gfx::Size& size,
                                                int samples,
                                                GLenum format) {
  if (auto old_attachment = attachments_.find(attachment);
      old_attachment != attachments_.end()) {
    if (old_attachment->second->Compatible(size, samples, format))
      return false;
    old_attachment->second->Detach();
    attachments_.erase(attachment);
  }

  if (format) {
    attachments_[attachment] =
        CreateAttachment(attachment, size, samples, format);
    attachments_[attachment]->Attach(attachment);
    return true;
  }
  return false;
}

std::unique_ptr<GLES2ExternalFramebuffer::Attachment>
GLES2ExternalFramebuffer::CreateAttachment(GLenum attachment,
                                           const gfx::Size& size,
                                           int samples,
                                           GLenum format) {
  if (attachment == GL_COLOR_ATTACHMENT0 && samples == 0) {
    return Attachment::CreateTexture(size, format);
  }

  return Attachment::CreateRenderbuffer(size, samples, format);
}

void GLES2ExternalFramebuffer::ResolveAndDetach() {
  if (!scoped_access_) {
    DCHECK(!shared_image_representation_);
    return;
  }

  gl::GLApi* const api = gl::g_current_gl_context;
  ScopedRestoreFramebuffer scoped_fbo_reset(api,
                                            supports_separate_fbo_bindings_);

  if (auto color_attachment = attachments_.find(GL_COLOR_ATTACHMENT0);
      color_attachment != attachments_.end()) {
    const auto& size = shared_image_representation_->size();

    // If we have separate attachment, we need to blit/resolve it to shared
    // image.
    if (color_attachment->second->NeedsResolve()) {
      DCHECK(supports_separate_fbo_bindings_);

      gl::ScopedCapability scoped_scissor(GL_SCISSOR_TEST, GL_FALSE);
      ScopedRestoreWriteMasks write_mask_restore(api);
      api->glColorMaskFn(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

      std::optional<ScopedRestoreWindowRectangles> window_rectangles_restore;
      if (supports_window_rectangles_) {
        window_rectangles_restore.emplace(api);
        api->glWindowRectanglesEXTFn(GL_EXCLUSIVE_EXT, 0, nullptr);
      }

      api->glBindFramebufferEXTFn(GL_READ_FRAMEBUFFER, fbo_);

      GLuint temp_fbo;
      api->glGenFramebuffersEXTFn(1, &temp_fbo);
      api->glBindFramebufferEXTFn(GL_DRAW_FRAMEBUFFER, temp_fbo);
      api->glFramebufferTexture2DEXTFn(
          GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
          shared_image_representation_->GetTextureBase()->service_id(), 0);

      api->glBlitFramebufferFn(0, 0, size.width(), size.height(), 0, 0,
                               size.width(), size.height(), GL_COLOR_BUFFER_BIT,
                               GL_NEAREST);

      api->glDeleteFramebuffersEXTFn(1, &temp_fbo);
    } else {
      api->glBindFramebufferEXTFn(GL_FRAMEBUFFER, fbo_);
      gl::ScopedRestoreTexture texture(api, GL_TEXTURE_2D);
      api->glBindTextureFn(
          GL_TEXTURE_2D,
          shared_image_representation_->GetTextureBase()->service_id());

      api->glCopyTexSubImage2DFn(GL_TEXTURE_2D, 0, 0, 0, 0, 0, size.width(),
                                 size.height());
    }
    // We did resolved to SharedImage, so we can mark it as cleared here.
    shared_image_representation_->SetCleared();
  } else {
    // Detach color attachment if we were attached directly.
    api->glBindFramebufferEXTFn(GL_FRAMEBUFFER, fbo_);
    api->glFramebufferTexture2DEXTFn(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_2D, 0, 0);
  }
  scoped_access_.reset();
  shared_image_representation_.reset();
}

GLuint GLES2ExternalFramebuffer::GetFramebufferId() const {
  return fbo_;
}

bool GLES2ExternalFramebuffer::IsSharedImageAttached() const {
  return !!scoped_access_;
}

gfx::Size GLES2ExternalFramebuffer::GetSize() const {
  DCHECK(IsSharedImageAttached());
  return shared_image_representation_->size();
}

GLenum GLES2ExternalFramebuffer::GetColorFormat() const {
  DCHECK(IsSharedImageAttached());
  auto it = attachments_.find(GL_COLOR_ATTACHMENT0);
  CHECK(it != attachments_.end());
  return it->second->format();
}

GLenum GLES2ExternalFramebuffer::GetDepthFormat() const {
  DCHECK(IsSharedImageAttached());
  if (auto it = attachments_.find(GL_DEPTH_STENCIL_ATTACHMENT);
      it != attachments_.end()) {
    return it->second->format();
  }

  if (auto it = attachments_.find(GL_DEPTH_ATTACHMENT);
      it != attachments_.end()) {
    return it->second->format();
  }

  return GL_NONE;
}

GLenum GLES2ExternalFramebuffer::GetStencilFormat() const {
  DCHECK(IsSharedImageAttached());
  DCHECK(IsSharedImageAttached());
  if (auto it = attachments_.find(GL_DEPTH_STENCIL_ATTACHMENT);
      it != attachments_.end()) {
    return it->second->format();
  }

  if (auto it = attachments_.find(GL_STENCIL_ATTACHMENT);
      it != attachments_.end()) {
    return it->second->format();
  }

  return GL_NONE;
}

int GLES2ExternalFramebuffer::GetSamplesCount() const {
  DCHECK(IsSharedImageAttached());
  auto it = attachments_.find(GL_COLOR_ATTACHMENT0);
  CHECK(it != attachments_.end());
  return it->second->samples_count();
}

bool GLES2ExternalFramebuffer::HasAlpha() const {
  DCHECK(IsSharedImageAttached());
  auto it = attachments_.find(GL_COLOR_ATTACHMENT0);
  CHECK(it != attachments_.end());
  return it->second->format() == GL_RGBA8;
}

bool GLES2ExternalFramebuffer::HasDepth() const {
  return GetDepthFormat() != GL_NONE;
}

bool GLES2ExternalFramebuffer::HasStencil() const {
  return GetStencilFormat() != GL_NONE;
}

}  // namespace gpu::gles2