| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 #include "platform/image-decoders/ImageDecoder.h" | 39 #include "platform/image-decoders/ImageDecoder.h" |
| 40 #include "platform/image-decoders/ImageFrame.h" | 40 #include "platform/image-decoders/ImageFrame.h" |
| 41 #include "platform/image-decoders/SegmentReader.h" | 41 #include "platform/image-decoders/SegmentReader.h" |
| 42 #include "platform/image-encoders/PNGImageEncoder.h" | 42 #include "platform/image-encoders/PNGImageEncoder.h" |
| 43 #include "third_party/skia/include/core/SkData.h" | 43 #include "third_party/skia/include/core/SkData.h" |
| 44 #include "third_party/skia/include/core/SkImage.h" | 44 #include "third_party/skia/include/core/SkImage.h" |
| 45 #include "third_party/skia/include/core/SkPictureRecorder.h" | 45 #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 46 #include "third_party/skia/include/core/SkStream.h" | 46 #include "third_party/skia/include/core/SkStream.h" |
| 47 #include "wtf/CurrentTime.h" | 47 #include "wtf/CurrentTime.h" |
| 48 #include "wtf/HexNumber.h" | 48 #include "wtf/HexNumber.h" |
| 49 #include "wtf/PtrUtil.h" | |
| 50 #include "wtf/text/Base64.h" | 49 #include "wtf/text/Base64.h" |
| 51 #include "wtf/text/TextEncoding.h" | 50 #include "wtf/text/TextEncoding.h" |
| 52 #include <memory> | |
| 53 | 51 |
| 54 namespace blink { | 52 namespace blink { |
| 55 | 53 |
| 56 PictureSnapshot::PictureSnapshot(PassRefPtr<const SkPicture> picture) | 54 PictureSnapshot::PictureSnapshot(PassRefPtr<const SkPicture> picture) |
| 57 : m_picture(picture) | 55 : m_picture(picture) |
| 58 { | 56 { |
| 59 } | 57 } |
| 60 | 58 |
| 61 static bool decodeBitmap(const void* data, size_t length, SkBitmap* result) | 59 static bool decodeBitmap(const void* data, size_t length, SkBitmap* result) |
| 62 { | 60 { |
| 63 std::unique_ptr<ImageDecoder> imageDecoder = ImageDecoder::create(static_cas
t<const char*>(data), length, | 61 OwnPtr<ImageDecoder> imageDecoder = ImageDecoder::create(static_cast<const c
har*>(data), length, |
| 64 ImageDecoder::AlphaPremultiplied, ImageDecoder::GammaAndColorProfileIgno
red); | 62 ImageDecoder::AlphaPremultiplied, ImageDecoder::GammaAndColorProfileIgno
red); |
| 65 if (!imageDecoder) | 63 if (!imageDecoder) |
| 66 return false; | 64 return false; |
| 67 | 65 |
| 68 // No need to copy the data; this decodes immediately. | 66 // No need to copy the data; this decodes immediately. |
| 69 RefPtr<SegmentReader> segmentReader = SegmentReader::createFromSkData(adoptR
ef(SkData::NewWithoutCopy(data, length))); | 67 RefPtr<SegmentReader> segmentReader = SegmentReader::createFromSkData(adoptR
ef(SkData::NewWithoutCopy(data, length))); |
| 70 imageDecoder->setData(segmentReader.release(), true); | 68 imageDecoder->setData(segmentReader.release(), true); |
| 71 ImageFrame* frame = imageDecoder->frameBufferAtIndex(0); | 69 ImageFrame* frame = imageDecoder->frameBufferAtIndex(0); |
| 72 if (!frame) | 70 if (!frame) |
| 73 return true; | 71 return true; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 102 canvas->restore(); | 100 canvas->restore(); |
| 103 } | 101 } |
| 104 return adoptRef(new PictureSnapshot(fromSkSp(recorder.finishRecordingAsPictu
re()))); | 102 return adoptRef(new PictureSnapshot(fromSkSp(recorder.finishRecordingAsPictu
re()))); |
| 105 } | 103 } |
| 106 | 104 |
| 107 bool PictureSnapshot::isEmpty() const | 105 bool PictureSnapshot::isEmpty() const |
| 108 { | 106 { |
| 109 return m_picture->cullRect().isEmpty(); | 107 return m_picture->cullRect().isEmpty(); |
| 110 } | 108 } |
| 111 | 109 |
| 112 std::unique_ptr<Vector<char>> PictureSnapshot::replay(unsigned fromStep, unsigne
d toStep, double scale) const | 110 PassOwnPtr<Vector<char>> PictureSnapshot::replay(unsigned fromStep, unsigned toS
tep, double scale) const |
| 113 { | 111 { |
| 114 const SkIRect bounds = m_picture->cullRect().roundOut(); | 112 const SkIRect bounds = m_picture->cullRect().roundOut(); |
| 115 | 113 |
| 116 // TODO(fmalita): convert this to SkSurface/SkImage, drop the intermediate S
kBitmap. | 114 // TODO(fmalita): convert this to SkSurface/SkImage, drop the intermediate S
kBitmap. |
| 117 SkBitmap bitmap; | 115 SkBitmap bitmap; |
| 118 bitmap.allocPixels(SkImageInfo::MakeN32Premul(bounds.width(), bounds.height(
))); | 116 bitmap.allocPixels(SkImageInfo::MakeN32Premul(bounds.width(), bounds.height(
))); |
| 119 bitmap.eraseARGB(0, 0, 0, 0); | 117 bitmap.eraseARGB(0, 0, 0, 0); |
| 120 { | 118 { |
| 121 ReplayingCanvas canvas(bitmap, fromStep, toStep); | 119 ReplayingCanvas canvas(bitmap, fromStep, toStep); |
| 122 // Disable LCD text preemptively, because the picture opacity is unknown
. | 120 // Disable LCD text preemptively, because the picture opacity is unknown
. |
| 123 // The canonical API involves SkSurface props, but since we're not SkSur
face-based | 121 // The canonical API involves SkSurface props, but since we're not SkSur
face-based |
| 124 // at this point (see TODO above) we (ab)use saveLayer for this purpose. | 122 // at this point (see TODO above) we (ab)use saveLayer for this purpose. |
| 125 SkAutoCanvasRestore autoRestore(&canvas, false); | 123 SkAutoCanvasRestore autoRestore(&canvas, false); |
| 126 canvas.saveLayer(nullptr, nullptr); | 124 canvas.saveLayer(nullptr, nullptr); |
| 127 | 125 |
| 128 canvas.scale(scale, scale); | 126 canvas.scale(scale, scale); |
| 129 canvas.resetStepCount(); | 127 canvas.resetStepCount(); |
| 130 m_picture->playback(&canvas, &canvas); | 128 m_picture->playback(&canvas, &canvas); |
| 131 } | 129 } |
| 132 std::unique_ptr<Vector<char>> base64Data = wrapUnique(new Vector<char>()); | 130 OwnPtr<Vector<char>> base64Data = adoptPtr(new Vector<char>()); |
| 133 Vector<char> encodedImage; | 131 Vector<char> encodedImage; |
| 134 | 132 |
| 135 RefPtr<SkImage> image = fromSkSp(SkImage::MakeFromBitmap(bitmap)); | 133 RefPtr<SkImage> image = fromSkSp(SkImage::MakeFromBitmap(bitmap)); |
| 136 if (!image) | 134 if (!image) |
| 137 return nullptr; | 135 return nullptr; |
| 138 | 136 |
| 139 ImagePixelLocker pixelLocker(image, kUnpremul_SkAlphaType, kRGBA_8888_SkColo
rType); | 137 ImagePixelLocker pixelLocker(image, kUnpremul_SkAlphaType, kRGBA_8888_SkColo
rType); |
| 140 ImageDataBuffer imageData(IntSize(image->width(), image->height()), | 138 ImageDataBuffer imageData(IntSize(image->width(), image->height()), |
| 141 static_cast<const unsigned char*>(pixelLocker.pixels())); | 139 static_cast<const unsigned char*>(pixelLocker.pixels())); |
| 142 if (!PNGImageEncoder::encode(imageData, reinterpret_cast<Vector<unsigned cha
r>*>(&encodedImage))) | 140 if (!PNGImageEncoder::encode(imageData, reinterpret_cast<Vector<unsigned cha
r>*>(&encodedImage))) |
| 143 return nullptr; | 141 return nullptr; |
| 144 | 142 |
| 145 base64Encode(encodedImage, *base64Data); | 143 base64Encode(encodedImage, *base64Data); |
| 146 return base64Data; | 144 return base64Data; |
| 147 } | 145 } |
| 148 | 146 |
| 149 std::unique_ptr<PictureSnapshot::Timings> PictureSnapshot::profile(unsigned minR
epeatCount, double minDuration, const FloatRect* clipRect) const | 147 PassOwnPtr<PictureSnapshot::Timings> PictureSnapshot::profile(unsigned minRepeat
Count, double minDuration, const FloatRect* clipRect) const |
| 150 { | 148 { |
| 151 std::unique_ptr<PictureSnapshot::Timings> timings = wrapUnique(new PictureSn
apshot::Timings()); | 149 OwnPtr<PictureSnapshot::Timings> timings = adoptPtr(new PictureSnapshot::Tim
ings()); |
| 152 timings->reserveCapacity(minRepeatCount); | 150 timings->reserveCapacity(minRepeatCount); |
| 153 const SkIRect bounds = m_picture->cullRect().roundOut(); | 151 const SkIRect bounds = m_picture->cullRect().roundOut(); |
| 154 SkBitmap bitmap; | 152 SkBitmap bitmap; |
| 155 bitmap.allocPixels(SkImageInfo::MakeN32Premul(bounds.width(), bounds.height(
))); | 153 bitmap.allocPixels(SkImageInfo::MakeN32Premul(bounds.width(), bounds.height(
))); |
| 156 bitmap.eraseARGB(0, 0, 0, 0); | 154 bitmap.eraseARGB(0, 0, 0, 0); |
| 157 | 155 |
| 158 double now = WTF::monotonicallyIncreasingTime(); | 156 double now = WTF::monotonicallyIncreasingTime(); |
| 159 double stopTime = now + minDuration; | 157 double stopTime = now + minDuration; |
| 160 for (unsigned step = 0; step < minRepeatCount || now < stopTime; ++step) { | 158 for (unsigned step = 0; step < minRepeatCount || now < stopTime; ++step) { |
| 161 timings->append(Vector<double>()); | 159 timings->append(Vector<double>()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 176 | 174 |
| 177 PassRefPtr<JSONArray> PictureSnapshot::snapshotCommandLog() const | 175 PassRefPtr<JSONArray> PictureSnapshot::snapshotCommandLog() const |
| 178 { | 176 { |
| 179 const SkIRect bounds = m_picture->cullRect().roundOut(); | 177 const SkIRect bounds = m_picture->cullRect().roundOut(); |
| 180 LoggingCanvas canvas(bounds.width(), bounds.height()); | 178 LoggingCanvas canvas(bounds.width(), bounds.height()); |
| 181 m_picture->playback(&canvas); | 179 m_picture->playback(&canvas); |
| 182 return canvas.log(); | 180 return canvas.log(); |
| 183 } | 181 } |
| 184 | 182 |
| 185 } // namespace blink | 183 } // namespace blink |
| OLD | NEW |