OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/OffscreenCanvasFrameDispatcherImpl.h" | 5 #include "platform/graphics/OffscreenCanvasFrameDispatcherImpl.h" |
6 | 6 |
7 #include "cc/output/compositor_frame.h" | 7 #include "cc/output/compositor_frame.h" |
8 #include "cc/output/delegated_frame_data.h" | 8 #include "cc/output/delegated_frame_data.h" |
9 #include "cc/quads/render_pass.h" | 9 #include "cc/quads/render_pass.h" |
10 #include "cc/quads/shared_quad_state.h" | 10 #include "cc/quads/shared_quad_state.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 m_binding(this) { | 46 m_binding(this) { |
47 DCHECK(!m_sink.is_bound()); | 47 DCHECK(!m_sink.is_bound()); |
48 mojom::blink::OffscreenCanvasCompositorFrameSinkProviderPtr provider; | 48 mojom::blink::OffscreenCanvasCompositorFrameSinkProviderPtr provider; |
49 Platform::current()->interfaceProvider()->getInterface( | 49 Platform::current()->interfaceProvider()->getInterface( |
50 mojo::GetProxy(&provider)); | 50 mojo::GetProxy(&provider)); |
51 provider->CreateCompositorFrameSink(m_surfaceId, | 51 provider->CreateCompositorFrameSink(m_surfaceId, |
52 m_binding.CreateInterfacePtrAndBind(), | 52 m_binding.CreateInterfacePtrAndBind(), |
53 mojo::GetProxy(&m_sink)); | 53 mojo::GetProxy(&m_sink)); |
54 } | 54 } |
55 | 55 |
56 // Case 1: both canvas and compositor are not gpu accelerated, or canvas is | 56 void OffscreenCanvasFrameDispatcherImpl::setTransferableResourceToSharedBitmap( |
57 // accelerated but --disable-gpu-compositing is specified, or | |
58 // WebGL's commit called with swiftshader. The last case is indicated by | |
59 // WebGraphicsContext3DProvider::isSoftwareRendering. | |
60 void OffscreenCanvasFrameDispatcherImpl::setTransferableResourceInMemory( | |
61 cc::TransferableResource& resource, | 57 cc::TransferableResource& resource, |
62 RefPtr<StaticBitmapImage> image) { | 58 RefPtr<StaticBitmapImage> image) { |
63 std::unique_ptr<cc::SharedBitmap> bitmap = | 59 std::unique_ptr<cc::SharedBitmap> bitmap = |
64 Platform::current()->allocateSharedBitmap(IntSize(m_width, m_height)); | 60 Platform::current()->allocateSharedBitmap(IntSize(m_width, m_height)); |
65 if (!bitmap) | 61 if (!bitmap) |
66 return; | 62 return; |
67 unsigned char* pixels = bitmap->pixels(); | 63 unsigned char* pixels = bitmap->pixels(); |
68 DCHECK(pixels); | 64 DCHECK(pixels); |
69 SkImageInfo imageInfo = SkImageInfo::Make( | 65 SkImageInfo imageInfo = SkImageInfo::Make( |
70 m_width, m_height, kN32_SkColorType, | 66 m_width, m_height, kN32_SkColorType, |
71 image->isPremultiplied() ? kPremul_SkAlphaType : kUnpremul_SkAlphaType); | 67 image->isPremultiplied() ? kPremul_SkAlphaType : kUnpremul_SkAlphaType); |
72 // TODO(xlai): Optimize to avoid copying pixels. See crbug.com/651456. | 68 // TODO(xlai): Optimize to avoid copying pixels. See crbug.com/651456. |
73 // However, in the case when |image| is texture backed, this function call | 69 // However, in the case when |image| is texture backed, this function call |
74 // does a GPU readback which is required. | 70 // does a GPU readback which is required. |
75 image->imageForCurrentFrame()->readPixels(imageInfo, pixels, | 71 image->imageForCurrentFrame()->readPixels(imageInfo, pixels, |
76 imageInfo.minRowBytes(), 0, 0); | 72 imageInfo.minRowBytes(), 0, 0); |
77 resource.mailbox_holder.mailbox = bitmap->id(); | 73 resource.mailbox_holder.mailbox = bitmap->id(); |
78 resource.mailbox_holder.texture_target = 0; | 74 resource.mailbox_holder.texture_target = 0; |
79 resource.is_software = true; | 75 resource.is_software = true; |
80 | 76 |
81 // Hold ref to |bitmap|, to keep it alive until the browser ReturnResources. | 77 // Hold ref to |bitmap|, to keep it alive until the browser ReturnResources. |
82 // It guarantees that the shared bitmap is not re-used or deleted. | 78 // It guarantees that the shared bitmap is not re-used or deleted. |
83 m_sharedBitmaps.add(m_nextResourceId, std::move(bitmap)); | 79 m_sharedBitmaps.add(m_nextResourceId, std::move(bitmap)); |
84 } | 80 } |
85 | 81 |
86 // Case 2: canvas is not gpu-accelerated, but compositor is | 82 void OffscreenCanvasFrameDispatcherImpl:: |
87 void OffscreenCanvasFrameDispatcherImpl::setTransferableResourceMemoryToTexture( | 83 setTransferableResourceToSharedGPUContext( |
88 cc::TransferableResource& resource, | 84 cc::TransferableResource& resource, |
89 RefPtr<StaticBitmapImage> image) { | 85 RefPtr<StaticBitmapImage> image) { |
90 // TODO(crbug.com/652707): When committing the first frame, there is no | 86 // TODO(crbug.com/652707): When committing the first frame, there is no |
91 // instance of SharedGpuContext yet, calling SharedGpuContext::gl() will | 87 // instance of SharedGpuContext yet, calling SharedGpuContext::gl() will |
92 // trigger a creation of an instace, which requires to create a | 88 // trigger a creation of an instace, which requires to create a |
93 // WebGraphicsContext3DProvider. This process is quite expensive, because | 89 // WebGraphicsContext3DProvider. This process is quite expensive, because |
94 // WebGraphicsContext3DProvider can only be constructed on the main thread, | 90 // WebGraphicsContext3DProvider can only be constructed on the main thread, |
95 // and bind to the worker thread if commit() is called on worker. In the | 91 // and bind to the worker thread if commit() is called on worker. In the |
96 // subsequent frame, we should already have a SharedGpuContext, then getting | 92 // subsequent frame, we should already have a SharedGpuContext, then getting |
97 // the gl interface should not be expensive. | 93 // the gl interface should not be expensive. |
98 gpu::gles2::GLES2Interface* gl = SharedGpuContext::gl(); | 94 gpu::gles2::GLES2Interface* gl = SharedGpuContext::gl(); |
99 | 95 |
(...skipping 11 matching lines...) Expand all Loading... |
111 info.minRowBytes(), 0, 0); | 107 info.minRowBytes(), 0, 0); |
112 | 108 |
113 GLuint textureId = 0u; | 109 GLuint textureId = 0u; |
114 gl->GenTextures(1, &textureId); | 110 gl->GenTextures(1, &textureId); |
115 gl->BindTexture(GL_TEXTURE_2D, textureId); | 111 gl->BindTexture(GL_TEXTURE_2D, textureId); |
116 GLenum format = | 112 GLenum format = |
117 (kN32_SkColorType == kRGBA_8888_SkColorType) ? GL_RGBA : GL_BGRA_EXT; | 113 (kN32_SkColorType == kRGBA_8888_SkColorType) ? GL_RGBA : GL_BGRA_EXT; |
118 gl->TexImage2D(GL_TEXTURE_2D, 0, format, m_width, m_height, 0, format, | 114 gl->TexImage2D(GL_TEXTURE_2D, 0, format, m_width, m_height, 0, format, |
119 GL_UNSIGNED_BYTE, 0); | 115 GL_UNSIGNED_BYTE, 0); |
120 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 116 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
121 // The pixel data will be uploaded to GPU memory, we have to keep the GPU | |
122 // memory alive until browser ReturnResources, so here we put textureId for | |
123 // that piece of GPU memory into a hashmap. | |
124 m_cachedTextureIds.add(m_nextResourceId, textureId); | |
125 gl->TexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_width, m_height, format, | 117 gl->TexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_width, m_height, format, |
126 GL_UNSIGNED_BYTE, dstPixels->data()); | 118 GL_UNSIGNED_BYTE, dstPixels->data()); |
127 | 119 |
128 gpu::Mailbox mailbox; | 120 gpu::Mailbox mailbox; |
129 gl->GenMailboxCHROMIUM(mailbox.name); | 121 gl->GenMailboxCHROMIUM(mailbox.name); |
130 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); | 122 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); |
131 | 123 |
132 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM(); | 124 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM(); |
133 gl->ShallowFlushCHROMIUM(); | 125 gl->ShallowFlushCHROMIUM(); |
134 gpu::SyncToken syncToken; | 126 gpu::SyncToken syncToken; |
135 gl->GenSyncTokenCHROMIUM(fenceSync, syncToken.GetData()); | 127 gl->GenSyncTokenCHROMIUM(fenceSync, syncToken.GetData()); |
136 | 128 |
137 resource.mailbox_holder = | 129 resource.mailbox_holder = |
138 gpu::MailboxHolder(mailbox, syncToken, GL_TEXTURE_2D); | 130 gpu::MailboxHolder(mailbox, syncToken, GL_TEXTURE_2D); |
139 resource.read_lock_fences_enabled = false; | 131 resource.read_lock_fences_enabled = false; |
140 resource.is_software = false; | 132 resource.is_software = false; |
| 133 |
| 134 // Hold ref to |textureId| for the piece of GPU memory where the pixel data |
| 135 // is uploaded to, to keep it alive until the browser ReturnResources. |
| 136 m_cachedTextureIds.add(m_nextResourceId, textureId); |
141 } | 137 } |
142 | 138 |
143 // Case 3: both canvas and compositor are gpu accelerated. | 139 void OffscreenCanvasFrameDispatcherImpl:: |
144 void OffscreenCanvasFrameDispatcherImpl::setTransferableResourceInTexture( | 140 setTransferableResourceToStaticBitmapImage( |
145 cc::TransferableResource& resource, | 141 cc::TransferableResource& resource, |
146 RefPtr<StaticBitmapImage> image) { | 142 RefPtr<StaticBitmapImage> image) { |
147 image->ensureMailbox(); | 143 image->ensureMailbox(); |
148 resource.mailbox_holder = gpu::MailboxHolder( | 144 resource.mailbox_holder = gpu::MailboxHolder( |
149 image->getMailbox(), image->getSyncToken(), GL_TEXTURE_2D); | 145 image->getMailbox(), image->getSyncToken(), GL_TEXTURE_2D); |
150 resource.read_lock_fences_enabled = false; | 146 resource.read_lock_fences_enabled = false; |
151 resource.is_software = false; | 147 resource.is_software = false; |
152 | 148 |
153 // Hold ref to |image|, to keep it alive until the browser ReturnResources. | 149 // Hold ref to |image|, to keep it alive until the browser ReturnResources. |
154 // It guarantees that the resource is not re-used or deleted. | 150 // It guarantees that the resource is not re-used or deleted. |
155 m_cachedImages.add(m_nextResourceId, std::move(image)); | 151 m_cachedImages.add(m_nextResourceId, std::move(image)); |
156 } | 152 } |
157 | 153 |
158 // When WebGL's commit is called on SwiftShader, we have software rendered | |
159 // WebGL. | |
160 void OffscreenCanvasFrameDispatcherImpl::dispatchFrame( | 154 void OffscreenCanvasFrameDispatcherImpl::dispatchFrame( |
161 RefPtr<StaticBitmapImage> image, | 155 RefPtr<StaticBitmapImage> image, |
162 bool isWebGLSoftwareRendering) { | 156 bool isWebGLSoftwareRendering /* This flag is true when WebGL's commit is |
| 157 called on SwiftShader. */) { |
163 if (!image) | 158 if (!image) |
164 return; | 159 return; |
165 if (!verifyImageSize(image->imageForCurrentFrame())) | 160 if (!verifyImageSize(image->imageForCurrentFrame())) |
166 return; | 161 return; |
167 cc::CompositorFrame frame; | 162 cc::CompositorFrame frame; |
168 // TODO(crbug.com/652931): update the device_scale_factor | 163 // TODO(crbug.com/652931): update the device_scale_factor |
169 frame.metadata.device_scale_factor = 1.0f; | 164 frame.metadata.device_scale_factor = 1.0f; |
170 frame.delegated_frame_data.reset(new cc::DelegatedFrameData); | 165 frame.delegated_frame_data.reset(new cc::DelegatedFrameData); |
171 | 166 |
172 const gfx::Rect bounds(m_width, m_height); | 167 const gfx::Rect bounds(m_width, m_height); |
173 const cc::RenderPassId renderPassId(1, 1); | 168 const cc::RenderPassId renderPassId(1, 1); |
174 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); | 169 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); |
175 pass->SetAll(renderPassId, bounds, bounds, gfx::Transform(), false); | 170 pass->SetAll(renderPassId, bounds, bounds, gfx::Transform(), false); |
176 | 171 |
177 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); | 172 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); |
178 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, false, 1.f, | 173 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, false, 1.f, |
179 SkXfermode::kSrcOver_Mode, 0); | 174 SkXfermode::kSrcOver_Mode, 0); |
180 | 175 |
181 cc::TransferableResource resource; | 176 cc::TransferableResource resource; |
182 resource.id = m_nextResourceId; | 177 resource.id = m_nextResourceId; |
183 resource.format = cc::ResourceFormat::RGBA_8888; | 178 resource.format = cc::ResourceFormat::RGBA_8888; |
184 // TODO(crbug.com/645590): filter should respect the image-rendering CSS | 179 // TODO(crbug.com/645590): filter should respect the image-rendering CSS |
185 // property of associated canvas element. | 180 // property of associated canvas element. |
186 resource.filter = GL_LINEAR; | 181 resource.filter = GL_LINEAR; |
187 resource.size = gfx::Size(m_width, m_height); | 182 resource.size = gfx::Size(m_width, m_height); |
188 // TODO(crbug.com/646022): making this overlay-able. | 183 // TODO(crbug.com/646022): making this overlay-able. |
189 resource.is_overlay_candidate = false; | 184 resource.is_overlay_candidate = false; |
190 | 185 |
191 if (!image->isTextureBacked() && | 186 if (image->isTextureBacked() && |
192 !Platform::current()->isGPUCompositingEnabled()) | 187 Platform::current()->isGPUCompositingEnabled() && |
193 setTransferableResourceInMemory(resource, image); | 188 !isWebGLSoftwareRendering) { |
194 else if (!image->isTextureBacked() && | 189 // Case 1: both canvas and compositor are gpu accelerated. |
195 Platform::current()->isGPUCompositingEnabled()) | 190 setTransferableResourceToStaticBitmapImage(resource, image); |
196 setTransferableResourceMemoryToTexture(resource, image); | 191 } else if (!Platform::current()->isGPUCompositingEnabled() || |
197 else if (image->isTextureBacked() && | 192 isWebGLSoftwareRendering) { |
198 (!Platform::current()->isGPUCompositingEnabled() || | 193 // Case 2: both canvas and compositor are not gpu accelerated, or canvas is |
199 isWebGLSoftwareRendering)) | 194 // accelerated but --disable-gpu-compositing is specified, or |
200 setTransferableResourceInMemory(resource, image); | 195 // WebGL's commit called with swiftshader. The last case is indicated by |
201 else | 196 // WebGraphicsContext3DProvider::isSoftwareRendering. |
202 setTransferableResourceInTexture(resource, image); | 197 setTransferableResourceToSharedBitmap(resource, image); |
| 198 } else { |
| 199 // Case 3: canvas is not gpu-accelerated, but compositor is. |
| 200 setTransferableResourceToSharedGPUContext(resource, image); |
| 201 } |
203 | 202 |
204 m_nextResourceId++; | 203 m_nextResourceId++; |
205 frame.delegated_frame_data->resource_list.push_back(std::move(resource)); | 204 frame.delegated_frame_data->resource_list.push_back(std::move(resource)); |
206 | 205 |
207 cc::TextureDrawQuad* quad = | 206 cc::TextureDrawQuad* quad = |
208 pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>(); | 207 pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>(); |
209 gfx::Size rectSize(m_width, m_height); | 208 gfx::Size rectSize(m_width, m_height); |
210 | 209 |
211 const bool needsBlending = true; | 210 const bool needsBlending = true; |
212 // TOOD(crbug.com/645993): this should be inherited from WebGL context's | 211 // TOOD(crbug.com/645993): this should be inherited from WebGL context's |
(...skipping 26 matching lines...) Expand all Loading... |
239 } | 238 } |
240 | 239 |
241 bool OffscreenCanvasFrameDispatcherImpl::verifyImageSize( | 240 bool OffscreenCanvasFrameDispatcherImpl::verifyImageSize( |
242 const sk_sp<SkImage>& image) { | 241 const sk_sp<SkImage>& image) { |
243 if (image && image->width() == m_width && image->height() == m_height) | 242 if (image && image->width() == m_width && image->height() == m_height) |
244 return true; | 243 return true; |
245 return false; | 244 return false; |
246 } | 245 } |
247 | 246 |
248 } // namespace blink | 247 } // namespace blink |
OLD | NEW |