Home | History | Annotate | Download | only in skia
      1 /*
      2  * Copyright (c) 2008, 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef NativeImageSkia_h
     32 #define NativeImageSkia_h
     33 
     34 #include "SkBitmap.h"
     35 #include "SkRect.h"
     36 #include "SkSize.h"
     37 #include "SkXfermode.h"
     38 #include "core/platform/graphics/GraphicsTypes.h"
     39 #include "wtf/Forward.h"
     40 #include "wtf/PassRefPtr.h"
     41 #include "wtf/RefCounted.h"
     42 
     43 class SkMatrix;
     44 class SkPaint;
     45 
     46 namespace WebCore {
     47 
     48 class FloatPoint;
     49 class FloatRect;
     50 class FloatSize;
     51 class GraphicsContext;
     52 
     53 // Used by computeResamplingMode to tell how bitmaps should be resampled.
     54 enum ResamplingMode {
     55     // Nearest neighbor resampling. Used when we detect that the page is
     56     // trying to make a pattern by stretching a small bitmap very large.
     57     NoResampling,
     58 
     59     // Default skia resampling. Used for large growing of images where high
     60     // quality resampling doesn't get us very much except a slowdown.
     61     LinearResampling,
     62 
     63     // High quality resampling.
     64     AwesomeResampling,
     65 };
     66 
     67 // This object is used as the "native image" in our port. When WebKit uses
     68 // PassNativeImagePtr / NativeImagePtr, it is a smart pointer to this type.
     69 // It has an SkBitmap, and also stores a cached resized image.
     70 class NativeImageSkia : public RefCounted<NativeImageSkia> {
     71 public:
     72     static PassRefPtr<NativeImageSkia> create()
     73     {
     74         return adoptRef(new NativeImageSkia());
     75     }
     76 
     77     // This factory method does a shallow copy of the passed-in SkBitmap
     78     // (ie., it references the same pixel data and bumps the refcount). Use
     79     // only when you want sharing semantics.
     80     static PassRefPtr<NativeImageSkia> create(const SkBitmap& bitmap, float resolutionScale = 1)
     81     {
     82         return adoptRef(new NativeImageSkia(bitmap, resolutionScale));
     83     }
     84 
     85     // This method does a shallow copy of the internal SkBitmap (ie., it
     86     // references the same pixel data and bumps the refcount). Use only when
     87     // you want sharing semantics.
     88     PassRefPtr<NativeImageSkia> clone() const
     89     {
     90         return adoptRef(new NativeImageSkia(m_image, m_resolutionScale, m_resizedImage, m_cachedImageInfo, m_resizeRequests));
     91     }
     92 
     93     ~NativeImageSkia();
     94 
     95     // Returns the number of bytes of image data. This includes the cached
     96     // resized version if there is one.
     97     int decodedSize() const;
     98 
     99     // Sets the immutable flag on the bitmap, indicating that the image data
    100     // will not be modified any further. This is called by the image decoder
    101     // when all data is complete, used by us to know whether we can cache
    102     // resized images, and used by Skia for various optimizations.
    103     void setDataComplete() { m_image.setImmutable(); }
    104 
    105     // Returns true if the entire image has been decoded.
    106     bool isDataComplete() const { return m_image.isImmutable(); }
    107 
    108     // Get reference to the internal SkBitmap representing this image.
    109     const SkBitmap& bitmap() const { return m_image; }
    110     SkBitmap& bitmap() { return m_image; }
    111 
    112     float resolutionScale() const { return m_resolutionScale; }
    113 
    114     // We can keep a resized version of the bitmap cached on this object.
    115     // This function will return true if there is a cached version of the given
    116     // scale and subset.
    117     bool hasResizedBitmap(const SkISize& scaledImageSize, const SkIRect& scaledImageSubset) const;
    118 
    119     // This will return an existing resized image subset, or generate a new one
    120     // of the specified size and subset and possibly cache it.
    121     //
    122     // scaledImageSize
    123     // Dimensions of the scaled full image.
    124     //
    125     // scaledImageSubset
    126     // Rectangle of the subset in the scaled image.
    127     SkBitmap resizedBitmap(const SkISize& scaledImageSize, const SkIRect& scaledImageSubset) const;
    128 
    129     void draw(GraphicsContext*, const SkRect& srcRect, const SkRect& destRect, SkXfermode::Mode) const;
    130     void drawPattern(
    131         GraphicsContext*,
    132         const FloatRect& srcRect,
    133         const FloatSize& scale,
    134         const FloatPoint& phase,
    135         CompositeOperator,
    136         const FloatRect& destRect,
    137         BlendMode) const;
    138 
    139 private:
    140     NativeImageSkia();
    141 
    142     NativeImageSkia(const SkBitmap&, float resolutionScale);
    143 
    144     // ImageResourceInfo is used to uniquely identify cached or requested image
    145     // resizes.
    146     // Image resize is identified by the scaled image size and scaled image subset.
    147     struct ImageResourceInfo {
    148         SkISize scaledImageSize;
    149         SkIRect scaledImageSubset;
    150 
    151         ImageResourceInfo();
    152 
    153         bool isEqual(const SkISize& otherScaledImageSize, const SkIRect& otherScaledImageSubset) const;
    154         void set(const SkISize& otherScaledImageSize, const SkIRect& otherScaledImageSubset);
    155         SkIRect rectInSubset(const SkIRect& otherScaledImageRect);
    156     };
    157 
    158     NativeImageSkia(const SkBitmap& image, float resolutionScale, const SkBitmap& resizedImage, const ImageResourceInfo&, int resizeRequests);
    159 
    160     // Returns true if the given resize operation should either resize the whole
    161     // image and cache it, or resize just the part it needs and throw the result
    162     // away.
    163     //
    164     // Calling this function may increment a request count that can change the
    165     // result of subsequent calls.
    166     //
    167     // On the one hand, if only a small subset is desired, then we will waste a
    168     // lot of time resampling the entire thing, so we only want to do exactly
    169     // what's required. On the other hand, resampling the entire bitmap is
    170     // better if we're going to be using it more than once (like a bitmap
    171     // scrolling on and off the screen. Since we only cache when doing the
    172     // entire thing, it's best to just do it up front.
    173     bool shouldCacheResampling(const SkISize& scaledImageSize, const SkIRect& scaledImageSubset) const;
    174 
    175     ResamplingMode computeResamplingMode(const SkMatrix&, float srcWidth, float srcHeight, float destWidth, float destHeight) const;
    176     SkBitmap extractScaledImageFragment(const SkRect& srcRect, float scaleX, float scaleY, SkRect* scaledSrcRect) const;
    177     void drawResampledBitmap(GraphicsContext*, SkPaint&, const SkRect& srcRect, const SkRect& destRect) const;
    178 
    179     // The original image.
    180     SkBitmap m_image;
    181     float m_resolutionScale;
    182 
    183     // The cached bitmap fragment. This is a subset of the scaled version of
    184     // |m_image|. empty() returns true if there is no cached image.
    185     mutable SkBitmap m_resizedImage;
    186 
    187     // References how many times that the image size has been requested for
    188     // the last size.
    189     //
    190     // Every time we get a call to shouldCacheResampling, if it matches the
    191     // m_cachedImageInfo, we'll increment the counter, and if not, we'll reset
    192     // the counter and save the dimensions.
    193     //
    194     // This allows us to see if many requests have been made for the same
    195     // resized image, we know that we should probably cache it, even if all of
    196     // those requests individually are small and would not otherwise be cached.
    197     //
    198     // We also track scaling information and destination subset for the scaled
    199     // image. See comments for ImageResourceInfo.
    200     mutable ImageResourceInfo m_cachedImageInfo;
    201     mutable int m_resizeRequests;
    202 };
    203 
    204 }
    205 #endif  // NativeImageSkia_h
    206