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 */ | |
30 | |
31 #include "sky/engine/config.h" | |
32 #include "sky/engine/platform/graphics/GraphicsContextRecorder.h" | |
33 | |
34 #include "platform/image-decoders/ImageDecoder.h" | |
35 #include "platform/image-decoders/ImageFrame.h" | |
36 #include "platform/image-encoders/skia/PNGImageEncoder.h" | |
37 #include "sky/engine/platform/graphics/ImageBuffer.h" | |
38 #include "sky/engine/platform/graphics/ImageSource.h" | |
39 #include "sky/engine/platform/graphics/LoggingCanvas.h" | |
40 #include "sky/engine/platform/graphics/ProfilingCanvas.h" | |
41 #include "sky/engine/platform/graphics/ReplayingCanvas.h" | |
42 #include "sky/engine/wtf/HexNumber.h" | |
43 #include "sky/engine/wtf/text/Base64.h" | |
44 #include "sky/engine/wtf/text/TextEncoding.h" | |
45 #include "third_party/skia/include/core/SkBitmapDevice.h" | |
46 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
47 #include "third_party/skia/include/core/SkStream.h" | |
48 | |
49 namespace blink { | |
50 | |
51 GraphicsContext* GraphicsContextRecorder::record(const IntSize& size, bool isCer
tainlyOpaque) | |
52 { | |
53 ASSERT(!m_picture); | |
54 ASSERT(!m_recorder); | |
55 ASSERT(!m_context); | |
56 m_isCertainlyOpaque = isCertainlyOpaque; | |
57 m_recorder = adoptPtr(new SkPictureRecorder); | |
58 SkCanvas* canvas = m_recorder->beginRecording(size.width(), size.height(), 0
, 0); | |
59 m_context = adoptPtr(new GraphicsContext(canvas)); | |
60 m_context->setRegionTrackingMode(isCertainlyOpaque ? GraphicsContext::Region
TrackingOpaque : GraphicsContext::RegionTrackingDisabled); | |
61 m_context->setCertainlyOpaque(isCertainlyOpaque); | |
62 return m_context.get(); | |
63 } | |
64 | |
65 PassRefPtr<GraphicsContextSnapshot> GraphicsContextRecorder::stop() | |
66 { | |
67 m_context.clear(); | |
68 m_picture = adoptRef(m_recorder->endRecording()); | |
69 m_recorder.clear(); | |
70 return adoptRef(new GraphicsContextSnapshot(m_picture.release())); | |
71 } | |
72 | |
73 GraphicsContextSnapshot::GraphicsContextSnapshot(PassRefPtr<SkPicture> picture) | |
74 : m_picture(picture) | |
75 { | |
76 } | |
77 | |
78 static bool decodeBitmap(const void* data, size_t length, SkBitmap* result) | |
79 { | |
80 RefPtr<SharedBuffer> buffer = SharedBuffer::create(static_cast<const char*>(
data), length); | |
81 OwnPtr<ImageDecoder> imageDecoder = ImageDecoder::create(*buffer, ImageSourc
e::AlphaPremultiplied, ImageSource::GammaAndColorProfileIgnored); | |
82 if (!imageDecoder) | |
83 return false; | |
84 imageDecoder->setData(buffer.get(), true); | |
85 ImageFrame* frame = imageDecoder->frameBufferAtIndex(0); | |
86 if (!frame) | |
87 return true; | |
88 *result = frame->getSkBitmap(); | |
89 return true; | |
90 } | |
91 | |
92 PassRefPtr<GraphicsContextSnapshot> GraphicsContextSnapshot::load(const char* da
ta, size_t size) | |
93 { | |
94 SkMemoryStream stream(data, size); | |
95 RefPtr<SkPicture> picture = adoptRef(SkPicture::CreateFromStream(&stream, de
codeBitmap)); | |
96 if (!picture) | |
97 return nullptr; | |
98 return adoptRef(new GraphicsContextSnapshot(picture)); | |
99 } | |
100 | |
101 PassOwnPtr<Vector<char> > GraphicsContextSnapshot::replay(unsigned fromStep, uns
igned toStep, double scale) const | |
102 { | |
103 const SkIRect bounds = m_picture->cullRect().roundOut(); | |
104 SkBitmap bitmap; | |
105 bitmap.allocPixels(SkImageInfo::MakeN32Premul(bounds.width(), bounds.height(
))); | |
106 { | |
107 ReplayingCanvas canvas(bitmap, fromStep, toStep); | |
108 canvas.scale(scale, scale); | |
109 canvas.resetStepCount(); | |
110 m_picture->playback(&canvas, &canvas); | |
111 } | |
112 OwnPtr<Vector<char> > base64Data = adoptPtr(new Vector<char>()); | |
113 Vector<char> encodedImage; | |
114 if (!PNGImageEncoder::encode(bitmap, reinterpret_cast<Vector<unsigned char>*
>(&encodedImage))) | |
115 return nullptr; | |
116 base64Encode(encodedImage, *base64Data); | |
117 return base64Data.release(); | |
118 } | |
119 | |
120 PassOwnPtr<GraphicsContextSnapshot::Timings> GraphicsContextSnapshot::profile(un
signed minRepeatCount, double minDuration) const | |
121 { | |
122 OwnPtr<GraphicsContextSnapshot::Timings> timings = adoptPtr(new GraphicsCont
extSnapshot::Timings()); | |
123 timings->reserveCapacity(minRepeatCount); | |
124 const SkIRect bounds = m_picture->cullRect().roundOut(); | |
125 SkBitmap bitmap; | |
126 bitmap.allocPixels(SkImageInfo::MakeN32Premul(bounds.width(), bounds.height(
))); | |
127 OwnPtr<ProfilingCanvas> canvas = adoptPtr(new ProfilingCanvas(bitmap)); | |
128 | |
129 double now = WTF::monotonicallyIncreasingTime(); | |
130 double stopTime = now + minDuration; | |
131 for (unsigned step = 0; step < minRepeatCount || now < stopTime; ++step) { | |
132 timings->append(Vector<double>()); | |
133 Vector<double>* currentTimings = &timings->last(); | |
134 if (timings->size() > 1) | |
135 currentTimings->reserveCapacity(timings->begin()->size()); | |
136 if (step) | |
137 canvas = adoptPtr(new ProfilingCanvas(bitmap)); | |
138 canvas->setTimings(currentTimings); | |
139 m_picture->playback(canvas.get()); | |
140 now = WTF::monotonicallyIncreasingTime(); | |
141 } | |
142 return timings.release(); | |
143 } | |
144 | |
145 PassRefPtr<JSONArray> GraphicsContextSnapshot::snapshotCommandLog() const | |
146 { | |
147 const SkIRect bounds = m_picture->cullRect().roundOut(); | |
148 LoggingCanvas canvas(bounds.width(), bounds.height()); | |
149 m_picture->playback(&canvas); | |
150 return canvas.log(); | |
151 } | |
152 | |
153 } // namespace blink | |
OLD | NEW |