Home | History | Annotate | Download | only in context
      1 /*
      2  * Copyright 2006, 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 #ifndef platform_graphics_context_h
     27 #define platform_graphics_context_h
     28 
     29 #include "IntRect.h"
     30 #include "GraphicsContext.h"
     31 #include "RenderSkinAndroid.h"
     32 #include "RenderSkinMediaButton.h"
     33 #include "SkCanvas.h"
     34 #include "SkPicture.h"
     35 #include "SkTDArray.h"
     36 #include <wtf/Vector.h>
     37 
     38 class SkCanvas;
     39 
     40 namespace WebCore {
     41 
     42 class PlatformGraphicsContext {
     43 public:
     44     class State;
     45 
     46     PlatformGraphicsContext();
     47     virtual ~PlatformGraphicsContext();
     48     virtual bool isPaintingDisabled() = 0;
     49 
     50     void setGraphicsContext(GraphicsContext* gc) { m_gc = gc; }
     51     virtual bool deleteUs() const { return false; }
     52 
     53     typedef enum { PaintingContext, RecordingContext } ContextType;
     54     virtual ContextType type() = 0;
     55 
     56     // State management
     57     virtual void beginTransparencyLayer(float opacity) = 0;
     58     virtual void endTransparencyLayer() = 0;
     59     virtual void save();
     60     virtual void restore();
     61 
     62     // State values
     63     virtual void setAlpha(float alpha);
     64     int getNormalizedAlpha() const;
     65     virtual void setCompositeOperation(CompositeOperator op);
     66     virtual bool setFillColor(const Color& c);
     67     virtual bool setFillShader(SkShader* fillShader);
     68     virtual void setLineCap(LineCap cap);
     69     virtual void setLineDash(const DashArray& dashes, float dashOffset);
     70     virtual void setLineJoin(LineJoin join);
     71     virtual void setMiterLimit(float limit);
     72     virtual void setShadow(int radius, int dx, int dy, SkColor c);
     73     virtual void setShouldAntialias(bool useAA);
     74     virtual bool setStrokeColor(const Color& c);
     75     virtual bool setStrokeShader(SkShader* strokeShader);
     76     virtual void setStrokeStyle(StrokeStyle style);
     77     virtual void setStrokeThickness(float f);
     78 
     79     // FIXME: These setupPaint* should be private, but
     80     //        they are used by FontAndroid currently
     81     virtual void setupPaintFill(SkPaint* paint) const;
     82     virtual bool setupPaintShadow(SkPaint* paint, SkPoint* offset) const;
     83     // Sets up the paint for stroking. Returns true if the style is really
     84     // just a dash of squares (the size of the paint's stroke-width.
     85     virtual bool setupPaintStroke(SkPaint* paint, SkRect* rect, bool isHLine = false);
     86 
     87     // Matrix operations
     88     virtual void concatCTM(const AffineTransform& affine) = 0;
     89     virtual void rotate(float angleInRadians) = 0;
     90     virtual void scale(const FloatSize& size) = 0;
     91     virtual void translate(float x, float y) = 0;
     92     virtual const SkMatrix& getTotalMatrix() = 0;
     93 
     94     // Clipping
     95     virtual void addInnerRoundedRectClip(const IntRect& rect, int thickness) = 0;
     96     virtual void canvasClip(const Path& path) = 0;
     97     virtual bool clip(const FloatRect& rect) = 0;
     98     virtual bool clip(const Path& path) = 0;
     99     virtual bool clipConvexPolygon(size_t numPoints, const FloatPoint*, bool antialias) = 0;
    100     virtual bool clipOut(const IntRect& r) = 0;
    101     virtual bool clipOut(const Path& p) = 0;
    102     virtual bool clipPath(const Path& pathToClip, WindRule clipRule) = 0;
    103     virtual SkIRect getTotalClipBounds() = 0;
    104 
    105     // Drawing
    106     virtual void clearRect(const FloatRect& rect) = 0;
    107     virtual void drawBitmapPattern(const SkBitmap& bitmap, const SkMatrix& matrix,
    108                            CompositeOperator compositeOp, const FloatRect& destRect) = 0;
    109     virtual void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
    110                         const SkRect& dst, CompositeOperator op = CompositeSourceOver) = 0;
    111     virtual void drawConvexPolygon(size_t numPoints, const FloatPoint* points,
    112                            bool shouldAntialias) = 0;
    113     virtual void drawEllipse(const IntRect& rect) = 0;
    114     virtual void drawFocusRing(const Vector<IntRect>& rects, int /* width */,
    115                        int /* offset */, const Color& color) = 0;
    116     virtual void drawHighlightForText(const Font& font, const TextRun& run,
    117                               const FloatPoint& point, int h,
    118                               const Color& backgroundColor, ColorSpace colorSpace,
    119                               int from, int to, bool isActive) = 0;
    120     virtual void drawLine(const IntPoint& point1, const IntPoint& point2) = 0;
    121     virtual void drawLineForText(const FloatPoint& pt, float width) = 0;
    122     virtual void drawLineForTextChecking(const FloatPoint& pt, float width,
    123                                          GraphicsContext::TextCheckingLineStyle) = 0;
    124     virtual void drawRect(const IntRect& rect) = 0;
    125     virtual void fillPath(const Path& pathToFill, WindRule fillRule) = 0;
    126     virtual void fillRect(const FloatRect& rect) = 0;
    127     void fillRect(const FloatRect& rect, const Color& color, ColorSpace) {
    128         fillRect(rect, color);
    129     }
    130     virtual void fillRect(const FloatRect& rect, const Color& color) = 0;
    131     void fillRoundedRect(const IntRect& rect, const IntSize& topLeft,
    132                          const IntSize& topRight, const IntSize& bottomLeft,
    133                          const IntSize& bottomRight, const Color& color,
    134                          ColorSpace) {
    135         fillRoundedRect(rect, topLeft, topRight, bottomLeft, bottomRight, color);
    136     }
    137     virtual void fillRoundedRect(const IntRect& rect, const IntSize& topLeft,
    138                          const IntSize& topRight, const IntSize& bottomLeft,
    139                          const IntSize& bottomRight, const Color& color) = 0;
    140     virtual void strokeArc(const IntRect& r, int startAngle, int angleSpan) = 0;
    141     virtual void strokePath(const Path& pathToStroke) = 0;
    142     virtual void strokeRect(const FloatRect& rect, float lineWidth) = 0;
    143 
    144     virtual void drawPosText(const void* text, size_t byteLength,
    145                              const SkPoint pos[], const SkPaint& paint) = 0;
    146     virtual void drawMediaButton(const IntRect& rect, RenderSkinMediaButton::MediaButton buttonType,
    147                                  bool translucent = false, bool drawBackground = true,
    148                                  const IntRect& thumb = IntRect()) = 0;
    149 
    150     virtual SkCanvas* recordingCanvas() = 0;
    151     virtual void setTextOffset(FloatSize offset) = 0;
    152 
    153     void setRawState(State* state) { m_state = state; }
    154 
    155     struct ShadowRec {
    156         SkScalar blur;
    157         SkScalar dx;
    158         SkScalar dy;
    159         SkColor color;  // alpha>0 means valid shadow
    160         ShadowRec(SkScalar b = 0,
    161                   SkScalar x = 0,
    162                   SkScalar y = 0,
    163                   SkColor c = 0) // by default, alpha=0, so no shadow
    164                 : blur(b), dx(x), dy(y), color(c)
    165             {};
    166     };
    167 
    168     class State {
    169     public:
    170         SkPathEffect* pathEffect;
    171         float miterLimit;
    172         float alpha;
    173         float strokeThickness;
    174         SkPaint::Cap lineCap;
    175         SkPaint::Join lineJoin;
    176         SkXfermode::Mode mode;
    177         int dashRatio; // Ratio of the length of a dash to its width
    178         ShadowRec shadow;
    179         SkColor fillColor;
    180         SkShader* fillShader;
    181         SkColor strokeColor;
    182         SkShader* strokeShader;
    183         bool useAA;
    184         StrokeStyle strokeStyle;
    185 
    186         State();
    187         State(const State& other);
    188         ~State();
    189 
    190         void setShadow(int radius, int dx, int dy, SkColor c);
    191         bool setupShadowPaint(SkPaint* paint, SkPoint* offset,
    192                               bool shadowsIgnoreTransforms);
    193         SkColor applyAlpha(SkColor c) const;
    194 
    195         State cloneInheritedProperties();
    196     private:
    197         // Not supported.
    198         void operator=(const State&);
    199 
    200         friend class PlatformGraphicsContextRecording;
    201         friend class PlatformGraphicsContextSkia;
    202     };
    203 
    204 protected:
    205     virtual bool shadowsIgnoreTransforms() const = 0;
    206     void setupPaintCommon(SkPaint* paint) const;
    207     GraphicsContext* m_gc; // Back-ptr to our parent
    208 
    209     WTF::Vector<State> m_stateStack;
    210     State* m_state;
    211 };
    212 
    213 }
    214 #endif
    215