Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/ImageFrameGenerator.cpp

Issue 1484853003: Ganesh: images upload to GPU performance fix (skip copying encoded data) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comment #59 fix. Not using fast malloc as suggested during merge - attempt to fix memory leak intro… Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 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 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "config.h" 26 #include "config.h"
27 27
28 #include "platform/graphics/ImageFrameGenerator.h" 28 #include "platform/graphics/ImageFrameGenerator.h"
29 29
30 #include "SkData.h"
30 #include "platform/SharedBuffer.h" 31 #include "platform/SharedBuffer.h"
31 #include "platform/TraceEvent.h" 32 #include "platform/TraceEvent.h"
32 #include "platform/graphics/ImageDecodingStore.h" 33 #include "platform/graphics/ImageDecodingStore.h"
33 #include "platform/image-decoders/ImageDecoder.h" 34 #include "platform/image-decoders/ImageDecoder.h"
34 35
35 namespace blink { 36 namespace blink {
36 37
37 static bool compatibleInfo(const SkImageInfo& src, const SkImageInfo& dst) 38 static bool compatibleInfo(const SkImageInfo& src, const SkImageInfo& dst)
38 { 39 {
39 if (src == dst) 40 if (src == dst)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 componentSizes[0].set(size.width(), size.height()); 95 componentSizes[0].set(size.width(), size.height());
95 size = decoder->decodedYUVSize(1, sizeType); 96 size = decoder->decodedYUVSize(1, sizeType);
96 componentSizes[1].set(size.width(), size.height()); 97 componentSizes[1].set(size.width(), size.height());
97 size = decoder->decodedYUVSize(2, sizeType); 98 size = decoder->decodedYUVSize(2, sizeType);
98 componentSizes[2].set(size.width(), size.height()); 99 componentSizes[2].set(size.width(), size.height());
99 return true; 100 return true;
100 } 101 }
101 102
102 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha redBuffer> data, bool allDataReceived, bool isMultiFrame) 103 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha redBuffer> data, bool allDataReceived, bool isMultiFrame)
103 : m_fullSize(fullSize) 104 : m_fullSize(fullSize)
105 , m_data(adoptRef(new ThreadSafeDataTransport()))
104 , m_isMultiFrame(isMultiFrame) 106 , m_isMultiFrame(isMultiFrame)
105 , m_decodeFailedAndEmpty(false) 107 , m_decodeFailedAndEmpty(false)
106 , m_decodeCount(0) 108 , m_decodeCount(0)
107 , m_frameCount(0) 109 , m_frameCount(0)
108 { 110 {
109 setData(data.get(), allDataReceived); 111 setData(data.get(), allDataReceived);
110 } 112 }
111 113
112 ImageFrameGenerator::~ImageFrameGenerator() 114 ImageFrameGenerator::~ImageFrameGenerator()
113 { 115 {
114 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); 116 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this);
115 } 117 }
116 118
117 void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataRec eived) 119 void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataRec eived)
118 { 120 {
119 m_data.setData(data.get(), allDataReceived); 121 m_data->setData(data.get(), allDataReceived);
120 } 122 }
121 123
122 void ImageFrameGenerator::copyData(RefPtr<SharedBuffer>* data, bool* allDataRece ived) 124 static void sharedSkDataReleaseCallback(const void* address, void* context)
123 { 125 {
126 ThreadSafeDataTransport* dataTransport = static_cast<ThreadSafeDataTransport *>(context);
127 #if ENABLE(ASSERT)
128 ASSERT(dataTransport);
124 SharedBuffer* buffer = 0; 129 SharedBuffer* buffer = 0;
125 m_data.data(&buffer, allDataReceived); 130 bool allDataReceived = false;
126 if (buffer) 131 dataTransport->data(&buffer, &allDataReceived);
127 *data = buffer->copy(); 132 ASSERT(allDataReceived && buffer && buffer->data() == address);
133 #endif
134 // Deref m_data now.
135 dataTransport->deref();
136 }
137
138 SkData* ImageFrameGenerator::refEncodedData() const
139 {
140 // Every SkData instance and SharedBuffer need to keep buffer->data() refcou nted.
141 // Both SkData and m_data.m_readBuffer are having separate ref counting impl ementations.
142 //
143 // SkData's SkData::NewWithProc is designed with similar use case in mind,
144 // unmapping wrapped shmem data once all SkData instances are disposed.
145 // in this case, sharedBufSkDataReleaseProc would just need to deref (since m_data->m_readBuffer
146 // might still hold a reference) and that is happening in sharedBufSkDataRel easeProc.
147 //
148 // Preventing m_data disposal if ImageFrameGenerator is thread safe - SkData could get disposed
149 // in any thread, and SkData would still need to hold the ref to SharedBuffe r if ImageFrameGenerator
150 // is deleted. This is why full m_data (ThreadSafeRefCounted) is passed to c lient - to hold the
151 // reference to SharedBuffer (accompanying returned SkData). By contract in Skia's SkData,
152 // client needs to call SkData unref that would end up in sharedBufSkDataRel easeProc
153 // releasing the ref to ThreadSafeDataTransport (in thread safe way) and und erlying SharedBuffer.
154 //
155 // A note about allDataReceived:
156 // SkData is returned only for full image (encoded) data downloaded. This is important since
157 // DeferredImageDecoder::setData is called only once with allDataReceived an d after that
158 // m_data->m_readBuffer.data() is not changed. See also RELEASE_ASSERT used in
159 // ThreadSafeDataTransport::data().
160 SharedBuffer* buffer = 0;
161 bool allDataReceived = false;
162 m_data->data(&buffer, &allDataReceived);
163 if (!allDataReceived)
164 return nullptr;
165
166 // While SkData is holding reference to underlying data, prevent disposing m _data and its content.
167 m_data->ref();
168 return SkData::NewWithProc(buffer->data(), buffer->size(), sharedSkDataRelea seCallback, m_data.get());
128 } 169 }
129 170
130 bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes) 171 bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes)
131 { 172 {
132 // This method is called to populate a discardable memory owned by Skia. 173 // This method is called to populate a discardable memory owned by Skia.
133 174
134 // Prevents concurrent decode or scale operations on the same image data. 175 // Prevents concurrent decode or scale operations on the same image data.
135 MutexLocker lock(m_decodeMutex); 176 MutexLocker lock(m_decodeMutex);
136 177
137 // This implementation does not support scaling so check the requested size. 178 // This implementation does not support scaling so check the requested size.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 218
178 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeToYUV", "generator", this, "decodeCount", static_cast<int>(m_decodeCount)); 219 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeToYUV", "generator", this, "decodeCount", static_cast<int>(m_decodeCount));
179 220
180 if (!planes || !planes[0] || !planes[1] || !planes[2] 221 if (!planes || !planes[0] || !planes[1] || !planes[2]
181 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { 222 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) {
182 return false; 223 return false;
183 } 224 }
184 225
185 SharedBuffer* data = 0; 226 SharedBuffer* data = 0;
186 bool allDataReceived = false; 227 bool allDataReceived = false;
187 m_data.data(&data, &allDataReceived); 228 m_data->data(&data, &allDataReceived);
188 229
189 // FIXME: YUV decoding does not currently support progressive decoding. 230 // FIXME: YUV decoding does not currently support progressive decoding.
190 ASSERT(allDataReceived); 231 ASSERT(allDataReceived);
191 232
192 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); 233 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied);
193 if (!decoder) 234 if (!decoder)
194 return false; 235 return false;
195 236
196 decoder->setData(data, allDataReceived); 237 decoder->setData(data, allDataReceived);
197 238
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 } 324 }
284 325
285 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap) 326 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap)
286 { 327 {
287 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); 328 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height());
288 329
289 ASSERT(decoder); 330 ASSERT(decoder);
290 SharedBuffer* data = 0; 331 SharedBuffer* data = 0;
291 bool allDataReceived = false; 332 bool allDataReceived = false;
292 bool newDecoder = false; 333 bool newDecoder = false;
293 m_data.data(&data, &allDataReceived); 334 m_data->data(&data, &allDataReceived);
294 335
295 // Try to create an ImageDecoder if we are not given one. 336 // Try to create an ImageDecoder if we are not given one.
296 if (!*decoder) { 337 if (!*decoder) {
297 newDecoder = true; 338 newDecoder = true;
298 if (m_imageDecoderFactory) 339 if (m_imageDecoderFactory)
299 *decoder = m_imageDecoderFactory->create().leakPtr(); 340 *decoder = m_imageDecoderFactory->create().leakPtr();
300 341
301 if (!*decoder) 342 if (!*decoder)
302 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); 343 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr();
303 344
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 } 392 }
352 393
353 bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3]) 394 bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3])
354 { 395 {
355 ASSERT(componentSizes); 396 ASSERT(componentSizes);
356 397
357 TRACE_EVENT2("webkit", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height()); 398 TRACE_EVENT2("webkit", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height());
358 399
359 SharedBuffer* data = 0; 400 SharedBuffer* data = 0;
360 bool allDataReceived = false; 401 bool allDataReceived = false;
361 m_data.data(&data, &allDataReceived); 402 m_data->data(&data, &allDataReceived);
362 403
363 // FIXME: YUV decoding does not currently support progressive decoding. 404 // FIXME: YUV decoding does not currently support progressive decoding.
364 if (!allDataReceived) 405 if (!allDataReceived)
365 return false; 406 return false;
366 407
367 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); 408 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied);
368 if (!decoder) 409 if (!decoder)
369 return false; 410 return false;
370 411
371 // JPEG images support YUV decoding: other decoders do not. So don't pump da ta into decoders 412 // JPEG images support YUV decoding: other decoders do not. So don't pump da ta into decoders
372 // that always return false to updateYUVComponentSizes() requests. 413 // that always return false to updateYUVComponentSizes() requests.
373 if (decoder->filenameExtension() != "jpg") 414 if (decoder->filenameExtension() != "jpg")
374 return false; 415 return false;
375 416
376 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding. 417 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding.
377 decoder->setData(data, allDataReceived); 418 decoder->setData(data, allDataReceived);
378 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); 419 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes);
379 decoder->setImagePlanes(dummyImagePlanes.release()); 420 decoder->setImagePlanes(dummyImagePlanes.release());
380 421
381 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder:: SizeForMemoryAllocation); 422 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder:: SizeForMemoryAllocation);
382 } 423 }
383 424
384 } // namespace blink 425 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698