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