Chromium Code Reviews| 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 gpu::gles2::GLES2Interface* GetContextGL() { | |
| 54 ui::ContextFactory* context_factory = | |
| 55 aura::Env::GetInstance()->context_factory(); | |
| 56 return context_factory->SharedMainThreadContextProvider()->ContextGL(); | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 //////////////////////////////////////////////////////////////////////////////// | |
| 62 // Buffer, public: | |
| 63 | |
| 64 Buffer::Buffer(scoped_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer, | |
| 65 unsigned texture_target) | |
| 66 : gpu_memory_buffer_(gpu_memory_buffer.Pass()), | |
| 67 texture_target_(texture_target), | |
| 68 texture_id_(0), | |
| 69 image_id_(GetContextGL()->CreateImageCHROMIUM( | |
| 70 gpu_memory_buffer_->AsClientBuffer(), | |
| 71 gpu_memory_buffer_->GetSize().width(), | |
| 72 gpu_memory_buffer_->GetSize().height(), | |
| 73 GLInternalFormat(gpu_memory_buffer_->GetFormat()))), | |
| 74 weak_ptr_factory_(this) {} | |
| 75 | |
| 76 Buffer::~Buffer() { | |
| 77 gpu::gles2::GLES2Interface* gles2 = GetContextGL(); | |
| 78 if (texture_id_) | |
| 79 gles2->DeleteTextures(1, &texture_id_); | |
| 80 if (image_id_) | |
| 81 gles2->DestroyImageCHROMIUM(image_id_); | |
| 82 } | |
| 83 | |
| 84 scoped_ptr<cc::SingleReleaseCallback> Buffer::GetTextureMailbox( | |
| 85 cc::TextureMailbox* texture_mailbox) { | |
| 86 // Buffer can only be used by one client at a time. If there's no image, | |
| 87 // then a previous call to GetTextureMailbox() is using it and it has not | |
| 88 // been released yet. | |
| 89 if (!image_id_) { | |
| 90 DLOG(WARNING) << "Client tried to use a buffer that has not been released"; | |
| 91 return nullptr; | |
| 92 } | |
| 93 | |
| 94 // Take ownerhsip of image and texture ids. | |
| 95 unsigned texture_id = 0; | |
| 96 unsigned image_id = 0; | |
| 97 std::swap(texture_id, texture_id_); | |
| 98 std::swap(image_id, image_id_); | |
| 99 | |
| 100 // Create a new texture if needed or bind existing one. | |
| 101 gpu::gles2::GLES2Interface* gles2 = GetContextGL(); | |
| 102 gles2->ActiveTexture(GL_TEXTURE0); | |
| 103 if (!texture_id) { | |
| 104 gles2->GenTextures(1, &texture_id); | |
| 105 gles2->BindTexture(texture_target_, texture_id); | |
| 106 gles2->TexParameteri(texture_target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 107 gles2->TexParameteri(texture_target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
| 108 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 109 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 110 } else { | |
| 111 gles2->BindTexture(texture_target_, texture_id); | |
| 112 } | |
| 113 | |
| 114 // Bind the image to texture. | |
| 115 gles2->BindTexImage2DCHROMIUM(texture_target_, image_id); | |
| 116 | |
| 117 // Create a mailbox for texture. | |
| 118 gpu::Mailbox mailbox; | |
| 119 gles2->GenMailboxCHROMIUM(mailbox.name); | |
|
piman
2015/11/17 02:43:53
mailboxe names are not free to create (crypto-secu
reveman
2015/11/17 18:04:04
Good point. Yes, buffers are meant to be reused. B
| |
| 120 gles2->ProduceTextureCHROMIUM(texture_target_, mailbox.name); | |
| 121 uint64 fence_sync = gles2->InsertFenceSyncCHROMIUM(); | |
| 122 gles2->OrderingBarrierCHROMIUM(); | |
| 123 gpu::SyncToken sync_token; | |
| 124 gles2->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); | |
| 125 | |
| 126 bool is_overlay_candidate = false; | |
| 127 *texture_mailbox = | |
| 128 cc::TextureMailbox(mailbox, sync_token, texture_target_, | |
| 129 gpu_memory_buffer_->GetSize(), is_overlay_candidate); | |
| 130 return cc::SingleReleaseCallback::Create( | |
| 131 base::Bind(&Buffer::Release, weak_ptr_factory_.GetWeakPtr(), | |
| 132 texture_target_, texture_id, image_id)) | |
| 133 .Pass(); | |
| 134 } | |
| 135 | |
| 136 scoped_refptr<base::trace_event::TracedValue> Buffer::AsTracedValue() const { | |
| 137 scoped_refptr<base::trace_event::TracedValue> value = | |
| 138 new base::trace_event::TracedValue; | |
| 139 value->SetInteger("width", size_.width()); | |
| 140 value->SetInteger("height", size_.height()); | |
| 141 value->SetInteger("format", | |
| 142 static_cast<int>(gpu_memory_buffer_->GetFormat())); | |
| 143 return value; | |
| 144 } | |
| 145 | |
| 146 //////////////////////////////////////////////////////////////////////////////// | |
| 147 // Buffer, private: | |
| 148 | |
| 149 // static | |
| 150 void Buffer::Release(base::WeakPtr<Buffer> buffer, | |
| 151 unsigned texture_target, | |
| 152 unsigned texture_id, | |
| 153 unsigned image_id, | |
| 154 const gpu::SyncToken& sync_token, | |
| 155 bool is_lost) { | |
| 156 TRACE_EVENT1("exo", "Buffer::Release", "is_lost", is_lost); | |
| 157 | |
| 158 gpu::gles2::GLES2Interface* gles2 = GetContextGL(); | |
| 159 gles2->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); | |
| 160 gles2->ActiveTexture(GL_TEXTURE0); | |
| 161 gles2->BindTexture(texture_target, texture_id); | |
| 162 gles2->ReleaseTexImage2DCHROMIUM(texture_target, image_id); | |
| 163 | |
| 164 // Delete resources and return if buffer is gone. | |
| 165 if (!buffer) { | |
| 166 gles2->DeleteTextures(1, &texture_id); | |
| 167 gles2->DestroyImageCHROMIUM(image_id); | |
| 168 return; | |
| 169 } | |
| 170 | |
| 171 DCHECK_EQ(buffer->texture_id_, 0u); | |
| 172 buffer->texture_id_ = texture_id; | |
| 173 DCHECK_EQ(buffer->image_id_, 0u); | |
| 174 buffer->image_id_ = image_id; | |
| 175 | |
| 176 if (!buffer->release_callback_.is_null()) | |
| 177 buffer->release_callback_.Run(); | |
| 178 } | |
| 179 | |
| 180 } // namespace exo | |
| OLD | NEW |