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