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 |
| 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. |
| 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 24 */ |
| 25 |
| 26 //////////////////////////////////////////////////////////////////////////////// |
| 27 #include "config.h" |
| 28 |
| 29 #include "DecodingImageGenerator.h" |
| 30 |
| 31 #include "ImageDecodingStore.h" |
| 32 #include "ImageFrameGenerator.h" |
| 33 #include "SkData.h" |
| 34 #include "platform/Logging.h" |
| 35 #include "platform/TraceEvent.h" |
| 36 #include "skia/ext/image_operations.h" |
| 37 #include "third_party/skia/src/lazy/SkDiscardablePixelRef.h" |
| 38 |
| 39 //////////////////////////////////////////////////////////////////////////////// |
| 40 namespace WebCore { |
| 41 |
| 42 namespace { |
| 43 |
| 44 #ifndef NDEBUG |
| 45 inline bool operator==(const IntSize& lhs, const SkISize& rhs) |
| 46 { |
| 47 return (lhs.width() == rhs.width()) && (lhs.height() == rhs.height()); |
| 48 } |
| 49 #endif |
| 50 |
| 51 inline SkImageInfo sizeToImageInfo(const SkISize& size) |
| 52 { |
| 53 SkImageInfo info; |
| 54 info.fWidth = size.width(); |
| 55 info.fHeight = size.height(); |
| 56 info.fColorType = kPMColor_SkColorType; |
| 57 info.fAlphaType = kPremul_SkAlphaType; |
| 58 return info; |
| 59 } |
| 60 |
| 61 bool copyFrame(const SkBitmap& bitmap, void* pixels, size_t rowBytes) |
| 62 { |
| 63 SkAutoLockPixels autoLockPixels(bitmap); |
| 64 void* bitmapPixels = bitmap.getPixels(); |
| 65 size_t bitmapRowBytes = bitmap.rowBytes(); |
| 66 size_t bytesPerLine = bitmap.bytesPerPixel() * bitmap.width(); |
| 67 ASSERT(bytesPerLine <= rowBytes); |
| 68 for (int row = 0; row < bitmap.height(); ++row) { |
| 69 // This memcpy() is wasteful and bad. |
| 70 // More refactoring should remove it. |
| 71 memcpy(pixels, bitmapPixels, bytesPerLine); |
| 72 pixels = static_cast<char*>(pixels) + rowBytes; |
| 73 bitmapPixels = static_cast<char*>(bitmapPixels) + bitmapRowBytes; |
| 74 } |
| 75 return true; |
| 76 } |
| 77 |
| 78 } // namespace |
| 79 //////////////////////////////////////////////////////////////////////////////// |
| 80 |
| 81 SkData* DecodingImageGenerator::refEncodedData() |
| 82 { |
| 83 RefPtr<SharedBuffer> buffer = 0; |
| 84 bool allDataReceived = false; |
| 85 m_frameGenerator->copyData(&buffer, &allDataReceived); |
| 86 if (buffer && allDataReceived) { |
| 87 const void* data = static_cast<const void*>(buffer->data()); |
| 88 size_t size = buffer->size(); |
| 89 return SkData::NewWithCopy(data, size); |
| 90 } |
| 91 return 0; |
| 92 } |
| 93 |
| 94 bool DecodingImageGenerator::Install(PassRefPtr<ImageFrameGenerator> frameGenera
tor, size_t index, SkBitmap* dst) |
| 95 { |
| 96 SkASSERT(dst); |
| 97 DecodingImageGenerator* gen(SkNEW_ARGS(DecodingImageGenerator, (frameGenerat
or, index))); |
| 98 return SkDiscardablePixelRef::Install(gen, dst); |
| 99 } |
| 100 |
| 101 DecodingImageGenerator::DecodingImageGenerator(PassRefPtr<ImageFrameGenerator> f
rameGenerator, size_t index) |
| 102 : m_frameGenerator(frameGenerator) |
| 103 , m_frameIndex(index) |
| 104 { |
| 105 } |
| 106 |
| 107 DecodingImageGenerator::~DecodingImageGenerator() |
| 108 { |
| 109 } |
| 110 |
| 111 bool DecodingImageGenerator::getInfo(SkImageInfo* info) |
| 112 { |
| 113 ASSERT(info); |
| 114 SkISize size = m_frameGenerator->getFullSize(); |
| 115 if ((size.width() <= 0) || (size.height() <= 0)) { |
| 116 return false; |
| 117 } |
| 118 *info = sizeToImageInfo(size); |
| 119 return true; |
| 120 } |
| 121 |
| 122 bool DecodingImageGenerator::getPixels(const SkImageInfo& info, void* pixels, si
ze_t rowBytes) |
| 123 { |
| 124 if (info != sizeToImageInfo(m_frameGenerator->getFullSize())) { |
| 125 return false; |
| 126 } |
| 127 RefPtr<SharedBuffer> data(0); |
| 128 bool allDataReceived = false; |
| 129 |
| 130 m_frameGenerator->copyData(&data, &allDataReceived); |
| 131 |
| 132 ASSERT(data); |
| 133 |
| 134 OwnPtr<ImageDecoder> decoder( |
| 135 ImageDecoder::create(*data, ImageSource::AlphaPremultiplied, |
| 136 ImageSource::GammaAndColorProfileApplied)); |
| 137 if (!decoder.get()) { |
| 138 return false; |
| 139 } |
| 140 decoder->setData(data.get(), allDataReceived); |
| 141 |
| 142 if (!decoder->isSizeAvailable()) { |
| 143 return false; |
| 144 } |
| 145 ASSERT(decoder->frameSizeAtIndex(m_frameIndex) |
| 146 == m_frameGenerator->getFullSize()); |
| 147 |
| 148 ImageFrame* frame = 0; |
| 149 frame = decoder->frameBufferAtIndex(m_frameIndex); |
| 150 if (frame) { |
| 151 ASSERT(frame->getSkBitmap().width() == info.fWidth); |
| 152 ASSERT(frame->getSkBitmap().height() == info.fHeight); |
| 153 // FIXME: copyFrame is wasteful. |
| 154 copyFrame(frame->getSkBitmap(), pixels, rowBytes); |
| 155 // FIXME: find some way to convince the ImageFrameGenerator to |
| 156 // release the memory. |
| 157 } |
| 158 return frame; |
| 159 } |
| 160 } // namespace WebCore |
OLD | NEW |