Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/MailboxTextureHolder.cpp

Issue 2455983005: Refactor AcceleratedStaticBitmapImage (Closed)
Patch Set: no crash! && address comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "platform/graphics/MailboxTextureHolder.h"
6
7 #include "gpu/command_buffer/client/gles2_interface.h"
8 #include "platform/CrossThreadFunctional.h"
9 #include "platform/graphics/SkiaTextureHolder.h"
10 #include "platform/graphics/WebGraphicsContext3DProviderWrapper.h"
11 #include "platform/graphics/gpu/SharedGpuContext.h"
12 #include "public/platform/Platform.h"
13 #include "skia/ext/texture_handle.h"
14 #include "third_party/skia/include/gpu/GrContext.h"
15
16 namespace blink {
17
18 namespace {
19
20 void releaseTexture(
21 bool isConvertedFromSkiaTexture,
22 unsigned textureId,
23 WeakPtr<WebGraphicsContext3DProviderWrapper> contextProvider,
24 std::unique_ptr<gpu::SyncToken> syncToken) {
25 if (!isConvertedFromSkiaTexture && textureId && contextProvider) {
26 contextProvider->contextProvider()->contextGL()->WaitSyncTokenCHROMIUM(
27 syncToken->GetData());
28 contextProvider->contextProvider()->contextGL()->DeleteTextures(1,
29 &textureId);
30 }
31 }
32
33 } // namespace
34
35 MailboxTextureHolder::MailboxTextureHolder(
36 const gpu::Mailbox& mailbox,
37 const gpu::SyncToken& syncToken,
38 unsigned textureIdToDeleteAfterMailboxConsumed,
39 WeakPtr<WebGraphicsContext3DProviderWrapper> contextProvider,
40 IntSize mailboxSize)
41 : m_mailbox(mailbox),
42 m_syncToken(syncToken),
43 m_textureId(textureIdToDeleteAfterMailboxConsumed),
44 m_contextProvider(contextProvider),
45 m_size(mailboxSize),
46 m_isConvertedFromSkiaTexture(false) {}
47
48 MailboxTextureHolder::MailboxTextureHolder(
49 std::unique_ptr<TextureHolder> textureHolder) {
50 DCHECK(textureHolder->isSkiaTextureHolder());
51 sk_sp<SkImage> image = textureHolder->skImage();
52 DCHECK(image);
53
54 gpu::gles2::GLES2Interface* sharedGL = SharedGpuContext::gl();
55 GrContext* sharedGrContext = SharedGpuContext::gr();
56 if (!sharedGrContext) {
57 // Can happen if the context is lost. The SkImage won't be any good now
58 // anyway.
59 return;
60 }
61 GLuint imageTextureId =
62 skia::GrBackendObjectToGrGLTextureInfo(image->getTextureHandle(true))
63 ->fID;
64 sharedGL->BindTexture(GL_TEXTURE_2D, imageTextureId);
65
66 sharedGL->GenMailboxCHROMIUM(m_mailbox.name);
67 sharedGL->ProduceTextureCHROMIUM(GL_TEXTURE_2D, m_mailbox.name);
68 const GLuint64 fenceSync = sharedGL->InsertFenceSyncCHROMIUM();
69 sharedGL->Flush();
70 sharedGL->GenSyncTokenCHROMIUM(fenceSync, m_syncToken.GetData());
71
72 sharedGL->BindTexture(GL_TEXTURE_2D, 0);
73 // We changed bound textures in this function, so reset the GrContext.
74 sharedGrContext->resetContext(kTextureBinding_GrGLBackendState);
75 m_size = IntSize(image->width(), image->height());
76 m_textureId = imageTextureId;
77 m_isConvertedFromSkiaTexture = true;
78 }
79
80 MailboxTextureHolder::~MailboxTextureHolder() {
81 // Avoid leaking mailboxes in cases where the texture gets recycled by skia.
82 if (SharedGpuContext::isValid()) {
83 SharedGpuContext::gl()->ProduceTextureDirectCHROMIUM(0, GL_TEXTURE_2D,
84 m_mailbox.name);
85 }
86 releaseTextureThreadSafe();
87 }
88
89 void MailboxTextureHolder::releaseTextureThreadSafe() {
90 // If this member is still null, it means we are still at the thread where
91 // the m_texture was created.
92 std::unique_ptr<gpu::SyncToken> passedSyncToken(
93 new gpu::SyncToken(m_syncToken));
94 if (!wasTransferred()) {
95 releaseTexture(m_isConvertedFromSkiaTexture, m_textureId, m_contextProvider,
96 std::move(passedSyncToken));
97 } else if (wasTransferred() && textureThreadTaskRunner()) {
98 textureThreadTaskRunner()->postTask(
99 BLINK_FROM_HERE,
100 crossThreadBind(&releaseTexture, m_isConvertedFromSkiaTexture,
101 m_textureId, passed(std::move(m_contextProvider)),
102 passed(std::move(passedSyncToken))));
103 }
104 m_textureId = 0u; // invalidate the texture.
105 setWasTransferred(false);
106 setTextureThreadTaskRunner(nullptr);
107 }
108
109 unsigned MailboxTextureHolder::sharedContextId() {
110 return SharedGpuContext::kNoSharedContext;
111 }
112
113 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698