OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/graphics/skia/ImagePixelLocker.h" | 5 #include "platform/graphics/skia/ImagePixelLocker.h" |
6 | 6 |
7 #include "third_party/skia/include/core/SkImage.h" | 7 #include "third_party/skia/include/core/SkImage.h" |
8 #include "third_party/skia/include/core/SkImageInfo.h" | 8 #include "third_party/skia/include/core/SkImageInfo.h" |
9 | 9 |
10 namespace blink { | 10 namespace blink { |
(...skipping 11 matching lines...) Expand all Loading... |
22 return info.alphaType() == alphaType || info.alphaType() == kOpaque_SkAlphaT
ype; | 22 return info.alphaType() == alphaType || info.alphaType() == kOpaque_SkAlphaT
ype; |
23 } | 23 } |
24 | 24 |
25 } // anonymous namespace | 25 } // anonymous namespace |
26 | 26 |
27 ImagePixelLocker::ImagePixelLocker(PassRefPtr<const SkImage> image, SkAlphaType
alphaType, | 27 ImagePixelLocker::ImagePixelLocker(PassRefPtr<const SkImage> image, SkAlphaType
alphaType, |
28 SkColorType colorType) | 28 SkColorType colorType) |
29 : m_image(image) | 29 : m_image(image) |
30 { | 30 { |
31 SkImageInfo info; | 31 SkImageInfo info; |
32 size_t imageRowBytes; | 32 size_t rowBytes; |
33 | 33 |
34 // If the image has in-RAM pixels and their format matches, use them directl
y. | 34 // If the image has in-RAM pixels and their format matches, use them directl
y. |
35 // TODO(fmalita): All current clients expect packed pixel rows. Maybe we co
uld update them | 35 // TODO(fmalita): All current clients expect packed pixel rows. Maybe we co
uld update them |
36 // to support arbitrary rowBytes, and relax the check below. | 36 // to support arbitrary rowBytes, and relax the check below. |
37 m_pixels = m_image->peekPixels(&info, &imageRowBytes); | 37 m_pixels = m_image->peekPixels(&info, &rowBytes); |
38 if (m_pixels | 38 if (m_pixels |
39 && infoIsCompatible(info, alphaType, colorType) | 39 && infoIsCompatible(info, alphaType, colorType) |
40 && imageRowBytes == info.minRowBytes()) { | 40 && rowBytes == info.minRowBytes()) { |
41 return; | 41 return; |
42 } | 42 } |
43 | 43 |
| 44 m_pixels = nullptr; |
| 45 |
44 // No luck, we need to read the pixels into our local buffer. | 46 // No luck, we need to read the pixels into our local buffer. |
45 info = SkImageInfo::Make(m_image->width(), m_image->height(), colorType, alp
haType); | 47 info = SkImageInfo::Make(m_image->width(), m_image->height(), colorType, alp
haType); |
46 if (!m_pixelStorage.tryAlloc(info) || !m_image->readPixels(m_pixelStorage, 0
, 0)) { | 48 rowBytes = info.minRowBytes(); |
47 m_pixels = nullptr; | 49 size_t size = info.getSafeSize(rowBytes); |
| 50 if (0 == size) |
48 return; | 51 return; |
49 } | |
50 | 52 |
51 m_pixels = m_pixelStorage.addr(); | 53 m_pixelStorage.reset(size); // this will throw on failure |
| 54 |
| 55 SkPixmap pixmap(info, m_pixelStorage.get(), rowBytes); |
| 56 |
| 57 if (!m_image->readPixels(pixmap, 0, 0)) |
| 58 return; |
| 59 |
| 60 m_pixels = m_pixelStorage.get(); |
52 } | 61 } |
53 | 62 |
54 } // namespace blink | 63 } // namespace blink |
OLD | NEW |