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" |
11 #include "cc/quads/solid_color_draw_quad.h" | 11 #include "cc/quads/solid_color_draw_quad.h" |
12 #include "cc/quads/texture_draw_quad.h" | 12 #include "cc/quads/texture_draw_quad.h" |
13 #include "cc/resources/returned_resource.h" | 13 #include "cc/resources/returned_resource.h" |
| 14 #include "gpu/command_buffer/client/gles2_interface.h" |
| 15 #include "platform/RuntimeEnabledFeatures.h" |
| 16 #include "platform/graphics/gpu/SharedGpuContext.h" |
14 #include "platform/RuntimeEnabledFeatures.h" | 17 #include "platform/RuntimeEnabledFeatures.h" |
15 #include "public/platform/InterfaceProvider.h" | 18 #include "public/platform/InterfaceProvider.h" |
16 #include "public/platform/Platform.h" | 19 #include "public/platform/Platform.h" |
| 20 #include "public/platform/WebGraphicsContext3DProvider.h" |
17 #include "public/platform/modules/offscreencanvas/offscreen_canvas_surface.mojom
-blink.h" | 21 #include "public/platform/modules/offscreencanvas/offscreen_canvas_surface.mojom
-blink.h" |
18 #include "third_party/khronos/GLES2/gl2.h" | 22 #include "third_party/khronos/GLES2/gl2.h" |
| 23 #include "third_party/khronos/GLES2/gl2ext.h" |
19 #include "third_party/skia/include/core/SkColor.h" | 24 #include "third_party/skia/include/core/SkColor.h" |
20 #include "third_party/skia/include/core/SkImage.h" | 25 #include "third_party/skia/include/core/SkImage.h" |
21 #include "third_party/skia/include/core/SkXfermode.h" | 26 #include "third_party/skia/include/core/SkXfermode.h" |
22 #include "ui/gfx/geometry/rect.h" | 27 #include "ui/gfx/geometry/rect.h" |
23 #include "ui/gfx/transform.h" | 28 #include "ui/gfx/transform.h" |
| 29 #include "wtf/typed_arrays/ArrayBuffer.h" |
| 30 #include "wtf/typed_arrays/Uint8Array.h" |
24 | 31 |
25 namespace blink { | 32 namespace blink { |
26 | 33 |
27 OffscreenCanvasFrameDispatcherImpl::OffscreenCanvasFrameDispatcherImpl( | 34 OffscreenCanvasFrameDispatcherImpl::OffscreenCanvasFrameDispatcherImpl( |
28 uint32_t clientId, | 35 uint32_t clientId, |
29 uint32_t localId, | 36 uint32_t localId, |
30 uint64_t nonce, | 37 uint64_t nonce, |
31 int width, | 38 int width, |
32 int height) | 39 int height) |
33 : m_surfaceId(cc::FrameSinkId(clientId, 0 /* sink_id */), localId, nonce), | 40 : m_surfaceId(cc::FrameSinkId(clientId, 0 /* sink_id */), localId, nonce), |
34 m_width(width), | 41 m_width(width), |
35 m_height(height), | 42 m_height(height), |
36 m_nextResourceId(1u), | 43 m_nextResourceId(1u), |
37 m_binding(this) { | 44 m_binding(this) { |
38 DCHECK(!m_sink.is_bound()); | 45 DCHECK(!m_sink.is_bound()); |
39 mojom::blink::OffscreenCanvasCompositorFrameSinkProviderPtr provider; | 46 mojom::blink::OffscreenCanvasCompositorFrameSinkProviderPtr provider; |
40 Platform::current()->interfaceProvider()->getInterface( | 47 Platform::current()->interfaceProvider()->getInterface( |
41 mojo::GetProxy(&provider)); | 48 mojo::GetProxy(&provider)); |
42 provider->CreateCompositorFrameSink(m_surfaceId, | 49 provider->CreateCompositorFrameSink(m_surfaceId, |
43 m_binding.CreateInterfacePtrAndBind(), | 50 m_binding.CreateInterfacePtrAndBind(), |
44 mojo::GetProxy(&m_sink)); | 51 mojo::GetProxy(&m_sink)); |
45 } | 52 } |
46 | 53 |
| 54 // Case 1: both canvas and compositor are not gpu accelerated. |
| 55 void OffscreenCanvasFrameDispatcherImpl::setTransferableResourceInMemory( |
| 56 cc::TransferableResource& resource, |
| 57 RefPtr<StaticBitmapImage> image) { |
| 58 std::unique_ptr<cc::SharedBitmap> bitmap = |
| 59 Platform::current()->allocateSharedBitmap(IntSize(m_width, m_height)); |
| 60 if (!bitmap) |
| 61 return; |
| 62 unsigned char* pixels = bitmap->pixels(); |
| 63 DCHECK(pixels); |
| 64 SkImageInfo imageInfo = SkImageInfo::Make( |
| 65 m_width, m_height, kN32_SkColorType, |
| 66 image->isPremultiplied() ? kPremul_SkAlphaType : kUnpremul_SkAlphaType); |
| 67 // TODO(xlai): Optimize to avoid copying pixels. See crbug.com/651456. |
| 68 image->imageForCurrentFrame()->readPixels(imageInfo, pixels, |
| 69 imageInfo.minRowBytes(), 0, 0); |
| 70 resource.mailbox_holder.mailbox = bitmap->id(); |
| 71 resource.mailbox_holder.texture_target = 0; |
| 72 resource.is_software = true; |
| 73 |
| 74 // Hold ref to |bitmap|, to keep it alive until the browser ReturnResources. |
| 75 // It guarantees that the shared bitmap is not re-used or deleted. |
| 76 m_sharedBitmaps.add(m_nextResourceId, std::move(bitmap)); |
| 77 } |
| 78 |
| 79 // Case 2: canvas is not gpu-accelerated, but compositor is |
| 80 void OffscreenCanvasFrameDispatcherImpl::setTransferableResourceMemoryToTexture( |
| 81 cc::TransferableResource& resource, |
| 82 RefPtr<StaticBitmapImage> image) { |
| 83 // TODO(crbug.com/652707): When committing the first frame, there is no |
| 84 // instance of SharedGpuContext yet, calling SharedGpuContext::gl() will |
| 85 // trigger a creation of an instace, which requires to create a |
| 86 // WebGraphicsContext3DProvider. This process is quite expensive, because |
| 87 // WebGraphicsContext3DProvider can only be constructed on the main thread, |
| 88 // and bind to the worker thread if commit() is called on worker. In the |
| 89 // subsequent frame, we should already have a SharedGpuContext, then getting |
| 90 // the gl interface should not be expensive. |
| 91 gpu::gles2::GLES2Interface* gl = SharedGpuContext::gl(); |
| 92 |
| 93 SkImageInfo info = SkImageInfo::Make( |
| 94 m_width, m_height, kN32_SkColorType, |
| 95 image->isPremultiplied() ? kPremul_SkAlphaType : kUnpremul_SkAlphaType); |
| 96 RefPtr<ArrayBuffer> dstBuffer = |
| 97 ArrayBuffer::createOrNull(m_width * m_height, info.bytesPerPixel()); |
| 98 // If it fails to create a buffer for copying the pixel data, then exit early. |
| 99 if (!dstBuffer) |
| 100 return; |
| 101 RefPtr<Uint8Array> dstPixels = |
| 102 Uint8Array::create(dstBuffer, 0, dstBuffer->byteLength()); |
| 103 image->imageForCurrentFrame()->readPixels(info, dstPixels->data(), |
| 104 info.minRowBytes(), 0, 0); |
| 105 |
| 106 GLuint textureId = 0u; |
| 107 gl->GenTextures(1, &textureId); |
| 108 gl->BindTexture(GL_TEXTURE_2D, textureId); |
| 109 GLenum format = |
| 110 (kN32_SkColorType == kRGBA_8888_SkColorType) ? GL_RGBA : GL_BGRA_EXT; |
| 111 gl->TexImage2D(GL_TEXTURE_2D, 0, format, m_width, m_height, 0, format, |
| 112 GL_UNSIGNED_BYTE, 0); |
| 113 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 114 // The pixel data will be uploaded to GPU memory, we have to keep the GPU |
| 115 // memory alive until browser ReturnResources, so here we put textureId for |
| 116 // that piece of GPU memory into a hashmap. |
| 117 m_cachedTextureIds.add(m_nextResourceId, textureId); |
| 118 gl->TexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_width, m_height, format, |
| 119 GL_UNSIGNED_BYTE, dstPixels->data()); |
| 120 |
| 121 gpu::Mailbox mailbox; |
| 122 gl->GenMailboxCHROMIUM(mailbox.name); |
| 123 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); |
| 124 |
| 125 const GLuint64 fenceSync = gl->InsertFenceSyncCHROMIUM(); |
| 126 gl->ShallowFlushCHROMIUM(); |
| 127 gpu::SyncToken syncToken; |
| 128 gl->GenSyncTokenCHROMIUM(fenceSync, syncToken.GetData()); |
| 129 |
| 130 resource.mailbox_holder = |
| 131 gpu::MailboxHolder(mailbox, syncToken, GL_TEXTURE_2D); |
| 132 resource.read_lock_fences_enabled = false; |
| 133 resource.is_software = false; |
| 134 } |
| 135 |
| 136 // Case 3: both canvas and compositor are gpu accelerated. |
| 137 void OffscreenCanvasFrameDispatcherImpl::setTransferableResourceInTexture( |
| 138 cc::TransferableResource& resource, |
| 139 RefPtr<StaticBitmapImage> image) { |
| 140 image->ensureMailbox(); |
| 141 resource.mailbox_holder = gpu::MailboxHolder( |
| 142 image->getMailbox(), image->getSyncToken(), GL_TEXTURE_2D); |
| 143 resource.read_lock_fences_enabled = false; |
| 144 resource.is_software = false; |
| 145 |
| 146 // Hold ref to |image|, to keep it alive until the browser ReturnResources. |
| 147 // It guarantees that the resource is not re-used or deleted. |
| 148 m_cachedImages.add(m_nextResourceId, std::move(image)); |
| 149 } |
| 150 |
47 void OffscreenCanvasFrameDispatcherImpl::dispatchFrame( | 151 void OffscreenCanvasFrameDispatcherImpl::dispatchFrame( |
48 RefPtr<StaticBitmapImage> image) { | 152 RefPtr<StaticBitmapImage> image) { |
49 if (!image) | 153 if (!image) |
50 return; | 154 return; |
51 if (!verifyImageSize(image->imageForCurrentFrame())) | 155 if (!verifyImageSize(image->imageForCurrentFrame())) |
52 return; | 156 return; |
53 cc::CompositorFrame frame; | 157 cc::CompositorFrame frame; |
54 frame.metadata.device_scale_factor = 1.0f; | 158 frame.metadata.device_scale_factor = 1.0f; |
55 frame.delegated_frame_data.reset(new cc::DelegatedFrameData); | 159 frame.delegated_frame_data.reset(new cc::DelegatedFrameData); |
56 | 160 |
57 const gfx::Rect bounds(m_width, m_height); | 161 const gfx::Rect bounds(m_width, m_height); |
58 const cc::RenderPassId renderPassId(1, 1); | 162 const cc::RenderPassId renderPassId(1, 1); |
59 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); | 163 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); |
60 pass->SetAll(renderPassId, bounds, bounds, gfx::Transform(), false); | 164 pass->SetAll(renderPassId, bounds, bounds, gfx::Transform(), false); |
61 | 165 |
62 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); | 166 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); |
63 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, false, 1.f, | 167 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, false, 1.f, |
64 SkXfermode::kSrcOver_Mode, 0); | 168 SkXfermode::kSrcOver_Mode, 0); |
65 | 169 |
66 cc::TransferableResource resource; | 170 cc::TransferableResource resource; |
67 resource.id = m_nextResourceId; | 171 resource.id = m_nextResourceId; |
68 resource.format = cc::ResourceFormat::RGBA_8888; | 172 resource.format = cc::ResourceFormat::RGBA_8888; |
69 // TODO(crbug.com/645590): filter should respect the image-rendering CSS prope
rty of associated canvas element. | 173 // TODO(crbug.com/645590): filter should respect the image-rendering CSS |
| 174 // property of associated canvas element. |
70 resource.filter = GL_LINEAR; | 175 resource.filter = GL_LINEAR; |
71 resource.size = gfx::Size(m_width, m_height); | 176 resource.size = gfx::Size(m_width, m_height); |
72 if (image->isTextureBacked()) { | |
73 image->ensureMailbox(); | |
74 resource.mailbox_holder = gpu::MailboxHolder( | |
75 image->getMailbox(), image->getSyncToken(), GL_TEXTURE_2D); | |
76 resource.read_lock_fences_enabled = false; | |
77 resource.is_software = false; | |
78 | |
79 // Hold ref to |image|, to keep it alive until the browser ReturnResources. | |
80 // It guarantees that the resource is not re-used or deleted. | |
81 m_cachedImages.add(getNextResourceIdAndIncrement(), std::move(image)); | |
82 } else { | |
83 std::unique_ptr<cc::SharedBitmap> bitmap = | |
84 Platform::current()->allocateSharedBitmap(IntSize(m_width, m_height)); | |
85 if (!bitmap) | |
86 return; | |
87 unsigned char* pixels = bitmap->pixels(); | |
88 DCHECK(pixels); | |
89 SkImageInfo imageInfo = SkImageInfo::Make( | |
90 m_width, m_height, kN32_SkColorType, | |
91 image->isPremultiplied() ? kPremul_SkAlphaType : kUnpremul_SkAlphaType); | |
92 // TODO(xlai): Optimize to avoid copying pixels. See crbug.com/651456. | |
93 image->imageForCurrentFrame()->readPixels(imageInfo, pixels, | |
94 imageInfo.minRowBytes(), 0, 0); | |
95 resource.mailbox_holder.mailbox = bitmap->id(); | |
96 resource.is_software = true; | |
97 | |
98 // Hold ref to |bitmap|, to keep it alive until the browser ReturnResources. | |
99 // It guarantees that the shared bitmap is not re-used or deleted. | |
100 m_sharedBitmaps.add(getNextResourceIdAndIncrement(), std::move(bitmap)); | |
101 } | |
102 // TODO(crbug.com/646022): making this overlay-able. | 177 // TODO(crbug.com/646022): making this overlay-able. |
103 resource.is_overlay_candidate = false; | 178 resource.is_overlay_candidate = false; |
104 | 179 |
| 180 if (!image->isTextureBacked() && |
| 181 !RuntimeEnabledFeatures::gpuCompositingEnabled()) |
| 182 setTransferableResourceInMemory(resource, image); |
| 183 else if (!image->isTextureBacked() && |
| 184 RuntimeEnabledFeatures::gpuCompositingEnabled()) |
| 185 setTransferableResourceMemoryToTexture(resource, image); |
| 186 else |
| 187 setTransferableResourceInTexture(resource, image); |
| 188 |
| 189 m_nextResourceId++; |
105 frame.delegated_frame_data->resource_list.push_back(std::move(resource)); | 190 frame.delegated_frame_data->resource_list.push_back(std::move(resource)); |
106 | 191 |
107 cc::TextureDrawQuad* quad = | 192 cc::TextureDrawQuad* quad = |
108 pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>(); | 193 pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>(); |
109 gfx::Size rectSize(m_width, m_height); | 194 gfx::Size rectSize(m_width, m_height); |
110 | 195 |
111 const bool needsBlending = true; | 196 const bool needsBlending = true; |
112 // TOOD(crbug.com/645993): this should be inherited from WebGL context's creat
ion settings. | 197 // TOOD(crbug.com/645993): this should be inherited from WebGL context's |
| 198 // creation settings. |
113 const bool premultipliedAlpha = true; | 199 const bool premultipliedAlpha = true; |
114 const gfx::PointF uvTopLeft(0.f, 0.f); | 200 const gfx::PointF uvTopLeft(0.f, 0.f); |
115 const gfx::PointF uvBottomRight(1.f, 1.f); | 201 const gfx::PointF uvBottomRight(1.f, 1.f); |
116 float vertexOpacity[4] = {1.f, 1.f, 1.f, 1.f}; | 202 float vertexOpacity[4] = {1.f, 1.f, 1.f, 1.f}; |
117 const bool yflipped = false; | 203 const bool yflipped = false; |
118 // TODO(crbug.com/645994): this should be true when using style "image-renderi
ng: pixelated". | 204 // TODO(crbug.com/645994): this should be true when using style |
| 205 // "image-rendering: pixelated". |
119 const bool nearestNeighbor = false; | 206 const bool nearestNeighbor = false; |
120 quad->SetAll(sqs, bounds, bounds, bounds, needsBlending, resource.id, | 207 quad->SetAll(sqs, bounds, bounds, bounds, needsBlending, resource.id, |
121 gfx::Size(), premultipliedAlpha, uvTopLeft, uvBottomRight, | 208 gfx::Size(), premultipliedAlpha, uvTopLeft, uvBottomRight, |
122 SK_ColorTRANSPARENT, vertexOpacity, yflipped, nearestNeighbor, | 209 SK_ColorTRANSPARENT, vertexOpacity, yflipped, nearestNeighbor, |
123 false); | 210 false); |
124 | 211 |
125 frame.delegated_frame_data->render_pass_list.push_back(std::move(pass)); | 212 frame.delegated_frame_data->render_pass_list.push_back(std::move(pass)); |
126 | 213 |
127 m_sink->SubmitCompositorFrame(std::move(frame), base::Closure()); | 214 m_sink->SubmitCompositorFrame(std::move(frame), base::Closure()); |
128 } | 215 } |
129 | 216 |
130 void OffscreenCanvasFrameDispatcherImpl::ReturnResources( | 217 void OffscreenCanvasFrameDispatcherImpl::ReturnResources( |
131 Vector<cc::mojom::blink::ReturnedResourcePtr> resources) { | 218 Vector<cc::mojom::blink::ReturnedResourcePtr> resources) { |
132 for (const auto& resource : resources) { | 219 for (const auto& resource : resources) { |
133 m_cachedImages.remove(resource->id); | 220 m_cachedImages.remove(resource->id); |
134 m_sharedBitmaps.remove(resource->id); | 221 m_sharedBitmaps.remove(resource->id); |
| 222 m_cachedTextureIds.remove(resource->id); |
135 } | 223 } |
136 } | 224 } |
137 | 225 |
138 bool OffscreenCanvasFrameDispatcherImpl::verifyImageSize( | 226 bool OffscreenCanvasFrameDispatcherImpl::verifyImageSize( |
139 const sk_sp<SkImage>& image) { | 227 const sk_sp<SkImage>& image) { |
140 if (image && image->width() == m_width && image->height() == m_height) | 228 if (image && image->width() == m_width && image->height() == m_height) |
141 return true; | 229 return true; |
142 return false; | 230 return false; |
143 } | 231 } |
144 | 232 |
145 } // namespace blink | 233 } // namespace blink |
OLD | NEW |