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 "IntSize.h"
     36 
     37 namespace WebCore {
     38 
     39 // This object is used as the "native image" in our port. When WebKit uses
     40 // "NativeImagePtr", it is a pointer to this type. It is an SkBitmap, but also
     41 // stores a cached resized image.
     42 class NativeImageSkia : public SkBitmap {
     43 public:
     44     NativeImageSkia();
     45     ~NativeImageSkia();
     46 
     47     // This constructor does a shallow copy of the passed-in SkBitmap (ie., it
     48     // references the same pixel data and bumps the refcount).  Use only when
     49     // you want sharing semantics.
     50     explicit NativeImageSkia(const SkBitmap&);
     51 
     52     // Returns the number of bytes of image data. This includes the cached
     53     // resized version if there is one.
     54     int decodedSize() const;
     55 
     56     // Sets the data complete flag. This is called by the image decoder when
     57     // all data is complete, and used by us to know whether we can cache
     58     // resized images.
     59     void setDataComplete() { m_isDataComplete = true; }
     60 
     61     // Returns true if the entire image has been decoded.
     62     bool isDataComplete() const { return m_isDataComplete; }
     63 
     64     // We can keep a resized version of the bitmap cached on this object.
     65     // This function will return true if there is a cached version of the
     66     // given image subset with the given dimensions.
     67     bool hasResizedBitmap(int width, int height) const;
     68 
     69     // This will return an existing resized image, or generate a new one of
     70     // the specified size and store it in the cache. Subsetted images can not
     71     // be cached unless the subset is the entire bitmap.
     72     SkBitmap resizedBitmap(int width, int height) const;
     73 
     74     // Returns true if the given resize operation should either resize the whole
     75     // image and cache it, or resize just the part it needs and throw the result
     76     // away.
     77     //
     78     // On the one hand, if only a small subset is desired, then we will waste a
     79     // lot of time resampling the entire thing, so we only want to do exactly
     80     // what's required. On the other hand, resampling the entire bitmap is
     81     // better if we're going to be using it more than once (like a bitmap
     82     // scrolling on and off the screen. Since we only cache when doing the
     83     // entire thing, it's best to just do it up front.
     84     bool shouldCacheResampling(int destWidth,
     85                                int destHeight,
     86                                int destSubsetWidth,
     87                                int destSubsetHeight) const;
     88 
     89 private:
     90     // Set to true when the data is complete. Before the entire image has
     91     // loaded, we do not want to cache a resize.
     92     bool m_isDataComplete;
     93 
     94     // The cached bitmap. This will be empty() if there is no cached image.
     95     mutable SkBitmap m_resizedImage;
     96 
     97     // References how many times that the image size has been requested for
     98     // the last size.
     99     //
    100     // Every time we get a request, if it matches the m_lastRequestSize, we'll
    101     // increment the counter, and if not, we'll reset the counter and save the
    102     // size.
    103     //
    104     // This allows us to see if many requests have been made for the same
    105     // resized image, we know that we should probably cache it, even if all of
    106     // those requests individually are small and would not otherwise be cached.
    107     mutable IntSize m_lastRequestSize;
    108     mutable int m_resizeRequests;
    109 };
    110 
    111 }
    112 #endif  // NativeImageSkia_h
    113 
    114