blob: 78cab656379320c950831d61bc71881c47a2ddca [file] [log] [blame]
[email protected]82efc0d2014-03-31 06:45:021// 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
[email protected]b63f1d62014-07-18 15:40:5913GLImageSurfaceTexture::GLImageSurfaceTexture(const gfx::Size& size)
14 : size_(size), texture_id_(0) {
15}
[email protected]82efc0d2014-03-31 06:45:0216
[email protected]d2eaf52f2014-07-31 15:01:2417GLImageSurfaceTexture::~GLImageSurfaceTexture() {
18 DCHECK(!surface_texture_);
19 DCHECK_EQ(0, texture_id_);
20}
[email protected]82efc0d2014-03-31 06:45:0221
[email protected]b63f1d62014-07-18 15:40:5922bool GLImageSurfaceTexture::Initialize(
23 const gfx::GpuMemoryBufferHandle& handle) {
[email protected]82efc0d2014-03-31 06:45:0224 DCHECK(!surface_texture_);
25 surface_texture_ =
26 SurfaceTextureTracker::GetInstance()->AcquireSurfaceTexture(
[email protected]b63f1d62014-07-18 15:40:5927 handle.surface_texture_id.primary_id,
28 handle.surface_texture_id.secondary_id);
[email protected]82efc0d2014-03-31 06:45:0229 return !!surface_texture_;
30}
31
[email protected]d2eaf52f2014-07-31 15:01:2432void GLImageSurfaceTexture::Destroy(bool have_context) {
[email protected]82efc0d2014-03-31 06:45:0233 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
revemance8fbe82014-09-05 02:19:5280bool GLImageSurfaceTexture::CopyTexImage(unsigned target) {
81 return false;
82}
83
[email protected]5e57db72014-08-01 21:50:1784bool 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
[email protected]82efc0d2014-03-31 06:45:0292} // namespace gfx