1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkBlitter.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,172 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#ifndef SkBlitter_DEFINED 1.14 +#define SkBlitter_DEFINED 1.15 + 1.16 +#include "SkBitmap.h" 1.17 +#include "SkBitmapProcShader.h" 1.18 +#include "SkMask.h" 1.19 +#include "SkMatrix.h" 1.20 +#include "SkPaint.h" 1.21 +#include "SkRefCnt.h" 1.22 +#include "SkRegion.h" 1.23 +#include "SkShader.h" 1.24 +#include "SkSmallAllocator.h" 1.25 + 1.26 +/** SkBlitter and its subclasses are responsible for actually writing pixels 1.27 + into memory. Besides efficiency, they handle clipping and antialiasing. 1.28 +*/ 1.29 +class SkBlitter { 1.30 +public: 1.31 + virtual ~SkBlitter(); 1.32 + 1.33 + /// Blit a horizontal run of one or more pixels. 1.34 + virtual void blitH(int x, int y, int width); 1.35 + /// Blit a horizontal run of antialiased pixels; runs[] is a *sparse* 1.36 + /// zero-terminated run-length encoding of spans of constant alpha values. 1.37 + virtual void blitAntiH(int x, int y, const SkAlpha antialias[], 1.38 + const int16_t runs[]); 1.39 + /// Blit a vertical run of pixels with a constant alpha value. 1.40 + virtual void blitV(int x, int y, int height, SkAlpha alpha); 1.41 + /// Blit a solid rectangle one or more pixels wide. 1.42 + virtual void blitRect(int x, int y, int width, int height); 1.43 + /** Blit a rectangle with one alpha-blended column on the left, 1.44 + width (zero or more) opaque pixels, and one alpha-blended column 1.45 + on the right. 1.46 + The result will always be at least two pixels wide. 1.47 + */ 1.48 + virtual void blitAntiRect(int x, int y, int width, int height, 1.49 + SkAlpha leftAlpha, SkAlpha rightAlpha); 1.50 + /// Blit a pattern of pixels defined by a rectangle-clipped mask; 1.51 + /// typically used for text. 1.52 + virtual void blitMask(const SkMask&, const SkIRect& clip); 1.53 + 1.54 + /** If the blitter just sets a single value for each pixel, return the 1.55 + bitmap it draws into, and assign value. If not, return NULL and ignore 1.56 + the value parameter. 1.57 + */ 1.58 + virtual const SkBitmap* justAnOpaqueColor(uint32_t* value); 1.59 + 1.60 + /** 1.61 + * Special method just to identify the null blitter, which is returned 1.62 + * from Choose() if the request cannot be fulfilled. Default impl 1.63 + * returns false. 1.64 + */ 1.65 + virtual bool isNullBlitter() const; 1.66 + 1.67 + ///@name non-virtual helpers 1.68 + void blitMaskRegion(const SkMask& mask, const SkRegion& clip); 1.69 + void blitRectRegion(const SkIRect& rect, const SkRegion& clip); 1.70 + void blitRegion(const SkRegion& clip); 1.71 + ///@} 1.72 + 1.73 + /** @name Factories 1.74 + Return the correct blitter to use given the specified context. 1.75 + */ 1.76 + static SkBlitter* Choose(const SkBitmap& device, 1.77 + const SkMatrix& matrix, 1.78 + const SkPaint& paint, 1.79 + SkTBlitterAllocator*, 1.80 + bool drawCoverage = false); 1.81 + 1.82 + static SkBlitter* ChooseSprite(const SkBitmap& device, 1.83 + const SkPaint&, 1.84 + const SkBitmap& src, 1.85 + int left, int top, 1.86 + SkTBlitterAllocator*); 1.87 + ///@} 1.88 + 1.89 +private: 1.90 +}; 1.91 + 1.92 +/** This blitter silently never draws anything. 1.93 +*/ 1.94 +class SkNullBlitter : public SkBlitter { 1.95 +public: 1.96 + virtual void blitH(int x, int y, int width) SK_OVERRIDE; 1.97 + virtual void blitAntiH(int x, int y, const SkAlpha[], 1.98 + const int16_t runs[]) SK_OVERRIDE; 1.99 + virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE; 1.100 + virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE; 1.101 + virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE; 1.102 + virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE; 1.103 + virtual bool isNullBlitter() const SK_OVERRIDE; 1.104 +}; 1.105 + 1.106 +/** Wraps another (real) blitter, and ensures that the real blitter is only 1.107 + called with coordinates that have been clipped by the specified clipRect. 1.108 + This means the caller need not perform the clipping ahead of time. 1.109 +*/ 1.110 +class SkRectClipBlitter : public SkBlitter { 1.111 +public: 1.112 + void init(SkBlitter* blitter, const SkIRect& clipRect) { 1.113 + SkASSERT(!clipRect.isEmpty()); 1.114 + fBlitter = blitter; 1.115 + fClipRect = clipRect; 1.116 + } 1.117 + 1.118 + virtual void blitH(int x, int y, int width) SK_OVERRIDE; 1.119 + virtual void blitAntiH(int x, int y, const SkAlpha[], 1.120 + const int16_t runs[]) SK_OVERRIDE; 1.121 + virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE; 1.122 + virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE; 1.123 + virtual void blitAntiRect(int x, int y, int width, int height, 1.124 + SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE; 1.125 + virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE; 1.126 + virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE; 1.127 + 1.128 +private: 1.129 + SkBlitter* fBlitter; 1.130 + SkIRect fClipRect; 1.131 +}; 1.132 + 1.133 +/** Wraps another (real) blitter, and ensures that the real blitter is only 1.134 + called with coordinates that have been clipped by the specified clipRgn. 1.135 + This means the caller need not perform the clipping ahead of time. 1.136 +*/ 1.137 +class SkRgnClipBlitter : public SkBlitter { 1.138 +public: 1.139 + void init(SkBlitter* blitter, const SkRegion* clipRgn) { 1.140 + SkASSERT(clipRgn && !clipRgn->isEmpty()); 1.141 + fBlitter = blitter; 1.142 + fRgn = clipRgn; 1.143 + } 1.144 + 1.145 + virtual void blitH(int x, int y, int width) SK_OVERRIDE; 1.146 + virtual void blitAntiH(int x, int y, const SkAlpha[], 1.147 + const int16_t runs[]) SK_OVERRIDE; 1.148 + virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE; 1.149 + virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE; 1.150 + virtual void blitAntiRect(int x, int y, int width, int height, 1.151 + SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE; 1.152 + virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE; 1.153 + virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE; 1.154 + 1.155 +private: 1.156 + SkBlitter* fBlitter; 1.157 + const SkRegion* fRgn; 1.158 +}; 1.159 + 1.160 +/** Factory to set up the appropriate most-efficient wrapper blitter 1.161 + to apply a clip. Returns a pointer to a member, so lifetime must 1.162 + be managed carefully. 1.163 +*/ 1.164 +class SkBlitterClipper { 1.165 +public: 1.166 + SkBlitter* apply(SkBlitter* blitter, const SkRegion* clip, 1.167 + const SkIRect* bounds = NULL); 1.168 + 1.169 +private: 1.170 + SkNullBlitter fNullBlitter; 1.171 + SkRectClipBlitter fRectBlitter; 1.172 + SkRgnClipBlitter fRgnBlitter; 1.173 +}; 1.174 + 1.175 +#endif