Home | History | Annotate | Download | only in nav
      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 
     35 RegionLayerDrawExtra::RegionLayerDrawExtra()
     36     : m_highlightColor(COLOR_HOLO_LIGHT)
     37 {}
     38 
     39 RegionLayerDrawExtra::~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 
     48 SkRegion* RegionLayerDrawExtra::getHighlightRegionsForLayer(const LayerAndroid* layer)
     49 {
     50     int layerId = layer ? layer->uniqueId() : 0;
     51     return m_highlightRegions.get(layerId);
     52 }
     53 
     54 void RegionLayerDrawExtra::addHighlightRegion(const LayerAndroid* layer, const Vector<IntRect>& rects,
     55                                               const IntPoint& additionalOffset,
     56                                               const IntRect& clipRect)
     57 {
     58     if (rects.isEmpty())
     59         return;
     60     int layerId = layer ? layer->uniqueId() : 0;
     61     SkRegion* region = m_highlightRegions.get(layerId);
     62     if (!region) {
     63         region = new SkRegion();
     64         m_highlightRegions.set(layerId, region);
     65     }
     66     IntPoint offset = additionalOffset;
     67     WebViewCore::layerToAbsoluteOffset(layer, offset);
     68     for (size_t i = 0; i < rects.size(); i++) {
     69         IntRect r = rects.at(i);
     70         if (!clipRect.isEmpty()) {
     71             r.intersect(clipRect);
     72             if (r.isEmpty())
     73                 continue; // don't add it to the region
     74         }
     75         r.move(-offset.x(), -offset.y());
     76         region->op(r.x(), r.y(), r.maxX(), r.maxY(), SkRegion::kUnion_Op);
     77     }
     78 }
     79 
     80 void RegionLayerDrawExtra::draw(SkCanvas* canvas, LayerAndroid* layer)
     81 {
     82     SkRegion* region = getHighlightRegionsForLayer(layer);
     83     if (!region || region->isEmpty())
     84         return;
     85     SkRegion::Iterator rgnIter(*region);
     86     SkPaint paint;
     87     paint.setColor(m_highlightColor.rgb());
     88     while (!rgnIter.done()) {
     89         const SkIRect& rect = rgnIter.rect();
     90         canvas->drawIRect(rect, paint);
     91         rgnIter.next();
     92     }
     93 }
     94 
     95 void RegionLayerDrawExtra::drawGL(GLExtras* glExtras, const LayerAndroid* layer)
     96 {
     97     SkRegion* region = getHighlightRegionsForLayer(layer);
     98     if (!region || region->isEmpty())
     99         return;
    100     const TransformationMatrix* transform = layer ? layer->drawTransform() : 0;
    101     glExtras->drawRegion(*region, true, false, transform, m_highlightColor);
    102 }
    103