Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Side by Side Diff: third_party/WebKit/Source/core/html/HTMLCanvasElement.cpp

Issue 2833593002: Remove unneeded abstractions and simplify RecordingImageBufferSurface (Closed)
Patch Set: imagebuffersurface-cleanups: morefixcompile Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2DTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 870 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 // resource limits. So we need to keep the number of texture resources 881 // resource limits. So we need to keep the number of texture resources
882 // under tight control 882 // under tight control
883 if (ImageBuffer::GetGlobalAcceleratedImageBufferCount() >= 883 if (ImageBuffer::GetGlobalAcceleratedImageBufferCount() >=
884 kMaxGlobalAcceleratedImageBufferCount) 884 kMaxGlobalAcceleratedImageBufferCount)
885 return false; 885 return false;
886 } 886 }
887 887
888 return true; 888 return true;
889 } 889 }
890 890
891 namespace {
892
893 class UnacceleratedSurfaceFactory
894 : public RecordingImageBufferFallbackSurfaceFactory {
895 public:
896 virtual std::unique_ptr<ImageBufferSurface> CreateSurface(
897 const IntSize& size,
898 OpacityMode opacity_mode,
899 const CanvasColorParams& color_params) {
900 return WTF::WrapUnique(new UnacceleratedImageBufferSurface(
901 size, opacity_mode, kInitializeImagePixels, color_params));
902 }
903
904 virtual ~UnacceleratedSurfaceFactory() {}
905 };
906
907 } // namespace
908
909 bool HTMLCanvasElement::ShouldUseDisplayList() { 891 bool HTMLCanvasElement::ShouldUseDisplayList() {
910 // Rasterization of web contents will blend in the output space. Only embed 892 // Rasterization of web contents will blend in the output space. Only embed
911 // the canvas as a display list if it intended to do output space blending as 893 // the canvas as a display list if it intended to do output space blending as
912 // well. 894 // well.
913 if (!context_->color_params().UsesOutputSpaceBlending()) 895 if (!context_->color_params().UsesOutputSpaceBlending())
914 return false; 896 return false;
915 897
916 if (RuntimeEnabledFeatures::forceDisplayList2dCanvasEnabled()) 898 if (RuntimeEnabledFeatures::forceDisplayList2dCanvasEnabled())
917 return true; 899 return true;
918 900
919 if (!RuntimeEnabledFeatures::displayList2dCanvasEnabled()) 901 if (!RuntimeEnabledFeatures::displayList2dCanvasEnabled())
920 return false; 902 return false;
921 903
922 return true; 904 return true;
923 } 905 }
924 906
925 std::unique_ptr<ImageBufferSurface> 907 std::unique_ptr<ImageBufferSurface>
926 HTMLCanvasElement::CreateWebGLImageBufferSurface(OpacityMode opacity_mode) { 908 HTMLCanvasElement::CreateWebGLImageBufferSurface(OpacityMode opacity_mode) {
927 DCHECK(Is3d()); 909 DCHECK(Is3d());
928 // If 3d, but the use of the canvas will be for non-accelerated content 910 // If 3d, but the use of the canvas will be for non-accelerated content
929 // then make a non-accelerated ImageBuffer. This means copying the internal 911 // then make a non-accelerated ImageBuffer. This means copying the internal
930 // Image will require a pixel readback, but that is unavoidable in this case. 912 // Image will require a pixel readback, but that is unavoidable in this case.
931 auto surface = WTF::WrapUnique(new AcceleratedImageBufferSurface( 913 auto surface = WTF::MakeUnique<AcceleratedImageBufferSurface>(
932 size(), opacity_mode, context_->color_params())); 914 size(), opacity_mode, context_->color_params());
933 if (surface->IsValid()) 915 if (surface->IsValid())
934 return std::move(surface); 916 return std::move(surface);
935 return nullptr; 917 return nullptr;
936 } 918 }
937 919
938 std::unique_ptr<ImageBufferSurface> 920 std::unique_ptr<ImageBufferSurface>
939 HTMLCanvasElement::CreateAcceleratedImageBufferSurface(OpacityMode opacity_mode, 921 HTMLCanvasElement::CreateAcceleratedImageBufferSurface(OpacityMode opacity_mode,
940 int* msaa_sample_count) { 922 int* msaa_sample_count) {
941 if (GetDocument().GetSettings()) { 923 if (GetDocument().GetSettings()) {
942 *msaa_sample_count = 924 *msaa_sample_count =
943 GetDocument().GetSettings()->GetAccelerated2dCanvasMSAASampleCount(); 925 GetDocument().GetSettings()->GetAccelerated2dCanvasMSAASampleCount();
944 } 926 }
945 927
946 // Avoid creating |contextProvider| until we're sure we want to try use it, 928 // Avoid creating |contextProvider| until we're sure we want to try use it,
947 // since it costs us GPU memory. 929 // since it costs us GPU memory.
948 std::unique_ptr<WebGraphicsContext3DProvider> context_provider( 930 std::unique_ptr<WebGraphicsContext3DProvider> context_provider(
949 Platform::Current()->CreateSharedOffscreenGraphicsContext3DProvider()); 931 Platform::Current()->CreateSharedOffscreenGraphicsContext3DProvider());
950 if (!context_provider) { 932 if (!context_provider) {
951 CanvasMetrics::CountCanvasContextUsage( 933 CanvasMetrics::CountCanvasContextUsage(
952 CanvasMetrics::kAccelerated2DCanvasGPUContextLost); 934 CanvasMetrics::kAccelerated2DCanvasGPUContextLost);
953 return nullptr; 935 return nullptr;
954 } 936 }
955 937
956 if (context_provider->IsSoftwareRendering()) 938 if (context_provider->IsSoftwareRendering())
957 return nullptr; // Don't use accelerated canvas with swiftshader. 939 return nullptr; // Don't use accelerated canvas with swiftshader.
958 940
959 std::unique_ptr<ImageBufferSurface> surface = 941 auto surface = WTF::MakeUnique<Canvas2DImageBufferSurface>(
960 WTF::WrapUnique(new Canvas2DImageBufferSurface( 942 std::move(context_provider), size(), *msaa_sample_count, opacity_mode,
961 std::move(context_provider), size(), *msaa_sample_count, opacity_mode, 943 Canvas2DLayerBridge::kEnableAcceleration, context_->color_params());
962 Canvas2DLayerBridge::kEnableAcceleration, context_->color_params()));
963 if (!surface->IsValid()) { 944 if (!surface->IsValid()) {
964 CanvasMetrics::CountCanvasContextUsage( 945 CanvasMetrics::CountCanvasContextUsage(
965 CanvasMetrics::kGPUAccelerated2DCanvasImageBufferCreationFailed); 946 CanvasMetrics::kGPUAccelerated2DCanvasImageBufferCreationFailed);
966 return nullptr; 947 return nullptr;
967 } 948 }
968 949
969 CanvasMetrics::CountCanvasContextUsage( 950 CanvasMetrics::CountCanvasContextUsage(
970 CanvasMetrics::kGPUAccelerated2DCanvasImageBufferCreated); 951 CanvasMetrics::kGPUAccelerated2DCanvasImageBufferCreated);
971 return surface; 952 return std::move(surface);
972 } 953 }
973 954
974 std::unique_ptr<ImageBufferSurface> 955 std::unique_ptr<ImageBufferSurface>
975 HTMLCanvasElement::CreateUnacceleratedImageBufferSurface( 956 HTMLCanvasElement::CreateUnacceleratedImageBufferSurface(
976 OpacityMode opacity_mode) { 957 OpacityMode opacity_mode) {
977 if (ShouldUseDisplayList()) { 958 if (ShouldUseDisplayList()) {
978 auto surface = WTF::WrapUnique(new RecordingImageBufferSurface( 959 auto surface = WTF::MakeUnique<RecordingImageBufferSurface>(
979 size(), WTF::WrapUnique(new UnacceleratedSurfaceFactory), opacity_mode, 960 size(), opacity_mode, context_->color_params());
980 context_->color_params()));
981 if (surface->IsValid()) { 961 if (surface->IsValid()) {
982 CanvasMetrics::CountCanvasContextUsage( 962 CanvasMetrics::CountCanvasContextUsage(
983 CanvasMetrics::kDisplayList2DCanvasImageBufferCreated); 963 CanvasMetrics::kDisplayList2DCanvasImageBufferCreated);
984 return std::move(surface); 964 return std::move(surface);
985 } 965 }
986 // We fallback to a non-display-list surface without recording a metric 966 // We fallback to a non-display-list surface without recording a metric
987 // here. 967 // here.
988 } 968 }
989 969
990 auto surface_factory = WTF::MakeUnique<UnacceleratedSurfaceFactory>(); 970 auto surface = WTF::MakeUnique<UnacceleratedImageBufferSurface>(
991 auto surface = surface_factory->CreateSurface(size(), opacity_mode, 971 size(), opacity_mode, kInitializeImagePixels, context_->color_params());
992 context_->color_params());
993 if (surface->IsValid()) { 972 if (surface->IsValid()) {
994 CanvasMetrics::CountCanvasContextUsage( 973 CanvasMetrics::CountCanvasContextUsage(
995 CanvasMetrics::kUnaccelerated2DCanvasImageBufferCreated); 974 CanvasMetrics::kUnaccelerated2DCanvasImageBufferCreated);
996 return surface; 975 return std::move(surface);
997 } 976 }
998 977
999 CanvasMetrics::CountCanvasContextUsage( 978 CanvasMetrics::CountCanvasContextUsage(
1000 CanvasMetrics::kUnaccelerated2DCanvasImageBufferCreationFailed); 979 CanvasMetrics::kUnaccelerated2DCanvasImageBufferCreationFailed);
1001 return nullptr; 980 return nullptr;
1002 } 981 }
1003 982
1004 void HTMLCanvasElement::CreateImageBuffer() { 983 void HTMLCanvasElement::CreateImageBuffer() {
1005 CreateImageBufferInternal(nullptr); 984 CreateImageBufferInternal(nullptr);
1006 if (did_fail_to_create_image_buffer_ && Is2d() && !size().IsEmpty()) 985 if (did_fail_to_create_image_buffer_ && Is2d() && !size().IsEmpty())
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
1472 DCHECK(!surface_layer_bridge_); 1451 DCHECK(!surface_layer_bridge_);
1473 LocalFrame* frame = GetDocument().GetFrame(); 1452 LocalFrame* frame = GetDocument().GetFrame();
1474 WebLayerTreeView* layer_tree_view = nullptr; 1453 WebLayerTreeView* layer_tree_view = nullptr;
1475 // TODO(xlai): Ensure OffscreenCanvas commit() is still functional when a 1454 // TODO(xlai): Ensure OffscreenCanvas commit() is still functional when a
1476 // frame-less HTML canvas's document is reparenting under another frame. 1455 // frame-less HTML canvas's document is reparenting under another frame.
1477 // See crbug.com/683172. 1456 // See crbug.com/683172.
1478 if (frame) { 1457 if (frame) {
1479 layer_tree_view = 1458 layer_tree_view =
1480 frame->GetPage()->GetChromeClient().GetWebLayerTreeView(frame); 1459 frame->GetPage()->GetChromeClient().GetWebLayerTreeView(frame);
1481 surface_layer_bridge_ = 1460 surface_layer_bridge_ =
1482 WTF::WrapUnique(new CanvasSurfaceLayerBridge(this, layer_tree_view)); 1461 WTF::MakeUnique<CanvasSurfaceLayerBridge>(this, layer_tree_view);
1483 // Creates a placeholder layer first before Surface is created. 1462 // Creates a placeholder layer first before Surface is created.
1484 surface_layer_bridge_->CreateSolidColorLayer(); 1463 surface_layer_bridge_->CreateSolidColorLayer();
1485 } 1464 }
1486 } 1465 }
1487 1466
1488 void HTMLCanvasElement::OnWebLayerReplaced() { 1467 void HTMLCanvasElement::OnWebLayerReplaced() {
1489 SetNeedsCompositingUpdate(); 1468 SetNeedsCompositingUpdate();
1490 } 1469 }
1491 1470
1492 } // namespace blink 1471 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2DTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698