1/*
2 * Copyright 2012, 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#include "config.h"
27
28#include "DrawExtra.h"
29#include "GLExtras.h"
30#include "LayerAndroid.h"
31#include "SkCanvas.h"
32#include "SkRegion.h"
33#include "WebViewCore.h"
34
35RegionLayerDrawExtra::RegionLayerDrawExtra()
36    : m_highlightColor(COLOR_HOLO_LIGHT)
37{}
38
39RegionLayerDrawExtra::~RegionLayerDrawExtra()
40{
41    HighlightRegionMap::iterator end = m_highlightRegions.end();
42    for (HighlightRegionMap::iterator it = m_highlightRegions.begin(); it != end; ++it) {
43        delete it->second;
44        it->second = 0;
45    }
46}
47
48SkRegion* RegionLayerDrawExtra::getHighlightRegionsForLayer(const LayerAndroid* layer)
49{
50    int layerId = layer ? layer->uniqueId() : 0;
51    return m_highlightRegions.get(layerId);
52}
53
54void RegionLayerDrawExtra::addHighlightRegion(const LayerAndroid* layer, const Vector<IntRect>& rects,
55                                              const IntPoint& additionalOffset)
56{
57    if (rects.isEmpty())
58        return;
59    int layerId = layer ? layer->uniqueId() : 0;
60    SkRegion* region = m_highlightRegions.get(layerId);
61    if (!region) {
62        region = new SkRegion();
63        m_highlightRegions.set(layerId, region);
64    }
65    IntPoint offset = additionalOffset;
66    WebViewCore::layerToAbsoluteOffset(layer, offset);
67    for (size_t i = 0; i < rects.size(); i++) {
68        IntRect r = rects.at(i);
69        r.move(-offset.x(), -offset.y());
70        region->op(r.x(), r.y(), r.maxX(), r.maxY(), SkRegion::kUnion_Op);
71    }
72}
73
74void RegionLayerDrawExtra::draw(SkCanvas* canvas, LayerAndroid* layer)
75{
76    SkRegion* region = getHighlightRegionsForLayer(layer);
77    if (!region || region->isEmpty())
78        return;
79    SkRegion::Iterator rgnIter(*region);
80    SkPaint paint;
81    paint.setColor(m_highlightColor.rgb());
82    while (!rgnIter.done()) {
83        const SkIRect& rect = rgnIter.rect();
84        canvas->drawIRect(rect, paint);
85        rgnIter.next();
86    }
87}
88
89void RegionLayerDrawExtra::drawGL(GLExtras* glExtras, const LayerAndroid* layer)
90{
91    SkRegion* region = getHighlightRegionsForLayer(layer);
92    if (!region || region->isEmpty())
93        return;
94    const TransformationMatrix* transform = layer ? layer->drawTransform() : 0;
95    glExtras->drawRegion(*region, true, false, transform, m_highlightColor);
96}
97