summaryrefslogtreecommitdiffstats
path: root/content/common/gpu/image_transport_surface_fbo_mac.mm
blob: 102a6b306f898e90f99aade538b73ce88b854227 (plain)
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
// Copyright 2014 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 "content/common/gpu/image_transport_surface_fbo_mac.h"

#include "base/trace_event/trace_event.h"
#include "content/common/gpu/gpu_messages.h"
#include "content/common/gpu/image_transport_surface_calayer_mac.h"
#include "content/common/gpu/image_transport_surface_iosurface_mac.h"
#include "ui/base/cocoa/remote_layer_api.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_surface_osmesa.h"

namespace content {

scoped_refptr<gfx::GLSurface> ImageTransportSurfaceCreateNativeSurface(
    GpuChannelManager* manager,
    GpuCommandBufferStub* stub,
    gfx::PluginWindowHandle handle) {
  return new ImageTransportSurfaceFBO(manager, stub, handle);
}

ImageTransportSurfaceFBO::ImageTransportSurfaceFBO(
    GpuChannelManager* manager,
    GpuCommandBufferStub* stub,
    gfx::PluginWindowHandle handle)
    : backbuffer_suggested_allocation_(true),
      frontbuffer_suggested_allocation_(true),
      fbo_id_(0),
      texture_id_(0),
      depth_stencil_renderbuffer_id_(0),
      has_complete_framebuffer_(false),
      context_(NULL),
      scale_factor_(1.f),
      made_current_(false),
      is_swap_buffers_send_pending_(false) {
  if (ui::RemoteLayerAPISupported())
    storage_provider_.reset(new CALayerStorageProvider(this));
  else
    storage_provider_.reset(new IOSurfaceStorageProvider(this));
  helper_.reset(new ImageTransportHelper(this, manager, stub, handle));
}

ImageTransportSurfaceFBO::~ImageTransportSurfaceFBO() {
}

bool ImageTransportSurfaceFBO::Initialize() {
  // Only support IOSurfaces if the GL implementation is the native desktop GL.
  // IO surfaces will not work with, for example, OSMesa software renderer
  // GL contexts.
  if (gfx::GetGLImplementation() !=
      gfx::kGLImplementationDesktopGLCoreProfile &&
      gfx::GetGLImplementation() != gfx::kGLImplementationDesktopGL &&
      gfx::GetGLImplementation() != gfx::kGLImplementationAppleGL)
    return false;

  if (!helper_->Initialize())
    return false;

  helper_->stub()->AddDestructionObserver(this);
  return true;
}

void ImageTransportSurfaceFBO::Destroy() {
  DestroyFramebuffer();
}

bool ImageTransportSurfaceFBO::DeferDraws() {
  storage_provider_->WillWriteToBackbuffer();
  // We should not have a pending send when we are drawing the next frame.
  DCHECK(!is_swap_buffers_send_pending_);

  // The call to WillWriteToBackbuffer could potentially force a draw. Ensure
  // that any changes made to the context's state are restored.
  context_->RestoreStateIfDirtiedExternally();
  return false;
}

bool ImageTransportSurfaceFBO::IsOffscreen() {
  return false;
}

bool ImageTransportSurfaceFBO::OnMakeCurrent(gfx::GLContext* context) {
  context_ = context;

  if (made_current_)
    return true;

  AllocateOrResizeFramebuffer(gfx::Size(1, 1), 1.f);

  made_current_ = true;
  return true;
}

void ImageTransportSurfaceFBO::NotifyWasBound() {
  // Sometimes calling glBindFramebuffer doesn't seem to be enough to get
  // rendered contents to show up in the color attachment. It appears that doing
  // a glBegin/End pair with program 0 is enough to tickle the driver into
  // actually effecting the binding.
  // http://crbug.com/435786
  DCHECK(has_complete_framebuffer_);

  // We will restore the current program after the dummy glBegin/End pair.
  // Ensure that we will be able to restore this state before attempting to
  // change it.
  GLint old_program_signed = 0;
  glGetIntegerv(GL_CURRENT_PROGRAM, &old_program_signed);
  GLuint old_program = static_cast<GLuint>(old_program_signed);
  if (old_program && glIsProgram(old_program)) {
    // A deleted program cannot be re-bound.
    GLint delete_status = GL_FALSE;
    glGetProgramiv(old_program, GL_DELETE_STATUS, &delete_status);
    if (delete_status == GL_TRUE)
      return;
    // A program which has had the most recent link fail cannot be re-bound.
    GLint link_status = GL_FALSE;
    glGetProgramiv(old_program, GL_LINK_STATUS, &link_status);
    if (link_status != GL_TRUE)
      return;
  }

  // Issue the dummy call and then restore the state.
  glUseProgram(0);
  GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  DCHECK(status == GL_FRAMEBUFFER_COMPLETE);
  if (gfx::GetGLImplementation() == gfx::kGLImplementationDesktopGL) {
    // These aren't present in the core profile.
    // TODO(ccameron): verify this workaround isn't still needed with
    // the core profile.
    glBegin(GL_TRIANGLES);
    glEnd();
  }
  glUseProgram(old_program);
}

unsigned int ImageTransportSurfaceFBO::GetBackingFrameBufferObject() {
  return fbo_id_;
}

bool ImageTransportSurfaceFBO::SetBackbufferAllocation(bool allocation) {
  if (backbuffer_suggested_allocation_ == allocation)
    return true;
  backbuffer_suggested_allocation_ = allocation;
  AdjustBufferAllocation();
  if (!allocation)
    storage_provider_->DiscardBackbuffer();
  return true;
}

void ImageTransportSurfaceFBO::SetFrontbufferAllocation(bool allocation) {
  if (frontbuffer_suggested_allocation_ == allocation)
    return;
  frontbuffer_suggested_allocation_ = allocation;
  AdjustBufferAllocation();
}

void ImageTransportSurfaceFBO::AdjustBufferAllocation() {
  // On mac, the frontbuffer and backbuffer are the same buffer. The buffer is
  // free'd when both the browser and gpu processes have Unref'd the IOSurface.
  if (!backbuffer_suggested_allocation_ &&
      !frontbuffer_suggested_allocation_ &&
      has_complete_framebuffer_) {
    DestroyFramebuffer();
  } else if (backbuffer_suggested_allocation_ && !has_complete_framebuffer_) {
    AllocateOrResizeFramebuffer(pixel_size_, scale_factor_);
  }
}

gfx::SwapResult ImageTransportSurfaceFBO::SwapBuffers() {
  TRACE_EVENT0("gpu", "ImageTransportSurfaceFBO::SwapBuffers");
  return SwapBuffersInternal() ? gfx::SwapResult::SWAP_ACK
                               : gfx::SwapResult::SWAP_FAILED;
}

bool ImageTransportSurfaceFBO::SwapBuffersInternal() {
  DCHECK(backbuffer_suggested_allocation_);
  if (!frontbuffer_suggested_allocation_)
    return true;
  glFlush();

  // It is the responsibility of the storage provider to send the swap IPC.
  is_swap_buffers_send_pending_ = true;
  storage_provider_->SwapBuffers();

  // The call to swapBuffers could potentially result in an immediate draw.
  // Ensure that any changes made to the context's state are restored.
  context_->RestoreStateIfDirtiedExternally();
  return true;
}

void ImageTransportSurfaceFBO::SendSwapBuffers(uint64 surface_handle,
                                               const gfx::Size pixel_size,
                                               float scale_factor) {
  TRACE_EVENT0("gpu", "ImageTransportSurfaceFBO::SendSwapBuffers");
  GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params;
  params.surface_handle = surface_handle;
  params.size = pixel_size;
  params.scale_factor = scale_factor;
  params.latency_info.swap(latency_info_);
  helper_->SendAcceleratedSurfaceBuffersSwapped(params);
  is_swap_buffers_send_pending_ = false;
}

void ImageTransportSurfaceFBO::SetRendererID(int renderer_id) {
  if (renderer_id)
    context_->share_group()->SetRendererID(renderer_id);
}

gfx::SwapResult ImageTransportSurfaceFBO::PostSubBuffer(int x,
                                                        int y,
                                                        int width,
                                                        int height) {
  TRACE_EVENT0("gpu", "ImageTransportSurfaceFBO::PostSubBuffer");
  return SwapBuffersInternal() ? gfx::SwapResult::SWAP_ACK
                               : gfx::SwapResult::SWAP_FAILED;
}

bool ImageTransportSurfaceFBO::SupportsPostSubBuffer() {
  return true;
}

gfx::Size ImageTransportSurfaceFBO::GetSize() {
  return pixel_size_;
}

void* ImageTransportSurfaceFBO::GetHandle() {
  return NULL;
}

void* ImageTransportSurfaceFBO::GetDisplay() {
  return NULL;
}

void ImageTransportSurfaceFBO::OnBufferPresented(
    const AcceleratedSurfaceMsg_BufferPresented_Params& params) {
  TRACE_EVENT0("gpu", "ImageTransportSurfaceFBO::OnBufferPresented");
  SetRendererID(params.renderer_id);
  storage_provider_->SwapBuffersAckedByBrowser(params.disable_throttling);
}

void ImageTransportSurfaceFBO::OnResize(gfx::Size pixel_size,
                                        float scale_factor) {
  TRACE_EVENT2("gpu", "ImageTransportSurfaceFBO::OnResize",
               "old_size", pixel_size_.ToString(),
               "new_size", pixel_size.ToString());
  // Caching |context_| from OnMakeCurrent. It should still be current.
  DCHECK(context_->IsCurrent(this));

  AllocateOrResizeFramebuffer(pixel_size, scale_factor);
}

void ImageTransportSurfaceFBO::SetLatencyInfo(
    const std::vector<ui::LatencyInfo>& latency_info) {
  for (size_t i = 0; i < latency_info.size(); i++)
    latency_info_.push_back(latency_info[i]);
}

void ImageTransportSurfaceFBO::WakeUpGpu() {
  NOTIMPLEMENTED();
}

void ImageTransportSurfaceFBO::OnWillDestroyStub() {
  helper_->stub()->RemoveDestructionObserver(this);
  Destroy();
}

void ImageTransportSurfaceFBO::DestroyFramebuffer() {
  // If we have resources to destroy, then make sure that we have a current
  // context which we can use to delete the resources.
  if (context_ || fbo_id_ || texture_id_ || depth_stencil_renderbuffer_id_) {
    DCHECK(gfx::GLContext::GetCurrent() == context_);
    DCHECK(context_->IsCurrent(this));
    DCHECK(CGLGetCurrentContext());
  }

  if (fbo_id_) {
    glDeleteFramebuffersEXT(1, &fbo_id_);
    fbo_id_ = 0;
  }

  if (texture_id_) {
    glDeleteTextures(1, &texture_id_);
    texture_id_ = 0;
  }

  if (depth_stencil_renderbuffer_id_) {
    glDeleteRenderbuffersEXT(1, &depth_stencil_renderbuffer_id_);
    depth_stencil_renderbuffer_id_ = 0;
  }

  storage_provider_->FreeColorBufferStorage();

  has_complete_framebuffer_ = false;
}

void ImageTransportSurfaceFBO::AllocateOrResizeFramebuffer(
    const gfx::Size& new_pixel_size, float new_scale_factor) {
  gfx::Size new_rounded_pixel_size =
      storage_provider_->GetRoundedSize(new_pixel_size);

  // Only recreate the surface's storage when the rounded up size has changed,
  // or the scale factor has changed.
  bool needs_new_storage =
      !has_complete_framebuffer_ ||
      new_rounded_pixel_size != rounded_pixel_size_ ||
      new_scale_factor != scale_factor_;

  // Save the new storage parameters.
  pixel_size_ = new_pixel_size;
  rounded_pixel_size_ = new_rounded_pixel_size;
  scale_factor_ = new_scale_factor;

  if (!needs_new_storage) {
    storage_provider_->FrameSizeChanged(pixel_size_, scale_factor_);
    return;
  }

  TRACE_EVENT2("gpu", "ImageTransportSurfaceFBO::AllocateOrResizeFramebuffer",
               "width", new_rounded_pixel_size.width(),
               "height", new_rounded_pixel_size.height());

  // GL_TEXTURE_RECTANGLE_ARB is the best supported render target on
  // Mac OS X and is required for IOSurface interoperability.
  GLint previous_texture_id = 0;

  GLenum texture_target = GL_TEXTURE_RECTANGLE_ARB;
  GLenum texture_binding_target = GL_TEXTURE_BINDING_RECTANGLE_ARB;
  // However, the remote core animation path on the core profile will
  // be the preferred combination going forward.
  if (gfx::GetGLImplementation() ==
      gfx::kGLImplementationDesktopGLCoreProfile &&
      ui::RemoteLayerAPISupported()) {
    texture_target = GL_TEXTURE_2D;
    texture_binding_target = GL_TEXTURE_BINDING_2D;
  }

  glGetIntegerv(texture_binding_target, &previous_texture_id);

  // Free the old IO Surface first to reduce memory fragmentation.
  DestroyFramebuffer();

  glGenFramebuffersEXT(1, &fbo_id_);
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_id_);

  glGenTextures(1, &texture_id_);

  glBindTexture(texture_target, texture_id_);
  glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(texture_target,
                  GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(texture_target,
                  GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
                            GL_COLOR_ATTACHMENT0_EXT,
                            texture_target,
                            texture_id_,
                            0);

  // Search through the provided attributes; if the caller has
  // requested a stencil buffer, try to get one.

  int32 stencil_bits =
      helper_->stub()->GetRequestedAttribute(EGL_STENCIL_SIZE);
  if (stencil_bits > 0) {
    // Create and bind the stencil buffer
    bool has_packed_depth_stencil =
         GLSurface::ExtensionsContain(
             reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)),
                                            "GL_EXT_packed_depth_stencil");

    if (has_packed_depth_stencil) {
      glGenRenderbuffersEXT(1, &depth_stencil_renderbuffer_id_);
      glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,
                            depth_stencil_renderbuffer_id_);
      glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT,
                              rounded_pixel_size_.width(),
                              rounded_pixel_size_.height());
      glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
                                  GL_STENCIL_ATTACHMENT_EXT,
                                  GL_RENDERBUFFER_EXT,
                                  depth_stencil_renderbuffer_id_);
    }

    // If we asked for stencil but the extension isn't present,
    // it's OK to silently fail; subsequent code will/must check
    // for the presence of a stencil buffer before attempting to
    // do stencil-based operations.
  }

  bool allocated_color_buffer = storage_provider_->AllocateColorBufferStorage(
      static_cast<CGLContextObj>(
          context_->GetHandle()),
          context_->GetStateWasDirtiedExternallyCallback(),
          texture_id_, rounded_pixel_size_, scale_factor_);
  if (!allocated_color_buffer) {
    DLOG(ERROR) << "Failed to allocate color buffer storage.";
    DestroyFramebuffer();
    return;
  }

  GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
    DLOG(ERROR) << "Framebuffer was incomplete: " << status;
    DestroyFramebuffer();
    return;
  }

  has_complete_framebuffer_ = true;
  storage_provider_->FrameSizeChanged(pixel_size_, scale_factor_);

  glBindTexture(texture_target, previous_texture_id);
  // The FBO remains bound for this GL context.
}

}  // namespace content