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