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 "platform/RuntimeEnabledFeatures.h" | |
14 #include "public/platform/InterfaceProvider.h" | 15 #include "public/platform/InterfaceProvider.h" |
15 #include "public/platform/Platform.h" | 16 #include "public/platform/Platform.h" |
16 #include "public/platform/modules/offscreencanvas/offscreen_canvas_surface.mojom -blink.h" | 17 #include "public/platform/modules/offscreencanvas/offscreen_canvas_surface.mojom -blink.h" |
17 #include "third_party/khronos/GLES2/gl2.h" | 18 #include "third_party/khronos/GLES2/gl2.h" |
18 #include "third_party/skia/include/core/SkColor.h" | 19 #include "third_party/skia/include/core/SkColor.h" |
19 #include "third_party/skia/include/core/SkImage.h" | 20 #include "third_party/skia/include/core/SkImage.h" |
20 #include "third_party/skia/include/core/SkXfermode.h" | 21 #include "third_party/skia/include/core/SkXfermode.h" |
21 #include "ui/gfx/geometry/rect.h" | 22 #include "ui/gfx/geometry/rect.h" |
22 #include "ui/gfx/transform.h" | 23 #include "ui/gfx/transform.h" |
23 | 24 |
(...skipping 23 matching lines...) Expand all Loading... | |
47 frame.delegated_frame_data.reset(new cc::DelegatedFrameData); | 48 frame.delegated_frame_data.reset(new cc::DelegatedFrameData); |
48 | 49 |
49 const gfx::Rect bounds(m_width, m_height); | 50 const gfx::Rect bounds(m_width, m_height); |
50 const cc::RenderPassId renderPassId(1, 1); | 51 const cc::RenderPassId renderPassId(1, 1); |
51 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); | 52 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); |
52 pass->SetAll(renderPassId, bounds, bounds, gfx::Transform(), false); | 53 pass->SetAll(renderPassId, bounds, bounds, gfx::Transform(), false); |
53 | 54 |
54 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); | 55 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); |
55 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, false, 1.f, SkX fermode::kSrcOver_Mode, 0); | 56 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, false, 1.f, SkX fermode::kSrcOver_Mode, 0); |
56 | 57 |
57 if (!image->isTextureBacked()) { | 58 if (!image->isTextureBacked() && !isMainThread()) { |
58 // TODO(xlai): Make unaccelerated 2d canvas work. See crbug.com/563858 | 59 // TODO(xlai): Implement unaccelerated 2d canvas on worker. |
60 // See crbug.com/563858. | |
59 // This is a temporary code that submits a solidColor frame. | 61 // This is a temporary code that submits a solidColor frame. |
60 cc::SolidColorDrawQuad* quad = pass->CreateAndAppendDrawQuad<cc::SolidCo lorDrawQuad>(); | 62 cc::SolidColorDrawQuad* quad = pass->CreateAndAppendDrawQuad<cc::SolidCo lorDrawQuad>(); |
61 const bool forceAntialiasingOff = false; | 63 const bool forceAntialiasingOff = false; |
62 quad->SetNew(sqs, bounds, bounds, SK_ColorGREEN, forceAntialiasingOff); | 64 quad->SetNew(sqs, bounds, bounds, SK_ColorGREEN, forceAntialiasingOff); |
63 } else { | 65 } else { |
64 cc::TransferableResource resource; | 66 cc::TransferableResource resource; |
65 resource.id = m_nextResourceId; | 67 resource.id = m_nextResourceId; |
66 resource.format = cc::ResourceFormat::RGBA_8888; | 68 resource.format = cc::ResourceFormat::RGBA_8888; |
67 // TODO(crbug.com/645590): filter should respect the image-rendering CSS property of associated canvas element. | 69 // TODO(crbug.com/645590): filter should respect the image-rendering CSS property of associated canvas element. |
68 resource.filter = GL_LINEAR; | 70 resource.filter = GL_LINEAR; |
69 resource.size = gfx::Size(m_width, m_height); | 71 resource.size = gfx::Size(m_width, m_height); |
70 image->ensureMailbox(); | 72 if (!image->isTextureBacked()) { |
71 resource.mailbox_holder = gpu::MailboxHolder(image->getMailbox(), image- >getSyncToken(), GL_TEXTURE_2D); | 73 std::unique_ptr<cc::SharedBitmap> bitmap = Platform::current()->allo cateSharedBitmap(IntSize(m_width, m_height)); |
72 resource.read_lock_fences_enabled = false; | 74 if (!bitmap) |
73 resource.is_software = false; | 75 return; |
76 unsigned char* pixels = bitmap->pixels(); | |
77 DCHECK(pixels); | |
78 SkImageInfo imageInfo = SkImageInfo::Make(m_width, m_height, kN32_Sk ColorType, image->isPremultiplied() ? kPremul_SkAlphaType : kUnpremul_SkAlphaTyp e); | |
79 // TODO(xlai): Optimize to avoid copying pixels. See crbug.com/65145 6. | |
80 image->imageForCurrentFrame()->readPixels(imageInfo, pixels, imageIn fo.minRowBytes(), 0, 0); | |
81 resource.mailbox_holder.mailbox = bitmap->id(); | |
82 resource.is_software = true; | |
83 | |
84 // Hold ref to |bitmap|, to keep it alive until the browser ReturnRe sources. | |
85 // It guarantees that the shared bitmap is not re-used or deleted. | |
86 m_sharedBitmaps.add(getNextResourceIdAndIncrement(), std::move(bitma p)); | |
87 } else { | |
88 image->ensureMailbox(); | |
89 resource.mailbox_holder = gpu::MailboxHolder(image->getMailbox(), im age->getSyncToken(), GL_TEXTURE_2D); | |
90 resource.read_lock_fences_enabled = false; | |
91 resource.is_software = false; | |
92 | |
93 // Hold ref to |image|, to keep it alive until the browser ReturnRes ources. | |
94 // It guarantees that the resource is not re-used or deleted. | |
95 m_cachedImages.add(getNextResourceIdAndIncrement(), std::move(image) ); | |
xidachen
2016/09/29 16:01:39
My understanding of junov@'s comment is that you d
| |
96 } | |
74 // TODO(crbug.com/646022): making this overlay-able. | 97 // TODO(crbug.com/646022): making this overlay-able. |
75 resource.is_overlay_candidate = false; | 98 resource.is_overlay_candidate = false; |
76 | 99 |
77 frame.delegated_frame_data->resource_list.push_back(std::move(resource)) ; | 100 frame.delegated_frame_data->resource_list.push_back(std::move(resource)) ; |
78 | 101 |
79 // Hold ref to |image|, to keep it alive until the browser ReturnResourc es. | |
80 // It guarantees that the resource is not re-used or deleted. | |
81 m_cachedImages.add(m_nextResourceId++, std::move(image)); | |
82 | 102 |
83 cc::TextureDrawQuad* quad = pass->CreateAndAppendDrawQuad<cc::TextureDra wQuad>(); | 103 cc::TextureDrawQuad* quad = pass->CreateAndAppendDrawQuad<cc::TextureDra wQuad>(); |
84 gfx::Size rectSize(m_width, m_height); | 104 gfx::Size rectSize(m_width, m_height); |
85 | 105 |
86 const bool needsBlending = true; | 106 const bool needsBlending = true; |
87 // TOOD(crbug.com/645993): this should be inherited from WebGL context's creation settings. | 107 // TOOD(crbug.com/645993): this should be inherited from WebGL context's creation settings. |
88 const bool premultipliedAlpha = true; | 108 const bool premultipliedAlpha = true; |
89 const gfx::PointF uvTopLeft(0.f, 0.f); | 109 const gfx::PointF uvTopLeft(0.f, 0.f); |
90 const gfx::PointF uvBottomRight(1.f, 1.f); | 110 const gfx::PointF uvBottomRight(1.f, 1.f); |
91 float vertexOpacity[4] = { 1.f, 1.f, 1.f, 1.f }; | 111 float vertexOpacity[4] = { 1.f, 1.f, 1.f, 1.f }; |
92 const bool yflipped = false; | 112 const bool yflipped = false; |
93 // TODO(crbug.com/645994): this should be true when using style "image-r endering: pixelated". | 113 // TODO(crbug.com/645994): this should be true when using style "image-r endering: pixelated". |
94 const bool nearestNeighbor = false; | 114 const bool nearestNeighbor = false; |
95 quad->SetAll(sqs, bounds, bounds, bounds, needsBlending, resource.id, gf x::Size(), premultipliedAlpha, | 115 quad->SetAll(sqs, bounds, bounds, bounds, needsBlending, resource.id, gf x::Size(), premultipliedAlpha, |
96 uvTopLeft, uvBottomRight, SK_ColorTRANSPARENT, vertexOpacity, yflipp ed, nearestNeighbor, false); | 116 uvTopLeft, uvBottomRight, SK_ColorTRANSPARENT, vertexOpacity, yflipp ed, nearestNeighbor, false); |
97 } | 117 } |
98 | 118 |
99 frame.delegated_frame_data->render_pass_list.push_back(std::move(pass)); | 119 frame.delegated_frame_data->render_pass_list.push_back(std::move(pass)); |
100 | 120 |
101 m_sink->SubmitCompositorFrame(std::move(frame), base::Closure()); | 121 m_sink->SubmitCompositorFrame(std::move(frame), base::Closure()); |
102 } | 122 } |
103 | 123 |
104 void OffscreenCanvasFrameDispatcherImpl::ReturnResources(Vector<cc::mojom::blink ::ReturnedResourcePtr> resources) | 124 void OffscreenCanvasFrameDispatcherImpl::ReturnResources(Vector<cc::mojom::blink ::ReturnedResourcePtr> resources) |
105 { | 125 { |
106 for (const auto& resource : resources) | 126 for (const auto& resource : resources) { |
107 m_cachedImages.remove(resource->id); | 127 m_cachedImages.remove(resource->id); |
128 m_sharedBitmaps.remove(resource->id); | |
129 } | |
108 } | 130 } |
109 | 131 |
110 bool OffscreenCanvasFrameDispatcherImpl::verifyImageSize(const sk_sp<SkImage>& i mage) | 132 bool OffscreenCanvasFrameDispatcherImpl::verifyImageSize(const sk_sp<SkImage>& i mage) |
111 { | 133 { |
112 if (image && image->width() == m_width && image->height() == m_height) | 134 if (image && image->width() == m_width && image->height() == m_height) |
113 return true; | 135 return true; |
114 return false; | 136 return false; |
115 } | 137 } |
116 | 138 |
117 } // namespace blink | 139 } // namespace blink |
OLD | NEW |