Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 | |
| 7 #include "platform/graphics/RecordingImageBufferSurface.h" | |
| 8 | |
| 9 #include "platform/graphics/GraphicsContext.h" | |
| 10 #include "platform/graphics/ImageBuffer.h" | |
| 11 #include "third_party/skia/include/core/SkCanvas.h" | |
| 12 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
| 13 #include "wtf/PassOwnPtr.h" | |
| 14 #include "wtf/PassRefPtr.h" | |
| 15 | |
| 16 namespace WebCore { | |
| 17 | |
| 18 RecordingImageBufferSurface::RecordingImageBufferSurface(const IntSize& size, Op acityMode opacityMode) | |
| 19 : ImageBufferSurface(size, opacityMode) | |
| 20 , m_graphicsContext(0) | |
| 21 , m_frameWasCleared(true) | |
| 22 { | |
| 23 initializeCurrentFrame(); | |
| 24 } | |
| 25 | |
| 26 RecordingImageBufferSurface::~RecordingImageBufferSurface() | |
| 27 { } | |
| 28 | |
| 29 void RecordingImageBufferSurface::initializeCurrentFrame() | |
| 30 { | |
| 31 static SkRTreeFactory rTreeFactory; | |
| 32 m_currentFrame = adoptPtr(new SkPictureRecorder); | |
| 33 m_currentFrame->beginRecording(size().width(), size().height(), &rTreeFactor y); | |
| 34 if (m_graphicsContext) { | |
| 35 m_graphicsContext->resetCanvas(m_currentFrame->getRecordingCanvas()); | |
| 36 m_graphicsContext->setTrackOpaqueRegion(true); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 void RecordingImageBufferSurface::setImageBuffer(ImageBuffer* imageBuffer) | |
| 41 { | |
| 42 m_graphicsContext = imageBuffer ? imageBuffer->context() : 0; | |
| 43 if (m_currentFrame) { | |
| 44 m_graphicsContext->setTrackOpaqueRegion(true); | |
|
Stephen White
2014/07/17 18:55:48
Either the ": 0" clause above can never happen (an
Justin Novosad
2014/07/17 19:19:09
Done.
| |
| 45 m_graphicsContext->resetCanvas(m_currentFrame->getRecordingCanvas()); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 void RecordingImageBufferSurface::willReadback() | |
| 50 { | |
| 51 fallBackToRasterCanvas(); | |
| 52 } | |
| 53 | |
| 54 void RecordingImageBufferSurface::fallBackToRasterCanvas() | |
| 55 { | |
| 56 if (m_rasterCanvas) { | |
| 57 ASSERT(!m_currentFrame); | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 m_rasterCanvas = adoptPtr(SkCanvas::NewRasterN32(size().width(), size().heig ht())); | |
| 62 | |
| 63 if (m_previousFrame) { | |
| 64 m_previousFrame->draw(m_rasterCanvas.get()); | |
| 65 m_previousFrame.clear(); | |
| 66 } | |
| 67 if (m_currentFrame) { | |
| 68 RefPtr<SkPicture> currentPicture = adoptRef(m_currentFrame->endRecording ()); | |
| 69 currentPicture->draw(m_rasterCanvas.get()); | |
| 70 m_currentFrame.clear(); | |
| 71 } | |
| 72 | |
| 73 if (m_graphicsContext) { | |
| 74 m_graphicsContext->setTrackOpaqueRegion(false); | |
| 75 m_graphicsContext->resetCanvas(m_rasterCanvas.get()); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 SkCanvas* RecordingImageBufferSurface::canvas() const | |
| 80 { | |
| 81 if (m_rasterCanvas) | |
| 82 return m_rasterCanvas.get(); | |
| 83 | |
| 84 ASSERT(m_currentFrame->getRecordingCanvas()); | |
| 85 return m_currentFrame->getRecordingCanvas(); | |
| 86 } | |
| 87 | |
| 88 PassRefPtr<SkPicture> RecordingImageBufferSurface::getPicture() | |
| 89 { | |
| 90 if (handleOpaqueFrame()) | |
| 91 return m_previousFrame; | |
| 92 | |
| 93 if (!m_rasterCanvas) | |
| 94 fallBackToRasterCanvas(); | |
| 95 return nullptr; | |
| 96 } | |
| 97 | |
| 98 void RecordingImageBufferSurface::didClearCanvas() | |
| 99 { | |
| 100 m_frameWasCleared = true; | |
| 101 } | |
| 102 | |
| 103 bool RecordingImageBufferSurface::handleOpaqueFrame() | |
| 104 { | |
| 105 if (!m_currentFrame) | |
| 106 return false; | |
| 107 IntRect canvasRect(IntPoint(0, 0), size()); | |
| 108 if (!m_frameWasCleared && !m_graphicsContext->opaqueRegion().asRect().contai ns(canvasRect)) | |
| 109 return false; | |
| 110 | |
| 111 SkCanvas* oldCanvas = m_currentFrame->getRecordingCanvas(); // Could be rast er or picture | |
| 112 | |
| 113 // FIXME(crbug.com/392614): handle transferring complex state from the curre nt picture to the new one. | |
| 114 if (oldCanvas->getSaveCount()) | |
| 115 return false; | |
| 116 if (!oldCanvas->isClipRect()) | |
| 117 return false; | |
| 118 | |
| 119 SkMatrix ctm = oldCanvas->getTotalMatrix(); | |
| 120 SkRect clip; | |
| 121 oldCanvas->getClipBounds(&clip); | |
| 122 | |
| 123 m_previousFrame = adoptRef(m_currentFrame->endRecording()); | |
| 124 initializeCurrentFrame(); | |
| 125 | |
| 126 SkCanvas* newCanvas = m_currentFrame->getRecordingCanvas(); | |
| 127 newCanvas->concat(ctm); | |
| 128 newCanvas->clipRect(clip); | |
| 129 | |
| 130 m_frameWasCleared = false; | |
| 131 return true; | |
| 132 } | |
| 133 | |
| 134 } // namespace WebCore | |
| OLD | NEW |