| OLD | NEW |
| 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 #ifndef ImageFrameGenerator_h | 26 #ifndef ImageFrameGenerator_h |
| 27 #define ImageFrameGenerator_h | 27 #define ImageFrameGenerator_h |
| 28 | 28 |
| 29 #include "platform/PlatformExport.h" | 29 #include "platform/PlatformExport.h" |
| 30 #include "platform/graphics/ThreadSafeDataTransport.h" | 30 #include "platform/image-decoders/SegmentReader.h" |
| 31 #include "third_party/skia/include/core/SkBitmap.h" | 31 #include "third_party/skia/include/core/SkBitmap.h" |
| 32 #include "third_party/skia/include/core/SkSize.h" | 32 #include "third_party/skia/include/core/SkSize.h" |
| 33 #include "third_party/skia/include/core/SkTypes.h" | 33 #include "third_party/skia/include/core/SkTypes.h" |
| 34 #include "third_party/skia/include/core/SkYUVSizeInfo.h" | 34 #include "third_party/skia/include/core/SkYUVSizeInfo.h" |
| 35 #include "wtf/Allocator.h" | 35 #include "wtf/Allocator.h" |
| 36 #include "wtf/Noncopyable.h" | 36 #include "wtf/Noncopyable.h" |
| 37 #include "wtf/PassOwnPtr.h" | 37 #include "wtf/PassOwnPtr.h" |
| 38 #include "wtf/PassRefPtr.h" | 38 #include "wtf/PassRefPtr.h" |
| 39 #include "wtf/RefCounted.h" | 39 #include "wtf/RefCounted.h" |
| 40 #include "wtf/RefPtr.h" | 40 #include "wtf/RefPtr.h" |
| 41 #include "wtf/ThreadSafeRefCounted.h" | 41 #include "wtf/ThreadSafeRefCounted.h" |
| 42 #include "wtf/ThreadingPrimitives.h" | 42 #include "wtf/ThreadingPrimitives.h" |
| 43 #include "wtf/Vector.h" | 43 #include "wtf/Vector.h" |
| 44 | 44 |
| 45 class SkData; | 45 class SkData; |
| 46 | 46 |
| 47 namespace blink { | 47 namespace blink { |
| 48 | 48 |
| 49 class ImageDecoder; | 49 class ImageDecoder; |
| 50 class SharedBuffer; | |
| 51 | 50 |
| 52 class PLATFORM_EXPORT ImageDecoderFactory { | 51 class PLATFORM_EXPORT ImageDecoderFactory { |
| 53 USING_FAST_MALLOC(ImageDecoderFactory); | 52 USING_FAST_MALLOC(ImageDecoderFactory); |
| 54 WTF_MAKE_NONCOPYABLE(ImageDecoderFactory); | 53 WTF_MAKE_NONCOPYABLE(ImageDecoderFactory); |
| 55 public: | 54 public: |
| 56 ImageDecoderFactory() {} | 55 ImageDecoderFactory() {} |
| 57 virtual ~ImageDecoderFactory() { } | 56 virtual ~ImageDecoderFactory() { } |
| 58 virtual PassOwnPtr<ImageDecoder> create() = 0; | 57 virtual PassOwnPtr<ImageDecoder> create() = 0; |
| 59 }; | 58 }; |
| 60 | 59 |
| 61 class PLATFORM_EXPORT ImageFrameGenerator final : public ThreadSafeRefCounted<Im
ageFrameGenerator> { | 60 class PLATFORM_EXPORT ImageFrameGenerator final : public ThreadSafeRefCounted<Im
ageFrameGenerator> { |
| 62 WTF_MAKE_NONCOPYABLE(ImageFrameGenerator); | 61 WTF_MAKE_NONCOPYABLE(ImageFrameGenerator); |
| 63 public: | 62 public: |
| 64 static PassRefPtr<ImageFrameGenerator> create(const SkISize& fullSize, PassR
efPtr<SharedBuffer> data, bool allDataReceived, bool isMultiFrame = false) | 63 static PassRefPtr<ImageFrameGenerator> create(const SkISize& fullSize, bool
isMultiFrame = false) |
| 65 { | 64 { |
| 66 return adoptRef(new ImageFrameGenerator(fullSize, data, allDataReceived,
isMultiFrame)); | 65 return adoptRef(new ImageFrameGenerator(fullSize, isMultiFrame)); |
| 67 } | 66 } |
| 68 | 67 |
| 69 ~ImageFrameGenerator(); | 68 ~ImageFrameGenerator(); |
| 70 | 69 |
| 71 void setData(PassRefPtr<SharedBuffer>, bool allDataReceived); | |
| 72 | |
| 73 // Return our encoded image data. Caller takes ownership and must unref the
data | |
| 74 // according to the contract SkImageGenerator::refEncodedData. Returns null
if | |
| 75 // the data is has not been fully received. | |
| 76 SkData* refEncodedData(); | |
| 77 | |
| 78 // Decodes and scales the specified frame at |index|. The dimensions and out
put | 70 // Decodes and scales the specified frame at |index|. The dimensions and out
put |
| 79 // format are given in SkImageInfo. Decoded pixels are written into |pixels|
with | 71 // format are given in SkImageInfo. Decoded pixels are written into |pixels|
with |
| 80 // a stride of |rowBytes|. Returns true if decoding was successful. | 72 // a stride of |rowBytes|. Returns true if decoding was successful. |
| 81 bool decodeAndScale(size_t index, const SkImageInfo&, void* pixels, size_t r
owBytes); | 73 bool decodeAndScale(SegmentReader*, bool allDataReceived, size_t index, cons
t SkImageInfo&, void* pixels, size_t rowBytes); |
| 82 | 74 |
| 83 // Decodes YUV components directly into the provided memory planes. | 75 // Decodes YUV components directly into the provided memory planes. |
| 84 bool decodeToYUV(size_t index, const SkISize componentSizes[3], void* planes
[3], const size_t rowBytes[3]); | 76 // Must not be called unless getYUVComponentSizes has been called and return
ed true. |
| 77 // YUV decoding does not currently support progressive decoding. In order to
support it, ImageDecoder needs something |
| 78 // analagous to its ImageFrame cache to hold partial planes, and the GPU cod
e needs to handle them. |
| 79 bool decodeToYUV(SegmentReader*, size_t index, const SkISize componentSizes[
3], void* planes[3], const size_t rowBytes[3]); |
| 85 | 80 |
| 86 const SkISize& getFullSize() const { return m_fullSize; } | 81 const SkISize& getFullSize() const { return m_fullSize; } |
| 87 | 82 |
| 88 bool isMultiFrame() const { return m_isMultiFrame; } | 83 bool isMultiFrame() const { return m_isMultiFrame; } |
| 89 bool decodeFailed() const { return m_decodeFailed; } | 84 bool decodeFailed() const { return m_decodeFailed; } |
| 90 | 85 |
| 91 bool hasAlpha(size_t index); | 86 bool hasAlpha(size_t index); |
| 92 | 87 |
| 93 bool getYUVComponentSizes(SkYUVSizeInfo*); | 88 // Must not be called unless the SkROBuffer has all the data. |
| 89 // YUV decoding does not currently support progressive decoding. See comment
above on decodeToYUV. |
| 90 bool getYUVComponentSizes(SegmentReader*, SkYUVSizeInfo*); |
| 94 | 91 |
| 95 private: | 92 private: |
| 96 ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<SharedBuffer>, bool
allDataReceived, bool isMultiFrame); | 93 ImageFrameGenerator(const SkISize& fullSize, bool isMultiFrame); |
| 97 | 94 |
| 98 friend class ImageFrameGeneratorTest; | 95 friend class ImageFrameGeneratorTest; |
| 99 friend class DeferredImageDecoderTest; | 96 friend class DeferredImageDecoderTest; |
| 100 // For testing. |factory| will overwrite the default ImageDecoder creation l
ogic if |factory->create()| returns non-zero. | 97 // For testing. |factory| will overwrite the default ImageDecoder creation l
ogic if |factory->create()| returns non-zero. |
| 101 void setImageDecoderFactory(PassOwnPtr<ImageDecoderFactory> factory) { m_ima
geDecoderFactory = std::move(factory); } | 98 void setImageDecoderFactory(PassOwnPtr<ImageDecoderFactory> factory) { m_ima
geDecoderFactory = std::move(factory); } |
| 102 | 99 |
| 103 void setHasAlpha(size_t index, bool hasAlpha); | 100 void setHasAlpha(size_t index, bool hasAlpha); |
| 104 | 101 |
| 105 // These methods are called while m_decodeMutex is locked. | 102 SkBitmap tryToResumeDecode(SegmentReader*, bool allDataReceived, size_t inde
x, const SkISize& scaledSize, SkBitmap::Allocator*); |
| 106 SkBitmap tryToResumeDecode(size_t index, const SkISize& scaledSize); | 103 // This method should only be called while m_decodeMutex is locked. |
| 107 bool decode(size_t index, ImageDecoder**, SkBitmap*); | 104 bool decode(SegmentReader*, bool allDataReceived, size_t index, ImageDecoder
**, SkBitmap*, SkBitmap::Allocator*); |
| 108 | 105 |
| 109 SkISize m_fullSize; | 106 const SkISize m_fullSize; |
| 110 | 107 |
| 111 // ThreadSafeDataTransport is referenced by this class and m_encodedData. | 108 const bool m_isMultiFrame; |
| 112 // In case that ImageFrameGenerator get's deleted before m_encodedData, | |
| 113 // m_encodedData would hold the reference to it (and underlying data). | |
| 114 RefPtr<ThreadSafeDataTransport> m_data; | |
| 115 | |
| 116 bool m_isMultiFrame; | |
| 117 bool m_decodeFailed; | 109 bool m_decodeFailed; |
| 118 bool m_yuvDecodingFailed; | 110 bool m_yuvDecodingFailed; |
| 119 size_t m_frameCount; | 111 size_t m_frameCount; |
| 120 Vector<bool> m_hasAlpha; | 112 Vector<bool> m_hasAlpha; |
| 121 | 113 |
| 122 class ExternalMemoryAllocator; | |
| 123 OwnPtr<ExternalMemoryAllocator> m_externalAllocator; | |
| 124 | |
| 125 OwnPtr<ImageDecoderFactory> m_imageDecoderFactory; | 114 OwnPtr<ImageDecoderFactory> m_imageDecoderFactory; |
| 126 | 115 |
| 127 // Prevents multiple decode operations on the same data. | 116 // Prevents multiple decode operations on the same data. |
| 128 Mutex m_decodeMutex; | 117 Mutex m_decodeMutex; |
| 129 | 118 |
| 130 // Protect concurrent access to m_hasAlpha. | 119 // Protect concurrent access to m_hasAlpha. |
| 131 Mutex m_alphaMutex; | 120 Mutex m_alphaMutex; |
| 132 | |
| 133 // Our encoded image data returned in refEncodedData. | |
| 134 SkData* m_encodedData; | |
| 135 | |
| 136 #if COMPILER(MSVC) | |
| 137 friend struct ::WTF::OwnedPtrDeleter<ExternalMemoryAllocator>; | |
| 138 #endif | |
| 139 }; | 121 }; |
| 140 | 122 |
| 141 } // namespace blink | 123 } // namespace blink |
| 142 | 124 |
| 143 #endif | 125 #endif |
| OLD | NEW |