Chromium Code Reviews| 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 "config.h" | 5 #include "config.h" |
| 6 | 6 |
| 7 #include "platform/graphics/RecordingImageBufferSurface.h" | 7 #include "platform/graphics/RecordingImageBufferSurface.h" |
| 8 | 8 |
| 9 #include "platform/graphics/GraphicsContext.h" | 9 #include "platform/graphics/GraphicsContext.h" |
| 10 #include "platform/graphics/ImageBuffer.h" | 10 #include "platform/graphics/ImageBuffer.h" |
| 11 #include "public/platform/Platform.h" | |
| 11 #include "third_party/skia/include/core/SkCanvas.h" | 12 #include "third_party/skia/include/core/SkCanvas.h" |
| 12 #include "third_party/skia/include/core/SkPictureRecorder.h" | 13 #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 13 #include "wtf/PassOwnPtr.h" | 14 #include "wtf/PassOwnPtr.h" |
| 14 #include "wtf/PassRefPtr.h" | 15 #include "wtf/PassRefPtr.h" |
| 15 | 16 |
| 16 namespace blink { | 17 namespace blink { |
| 17 | 18 |
| 18 RecordingImageBufferSurface::RecordingImageBufferSurface(const IntSize& size, Op acityMode opacityMode) | 19 RecordingImageBufferSurface::RecordingImageBufferSurface(const IntSize& size, Op acityMode opacityMode) |
| 19 : ImageBufferSurface(size, opacityMode) | 20 : ImageBufferSurface(size, opacityMode) |
| 20 , m_graphicsContext(0) | 21 , m_graphicsContext(0) |
| 21 , m_initialSaveCount(0) | 22 , m_initialSaveCount(0) |
| 22 , m_frameWasCleared(true) | 23 , m_frameWasCleared(true) |
| 23 , m_surfaceUsedSincePreviousFrameWasPresented(false) | 24 , m_recordedSinceLastFrameWasFinalized(false) |
| 24 { | 25 { |
| 25 initializeCurrentFrame(); | 26 initializeCurrentFrame(); |
| 26 } | 27 } |
| 27 | 28 |
| 28 RecordingImageBufferSurface::~RecordingImageBufferSurface() | 29 RecordingImageBufferSurface::~RecordingImageBufferSurface() |
| 29 { } | 30 { |
| 31 if (m_recordedSinceLastFrameWasFinalized) { | |
| 32 blink::Platform::current()->currentThread()->removeTaskObserver(this); | |
| 33 } | |
| 34 } | |
| 30 | 35 |
| 31 void RecordingImageBufferSurface::initializeCurrentFrame() | 36 void RecordingImageBufferSurface::initializeCurrentFrame() |
| 32 { | 37 { |
| 33 static SkRTreeFactory rTreeFactory; | 38 static SkRTreeFactory rTreeFactory; |
| 34 m_currentFrame = adoptPtr(new SkPictureRecorder); | 39 m_currentFrame = adoptPtr(new SkPictureRecorder); |
| 35 m_currentFrame->beginRecording(size().width(), size().height(), &rTreeFactor y); | 40 m_currentFrame->beginRecording(size().width(), size().height(), &rTreeFactor y); |
| 36 m_initialSaveCount = m_currentFrame->getRecordingCanvas()->getSaveCount(); | 41 m_initialSaveCount = m_currentFrame->getRecordingCanvas()->getSaveCount(); |
| 37 if (m_graphicsContext) { | 42 if (m_graphicsContext) { |
| 38 m_graphicsContext->resetCanvas(m_currentFrame->getRecordingCanvas()); | 43 m_graphicsContext->resetCanvas(m_currentFrame->getRecordingCanvas()); |
| 39 m_graphicsContext->setRegionTrackingMode(GraphicsContext::RegionTracking Overwrite); | 44 m_graphicsContext->setRegionTrackingMode(GraphicsContext::RegionTracking Overwrite); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 { | 88 { |
| 84 if (m_rasterCanvas) | 89 if (m_rasterCanvas) |
| 85 return m_rasterCanvas.get(); | 90 return m_rasterCanvas.get(); |
| 86 | 91 |
| 87 ASSERT(m_currentFrame->getRecordingCanvas()); | 92 ASSERT(m_currentFrame->getRecordingCanvas()); |
| 88 return m_currentFrame->getRecordingCanvas(); | 93 return m_currentFrame->getRecordingCanvas(); |
| 89 } | 94 } |
| 90 | 95 |
| 91 PassRefPtr<SkPicture> RecordingImageBufferSurface::getPicture() | 96 PassRefPtr<SkPicture> RecordingImageBufferSurface::getPicture() |
| 92 { | 97 { |
| 93 if (handleOpaqueFrame()) { | 98 if (finalizeFrame()) { |
| 94 m_surfaceUsedSincePreviousFrameWasPresented = false; | |
| 95 return m_previousFrame; | 99 return m_previousFrame; |
| 96 } | 100 } |
| 97 | 101 |
| 98 if (!m_rasterCanvas) | 102 if (!m_rasterCanvas) |
| 99 fallBackToRasterCanvas(); | 103 fallBackToRasterCanvas(); |
| 100 return nullptr; | 104 return nullptr; |
| 101 } | 105 } |
| 102 | 106 |
| 103 void RecordingImageBufferSurface::willUse() | 107 void RecordingImageBufferSurface::didDraw() |
| 104 { | 108 { |
| 105 m_surfaceUsedSincePreviousFrameWasPresented = true; | 109 if (!m_recordedSinceLastFrameWasFinalized && m_currentFrame) { |
| 110 m_recordedSinceLastFrameWasFinalized = true; | |
| 111 blink::Platform::current()->currentThread()->addTaskObserver(this); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void RecordingImageBufferSurface::willProcessTask() | |
| 116 { | |
| 117 } | |
| 118 | |
| 119 void RecordingImageBufferSurface::didProcessTask() | |
| 120 { | |
| 121 ASSERT(m_recordedSinceLastFrameWasFinalized); | |
| 122 // This is to insert a frame barrier at the end of each script execution | |
| 123 // task that touches the canvas. finalizeFrame() will discard the previous | |
| 124 // frame and replace it with the current frame even if it has not been | |
| 125 // consumed, thus allowing the renderer to skip ahead to the most recently | |
| 126 // recorded frame. | |
| 127 if (!finalizeFrame()) { | |
| 128 ASSERT(!m_rasterCanvas); | |
| 129 fallBackToRasterCanvas(); | |
| 130 } | |
| 106 } | 131 } |
| 107 | 132 |
| 108 void RecordingImageBufferSurface::didClearCanvas() | 133 void RecordingImageBufferSurface::didClearCanvas() |
| 109 { | 134 { |
| 110 m_frameWasCleared = true; | 135 m_frameWasCleared = true; |
| 111 } | 136 } |
| 112 | 137 |
| 113 bool RecordingImageBufferSurface::handleOpaqueFrame() | 138 bool RecordingImageBufferSurface::finalizeFrame() |
| 114 { | 139 { |
| 115 if (!m_currentFrame) | 140 if (!m_currentFrame) { |
| 141 ASSERT(!m_recordedSinceLastFrameWasFinalized); | |
| 116 return false; | 142 return false; |
| 143 } | |
| 144 | |
| 145 bool canvasIsAnimated = m_recordedSinceLastFrameWasFinalized; | |
| 146 if (m_recordedSinceLastFrameWasFinalized) { | |
| 147 blink::Platform::current()->currentThread()->removeTaskObserver(this); | |
| 148 m_recordedSinceLastFrameWasFinalized = false; | |
| 149 } | |
| 150 | |
| 117 IntRect canvasRect(IntPoint(0, 0), size()); | 151 IntRect canvasRect(IntPoint(0, 0), size()); |
| 118 if (!m_frameWasCleared && !m_graphicsContext->opaqueRegion().asRect().contai ns(canvasRect)) { | 152 if (!m_frameWasCleared && !m_graphicsContext->opaqueRegion().asRect().contai ns(canvasRect)) { |
| 119 // No clear happened. If absolutely nothing was drawn, then we can just continue to use the previous frame. | 153 // No clear happened. If absolutely nothing was drawn, then we can just continue to use the previous frame. |
| 120 return !m_surfaceUsedSincePreviousFrameWasPresented; | 154 return !canvasIsAnimated; |
|
Stephen White
2014/07/29 18:38:55
Is this more like "canvas has changed"? I.e., some
| |
| 121 } | 155 } |
| 122 | 156 |
| 123 SkCanvas* oldCanvas = m_currentFrame->getRecordingCanvas(); // Could be rast er or picture | 157 SkCanvas* oldCanvas = m_currentFrame->getRecordingCanvas(); // Could be rast er or picture |
| 124 | 158 |
| 125 // FIXME(crbug.com/392614): handle transferring complex state from the curre nt picture to the new one. | 159 // FIXME(crbug.com/392614): handle transferring complex state from the curre nt picture to the new one. |
| 126 if (oldCanvas->getSaveCount() > m_initialSaveCount) | 160 if (oldCanvas->getSaveCount() > m_initialSaveCount) |
| 127 return false; | 161 return false; |
| 128 | 162 |
| 129 if (!oldCanvas->isClipRect()) | 163 if (!oldCanvas->isClipRect()) |
| 130 return false; | 164 return false; |
| 131 | 165 |
| 132 SkMatrix ctm = oldCanvas->getTotalMatrix(); | 166 SkMatrix ctm = oldCanvas->getTotalMatrix(); |
| 133 SkRect clip; | 167 SkRect clip; |
| 134 oldCanvas->getClipBounds(&clip); | 168 oldCanvas->getClipBounds(&clip); |
| 135 | 169 |
| 136 m_previousFrame = adoptRef(m_currentFrame->endRecording()); | 170 m_previousFrame = adoptRef(m_currentFrame->endRecording()); |
| 137 initializeCurrentFrame(); | 171 initializeCurrentFrame(); |
| 138 | 172 |
| 139 SkCanvas* newCanvas = m_currentFrame->getRecordingCanvas(); | 173 SkCanvas* newCanvas = m_currentFrame->getRecordingCanvas(); |
| 140 newCanvas->concat(ctm); | 174 newCanvas->concat(ctm); |
| 141 newCanvas->clipRect(clip); | 175 newCanvas->clipRect(clip); |
| 142 | 176 |
| 143 m_frameWasCleared = false; | 177 m_frameWasCleared = false; |
| 144 return true; | 178 return true; |
| 145 } | 179 } |
| 146 | 180 |
| 147 } // namespace blink | 181 } // namespace blink |
| OLD | NEW |