OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/exo/buffer.h" |
| 6 |
| 7 #include <GLES2/gl2.h> |
| 8 #include <GLES2/gl2ext.h> |
| 9 #include <GLES2/gl2extchromium.h> |
| 10 |
| 11 #include <algorithm> |
| 12 |
| 13 #include "base/logging.h" |
| 14 #include "base/trace_event/trace_event.h" |
| 15 #include "base/trace_event/trace_event_argument.h" |
| 16 #include "cc/output/context_provider.h" |
| 17 #include "cc/resources/single_release_callback.h" |
| 18 #include "cc/resources/texture_mailbox.h" |
| 19 #include "gpu/command_buffer/client/gles2_interface.h" |
| 20 #include "gpu/command_buffer/common/mailbox.h" |
| 21 #include "ui/aura/env.h" |
| 22 #include "ui/compositor/compositor.h" |
| 23 #include "ui/gfx/gpu_memory_buffer.h" |
| 24 |
| 25 namespace exo { |
| 26 namespace { |
| 27 |
| 28 GLenum GLInternalFormat(gfx::BufferFormat format) { |
| 29 const GLenum kGLInternalFormats[] = { |
| 30 GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD, // ATC |
| 31 GL_COMPRESSED_RGB_S3TC_DXT1_EXT, // ATCIA |
| 32 GL_COMPRESSED_RGB_S3TC_DXT1_EXT, // DXT1 |
| 33 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, // DXT5 |
| 34 GL_ETC1_RGB8_OES, // ETC1 |
| 35 GL_R8_EXT, // R_8 |
| 36 GL_RGBA, // RGBA_4444 |
| 37 GL_RGB, // RGBX_8888 |
| 38 GL_RGBA, // RGBA_8888 |
| 39 GL_RGB, // BGRX_8888 |
| 40 GL_BGRA_EXT, // BGRA_8888 |
| 41 GL_RGB_YUV_420_CHROMIUM, // YUV_420 |
| 42 GL_INVALID_ENUM, // YUV_420_BIPLANAR |
| 43 GL_RGB_YCBCR_422_CHROMIUM, // UYVY_422 |
| 44 }; |
| 45 static_assert(arraysize(kGLInternalFormats) == |
| 46 (static_cast<int>(gfx::BufferFormat::LAST) + 1), |
| 47 "BufferFormat::LAST must be last value of kGLInternalFormats"); |
| 48 |
| 49 DCHECK(format <= gfx::BufferFormat::LAST); |
| 50 return kGLInternalFormats[static_cast<int>(format)]; |
| 51 } |
| 52 |
| 53 ui::ContextFactory* ContextFactory() { |
| 54 return aura::Env::GetInstance()->context_factory(); |
| 55 } |
| 56 |
| 57 gpu::gles2::GLES2Interface* GetContextGL() { |
| 58 return ContextFactory()->SharedMainThreadContextProvider()->ContextGL(); |
| 59 } |
| 60 |
| 61 } // namespace |
| 62 |
| 63 //////////////////////////////////////////////////////////////////////////////// |
| 64 // Buffer, public: |
| 65 |
| 66 Buffer::Buffer(scoped_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer) |
| 67 : gpu_memory_buffer_(gpu_memory_buffer.Pass()), |
| 68 texture_target_(ContextFactory()->GetImageTextureTarget( |
| 69 gpu_memory_buffer_->GetFormat(), |
| 70 gfx::BufferUsage::GPU_READ)), |
| 71 texture_id_(0), |
| 72 image_id_(GetContextGL()->CreateImageCHROMIUM( |
| 73 gpu_memory_buffer_->AsClientBuffer(), |
| 74 gpu_memory_buffer_->GetSize().width(), |
| 75 gpu_memory_buffer_->GetSize().height(), |
| 76 GLInternalFormat(gpu_memory_buffer_->GetFormat()))), |
| 77 weak_ptr_factory_(this) {} |
| 78 |
| 79 Buffer::~Buffer() { |
| 80 gpu::gles2::GLES2Interface* gles2 = GetContextGL(); |
| 81 if (texture_id_) |
| 82 gles2->DeleteTextures(1, &texture_id_); |
| 83 if (image_id_) |
| 84 gles2->DestroyImageCHROMIUM(image_id_); |
| 85 } |
| 86 |
| 87 scoped_ptr<cc::SingleReleaseCallback> Buffer::GetTextureMailbox( |
| 88 cc::TextureMailbox* texture_mailbox) { |
| 89 // Buffer can only be used by one client at a time. If there's no image, |
| 90 // then a previous call to GetTextureMailbox() is using it and it has not |
| 91 // been released yet. |
| 92 if (!image_id_) { |
| 93 DLOG(WARNING) << "Client tried to use a buffer that has not been released"; |
| 94 return nullptr; |
| 95 } |
| 96 |
| 97 // Take ownerhsip of image and texture ids. |
| 98 unsigned texture_id = 0; |
| 99 unsigned image_id = 0; |
| 100 std::swap(texture_id, texture_id_); |
| 101 std::swap(image_id, image_id_); |
| 102 |
| 103 // Create a new texture if needed or bind existing one. |
| 104 gpu::gles2::GLES2Interface* gles2 = GetContextGL(); |
| 105 gles2->ActiveTexture(GL_TEXTURE0); |
| 106 if (!texture_id) { |
| 107 gles2->GenTextures(1, &texture_id); |
| 108 gles2->BindTexture(texture_target_, texture_id); |
| 109 gles2->TexParameteri(texture_target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 110 gles2->TexParameteri(texture_target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 111 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 112 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 113 } else { |
| 114 gles2->BindTexture(texture_target_, texture_id); |
| 115 } |
| 116 |
| 117 // Bind the image to texture. |
| 118 gles2->BindTexImage2DCHROMIUM(texture_target_, image_id); |
| 119 |
| 120 // Create a mailbox for texture. |
| 121 gpu::Mailbox mailbox; |
| 122 gles2->GenMailboxCHROMIUM(mailbox.name); |
| 123 gles2->ProduceTextureCHROMIUM(texture_target_, mailbox.name); |
| 124 uint64 fence_sync = gles2->InsertFenceSyncCHROMIUM(); |
| 125 gles2->OrderingBarrierCHROMIUM(); |
| 126 gpu::SyncToken sync_token; |
| 127 gles2->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); |
| 128 |
| 129 bool is_overlay_candidate = false; |
| 130 *texture_mailbox = |
| 131 cc::TextureMailbox(mailbox, sync_token, texture_target_, |
| 132 gpu_memory_buffer_->GetSize(), is_overlay_candidate); |
| 133 return cc::SingleReleaseCallback::Create( |
| 134 base::Bind(&Buffer::Release, weak_ptr_factory_.GetWeakPtr(), |
| 135 texture_target_, texture_id, image_id)) |
| 136 .Pass(); |
| 137 } |
| 138 |
| 139 scoped_refptr<base::trace_event::TracedValue> Buffer::AsTracedValue() const { |
| 140 scoped_refptr<base::trace_event::TracedValue> value = |
| 141 new base::trace_event::TracedValue; |
| 142 value->SetInteger("width", size_.width()); |
| 143 value->SetInteger("height", size_.height()); |
| 144 value->SetInteger("format", |
| 145 static_cast<int>(gpu_memory_buffer_->GetFormat())); |
| 146 return value; |
| 147 } |
| 148 |
| 149 //////////////////////////////////////////////////////////////////////////////// |
| 150 // Buffer, private: |
| 151 |
| 152 // static |
| 153 void Buffer::Release(base::WeakPtr<Buffer> buffer, |
| 154 unsigned texture_target, |
| 155 unsigned texture_id, |
| 156 unsigned image_id, |
| 157 const gpu::SyncToken& sync_token, |
| 158 bool is_lost) { |
| 159 TRACE_EVENT1("exo", "Buffer::Release", "is_lost", is_lost); |
| 160 |
| 161 gpu::gles2::GLES2Interface* gles2 = GetContextGL(); |
| 162 gles2->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); |
| 163 gles2->ActiveTexture(GL_TEXTURE0); |
| 164 gles2->BindTexture(texture_target, texture_id); |
| 165 gles2->ReleaseTexImage2DCHROMIUM(texture_target, image_id); |
| 166 |
| 167 // Delete resources and return if buffer is gone. |
| 168 if (!buffer) { |
| 169 gles2->DeleteTextures(1, &texture_id); |
| 170 gles2->DestroyImageCHROMIUM(image_id); |
| 171 return; |
| 172 } |
| 173 |
| 174 DCHECK_EQ(buffer->texture_id_, 0u); |
| 175 buffer->texture_id_ = texture_id; |
| 176 DCHECK_EQ(buffer->image_id_, 0u); |
| 177 buffer->image_id_ = image_id; |
| 178 |
| 179 if (!buffer->release_callback_.is_null()) |
| 180 buffer->release_callback_.Run(); |
| 181 } |
| 182 |
| 183 } // namespace exo |
OLD | NEW |