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..19b061456d3f1f2bea233c48d37543dc7e532d55 |
--- /dev/null |
+++ b/Source/platform/graphics/RecordingImageBufferSurface.cpp |
@@ -0,0 +1,160 @@ |
+/* |
+ * 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. |
+ */ |
+ |
+#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) |
Stephen White
2014/07/09 20:04:08
You only ever seem to use m_imageBuffer->context()
Justin Novosad
2014/07/11 21:16:08
Technically that would be just as circular IMHO (a
Stephen White
2014/07/14 15:19:15
I realize that, but this class only needs the Grap
|
+ , 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); |
dshwang
2014/07/10 13:01:52
how much overhead when we turn on TrackOpaqueRegio
Justin Novosad
2014/07/11 21:16:08
It is low overhead. This method only tracks simple
|
+ } |
+} |
+ |
+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) |
+ 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() |
+{ |
dshwang
2014/07/10 13:01:52
Following code might be needed, because we cannot
Justin Novosad
2014/07/11 21:16:08
Your logic is correct, but the check is not needed
|
+ if (handleOpaqueFrame()) |
+ return m_previousFrame; |
Stephen White
2014/07/09 20:04:08
As discussed, it would be nice to handle the case
Justin Novosad
2014/07/11 21:16:08
Agreed. I filed a bug about this so we can deal wi
|
+ |
+ 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 |
+ |
+ // TODO: handle transferring complex state from the current |
Stephen White
2014/07/09 20:04:08
Pls add a bug for this.
Justin Novosad
2014/07/11 21:16:08
Done.
|
+ // picture to the new one. |
+ if (oldCanvas->getSaveCount()) |
+ return false; |
+ if (!oldCanvas->isClipRect()) |
Stephen White
2014/07/09 20:04:08
You should probably also check that the clip op is
Justin Novosad
2014/07/11 21:16:08
Already covered by isClipRect. IsClipRect checks w
Stephen White
2014/07/14 15:19:15
Acknowledged.
|
+ 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 |