Home | History | Annotate | Download | only in rendering
      1 /*
      2  * Copyright 2011, The Android Open Source Project
      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  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * 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 THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER 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 #ifndef ImageTexture_h
     27 #define ImageTexture_h
     28 
     29 #include "GLUtils.h"
     30 #include "SkBitmap.h"
     31 #include "SkBitmapRef.h"
     32 #include "SkPicture.h"
     33 #include "SkRefCnt.h"
     34 #include "TilePainter.h"
     35 
     36 namespace WebCore {
     37 
     38 class GLWebViewState;
     39 class LayerAndroid;
     40 class TexturesResult;
     41 class TileGrid;
     42 
     43 /////////////////////////////////////////////////////////////////////////////////
     44 // Image sharing codepath for layers
     45 /////////////////////////////////////////////////////////////////////////////////
     46 //
     47 // Layers containing only an image take a slightly different codepath;
     48 // GraphicsLayer::setContentsToImage() is called on the webcore thread,
     49 // passing an Image instance. We get the native image (an SkBitmap) and create
     50 // an ImageTexture instance with it (or increment the refcount of an existing
     51 // instance if the SkBitmap is similar to one already stored in ImagesManager,
     52 // i.e. if two GraphicsLayer share the same image).
     53 //
     54 // To detect if an image is similar, we compute and use a CRC. Each ImageTexture
     55 // is stored in ImagesManager using its CRC as a hash key.
     56 // Simply comparing the address is not enough -- different image could end up
     57 // at the same address (i.e. the image is deallocated then a new one is
     58 // reallocated at the old address)
     59 //
     60 // Each ImageTexture's CRC being unique, LayerAndroid instances simply store that
     61 // and retain/release the corresponding ImageTexture (so that
     62 // queued painting request will work correctly and not crash...).
     63 // LayerAndroid running on the UI thread will get the corresponding
     64 // ImageTexture at draw time.
     65 //
     66 // ImageTexture recopy the original SkBitmap so that they can safely be used
     67 // on a different thread; it uses TileGrid to allocate and paint the image,
     68 // so that we can share the same textures and limits as the rest of the layers.
     69 //
     70 /////////////////////////////////////////////////////////////////////////////////
     71 class ImageTexture : public TilePainter {
     72 public:
     73     ImageTexture(SkBitmap* bmp, unsigned crc);
     74     virtual ~ImageTexture();
     75 
     76     bool prepareGL(GLWebViewState*);
     77     void drawGL(LayerAndroid* layer, float opacity, FloatPoint* offset = 0);
     78     void drawCanvas(SkCanvas*, SkRect&);
     79     bool hasContentToShow();
     80     SkBitmap* bitmap() { return m_image; }
     81     unsigned imageCRC() { return m_crc; }
     82 
     83     static SkBitmap* convertBitmap(SkBitmap* bitmap);
     84 
     85     static unsigned computeCRC(const SkBitmap* bitmap);
     86     bool equalsCRC(unsigned crc);
     87 
     88     // methods used by TileGrid
     89     virtual bool paint(SkCanvas* canvas);
     90     virtual float opacity();
     91 
     92     int nbTextures();
     93 
     94     virtual SurfaceType type() { return TilePainter::Image; }
     95     unsigned int getImageTextureId();
     96 private:
     97     const TransformationMatrix* transform();
     98     void getImageToLayerScale(float* scaleW, float* scaleH) const;
     99 
    100     SkBitmapRef* m_imageRef;
    101     SkBitmap* m_image;
    102     TileGrid* m_tileGrid;
    103     LayerAndroid* m_layer;
    104     SkPicture* m_picture;
    105     TransformationMatrix m_layerMatrix;
    106     unsigned m_crc;
    107 };
    108 
    109 } // namespace WebCore
    110 
    111 #endif // ImageTexture
    112