summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/android/rendering/ImageTexture.h
blob: 53df6f954156b228e99f262de9dd474092105267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright 2011, The Android Open Source Project
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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.
 */

#ifndef ImageTexture_h
#define ImageTexture_h

#include "GLUtils.h"
#include "SkBitmap.h"
#include "SkBitmapRef.h"
#include "SkPicture.h"
#include "SkRefCnt.h"
#include "TilePainter.h"

namespace WebCore {

class GLWebViewState;
class LayerAndroid;
class TexturesResult;
class TileGrid;

/////////////////////////////////////////////////////////////////////////////////
// Image sharing codepath for layers
/////////////////////////////////////////////////////////////////////////////////
//
// Layers containing only an image take a slightly different codepath;
// GraphicsLayer::setContentsToImage() is called on the webcore thread,
// passing an Image instance. We get the native image (an SkBitmap) and create
// an ImageTexture instance with it (or increment the refcount of an existing
// instance if the SkBitmap is similar to one already stored in ImagesManager,
// i.e. if two GraphicsLayer share the same image).
//
// To detect if an image is similar, we compute and use a CRC. Each ImageTexture
// is stored in ImagesManager using its CRC as a hash key.
// Simply comparing the address is not enough -- different image could end up
// at the same address (i.e. the image is deallocated then a new one is
// reallocated at the old address)
//
// Each ImageTexture's CRC being unique, LayerAndroid instances simply store that
// and retain/release the corresponding ImageTexture (so that
// queued painting request will work correctly and not crash...).
// LayerAndroid running on the UI thread will get the corresponding
// ImageTexture at draw time.
//
// ImageTexture recopy the original SkBitmap so that they can safely be used
// on a different thread; it uses TileGrid to allocate and paint the image,
// so that we can share the same textures and limits as the rest of the layers.
//
/////////////////////////////////////////////////////////////////////////////////
class ImageTexture : public TilePainter {
public:
    ImageTexture(SkBitmap* bmp, unsigned crc);
    virtual ~ImageTexture();

    bool prepareGL(GLWebViewState*);
    void drawGL(LayerAndroid* layer, float opacity, FloatPoint* offset = 0);
    void drawCanvas(SkCanvas*, SkRect&);
    bool hasContentToShow();
    SkBitmap* bitmap() { return m_image; }
    unsigned imageCRC() { return m_crc; }

    static SkBitmap* convertBitmap(SkBitmap* bitmap);

    static unsigned computeCRC(const SkBitmap* bitmap);
    bool equalsCRC(unsigned crc);

    // methods used by TileGrid
    virtual bool paint(SkCanvas* canvas);
    virtual float opacity();

    int nbTextures();

    virtual SurfaceType type() { return TilePainter::Image; }
    unsigned int getImageTextureId();
private:
    const TransformationMatrix* transform();
    void getImageToLayerScale(float* scaleW, float* scaleH) const;

    SkBitmapRef* m_imageRef;
    SkBitmap* m_image;
    TileGrid* m_tileGrid;
    LayerAndroid* m_layer;
    SkPicture* m_picture;
    TransformationMatrix m_layerMatrix;
    unsigned m_crc;
};

} // namespace WebCore

#endif // ImageTexture