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

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

Issue 2026803002: Avoid GPU readback in tex(Sub)Image2D(ImageBitmap) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: change function name, layout still broken, DONOT COMMIT THIS PS Created 4 years, 6 months 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
« no previous file with comments | « third_party/WebKit/Source/platform/graphics/StaticBitmapImage.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "platform/graphics/StaticBitmapImage.h" 5 #include "platform/graphics/StaticBitmapImage.h"
6 6
7 #include "gpu/command_buffer/client/gles2_interface.h" 7 #include "gpu/command_buffer/client/gles2_interface.h"
8 #include "platform/graphics/GraphicsContext.h" 8 #include "platform/graphics/GraphicsContext.h"
9 #include "platform/graphics/ImageObserver.h" 9 #include "platform/graphics/ImageObserver.h"
10 #include "public/platform/Platform.h" 10 #include "public/platform/Platform.h"
11 #include "public/platform/WebGraphicsContext3DProvider.h" 11 #include "public/platform/WebGraphicsContext3DProvider.h"
12 #include "skia/ext/texture_handle.h" 12 #include "skia/ext/texture_handle.h"
13 #include "third_party/skia/include/core/SkCanvas.h" 13 #include "third_party/skia/include/core/SkCanvas.h"
14 #include "third_party/skia/include/core/SkImage.h" 14 #include "third_party/skia/include/core/SkImage.h"
15 #include "third_party/skia/include/core/SkPaint.h" 15 #include "third_party/skia/include/core/SkPaint.h"
16 #include "third_party/skia/include/core/SkShader.h" 16 #include "third_party/skia/include/core/SkShader.h"
17 #include "third_party/skia/include/gpu/GrContext.h"
17 18
18 namespace blink { 19 namespace blink {
19 20
20 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(PassRefPtr<SkImage> imag e) 21 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(PassRefPtr<SkImage> imag e)
21 { 22 {
22 if (!image) 23 if (!image)
23 return nullptr; 24 return nullptr;
24 return adoptRef(new StaticBitmapImage(image)); 25 return adoptRef(new StaticBitmapImage(image));
25 } 26 }
26 27
27 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(WebExternalTextureMailbo x& mailbox) 28 PassRefPtr<StaticBitmapImage> StaticBitmapImage::create(WebExternalTextureMailbo x& mailbox)
28 { 29 {
29 return adoptRef(new StaticBitmapImage(mailbox)); 30 return adoptRef(new StaticBitmapImage(mailbox));
30 } 31 }
31 32
32 StaticBitmapImage::StaticBitmapImage(PassRefPtr<SkImage> image) : m_image(image) 33 StaticBitmapImage::StaticBitmapImage(PassRefPtr<SkImage> image) : m_image(image)
33 { 34 {
34 ASSERT(m_image); 35 ASSERT(m_image);
35 } 36 }
36 37
37 StaticBitmapImage::StaticBitmapImage(WebExternalTextureMailbox& mailbox) : m_mai lbox(mailbox) 38 StaticBitmapImage::StaticBitmapImage(WebExternalTextureMailbox& mailbox) : m_mai lbox(mailbox)
38 { 39 {
39 } 40 }
40 41
41 StaticBitmapImage::~StaticBitmapImage() { } 42 StaticBitmapImage::~StaticBitmapImage() { }
42 43
43 IntSize StaticBitmapImage::size() const 44 IntSize StaticBitmapImage::size() const
44 { 45 {
46 if (!m_image)
47 return IntSize(m_mailbox.textureSize.width, m_mailbox.textureSize.height );
45 return IntSize(m_image->width(), m_image->height()); 48 return IntSize(m_image->width(), m_image->height());
46 } 49 }
47 50
51 bool StaticBitmapImage::isTextureBacked()
52 {
53 return m_image && m_image->isTextureBacked();
54 }
55
48 bool StaticBitmapImage::currentFrameKnownToBeOpaque(MetadataMode) 56 bool StaticBitmapImage::currentFrameKnownToBeOpaque(MetadataMode)
49 { 57 {
50 return m_image->isOpaque(); 58 return m_image->isOpaque();
51 } 59 }
52 60
53 void StaticBitmapImage::draw(SkCanvas* canvas, const SkPaint& paint, const Float Rect& dstRect, 61 void StaticBitmapImage::draw(SkCanvas* canvas, const SkPaint& paint, const Float Rect& dstRect,
54 const FloatRect& srcRect, RespectImageOrientationEnum, ImageClampingMode cla mpMode) 62 const FloatRect& srcRect, RespectImageOrientationEnum, ImageClampingMode cla mpMode)
55 { 63 {
56 FloatRect adjustedSrcRect = srcRect; 64 FloatRect adjustedSrcRect = srcRect;
57 adjustedSrcRect.intersect(SkRect::Make(m_image->bounds())); 65 adjustedSrcRect.intersect(SkRect::Make(m_image->bounds()));
58 66
59 if (dstRect.isEmpty() || adjustedSrcRect.isEmpty()) 67 if (dstRect.isEmpty() || adjustedSrcRect.isEmpty())
60 return; // Nothing to draw. 68 return; // Nothing to draw.
61 69
62 canvas->drawImageRect(m_image.get(), adjustedSrcRect, dstRect, &paint, 70 canvas->drawImageRect(m_image.get(), adjustedSrcRect, dstRect, &paint,
63 WebCoreClampingModeToSkiaRectConstraint(clampMode)); 71 WebCoreClampingModeToSkiaRectConstraint(clampMode));
64 72
65 if (ImageObserver* observer = getImageObserver()) 73 if (ImageObserver* observer = getImageObserver())
66 observer->didDraw(this); 74 observer->didDraw(this);
67 } 75 }
68 76
69 PassRefPtr<SkImage> StaticBitmapImage::imageForCurrentFrame() 77 void StaticBitmapImage::switchStorageToSkImage(WebGraphicsContext3DProvider* pro vider)
70 { 78 {
71 if (m_image)
72 return m_image;
73 DCHECK(isMainThread());
74 // In the place when we consume an ImageBitmap that is gpu texture backed,
75 // create a new SkImage from that texture.
76 // TODO(xidachen): make this work on a worker thread.
77 OwnPtr<WebGraphicsContext3DProvider> provider = adoptPtr(Platform::current() ->createSharedOffscreenGraphicsContext3DProvider());
78 if (!provider) 79 if (!provider)
79 return nullptr; 80 return;
80 GrContext* grContext = provider->grContext(); 81 GrContext* grContext = provider->grContext();
81 if (!grContext) 82 if (!grContext)
82 return nullptr; 83 return;
83 gpu::gles2::GLES2Interface* gl = provider->contextGL(); 84 gpu::gles2::GLES2Interface* gl = provider->contextGL();
84 if (!gl) 85 if (!gl)
85 return nullptr; 86 return;
86 gl->WaitSyncTokenCHROMIUM(m_mailbox.syncToken); 87 gl->WaitSyncTokenCHROMIUM(m_mailbox.syncToken);
87 GLuint textureId = gl->CreateAndConsumeTextureCHROMIUM(GL_TEXTURE_2D, m_mail box.name); 88 m_textureId = gl->CreateAndConsumeTextureCHROMIUM(GL_TEXTURE_2D, m_mailbox.n ame);
88 GrGLTextureInfo textureInfo; 89 GrGLTextureInfo textureInfo;
89 textureInfo.fTarget = GL_TEXTURE_2D; 90 textureInfo.fTarget = GL_TEXTURE_2D;
90 textureInfo.fID = textureId; 91 textureInfo.fID = m_textureId;
91 GrBackendTextureDesc backendTexture; 92 GrBackendTextureDesc backendTexture;
92 backendTexture.fOrigin = kBottomLeft_GrSurfaceOrigin; 93 // backendTexture.fOrigin = kBottomLeft_GrSurfaceOrigin;
94 backendTexture.fOrigin = kTopLeft_GrSurfaceOrigin;
93 backendTexture.fWidth = m_mailbox.textureSize.width; 95 backendTexture.fWidth = m_mailbox.textureSize.width;
94 backendTexture.fHeight = m_mailbox.textureSize.height; 96 backendTexture.fHeight = m_mailbox.textureSize.height;
95 backendTexture.fConfig = kSkia8888_GrPixelConfig; 97 // backendTexture.fConfig = kSkia8888_GrPixelConfig;
98 backendTexture.fConfig = kRGBA_8888_GrPixelConfig;
96 backendTexture.fTextureHandle = skia::GrGLTextureInfoToGrBackendObject(textu reInfo); 99 backendTexture.fTextureHandle = skia::GrGLTextureInfoToGrBackendObject(textu reInfo);
97 sk_sp<SkImage> skImage = SkImage::MakeFromAdoptedTexture(grContext, backendT exture); 100 sk_sp<SkImage> skImage = SkImage::MakeFromAdoptedTexture(grContext, backendT exture);
98 m_image = fromSkSp(skImage); 101 m_image = fromSkSp(skImage);
102 }
103
104 void StaticBitmapImage::copyToTexture(WebGraphicsContext3DProvider* provider, GL uint destinationTexture, GLenum internalFormat, GLenum destType)
105 {
106 gpu::gles2::GLES2Interface* gl = provider->contextGL();
107 if (!gl)
108 return;
109 gl->CopyTextureCHROMIUM(m_textureId, destinationTexture, internalFormat, des tType, false, false, false);
110 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM();
111 GLbyte syncToken[24];
112 gl->GenSyncTokenCHROMIUM(fenceSync, syncToken);
113 }
114
115 bool StaticBitmapImage::switchStorageToMailbox(WebGraphicsContext3DProvider* pro vider)
116 {
117 m_mailbox.textureSize = WebSize(m_image->width(), m_image->height());
118 GrContext* grContext = provider->grContext();
119 if (!grContext)
120 return false;
121 grContext->flush();
122 m_mailbox.textureTarget = GL_TEXTURE_2D;
123 gpu::gles2::GLES2Interface* gl = provider->contextGL();
124 if (!gl)
125 return false;
126 GLuint textureID = skia::GrBackendObjectToGrGLTextureInfo(m_image->getTextur eHandle(true))->fID;
127 gl->BindTexture(GL_TEXTURE_2D, textureID);
128
129 gl->GenMailboxCHROMIUM(m_mailbox.name);
130 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, m_mailbox.name);
131 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM();
132 gl->Flush();
133 gl->GenSyncTokenCHROMIUM(fenceSync, m_mailbox.syncToken);
134 m_mailbox.validSyncToken = true;
135 gl->BindTexture(GL_TEXTURE_2D, 0);
136 grContext->resetContext(kTextureBinding_GrGLBackendState);
137 m_image = nullptr;
138 return true;
139 }
140
141 PassRefPtr<SkImage> StaticBitmapImage::imageForCurrentFrame(WebGraphicsContext3D Provider* contextProvider)
142 {
143 if (m_image) {
144 // If the SkImage is not texture-backed
145 if (!m_image->isTextureBacked())
146 return m_image;
147 // SkImage is texture-backed, but no mailbox exists. virtual/gpu/ cases.
148 if (!hasMailbox()) {
149 // SkImage is texture-backed on the shared context
150 if (contextProvider) {
xidachen 2016/06/14 18:09:40 bsalomon@: in a layout test that runs on GPU, it c
bsalomon 2016/06/14 18:25:22 From what I can tell copyToTexture doesn't look at
151 OwnPtr<WebGraphicsContext3DProvider> sharedProvider = adoptPtr(P latform::current()->createSharedOffscreenGraphicsContext3DProvider());
152 if (!switchStorageToMailbox(sharedProvider.get()))
153 return nullptr;
154 switchStorageToSkImage(contextProvider);
155 RefPtr<SkImage> retVal = m_image;
156 // Get a new mailbox because we cannot retain a texture in the W ebGL context.
xidachen 2016/06/14 18:09:41 junov@: I tried to inject a readPixels() call to t
157 if (!switchStorageToMailbox(contextProvider))
158 return nullptr;
159 return retVal;
160 }
161 return m_image;
162 }
163 // Has mailbox, but contextProvider is null (2D).
164 if (!contextProvider)
165 return m_image;
166 // 3D context, consume mailbox, prepare a new mailbox.
167 switchStorageToSkImage(contextProvider);
168 RefPtr<SkImage> retVal = m_image;
169 if (!switchStorageToMailbox(contextProvider))
170 return nullptr;
171 return retVal;
172 }
173 // No mailbox, return null;
174 if (!hasMailbox())
175 return nullptr;
176 // Has mailbox, consume mailbox, prepare a new mailbox if contextProvider is not null (3D).
177 if (!contextProvider) {
178 DCHECK(isMainThread());
179 // TODO(xidachen): make this work on a worker thread.
180 OwnPtr<WebGraphicsContext3DProvider> sharedProvider = adoptPtr(Platform: :current()->createSharedOffscreenGraphicsContext3DProvider());
181 switchStorageToSkImage(sharedProvider.get());
182 } else {
183 switchStorageToSkImage(contextProvider);
184 RefPtr<SkImage> retVal = m_image;
185 if (!switchStorageToMailbox(contextProvider))
186 return nullptr;
187 return retVal;
188 }
99 return m_image; 189 return m_image;
100 } 190 }
101 191
102 } // namespace blink 192 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/graphics/StaticBitmapImage.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698