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