Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / graphics / PictureSnapshot.cpp
blobfb416177ceab8fffd995a1e770b65b31454ca8ae
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 #include "config.h"
32 #include "platform/graphics/PictureSnapshot.h"
34 #include "platform/geometry/IntSize.h"
35 #include "platform/graphics/ImageBuffer.h"
36 #include "platform/graphics/LoggingCanvas.h"
37 #include "platform/graphics/ProfilingCanvas.h"
38 #include "platform/graphics/ReplayingCanvas.h"
39 #include "platform/graphics/skia/ImagePixelLocker.h"
40 #include "platform/image-decoders/ImageDecoder.h"
41 #include "platform/image-decoders/ImageFrame.h"
42 #include "platform/image-encoders/skia/PNGImageEncoder.h"
43 #include "third_party/skia/include/core/SkBitmapDevice.h"
44 #include "third_party/skia/include/core/SkImage.h"
45 #include "third_party/skia/include/core/SkPictureRecorder.h"
46 #include "third_party/skia/include/core/SkStream.h"
47 #include "wtf/HexNumber.h"
48 #include "wtf/text/Base64.h"
49 #include "wtf/text/TextEncoding.h"
51 namespace blink {
53 PictureSnapshot::PictureSnapshot(PassRefPtr<const SkPicture> picture)
54 : m_picture(picture)
58 static bool decodeBitmap(const void* data, size_t length, SkBitmap* result)
60 RefPtr<SharedBuffer> buffer = SharedBuffer::create(static_cast<const char*>(data), length);
61 OwnPtr<ImageDecoder> imageDecoder = ImageDecoder::create(*buffer, ImageDecoder::AlphaPremultiplied, ImageDecoder::GammaAndColorProfileIgnored);
62 if (!imageDecoder)
63 return false;
64 imageDecoder->setData(buffer.get(), true);
65 ImageFrame* frame = imageDecoder->frameBufferAtIndex(0);
66 if (!frame)
67 return true;
68 *result = frame->getSkBitmap();
69 return true;
72 PassRefPtr<PictureSnapshot> PictureSnapshot::load(const Vector<RefPtr<TilePictureStream>>& tiles)
74 ASSERT(!tiles.isEmpty());
75 Vector<RefPtr<SkPicture>> pictures;
76 pictures.reserveCapacity(tiles.size());
77 FloatRect unionRect;
78 for (const auto& tileStream : tiles) {
79 SkMemoryStream stream(tileStream->data.begin(), tileStream->data.size());
80 RefPtr<SkPicture> picture = adoptRef(SkPicture::CreateFromStream(&stream, decodeBitmap));
81 if (!picture)
82 return nullptr;
83 FloatRect cullRect(picture->cullRect());
84 cullRect.moveBy(tileStream->layerOffset);
85 unionRect.unite(cullRect);
86 pictures.append(picture);
88 if (tiles.size() == 1)
89 return adoptRef(new PictureSnapshot(pictures[0]));
90 SkPictureRecorder recorder;
91 SkCanvas* canvas = recorder.beginRecording(unionRect.width(), unionRect.height(), 0, 0);
92 for (size_t i = 0; i < pictures.size(); ++i) {
93 canvas->save();
94 canvas->translate(tiles[i]->layerOffset.x() - unionRect.x(), tiles[i]->layerOffset.y() - unionRect.y());
95 pictures[i]->playback(canvas, 0);
96 canvas->restore();
98 return adoptRef(new PictureSnapshot(adoptRef(recorder.endRecordingAsPicture())));
101 bool PictureSnapshot::isEmpty() const
103 return m_picture->cullRect().isEmpty();
106 PassOwnPtr<Vector<char>> PictureSnapshot::replay(unsigned fromStep, unsigned toStep, double scale) const
108 const SkIRect bounds = m_picture->cullRect().roundOut();
110 // TODO(fmalita): convert this to SkSurface/SkImage, drop the intermediate SkBitmap.
111 SkBitmap bitmap;
112 bitmap.allocPixels(SkImageInfo::MakeN32Premul(bounds.width(), bounds.height()));
113 bitmap.eraseARGB(0, 0, 0, 0);
115 ReplayingCanvas canvas(bitmap, fromStep, toStep);
116 canvas.scale(scale, scale);
117 canvas.resetStepCount();
118 m_picture->playback(&canvas, &canvas);
120 OwnPtr<Vector<char>> base64Data = adoptPtr(new Vector<char>());
121 Vector<char> encodedImage;
123 RefPtr<SkImage> image = adoptRef(SkImage::NewFromBitmap(bitmap));
124 if (!image)
125 return nullptr;
127 ImagePixelLocker pixelLocker(image, kUnpremul_SkAlphaType);
128 ImageDataBuffer imageData(IntSize(image->width(), image->height()),
129 static_cast<const unsigned char*>(pixelLocker.pixels()));
130 if (!PNGImageEncoder::encode(imageData, reinterpret_cast<Vector<unsigned char>*>(&encodedImage)))
131 return nullptr;
133 base64Encode(encodedImage, *base64Data);
134 return base64Data.release();
137 PassOwnPtr<PictureSnapshot::Timings> PictureSnapshot::profile(unsigned minRepeatCount, double minDuration, const FloatRect* clipRect) const
139 OwnPtr<PictureSnapshot::Timings> timings = adoptPtr(new PictureSnapshot::Timings());
140 timings->reserveCapacity(minRepeatCount);
141 const SkIRect bounds = m_picture->cullRect().roundOut();
142 SkBitmap bitmap;
143 bitmap.allocPixels(SkImageInfo::MakeN32Premul(bounds.width(), bounds.height()));
144 bitmap.eraseARGB(0, 0, 0, 0);
146 double now = WTF::monotonicallyIncreasingTime();
147 double stopTime = now + minDuration;
148 for (unsigned step = 0; step < minRepeatCount || now < stopTime; ++step) {
149 timings->append(Vector<double>());
150 Vector<double>* currentTimings = &timings->last();
151 if (timings->size() > 1)
152 currentTimings->reserveCapacity(timings->begin()->size());
153 ProfilingCanvas canvas(bitmap);
154 if (clipRect) {
155 canvas.clipRect(SkRect::MakeXYWH(clipRect->x(), clipRect->y(), clipRect->width(), clipRect->height()));
156 canvas.resetStepCount();
158 canvas.setTimings(currentTimings);
159 m_picture->playback(&canvas);
160 now = WTF::monotonicallyIncreasingTime();
162 return timings.release();
165 PassRefPtr<JSONArray> PictureSnapshot::snapshotCommandLog() const
167 const SkIRect bounds = m_picture->cullRect().roundOut();
168 LoggingCanvas canvas(bounds.width(), bounds.height());
169 m_picture->playback(&canvas);
170 return canvas.log();
173 } // namespace blink