Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2010 The Chromium Authors. All rights reserved. | 1 // Copyright 2010 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 "cc/output/gl_renderer.h" | 5 #include "cc/output/gl_renderer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 831 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 842 resource_provider_, device_background_texture->id(), false); | 842 resource_provider_, device_background_texture->id(), false); |
| 843 GetFramebufferTexture(lock.texture_id(), bounding_rect); | 843 GetFramebufferTexture(lock.texture_id(), bounding_rect); |
| 844 } | 844 } |
| 845 return device_background_texture; | 845 return device_background_texture; |
| 846 } | 846 } |
| 847 | 847 |
| 848 sk_sp<SkImage> GLRenderer::ApplyBackgroundFilters( | 848 sk_sp<SkImage> GLRenderer::ApplyBackgroundFilters( |
| 849 DrawingFrame* frame, | 849 DrawingFrame* frame, |
| 850 const RenderPassDrawQuad* quad, | 850 const RenderPassDrawQuad* quad, |
| 851 ScopedResource* background_texture, | 851 ScopedResource* background_texture, |
| 852 const gfx::RectF& rect) { | 852 const gfx::RectF& rect, |
| 853 SkIPoint* offset, | |
| 854 SkIRect* subset, | |
| 855 bool flip_texture) { | |
| 853 DCHECK(ShouldApplyBackgroundFilters(quad)); | 856 DCHECK(ShouldApplyBackgroundFilters(quad)); |
| 854 auto use_gr_context = ScopedUseGrContext::Create(this, frame); | 857 |
| 855 sk_sp<SkImageFilter> filter = RenderSurfaceFilters::BuildImageFilter( | 858 sk_sp<SkImageFilter> filter = RenderSurfaceFilters::BuildImageFilter( |
| 856 quad->background_filters, gfx::SizeF(background_texture->size())); | 859 quad->background_filters, gfx::SizeF(background_texture->size())); |
| 857 | 860 |
| 858 // TODO(senorblanco): background filters should be moved to the | 861 sk_sp<SkImage> background_with_filters = ApplyImageFilter( |
| 859 // makeWithFilter fast-path, and go back to calling ApplyImageFilter(). | 862 ScopedUseGrContext::Create(this, frame), resource_provider_, rect, rect, |
| 860 // See http://crbug.com/613233. | 863 quad->filters_scale, std::move(filter), background_texture, offset, |
| 861 if (!filter || !use_gr_context) | 864 subset, flip_texture); |
| 862 return nullptr; | |
| 863 | 865 |
| 864 ResourceProvider::ScopedReadLockGL lock(resource_provider_, | 866 return background_with_filters; |
| 865 background_texture->id()); | |
| 866 | |
| 867 bool flip_texture = true; | |
| 868 sk_sp<SkImage> src_image = | |
| 869 WrapTexture(lock, use_gr_context->context(), flip_texture); | |
| 870 if (!src_image) { | |
| 871 TRACE_EVENT_INSTANT0( | |
| 872 "cc", "ApplyBackgroundFilters wrap background texture failed", | |
| 873 TRACE_EVENT_SCOPE_THREAD); | |
| 874 return nullptr; | |
| 875 } | |
| 876 | |
| 877 // Create surface to draw into. | |
| 878 SkImageInfo dst_info = | |
| 879 SkImageInfo::MakeN32Premul(rect.width(), rect.height()); | |
| 880 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget( | |
| 881 use_gr_context->context(), SkBudgeted::kYes, dst_info); | |
| 882 if (!surface) { | |
| 883 TRACE_EVENT_INSTANT0("cc", | |
| 884 "ApplyBackgroundFilters surface allocation failed", | |
| 885 TRACE_EVENT_SCOPE_THREAD); | |
| 886 return nullptr; | |
| 887 } | |
| 888 | |
| 889 SkMatrix local_matrix; | |
| 890 local_matrix.setScale(quad->filters_scale.x(), quad->filters_scale.y()); | |
| 891 | |
| 892 SkPaint paint; | |
| 893 paint.setImageFilter(filter->makeWithLocalMatrix(local_matrix)); | |
| 894 surface->getCanvas()->translate(-rect.x(), -rect.y()); | |
| 895 surface->getCanvas()->drawImage(src_image, rect.x(), rect.y(), &paint); | |
| 896 // Flush the drawing before source texture read lock goes out of scope. | |
| 897 // Skia API does not guarantee that when the SkImage goes out of scope, | |
| 898 // its externally referenced resources would force the rendering to be | |
| 899 // flushed. | |
| 900 surface->getCanvas()->flush(); | |
| 901 sk_sp<SkImage> image = surface->makeImageSnapshot(); | |
| 902 if (!image || !image->isTextureBacked()) { | |
| 903 return nullptr; | |
| 904 } | |
| 905 | |
| 906 return image; | |
| 907 } | 867 } |
| 908 | 868 |
| 909 // Map device space quad to local space. Device_transform has no 3d | 869 // Map device space quad to local space. Device_transform has no 3d |
| 910 // component since it was flattened, so we don't need to project. We should | 870 // component since it was flattened, so we don't need to project. We should |
| 911 // have already checked that the transform was uninvertible before this call. | 871 // have already checked that the transform was uninvertible before this call. |
| 912 gfx::QuadF MapQuadToLocalSpace(const gfx::Transform& device_transform, | 872 gfx::QuadF MapQuadToLocalSpace(const gfx::Transform& device_transform, |
| 913 const gfx::QuadF& device_quad) { | 873 const gfx::QuadF& device_quad) { |
| 914 gfx::Transform inverse_device_transform(gfx::Transform::kSkipInitialization); | 874 gfx::Transform inverse_device_transform(gfx::Transform::kSkipInitialization); |
| 915 DCHECK(device_transform.IsInvertible()); | 875 DCHECK(device_transform.IsInvertible()); |
| 916 bool did_invert = device_transform.GetInverse(&inverse_device_transform); | 876 bool did_invert = device_transform.GetInverse(&inverse_device_transform); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1035 SkXfermode::Mode blend_mode = quad->shared_quad_state->blend_mode; | 995 SkXfermode::Mode blend_mode = quad->shared_quad_state->blend_mode; |
| 1036 bool use_shaders_for_blending = | 996 bool use_shaders_for_blending = |
| 1037 !CanApplyBlendModeUsingBlendFunc(blend_mode) || | 997 !CanApplyBlendModeUsingBlendFunc(blend_mode) || |
| 1038 ShouldApplyBackgroundFilters(quad) || | 998 ShouldApplyBackgroundFilters(quad) || |
| 1039 settings_->force_blending_with_shaders; | 999 settings_->force_blending_with_shaders; |
| 1040 | 1000 |
| 1041 std::unique_ptr<ScopedResource> background_texture; | 1001 std::unique_ptr<ScopedResource> background_texture; |
| 1042 sk_sp<SkImage> background_image; | 1002 sk_sp<SkImage> background_image; |
| 1043 GLuint background_image_id = 0; | 1003 GLuint background_image_id = 0; |
| 1044 gfx::Rect background_rect; | 1004 gfx::Rect background_rect; |
| 1005 SkIPoint background_offset; | |
| 1006 SkIRect background_subset; | |
| 1045 if (use_shaders_for_blending) { | 1007 if (use_shaders_for_blending) { |
| 1046 // Compute a bounding box around the pixels that will be visible through | 1008 // Compute a bounding box around the pixels that will be visible through |
| 1047 // the quad. | 1009 // the quad. |
| 1048 background_rect = GetBackdropBoundingBoxForRenderPassQuad( | 1010 background_rect = GetBackdropBoundingBoxForRenderPassQuad( |
| 1049 frame, quad, contents_device_transform, clip_region, use_aa); | 1011 frame, quad, contents_device_transform, clip_region, use_aa); |
| 1050 | 1012 |
| 1051 if (!background_rect.IsEmpty()) { | 1013 if (!background_rect.IsEmpty()) { |
| 1052 // The pixels from the filtered background should completely replace the | 1014 // The pixels from the filtered background should completely replace the |
| 1053 // current pixel values. | 1015 // current pixel values. |
| 1054 if (blend_enabled()) | 1016 if (blend_enabled()) |
| 1055 SetBlendEnabled(false); | 1017 SetBlendEnabled(false); |
| 1056 | 1018 |
| 1057 // Read the pixels in the bounding box into a buffer R. | 1019 // Read the pixels in the bounding box into a buffer R. |
| 1058 // This function allocates a texture, which should contribute to the | 1020 // This function allocates a texture, which should contribute to the |
| 1059 // amount of memory used by render surfaces: | 1021 // amount of memory used by render surfaces: |
| 1060 // LayerTreeHost::CalculateMemoryForRenderSurfaces. | 1022 // LayerTreeHost::CalculateMemoryForRenderSurfaces. |
| 1061 background_texture = GetBackdropTexture(background_rect); | 1023 background_texture = GetBackdropTexture(background_rect); |
| 1062 | 1024 |
| 1063 if (ShouldApplyBackgroundFilters(quad) && background_texture) { | 1025 if (ShouldApplyBackgroundFilters(quad) && background_texture) { |
| 1064 // Apply the background filters to R, so that it is applied in the | 1026 // Apply the background filters to R, so that it is applied in the |
| 1065 // pixels' coordinate space. | 1027 // pixels' coordinate space. |
| 1066 background_image = ApplyBackgroundFilters( | 1028 background_image = ApplyBackgroundFilters( |
| 1067 frame, quad, background_texture.get(), gfx::RectF(background_rect)); | 1029 frame, quad, background_texture.get(), gfx::RectF(background_rect), |
| 1030 &background_offset, &background_subset, flip_texture); | |
| 1068 if (background_image) | 1031 if (background_image) |
| 1069 background_image_id = skia::GrBackendObjectToGrGLTextureInfo( | 1032 background_image_id = skia::GrBackendObjectToGrGLTextureInfo( |
| 1070 background_image->getTextureHandle(true)) | 1033 background_image->getTextureHandle(true)) |
| 1071 ->fID; | 1034 ->fID; |
| 1072 DCHECK(background_image_id); | 1035 DCHECK(background_image_id); |
| 1073 } | 1036 } |
| 1074 } | 1037 } |
| 1075 | 1038 |
| 1076 if (!background_texture) { | 1039 if (!background_texture) { |
| 1077 // Something went wrong with reading the backdrop. | 1040 // Something went wrong with reading the backdrop. |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1351 | 1314 |
| 1352 std::unique_ptr<ResourceProvider::ScopedSamplerGL> | 1315 std::unique_ptr<ResourceProvider::ScopedSamplerGL> |
| 1353 shader_background_sampler_lock; | 1316 shader_background_sampler_lock; |
| 1354 if (locations.backdrop != -1) { | 1317 if (locations.backdrop != -1) { |
| 1355 DCHECK(background_texture || background_image_id); | 1318 DCHECK(background_texture || background_image_id); |
| 1356 DCHECK_NE(locations.backdrop, 0); | 1319 DCHECK_NE(locations.backdrop, 0); |
| 1357 DCHECK_NE(locations.backdrop_rect, 0); | 1320 DCHECK_NE(locations.backdrop_rect, 0); |
| 1358 | 1321 |
| 1359 gl_->Uniform1i(locations.backdrop, ++last_texture_unit); | 1322 gl_->Uniform1i(locations.backdrop, ++last_texture_unit); |
| 1360 | 1323 |
| 1361 gl_->Uniform4f(locations.backdrop_rect, background_rect.x(), | 1324 gfx::Size background_texture_size; |
| 1362 background_rect.y(), background_rect.width(), | 1325 if (background_image) { |
| 1363 background_rect.height()); | 1326 background_texture_size.set_width(background_image->width()); |
| 1327 background_texture_size.set_height(background_image->height()); | |
|
Stephen White
2016/07/12 16:37:52
It looks like the backdrop_rect is also used when
Anton Obzhirov
2016/07/14 12:32:09
Yes, you are right, I'll move it up as you suggest
| |
| 1328 } | |
| 1329 | |
| 1330 gfx::RectF tex_rect(background_rect); | |
| 1331 tex_rect.set_width(background_texture_size.width()); | |
| 1332 tex_rect.set_height(background_texture_size.height()); | |
| 1333 tex_rect.set_x(background_rect.x() + background_offset.x()); | |
| 1334 tex_rect.set_y( | |
| 1335 background_rect.y() + background_offset.y() - | |
| 1336 (background_texture_size.height() - background_subset.height())); | |
| 1337 | |
| 1338 gl_->Uniform4f(locations.backdrop_rect, tex_rect.x(), tex_rect.y(), | |
| 1339 tex_rect.width(), tex_rect.height()); | |
| 1364 | 1340 |
| 1365 if (background_image_id) { | 1341 if (background_image_id) { |
| 1366 gl_->ActiveTexture(GL_TEXTURE0 + last_texture_unit); | 1342 gl_->ActiveTexture(GL_TEXTURE0 + last_texture_unit); |
| 1367 gl_->BindTexture(GL_TEXTURE_2D, background_image_id); | 1343 gl_->BindTexture(GL_TEXTURE_2D, background_image_id); |
| 1368 gl_->ActiveTexture(GL_TEXTURE0); | 1344 gl_->ActiveTexture(GL_TEXTURE0); |
| 1369 if (mask_for_background) | 1345 if (mask_for_background) |
| 1370 gl_->Uniform1i(locations.original_backdrop, ++last_texture_unit); | 1346 gl_->Uniform1i(locations.original_backdrop, ++last_texture_unit); |
| 1371 } | 1347 } |
| 1372 if (background_texture) { | 1348 if (background_texture) { |
| 1373 shader_background_sampler_lock = | 1349 shader_background_sampler_lock = |
| (...skipping 2312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3686 texture_id = pending_overlay_resources_.back()->texture_id(); | 3662 texture_id = pending_overlay_resources_.back()->texture_id(); |
| 3687 } | 3663 } |
| 3688 | 3664 |
| 3689 context_support_->ScheduleOverlayPlane( | 3665 context_support_->ScheduleOverlayPlane( |
| 3690 overlay.plane_z_order, overlay.transform, texture_id, | 3666 overlay.plane_z_order, overlay.transform, texture_id, |
| 3691 ToNearestRect(overlay.display_rect), overlay.uv_rect); | 3667 ToNearestRect(overlay.display_rect), overlay.uv_rect); |
| 3692 } | 3668 } |
| 3693 } | 3669 } |
| 3694 | 3670 |
| 3695 } // namespace cc | 3671 } // namespace cc |
| OLD | NEW |