diff options
-rw-r--r-- | webkit/api/public/WebImage.h | 87 | ||||
-rw-r--r-- | webkit/api/src/WebImageCG.cpp | 92 | ||||
-rw-r--r-- | webkit/api/src/WebImageSkia.cpp | 72 | ||||
-rw-r--r-- | webkit/glue/image_decoder.cc | 65 | ||||
-rw-r--r-- | webkit/glue/webclipboard_impl.cc | 17 | ||||
-rw-r--r-- | webkit/webkit.gyp | 10 |
6 files changed, 189 insertions, 154 deletions
diff --git a/webkit/api/public/WebImage.h b/webkit/api/public/WebImage.h index 5dd4d52..cef557b 100644 --- a/webkit/api/public/WebImage.h +++ b/webkit/api/public/WebImage.h @@ -1,10 +1,10 @@ /* * Copyright (C) 2009 Google Inc. All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: - * + * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above @@ -14,7 +14,7 @@ * * Neither the name of Google Inc. nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR @@ -32,24 +32,24 @@ #define WebImage_h #include "WebCommon.h" -#include "WebSize.h" #if WEBKIT_USING_SKIA -class SkBitmap; +#include <SkBitmap.h> +#elif WEBKIT_USING_CG +typedef struct CGImage* CGImageRef; #endif namespace WebKit { - - class WebImagePixels; - class WebImagePrivate; + class WebData; + struct WebSize; // A container for an ARGB bitmap. class WebImage { public: ~WebImage() { reset(); } - WebImage() : m_private(0) { } - WebImage(const WebImage& image) : m_private(0) { assign(image); } + WebImage() { init(); } + WebImage(const WebImage& image) { init(); assign(image); } WebImage& operator=(const WebImage& image) { @@ -57,70 +57,51 @@ namespace WebKit { return *this; } + // Decodes the given image data. If the image has multiple frames, + // then the frame whose size is desiredSize is returned. Otherwise, + // the first frame is returned. + WEBKIT_API static WebImage fromData(const WebData&, const WebSize& desiredSize); + WEBKIT_API void reset(); - WEBKIT_API WebSize size() const; + WEBKIT_API void assign(const WebImage&); - WebImagePixels pixels() const; - bool isNull() const { return m_private == 0; } + WEBKIT_API bool isNull() const; + WEBKIT_API WebSize size() const; #if WEBKIT_USING_SKIA - WebImage(const SkBitmap& bitmap) : m_private(0) - { - assign(bitmap); - } + WebImage(const SkBitmap& bitmap) : m_bitmap(bitmap) { } WebImage& operator=(const SkBitmap& bitmap) { - assign(bitmap); + m_bitmap = bitmap; return *this; } - WEBKIT_API operator SkBitmap() const; -#endif + SkBitmap& getSkBitmap() { return m_bitmap; } + const SkBitmap& getSkBitmap() const { return m_bitmap; } private: - friend class WebImagePixels; - - WEBKIT_API void assign(const WebImage&); - WEBKIT_API const void* lockPixels(); - WEBKIT_API void unlockPixels(); + void init() { } + SkBitmap m_bitmap; -#if WEBKIT_USING_SKIA - WEBKIT_API void assign(const SkBitmap&); -#endif +#elif WEBKIT_USING_CG + WebImage(CGImageRef imageRef) { init(); assign(imageRef); } - WebImagePrivate* m_private; - }; - - class WebImagePixels { - public: - WebImagePixels(WebImage* image) : m_image(image) - { - m_data = m_image->lockPixels(); - } - - WebImagePixels(const WebImagePixels& other) : m_image(other.m_image) + WebImage& operator=(CGImageRef imageRef) { - m_data = m_image->lockPixels(); + assign(imageRef); + return *this; } - ~WebImagePixels() { m_image->unlockPixels(); } - - const void* get() const { return m_data; } - operator const void*() const { return m_data; } + CGImageRef getCGImageRef() const { return m_imageRef; } private: - WebImagePixels& operator=(const WebImagePixels&); - - WebImage* m_image; - const void* m_data; + void init() { m_imageRef = 0; } + void assign(CGImageRef); + CGImageRef m_imageRef; +#endif }; - inline WebImagePixels WebImage::pixels() const - { - return WebImagePixels(const_cast<WebImage*>(this)); - } - } // namespace WebKit #endif diff --git a/webkit/api/src/WebImageCG.cpp b/webkit/api/src/WebImageCG.cpp new file mode 100644 index 0000000..3cf1bad --- /dev/null +++ b/webkit/api/src/WebImageCG.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "WebImage.h" + +#include <CoreGraphics/CGImage.h> + +#include "WebData.h" +#include "WebSize.h" + +#include "ImageSource.h" +#include "SharedBuffer.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RetainPtr.h> + +using namespace WebCore; + +namespace WebKit { + +WebImage WebImage::fromData(const WebData& data, const WebSize& desiredSize) +{ + // FIXME: ImageSource does not support picking a matching frame. We'll + // likely need to enumerate the frames ourselves. + + ImageSource source; + source.setData(PassRefPtr<SharedBuffer>(data).get(), true); + if (!source.isSizeAvailable()) + return WebImage(); + + RetainPtr<CGImageRef> frame0(AdoptCF, source.createFrameAtIndex(0)); + if (!frame0) + return WebImage(); + + return WebImage(frame0.get()); +} + +void WebImage::reset() +{ + CGImageRelease(m_imageRef); + m_imageRef = 0; +} + +void WebImage::assign(const WebImage& image) +{ + assign(image.m_imageRef); +} + +bool WebImage::isNull() const +{ + return m_imageRef != 0; +} + +WebSize WebImage::size() const +{ + return WebSize(CGImageGetWidth(m_imageRef), CGImageGetHeight(m_imageRef)); +} + +void WebImage::assign(CGImageRef imageRef) +{ + CGImageRelease(m_imageRef); + CGImageRetain(m_imageRef = imageRef); +} + +} // namespace WebKit diff --git a/webkit/api/src/WebImageSkia.cpp b/webkit/api/src/WebImageSkia.cpp index 12c8d178..5519166 100644 --- a/webkit/api/src/WebImageSkia.cpp +++ b/webkit/api/src/WebImageSkia.cpp @@ -1,10 +1,10 @@ /* * Copyright (C) 2009 Google Inc. All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: - * + * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above @@ -14,7 +14,7 @@ * * Neither the name of Google Inc. nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR @@ -31,65 +31,51 @@ #include "config.h" #include "WebImage.h" -#include <SkBitmap.h> +#include "WebData.h" +#include "WebSize.h" -namespace WebKit { +#include "ImageSourceSkia.h" +#include "NativeImageSkia.h" +#include "SharedBuffer.h" +#include <wtf/OwnPtr.h> +#include <wtf/PassRefPtr.h> -class WebImagePrivate : public SkBitmap { -public: - WebImagePrivate(const SkBitmap& bitmap) : SkBitmap(bitmap) { } -}; +using namespace WebCore; -void WebImage::reset() -{ - delete m_private; - m_private = 0; -} +namespace WebKit { -WebSize WebImage::size() const +WebImage WebImage::fromData(const WebData& data, const WebSize& desiredSize) { - if (!m_private) - return WebSize(); + ImageSourceSkia source; + source.setData(PassRefPtr<SharedBuffer>(data).get(), true, desiredSize); + if (!source.isSizeAvailable()) + return WebImage(); - return WebSize(m_private->width(), m_private->height()); -} + OwnPtr<NativeImageSkia> frame0(source.createFrameAtIndex(0)); + if (!frame0.get()) + return WebImage(); -void WebImage::assign(const WebImage& image) -{ - if (m_private) - delete m_private; - - if (image.m_private) - m_private = new WebImagePrivate(*image.m_private); - else - m_private = 0; + return WebImage(*frame0); } -WebImage::operator SkBitmap() const +void WebImage::reset() { - if (!m_private) - return SkBitmap(); - - return *m_private; + m_bitmap.reset(); } -void WebImage::assign(const SkBitmap& bitmap) +void WebImage::assign(const WebImage& image) { - if (m_private) - delete m_private; - - m_private = new WebImagePrivate(bitmap); + m_bitmap = image.m_bitmap; } -const void* WebImage::lockPixels() +bool WebImage::isNull() const { - m_private->lockPixels(); - return static_cast<void*>(m_private->getPixels()); + return m_bitmap.isNull(); } -void WebImage::unlockPixels() +WebSize WebImage::size() const { - m_private->unlockPixels(); + return WebSize(m_bitmap.width(), m_bitmap.height()); } } // namespace WebKit diff --git a/webkit/glue/image_decoder.cc b/webkit/glue/image_decoder.cc index 73c3f9b..2a7084b 100644 --- a/webkit/glue/image_decoder.cc +++ b/webkit/glue/image_decoder.cc @@ -5,22 +5,17 @@ #include "config.h" #include "webkit/glue/image_decoder.h" -#include "base/compiler_specific.h" +#include "webkit/api/public/WebData.h" +#include "webkit/api/public/WebImage.h" +#include "webkit/api/public/WebSize.h" #include "third_party/skia/include/core/SkBitmap.h" -MSVC_PUSH_WARNING_LEVEL(0); -#if defined(OS_WIN) || defined(OS_LINUX) -#include "ImageSourceSkia.h" -#include "NativeImageSkia.h" -#elif defined(OS_MACOSX) -#include "ImageSource.h" -#include "RetainPtr.h" +#if WEBKIT_USING_CG #include "skia/ext/skia_utils_mac.h" #endif -#include "IntSize.h" -#include "RefPtr.h" -#include "SharedBuffer.h" -MSVC_POP_WARNING(); + +using WebKit::WebData; +using WebKit::WebImage; namespace webkit_glue { @@ -35,46 +30,12 @@ ImageDecoder::~ImageDecoder() { } SkBitmap ImageDecoder::Decode(const unsigned char* data, size_t size) const { - - // What's going on here? ImageDecoder is only used by ImageResourceFetcher, - // which is only used (but extensively) by WebViewImpl. On the Mac we're using - // CoreGraphics, but right now WebViewImpl uses SkBitmaps everywhere. For now, - // this is a convenient bottleneck to convert from CGImageRefs to SkBitmaps, - // but in the future we will need to replumb to get CGImageRefs (or whatever - // the native type is) everywhere, directly. - -#if defined(OS_WIN) || defined(OS_LINUX) - WebCore::ImageSourceSkia source; -#elif defined(OS_MACOSX) - WebCore::ImageSource source; -#endif - WTF::RefPtr<WebCore::SharedBuffer> buffer(WebCore::SharedBuffer::create( - data, static_cast<int>(size))); -#if defined(OS_WIN) || defined(OS_LINUX) - source.setData(buffer.get(), true, - WebCore::IntSize(desired_icon_size_.width(), - desired_icon_size_.height())); -#elif defined(OS_MACOSX) - source.setData(buffer.get(), true); -#endif - - if (!source.isSizeAvailable()) - return SkBitmap(); - - WebCore::NativeImagePtr frame0 = source.createFrameAtIndex(0); - if (!frame0) - return SkBitmap(); - -#if defined(OS_WIN) || defined(OS_LINUX) - SkBitmap retval = *reinterpret_cast<SkBitmap*>(frame0); - delete frame0; - return retval; -#elif defined(OS_MACOSX) - // TODO(port): should we delete frame0 like Linux/Windows do above? - // BitmapImage releases automatically, but we're bypassing it so we'll need - // to do the releasing. - RetainPtr<CGImageRef> image(AdoptCF, frame0); - return gfx::CGImageToSkBitmap(image.get()); + const WebImage& image = WebImage::fromData( + WebData(reinterpret_cast<const char*>(data), size), desired_icon_size_); +#if WEBKIT_USING_SKIA + return image.getSkBitmap(); +#elif WEBKIT_USING_CG + return gfx::CGImageToSkBitmap(image.getCGImageRef()); #endif } diff --git a/webkit/glue/webclipboard_impl.cc b/webkit/glue/webclipboard_impl.cc index dd96dca..e2be557 100644 --- a/webkit/glue/webclipboard_impl.cc +++ b/webkit/glue/webclipboard_impl.cc @@ -9,12 +9,18 @@ #include "base/string_util.h" #include "googleurl/src/gurl.h" #include "net/base/escape.h" +#include "third_party/skia/include/core/SkBitmap.h" #include "webkit/api/public/WebImage.h" +#include "webkit/api/public/WebSize.h" #include "webkit/api/public/WebString.h" #include "webkit/api/public/WebURL.h" #include "webkit/glue/scoped_clipboard_writer_glue.h" #include "webkit/glue/webkit_glue.h" +#if WEBKIT_USING_CG +#include "skia/ext/skia_utils_mac.h" +#endif + using WebKit::WebClipboard; using WebKit::WebImage; using WebKit::WebString; @@ -121,10 +127,15 @@ void WebClipboardImpl::writeImage( const WebImage& image, const WebURL& url, const WebString& title) { ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); -#if defined(OS_WIN) || defined(OS_LINUX) - if (!image.isNull()) - scw.WriteBitmapFromPixels(image.pixels(), image.size()); + if (!image.isNull()) { +#if WEBKIT_USING_SKIA + const SkBitmap& bitmap = image.getSkBitmap(); +#elif WEBKIT_USING_CG + const SkBitmap& bitmap = gfx::CGImageToSkBitmap(image.getCGImageRef()); #endif + SkAutoLockPixels locked(bitmap); + scw.WriteBitmapFromPixels(bitmap.getPixels(), image.size()); + } if (!url.isEmpty()) { scw.WriteBookmark(title, url.spec()); diff --git a/webkit/webkit.gyp b/webkit/webkit.gyp index f611da5..be7658d 100644 --- a/webkit/webkit.gyp +++ b/webkit/webkit.gyp @@ -4272,6 +4272,7 @@ 'api/src/WebForm.cpp', 'api/src/WebHistoryItem.cpp', 'api/src/WebHTTPBody.cpp', + 'api/src/WebImageCG.cpp', 'api/src/WebImageSkia.cpp', 'api/src/WebInputEvent.cpp', 'api/src/WebKit.cpp', @@ -4312,11 +4313,14 @@ 'include_dirs': [ 'api/public/mac', ], - 'sources!': [ - 'api/src/WebImageSkia.cpp', + 'sources/': [ + ['exclude', 'Skia\\.cpp$'], ], }, { # else: OS!="mac" - 'sources/': [['exclude', '/mac/']], + 'sources/': [ + ['exclude', '/mac/'], + ['exclude', 'CG\\.cpp$'], + ], }], ['OS=="win"', { 'include_dirs': [ |