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     ///@name non-virtual helpers
     55     void blitMaskRegion(const SkMask& mask, const SkRegion& clip);
     56     void blitRectRegion(const SkIRect& rect, const SkRegion& clip);
     57     void blitRegion(const SkRegion& clip);
     58     ///@}
     59 
     60     /** @name Factories
     61         Return the correct blitter to use given the specified context.
     62      */
     63     static SkBlitter* Choose(const SkBitmap& device,
     64                              const SkMatrix& matrix,
     65                              const SkPaint& paint) {
     66         return Choose(device, matrix, paint, NULL, 0);
     67     }
     68 
     69     static SkBlitter* Choose(const SkBitmap& device,
     70                              const SkMatrix& matrix,
     71                              const SkPaint& paint,
     72                              void* storage, size_t storageSize);
     73 
     74     static SkBlitter* ChooseSprite(const SkBitmap& device,
     75                                    const SkPaint&,
     76                                    const SkBitmap& src,
     77                                    int left, int top,
     78                                    void* storage, size_t storageSize);
     79     ///@}
     80 
     81 private:
     82 };
     83 
     84 /** This blitter silently never draws anything.
     85 */
     86 class SkNullBlitter : public SkBlitter {
     87 public:
     88     virtual void blitH(int x, int y, int width) SK_OVERRIDE;
     89     virtual void blitAntiH(int x, int y, const SkAlpha[],
     90                            const int16_t runs[]) SK_OVERRIDE;
     91     virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
     92     virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
     93     virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
     94     virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
     95 };
     96 
     97 /** Wraps another (real) blitter, and ensures that the real blitter is only
     98     called with coordinates that have been clipped by the specified clipRect.
     99     This means the caller need not perform the clipping ahead of time.
    100 */
    101 class SkRectClipBlitter : public SkBlitter {
    102 public:
    103     void init(SkBlitter* blitter, const SkIRect& clipRect) {
    104         SkASSERT(!clipRect.isEmpty());
    105         fBlitter = blitter;
    106         fClipRect = clipRect;
    107     }
    108 
    109     virtual void blitH(int x, int y, int width) SK_OVERRIDE;
    110     virtual void blitAntiH(int x, int y, const SkAlpha[],
    111                            const int16_t runs[]) SK_OVERRIDE;
    112     virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
    113     virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
    114     virtual void blitAntiRect(int x, int y, int width, int height,
    115                      SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE;
    116     virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
    117     virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
    118 
    119 private:
    120     SkBlitter*  fBlitter;
    121     SkIRect     fClipRect;
    122 };
    123 
    124 /** Wraps another (real) blitter, and ensures that the real blitter is only
    125     called with coordinates that have been clipped by the specified clipRgn.
    126     This means the caller need not perform the clipping ahead of time.
    127 */
    128 class SkRgnClipBlitter : public SkBlitter {
    129 public:
    130     void init(SkBlitter* blitter, const SkRegion* clipRgn) {
    131         SkASSERT(clipRgn && !clipRgn->isEmpty());
    132         fBlitter = blitter;
    133         fRgn = clipRgn;
    134     }
    135 
    136     virtual void blitH(int x, int y, int width) SK_OVERRIDE;
    137     virtual void blitAntiH(int x, int y, const SkAlpha[],
    138                            const int16_t runs[]) SK_OVERRIDE;
    139     virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
    140     virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
    141     virtual void blitAntiRect(int x, int y, int width, int height,
    142                      SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE;
    143     virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
    144     virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
    145 
    146 private:
    147     SkBlitter*      fBlitter;
    148     const SkRegion* fRgn;
    149 };
    150 
    151 /** Factory to set up the appropriate most-efficient wrapper blitter
    152     to apply a clip. Returns a pointer to a member, so lifetime must
    153     be managed carefully.
    154 */
    155 class SkBlitterClipper {
    156 public:
    157     SkBlitter*  apply(SkBlitter* blitter, const SkRegion* clip,
    158                       const SkIRect* bounds = NULL);
    159 
    160 private:
    161     SkNullBlitter       fNullBlitter;
    162     SkRectClipBlitter   fRectBlitter;
    163     SkRgnClipBlitter    fRgnBlitter;
    164 };
    165 
    166 #endif
    167