reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 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> |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 10 | #include <stdint.h> |
dcheng | 31759da | 2016-04-21 01:26:31 | [diff] [blame] | 11 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 12 | #include <algorithm> |
dcheng | 27f7483f | 2015-12-29 22:26:56 | [diff] [blame] | 13 | #include <utility> |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 14 | |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 15 | #include "base/callback_helpers.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 16 | #include "base/logging.h" |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 17 | #include "base/macros.h" |
dcheng | 31759da | 2016-04-21 01:26:31 | [diff] [blame] | 18 | #include "base/memory/ptr_util.h" |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 19 | #include "base/memory/weak_ptr.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 20 | #include "base/threading/thread_task_runner_handle.h" |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 21 | #include "base/time/time.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 22 | #include "base/trace_event/trace_event.h" |
| 23 | #include "base/trace_event/trace_event_argument.h" |
| 24 | #include "cc/output/context_provider.h" |
| 25 | #include "cc/resources/single_release_callback.h" |
| 26 | #include "cc/resources/texture_mailbox.h" |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 27 | #include "components/exo/compositor_frame_sink_holder.h" |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 28 | #include "gpu/command_buffer/client/context_support.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 29 | #include "gpu/command_buffer/client/gles2_interface.h" |
| 30 | #include "ui/aura/env.h" |
| 31 | #include "ui/compositor/compositor.h" |
| 32 | #include "ui/gfx/gpu_memory_buffer.h" |
| 33 | |
| 34 | namespace exo { |
| 35 | namespace { |
| 36 | |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 37 | // The amount of time before we wait for release queries using |
| 38 | // GetQueryObjectuivEXT(GL_QUERY_RESULT_EXT). |
| 39 | const int kWaitForReleaseDelayMs = 500; |
| 40 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 41 | GLenum GLInternalFormat(gfx::BufferFormat format) { |
| 42 | const GLenum kGLInternalFormats[] = { |
| 43 | GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD, // ATC |
| 44 | GL_COMPRESSED_RGB_S3TC_DXT1_EXT, // ATCIA |
| 45 | GL_COMPRESSED_RGB_S3TC_DXT1_EXT, // DXT1 |
| 46 | GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, // DXT5 |
| 47 | GL_ETC1_RGB8_OES, // ETC1 |
| 48 | GL_R8_EXT, // R_8 |
dongseong.hwang | 961b0e1 | 2016-10-04 10:15:30 | [diff] [blame] | 49 | GL_RG8_EXT, // RG_88 |
reveman | 5954fa1 | 2016-05-18 21:56:38 | [diff] [blame] | 50 | GL_RGB, // BGR_565 |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 51 | GL_RGBA, // RGBA_4444 |
| 52 | GL_RGB, // RGBX_8888 |
| 53 | GL_RGBA, // RGBA_8888 |
| 54 | GL_RGB, // BGRX_8888 |
| 55 | GL_BGRA_EXT, // BGRA_8888 |
ccameron | c1fcde5 | 2017-04-12 19:40:49 | [diff] [blame^] | 56 | GL_RGBA, // RGBA_F16 |
dcastagna | cbea426 | 2016-06-08 00:26:02 | [diff] [blame] | 57 | GL_RGB_YCRCB_420_CHROMIUM, // YVU_420 |
dcastagna | 23d30bfd | 2016-09-24 02:06:20 | [diff] [blame] | 58 | GL_RGB_YCBCR_420V_CHROMIUM, // YUV_420_BIPLANAR |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 59 | GL_RGB_YCBCR_422_CHROMIUM, // UYVY_422 |
| 60 | }; |
| 61 | static_assert(arraysize(kGLInternalFormats) == |
| 62 | (static_cast<int>(gfx::BufferFormat::LAST) + 1), |
| 63 | "BufferFormat::LAST must be last value of kGLInternalFormats"); |
| 64 | |
| 65 | DCHECK(format <= gfx::BufferFormat::LAST); |
| 66 | return kGLInternalFormats[static_cast<int>(format)]; |
| 67 | } |
| 68 | |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 69 | unsigned CreateGLTexture(gpu::gles2::GLES2Interface* gles2, GLenum target) { |
| 70 | unsigned texture_id = 0; |
| 71 | gles2->GenTextures(1, &texture_id); |
| 72 | gles2->ActiveTexture(GL_TEXTURE0); |
| 73 | gles2->BindTexture(target, texture_id); |
| 74 | gles2->TexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 75 | gles2->TexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 76 | gles2->TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 77 | gles2->TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 78 | return texture_id; |
| 79 | } |
| 80 | |
| 81 | void CreateGLTextureMailbox(gpu::gles2::GLES2Interface* gles2, |
| 82 | unsigned texture_id, |
| 83 | GLenum target, |
| 84 | gpu::Mailbox* mailbox) { |
| 85 | gles2->ActiveTexture(GL_TEXTURE0); |
| 86 | gles2->BindTexture(target, texture_id); |
| 87 | gles2->GenMailboxCHROMIUM(mailbox->name); |
| 88 | gles2->ProduceTextureCHROMIUM(target, mailbox->name); |
| 89 | } |
| 90 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 91 | } // namespace |
| 92 | |
| 93 | //////////////////////////////////////////////////////////////////////////////// |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 94 | // Buffer::Texture |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 95 | |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 96 | // Encapsulates the state and logic needed to bind a buffer to a GLES2 texture. |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 97 | class Buffer::Texture : public ui::ContextFactoryObserver { |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 98 | public: |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 99 | Texture(ui::ContextFactory* context_factory, |
| 100 | cc::ContextProvider* context_provider); |
| 101 | Texture(ui::ContextFactory* context_factory, |
| 102 | cc::ContextProvider* context_provider, |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 103 | gfx::GpuMemoryBuffer* gpu_memory_buffer, |
| 104 | unsigned texture_target, |
| 105 | unsigned query_type); |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 106 | ~Texture() override; |
| 107 | |
| 108 | // Overridden from ui::ContextFactoryObserver: |
| 109 | void OnLostResources() override; |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 110 | |
| 111 | // Returns true if GLES2 resources for texture have been lost. |
| 112 | bool IsLost(); |
| 113 | |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 114 | // Allow texture to be reused after |sync_token| has passed and runs |
| 115 | // |callback|. |
| 116 | void Release(const base::Closure& callback, |
| 117 | const gpu::SyncToken& sync_token, |
| 118 | bool is_lost); |
| 119 | |
| 120 | // Binds the contents referenced by |image_id_| to the texture returned by |
| 121 | // mailbox(). Returns a sync token that can be used when accessing texture |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 122 | // from a different context. |
| 123 | gpu::SyncToken BindTexImage(); |
| 124 | |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 125 | // Releases the contents referenced by |image_id_| after |sync_token| has |
| 126 | // passed and runs |callback| when completed. |
| 127 | void ReleaseTexImage(const base::Closure& callback, |
| 128 | const gpu::SyncToken& sync_token, |
| 129 | bool is_lost); |
| 130 | |
| 131 | // Copy the contents of texture to |destination| and runs |callback| when |
| 132 | // completed. Returns a sync token that can be used when accessing texture |
| 133 | // from a different context. |
| 134 | gpu::SyncToken CopyTexImage(Texture* destination, |
| 135 | const base::Closure& callback); |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 136 | |
| 137 | // Returns the mailbox for this texture. |
| 138 | gpu::Mailbox mailbox() const { return mailbox_; } |
| 139 | |
| 140 | private: |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 141 | void DestroyResources(); |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 142 | void ReleaseWhenQueryResultIsAvailable(const base::Closure& callback); |
| 143 | void Released(); |
| 144 | void ScheduleWaitForRelease(base::TimeDelta delay); |
| 145 | void WaitForRelease(); |
| 146 | |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 147 | ui::ContextFactory* context_factory_; |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 148 | scoped_refptr<cc::ContextProvider> context_provider_; |
| 149 | const unsigned texture_target_; |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 150 | const unsigned query_type_; |
| 151 | const GLenum internalformat_; |
reveman | 2d3815d | 2016-06-26 20:13:25 | [diff] [blame] | 152 | unsigned image_id_ = 0; |
| 153 | unsigned query_id_ = 0; |
| 154 | unsigned texture_id_ = 0; |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 155 | gpu::Mailbox mailbox_; |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 156 | base::Closure release_callback_; |
| 157 | base::TimeTicks wait_for_release_time_; |
reveman | 2d3815d | 2016-06-26 20:13:25 | [diff] [blame] | 158 | bool wait_for_release_pending_ = false; |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 159 | base::WeakPtrFactory<Texture> weak_ptr_factory_; |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 160 | |
| 161 | DISALLOW_COPY_AND_ASSIGN(Texture); |
| 162 | }; |
| 163 | |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 164 | Buffer::Texture::Texture(ui::ContextFactory* context_factory, |
| 165 | cc::ContextProvider* context_provider) |
| 166 | : context_factory_(context_factory), |
| 167 | context_provider_(context_provider), |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 168 | texture_target_(GL_TEXTURE_2D), |
| 169 | query_type_(GL_COMMANDS_COMPLETED_CHROMIUM), |
| 170 | internalformat_(GL_RGBA), |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 171 | weak_ptr_factory_(this) { |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 172 | gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 173 | texture_id_ = CreateGLTexture(gles2, texture_target_); |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 174 | // Generate a crypto-secure random mailbox name. |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 175 | CreateGLTextureMailbox(gles2, texture_id_, texture_target_, &mailbox_); |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 176 | // Provides a notification when |context_provider_| is lost. |
| 177 | context_factory_->AddObserver(this); |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 178 | } |
| 179 | |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 180 | Buffer::Texture::Texture(ui::ContextFactory* context_factory, |
| 181 | cc::ContextProvider* context_provider, |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 182 | gfx::GpuMemoryBuffer* gpu_memory_buffer, |
| 183 | unsigned texture_target, |
| 184 | unsigned query_type) |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 185 | : context_factory_(context_factory), |
| 186 | context_provider_(context_provider), |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 187 | texture_target_(texture_target), |
| 188 | query_type_(query_type), |
| 189 | internalformat_(GLInternalFormat(gpu_memory_buffer->GetFormat())), |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 190 | weak_ptr_factory_(this) { |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 191 | gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
| 192 | gfx::Size size = gpu_memory_buffer->GetSize(); |
| 193 | image_id_ = |
| 194 | gles2->CreateImageCHROMIUM(gpu_memory_buffer->AsClientBuffer(), |
| 195 | size.width(), size.height(), internalformat_); |
dcastagna | 2db83c7ef | 2016-06-13 23:33:22 | [diff] [blame] | 196 | DLOG_IF(WARNING, !image_id_) << "Failed to create GLImage"; |
| 197 | |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 198 | gles2->GenQueriesEXT(1, &query_id_); |
| 199 | texture_id_ = CreateGLTexture(gles2, texture_target_); |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 200 | // Provides a notification when |context_provider_| is lost. |
| 201 | context_factory_->AddObserver(this); |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 202 | } |
| 203 | |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 204 | Buffer::Texture::~Texture() { |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 205 | DestroyResources(); |
| 206 | context_factory_->RemoveObserver(this); |
| 207 | } |
| 208 | |
| 209 | void Buffer::Texture::OnLostResources() { |
| 210 | DestroyResources(); |
| 211 | context_provider_ = nullptr; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 212 | } |
| 213 | |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 214 | bool Buffer::Texture::IsLost() { |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 215 | if (context_provider_) { |
| 216 | gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
| 217 | return gles2->GetGraphicsResetStatusKHR() != GL_NO_ERROR; |
| 218 | } |
| 219 | return true; |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 220 | } |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 221 | |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 222 | void Buffer::Texture::Release(const base::Closure& callback, |
| 223 | const gpu::SyncToken& sync_token, |
| 224 | bool is_lost) { |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 225 | if (context_provider_) { |
| 226 | gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
| 227 | if (sync_token.HasData()) |
| 228 | gles2->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); |
| 229 | } |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 230 | |
| 231 | // Run callback as texture can be reused immediately after waiting for sync |
| 232 | // token. |
| 233 | callback.Run(); |
| 234 | } |
| 235 | |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 236 | gpu::SyncToken Buffer::Texture::BindTexImage() { |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 237 | gpu::SyncToken sync_token; |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 238 | if (context_provider_) { |
| 239 | gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
| 240 | gles2->ActiveTexture(GL_TEXTURE0); |
| 241 | gles2->BindTexture(texture_target_, texture_id_); |
| 242 | DCHECK_NE(image_id_, 0u); |
| 243 | gles2->BindTexImage2DCHROMIUM(texture_target_, image_id_); |
| 244 | // Generate a crypto-secure random mailbox name if not already done. |
| 245 | if (mailbox_.IsZero()) |
| 246 | CreateGLTextureMailbox(gles2, texture_id_, texture_target_, &mailbox_); |
| 247 | // Create and return a sync token that can be used to ensure that the |
| 248 | // BindTexImage2DCHROMIUM call is processed before issuing any commands |
| 249 | // that will read from the texture on a different context. |
| 250 | uint64_t fence_sync = gles2->InsertFenceSyncCHROMIUM(); |
| 251 | gles2->OrderingBarrierCHROMIUM(); |
| 252 | gles2->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); |
| 253 | } |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 254 | return sync_token; |
| 255 | } |
| 256 | |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 257 | void Buffer::Texture::ReleaseTexImage(const base::Closure& callback, |
| 258 | const gpu::SyncToken& sync_token, |
| 259 | bool is_lost) { |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 260 | if (context_provider_) { |
| 261 | gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
| 262 | if (sync_token.HasData()) |
| 263 | gles2->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); |
| 264 | gles2->ActiveTexture(GL_TEXTURE0); |
| 265 | gles2->BindTexture(texture_target_, texture_id_); |
| 266 | DCHECK_NE(query_id_, 0u); |
| 267 | gles2->BeginQueryEXT(query_type_, query_id_); |
| 268 | gles2->ReleaseTexImage2DCHROMIUM(texture_target_, image_id_); |
| 269 | gles2->EndQueryEXT(query_type_); |
| 270 | // Run callback when query result is available and ReleaseTexImage has been |
| 271 | // handled if sync token has data and buffer has been used. If buffer was |
| 272 | // never used then run the callback immediately. |
| 273 | if (sync_token.HasData()) { |
| 274 | ReleaseWhenQueryResultIsAvailable(callback); |
| 275 | return; |
| 276 | } |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 277 | } |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 278 | callback.Run(); |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | gpu::SyncToken Buffer::Texture::CopyTexImage(Texture* destination, |
| 282 | const base::Closure& callback) { |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 283 | gpu::SyncToken sync_token; |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 284 | if (context_provider_) { |
| 285 | gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
| 286 | gles2->ActiveTexture(GL_TEXTURE0); |
| 287 | gles2->BindTexture(texture_target_, texture_id_); |
| 288 | DCHECK_NE(image_id_, 0u); |
| 289 | gles2->BindTexImage2DCHROMIUM(texture_target_, image_id_); |
qiankun.miao | 728aa86 | 2017-01-22 15:03:03 | [diff] [blame] | 290 | gles2->CopyTextureCHROMIUM(texture_id_, 0, destination->texture_target_, |
| 291 | destination->texture_id_, 0, internalformat_, |
| 292 | GL_UNSIGNED_BYTE, false, false, false); |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 293 | DCHECK_NE(query_id_, 0u); |
| 294 | gles2->BeginQueryEXT(query_type_, query_id_); |
| 295 | gles2->ReleaseTexImage2DCHROMIUM(texture_target_, image_id_); |
| 296 | gles2->EndQueryEXT(query_type_); |
| 297 | // Run callback when query result is available and ReleaseTexImage has been |
| 298 | // handled. |
| 299 | ReleaseWhenQueryResultIsAvailable(callback); |
| 300 | // Create and return a sync token that can be used to ensure that the |
| 301 | // CopyTextureCHROMIUM call is processed before issuing any commands |
| 302 | // that will read from the target texture on a different context. |
| 303 | uint64_t fence_sync = gles2->InsertFenceSyncCHROMIUM(); |
| 304 | gles2->OrderingBarrierCHROMIUM(); |
| 305 | gles2->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); |
| 306 | } |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 307 | return sync_token; |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 308 | } |
| 309 | |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 310 | void Buffer::Texture::DestroyResources() { |
| 311 | if (context_provider_) { |
| 312 | gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
| 313 | gles2->DeleteTextures(1, &texture_id_); |
| 314 | if (query_id_) |
| 315 | gles2->DeleteQueriesEXT(1, &query_id_); |
| 316 | if (image_id_) |
| 317 | gles2->DestroyImageCHROMIUM(image_id_); |
| 318 | } |
| 319 | } |
| 320 | |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 321 | void Buffer::Texture::ReleaseWhenQueryResultIsAvailable( |
| 322 | const base::Closure& callback) { |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 323 | DCHECK(context_provider_); |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 324 | DCHECK(release_callback_.is_null()); |
| 325 | release_callback_ = callback; |
| 326 | base::TimeDelta wait_for_release_delay = |
| 327 | base::TimeDelta::FromMilliseconds(kWaitForReleaseDelayMs); |
| 328 | wait_for_release_time_ = base::TimeTicks::Now() + wait_for_release_delay; |
| 329 | ScheduleWaitForRelease(wait_for_release_delay); |
| 330 | context_provider_->ContextSupport()->SignalQuery( |
| 331 | query_id_, |
| 332 | base::Bind(&Buffer::Texture::Released, weak_ptr_factory_.GetWeakPtr())); |
| 333 | } |
| 334 | |
| 335 | void Buffer::Texture::Released() { |
| 336 | if (!release_callback_.is_null()) |
| 337 | base::ResetAndReturn(&release_callback_).Run(); |
| 338 | } |
| 339 | |
| 340 | void Buffer::Texture::ScheduleWaitForRelease(base::TimeDelta delay) { |
| 341 | if (wait_for_release_pending_) |
| 342 | return; |
| 343 | |
| 344 | wait_for_release_pending_ = true; |
| 345 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 346 | FROM_HERE, base::Bind(&Buffer::Texture::WaitForRelease, |
| 347 | weak_ptr_factory_.GetWeakPtr()), |
| 348 | delay); |
| 349 | } |
| 350 | |
| 351 | void Buffer::Texture::WaitForRelease() { |
| 352 | DCHECK(wait_for_release_pending_); |
| 353 | wait_for_release_pending_ = false; |
| 354 | |
| 355 | if (release_callback_.is_null()) |
| 356 | return; |
| 357 | |
| 358 | base::TimeTicks current_time = base::TimeTicks::Now(); |
| 359 | if (current_time < wait_for_release_time_) { |
| 360 | ScheduleWaitForRelease(wait_for_release_time_ - current_time); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | base::Closure callback = base::ResetAndReturn(&release_callback_); |
| 365 | |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 366 | if (context_provider_) { |
reveman | ae3b7c85 | 2016-02-25 01:50:03 | [diff] [blame] | 367 | TRACE_EVENT0("exo", "Buffer::Texture::WaitForQueryResult"); |
| 368 | |
| 369 | // We need to wait for the result to be available. Getting the result of |
| 370 | // the query implies waiting for it to become available. The actual result |
| 371 | // is unimportant and also not well defined. |
| 372 | unsigned result = 0; |
| 373 | gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
| 374 | gles2->GetQueryObjectuivEXT(query_id_, GL_QUERY_RESULT_EXT, &result); |
| 375 | } |
| 376 | |
| 377 | callback.Run(); |
| 378 | } |
| 379 | |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 380 | //////////////////////////////////////////////////////////////////////////////// |
| 381 | // Buffer, public: |
| 382 | |
dcheng | 31759da | 2016-04-21 01:26:31 | [diff] [blame] | 383 | Buffer::Buffer(std::unique_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer) |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 384 | : gpu_memory_buffer_(std::move(gpu_memory_buffer)), |
| 385 | texture_target_(GL_TEXTURE_2D), |
| 386 | query_type_(GL_COMMANDS_COMPLETED_CHROMIUM), |
| 387 | use_zero_copy_(true), |
reveman | 2d3815d | 2016-06-26 20:13:25 | [diff] [blame] | 388 | is_overlay_candidate_(false) {} |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 389 | |
dcheng | 31759da | 2016-04-21 01:26:31 | [diff] [blame] | 390 | Buffer::Buffer(std::unique_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer, |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 391 | unsigned texture_target, |
| 392 | unsigned query_type, |
reveman | 244f962 | 2016-03-04 21:33:33 | [diff] [blame] | 393 | bool use_zero_copy, |
| 394 | bool is_overlay_candidate) |
dcheng | 27f7483f | 2015-12-29 22:26:56 | [diff] [blame] | 395 | : gpu_memory_buffer_(std::move(gpu_memory_buffer)), |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 396 | texture_target_(texture_target), |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 397 | query_type_(query_type), |
| 398 | use_zero_copy_(use_zero_copy), |
reveman | 2d3815d | 2016-06-26 20:13:25 | [diff] [blame] | 399 | is_overlay_candidate_(is_overlay_candidate) {} |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 400 | |
| 401 | Buffer::~Buffer() {} |
| 402 | |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 403 | bool Buffer::ProduceTransferableResource( |
| 404 | CompositorFrameSinkHolder* compositor_frame_sink_holder, |
| 405 | cc::ResourceId resource_id, |
reveman | 85b7a56 | 2016-03-17 23:27:32 | [diff] [blame] | 406 | bool secure_output_only, |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 407 | bool client_usage, |
| 408 | cc::TransferableResource* resource) { |
jbauman | 45c0686 | 2016-06-23 19:35:02 | [diff] [blame] | 409 | DCHECK(attach_count_); |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 410 | DLOG_IF(WARNING, !release_contents_callback_.IsCancelled() && client_usage) |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 411 | << "Producing a texture mailbox for a buffer that has not been released"; |
| 412 | |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 413 | // If textures are lost, destroy them to ensure that we create new ones below. |
| 414 | if (contents_texture_ && contents_texture_->IsLost()) |
| 415 | contents_texture_.reset(); |
| 416 | if (texture_ && texture_->IsLost()) |
| 417 | texture_.reset(); |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 418 | |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 419 | ui::ContextFactory* context_factory = |
| 420 | aura::Env::GetInstance()->context_factory(); |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 421 | // Note: This can fail if GPU acceleration has been disabled. |
| 422 | scoped_refptr<cc::ContextProvider> context_provider = |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 423 | context_factory->SharedMainThreadContextProvider(); |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 424 | if (!context_provider) { |
| 425 | DLOG(WARNING) << "Failed to acquire a context provider"; |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 426 | resource->id = 0; |
| 427 | resource->size = gfx::Size(); |
| 428 | return false; |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 429 | } |
| 430 | |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 431 | // The reference to the CompositorFrameSinkHolder keeps it alive until a |
| 432 | // release callback is received. |
| 433 | compositor_frame_sink_holder_ = compositor_frame_sink_holder; |
| 434 | |
| 435 | resource->id = resource_id; |
| 436 | resource->format = cc::RGBA_8888; |
| 437 | resource->filter = GL_LINEAR; |
| 438 | resource->size = gpu_memory_buffer_->GetSize(); |
| 439 | |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 440 | // Create a new image texture for |gpu_memory_buffer_| with |texture_target_| |
| 441 | // if one doesn't already exist. The contents of this buffer are copied to |
| 442 | // |texture| using a call to CopyTexImage. |
| 443 | if (!contents_texture_) { |
ricea | 85ec5795 | 2016-08-31 09:34:10 | [diff] [blame] | 444 | contents_texture_ = base::MakeUnique<Texture>( |
| 445 | context_factory, context_provider.get(), gpu_memory_buffer_.get(), |
| 446 | texture_target_, query_type_); |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 447 | } |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 448 | Texture* contents_texture = contents_texture_.get(); |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 449 | |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 450 | // Cancel pending contents release callback. |
| 451 | release_contents_callback_.Reset( |
| 452 | base::Bind(&Buffer::ReleaseContents, base::Unretained(this))); |
| 453 | |
| 454 | // Zero-copy means using the contents texture directly. |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 455 | if (use_zero_copy_) { |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 456 | // This binds the latest contents of this buffer to |contents_texture|. |
| 457 | gpu::SyncToken sync_token = contents_texture->BindTexImage(); |
| 458 | resource->mailbox_holder = gpu::MailboxHolder(contents_texture->mailbox(), |
| 459 | sync_token, texture_target_); |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 460 | resource->is_overlay_candidate = is_overlay_candidate_; |
dcastagna | 8409354 | 2017-03-14 02:11:13 | [diff] [blame] | 461 | resource->buffer_format = gpu_memory_buffer_->GetFormat(); |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 462 | |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 463 | // The contents texture will be released when no longer used by the |
| 464 | // compositor. |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 465 | compositor_frame_sink_holder_->SetResourceReleaseCallback( |
| 466 | resource_id, |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 467 | base::Bind(&Buffer::Texture::ReleaseTexImage, |
| 468 | base::Unretained(contents_texture), |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 469 | base::Bind(&Buffer::ReleaseContentsTexture, AsWeakPtr(), |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 470 | base::Passed(&contents_texture_), |
| 471 | release_contents_callback_.callback()))); |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 472 | return true; |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // Create a mailbox texture that we copy the buffer contents to. |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 476 | if (!texture_) { |
| 477 | texture_ = |
ricea | 85ec5795 | 2016-08-31 09:34:10 | [diff] [blame] | 478 | base::MakeUnique<Texture>(context_factory, context_provider.get()); |
reveman | d0b75c3 | 2016-08-12 02:46:52 | [diff] [blame] | 479 | } |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 480 | Texture* texture = texture_.get(); |
| 481 | |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 482 | // Copy the contents of |contents_texture| to |texture| and produce a |
| 483 | // texture mailbox from the result in |texture|. The contents texture will |
| 484 | // be released when copy has completed. |
reveman | 7ee8b4e | 2017-01-11 22:10:53 | [diff] [blame] | 485 | gpu::SyncToken sync_token = contents_texture->CopyTexImage( |
| 486 | texture, base::Bind(&Buffer::ReleaseContentsTexture, AsWeakPtr(), |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 487 | base::Passed(&contents_texture_), |
| 488 | release_contents_callback_.callback())); |
reveman | 7ee8b4e | 2017-01-11 22:10:53 | [diff] [blame] | 489 | resource->mailbox_holder = |
| 490 | gpu::MailboxHolder(texture->mailbox(), sync_token, GL_TEXTURE_2D); |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 491 | resource->is_overlay_candidate = false; |
| 492 | |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 493 | // The mailbox texture will be released when no longer used by the |
| 494 | // compositor. |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 495 | compositor_frame_sink_holder_->SetResourceReleaseCallback( |
| 496 | resource_id, |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 497 | base::Bind(&Buffer::Texture::Release, base::Unretained(texture), |
| 498 | base::Bind(&Buffer::ReleaseTexture, AsWeakPtr(), |
| 499 | base::Passed(&texture_)))); |
staraz | d40f036 | 2017-01-04 00:46:13 | [diff] [blame] | 500 | return true; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 501 | } |
| 502 | |
jbauman | 45c0686 | 2016-06-23 19:35:02 | [diff] [blame] | 503 | void Buffer::OnAttach() { |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 504 | DLOG_IF(WARNING, attach_count_) |
jbauman | 45c0686 | 2016-06-23 19:35:02 | [diff] [blame] | 505 | << "Reattaching a buffer that is already attached to another surface."; |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 506 | ++attach_count_; |
jbauman | 45c0686 | 2016-06-23 19:35:02 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void Buffer::OnDetach() { |
| 510 | DCHECK_GT(attach_count_, 0u); |
| 511 | --attach_count_; |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 512 | |
| 513 | // Release buffer if no longer attached to a surface and content has been |
| 514 | // released. |
| 515 | if (!attach_count_ && release_contents_callback_.IsCancelled()) |
| 516 | Release(); |
jbauman | 45c0686 | 2016-06-23 19:35:02 | [diff] [blame] | 517 | } |
| 518 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 519 | gfx::Size Buffer::GetSize() const { |
| 520 | return gpu_memory_buffer_->GetSize(); |
| 521 | } |
| 522 | |
reveman | ca132dc | 2017-01-31 22:35:54 | [diff] [blame] | 523 | gfx::BufferFormat Buffer::GetFormat() const { |
| 524 | return gpu_memory_buffer_->GetFormat(); |
| 525 | } |
| 526 | |
dcheng | 31759da | 2016-04-21 01:26:31 | [diff] [blame] | 527 | std::unique_ptr<base::trace_event::TracedValue> Buffer::AsTracedValue() const { |
| 528 | std::unique_ptr<base::trace_event::TracedValue> value( |
primiano | cb1afb3 | 2016-02-29 20:46:05 | [diff] [blame] | 529 | new base::trace_event::TracedValue()); |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 530 | gfx::Size size = gpu_memory_buffer_->GetSize(); |
| 531 | value->SetInteger("width", size.width()); |
| 532 | value->SetInteger("height", size.height()); |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 533 | value->SetInteger("format", |
| 534 | static_cast<int>(gpu_memory_buffer_->GetFormat())); |
| 535 | return value; |
| 536 | } |
| 537 | |
| 538 | //////////////////////////////////////////////////////////////////////////////// |
| 539 | // Buffer, private: |
| 540 | |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 541 | void Buffer::Release() { |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 542 | // Run release callback to notify the client that buffer has been released. |
| 543 | if (!release_callback_.is_null()) |
| 544 | release_callback_.Run(); |
| 545 | } |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 546 | |
dcheng | 31759da | 2016-04-21 01:26:31 | [diff] [blame] | 547 | void Buffer::ReleaseTexture(std::unique_ptr<Texture> texture) { |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 548 | texture_ = std::move(texture); |
| 549 | } |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 550 | |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 551 | void Buffer::ReleaseContentsTexture(std::unique_ptr<Texture> texture, |
| 552 | const base::Closure& callback) { |
reveman | c8623d5b | 2016-02-09 02:29:10 | [diff] [blame] | 553 | contents_texture_ = std::move(texture); |
reveman | 2cd6978 | 2017-02-01 20:19:44 | [diff] [blame] | 554 | callback.Run(); |
| 555 | } |
| 556 | |
| 557 | void Buffer::ReleaseContents() { |
| 558 | TRACE_EVENT0("exo", "Buffer::ReleaseContents"); |
| 559 | |
| 560 | // Cancel callback to indicate that buffer has been released. |
| 561 | release_contents_callback_.Cancel(); |
| 562 | |
| 563 | // Release buffer if not attached to surface. |
| 564 | if (!attach_count_) |
| 565 | Release(); |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | } // namespace exo |