1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/gl/gl_image_surface_texture.h"
6
7#include "base/debug/trace_event.h"
8#include "ui/gl/android/surface_texture.h"
9#include "ui/gl/android/surface_texture_tracker.h"
10
11namespace gfx {
12
13GLImageSurfaceTexture::GLImageSurfaceTexture(const gfx::Size& size)
14    : size_(size), texture_id_(0) {
15}
16
17GLImageSurfaceTexture::~GLImageSurfaceTexture() {
18  DCHECK(!surface_texture_);
19  DCHECK_EQ(0, texture_id_);
20}
21
22bool GLImageSurfaceTexture::Initialize(
23    const gfx::GpuMemoryBufferHandle& handle) {
24  DCHECK(!surface_texture_);
25  surface_texture_ =
26      SurfaceTextureTracker::GetInstance()->AcquireSurfaceTexture(
27          handle.surface_texture_id.primary_id,
28          handle.surface_texture_id.secondary_id);
29  return !!surface_texture_;
30}
31
32void GLImageSurfaceTexture::Destroy(bool have_context) {
33  surface_texture_ = NULL;
34  texture_id_ = 0;
35}
36
37gfx::Size GLImageSurfaceTexture::GetSize() { return size_; }
38
39bool GLImageSurfaceTexture::BindTexImage(unsigned target) {
40  TRACE_EVENT0("gpu", "GLImageSurfaceTexture::BindTexImage");
41
42  if (target != GL_TEXTURE_EXTERNAL_OES) {
43    LOG(ERROR)
44        << "Surface texture can only be bound to TEXTURE_EXTERNAL_OES target";
45    return false;
46  }
47
48  GLint texture_id;
49  glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id);
50  DCHECK(texture_id);
51
52  if (texture_id_ && texture_id_ != texture_id) {
53    LOG(ERROR) << "Surface texture can only be bound to one texture ID";
54    return false;
55  }
56
57  DCHECK(surface_texture_);
58  if (texture_id != texture_id_) {
59    // Note: Surface textures used as gpu memory buffers are created with an
60    // initial dummy texture id of 0. We need to call DetachFromGLContext() here
61    // to detach from the dummy texture before we can attach to a real texture
62    // id. DetachFromGLContext() will delete the texture for the current
63    // attachment point so it's important that this is never called when
64    // attached to a real texture id. Detaching from the dummy texture id should
65    // not cause any problems as the GL should silently ignore 0 when passed to
66    // glDeleteTextures.
67    DCHECK_EQ(0, texture_id_);
68    surface_texture_->DetachFromGLContext();
69
70    // This will attach the surface texture to the texture currently bound to
71    // GL_TEXTURE_EXTERNAL_OES target.
72    surface_texture_->AttachToGLContext();
73    texture_id_ = texture_id;
74  }
75
76  surface_texture_->UpdateTexImage();
77  return true;
78}
79
80bool GLImageSurfaceTexture::CopyTexImage(unsigned target) {
81  return false;
82}
83
84bool GLImageSurfaceTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
85                                                 int z_order,
86                                                 OverlayTransform transform,
87                                                 const Rect& bounds_rect,
88                                                 const RectF& crop_rect) {
89  return false;
90}
91
92}  // namespace gfx
93