Index: Source/platform/graphics/RecordingImageBufferSurface.cpp |
diff --git a/Source/platform/graphics/RecordingImageBufferSurface.cpp b/Source/platform/graphics/RecordingImageBufferSurface.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9320ee0cc09b0f76837be347be8a783b6876ed20 |
--- /dev/null |
+++ b/Source/platform/graphics/RecordingImageBufferSurface.cpp |
@@ -0,0 +1,159 @@ |
+/* |
+ * Copyright (c) 2013, Google Inc. All rights reserved. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions are |
+ * met: |
+ * |
+ * * Redistributions of source code must retain the above copyright |
+ * notice, this list of conditions and the following disclaimer. |
+ * * Redistributions in binary form must reproduce the above |
+ * copyright notice, this list of conditions and the following disclaimer |
+ * in the documentation and/or other materials provided with the |
+ * distribution. |
+ * * Neither the name of Google Inc. nor the names of its |
+ * contributors may be used to endorse or promote products derived from |
+ * this software without specific prior written permission. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ */ |
dshwang
2014/07/14 11:45:40
nits: Use brand-new license.
Stephen White
2014/07/14 15:19:15
Good point; see http://www.chromium.org/blink/codi
Justin Novosad
2014/07/17 18:34:14
Done.
|
+ |
+#include "config.h" |
+ |
+#include "platform/graphics/RecordingImageBufferSurface.h" |
+ |
+#include "platform/graphics/GraphicsContext.h" |
+#include "platform/graphics/ImageBuffer.h" |
+#include "third_party/skia/include/core/SkCanvas.h" |
+#include "third_party/skia/include/core/SkPictureRecorder.h" |
+#include "wtf/PassOwnPtr.h" |
+#include "wtf/PassRefPtr.h" |
+ |
+namespace WebCore { |
+ |
+RecordingImageBufferSurface::RecordingImageBufferSurface(const IntSize& size, OpacityMode opacityMode) |
+ : ImageBufferSurface(size, opacityMode) |
+ , m_imageBuffer(0) |
+ , m_frameWasCleared(true) |
+{ |
+ initializeCurrentFrame(); |
+} |
+ |
+RecordingImageBufferSurface::~RecordingImageBufferSurface() |
+{ } |
+ |
+void RecordingImageBufferSurface::initializeCurrentFrame() |
+{ |
+ static SkRTreeFactory rTreeFactory; |
+ m_currentFrame = adoptPtr(new SkPictureRecorder); |
+ m_currentFrame->beginRecording(size().width(), size().height(), &rTreeFactory); |
+ if (m_imageBuffer) { |
+ m_imageBuffer->context()->resetCanvas(m_currentFrame->getRecordingCanvas()); |
+ m_imageBuffer->context()->setTrackOpaqueRegion(true); |
+ } |
+} |
+ |
+void RecordingImageBufferSurface::setImageBuffer(ImageBuffer* imageBuffer) |
+{ |
+ m_imageBuffer = imageBuffer; |
+ if (m_currentFrame) { |
+ m_imageBuffer->context()->setTrackOpaqueRegion(true); |
+ m_imageBuffer->context()->resetCanvas(m_currentFrame->getRecordingCanvas()); |
+ } |
+} |
+ |
+ |
+void RecordingImageBufferSurface::willReadback() |
+{ |
+ fallBackToRasterCanvas(); |
+} |
+ |
+void RecordingImageBufferSurface::fallBackToRasterCanvas() |
+{ |
+ if (m_rasterCanvas) |
dshwang
2014/07/14 11:45:40
How about adding ASSERT(!m_currentFrame);?
Justin Novosad
2014/07/17 18:34:14
Done.
|
+ return; |
+ |
+ m_rasterCanvas = adoptPtr(SkCanvas::NewRasterN32(size().width(), size().height())); |
+ |
+ if (m_previousFrame) { |
+ m_previousFrame->draw(m_rasterCanvas.get()); |
+ m_previousFrame.clear(); |
+ } |
+ if (m_currentFrame) { |
+ RefPtr<SkPicture> currentPicture = adoptRef(m_currentFrame->endRecording()); |
+ currentPicture->draw(m_rasterCanvas.get()); |
+ m_currentFrame.clear(); |
+ } |
+ |
+ if (m_imageBuffer) { |
+ m_imageBuffer->context()->setTrackOpaqueRegion(false); |
+ m_imageBuffer->context()->resetCanvas(m_rasterCanvas.get()); |
+ } |
+} |
+ |
+SkCanvas* RecordingImageBufferSurface::canvas() const |
+{ |
+ if (m_rasterCanvas) |
+ return m_rasterCanvas.get(); |
+ |
+ ASSERT(m_currentFrame->getRecordingCanvas()); |
+ return m_currentFrame->getRecordingCanvas(); |
+} |
+ |
+PassRefPtr<SkPicture> RecordingImageBufferSurface::getPicture() |
+{ |
+ if (handleOpaqueFrame()) |
+ return m_previousFrame; |
+ |
+ if (!m_rasterCanvas) |
+ fallBackToRasterCanvas(); |
+ return nullptr; |
+} |
+ |
+void RecordingImageBufferSurface::didClearCanvas() |
+{ |
+ m_frameWasCleared = true; |
+} |
+ |
+bool RecordingImageBufferSurface::handleOpaqueFrame() |
+{ |
+ if (!m_currentFrame) |
+ return false; |
+ IntRect canvasRect(IntPoint(0, 0), size()); |
+ if (!m_frameWasCleared && !m_imageBuffer->context()->opaqueRegion().asRect().contains(canvasRect)) |
+ return false; |
+ |
+ SkCanvas* oldCanvas = m_currentFrame->getRecordingCanvas(); // Could be raster or picture |
+ |
+ // FIXME(crbug.com/392614): handle transferring complex state from the current picture to the new one. |
+ if (oldCanvas->getSaveCount()) |
+ return false; |
+ if (!oldCanvas->isClipRect() || oldCanvas->) |
+ return false; |
+ |
+ SkMatrix ctm = oldCanvas->getTotalMatrix(); |
+ SkRect clip; |
+ oldCanvas->getClipBounds(&clip); |
+ |
+ m_previousFrame = adoptRef(m_currentFrame->endRecording()); |
+ initializeCurrentFrame(); |
+ |
+ SkCanvas* newCanvas = m_currentFrame->getRecordingCanvas(); |
+ newCanvas->concat(ctm); |
+ newCanvas->clipRect(clip); |
+ |
+ m_frameWasCleared = false; |
+ return true; |
+} |
+ |
+} // namespace WebCore |