Home | History | Annotate | Download | only in skia
      1 /*
      2  * Copyright (c) 2008, Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef PlatformContextSkia_h
     32 #define PlatformContextSkia_h
     33 
     34 #include "GraphicsContext.h"
     35 #include "Noncopyable.h"
     36 
     37 #include "SkDashPathEffect.h"
     38 #include "SkDrawLooper.h"
     39 #include "SkPaint.h"
     40 #include "SkPath.h"
     41 #include "skia/ext/platform_canvas.h"
     42 
     43 #include <wtf/Vector.h>
     44 
     45 namespace WebCore {
     46 
     47 enum CompositeOperator;
     48 class DrawingBuffer;
     49 class GLES2Canvas;
     50 class GraphicsContext3D;
     51 class Texture;
     52 
     53 // This class holds the platform-specific state for GraphicsContext. We put
     54 // most of our Skia wrappers on this class. In theory, a lot of this stuff could
     55 // be moved to GraphicsContext directly, except that some code external to this
     56 // would like to poke at our graphics layer as well (like the Image and Font
     57 // stuff, which needs some amount of our wrappers and state around SkCanvas).
     58 //
     59 // So in general, this class uses just Skia types except when there's no easy
     60 // conversion. GraphicsContext is responsible for converting the WebKit types to
     61 // Skia types and setting up the eventual call to the Skia functions.
     62 //
     63 // This class then keeps track of all the current Skia state. WebKit expects
     64 // that the graphics state that is pushed and popped by save() and restore()
     65 // includes things like colors and pen styles. Skia does this differently, where
     66 // push and pop only includes transforms and bitmaps, and the application is
     67 // responsible for managing the painting state which is store in separate
     68 // SkPaint objects. This class provides the adaptor that allows the painting
     69 // state to be pushed and popped along with the bitmap.
     70 class PlatformContextSkia {
     71     WTF_MAKE_NONCOPYABLE(PlatformContextSkia);
     72 public:
     73     // For printing, there shouldn't be any canvas. canvas can be NULL. If you
     74     // supply a NULL canvas, you can also call setCanvas later.
     75     PlatformContextSkia(SkCanvas*);
     76     ~PlatformContextSkia();
     77 
     78     // Sets the canvas associated with this context. Use when supplying NULL
     79     // to the constructor.
     80     void setCanvas(SkCanvas*);
     81 
     82     // If false we're rendering to a GraphicsContext for a web page, if false
     83     // we're not (as is the case when rendering to a canvas object).
     84     // If this is true the contents have not been marked up with the magic
     85     // color and all text drawing needs to go to a layer so that the alpha is
     86     // correctly updated.
     87     void setDrawingToImageBuffer(bool);
     88     bool isDrawingToImageBuffer() const;
     89 
     90     void save();
     91     void restore();
     92 
     93     // Begins a layer that is clipped to the image |imageBuffer| at the location
     94     // |rect|. This layer is implicitly restored when the next restore is
     95     // invoked.
     96     // NOTE: |imageBuffer| may be deleted before the |restore| is invoked.
     97     void beginLayerClippedToImage(const FloatRect&, const ImageBuffer*);
     98     void clipPathAntiAliased(const SkPath&);
     99 
    100     // Sets up the common flags on a paint for antialiasing, effects, etc.
    101     // This is implicitly called by setupPaintFill and setupPaintStroke, but
    102     // you may wish to call it directly sometimes if you don't want that other
    103     // behavior.
    104     void setupPaintCommon(SkPaint*) const;
    105 
    106     // Sets up the paint for the current fill style.
    107     void setupPaintForFilling(SkPaint*) const;
    108 
    109     // Sets up the paint for stroking. Returns an int representing the width of
    110     // the pen, or 1 if the pen's width is 0 if a non-zero length is provided,
    111     // the number of dashes/dots on a dashed/dotted line will be adjusted to
    112     // start and end that length with a dash/dot.
    113     float setupPaintForStroking(SkPaint*, SkRect*, int length) const;
    114 
    115     // State setting functions.
    116     void setDrawLooper(SkDrawLooper*);  // Note: takes an additional ref.
    117     void setMiterLimit(float);
    118     void setAlpha(float);
    119     void setLineCap(SkPaint::Cap);
    120     void setLineJoin(SkPaint::Join);
    121     void setXfermodeMode(SkXfermode::Mode);
    122     void setFillColor(SkColor);
    123     void setFillShader(SkShader*);
    124     void setStrokeStyle(StrokeStyle);
    125     void setStrokeColor(SkColor);
    126     void setStrokeThickness(float thickness);
    127     void setStrokeShader(SkShader*);
    128     void setTextDrawingMode(TextDrawingModeFlags mode);
    129     void setUseAntialiasing(bool enable);
    130     void setDashPathEffect(SkDashPathEffect*);
    131 
    132     SkDrawLooper* getDrawLooper() const;
    133     StrokeStyle getStrokeStyle() const;
    134     float getStrokeThickness() const;
    135     TextDrawingModeFlags getTextDrawingMode() const;
    136     float getAlpha() const;
    137     int getNormalizedAlpha() const;
    138 
    139     void canvasClipPath(const SkPath&);
    140 
    141     // Returns the fill color. The returned color has it's alpha adjusted
    142     // by the current alpha.
    143     SkColor effectiveFillColor() const;
    144 
    145     // Returns the stroke color. The returned color has it's alpha adjusted
    146     // by the current alpha.
    147     SkColor effectiveStrokeColor() const;
    148 
    149     // Returns the canvas used for painting, NOT guaranteed to be non-null.
    150     SkCanvas* canvas() { return m_canvas; }
    151 
    152     InterpolationQuality interpolationQuality() const;
    153     void setInterpolationQuality(InterpolationQuality interpolationQuality);
    154 
    155     // FIXME: This should be pushed down to GraphicsContext.
    156     void drawRect(SkRect rect);
    157 
    158     // FIXME: I'm still unsure how I will serialize this call.
    159     void paintSkPaint(const SkRect&, const SkPaint&);
    160 
    161     const SkBitmap* bitmap() const;
    162 
    163     // Returns if the context is a printing context instead of a display
    164     // context. Bitmap shouldn't be resampled when printing to keep the best
    165     // possible quality.
    166     bool printing() const { return m_printing; }
    167     void setPrinting(bool p) { m_printing = p; }
    168 
    169     // Returns if the context allows rendering of fonts using native platform
    170     // APIs. If false is returned font rendering is performed using the skia
    171     // text drawing APIs.
    172     bool isNativeFontRenderingAllowed();
    173 
    174     void getImageResamplingHint(IntSize* srcSize, FloatSize* dstSize) const;
    175     void setImageResamplingHint(const IntSize& srcSize, const FloatSize& dstSize);
    176     void clearImageResamplingHint();
    177     bool hasImageResamplingHint() const;
    178 
    179     bool canAccelerate() const;
    180     bool canvasClipApplied() const;
    181     bool useGPU() { return m_useGPU; }
    182     void setSharedGraphicsContext3D(SharedGraphicsContext3D*, DrawingBuffer*, const IntSize&);
    183 #if ENABLE(ACCELERATED_2D_CANVAS)
    184     GLES2Canvas* gpuCanvas() const { return m_gpuCanvas.get(); }
    185 #else
    186     GLES2Canvas* gpuCanvas() const { return 0; }
    187 #endif
    188     // Call these before making a call that manipulates the underlying
    189     // SkCanvas or WebCore::GLES2Canvas
    190     void prepareForSoftwareDraw() const;
    191     void prepareForHardwareDraw() const;
    192     // Call to force the SkCanvas to contain all rendering results.
    193     void syncSoftwareCanvas() const;
    194     void markDirtyRect(const IntRect& rect);
    195 
    196 private:
    197     // Used when restoring and the state has an image clip. Only shows the pixels in
    198     // m_canvas that are also in imageBuffer.
    199     void applyClipFromImage(const FloatRect&, const SkBitmap&);
    200     void applyAntiAliasedClipPaths(WTF::Vector<SkPath>& paths);
    201 
    202     void uploadSoftwareToHardware(CompositeOperator) const;
    203     void readbackHardwareToSoftware() const;
    204 
    205     // Defines drawing style.
    206     struct State;
    207 
    208     // NULL indicates painting is disabled. Never delete this object.
    209     SkCanvas* m_canvas;
    210 
    211     // States stack. Enables local drawing state change with save()/restore()
    212     // calls.
    213     WTF::Vector<State> m_stateStack;
    214     // Pointer to the current drawing state. This is a cached value of
    215     // mStateStack.back().
    216     State* m_state;
    217 
    218     // Stores image sizes for a hint to compute image resampling modes.
    219     // Values are used in ImageSkia.cpp
    220     IntSize m_imageResamplingHintSrcSize;
    221     FloatSize m_imageResamplingHintDstSize;
    222     bool m_printing;
    223     bool m_drawingToImageBuffer;
    224     bool m_useGPU;
    225 #if ENABLE(ACCELERATED_2D_CANVAS)
    226     OwnPtr<GLES2Canvas> m_gpuCanvas;
    227     mutable RefPtr<Texture> m_uploadTexture;
    228 #endif
    229     mutable enum { None, Software, Mixed, Hardware } m_backingStoreState;
    230     mutable IntRect m_softwareDirtyRect;
    231 };
    232 
    233 }
    234 #endif // PlatformContextSkia_h
    235