File: MailboxTextureHolder.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (112 lines) | stat: -rw-r--r-- 4,188 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "platform/graphics/MailboxTextureHolder.h"

#include "gpu/command_buffer/client/gles2_interface.h"
#include "platform/CrossThreadFunctional.h"
#include "platform/graphics/SkiaTextureHolder.h"
#include "platform/graphics/gpu/SharedGpuContext.h"
#include "public/platform/Platform.h"
#include "skia/ext/texture_handle.h"
#include "third_party/skia/include/gpu/GrContext.h"

namespace blink {

namespace {

void releaseTexture(
    bool isConvertedFromSkiaTexture,
    unsigned textureId,
    WeakPtr<WebGraphicsContext3DProviderWrapper> contextProvider,
    std::unique_ptr<gpu::SyncToken> syncToken) {
  if (!isConvertedFromSkiaTexture && textureId && contextProvider) {
    contextProvider->contextProvider()->contextGL()->WaitSyncTokenCHROMIUM(
        syncToken->GetData());
    contextProvider->contextProvider()->contextGL()->DeleteTextures(1,
                                                                    &textureId);
  }
}

}  // namespace

MailboxTextureHolder::MailboxTextureHolder(
    const gpu::Mailbox& mailbox,
    const gpu::SyncToken& syncToken,
    unsigned textureIdToDeleteAfterMailboxConsumed,
    WeakPtr<WebGraphicsContext3DProviderWrapper> contextProvider,
    IntSize mailboxSize)
    : m_mailbox(mailbox),
      m_syncToken(syncToken),
      m_textureId(textureIdToDeleteAfterMailboxConsumed),
      m_contextProvider(contextProvider),
      m_size(mailboxSize),
      m_isConvertedFromSkiaTexture(false) {}

MailboxTextureHolder::MailboxTextureHolder(
    std::unique_ptr<TextureHolder> textureHolder) {
  DCHECK(textureHolder->isSkiaTextureHolder());
  sk_sp<SkImage> image = textureHolder->skImage();
  DCHECK(image);

  gpu::gles2::GLES2Interface* sharedGL = SharedGpuContext::gl();
  GrContext* sharedGrContext = SharedGpuContext::gr();
  if (!sharedGrContext) {
    // Can happen if the context is lost. The SkImage won't be any good now
    // anyway.
    return;
  }
  GLuint imageTextureId =
      skia::GrBackendObjectToGrGLTextureInfo(image->getTextureHandle(true))
          ->fID;
  sharedGL->BindTexture(GL_TEXTURE_2D, imageTextureId);

  sharedGL->GenMailboxCHROMIUM(m_mailbox.name);
  sharedGL->ProduceTextureCHROMIUM(GL_TEXTURE_2D, m_mailbox.name);
  const GLuint64 fenceSync = sharedGL->InsertFenceSyncCHROMIUM();
  sharedGL->Flush();
  sharedGL->GenSyncTokenCHROMIUM(fenceSync, m_syncToken.GetData());

  sharedGL->BindTexture(GL_TEXTURE_2D, 0);
  // We changed bound textures in this function, so reset the GrContext.
  sharedGrContext->resetContext(kTextureBinding_GrGLBackendState);
  m_size = IntSize(image->width(), image->height());
  m_textureId = imageTextureId;
  m_isConvertedFromSkiaTexture = true;
}

MailboxTextureHolder::~MailboxTextureHolder() {
  // Avoid leaking mailboxes in cases where the texture gets recycled by skia.
  if (SharedGpuContext::isValid()) {
    SharedGpuContext::gl()->ProduceTextureDirectCHROMIUM(0, GL_TEXTURE_2D,
                                                         m_mailbox.name);
  }
  releaseTextureThreadSafe();
}

void MailboxTextureHolder::releaseTextureThreadSafe() {
  // If this member is still null, it means we are still at the thread where
  // the m_texture was created.
  std::unique_ptr<gpu::SyncToken> passedSyncToken(
      new gpu::SyncToken(m_syncToken));
  if (!wasTransferred()) {
    releaseTexture(m_isConvertedFromSkiaTexture, m_textureId, m_contextProvider,
                   std::move(passedSyncToken));
  } else if (wasTransferred() && textureThreadTaskRunner()) {
    textureThreadTaskRunner()->postTask(
        BLINK_FROM_HERE,
        crossThreadBind(&releaseTexture, m_isConvertedFromSkiaTexture,
                        m_textureId, WTF::passed(std::move(m_contextProvider)),
                        WTF::passed(std::move(passedSyncToken))));
  }
  m_textureId = 0u;  // invalidate the texture.
  setWasTransferred(false);
  setTextureThreadTaskRunner(nullptr);
}

unsigned MailboxTextureHolder::sharedContextId() {
  return SharedGpuContext::kNoSharedContext;
}

}  // namespace blink