blob: 205a6466fc1e432d752616741945ea282d8000f8 [file] [log] [blame]
revemanb195f41d2015-11-19 22:16:481// 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>
avibc5337b2015-12-25 23:16:3310#include <stdint.h>
dcheng31759da2016-04-21 01:26:3111
revemanb195f41d2015-11-19 22:16:4812#include <algorithm>
dcheng27f7483f2015-12-29 22:26:5613#include <utility>
revemanb195f41d2015-11-19 22:16:4814
revemanae3b7c852016-02-25 01:50:0315#include "base/callback_helpers.h"
revemanb195f41d2015-11-19 22:16:4816#include "base/logging.h"
avibc5337b2015-12-25 23:16:3317#include "base/macros.h"
dcheng31759da2016-04-21 01:26:3118#include "base/memory/ptr_util.h"
revemanae3b7c852016-02-25 01:50:0319#include "base/memory/weak_ptr.h"
gab7966d312016-05-11 20:35:0120#include "base/threading/thread_task_runner_handle.h"
revemanae3b7c852016-02-25 01:50:0321#include "base/time/time.h"
revemanb195f41d2015-11-19 22:16:4822#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"
revemanc8623d5b2016-02-09 02:29:1027#include "gpu/command_buffer/client/context_support.h"
revemanb195f41d2015-11-19 22:16:4828#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
33namespace exo {
34namespace {
35
revemanae3b7c852016-02-25 01:50:0336// The amount of time before we wait for release queries using
37// GetQueryObjectuivEXT(GL_QUERY_RESULT_EXT).
38const int kWaitForReleaseDelayMs = 500;
39
revemanb195f41d2015-11-19 22:16:4840GLenum 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
reveman5954fa12016-05-18 21:56:3848 GL_RGB, // BGR_565
revemanb195f41d2015-11-19 22:16:4849 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
dcastagnacbea4262016-06-08 00:26:0254 GL_RGB_YCRCB_420_CHROMIUM, // YVU_420
revemanb195f41d2015-11-19 22:16:4855 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
revemanc8623d5b2016-02-09 02:29:1066unsigned 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
78void 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
revemanb195f41d2015-11-19 22:16:4888} // namespace
89
90////////////////////////////////////////////////////////////////////////////////
revemanced21f862015-11-24 00:42:4991// Buffer::Texture
revemanb195f41d2015-11-19 22:16:4892
revemanced21f862015-11-24 00:42:4993// Encapsulates the state and logic needed to bind a buffer to a GLES2 texture.
revemand0b75c32016-08-12 02:46:5294class Buffer::Texture : public ui::ContextFactoryObserver {
revemanced21f862015-11-24 00:42:4995 public:
revemand0b75c32016-08-12 02:46:5296 Texture(ui::ContextFactory* context_factory,
97 cc::ContextProvider* context_provider);
98 Texture(ui::ContextFactory* context_factory,
99 cc::ContextProvider* context_provider,
revemanc8623d5b2016-02-09 02:29:10100 gfx::GpuMemoryBuffer* gpu_memory_buffer,
101 unsigned texture_target,
102 unsigned query_type);
revemand0b75c32016-08-12 02:46:52103 ~Texture() override;
104
105 // Overridden from ui::ContextFactoryObserver:
106 void OnLostResources() override;
revemanced21f862015-11-24 00:42:49107
108 // Returns true if GLES2 resources for texture have been lost.
109 bool IsLost();
110
revemanc8623d5b2016-02-09 02:29:10111 // 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
revemanced21f862015-11-24 00:42:49119 // from a different context.
120 gpu::SyncToken BindTexImage();
121
revemanc8623d5b2016-02-09 02:29:10122 // 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);
revemanced21f862015-11-24 00:42:49133
134 // Returns the mailbox for this texture.
135 gpu::Mailbox mailbox() const { return mailbox_; }
136
137 private:
revemand0b75c32016-08-12 02:46:52138 void DestroyResources();
revemanae3b7c852016-02-25 01:50:03139 void ReleaseWhenQueryResultIsAvailable(const base::Closure& callback);
140 void Released();
141 void ScheduleWaitForRelease(base::TimeDelta delay);
142 void WaitForRelease();
143
revemand0b75c32016-08-12 02:46:52144 ui::ContextFactory* context_factory_;
revemanced21f862015-11-24 00:42:49145 scoped_refptr<cc::ContextProvider> context_provider_;
146 const unsigned texture_target_;
revemanc8623d5b2016-02-09 02:29:10147 const unsigned query_type_;
148 const GLenum internalformat_;
reveman2d3815d2016-06-26 20:13:25149 unsigned image_id_ = 0;
150 unsigned query_id_ = 0;
151 unsigned texture_id_ = 0;
revemanced21f862015-11-24 00:42:49152 gpu::Mailbox mailbox_;
revemanae3b7c852016-02-25 01:50:03153 base::Closure release_callback_;
154 base::TimeTicks wait_for_release_time_;
reveman2d3815d2016-06-26 20:13:25155 bool wait_for_release_pending_ = false;
revemanae3b7c852016-02-25 01:50:03156 base::WeakPtrFactory<Texture> weak_ptr_factory_;
revemanced21f862015-11-24 00:42:49157
158 DISALLOW_COPY_AND_ASSIGN(Texture);
159};
160
revemand0b75c32016-08-12 02:46:52161Buffer::Texture::Texture(ui::ContextFactory* context_factory,
162 cc::ContextProvider* context_provider)
163 : context_factory_(context_factory),
164 context_provider_(context_provider),
revemanc8623d5b2016-02-09 02:29:10165 texture_target_(GL_TEXTURE_2D),
166 query_type_(GL_COMMANDS_COMPLETED_CHROMIUM),
167 internalformat_(GL_RGBA),
revemanae3b7c852016-02-25 01:50:03168 weak_ptr_factory_(this) {
revemanced21f862015-11-24 00:42:49169 gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL();
revemanc8623d5b2016-02-09 02:29:10170 texture_id_ = CreateGLTexture(gles2, texture_target_);
revemanb195f41d2015-11-19 22:16:48171 // Generate a crypto-secure random mailbox name.
revemanc8623d5b2016-02-09 02:29:10172 CreateGLTextureMailbox(gles2, texture_id_, texture_target_, &mailbox_);
revemand0b75c32016-08-12 02:46:52173 // Provides a notification when |context_provider_| is lost.
174 context_factory_->AddObserver(this);
revemanc8623d5b2016-02-09 02:29:10175}
176
revemand0b75c32016-08-12 02:46:52177Buffer::Texture::Texture(ui::ContextFactory* context_factory,
178 cc::ContextProvider* context_provider,
revemanc8623d5b2016-02-09 02:29:10179 gfx::GpuMemoryBuffer* gpu_memory_buffer,
180 unsigned texture_target,
181 unsigned query_type)
revemand0b75c32016-08-12 02:46:52182 : context_factory_(context_factory),
183 context_provider_(context_provider),
revemanc8623d5b2016-02-09 02:29:10184 texture_target_(texture_target),
185 query_type_(query_type),
186 internalformat_(GLInternalFormat(gpu_memory_buffer->GetFormat())),
revemanae3b7c852016-02-25 01:50:03187 weak_ptr_factory_(this) {
revemanc8623d5b2016-02-09 02:29:10188 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_);
dcastagna2db83c7ef2016-06-13 23:33:22193 DLOG_IF(WARNING, !image_id_) << "Failed to create GLImage";
194
revemanc8623d5b2016-02-09 02:29:10195 gles2->GenQueriesEXT(1, &query_id_);
196 texture_id_ = CreateGLTexture(gles2, texture_target_);
revemand0b75c32016-08-12 02:46:52197 // Provides a notification when |context_provider_| is lost.
198 context_factory_->AddObserver(this);
revemanb195f41d2015-11-19 22:16:48199}
200
revemanced21f862015-11-24 00:42:49201Buffer::Texture::~Texture() {
revemand0b75c32016-08-12 02:46:52202 DestroyResources();
203 context_factory_->RemoveObserver(this);
204}
205
206void Buffer::Texture::OnLostResources() {
207 DestroyResources();
208 context_provider_ = nullptr;
revemanb195f41d2015-11-19 22:16:48209}
210
revemanced21f862015-11-24 00:42:49211bool Buffer::Texture::IsLost() {
revemand0b75c32016-08-12 02:46:52212 if (context_provider_) {
213 gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL();
214 return gles2->GetGraphicsResetStatusKHR() != GL_NO_ERROR;
215 }
216 return true;
revemanced21f862015-11-24 00:42:49217}
revemanb195f41d2015-11-19 22:16:48218
revemanc8623d5b2016-02-09 02:29:10219void Buffer::Texture::Release(const base::Closure& callback,
220 const gpu::SyncToken& sync_token,
221 bool is_lost) {
revemand0b75c32016-08-12 02:46:52222 if (context_provider_) {
223 gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL();
224 if (sync_token.HasData())
225 gles2->WaitSyncTokenCHROMIUM(sync_token.GetConstData());
226 }
revemanc8623d5b2016-02-09 02:29:10227
228 // Run callback as texture can be reused immediately after waiting for sync
229 // token.
230 callback.Run();
231}
232
revemanced21f862015-11-24 00:42:49233gpu::SyncToken Buffer::Texture::BindTexImage() {
revemanb195f41d2015-11-19 22:16:48234 gpu::SyncToken sync_token;
revemand0b75c32016-08-12 02:46:52235 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 }
revemanced21f862015-11-24 00:42:49251 return sync_token;
252}
253
revemanc8623d5b2016-02-09 02:29:10254void Buffer::Texture::ReleaseTexImage(const base::Closure& callback,
255 const gpu::SyncToken& sync_token,
256 bool is_lost) {
revemand0b75c32016-08-12 02:46:52257 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 }
revemanc8623d5b2016-02-09 02:29:10274 }
revemand0b75c32016-08-12 02:46:52275 callback.Run();
revemanc8623d5b2016-02-09 02:29:10276}
277
278gpu::SyncToken Buffer::Texture::CopyTexImage(Texture* destination,
279 const base::Closure& callback) {
revemanc8623d5b2016-02-09 02:29:10280 gpu::SyncToken sync_token;
revemand0b75c32016-08-12 02:46:52281 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 }
revemanc8623d5b2016-02-09 02:29:10304 return sync_token;
revemanced21f862015-11-24 00:42:49305}
306
revemand0b75c32016-08-12 02:46:52307void 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
revemanae3b7c852016-02-25 01:50:03318void Buffer::Texture::ReleaseWhenQueryResultIsAvailable(
319 const base::Closure& callback) {
revemand0b75c32016-08-12 02:46:52320 DCHECK(context_provider_);
revemanae3b7c852016-02-25 01:50:03321 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
332void Buffer::Texture::Released() {
333 if (!release_callback_.is_null())
334 base::ResetAndReturn(&release_callback_).Run();
335}
336
337void 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
348void 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
revemand0b75c32016-08-12 02:46:52363 if (context_provider_) {
revemanae3b7c852016-02-25 01:50:03364 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
revemanced21f862015-11-24 00:42:49377////////////////////////////////////////////////////////////////////////////////
378// Buffer, public:
379
dcheng31759da2016-04-21 01:26:31380Buffer::Buffer(std::unique_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer)
revemanc8623d5b2016-02-09 02:29:10381 : 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),
reveman2d3815d2016-06-26 20:13:25385 is_overlay_candidate_(false) {}
revemanc8623d5b2016-02-09 02:29:10386
dcheng31759da2016-04-21 01:26:31387Buffer::Buffer(std::unique_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer,
revemanc8623d5b2016-02-09 02:29:10388 unsigned texture_target,
389 unsigned query_type,
reveman244f9622016-03-04 21:33:33390 bool use_zero_copy,
391 bool is_overlay_candidate)
dcheng27f7483f2015-12-29 22:26:56392 : gpu_memory_buffer_(std::move(gpu_memory_buffer)),
revemanced21f862015-11-24 00:42:49393 texture_target_(texture_target),
revemanc8623d5b2016-02-09 02:29:10394 query_type_(query_type),
395 use_zero_copy_(use_zero_copy),
reveman2d3815d2016-06-26 20:13:25396 is_overlay_candidate_(is_overlay_candidate) {}
revemanced21f862015-11-24 00:42:49397
398Buffer::~Buffer() {}
399
dcheng31759da2016-04-21 01:26:31400std::unique_ptr<cc::SingleReleaseCallback> Buffer::ProduceTextureMailbox(
revemanc8623d5b2016-02-09 02:29:10401 cc::TextureMailbox* texture_mailbox,
reveman85b7a562016-03-17 23:27:32402 bool secure_output_only,
reveman56f345902016-06-06 03:58:28403 bool client_usage) {
jbauman45c06862016-06-23 19:35:02404 DCHECK(attach_count_);
reveman56f345902016-06-06 03:58:28405 DLOG_IF(WARNING, use_count_ && client_usage)
revemanced21f862015-11-24 00:42:49406 << "Producing a texture mailbox for a buffer that has not been released";
407
revemanc8623d5b2016-02-09 02:29:10408 // 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.
reveman56f345902016-06-06 03:58:28415 if (use_count_ && client_usage)
revemanc8623d5b2016-02-09 02:29:10416 release_callback_.Reset();
417
revemanced21f862015-11-24 00:42:49418 // Increment the use count for this buffer.
419 ++use_count_;
420
revemanc8623d5b2016-02-09 02:29:10421 // 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();
revemanced21f862015-11-24 00:42:49426
revemand0b75c32016-08-12 02:46:52427 ui::ContextFactory* context_factory =
428 aura::Env::GetInstance()->context_factory();
revemanc8623d5b2016-02-09 02:29:10429 // Note: This can fail if GPU acceleration has been disabled.
430 scoped_refptr<cc::ContextProvider> context_provider =
revemand0b75c32016-08-12 02:46:52431 context_factory->SharedMainThreadContextProvider();
revemanc8623d5b2016-02-09 02:29:10432 if (!context_provider) {
433 DLOG(WARNING) << "Failed to acquire a context provider";
434 Release(); // Decrements the use count
435 return nullptr;
revemanced21f862015-11-24 00:42:49436 }
437
revemanc8623d5b2016-02-09 02:29:10438 // 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_) {
ricea85ec57952016-08-31 09:34:10442 contents_texture_ = base::MakeUnique<Texture>(
443 context_factory, context_provider.get(), gpu_memory_buffer_.get(),
444 texture_target_, query_type_);
revemanc8623d5b2016-02-09 02:29:10445 }
revemanb195f41d2015-11-19 22:16:48446
revemanc8623d5b2016-02-09 02:29:10447 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
ccameron399d94a2016-06-22 03:08:58454 *texture_mailbox =
455 cc::TextureMailbox(texture->mailbox(), sync_token, texture_target_,
456 gpu_memory_buffer_->GetSize(), is_overlay_candidate_,
457 secure_output_only);
revemanc8623d5b2016-02-09 02:29:10458 // 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.
revemand0b75c32016-08-12 02:46:52467 if (!texture_) {
468 texture_ =
ricea85ec57952016-08-31 09:34:10469 base::MakeUnique<Texture>(context_factory, context_provider.get());
revemand0b75c32016-08-12 02:46:52470 }
revemanc8623d5b2016-02-09 02:29:10471
reveman17fdee02016-02-10 04:26:47472 // 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();
revemanc8623d5b2016-02-09 02:29:10475 Texture* texture = texture_.get();
476
477 // The contents texture will be released when copy has completed.
reveman17fdee02016-02-10 04:26:47478 gpu::SyncToken sync_token = contents_texture->CopyTexImage(
revemanc8623d5b2016-02-09 02:29:10479 texture, base::Bind(&Buffer::ReleaseContentsTexture, AsWeakPtr(),
480 base::Passed(&contents_texture_)));
ccameron399d94a2016-06-22 03:08:58481 *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);
revemanc8623d5b2016-02-09 02:29:10485 // The mailbox texture will be released when no longer used by the
486 // compositor.
revemanb195f41d2015-11-19 22:16:48487 return cc::SingleReleaseCallback::Create(
revemanc8623d5b2016-02-09 02:29:10488 base::Bind(&Buffer::Texture::Release, base::Unretained(texture),
489 base::Bind(&Buffer::ReleaseTexture, AsWeakPtr(),
490 base::Passed(&texture_))));
revemanb195f41d2015-11-19 22:16:48491}
492
jbauman45c06862016-06-23 19:35:02493void 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
499void Buffer::OnDetach() {
500 DCHECK_GT(attach_count_, 0u);
501 --attach_count_;
502 CheckReleaseCallback();
503}
504
revemanb195f41d2015-11-19 22:16:48505gfx::Size Buffer::GetSize() const {
506 return gpu_memory_buffer_->GetSize();
507}
508
dcheng31759da2016-04-21 01:26:31509std::unique_ptr<base::trace_event::TracedValue> Buffer::AsTracedValue() const {
510 std::unique_ptr<base::trace_event::TracedValue> value(
primianocb1afb32016-02-29 20:46:05511 new base::trace_event::TracedValue());
revemanced21f862015-11-24 00:42:49512 gfx::Size size = gpu_memory_buffer_->GetSize();
513 value->SetInteger("width", size.width());
514 value->SetInteger("height", size.height());
revemanb195f41d2015-11-19 22:16:48515 value->SetInteger("format",
516 static_cast<int>(gpu_memory_buffer_->GetFormat()));
517 return value;
518}
519
520////////////////////////////////////////////////////////////////////////////////
521// Buffer, private:
522
revemanced21f862015-11-24 00:42:49523void Buffer::Release() {
524 DCHECK_GT(use_count_, 0u);
jbauman45c06862016-06-23 19:35:02525 --use_count_;
526 CheckReleaseCallback();
527}
528
529void Buffer::CheckReleaseCallback() {
530 if (attach_count_ || use_count_)
revemanb195f41d2015-11-19 22:16:48531 return;
revemanb195f41d2015-11-19 22:16:48532
revemanced21f862015-11-24 00:42:49533 // Run release callback to notify the client that buffer has been released.
534 if (!release_callback_.is_null())
535 release_callback_.Run();
536}
revemanb195f41d2015-11-19 22:16:48537
dcheng31759da2016-04-21 01:26:31538void Buffer::ReleaseTexture(std::unique_ptr<Texture> texture) {
revemanc8623d5b2016-02-09 02:29:10539 texture_ = std::move(texture);
540}
revemanced21f862015-11-24 00:42:49541
dcheng31759da2016-04-21 01:26:31542void Buffer::ReleaseContentsTexture(std::unique_ptr<Texture> texture) {
revemanc8623d5b2016-02-09 02:29:10543 TRACE_EVENT0("exo", "Buffer::ReleaseContentsTexture");
revemanced21f862015-11-24 00:42:49544
revemanc8623d5b2016-02-09 02:29:10545 contents_texture_ = std::move(texture);
546 Release();
revemanb195f41d2015-11-19 22:16:48547}
548
549} // namespace exo