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