1 // Copyright 2013 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 "cc/resources/texture_mailbox.h" 6 7 #include "base/logging.h" 8 #include "third_party/khronos/GLES2/gl2.h" 9 10 namespace cc { 11 12 TextureMailbox::TextureMailbox() 13 : target_(GL_TEXTURE_2D), 14 sync_point_(0), 15 shared_memory_(NULL) { 16 } 17 18 TextureMailbox::TextureMailbox( 19 const std::string& mailbox_name, 20 const ReleaseCallback& callback) 21 : callback_(callback), 22 target_(GL_TEXTURE_2D), 23 sync_point_(0), 24 shared_memory_(NULL) { 25 DCHECK(mailbox_name.empty() == callback.is_null()); 26 if (!mailbox_name.empty()) { 27 CHECK(mailbox_name.size() == sizeof(name_.name)); 28 name_.SetName(reinterpret_cast<const int8*>(mailbox_name.data())); 29 } 30 } 31 32 TextureMailbox::TextureMailbox( 33 const gpu::Mailbox& mailbox_name, 34 const ReleaseCallback& callback) 35 : callback_(callback), 36 target_(GL_TEXTURE_2D), 37 sync_point_(0), 38 shared_memory_(NULL) { 39 DCHECK(mailbox_name.IsZero() == callback.is_null()); 40 name_.SetName(mailbox_name.name); 41 } 42 43 TextureMailbox::TextureMailbox( 44 const gpu::Mailbox& mailbox_name, 45 const ReleaseCallback& callback, 46 unsigned sync_point) 47 : callback_(callback), 48 target_(GL_TEXTURE_2D), 49 sync_point_(sync_point), 50 shared_memory_(NULL) { 51 DCHECK(mailbox_name.IsZero() == callback.is_null()); 52 name_.SetName(mailbox_name.name); 53 } 54 55 TextureMailbox::TextureMailbox( 56 const gpu::Mailbox& mailbox_name, 57 const ReleaseCallback& callback, 58 unsigned texture_target, 59 unsigned sync_point) 60 : callback_(callback), 61 target_(texture_target), 62 sync_point_(sync_point), 63 shared_memory_(NULL) { 64 DCHECK(mailbox_name.IsZero() == callback.is_null()); 65 name_.SetName(mailbox_name.name); 66 } 67 68 TextureMailbox::TextureMailbox( 69 base::SharedMemory* shared_memory, 70 gfx::Size size, 71 const ReleaseCallback& callback) 72 : callback_(callback), 73 target_(GL_TEXTURE_2D), 74 sync_point_(0), 75 shared_memory_(shared_memory), 76 shared_memory_size_(size) { 77 } 78 79 TextureMailbox::~TextureMailbox() { 80 } 81 82 bool TextureMailbox::Equals(const TextureMailbox& other) const { 83 if (other.IsTexture()) 84 return ContainsMailbox(other.name()); 85 else if (other.IsSharedMemory()) 86 return ContainsHandle(other.shared_memory_->handle()); 87 88 DCHECK(!other.IsValid()); 89 return !IsValid(); 90 } 91 92 bool TextureMailbox::ContainsMailbox(const gpu::Mailbox& other) const { 93 return IsTexture() && !memcmp(data(), other.name, sizeof(name_.name)); 94 } 95 96 bool TextureMailbox::ContainsHandle(base::SharedMemoryHandle handle) const { 97 return shared_memory_ && shared_memory_->handle() == handle; 98 } 99 100 void TextureMailbox::RunReleaseCallback(unsigned sync_point, 101 bool lost_resource) const { 102 if (!callback_.is_null()) 103 callback_.Run(sync_point, lost_resource); 104 } 105 106 void TextureMailbox::SetName(const gpu::Mailbox& other) { 107 DCHECK(shared_memory_ == NULL); 108 name_.SetName(other.name); 109 } 110 111 TextureMailbox TextureMailbox::CopyWithNewCallback( 112 const ReleaseCallback& callback) const { 113 TextureMailbox result(*this); 114 result.callback_ = callback; 115 return result; 116 } 117 118 size_t TextureMailbox::shared_memory_size_in_bytes() const { 119 return 4 * shared_memory_size_.GetArea(); 120 } 121 122 } // namespace cc 123