summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/android/rendering/ImageTexture.cpp
blob: db03753a772b2433e75290f05db99da6416f3fad (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * 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.
 */

#define LOG_TAG "ImageTexture"
#define LOG_NDEBUG 1

#include "config.h"
#include "ImageTexture.h"

#include "AndroidLog.h"
#include "ClassTracker.h"
#include "ImagesManager.h"
#include "LayerAndroid.h"
#include "SkDevice.h"
#include "SkPicture.h"
#include "TileGrid.h"
#include "TilesManager.h"

namespace WebCore {

// CRC computation adapted from Tools/DumpRenderTree/CyclicRedundancyCheck.cpp
static void makeCrcTable(unsigned crcTable[256])
{
    for (unsigned i = 0; i < 256; i++) {
        unsigned c = i;
        for (int k = 0; k < 8; k++) {
            if (c & 1)
                c = -306674912 ^ ((c >> 1) & 0x7fffffff);
            else
                c = c >> 1;
        }
        crcTable[i] = c;
    }
}

unsigned computeCrc(uint8_t* buffer, size_t size)
{
    static unsigned crcTable[256];
    static bool crcTableComputed = false;
    if (!crcTableComputed) {
        makeCrcTable(crcTable);
        crcTableComputed = true;
    }

    unsigned crc = 0xffffffffL;
    for (size_t i = 0; i < size; ++i)
        crc = crcTable[(crc ^ buffer[i]) & 0xff] ^ ((crc >> 8) & 0x00ffffffL);
    return crc ^ 0xffffffffL;
}

ImageTexture::ImageTexture(SkBitmap* bmp, unsigned crc)
    : m_image(bmp)
    , m_tileGrid(0)
    , m_layer(0)
    , m_picture(0)
    , m_crc(crc)
{
#ifdef DEBUG_COUNT
    ClassTracker::instance()->increment("ImageTexture");
#endif
    if (!m_image)
        return;

    // NOTE: This constructor is called on the webcore thread

    // Create a picture containing the image (needed for TileGrid)
    m_picture = new SkPicture();
    SkCanvas* pcanvas = m_picture->beginRecording(m_image->width(), m_image->height());
    pcanvas->clear(SkColorSetARGBInline(0, 0, 0, 0));
    pcanvas->drawBitmap(*m_image, 0, 0);
    m_picture->endRecording();
}

ImageTexture::~ImageTexture()
{
#ifdef DEBUG_COUNT
    ClassTracker::instance()->decrement("ImageTexture");
#endif
    delete m_image;
    delete m_tileGrid;
    SkSafeUnref(m_picture);
    ImagesManager::instance()->onImageTextureDestroy(m_crc);
}

SkBitmap* ImageTexture::convertBitmap(SkBitmap* bitmap)
{
    SkBitmap* img = new SkBitmap();
    int w = bitmap->width();
    int h = bitmap->height();

    // Create a copy of the image
    img->setConfig(SkBitmap::kARGB_8888_Config, w, h);
    img->allocPixels();
    SkDevice* device = new SkDevice(*img);
    SkCanvas canvas;
    canvas.setDevice(device);
    device->unref();
    SkRect dest;
    dest.set(0, 0, w, h);
    img->setIsOpaque(false);
    img->eraseARGB(0, 0, 0, 0);
    canvas.drawBitmapRect(*bitmap, 0, dest);

    return img;
}

unsigned ImageTexture::computeCRC(const SkBitmap* bitmap)
{
    if (!bitmap)
        return 0;
    bitmap->lockPixels();
    uint8_t* img = static_cast<uint8_t*>(bitmap->getPixels());
    unsigned crc = 0;
    if (img)
        crc = computeCrc(img, bitmap->getSize());
    bitmap->unlockPixels();
    return crc;
}

bool ImageTexture::equalsCRC(unsigned crc)
{
    return m_crc == crc;
}

// Return 0 if the image does not meet the repeatable criteria.
unsigned int ImageTexture::getImageTextureId()
{
    return m_tileGrid ? m_tileGrid->getImageTextureId() : 0;
}

int ImageTexture::nbTextures()
{
    if (!hasContentToShow())
        return 0;
    if (!m_tileGrid)
        return 0;

    // TODO: take in account the visible clip (need to maintain
    // a list of the clients layer, etc.)
    IntRect visibleContentArea(0, 0, m_image->width(), m_image->height());
    int nbTextures = m_tileGrid->nbTextures(visibleContentArea, 1.0);
    ALOGV("ImageTexture %p, %d x %d needs %d textures",
          this, m_image->width(), m_image->height(),
          nbTextures);
    return nbTextures;
}

bool ImageTexture::hasContentToShow()
{
    // Don't display 1x1 image -- no need to allocate a full texture for this
    if (!m_image)
        return false;
    if (m_image->width() == 1 && m_image->height() == 1)
        return false;
    return true;
}

bool ImageTexture::prepareGL(GLWebViewState* state)
{
    if (!hasContentToShow())
        return false;

    if (!m_tileGrid && m_picture) {
        bool isBaseSurface = false;
        m_tileGrid = new TileGrid(isBaseSurface);
        SkRegion region;
        region.setRect(0, 0, m_image->width(), m_image->height());
        m_tileGrid->markAsDirty(region);
    }

    if (!m_tileGrid)
        return false;

    IntRect fullContentArea(0, 0, m_image->width(), m_image->height());
    m_tileGrid->prepareGL(state, 1.0, fullContentArea, fullContentArea, this);
    if (m_tileGrid->isReady()) {
        m_tileGrid->swapTiles();
        return false;
    }
    return true;
}

const TransformationMatrix* ImageTexture::transform()
{
    if (!m_layer)
        return 0;

    TransformationMatrix d = *(m_layer->drawTransform());
    TransformationMatrix m;
    float scaleW = 1.0f;
    float scaleH = 1.0f;
    getImageToLayerScale(&scaleW, &scaleH);

    m.scaleNonUniform(scaleW, scaleH);
    m_layerMatrix = d.multiply(m);
    return &m_layerMatrix;
}

float ImageTexture::opacity()
{
    if (!m_layer)
        return 1.0;
    return m_layer->drawOpacity();
}

bool ImageTexture::paint(SkCanvas* canvas)
{
    if (!m_picture) {
        ALOGV("IT %p COULDNT PAINT, NO PICTURE", this);
        return false;
    }

    ALOGV("IT %p painting with picture %p", this, m_picture);
    canvas->drawPicture(*m_picture);

    return true;
}

void ImageTexture::getImageToLayerScale(float* scaleW, float* scaleH) const
{
    if (!scaleW || !scaleH)
        return;


    IntRect layerArea = m_layer->fullContentArea();

    if (layerArea.width() == 0 || layerArea.height() == 0)
        return;

    // calculate X, Y scale difference between image pixel coordinates and layer
    // content coordinates

    *scaleW = static_cast<float>(layerArea.width()) / static_cast<float>(m_image->width());
    *scaleH = static_cast<float>(layerArea.height()) / static_cast<float>(m_image->height());
}

void ImageTexture::drawGL(LayerAndroid* layer,
                         float opacity, FloatPoint* offset)
{
    if (!layer)
        return;
    if (!hasContentToShow())
        return;

    // TileGrid::draw() will call us back to know the
    // transform and opacity, so we need to set m_layer
    m_layer = layer;
    if (m_tileGrid) {
        bool force3dContentVisible = true;
        IntRect visibleContentArea = m_layer->visibleContentArea(force3dContentVisible);

        // transform visibleContentArea size to image size
        float scaleW = 1.0f;
        float scaleH = 1.0f;
        getImageToLayerScale(&scaleW, &scaleH);
        visibleContentArea.setX(visibleContentArea.x() / scaleW);
        visibleContentArea.setWidth(visibleContentArea.width() / scaleW);
        visibleContentArea.setY(visibleContentArea.y() / scaleH);
        visibleContentArea.setHeight(visibleContentArea.height() / scaleH);

        const TransformationMatrix* transformation = transform();
        if (offset)
            m_layerMatrix.translate(offset->x(), offset->y());
        m_tileGrid->drawGL(visibleContentArea, opacity, transformation);
    }
    m_layer = 0;
}

void ImageTexture::drawCanvas(SkCanvas* canvas, SkRect& rect)
{
    if (canvas && m_image)
        canvas->drawBitmapRect(*m_image, 0, rect);
}

} // namespace WebCore