OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/browser/compositor/surface_utils.h" | 5 #include "content/browser/compositor/surface_utils.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | |
8 #include "base/memory/ref_counted.h" | |
9 #include "build/build_config.h" | 7 #include "build/build_config.h" |
10 #include "cc/output/copy_output_result.h" | |
11 #include "cc/resources/single_release_callback.h" | |
12 #include "cc/surfaces/surface_id_allocator.h" | 8 #include "cc/surfaces/surface_id_allocator.h" |
13 #include "content/common/gpu/client/gl_helper.h" | |
14 #include "skia/ext/image_operations.h" | |
15 #include "skia/ext/refptr.h" | |
16 #include "third_party/skia/include/core/SkCanvas.h" | |
17 #include "third_party/skia/include/core/SkColorFilter.h" | |
18 #include "third_party/skia/include/core/SkPaint.h" | |
19 #include "third_party/skia/include/effects/SkLumaColorFilter.h" | |
20 #include "ui/gfx/geometry/rect.h" | |
21 | |
22 | 9 |
23 #if defined(OS_ANDROID) && !defined(USE_AURA) | 10 #if defined(OS_ANDROID) && !defined(USE_AURA) |
24 #include "content/browser/renderer_host/compositor_impl_android.h" | 11 #include "content/browser/renderer_host/compositor_impl_android.h" |
25 #else | 12 #else |
26 #include "content/browser/compositor/image_transport_factory.h" | 13 #include "content/browser/compositor/image_transport_factory.h" |
27 #include "ui/compositor/compositor.h" | 14 #include "ui/compositor/compositor.h" |
28 #endif | 15 #endif |
29 | 16 |
30 namespace { | |
31 | |
32 #if !defined(OS_ANDROID) || defined(USE_AURA) | |
33 void CopyFromCompositingSurfaceFinished( | |
34 const content::ReadbackRequestCallback& callback, | |
35 scoped_ptr<cc::SingleReleaseCallback> release_callback, | |
36 scoped_ptr<SkBitmap> bitmap, | |
37 scoped_ptr<SkAutoLockPixels> bitmap_pixels_lock, | |
38 bool result) { | |
39 bitmap_pixels_lock.reset(); | |
40 | |
41 gpu::SyncToken sync_token; | |
42 if (result) { | |
43 content::GLHelper* gl_helper = | |
44 content::ImageTransportFactory::GetInstance()->GetGLHelper(); | |
45 if (gl_helper) | |
46 gl_helper->GenerateSyncToken(&sync_token); | |
47 } | |
48 const bool lost_resource = !sync_token.HasData(); | |
49 release_callback->Run(sync_token, lost_resource); | |
50 | |
51 callback.Run(*bitmap, | |
52 result ? content::READBACK_SUCCESS : content::READBACK_FAILED); | |
53 } | |
54 #endif | |
55 | |
56 void PrepareTextureCopyOutputResult( | |
57 const gfx::Size& dst_size_in_pixel, | |
58 const SkColorType color_type, | |
59 const content::ReadbackRequestCallback& callback, | |
60 scoped_ptr<cc::CopyOutputResult> result) { | |
61 #if defined(OS_ANDROID) && !defined(USE_AURA) | |
62 //TODO(wjmaclean): See if there's an equivalent pathway for Android and | |
63 // implement it here. | |
64 callback.Run(SkBitmap(), content::READBACK_FAILED); | |
65 #else | |
66 DCHECK(result->HasTexture()); | |
67 base::ScopedClosureRunner scoped_callback_runner( | |
68 base::Bind(callback, SkBitmap(), content::READBACK_FAILED)); | |
69 | |
70 // TODO(siva.gunturi): We should be able to validate the format here using | |
71 // GLHelper::IsReadbackConfigSupported before we processs the result. | |
72 // See crbug.com/415682 and crbug.com/415131. | |
73 scoped_ptr<SkBitmap> bitmap(new SkBitmap); | |
74 if (!bitmap->tryAllocPixels(SkImageInfo::Make( | |
75 dst_size_in_pixel.width(), dst_size_in_pixel.height(), color_type, | |
76 kOpaque_SkAlphaType))) { | |
77 scoped_callback_runner.Reset(base::Bind( | |
78 callback, SkBitmap(), content::READBACK_BITMAP_ALLOCATION_FAILURE)); | |
79 return; | |
80 } | |
81 | |
82 content::ImageTransportFactory* factory = | |
83 content::ImageTransportFactory::GetInstance(); | |
84 content::GLHelper* gl_helper = factory->GetGLHelper(); | |
85 if (!gl_helper) | |
86 return; | |
87 | |
88 scoped_ptr<SkAutoLockPixels> bitmap_pixels_lock( | |
89 new SkAutoLockPixels(*bitmap)); | |
90 uint8_t* pixels = static_cast<uint8_t*>(bitmap->getPixels()); | |
91 | |
92 cc::TextureMailbox texture_mailbox; | |
93 scoped_ptr<cc::SingleReleaseCallback> release_callback; | |
94 result->TakeTexture(&texture_mailbox, &release_callback); | |
95 DCHECK(texture_mailbox.IsTexture()); | |
96 | |
97 ignore_result(scoped_callback_runner.Release()); | |
98 | |
99 gl_helper->CropScaleReadbackAndCleanMailbox( | |
100 texture_mailbox.mailbox(), texture_mailbox.sync_token(), result->size(), | |
101 gfx::Rect(result->size()), dst_size_in_pixel, pixels, color_type, | |
102 base::Bind(&CopyFromCompositingSurfaceFinished, callback, | |
103 base::Passed(&release_callback), base::Passed(&bitmap), | |
104 base::Passed(&bitmap_pixels_lock)), | |
105 content::GLHelper::SCALER_QUALITY_GOOD); | |
106 #endif | |
107 } | |
108 | |
109 void PrepareBitmapCopyOutputResult( | |
110 const gfx::Size& dst_size_in_pixel, | |
111 const SkColorType preferred_color_type, | |
112 const content::ReadbackRequestCallback& callback, | |
113 scoped_ptr<cc::CopyOutputResult> result) { | |
114 SkColorType color_type = preferred_color_type; | |
115 if (color_type != kN32_SkColorType && color_type != kAlpha_8_SkColorType) { | |
116 // Switch back to default colortype if format not supported. | |
117 color_type = kN32_SkColorType; | |
118 } | |
119 DCHECK(result->HasBitmap()); | |
120 scoped_ptr<SkBitmap> source = result->TakeBitmap(); | |
121 DCHECK(source); | |
122 SkBitmap scaled_bitmap; | |
123 if (source->width() != dst_size_in_pixel.width() || | |
124 source->height() != dst_size_in_pixel.height()) { | |
125 scaled_bitmap = skia::ImageOperations::Resize( | |
126 *source, skia::ImageOperations::RESIZE_BEST, dst_size_in_pixel.width(), | |
127 dst_size_in_pixel.height()); | |
128 } else { | |
129 scaled_bitmap = *source; | |
130 } | |
131 if (color_type == kN32_SkColorType) { | |
132 DCHECK_EQ(scaled_bitmap.colorType(), kN32_SkColorType); | |
133 callback.Run(scaled_bitmap, content::READBACK_SUCCESS); | |
134 return; | |
135 } | |
136 DCHECK_EQ(color_type, kAlpha_8_SkColorType); | |
137 // The software path currently always returns N32 bitmap regardless of the | |
138 // |color_type| we ask for. | |
139 DCHECK_EQ(scaled_bitmap.colorType(), kN32_SkColorType); | |
140 // Paint |scaledBitmap| to alpha-only |grayscale_bitmap|. | |
141 SkBitmap grayscale_bitmap; | |
142 bool success = grayscale_bitmap.tryAllocPixels( | |
143 SkImageInfo::MakeA8(scaled_bitmap.width(), scaled_bitmap.height())); | |
144 if (!success) { | |
145 callback.Run(SkBitmap(), content::READBACK_BITMAP_ALLOCATION_FAILURE); | |
146 return; | |
147 } | |
148 SkCanvas canvas(grayscale_bitmap); | |
149 SkPaint paint; | |
150 skia::RefPtr<SkColorFilter> filter = | |
151 skia::AdoptRef(SkLumaColorFilter::Create()); | |
152 paint.setColorFilter(filter.get()); | |
153 canvas.drawBitmap(scaled_bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); | |
154 callback.Run(grayscale_bitmap, content::READBACK_SUCCESS); | |
155 } | |
156 | |
157 } // namespace | |
158 | |
159 namespace content { | 17 namespace content { |
160 | 18 |
161 scoped_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator() { | 19 scoped_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator() { |
162 #if defined(OS_ANDROID) && !defined(USE_AURA) | 20 #if defined(OS_ANDROID) && !defined(USE_AURA) |
163 return CompositorImpl::CreateSurfaceIdAllocator(); | 21 return CompositorImpl::CreateSurfaceIdAllocator(); |
164 #else | 22 #else |
165 ImageTransportFactory* factory = ImageTransportFactory::GetInstance(); | 23 ImageTransportFactory* factory = ImageTransportFactory::GetInstance(); |
166 return factory->GetContextFactory()->CreateSurfaceIdAllocator(); | 24 return factory->GetContextFactory()->CreateSurfaceIdAllocator(); |
167 #endif | 25 #endif |
168 } | 26 } |
169 | 27 |
170 cc::SurfaceManager* GetSurfaceManager() { | 28 cc::SurfaceManager* GetSurfaceManager() { |
171 #if defined(OS_ANDROID) && !defined(USE_AURA) | 29 #if defined(OS_ANDROID) && !defined(USE_AURA) |
172 return CompositorImpl::GetSurfaceManager(); | 30 return CompositorImpl::GetSurfaceManager(); |
173 #else | 31 #else |
174 ImageTransportFactory* factory = ImageTransportFactory::GetInstance(); | 32 ImageTransportFactory* factory = ImageTransportFactory::GetInstance(); |
175 return factory->GetSurfaceManager(); | 33 return factory->GetSurfaceManager(); |
176 #endif | 34 #endif |
177 } | 35 } |
178 | 36 |
179 void CopyFromCompositingSurfaceHasResult( | |
180 const gfx::Size& dst_size_in_pixel, | |
181 const SkColorType color_type, | |
182 const ReadbackRequestCallback& callback, | |
183 scoped_ptr<cc::CopyOutputResult> result) { | |
184 if (result->IsEmpty() || result->size().IsEmpty()) { | |
185 callback.Run(SkBitmap(), READBACK_FAILED); | |
186 return; | |
187 } | |
188 | |
189 gfx::Size output_size_in_pixel; | |
190 if (dst_size_in_pixel.IsEmpty()) | |
191 output_size_in_pixel = result->size(); | |
192 else | |
193 output_size_in_pixel = dst_size_in_pixel; | |
194 | |
195 if (result->HasTexture()) { | |
196 // GPU-accelerated path | |
197 PrepareTextureCopyOutputResult(output_size_in_pixel, color_type, callback, | |
198 std::move(result)); | |
199 return; | |
200 } | |
201 | |
202 DCHECK(result->HasBitmap()); | |
203 // Software path | |
204 PrepareBitmapCopyOutputResult(output_size_in_pixel, color_type, callback, | |
205 std::move(result)); | |
206 } | |
207 | |
208 } // namespace content | 37 } // namespace content |
OLD | NEW |