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

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

Issue 2376743002: Implement commit for unaccelerated canvas with GPU compositing (Closed)
Patch Set: another minor cleanup Created 4 years, 2 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
OLDNEW
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(
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(xidachen): When committing the first frame, there is no instance of Sh aredGpuContext
85 // yet, calling SharedGpuContext::gl() will trigger a creation of an instace, which requires
86 // to create a WebGraphicsContext3DProvider. This process is quite expensive, because
87 // WebGraphicsContext3DProvider can only be constructed on the main thread, an d bind to the
88 // worker thread if commit() is called on worker. In the subsequent frame, we should already
89 // have a SharedGpuContext, then getting the gl interface should not be expens ive.
90 // Try to optimize the first frame in the future.
Justin Novosad 2016/10/04 14:20:48 You need a crbug for this. There is a potential fo
xidachen 2016/10/04 14:43:06 Done.
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::setTransferableResourceCase3(
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
49 void OffscreenCanvasFrameDispatcherImpl::dispatchFrame( 151 void OffscreenCanvasFrameDispatcherImpl::dispatchFrame(
50 RefPtr<StaticBitmapImage> image) { 152 RefPtr<StaticBitmapImage> image) {
51 if (!image) 153 if (!image)
52 return; 154 return;
53 if (!verifyImageSize(image->imageForCurrentFrame())) 155 if (!verifyImageSize(image->imageForCurrentFrame()))
54 return; 156 return;
55 cc::CompositorFrame frame; 157 cc::CompositorFrame frame;
56 frame.metadata.device_scale_factor = 1.0f; 158 frame.metadata.device_scale_factor = 1.0f;
57 frame.delegated_frame_data.reset(new cc::DelegatedFrameData); 159 frame.delegated_frame_data.reset(new cc::DelegatedFrameData);
58 160
59 const gfx::Rect bounds(m_width, m_height); 161 const gfx::Rect bounds(m_width, m_height);
60 const cc::RenderPassId renderPassId(1, 1); 162 const cc::RenderPassId renderPassId(1, 1);
61 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); 163 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create();
62 pass->SetAll(renderPassId, bounds, bounds, gfx::Transform(), false); 164 pass->SetAll(renderPassId, bounds, bounds, gfx::Transform(), false);
63 165
64 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); 166 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
65 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, false, 1.f, 167 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, false, 1.f,
66 SkXfermode::kSrcOver_Mode, 0); 168 SkXfermode::kSrcOver_Mode, 0);
67 169
68 cc::TransferableResource resource; 170 cc::TransferableResource resource;
69 resource.id = m_nextResourceId; 171 resource.id = m_nextResourceId;
70 resource.format = cc::ResourceFormat::RGBA_8888; 172 resource.format = cc::ResourceFormat::RGBA_8888;
71 // 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 prope rty of associated canvas element.
72 resource.filter = GL_LINEAR; 174 resource.filter = GL_LINEAR;
73 resource.size = gfx::Size(m_width, m_height); 175 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. 176 // TODO(crbug.com/646022): making this overlay-able.
105 resource.is_overlay_candidate = false; 177 resource.is_overlay_candidate = false;
106 178
179 if (!image->isTextureBacked() &&
180 !RuntimeEnabledFeatures::gpuCompositingEnabled())
181 setTransferableResourceCase1(resource, image);
182 else if (!image->isTextureBacked() &&
183 RuntimeEnabledFeatures::gpuCompositingEnabled())
184 setTransferableResourceCase2(resource, image);
185 else
186 setTransferableResourceCase3(resource, image);
187
188 m_nextResourceId++;
107 frame.delegated_frame_data->resource_list.push_back(std::move(resource)); 189 frame.delegated_frame_data->resource_list.push_back(std::move(resource));
108 190
109 cc::TextureDrawQuad* quad = 191 cc::TextureDrawQuad* quad =
110 pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>(); 192 pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>();
111 gfx::Size rectSize(m_width, m_height); 193 gfx::Size rectSize(m_width, m_height);
112 194
113 const bool needsBlending = true; 195 const bool needsBlending = true;
114 // TOOD(crbug.com/645993): this should be inherited from WebGL context's creat ion settings. 196 // TOOD(crbug.com/645993): this should be inherited from WebGL context's creat ion settings.
115 const bool premultipliedAlpha = true; 197 const bool premultipliedAlpha = true;
116 const gfx::PointF uvTopLeft(0.f, 0.f); 198 const gfx::PointF uvTopLeft(0.f, 0.f);
(...skipping 10 matching lines...) Expand all
127 frame.delegated_frame_data->render_pass_list.push_back(std::move(pass)); 209 frame.delegated_frame_data->render_pass_list.push_back(std::move(pass));
128 210
129 m_sink->SubmitCompositorFrame(std::move(frame), base::Closure()); 211 m_sink->SubmitCompositorFrame(std::move(frame), base::Closure());
130 } 212 }
131 213
132 void OffscreenCanvasFrameDispatcherImpl::ReturnResources( 214 void OffscreenCanvasFrameDispatcherImpl::ReturnResources(
133 Vector<cc::mojom::blink::ReturnedResourcePtr> resources) { 215 Vector<cc::mojom::blink::ReturnedResourcePtr> resources) {
134 for (const auto& resource : resources) { 216 for (const auto& resource : resources) {
135 m_cachedImages.remove(resource->id); 217 m_cachedImages.remove(resource->id);
136 m_sharedBitmaps.remove(resource->id); 218 m_sharedBitmaps.remove(resource->id);
219 m_cachedTextureIds.remove(resource->id);
137 } 220 }
138 } 221 }
139 222
140 bool OffscreenCanvasFrameDispatcherImpl::verifyImageSize( 223 bool OffscreenCanvasFrameDispatcherImpl::verifyImageSize(
141 const sk_sp<SkImage>& image) { 224 const sk_sp<SkImage>& image) {
142 if (image && image->width() == m_width && image->height() == m_height) 225 if (image && image->width() == m_width && image->height() == m_height)
143 return true; 226 return true;
144 return false; 227 return false;
145 } 228 }
146 229
147 } // namespace blink 230 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698