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#define LOG_TAG "RasterRenderer"
27#define LOG_NDEBUG 1
28
29#include "config.h"
30#include "RasterRenderer.h"
31
32#if USE(ACCELERATED_COMPOSITING)
33
34#include "AndroidLog.h"
35#include "GLUtils.h"
36#include "SkBitmap.h"
37#include "SkBitmapRef.h"
38#include "SkCanvas.h"
39#include "SkDevice.h"
40#include "Tile.h"
41#include "TilesManager.h"
42
43namespace WebCore {
44
45RasterRenderer::RasterRenderer()
46  : BaseRenderer(BaseRenderer::Raster)
47  , m_bitmapIsPureColor(false)
48{
49    m_bitmap.setConfig(SkBitmap::kARGB_8888_Config,
50                       TilesManager::instance()->tileWidth(),
51                       TilesManager::instance()->tileHeight());
52    m_bitmap.allocPixels();
53#ifdef DEBUG_COUNT
54    ClassTracker::instance()->increment("RasterRenderer");
55#endif
56}
57
58RasterRenderer::~RasterRenderer()
59{
60#ifdef DEBUG_COUNT
61    ClassTracker::instance()->decrement("RasterRenderer");
62#endif
63}
64
65void RasterRenderer::setupCanvas(const TileRenderInfo& renderInfo, SkCanvas* canvas)
66{
67    TRACE_METHOD();
68
69    if (renderInfo.baseTile->isLayerTile()) {
70        m_bitmap.setIsOpaque(false);
71
72        // clear bitmap if necessary
73        if (!m_bitmapIsPureColor || m_bitmapPureColor != Color::transparent)
74            m_bitmap.eraseARGB(0, 0, 0, 0);
75    } else {
76        Color defaultBackground = Color::white;
77        Color* background = renderInfo.tilePainter->background();
78        if (!background) {
79            ALOGV("No background color for base layer!");
80            background = &defaultBackground;
81        }
82        ALOGV("setupCanvas use background on Base Layer %x", background->rgb());
83        m_bitmap.setIsOpaque(!background->hasAlpha());
84
85        // fill background color if necessary
86        if (!m_bitmapIsPureColor || m_bitmapPureColor != *background)
87            m_bitmap.eraseARGB(background->alpha(), background->red(),
88                               background->green(), background->blue());
89    }
90
91    SkDevice* device = new SkDevice(m_bitmap);
92
93    canvas->setDevice(device);
94
95    device->unref();
96}
97
98void RasterRenderer::renderingComplete(const TileRenderInfo& renderInfo, SkCanvas* canvas)
99{
100    // We may swap the content of m_bitmap with the bitmap in the transfer queue.
101    GLUtils::paintTextureWithBitmap(&renderInfo, m_bitmap);
102}
103
104void RasterRenderer::deviceCheckForPureColor(TileRenderInfo& renderInfo, SkCanvas* canvas)
105{
106    if (!renderInfo.isPureColor) {
107        // base renderer may have already determined isPureColor, so only do the
108        // brute force check if needed
109        renderInfo.isPureColor = GLUtils::isPureColorBitmap(m_bitmap, renderInfo.pureColor);
110    }
111
112    m_bitmapIsPureColor = renderInfo.isPureColor;
113    m_bitmapPureColor = renderInfo.pureColor;
114}
115
116} // namespace WebCore
117
118#endif // USE(ACCELERATED_COMPOSITING)
119