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

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

Issue 2326473005: Use sk_sp<SkBitmap::Allocator> instead of raw pointers or WTF::RefPtr<T>. (Closed)
Patch Set: Created 4 years, 3 months 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
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); 117 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this);
118 } 118 }
119 119
120 bool ImageFrameGenerator::decodeAndScale(SegmentReader* data, bool allDataReceiv ed, size_t index, const SkImageInfo& info, void* pixels, size_t rowBytes) 120 bool ImageFrameGenerator::decodeAndScale(SegmentReader* data, bool allDataReceiv ed, size_t index, const SkImageInfo& info, void* pixels, size_t rowBytes)
121 { 121 {
122 if (m_decodeFailed) 122 if (m_decodeFailed)
123 return false; 123 return false;
124 124
125 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", static_cast<int>(index)); 125 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", static_cast<int>(index));
126 126
127 RefPtr<ExternalMemoryAllocator> externalAllocator = adoptRef(new ExternalMem oryAllocator(info, pixels, rowBytes)); 127 sk_sp<ExternalMemoryAllocator> externalAllocator(new ExternalMemoryAllocator (info, pixels, rowBytes));
128 128
129 // This implementation does not support scaling so check the requested size. 129 // This implementation does not support scaling so check the requested size.
130 SkISize scaledSize = SkISize::Make(info.width(), info.height()); 130 SkISize scaledSize = SkISize::Make(info.width(), info.height());
131 ASSERT(m_fullSize == scaledSize); 131 ASSERT(m_fullSize == scaledSize);
132 132
133 // TODO (scroggo): Convert tryToResumeDecode() and decode() to take a 133 SkBitmap bitmap = tryToResumeDecode(data, allDataReceived, index, scaledSize , std::move(externalAllocator));
134 // sk_sp<SkBitmap::Allocator> instead of a bare pointer.
135 SkBitmap bitmap = tryToResumeDecode(data, allDataReceived, index, scaledSize , externalAllocator.get());
136 if (bitmap.isNull()) 134 if (bitmap.isNull())
137 return false; 135 return false;
138 136
139 // Check to see if the decoder has written directly to the pixel memory 137 // Check to see if the decoder has written directly to the pixel memory
140 // provided. If not, make a copy. 138 // provided. If not, make a copy.
141 ASSERT(bitmap.width() == scaledSize.width()); 139 ASSERT(bitmap.width() == scaledSize.width());
142 ASSERT(bitmap.height() == scaledSize.height()); 140 ASSERT(bitmap.height() == scaledSize.height());
143 SkAutoLockPixels bitmapLock(bitmap); 141 SkAutoLockPixels bitmapLock(bitmap);
144 if (bitmap.getPixels() != pixels) 142 if (bitmap.getPixels() != pixels)
145 return bitmap.copyPixelsTo(pixels, rowBytes * info.height(), rowBytes); 143 return bitmap.copyPixelsTo(pixels, rowBytes * info.height(), rowBytes);
(...skipping 27 matching lines...) Expand all
173 if (decoder->decodeToYUV()) { 171 if (decoder->decodeToYUV()) {
174 setHasAlpha(0, false); // YUV is always opaque 172 setHasAlpha(0, false); // YUV is always opaque
175 return true; 173 return true;
176 } 174 }
177 175
178 ASSERT(decoder->failed()); 176 ASSERT(decoder->failed());
179 m_yuvDecodingFailed = true; 177 m_yuvDecodingFailed = true;
180 return false; 178 return false;
181 } 179 }
182 180
183 SkBitmap ImageFrameGenerator::tryToResumeDecode(SegmentReader* data, bool allDat aReceived, size_t index, const SkISize& scaledSize, SkBitmap::Allocator* allocat or) 181 SkBitmap ImageFrameGenerator::tryToResumeDecode(SegmentReader* data, bool allDat aReceived, size_t index, const SkISize& scaledSize, sk_sp<SkBitmap::Allocator> a llocator)
184 { 182 {
185 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index ", static_cast<int>(index)); 183 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index ", static_cast<int>(index));
186 184
187 ImageDecoder* decoder = 0; 185 ImageDecoder* decoder = 0;
188 186
189 // Lock the mutex, so only one thread can use the decoder at once. 187 // Lock the mutex, so only one thread can use the decoder at once.
190 MutexLocker lock(m_decodeMutex); 188 MutexLocker lock(m_decodeMutex);
191 const bool resumeDecoding = ImageDecodingStore::instance().lockDecoder(this, m_fullSize, &decoder); 189 const bool resumeDecoding = ImageDecodingStore::instance().lockDecoder(this, m_fullSize, &decoder);
192 ASSERT(!resumeDecoding || decoder); 190 ASSERT(!resumeDecoding || decoder);
193 191
194 SkBitmap fullSizeImage; 192 SkBitmap fullSizeImage;
195 bool complete = decode(data, allDataReceived, index, &decoder, &fullSizeImag e, allocator); 193 bool complete = decode(data, allDataReceived, index, &decoder, &fullSizeImag e, std::move(allocator));
196 194
197 if (!decoder) 195 if (!decoder)
198 return SkBitmap(); 196 return SkBitmap();
199 197
200 // If we are not resuming decoding that means the decoder is freshly 198 // If we are not resuming decoding that means the decoder is freshly
201 // created and we have ownership. If we are resuming decoding then 199 // created and we have ownership. If we are resuming decoding then
202 // the decoder is owned by ImageDecodingStore. 200 // the decoder is owned by ImageDecodingStore.
203 std::unique_ptr<ImageDecoder> decoderContainer; 201 std::unique_ptr<ImageDecoder> decoderContainer;
204 if (!resumeDecoding) 202 if (!resumeDecoding)
205 decoderContainer = wrapUnique(decoder); 203 decoderContainer = wrapUnique(decoder);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 MutexLocker lock(m_alphaMutex); 243 MutexLocker lock(m_alphaMutex);
246 if (index >= m_hasAlpha.size()) { 244 if (index >= m_hasAlpha.size()) {
247 const size_t oldSize = m_hasAlpha.size(); 245 const size_t oldSize = m_hasAlpha.size();
248 m_hasAlpha.resize(index + 1); 246 m_hasAlpha.resize(index + 1);
249 for (size_t i = oldSize; i < m_hasAlpha.size(); ++i) 247 for (size_t i = oldSize; i < m_hasAlpha.size(); ++i)
250 m_hasAlpha[i] = true; 248 m_hasAlpha[i] = true;
251 } 249 }
252 m_hasAlpha[index] = hasAlpha; 250 m_hasAlpha[index] = hasAlpha;
253 } 251 }
254 252
255 bool ImageFrameGenerator::decode(SegmentReader* data, bool allDataReceived, size _t index, ImageDecoder** decoder, SkBitmap* bitmap, SkBitmap::Allocator* allocat or) 253 bool ImageFrameGenerator::decode(SegmentReader* data, bool allDataReceived, size _t index, ImageDecoder** decoder, SkBitmap* bitmap, sk_sp<SkBitmap::Allocator> a llocator)
256 { 254 {
257 ASSERT(m_decodeMutex.locked()); 255 ASSERT(m_decodeMutex.locked());
258 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); 256 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height());
259 257
260 // Try to create an ImageDecoder if we are not given one. 258 // Try to create an ImageDecoder if we are not given one.
261 ASSERT(decoder); 259 ASSERT(decoder);
262 bool newDecoder = false; 260 bool newDecoder = false;
263 bool shouldCallSetData = true; 261 bool shouldCallSetData = true;
264 if (!*decoder) { 262 if (!*decoder) {
265 newDecoder = true; 263 newDecoder = true;
266 if (m_imageDecoderFactory) 264 if (m_imageDecoderFactory)
267 *decoder = m_imageDecoderFactory->create().release(); 265 *decoder = m_imageDecoderFactory->create().release();
268 266
269 if (!*decoder) { 267 if (!*decoder) {
270 *decoder = ImageDecoder::create(data, allDataReceived, ImageDecoder: :AlphaPremultiplied, ImageDecoder::GammaAndColorProfileApplied).release(); 268 *decoder = ImageDecoder::create(data, allDataReceived, ImageDecoder: :AlphaPremultiplied, ImageDecoder::GammaAndColorProfileApplied).release();
271 // The newly created decoder just grabbed the data. No need to rese t it. 269 // The newly created decoder just grabbed the data. No need to rese t it.
272 shouldCallSetData = false; 270 shouldCallSetData = false;
273 } 271 }
274 272
275 if (!*decoder) 273 if (!*decoder)
276 return false; 274 return false;
277 } 275 }
278 276
279 if (!m_isMultiFrame && newDecoder && allDataReceived) { 277 if (!m_isMultiFrame && newDecoder && allDataReceived) {
280 // If we're using an external memory allocator that means we're decoding 278 // If we're using an external memory allocator that means we're decoding
281 // directly into the output memory and we can save one memcpy. 279 // directly into the output memory and we can save one memcpy.
282 ASSERT(allocator); 280 ASSERT(allocator);
283 (*decoder)->setMemoryAllocator(allocator); 281 (*decoder)->setMemoryAllocator(std::move(allocator));
Łukasz Anforowicz 2016/09/09 20:10:04 |allocator| comes from |decodeAndScale|, which wil
scroggo_chromium 2016/09/09 20:51:04 It's safe (but arguably not safe from future chang
Łukasz Anforowicz 2016/09/12 20:50:09 My first priority is to eradicate RefPtr<SkFoo> (b
scroggo_chromium 2016/09/13 14:54:42 Another note: it would *not* be safe if we did not
Łukasz Anforowicz 2016/09/13 18:17:31 Thanks for pointing this out - I agree that using
284 } 282 }
285 283
286 if (shouldCallSetData) 284 if (shouldCallSetData)
287 (*decoder)->setData(data, allDataReceived); 285 (*decoder)->setData(data, allDataReceived);
288 ImageFrame* frame = (*decoder)->frameBufferAtIndex(index); 286 ImageFrame* frame = (*decoder)->frameBufferAtIndex(index);
289 287
290 // For multi-frame image decoders, we need to know how many frames are 288 // For multi-frame image decoders, we need to know how many frames are
291 // in that image in order to release the decoder when all frames are 289 // in that image in order to release the decoder when all frames are
292 // decoded. frameCount() is reliable only if all data is received and set in 290 // decoded. frameCount() is reliable only if all data is received and set in
293 // decoder, particularly with GIF. 291 // decoder, particularly with GIF.
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 return false; 335 return false;
338 336
339 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding. 337 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding.
340 std::unique_ptr<ImagePlanes> dummyImagePlanes = wrapUnique(new ImagePlanes); 338 std::unique_ptr<ImagePlanes> dummyImagePlanes = wrapUnique(new ImagePlanes);
341 decoder->setImagePlanes(std::move(dummyImagePlanes)); 339 decoder->setImagePlanes(std::move(dummyImagePlanes));
342 340
343 return updateYUVComponentSizes(decoder.get(), sizeInfo->fSizes, sizeInfo->fW idthBytes); 341 return updateYUVComponentSizes(decoder.get(), sizeInfo->fSizes, sizeInfo->fW idthBytes);
344 } 342 }
345 343
346 } // namespace blink 344 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698