Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2006 The Android Open Source Project
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef SkBlitter_DEFINED
     11 #define SkBlitter_DEFINED
     12 
     13 #include "SkBitmap.h"
     14 #include "SkMatrix.h"
     15 #include "SkPaint.h"
     16 #include "SkRefCnt.h"
     17 #include "SkRegion.h"
     18 #include "SkMask.h"
     19 
     20 /** SkBlitter and its subclasses are responsible for actually writing pixels
     21     into memory. Besides efficiency, they handle clipping and antialiasing.
     22 */
     23 class SkBlitter {
     24 public:
     25     virtual ~SkBlitter();
     26 
     27     /// Blit a horizontal run of one or more pixels.
     28     virtual void blitH(int x, int y, int width);
     29     /// Blit a horizontal run of antialiased pixels; runs[] is a *sparse*
     30     /// zero-terminated run-length encoding of spans of constant alpha values.
     31     virtual void blitAntiH(int x, int y, const SkAlpha antialias[],
     32                            const int16_t runs[]);
     33     /// Blit a vertical run of pixels with a constant alpha value.
     34     virtual void blitV(int x, int y, int height, SkAlpha alpha);
     35     /// Blit a solid rectangle one or more pixels wide.
     36     virtual void blitRect(int x, int y, int width, int height);
     37     /** Blit a rectangle with one alpha-blended column on the left,
     38         width (zero or more) opaque pixels, and one alpha-blended column
     39         on the right.
     40         The result will always be at least two pixels wide.
     41     */
     42     virtual void blitAntiRect(int x, int y, int width, int height,
     43                               SkAlpha leftAlpha, SkAlpha rightAlpha);
     44     /// Blit a pattern of pixels defined by a rectangle-clipped mask;
     45     /// typically used for text.
     46     virtual void blitMask(const SkMask&, const SkIRect& clip);
     47 
     48     /** If the blitter just sets a single value for each pixel, return the
     49         bitmap it draws into, and assign value. If not, return NULL and ignore
     50         the value parameter.
     51     */
     52     virtual const SkBitmap* justAnOpaqueColor(uint32_t* value);
     53 
     54     /**
     55      *  Special method just to identify the null blitter, which is returned
     56      *  from Choose() if the request cannot be fulfilled. Default impl
     57      *  returns false.
     58      */
     59     virtual bool isNullBlitter() const;
     60 
     61     ///@name non-virtual helpers
     62     void blitMaskRegion(const SkMask& mask, const SkRegion& clip);
     63     void blitRectRegion(const SkIRect& rect, const SkRegion& clip);
     64     void blitRegion(const SkRegion& clip);
     65     ///@}
     66 
     67     /** @name Factories
     68         Return the correct blitter to use given the specified context.
     69      */
     70     static SkBlitter* Choose(const SkBitmap& device,
     71                              const SkMatrix& matrix,
     72                              const SkPaint& paint) {
     73         return Choose(device, matrix, paint, NULL, 0);
     74     }
     75 
     76     static SkBlitter* Choose(const SkBitmap& device,
     77                              const SkMatrix& matrix,
     78                              const SkPaint& paint,
     79                              void* storage, size_t storageSize);
     80 
     81     static SkBlitter* ChooseSprite(const SkBitmap& device,
     82                                    const SkPaint&,
     83                                    const SkBitmap& src,
     84                                    int left, int top,
     85                                    void* storage, size_t storageSize);
     86     ///@}
     87 
     88 private:
     89 };
     90 
     91 /** This blitter silently never draws anything.
     92 */
     93 class SkNullBlitter : public SkBlitter {
     94 public:
     95     virtual void blitH(int x, int y, int width) SK_OVERRIDE;
     96     virtual void blitAntiH(int x, int y, const SkAlpha[],
     97                            const int16_t runs[]) SK_OVERRIDE;
     98     virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
     99     virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
    100     virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
    101     virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
    102     virtual bool isNullBlitter() const SK_OVERRIDE;
    103 };
    104 
    105 /** Wraps another (real) blitter, and ensures that the real blitter is only
    106     called with coordinates that have been clipped by the specified clipRect.
    107     This means the caller need not perform the clipping ahead of time.
    108 */
    109 class SkRectClipBlitter : public SkBlitter {
    110 public:
    111     void init(SkBlitter* blitter, const SkIRect& clipRect) {
    112         SkASSERT(!clipRect.isEmpty());
    113         fBlitter = blitter;
    114         fClipRect = clipRect;
    115     }
    116 
    117     virtual void blitH(int x, int y, int width) SK_OVERRIDE;
    118     virtual void blitAntiH(int x, int y, const SkAlpha[],
    119                            const int16_t runs[]) SK_OVERRIDE;
    120     virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
    121     virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
    122     virtual void blitAntiRect(int x, int y, int width, int height,
    123                      SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE;
    124     virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
    125     virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
    126 
    127 private:
    128     SkBlitter*  fBlitter;
    129     SkIRect     fClipRect;
    130 };
    131 
    132 /** Wraps another (real) blitter, and ensures that the real blitter is only
    133     called with coordinates that have been clipped by the specified clipRgn.
    134     This means the caller need not perform the clipping ahead of time.
    135 */
    136 class SkRgnClipBlitter : public SkBlitter {
    137 public:
    138     void init(SkBlitter* blitter, const SkRegion* clipRgn) {
    139         SkASSERT(clipRgn && !clipRgn->isEmpty());
    140         fBlitter = blitter;
    141         fRgn = clipRgn;
    142     }
    143 
    144     virtual void blitH(int x, int y, int width) SK_OVERRIDE;
    145     virtual void blitAntiH(int x, int y, const SkAlpha[],
    146                            const int16_t runs[]) SK_OVERRIDE;
    147     virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
    148     virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
    149     virtual void blitAntiRect(int x, int y, int width, int height,
    150                      SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE;
    151     virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
    152     virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
    153 
    154 private:
    155     SkBlitter*      fBlitter;
    156     const SkRegion* fRgn;
    157 };
    158 
    159 /** Factory to set up the appropriate most-efficient wrapper blitter
    160     to apply a clip. Returns a pointer to a member, so lifetime must
    161     be managed carefully.
    162 */
    163 class SkBlitterClipper {
    164 public:
    165     SkBlitter*  apply(SkBlitter* blitter, const SkRegion* clip,
    166                       const SkIRect* bounds = NULL);
    167 
    168 private:
    169     SkNullBlitter       fNullBlitter;
    170     SkRectClipBlitter   fRectBlitter;
    171     SkRgnClipBlitter    fRgnBlitter;
    172 };
    173 
    174 #endif
    175