Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef LayerAndroid_h
     18 #define LayerAndroid_h
     19 
     20 #if USE(ACCELERATED_COMPOSITING)
     21 
     22 #include "RefPtr.h"
     23 #include "SkColor.h"
     24 #include "SkLayer.h"
     25 #include "StringHash.h"
     26 #include <wtf/HashMap.h>
     27 
     28 class SkCanvas;
     29 class SkMatrix;
     30 class SkPicture;
     31 
     32 namespace android {
     33 class DrawExtra;
     34 }
     35 
     36 using namespace android;
     37 
     38 struct SkLength {
     39     enum SkLengthType { Undefined, Auto, Relative, Percent, Fixed, Static, Intrinsic, MinIntrinsic };
     40     SkLengthType type;
     41     SkScalar value;
     42     SkLength() {
     43         type = Undefined;
     44         value = 0;
     45     }
     46     bool defined() const {
     47         if (type == Undefined)
     48             return false;
     49         return true;
     50     }
     51     float calcFloatValue(float max) const {
     52         switch (type) {
     53             case Percent:
     54                 return (max * value) / 100.0f;
     55             case Fixed:
     56                 return value;
     57             default:
     58                 return value;
     59         }
     60     }
     61 };
     62 
     63 namespace WebCore {
     64 
     65 class AndroidAnimation;
     66 
     67 class LayerAndroid : public SkLayer {
     68 
     69 public:
     70     LayerAndroid(bool isRootLayer);
     71     LayerAndroid(const LayerAndroid& layer);
     72     LayerAndroid(SkPicture* );
     73     virtual ~LayerAndroid();
     74 
     75     static int instancesCount();
     76 
     77     void setTranslation(SkScalar x, SkScalar y) { m_translation.set(x, y); }
     78     void setRotation(SkScalar a) { m_angleTransform = a; m_doRotation = true; }
     79     void setScale(SkScalar x, SkScalar y) { m_scale.set(x, y); }
     80     SkPoint translation() const { return m_translation; }
     81     SkRect  bounds() const {
     82         const SkPoint& pos = this->getPosition();
     83         const SkSize& size = this->getSize();
     84         SkRect rect;
     85         rect.set(pos.fX, pos.fY,
     86                  pos.fX + size.width(),
     87                  pos.fY + size.height());
     88         rect.offset(m_translation.fX, m_translation.fY);
     89         return rect;
     90     }
     91     void setFixedPosition(SkLength left,   // CSS left property
     92                           SkLength top,    // CSS top property
     93                           SkLength right,  // CSS right property
     94                           SkLength bottom, // CSS bottom property
     95                           SkLength marginLeft,   // CSS margin-left property
     96                           SkLength marginTop,    // CSS margin-top property
     97                           SkLength marginRight,  // CSS margin-right property
     98                           SkLength marginBottom, // CSS margin-bottom property
     99                           int offsetX,     // X Offset from the renderer
    100                           int offsetY,     // Y Offset from the renderer
    101                           int width,       // visible overflow width
    102                           int height) {    // visible overflow height
    103         m_fixedLeft = left;
    104         m_fixedTop = top;
    105         m_fixedRight = right;
    106         m_fixedBottom = bottom;
    107         m_fixedMarginLeft = marginLeft;
    108         m_fixedMarginTop = marginTop;
    109         m_fixedMarginRight = marginRight;
    110         m_fixedMarginBottom = marginBottom;
    111         m_fixedOffset.set(offsetX, offsetY);
    112         m_fixedWidth = width;
    113         m_fixedHeight = height;
    114         m_isFixed = true;
    115         setInheritFromRootTransform(true);
    116     }
    117 
    118     void setBackgroundColor(SkColor color);
    119     void setMaskLayer(LayerAndroid*);
    120     void setMasksToBounds(bool);
    121 
    122     void setIsRootLayer(bool isRootLayer) { m_isRootLayer = isRootLayer; }
    123 
    124     SkPicture* recordContext();
    125 
    126     void addAnimation(PassRefPtr<AndroidAnimation> anim);
    127     void removeAnimation(const String& name);
    128     bool evaluateAnimations() const;
    129     bool evaluateAnimations(double time) const;
    130     bool hasAnimations() const;
    131 
    132     SkPicture* picture() const { return m_recordingPicture; }
    133 
    134     // remove layers bounds from visible rectangle to show what can be
    135     // scrolled into view; returns original minus layer bounds in global space.
    136     SkRect subtractLayers(const SkRect& visibleRect) const;
    137 
    138     void dumpLayers(FILE*, int indentLevel) const;
    139     void dumpToLog() const;
    140 
    141     /** Call this with the current viewport (scrolling, zoom) to update
    142         the position of the fixed layers.
    143 
    144         This call is recursive, so it should be called on the root of the
    145         hierarchy.
    146     */
    147     void updateFixedLayersPositions(const SkRect& viewPort);
    148 
    149     /** Call this to update the position attribute, so that later calls
    150         like bounds() will report the corrected position.
    151 
    152         This call is recursive, so it should be called on the root of the
    153         hierarchy.
    154      */
    155     void updatePositions();
    156 
    157     void clipArea(SkTDArray<SkRect>* region) const;
    158     const LayerAndroid* find(int x, int y) const;
    159     const LayerAndroid* findById(int uniqueID) const;
    160     LayerAndroid* getChild(int index) const {
    161         return static_cast<LayerAndroid*>(this->INHERITED::getChild(index));
    162     }
    163     void setExtra(DrawExtra* extra);  // does not assign ownership
    164     int uniqueId() const { return m_uniqueId; }
    165     bool isFixed() { return m_isFixed; }
    166     const SkPoint& getOffset() const { return m_fixedOffset; }
    167     int getFixedWidth() { return m_fixedWidth; }
    168     int getFixedHeight() { return m_fixedHeight; }
    169 
    170 protected:
    171     virtual void onDraw(SkCanvas*, SkScalar opacity);
    172 
    173 private:
    174 #if DUMP_NAV_CACHE
    175     friend class CachedLayer::Debug; // debugging access only
    176 #endif
    177     void bounds(SkRect* ) const;
    178     bool prepareContext(bool force = false);
    179     void clipInner(SkTDArray<SkRect>* region, const SkRect& local) const;
    180 
    181     bool m_isRootLayer;
    182     bool m_drawsContent;
    183     bool m_haveClip;
    184     bool m_doRotation;
    185     bool m_isFixed;
    186     bool m_backgroundColorSet;
    187 
    188     SkLength m_fixedLeft;
    189     SkLength m_fixedTop;
    190     SkLength m_fixedRight;
    191     SkLength m_fixedBottom;
    192     SkLength m_fixedMarginLeft;
    193     SkLength m_fixedMarginTop;
    194     SkLength m_fixedMarginRight;
    195     SkLength m_fixedMarginBottom;
    196     SkPoint  m_fixedOffset;
    197     int m_fixedWidth;
    198     int m_fixedHeight;
    199 
    200     SkPoint m_translation;
    201     SkPoint m_scale;
    202     SkScalar m_angleTransform;
    203     SkColor m_backgroundColor;
    204 
    205     SkPicture* m_recordingPicture;
    206 
    207     typedef HashMap<String, RefPtr<AndroidAnimation> > KeyframesMap;
    208     KeyframesMap m_animations;
    209     DrawExtra* m_extra;
    210     int m_uniqueId;
    211 
    212     typedef SkLayer INHERITED;
    213 };
    214 
    215 }
    216 
    217 #else
    218 
    219 class SkPicture;
    220 
    221 namespace WebCore {
    222 
    223 class LayerAndroid {
    224 public:
    225     LayerAndroid(SkPicture* picture) :
    226         m_recordingPicture(picture), // does not assign ownership
    227         m_uniqueId(-1)
    228     {}
    229     SkPicture* picture() const { return m_recordingPicture; }
    230     int uniqueId() const { return m_uniqueId; }
    231 private:
    232     SkPicture* m_recordingPicture;
    233     int m_uniqueId;
    234 };
    235 
    236 }
    237 
    238 #endif // USE(ACCELERATED_COMPOSITING)
    239 
    240 #endif  // LayerAndroid_h
    241