| 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" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "cc/output/copy_output_result.h" | 10 #include "cc/output/copy_output_result.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #else | 23 #else |
| 24 #include "content/browser/compositor/image_transport_factory.h" | 24 #include "content/browser/compositor/image_transport_factory.h" |
| 25 #include "ui/compositor/compositor.h" | 25 #include "ui/compositor/compositor.h" |
| 26 #endif | 26 #endif |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 #if !defined(OS_ANDROID) || defined(USE_AURA) | 30 #if !defined(OS_ANDROID) || defined(USE_AURA) |
| 31 void CopyFromCompositingSurfaceFinished( | 31 void CopyFromCompositingSurfaceFinished( |
| 32 const content::ReadbackRequestCallback& callback, | 32 const content::ReadbackRequestCallback& callback, |
| 33 scoped_ptr<cc::SingleReleaseCallback> release_callback, | 33 std::unique_ptr<cc::SingleReleaseCallback> release_callback, |
| 34 scoped_ptr<SkBitmap> bitmap, | 34 std::unique_ptr<SkBitmap> bitmap, |
| 35 scoped_ptr<SkAutoLockPixels> bitmap_pixels_lock, | 35 std::unique_ptr<SkAutoLockPixels> bitmap_pixels_lock, |
| 36 bool result) { | 36 bool result) { |
| 37 bitmap_pixels_lock.reset(); | 37 bitmap_pixels_lock.reset(); |
| 38 | 38 |
| 39 gpu::SyncToken sync_token; | 39 gpu::SyncToken sync_token; |
| 40 if (result) { | 40 if (result) { |
| 41 content::GLHelper* gl_helper = | 41 content::GLHelper* gl_helper = |
| 42 content::ImageTransportFactory::GetInstance()->GetGLHelper(); | 42 content::ImageTransportFactory::GetInstance()->GetGLHelper(); |
| 43 if (gl_helper) | 43 if (gl_helper) |
| 44 gl_helper->GenerateSyncToken(&sync_token); | 44 gl_helper->GenerateSyncToken(&sync_token); |
| 45 } | 45 } |
| 46 const bool lost_resource = !sync_token.HasData(); | 46 const bool lost_resource = !sync_token.HasData(); |
| 47 release_callback->Run(sync_token, lost_resource); | 47 release_callback->Run(sync_token, lost_resource); |
| 48 | 48 |
| 49 callback.Run(*bitmap, | 49 callback.Run(*bitmap, |
| 50 result ? content::READBACK_SUCCESS : content::READBACK_FAILED); | 50 result ? content::READBACK_SUCCESS : content::READBACK_FAILED); |
| 51 } | 51 } |
| 52 #endif | 52 #endif |
| 53 | 53 |
| 54 // TODO(wjmaclean): There is significant overlap between | 54 // TODO(wjmaclean): There is significant overlap between |
| 55 // PrepareTextureCopyOutputResult and CopyFromCompositingSurfaceFinished in | 55 // PrepareTextureCopyOutputResult and CopyFromCompositingSurfaceFinished in |
| 56 // this file, and the versions in RenderWidgetHostViewAndroid. They should | 56 // this file, and the versions in RenderWidgetHostViewAndroid. They should |
| 57 // be merged. See https://crbug.com/582955 | 57 // be merged. See https://crbug.com/582955 |
| 58 void PrepareTextureCopyOutputResult( | 58 void PrepareTextureCopyOutputResult( |
| 59 const gfx::Size& dst_size_in_pixel, | 59 const gfx::Size& dst_size_in_pixel, |
| 60 const SkColorType color_type, | 60 const SkColorType color_type, |
| 61 const content::ReadbackRequestCallback& callback, | 61 const content::ReadbackRequestCallback& callback, |
| 62 scoped_ptr<cc::CopyOutputResult> result) { | 62 std::unique_ptr<cc::CopyOutputResult> result) { |
| 63 #if defined(OS_ANDROID) && !defined(USE_AURA) | 63 #if defined(OS_ANDROID) && !defined(USE_AURA) |
| 64 // TODO(wjmaclean): See if there's an equivalent pathway for Android and | 64 // TODO(wjmaclean): See if there's an equivalent pathway for Android and |
| 65 // implement it here. | 65 // implement it here. |
| 66 callback.Run(SkBitmap(), content::READBACK_FAILED); | 66 callback.Run(SkBitmap(), content::READBACK_FAILED); |
| 67 #else | 67 #else |
| 68 DCHECK(result->HasTexture()); | 68 DCHECK(result->HasTexture()); |
| 69 base::ScopedClosureRunner scoped_callback_runner( | 69 base::ScopedClosureRunner scoped_callback_runner( |
| 70 base::Bind(callback, SkBitmap(), content::READBACK_FAILED)); | 70 base::Bind(callback, SkBitmap(), content::READBACK_FAILED)); |
| 71 | 71 |
| 72 // TODO(siva.gunturi): We should be able to validate the format here using | 72 // TODO(siva.gunturi): We should be able to validate the format here using |
| 73 // GLHelper::IsReadbackConfigSupported before we processs the result. | 73 // GLHelper::IsReadbackConfigSupported before we processs the result. |
| 74 // See crbug.com/415682 and crbug.com/415131. | 74 // See crbug.com/415682 and crbug.com/415131. |
| 75 scoped_ptr<SkBitmap> bitmap(new SkBitmap); | 75 std::unique_ptr<SkBitmap> bitmap(new SkBitmap); |
| 76 if (!bitmap->tryAllocPixels(SkImageInfo::Make( | 76 if (!bitmap->tryAllocPixels(SkImageInfo::Make( |
| 77 dst_size_in_pixel.width(), dst_size_in_pixel.height(), color_type, | 77 dst_size_in_pixel.width(), dst_size_in_pixel.height(), color_type, |
| 78 kOpaque_SkAlphaType))) { | 78 kOpaque_SkAlphaType))) { |
| 79 scoped_callback_runner.Reset(base::Bind( | 79 scoped_callback_runner.Reset(base::Bind( |
| 80 callback, SkBitmap(), content::READBACK_BITMAP_ALLOCATION_FAILURE)); | 80 callback, SkBitmap(), content::READBACK_BITMAP_ALLOCATION_FAILURE)); |
| 81 return; | 81 return; |
| 82 } | 82 } |
| 83 | 83 |
| 84 content::ImageTransportFactory* factory = | 84 content::ImageTransportFactory* factory = |
| 85 content::ImageTransportFactory::GetInstance(); | 85 content::ImageTransportFactory::GetInstance(); |
| 86 content::GLHelper* gl_helper = factory->GetGLHelper(); | 86 content::GLHelper* gl_helper = factory->GetGLHelper(); |
| 87 if (!gl_helper) | 87 if (!gl_helper) |
| 88 return; | 88 return; |
| 89 | 89 |
| 90 scoped_ptr<SkAutoLockPixels> bitmap_pixels_lock( | 90 std::unique_ptr<SkAutoLockPixels> bitmap_pixels_lock( |
| 91 new SkAutoLockPixels(*bitmap)); | 91 new SkAutoLockPixels(*bitmap)); |
| 92 uint8_t* pixels = static_cast<uint8_t*>(bitmap->getPixels()); | 92 uint8_t* pixels = static_cast<uint8_t*>(bitmap->getPixels()); |
| 93 | 93 |
| 94 cc::TextureMailbox texture_mailbox; | 94 cc::TextureMailbox texture_mailbox; |
| 95 scoped_ptr<cc::SingleReleaseCallback> release_callback; | 95 std::unique_ptr<cc::SingleReleaseCallback> release_callback; |
| 96 result->TakeTexture(&texture_mailbox, &release_callback); | 96 result->TakeTexture(&texture_mailbox, &release_callback); |
| 97 DCHECK(texture_mailbox.IsTexture()); | 97 DCHECK(texture_mailbox.IsTexture()); |
| 98 | 98 |
| 99 ignore_result(scoped_callback_runner.Release()); | 99 ignore_result(scoped_callback_runner.Release()); |
| 100 | 100 |
| 101 gl_helper->CropScaleReadbackAndCleanMailbox( | 101 gl_helper->CropScaleReadbackAndCleanMailbox( |
| 102 texture_mailbox.mailbox(), texture_mailbox.sync_token(), result->size(), | 102 texture_mailbox.mailbox(), texture_mailbox.sync_token(), result->size(), |
| 103 gfx::Rect(result->size()), dst_size_in_pixel, pixels, color_type, | 103 gfx::Rect(result->size()), dst_size_in_pixel, pixels, color_type, |
| 104 base::Bind(&CopyFromCompositingSurfaceFinished, callback, | 104 base::Bind(&CopyFromCompositingSurfaceFinished, callback, |
| 105 base::Passed(&release_callback), base::Passed(&bitmap), | 105 base::Passed(&release_callback), base::Passed(&bitmap), |
| 106 base::Passed(&bitmap_pixels_lock)), | 106 base::Passed(&bitmap_pixels_lock)), |
| 107 content::GLHelper::SCALER_QUALITY_GOOD); | 107 content::GLHelper::SCALER_QUALITY_GOOD); |
| 108 #endif | 108 #endif |
| 109 } | 109 } |
| 110 | 110 |
| 111 void PrepareBitmapCopyOutputResult( | 111 void PrepareBitmapCopyOutputResult( |
| 112 const gfx::Size& dst_size_in_pixel, | 112 const gfx::Size& dst_size_in_pixel, |
| 113 const SkColorType preferred_color_type, | 113 const SkColorType preferred_color_type, |
| 114 const content::ReadbackRequestCallback& callback, | 114 const content::ReadbackRequestCallback& callback, |
| 115 scoped_ptr<cc::CopyOutputResult> result) { | 115 std::unique_ptr<cc::CopyOutputResult> result) { |
| 116 SkColorType color_type = preferred_color_type; | 116 SkColorType color_type = preferred_color_type; |
| 117 if (color_type != kN32_SkColorType && color_type != kAlpha_8_SkColorType) { | 117 if (color_type != kN32_SkColorType && color_type != kAlpha_8_SkColorType) { |
| 118 // Switch back to default colortype if format not supported. | 118 // Switch back to default colortype if format not supported. |
| 119 color_type = kN32_SkColorType; | 119 color_type = kN32_SkColorType; |
| 120 } | 120 } |
| 121 DCHECK(result->HasBitmap()); | 121 DCHECK(result->HasBitmap()); |
| 122 scoped_ptr<SkBitmap> source = result->TakeBitmap(); | 122 std::unique_ptr<SkBitmap> source = result->TakeBitmap(); |
| 123 DCHECK(source); | 123 DCHECK(source); |
| 124 SkBitmap scaled_bitmap; | 124 SkBitmap scaled_bitmap; |
| 125 if (source->width() != dst_size_in_pixel.width() || | 125 if (source->width() != dst_size_in_pixel.width() || |
| 126 source->height() != dst_size_in_pixel.height()) { | 126 source->height() != dst_size_in_pixel.height()) { |
| 127 scaled_bitmap = skia::ImageOperations::Resize( | 127 scaled_bitmap = skia::ImageOperations::Resize( |
| 128 *source, skia::ImageOperations::RESIZE_BEST, dst_size_in_pixel.width(), | 128 *source, skia::ImageOperations::RESIZE_BEST, dst_size_in_pixel.width(), |
| 129 dst_size_in_pixel.height()); | 129 dst_size_in_pixel.height()); |
| 130 } else { | 130 } else { |
| 131 scaled_bitmap = *source; | 131 scaled_bitmap = *source; |
| 132 } | 132 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 151 SkPaint paint; | 151 SkPaint paint; |
| 152 paint.setColorFilter(SkLumaColorFilter::Make()); | 152 paint.setColorFilter(SkLumaColorFilter::Make()); |
| 153 canvas.drawBitmap(scaled_bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); | 153 canvas.drawBitmap(scaled_bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); |
| 154 callback.Run(grayscale_bitmap, content::READBACK_SUCCESS); | 154 callback.Run(grayscale_bitmap, content::READBACK_SUCCESS); |
| 155 } | 155 } |
| 156 | 156 |
| 157 } // namespace | 157 } // namespace |
| 158 | 158 |
| 159 namespace content { | 159 namespace content { |
| 160 | 160 |
| 161 scoped_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator() { | 161 std::unique_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator() { |
| 162 #if defined(OS_ANDROID) | 162 #if defined(OS_ANDROID) |
| 163 return CompositorImpl::CreateSurfaceIdAllocator(); | 163 return CompositorImpl::CreateSurfaceIdAllocator(); |
| 164 #else | 164 #else |
| 165 ImageTransportFactory* factory = ImageTransportFactory::GetInstance(); | 165 ImageTransportFactory* factory = ImageTransportFactory::GetInstance(); |
| 166 return factory->GetContextFactory()->CreateSurfaceIdAllocator(); | 166 return factory->GetContextFactory()->CreateSurfaceIdAllocator(); |
| 167 #endif | 167 #endif |
| 168 } | 168 } |
| 169 | 169 |
| 170 cc::SurfaceManager* GetSurfaceManager() { | 170 cc::SurfaceManager* GetSurfaceManager() { |
| 171 #if defined(OS_ANDROID) | 171 #if defined(OS_ANDROID) |
| 172 return CompositorImpl::GetSurfaceManager(); | 172 return CompositorImpl::GetSurfaceManager(); |
| 173 #else | 173 #else |
| 174 ImageTransportFactory* factory = ImageTransportFactory::GetInstance(); | 174 ImageTransportFactory* factory = ImageTransportFactory::GetInstance(); |
| 175 return factory->GetSurfaceManager(); | 175 return factory->GetSurfaceManager(); |
| 176 #endif | 176 #endif |
| 177 } | 177 } |
| 178 | 178 |
| 179 void CopyFromCompositingSurfaceHasResult( | 179 void CopyFromCompositingSurfaceHasResult( |
| 180 const gfx::Size& dst_size_in_pixel, | 180 const gfx::Size& dst_size_in_pixel, |
| 181 const SkColorType color_type, | 181 const SkColorType color_type, |
| 182 const ReadbackRequestCallback& callback, | 182 const ReadbackRequestCallback& callback, |
| 183 scoped_ptr<cc::CopyOutputResult> result) { | 183 std::unique_ptr<cc::CopyOutputResult> result) { |
| 184 if (result->IsEmpty() || result->size().IsEmpty()) { | 184 if (result->IsEmpty() || result->size().IsEmpty()) { |
| 185 callback.Run(SkBitmap(), READBACK_FAILED); | 185 callback.Run(SkBitmap(), READBACK_FAILED); |
| 186 return; | 186 return; |
| 187 } | 187 } |
| 188 | 188 |
| 189 gfx::Size output_size_in_pixel; | 189 gfx::Size output_size_in_pixel; |
| 190 if (dst_size_in_pixel.IsEmpty()) | 190 if (dst_size_in_pixel.IsEmpty()) |
| 191 output_size_in_pixel = result->size(); | 191 output_size_in_pixel = result->size(); |
| 192 else | 192 else |
| 193 output_size_in_pixel = dst_size_in_pixel; | 193 output_size_in_pixel = dst_size_in_pixel; |
| 194 | 194 |
| 195 if (result->HasTexture()) { | 195 if (result->HasTexture()) { |
| 196 // GPU-accelerated path | 196 // GPU-accelerated path |
| 197 PrepareTextureCopyOutputResult(output_size_in_pixel, color_type, callback, | 197 PrepareTextureCopyOutputResult(output_size_in_pixel, color_type, callback, |
| 198 std::move(result)); | 198 std::move(result)); |
| 199 return; | 199 return; |
| 200 } | 200 } |
| 201 | 201 |
| 202 DCHECK(result->HasBitmap()); | 202 DCHECK(result->HasBitmap()); |
| 203 // Software path | 203 // Software path |
| 204 PrepareBitmapCopyOutputResult(output_size_in_pixel, color_type, callback, | 204 PrepareBitmapCopyOutputResult(output_size_in_pixel, color_type, callback, |
| 205 std::move(result)); | 205 std::move(result)); |
| 206 } | 206 } |
| 207 | 207 |
| 208 } // namespace content | 208 } // namespace content |
| OLD | NEW |