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

Side by Side Diff: third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2DTest.cpp

Issue 2063473002: Make 2D canvas disable gpu acceleration when getImageData is called (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: unit test build fix Created 4 years, 5 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
OLDNEW
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/fetch/MemoryCache.h" 7 #include "core/fetch/MemoryCache.h"
8 #include "core/frame/FrameView.h" 8 #include "core/frame/FrameView.h"
9 #include "core/frame/ImageBitmap.h" 9 #include "core/frame/ImageBitmap.h"
10 #include "core/html/HTMLCanvasElement.h" 10 #include "core/html/HTMLCanvasElement.h"
11 #include "core/html/HTMLDocument.h" 11 #include "core/html/HTMLDocument.h"
12 #include "core/html/ImageData.h" 12 #include "core/html/ImageData.h"
13 #include "core/imagebitmap/ImageBitmapOptions.h" 13 #include "core/imagebitmap/ImageBitmapOptions.h"
14 #include "core/loader/EmptyClients.h" 14 #include "core/loader/EmptyClients.h"
15 #include "core/testing/DummyPageHolder.h" 15 #include "core/testing/DummyPageHolder.h"
16 #include "modules/canvas2d/CanvasGradient.h" 16 #include "modules/canvas2d/CanvasGradient.h"
17 #include "modules/canvas2d/CanvasPattern.h" 17 #include "modules/canvas2d/CanvasPattern.h"
18 #include "modules/webgl/WebGLRenderingContext.h" 18 #include "modules/webgl/WebGLRenderingContext.h"
19 #include "platform/graphics/Canvas2DImageBufferSurface.h"
19 #include "platform/graphics/ExpensiveCanvasHeuristicParameters.h" 20 #include "platform/graphics/ExpensiveCanvasHeuristicParameters.h"
20 #include "platform/graphics/RecordingImageBufferSurface.h" 21 #include "platform/graphics/RecordingImageBufferSurface.h"
21 #include "platform/graphics/StaticBitmapImage.h" 22 #include "platform/graphics/StaticBitmapImage.h"
22 #include "platform/graphics/UnacceleratedImageBufferSurface.h" 23 #include "platform/graphics/UnacceleratedImageBufferSurface.h"
24 #include "platform/graphics/test/FakeGLES2Interface.h"
25 #include "platform/graphics/test/FakeWebGraphicsContext3DProvider.h"
23 #include "testing/gmock/include/gmock/gmock.h" 26 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/skia/include/core/SkSurface.h" 28 #include "third_party/skia/include/core/SkSurface.h"
26 #include "wtf/PtrUtil.h" 29 #include "wtf/PtrUtil.h"
27 #include <memory> 30 #include <memory>
28 31
29 using ::testing::Mock; 32 using ::testing::Mock;
30 33
31 namespace blink { 34 namespace blink {
32 35
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 HTMLDocument& document() const { return *m_document; } 84 HTMLDocument& document() const { return *m_document; }
82 HTMLCanvasElement& canvasElement() const { return *m_canvasElement; } 85 HTMLCanvasElement& canvasElement() const { return *m_canvasElement; }
83 CanvasRenderingContext2D* context2d() const { return static_cast<CanvasRende ringContext2D*>(canvasElement().renderingContext()); } 86 CanvasRenderingContext2D* context2d() const { return static_cast<CanvasRende ringContext2D*>(canvasElement().renderingContext()); }
84 intptr_t getGlobalGPUMemoryUsage() const { return ImageBuffer::getGlobalGPUM emoryUsage(); } 87 intptr_t getGlobalGPUMemoryUsage() const { return ImageBuffer::getGlobalGPUM emoryUsage(); }
85 unsigned getGlobalAcceleratedImageBufferCount() const { return ImageBuffer:: getGlobalAcceleratedImageBufferCount(); } 88 unsigned getGlobalAcceleratedImageBufferCount() const { return ImageBuffer:: getGlobalAcceleratedImageBufferCount(); }
86 intptr_t getCurrentGPUMemoryUsage() const { return canvasElement().buffer()- >getGPUMemoryUsage(); } 89 intptr_t getCurrentGPUMemoryUsage() const { return canvasElement().buffer()- >getGPUMemoryUsage(); }
87 90
88 void createContext(OpacityMode); 91 void createContext(OpacityMode);
89 void TearDown(); 92 void TearDown();
90 void unrefCanvas(); 93 void unrefCanvas();
94 PassRefPtr<Canvas2DLayerBridge> makeBridge(std::unique_ptr<FakeWebGraphicsCo ntext3DProvider>, const IntSize&, Canvas2DLayerBridge::AccelerationMode);
91 95
92 private: 96 private:
93 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; 97 std::unique_ptr<DummyPageHolder> m_dummyPageHolder;
94 Persistent<HTMLDocument> m_document; 98 Persistent<HTMLDocument> m_document;
95 Persistent<HTMLCanvasElement> m_canvasElement; 99 Persistent<HTMLCanvasElement> m_canvasElement;
96 Persistent<MemoryCache> m_globalMemoryCache; 100 Persistent<MemoryCache> m_globalMemoryCache;
97 101
98 class WrapGradients final : public GarbageCollectedFinalized<WrapGradients> { 102 class WrapGradients final : public GarbageCollectedFinalized<WrapGradients> {
99 public: 103 public:
100 static WrapGradients* create() 104 static WrapGradients* create()
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 175
172 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()); 176 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create());
173 } 177 }
174 178
175 void CanvasRenderingContext2DTest::TearDown() 179 void CanvasRenderingContext2DTest::TearDown()
176 { 180 {
177 ThreadHeap::collectGarbage(BlinkGC::NoHeapPointersOnStack, BlinkGC::GCWithSw eep, BlinkGC::ForcedGC); 181 ThreadHeap::collectGarbage(BlinkGC::NoHeapPointersOnStack, BlinkGC::GCWithSw eep, BlinkGC::ForcedGC);
178 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); 182 replaceMemoryCacheForTesting(m_globalMemoryCache.release());
179 } 183 }
180 184
185 PassRefPtr<Canvas2DLayerBridge> CanvasRenderingContext2DTest::makeBridge(std::un ique_ptr<FakeWebGraphicsContext3DProvider> provider, const IntSize& size, Canvas 2DLayerBridge::AccelerationMode accelerationMode)
186 {
187 return adoptRef(new Canvas2DLayerBridge(std::move(provider), size, 0, NonOpa que, accelerationMode));
188 }
189
181 //============================================================================ 190 //============================================================================
182 191
183 class FakeAcceleratedImageBufferSurfaceForTesting : public UnacceleratedImageBuf ferSurface { 192 class FakeAcceleratedImageBufferSurfaceForTesting : public UnacceleratedImageBuf ferSurface {
184 public: 193 public:
185 FakeAcceleratedImageBufferSurfaceForTesting(const IntSize& size, OpacityMode mode) 194 FakeAcceleratedImageBufferSurfaceForTesting(const IntSize& size, OpacityMode mode)
186 : UnacceleratedImageBufferSurface(size, mode) 195 : UnacceleratedImageBufferSurface(size, mode)
187 , m_isAccelerated(true) { } 196 , m_isAccelerated(true) { }
188 ~FakeAcceleratedImageBufferSurfaceForTesting() override { } 197 ~FakeAcceleratedImageBufferSurfaceForTesting() override { }
189 bool isAccelerated() const override { return m_isAccelerated; } 198 bool isAccelerated() const override { return m_isAccelerated; }
190 void setIsAccelerated(bool isAccelerated) 199 void setIsAccelerated(bool isAccelerated)
(...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 } 753 }
745 754
746 TEST_F(CanvasRenderingContext2DTest, ContextDisposedBeforeCanvas) 755 TEST_F(CanvasRenderingContext2DTest, ContextDisposedBeforeCanvas)
747 { 756 {
748 createContext(NonOpaque); 757 createContext(NonOpaque);
749 758
750 canvasElement().detachContext(); 759 canvasElement().detachContext();
751 // Passes by not crashing later during teardown 760 // Passes by not crashing later during teardown
752 } 761 }
753 762
763 TEST_F(CanvasRenderingContext2DTest, GetImageDataDisablesAcceleration)
764 {
765 createContext(NonOpaque);
766 FakeGLES2Interface gl;
767 std::unique_ptr<FakeWebGraphicsContext3DProvider> contextProvider(new FakeWe bGraphicsContext3DProvider(&gl));
768 IntSize size(300, 300);
769 RefPtr<Canvas2DLayerBridge> bridge = makeBridge(std::move(contextProvider), size, Canvas2DLayerBridge::EnableAcceleration);
770 std::unique_ptr<Canvas2DImageBufferSurface> surface(new Canvas2DImageBufferS urface(bridge, size, NonOpaque));
771 canvasElement().createImageBufferUsingSurfaceForTesting(std::move(surface));
772
773 EXPECT_TRUE(bridge->isAccelerated());
774 EXPECT_EQ(1u, getGlobalAcceleratedImageBufferCount());
775 EXPECT_EQ(720000, getGlobalGPUMemoryUsage());
776
777 bool savedValue = RuntimeEnabledFeatures::canvas2dFixedRenderingModeEnabled( );
778 RuntimeEnabledFeatures::setCanvas2dFixedRenderingModeEnabled(false);
xlai (Olivia) 2016/06/23 19:55:02 Nits: Why don't you set the Canvas2dFixedRendering
Justin Novosad 2016/06/23 21:02:09 Done.
779 TrackExceptionState exceptionState;
780 context2d()->getImageData(0, 0, 1, 1, exceptionState);
781 RuntimeEnabledFeatures::setCanvas2dFixedRenderingModeEnabled(savedValue);
782
783 EXPECT_FALSE(exceptionState.hadException());
784 EXPECT_FALSE(bridge->isAccelerated());
785 EXPECT_EQ(0u, getGlobalAcceleratedImageBufferCount());
786 EXPECT_EQ(0, getGlobalGPUMemoryUsage());
787 }
788
754 } // namespace blink 789 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698