OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "modules/canvas2d/CanvasRenderingContext2D.h" | 5 #include "modules/canvas2d/CanvasRenderingContext2D.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/fetch/MemoryCache.h" | 8 #include "core/fetch/MemoryCache.h" |
9 #include "core/frame/FrameView.h" | 9 #include "core/frame/FrameView.h" |
10 #include "core/frame/ImageBitmap.h" | 10 #include "core/frame/ImageBitmap.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 class FakeImageSource : public CanvasImageSource { | 41 class FakeImageSource : public CanvasImageSource { |
42 public: | 42 public: |
43 FakeImageSource(IntSize, BitmapOpacity); | 43 FakeImageSource(IntSize, BitmapOpacity); |
44 | 44 |
45 PassRefPtr<Image> getSourceImageForCanvas(SourceImageStatus*, AccelerationHi
nt, SnapshotReason, const FloatSize&) const override; | 45 PassRefPtr<Image> getSourceImageForCanvas(SourceImageStatus*, AccelerationHi
nt, SnapshotReason, const FloatSize&) const override; |
46 | 46 |
47 bool wouldTaintOrigin(SecurityOrigin* destinationSecurityOrigin) const overr
ide { return false; } | 47 bool wouldTaintOrigin(SecurityOrigin* destinationSecurityOrigin) const overr
ide { return false; } |
48 FloatSize elementSize(const FloatSize&) const override { return FloatSize(m_
size); } | 48 FloatSize elementSize(const FloatSize&) const override { return FloatSize(m_
size); } |
49 bool isOpaque() const override { return m_isOpaque; } | 49 bool isOpaque() const override { return m_isOpaque; } |
| 50 bool isAccelerated() const { return false; } |
50 int sourceWidth() override { return m_size.width(); } | 51 int sourceWidth() override { return m_size.width(); } |
51 int sourceHeight() override { return m_size.height(); } | 52 int sourceHeight() override { return m_size.height(); } |
52 | 53 |
53 ~FakeImageSource() override { } | 54 ~FakeImageSource() override { } |
54 | 55 |
55 private: | 56 private: |
56 IntSize m_size; | 57 IntSize m_size; |
57 RefPtr<Image> m_image; | 58 RefPtr<Image> m_image; |
58 bool m_isOpaque; | 59 bool m_isOpaque; |
59 }; | 60 }; |
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
792 } else { | 793 } else { |
793 EXPECT_TRUE(canvasElement().buffer()->isAccelerated()); | 794 EXPECT_TRUE(canvasElement().buffer()->isAccelerated()); |
794 EXPECT_EQ(1u, getGlobalAcceleratedImageBufferCount()); | 795 EXPECT_EQ(1u, getGlobalAcceleratedImageBufferCount()); |
795 EXPECT_EQ(720000, getGlobalGPUMemoryUsage()); | 796 EXPECT_EQ(720000, getGlobalGPUMemoryUsage()); |
796 } | 797 } |
797 | 798 |
798 // Restore global state to prevent side-effects on other tests | 799 // Restore global state to prevent side-effects on other tests |
799 RuntimeEnabledFeatures::setCanvas2dFixedRenderingModeEnabled(savedFixedRende
ringMode); | 800 RuntimeEnabledFeatures::setCanvas2dFixedRenderingModeEnabled(savedFixedRende
ringMode); |
800 } | 801 } |
801 | 802 |
| 803 TEST_F(CanvasRenderingContext2DTest, TextureUploadHeuristics) |
| 804 { |
| 805 bool savedFixedRenderingMode = RuntimeEnabledFeatures::canvas2dFixedRenderin
gModeEnabled(); |
| 806 RuntimeEnabledFeatures::setCanvas2dFixedRenderingModeEnabled(false); |
| 807 |
| 808 enum TestVariants { |
| 809 LargeTextureDisablesAcceleration = 0, |
| 810 SmallTextureDoesNotDisableAcceleration = 1, |
| 811 |
| 812 TestVariantCount = 2, |
| 813 }; |
| 814 |
| 815 for (int testVariant = 0; testVariant < TestVariantCount; testVariant++) { |
| 816 int delta = testVariant == LargeTextureDisablesAcceleration ? 1 : -1; |
| 817 int srcSize = std::sqrt(static_cast<float>(ExpensiveCanvasHeuristicParam
eters::DrawImageTextureUploadSoftSizeLimit)) + delta; |
| 818 int dstSize = srcSize / std::sqrt(static_cast<float>(ExpensiveCanvasHeur
isticParameters::DrawImageTextureUploadSoftSizeLimitScaleThreshold)) - delta; |
| 819 |
| 820 createContext(NonOpaque); |
| 821 FakeGLES2Interface gl; |
| 822 std::unique_ptr<FakeWebGraphicsContext3DProvider> contextProvider(new Fa
keWebGraphicsContext3DProvider(&gl)); |
| 823 IntSize size(dstSize, dstSize); |
| 824 RefPtr<Canvas2DLayerBridge> bridge = makeBridge(std::move(contextProvide
r), size, Canvas2DLayerBridge::EnableAcceleration); |
| 825 std::unique_ptr<Canvas2DImageBufferSurface> surface(new Canvas2DImageBuf
ferSurface(bridge, size)); |
| 826 canvasElement().createImageBufferUsingSurfaceForTesting(std::move(surfac
e)); |
| 827 |
| 828 EXPECT_TRUE(canvasElement().buffer()->isAccelerated()); |
| 829 EXPECT_EQ(1u, getGlobalAcceleratedImageBufferCount()); |
| 830 // 4 bytes per pixel * 2 buffers = 8 |
| 831 EXPECT_EQ(8*dstSize*dstSize, getGlobalGPUMemoryUsage()); |
| 832 sk_sp<SkSurface> skSurface = SkSurface::MakeRasterN32Premul(srcSize, src
Size); |
| 833 RefPtr<StaticBitmapImage> bigBitmap = StaticBitmapImage::create(skSurfac
e->makeImageSnapshot()); |
| 834 ImageBitmap* bigImage = ImageBitmap::create(std::move(bigBitmap)); |
| 835 NonThrowableExceptionState exceptionState; |
| 836 context2d()->drawImage(nullptr, bigImage, 0, 0, srcSize, srcSize, 0, 0,
dstSize, dstSize, exceptionState); |
| 837 EXPECT_FALSE(exceptionState.hadException()); |
| 838 |
| 839 if (testVariant == LargeTextureDisablesAcceleration) { |
| 840 EXPECT_FALSE(canvasElement().buffer()->isAccelerated()); |
| 841 EXPECT_EQ(0u, getGlobalAcceleratedImageBufferCount()); |
| 842 EXPECT_EQ(0, getGlobalGPUMemoryUsage()); |
| 843 } else { |
| 844 EXPECT_TRUE(canvasElement().buffer()->isAccelerated()); |
| 845 EXPECT_EQ(1u, getGlobalAcceleratedImageBufferCount()); |
| 846 EXPECT_EQ(8*dstSize*dstSize, getGlobalGPUMemoryUsage()); |
| 847 } |
| 848 } |
| 849 // Restore global state to prevent side-effects on other tests |
| 850 RuntimeEnabledFeatures::setCanvas2dFixedRenderingModeEnabled(savedFixedRende
ringMode); |
| 851 } |
| 852 |
802 TEST_F(CanvasRenderingContext2DTest, IsAccelerationOptimalForCanvasContentHeuris
tic) | 853 TEST_F(CanvasRenderingContext2DTest, IsAccelerationOptimalForCanvasContentHeuris
tic) |
803 { | 854 { |
804 createContext(NonOpaque); | 855 createContext(NonOpaque); |
805 | 856 |
806 std::unique_ptr<FakeAcceleratedImageBufferSurfaceForTesting> fakeAccelerateS
urface = wrapUnique(new FakeAcceleratedImageBufferSurfaceForTesting(IntSize(10,
10), NonOpaque)); | 857 std::unique_ptr<FakeAcceleratedImageBufferSurfaceForTesting> fakeAccelerateS
urface = wrapUnique(new FakeAcceleratedImageBufferSurfaceForTesting(IntSize(10,
10), NonOpaque)); |
807 canvasElement().createImageBufferUsingSurfaceForTesting(std::move(fakeAccele
rateSurface)); | 858 canvasElement().createImageBufferUsingSurfaceForTesting(std::move(fakeAccele
rateSurface)); |
808 | 859 |
809 NonThrowableExceptionState exceptionState; | 860 NonThrowableExceptionState exceptionState; |
810 | 861 |
811 CanvasRenderingContext2D* context = context2d(); | 862 CanvasRenderingContext2D* context = context2d(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 EXPECT_FALSE(canvasElement().buffer()->isAccelerated()); | 903 EXPECT_FALSE(canvasElement().buffer()->isAccelerated()); |
853 | 904 |
854 context->fillRect(10, 10, 100, 100); | 905 context->fillRect(10, 10, 100, 100); |
855 | 906 |
856 EXPECT_EQ(0, getCurrentGPUMemoryUsage()); | 907 EXPECT_EQ(0, getCurrentGPUMemoryUsage()); |
857 EXPECT_EQ(0, getGlobalGPUMemoryUsage()); | 908 EXPECT_EQ(0, getGlobalGPUMemoryUsage()); |
858 EXPECT_EQ(0u, getGlobalAcceleratedImageBufferCount()); | 909 EXPECT_EQ(0u, getGlobalAcceleratedImageBufferCount()); |
859 } | 910 } |
860 | 911 |
861 } // namespace blink | 912 } // namespace blink |
OLD | NEW |