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 "public/platform/InterfaceProvider.h" | 14 #include "public/platform/InterfaceProvider.h" |
15 #include "public/platform/Platform.h" | 15 #include "public/platform/Platform.h" |
16 #include "public/platform/modules/offscreencanvas/offscreen_canvas_surface.mojom -blink.h" | 16 #include "public/platform/modules/offscreencanvas/offscreen_canvas_surface.mojom -blink.h" |
17 #include "third_party/khronos/GLES2/gl2.h" | 17 #include "third_party/khronos/GLES2/gl2.h" |
18 #include "third_party/skia/include/core/SkColor.h" | 18 #include "third_party/skia/include/core/SkColor.h" |
19 #include "third_party/skia/include/core/SkImage.h" | |
19 #include "third_party/skia/include/core/SkXfermode.h" | 20 #include "third_party/skia/include/core/SkXfermode.h" |
20 #include "ui/gfx/geometry/rect.h" | 21 #include "ui/gfx/geometry/rect.h" |
21 #include "ui/gfx/transform.h" | 22 #include "ui/gfx/transform.h" |
22 | 23 |
23 namespace blink { | 24 namespace blink { |
24 | 25 |
25 OffscreenCanvasFrameDispatcherImpl::OffscreenCanvasFrameDispatcherImpl(uint32_t clientId, uint32_t localId, uint64_t nonce, int width, int height) | 26 OffscreenCanvasFrameDispatcherImpl::OffscreenCanvasFrameDispatcherImpl(uint32_t clientId, uint32_t localId, uint64_t nonce, int width, int height) |
26 : m_surfaceId(cc::SurfaceId(clientId, localId, nonce)) | 27 : m_surfaceId(cc::SurfaceId(clientId, localId, nonce)) |
27 , m_width(width) | 28 , m_width(width) |
28 , m_height(height) | 29 , m_height(height) |
30 , m_nextResourceId(1u) | |
29 , m_binding(this) | 31 , m_binding(this) |
30 { | 32 { |
31 DCHECK(!m_sink.is_bound()); | 33 DCHECK(!m_sink.is_bound()); |
32 mojom::blink::OffscreenCanvasCompositorFrameSinkProviderPtr provider; | 34 mojom::blink::OffscreenCanvasCompositorFrameSinkProviderPtr provider; |
33 Platform::current()->interfaceProvider()->getInterface(mojo::GetProxy(&provi der)); | 35 Platform::current()->interfaceProvider()->getInterface(mojo::GetProxy(&provi der)); |
34 provider->CreateCompositorFrameSink(m_surfaceId, m_binding.CreateInterfacePt rAndBind(), mojo::GetProxy(&m_sink)); | 36 provider->CreateCompositorFrameSink(m_surfaceId, m_binding.CreateInterfacePt rAndBind(), mojo::GetProxy(&m_sink)); |
35 } | 37 } |
36 | 38 |
37 void OffscreenCanvasFrameDispatcherImpl::dispatchFrame(RefPtr<StaticBitmapImage> image) | 39 void OffscreenCanvasFrameDispatcherImpl::dispatchFrame(RefPtr<StaticBitmapImage> image) |
38 { | 40 { |
41 if (!image) | |
42 return; | |
43 if (!verifyImageSize(image->imageForCurrentFrame())) | |
44 return; | |
39 cc::CompositorFrame frame; | 45 cc::CompositorFrame frame; |
40 frame.metadata.device_scale_factor = 1.0f; | 46 frame.metadata.device_scale_factor = 1.0f; |
41 frame.delegated_frame_data.reset(new cc::DelegatedFrameData); | 47 frame.delegated_frame_data.reset(new cc::DelegatedFrameData); |
42 | 48 |
43 const gfx::Rect bounds(m_width, m_height); | 49 const gfx::Rect bounds(m_width, m_height); |
44 const cc::RenderPassId renderPassId(1, 1); | 50 const cc::RenderPassId renderPassId(1, 1); |
45 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); | 51 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); |
46 pass->SetAll(renderPassId, bounds, bounds, gfx::Transform(), false); | 52 pass->SetAll(renderPassId, bounds, bounds, gfx::Transform(), false); |
47 | 53 |
48 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); | 54 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); |
49 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, false, 1.f, SkX fermode::kSrcOver_Mode, 0); | 55 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, false, 1.f, SkX fermode::kSrcOver_Mode, 0); |
50 | 56 |
51 unsigned nextResourceId = 1u; | 57 if (!image->isTextureBacked()) { |
52 // TODO(xidachen): for now, we just submit a SolidColor frame to compositor in the 2d case, | 58 // TODO(xlai): Make unaccelerated 2d canvas work. See crbug.com/563858 |
53 // we should extract data from image. | 59 // This is a temporary code that submits a solidColor frame. |
54 if (!image) { | |
55 cc::SolidColorDrawQuad* quad = pass->CreateAndAppendDrawQuad<cc::SolidCo lorDrawQuad>(); | 60 cc::SolidColorDrawQuad* quad = pass->CreateAndAppendDrawQuad<cc::SolidCo lorDrawQuad>(); |
56 const bool forceAntialiasingOff = false; | 61 const bool forceAntialiasingOff = false; |
57 quad->SetNew(sqs, bounds, bounds, SK_ColorGREEN, forceAntialiasingOff); | 62 quad->SetNew(sqs, bounds, bounds, SK_ColorGREEN, forceAntialiasingOff); |
58 } else { // WebGL | 63 } else { |
59 DCHECK(image->isTextureBacked()); | 64 DCHECK(image->isTextureBacked()); |
60 cc::TransferableResource resource; | 65 cc::TransferableResource resource; |
61 resource.id = nextResourceId; | 66 resource.id = m_nextResourceId; |
62 resource.format = cc::ResourceFormat::RGBA_8888; | 67 resource.format = cc::ResourceFormat::RGBA_8888; |
63 // TODO(crbug.com/645590): filter should respect the image-rendering CSS property of associated canvas element. | 68 // TODO(crbug.com/645590): filter should respect the image-rendering CSS property of associated canvas element. |
64 resource.filter = GL_LINEAR; | 69 resource.filter = GL_LINEAR; |
65 resource.size = gfx::Size(m_width, m_height); | 70 resource.size = gfx::Size(m_width, m_height); |
66 image->ensureMailbox(); | 71 image->ensureMailbox(); |
67 resource.mailbox_holder = gpu::MailboxHolder(image->getMailbox(), image- >getSyncToken(), GL_TEXTURE_2D); | 72 resource.mailbox_holder = gpu::MailboxHolder(image->getMailbox(), image- >getSyncToken(), GL_TEXTURE_2D); |
68 resource.read_lock_fences_enabled = false; | 73 resource.read_lock_fences_enabled = false; |
69 resource.is_software = false; | 74 resource.is_software = false; |
70 // TODO(crbug.com/646022): making this overlay-able. | 75 // TODO(crbug.com/646022): making this overlay-able. |
71 resource.is_overlay_candidate = false; | 76 resource.is_overlay_candidate = false; |
77 | |
72 frame.delegated_frame_data->resource_list.push_back(std::move(resource)) ; | 78 frame.delegated_frame_data->resource_list.push_back(std::move(resource)) ; |
73 | 79 |
74 // Hold ref to |image|, to keep it alive until the browser ReturnResourc es. | 80 // Hold ref to |image|, to keep it alive until the browser ReturnResourc es. |
75 // It guarantees that the resource is not re-used or deleted. | 81 // It guarantees that the resource is not re-used or deleted. |
76 m_cachedImages.add(nextResourceId++, std::move(image)); | 82 m_cachedImages.add(m_nextResourceId++, std::move(image)); |
77 | 83 |
78 cc::TextureDrawQuad* quad = pass->CreateAndAppendDrawQuad<cc::TextureDra wQuad>(); | 84 cc::TextureDrawQuad* quad = pass->CreateAndAppendDrawQuad<cc::TextureDra wQuad>(); |
79 gfx::Size rectSize(m_width, m_height); | 85 gfx::Size rectSize(m_width, m_height); |
80 | 86 |
81 const bool needsBlending = true; | 87 const bool needsBlending = true; |
82 // TOOD(crbug.com/645993): this should be inherited from WebGL context's creation settings. | 88 // TOOD(crbug.com/645993): this should be inherited from WebGL context's creation settings. |
83 const bool premultipliedAlpha = true; | 89 const bool premultipliedAlpha = true; |
84 const gfx::PointF uvTopLeft(0.f, 0.f); | 90 const gfx::PointF uvTopLeft(0.f, 0.f); |
85 const gfx::PointF uvBottomRight(1.f, 1.f); | 91 const gfx::PointF uvBottomRight(1.f, 1.f); |
86 float vertexOpacity[4] = { 1.f, 1.f, 1.f, 1.f }; | 92 float vertexOpacity[4] = { 1.f, 1.f, 1.f, 1.f }; |
87 const bool yflipped = false; | 93 const bool yflipped = false; |
88 // TODO(crbug.com/645994): this should be true when using style "image-r endering: pixelated". | 94 // TODO(crbug.com/645994): this should be true when using style "image-r endering: pixelated". |
89 const bool nearestNeighbor = false; | 95 const bool nearestNeighbor = false; |
90 quad->SetAll(sqs, bounds, bounds, bounds, needsBlending, resource.id, gf x::Size(), premultipliedAlpha, | 96 quad->SetAll(sqs, bounds, bounds, bounds, needsBlending, resource.id, gf x::Size(), premultipliedAlpha, |
91 uvTopLeft, uvBottomRight, SK_ColorTRANSPARENT, vertexOpacity, yflipp ed, nearestNeighbor, false); | 97 uvTopLeft, uvBottomRight, SK_ColorTRANSPARENT, vertexOpacity, yflipp ed, nearestNeighbor, false); |
92 } | 98 } |
93 | 99 |
94 frame.delegated_frame_data->render_pass_list.push_back(std::move(pass)); | 100 frame.delegated_frame_data->render_pass_list.push_back(std::move(pass)); |
95 | 101 |
96 m_sink->SubmitCompositorFrame(std::move(frame), base::Closure()); | 102 m_sink->SubmitCompositorFrame(std::move(frame), base::Closure()); |
97 } | 103 } |
98 | 104 |
99 void OffscreenCanvasFrameDispatcherImpl::ReturnResources(Vector<cc::mojom::blink ::ReturnedResourcePtr> resources) | 105 void OffscreenCanvasFrameDispatcherImpl::ReturnResources(Vector<cc::mojom::blink ::ReturnedResourcePtr> resources) |
100 { | 106 { |
101 for (const auto& resource : resources) | 107 for (const auto& resource : resources) |
102 m_cachedImages.remove(resource->id); | 108 m_cachedImages.remove(resource->id); |
103 } | 109 } |
104 | 110 |
111 bool OffscreenCanvasFrameDispatcherImpl::verifyImageSize(const sk_sp<SkImage>& i mage) | |
112 { | |
113 if (image && image->width() == m_width && image->height() == m_height) | |
xidachen
2016/09/22 19:05:55
Is if (image) needed? Before this function, we hav
xlai (Olivia)
2016/09/22 19:36:18
Yes, it is needed. The "image" variable is referri
| |
114 return true; | |
115 return false; | |
116 } | |
117 | |
105 } // namespace blink | 118 } // namespace blink |
OLD | NEW |