Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2013, Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
|
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.
| |
| 30 | |
| 31 #include "config.h" | |
| 32 | |
| 33 #include "platform/graphics/RecordingImageBufferSurface.h" | |
| 34 | |
| 35 #include "platform/graphics/GraphicsContext.h" | |
| 36 #include "platform/graphics/ImageBuffer.h" | |
| 37 #include "third_party/skia/include/core/SkCanvas.h" | |
| 38 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
| 39 #include "wtf/PassOwnPtr.h" | |
| 40 #include "wtf/PassRefPtr.h" | |
| 41 | |
| 42 namespace WebCore { | |
| 43 | |
| 44 RecordingImageBufferSurface::RecordingImageBufferSurface(const IntSize& size, Op acityMode opacityMode) | |
| 45 : ImageBufferSurface(size, opacityMode) | |
| 46 , m_imageBuffer(0) | |
| 47 , m_frameWasCleared(true) | |
| 48 { | |
| 49 initializeCurrentFrame(); | |
| 50 } | |
| 51 | |
| 52 RecordingImageBufferSurface::~RecordingImageBufferSurface() | |
| 53 { } | |
| 54 | |
| 55 void RecordingImageBufferSurface::initializeCurrentFrame() | |
| 56 { | |
| 57 static SkRTreeFactory rTreeFactory; | |
| 58 m_currentFrame = adoptPtr(new SkPictureRecorder); | |
| 59 m_currentFrame->beginRecording(size().width(), size().height(), &rTreeFactor y); | |
| 60 if (m_imageBuffer) { | |
| 61 m_imageBuffer->context()->resetCanvas(m_currentFrame->getRecordingCanvas ()); | |
| 62 m_imageBuffer->context()->setTrackOpaqueRegion(true); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void RecordingImageBufferSurface::setImageBuffer(ImageBuffer* imageBuffer) | |
| 67 { | |
| 68 m_imageBuffer = imageBuffer; | |
| 69 if (m_currentFrame) { | |
| 70 m_imageBuffer->context()->setTrackOpaqueRegion(true); | |
| 71 m_imageBuffer->context()->resetCanvas(m_currentFrame->getRecordingCanvas ()); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 | |
| 76 void RecordingImageBufferSurface::willReadback() | |
| 77 { | |
| 78 fallBackToRasterCanvas(); | |
| 79 } | |
| 80 | |
| 81 void RecordingImageBufferSurface::fallBackToRasterCanvas() | |
| 82 { | |
| 83 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.
| |
| 84 return; | |
| 85 | |
| 86 m_rasterCanvas = adoptPtr(SkCanvas::NewRasterN32(size().width(), size().heig ht())); | |
| 87 | |
| 88 if (m_previousFrame) { | |
| 89 m_previousFrame->draw(m_rasterCanvas.get()); | |
| 90 m_previousFrame.clear(); | |
| 91 } | |
| 92 if (m_currentFrame) { | |
| 93 RefPtr<SkPicture> currentPicture = adoptRef(m_currentFrame->endRecording ()); | |
| 94 currentPicture->draw(m_rasterCanvas.get()); | |
| 95 m_currentFrame.clear(); | |
| 96 } | |
| 97 | |
| 98 if (m_imageBuffer) { | |
| 99 m_imageBuffer->context()->setTrackOpaqueRegion(false); | |
| 100 m_imageBuffer->context()->resetCanvas(m_rasterCanvas.get()); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 SkCanvas* RecordingImageBufferSurface::canvas() const | |
| 105 { | |
| 106 if (m_rasterCanvas) | |
| 107 return m_rasterCanvas.get(); | |
| 108 | |
| 109 ASSERT(m_currentFrame->getRecordingCanvas()); | |
| 110 return m_currentFrame->getRecordingCanvas(); | |
| 111 } | |
| 112 | |
| 113 PassRefPtr<SkPicture> RecordingImageBufferSurface::getPicture() | |
| 114 { | |
| 115 if (handleOpaqueFrame()) | |
| 116 return m_previousFrame; | |
| 117 | |
| 118 if (!m_rasterCanvas) | |
| 119 fallBackToRasterCanvas(); | |
| 120 return nullptr; | |
| 121 } | |
| 122 | |
| 123 void RecordingImageBufferSurface::didClearCanvas() | |
| 124 { | |
| 125 m_frameWasCleared = true; | |
| 126 } | |
| 127 | |
| 128 bool RecordingImageBufferSurface::handleOpaqueFrame() | |
| 129 { | |
| 130 if (!m_currentFrame) | |
| 131 return false; | |
| 132 IntRect canvasRect(IntPoint(0, 0), size()); | |
| 133 if (!m_frameWasCleared && !m_imageBuffer->context()->opaqueRegion().asRect() .contains(canvasRect)) | |
| 134 return false; | |
| 135 | |
| 136 SkCanvas* oldCanvas = m_currentFrame->getRecordingCanvas(); // Could be rast er or picture | |
| 137 | |
| 138 // FIXME(crbug.com/392614): handle transferring complex state from the curre nt picture to the new one. | |
| 139 if (oldCanvas->getSaveCount()) | |
| 140 return false; | |
| 141 if (!oldCanvas->isClipRect() || oldCanvas->) | |
| 142 return false; | |
| 143 | |
| 144 SkMatrix ctm = oldCanvas->getTotalMatrix(); | |
| 145 SkRect clip; | |
| 146 oldCanvas->getClipBounds(&clip); | |
| 147 | |
| 148 m_previousFrame = adoptRef(m_currentFrame->endRecording()); | |
| 149 initializeCurrentFrame(); | |
| 150 | |
| 151 SkCanvas* newCanvas = m_currentFrame->getRecordingCanvas(); | |
| 152 newCanvas->concat(ctm); | |
| 153 newCanvas->clipRect(clip); | |
| 154 | |
| 155 m_frameWasCleared = false; | |
| 156 return true; | |
| 157 } | |
| 158 | |
| 159 } // namespace WebCore | |
| OLD | NEW |