blob: 5c6a4152fc285dd9c92fedc65f8e727f8bb920dc [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2007 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
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#ifndef SkBitmapProcState_DEFINED
11#define SkBitmapProcState_DEFINED
12
13#include "SkBitmap.h"
humper@google.comb0889472013-07-09 21:37:14 +000014#include "SkBitmapFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkMatrix.h"
16
reed@google.com77613a52012-03-14 12:21:17 +000017#define FractionalInt_IS_64BIT
reed@google.com4bc0a9d2012-03-07 21:47:41 +000018
19#ifdef FractionalInt_IS_64BIT
20 typedef SkFixed48 SkFractionalInt;
21 #define SkScalarToFractionalInt(x) SkScalarToFixed48(x)
22 #define SkFractionalIntToFixed(x) SkFixed48ToFixed(x)
reed@google.com411215a2012-03-08 20:13:46 +000023 #define SkFixedToFractionalInt(x) SkFixedToFixed48(x)
reed@google.com4bc0a9d2012-03-07 21:47:41 +000024 #define SkFractionalIntToInt(x) SkFixed48ToInt(x)
25#else
26 typedef SkFixed SkFractionalInt;
27 #define SkScalarToFractionalInt(x) SkScalarToFixed(x)
28 #define SkFractionalIntToFixed(x) (x)
reed@google.com411215a2012-03-08 20:13:46 +000029 #define SkFixedToFractionalInt(x) (x)
reed@google.com4bc0a9d2012-03-07 21:47:41 +000030 #define SkFractionalIntToInt(x) ((x) >> 16)
31#endif
32
reed@android.com8a1c16f2008-12-17 15:59:43 +000033class SkPaint;
34
35struct SkBitmapProcState {
36
commit-bot@chromium.orge81d1bc2013-07-10 20:42:12 +000037 SkBitmapProcState(): fBitmapFilter(NULL) {}
38 ~SkBitmapProcState() {
39 SkDELETE(fBitmapFilter);
40 }
41
reed@android.com7a99eb12009-07-16 01:13:14 +000042 typedef void (*ShaderProc32)(const SkBitmapProcState&, int x, int y,
43 SkPMColor[], int count);
44
45 typedef void (*ShaderProc16)(const SkBitmapProcState&, int x, int y,
46 uint16_t[], int count);
47
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 typedef void (*MatrixProc)(const SkBitmapProcState&,
49 uint32_t bitmapXY[],
50 int count,
51 int x, int y);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 typedef void (*SampleProc32)(const SkBitmapProcState&,
54 const uint32_t[],
55 int count,
56 SkPMColor colors[]);
57
58 typedef void (*SampleProc16)(const SkBitmapProcState&,
59 const uint32_t[],
60 int count,
61 uint16_t colors[]);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000062
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 typedef U16CPU (*FixedTileProc)(SkFixed); // returns 0..0xFFFF
reed@google.comf444e8c2012-03-09 19:59:46 +000064 typedef U16CPU (*FixedTileLowBitsProc)(SkFixed, int); // returns 0..0xF
reed@android.com07d1f002009-08-13 19:35:48 +000065 typedef U16CPU (*IntTileProc)(int value, int count); // returns 0..count-1
reed@android.com7a99eb12009-07-16 01:13:14 +000066
reed@android.comeef375b2009-08-03 14:45:45 +000067 const SkBitmap* fBitmap; // chooseProcs - orig or mip
68 const SkMatrix* fInvMatrix; // chooseProcs
69 SkMatrix::MapXYProc fInvProc; // chooseProcs
70
reed@google.com4bc0a9d2012-03-07 21:47:41 +000071 SkFractionalInt fInvSxFractionalInt;
reed@google.com411215a2012-03-08 20:13:46 +000072 SkFractionalInt fInvKyFractionalInt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 FixedTileProc fTileProcX; // chooseProcs
75 FixedTileProc fTileProcY; // chooseProcs
reed@google.comf444e8c2012-03-09 19:59:46 +000076 FixedTileLowBitsProc fTileLowBitsProcX; // chooseProcs
77 FixedTileLowBitsProc fTileLowBitsProcY; // chooseProcs
reed@android.com07d1f002009-08-13 19:35:48 +000078 IntTileProc fIntTileProcY; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 SkFixed fFilterOneX;
80 SkFixed fFilterOneY;
81
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 SkPMColor fPaintPMColor; // chooseProcs - A8 config
reed@android.comeef375b2009-08-03 14:45:45 +000083 SkFixed fInvSx; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 SkFixed fInvKy; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 uint16_t fAlphaScale; // chooseProcs
86 uint8_t fInvType; // chooseProcs
87 uint8_t fTileModeX; // CONSTRUCTOR
88 uint8_t fTileModeY; // CONSTRUCTOR
89 SkBool8 fDoFilter; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
reed@android.comc9a1d4b2009-08-03 15:05:55 +000091 /** Platforms implement this, and can optionally overwrite only the
92 following fields:
93
94 fShaderProc32
95 fShaderProc16
96 fMatrixProc
97 fSampleProc32
98 fSampleProc32
99
100 They will already have valid function pointers, so a platform that does
101 not have an accelerated version can just leave that field as is. A valid
102 implementation can do nothing (see SkBitmapProcState_opts_none.cpp)
103 */
104 void platformProcs();
105
reed@google.com4c69a062013-05-23 20:11:56 +0000106
reed@android.com258cb222010-04-14 13:36:33 +0000107 /** Given the byte size of the index buffer to be passed to the matrix proc,
108 return the maximum number of resulting pixels that can be computed
109 (i.e. the number of SkPMColor values to be written by the sample proc).
110 This routine takes into account that filtering and scale-vs-affine
111 affect the amount of buffer space needed.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000112
reed@android.com4c128c42009-08-14 13:54:37 +0000113 Only valid to call after chooseProcs (setContext) has been called. It is
114 safe to call this inside the shader's shadeSpan() method.
115 */
116 int maxCountForBufferSize(size_t bufferSize) const;
117
reed@google.com9fe287b2012-03-27 15:54:28 +0000118 // If a shader proc is present, then the corresponding matrix/sample procs
119 // are ignored
120 ShaderProc32 getShaderProc32() const { return fShaderProc32; }
121 ShaderProc16 getShaderProc16() const { return fShaderProc16; }
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +0000122
humper@google.comb0889472013-07-09 21:37:14 +0000123 SkBitmapFilter* getBitmapFilter() const { return fBitmapFilter; }
reed@google.com9fe287b2012-03-27 15:54:28 +0000124
125#ifdef SK_DEBUG
126 MatrixProc getMatrixProc() const;
127#else
128 MatrixProc getMatrixProc() const { return fMatrixProc; }
129#endif
130 SampleProc32 getSampleProc32() const { return fSampleProc32; }
131 SampleProc16 getSampleProc16() const { return fSampleProc16; }
132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133private:
reed@android.comeef375b2009-08-03 14:45:45 +0000134 friend class SkBitmapProcShader;
135
reed@google.com9fe287b2012-03-27 15:54:28 +0000136 ShaderProc32 fShaderProc32; // chooseProcs
137 ShaderProc16 fShaderProc16; // chooseProcs
138 // These are used if the shaderproc is NULL
139 MatrixProc fMatrixProc; // chooseProcs
140 SampleProc32 fSampleProc32; // chooseProcs
141 SampleProc16 fSampleProc16; // chooseProcs
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000142
reed@android.comeef375b2009-08-03 14:45:45 +0000143 SkMatrix fUnitInvMatrix; // chooseProcs
144 SkBitmap fOrigBitmap; // CONSTRUCTOR
145 SkBitmap fMipBitmap;
146
reed@android.com07d1f002009-08-13 19:35:48 +0000147 MatrixProc chooseMatrixProc(bool trivial_matrix);
reed@android.comeef375b2009-08-03 14:45:45 +0000148 bool chooseProcs(const SkMatrix& inv, const SkPaint&);
reed@google.com9a4c7462012-10-12 18:21:37 +0000149 ShaderProc32 chooseShaderProc32();
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +0000150
humper@google.comb0889472013-07-09 21:37:14 +0000151 void buildFilterCoefficients(SkFixed dst[4], float t) const;
152 SkBitmapFilter *fBitmapFilter;
skia.committer@gmail.comb0a327e2012-11-21 02:02:25 +0000153
humper@google.comb0889472013-07-09 21:37:14 +0000154 ShaderProc32 chooseBitmapFilterProc(const SkPaint &paint);
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +0000155
reed@google.com6bb92bc2012-11-20 19:45:16 +0000156 // Return false if we failed to setup for fast translate (e.g. overflow)
157 bool setupForTranslate();
reed@google.com9fe287b2012-03-27 15:54:28 +0000158
159#ifdef SK_DEBUG
160 static void DebugMatrixProc(const SkBitmapProcState&,
161 uint32_t[], int count, int x, int y);
162#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163};
164
reed@android.com07d1f002009-08-13 19:35:48 +0000165/* Macros for packing and unpacking pairs of 16bit values in a 32bit uint.
166 Used to allow access to a stream of uint16_t either one at a time, or
167 2 at a time by unpacking a uint32_t
168 */
169#ifdef SK_CPU_BENDIAN
170 #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec))
171 #define UNPACK_PRIMARY_SHORT(packed) ((uint32_t)(packed) >> 16)
172 #define UNPACK_SECONDARY_SHORT(packed) ((packed) & 0xFFFF)
173#else
174 #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16))
175 #define UNPACK_PRIMARY_SHORT(packed) ((packed) & 0xFFFF)
176 #define UNPACK_SECONDARY_SHORT(packed) ((uint32_t)(packed) >> 16)
177#endif
178
179#ifdef SK_DEBUG
180 static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) {
181 SkASSERT((uint16_t)pri == pri);
182 SkASSERT((uint16_t)sec == sec);
183 return PACK_TWO_SHORTS(pri, sec);
184 }
185#else
186 #define pack_two_shorts(pri, sec) PACK_TWO_SHORTS(pri, sec)
187#endif
188
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +0000189// These functions are generated via macros, but are exposed here so that
190// platformProcs may test for them by name.
191void S32_opaque_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
192 int count, SkPMColor colors[]);
senorblanco@chromium.orgf3f0bd72009-12-10 22:46:31 +0000193void S32_alpha_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
194 int count, SkPMColor colors[]);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000195void S32_opaque_D32_filter_DXDY(const SkBitmapProcState& s,
196 const uint32_t xy[], int count, SkPMColor colors[]);
197void S32_alpha_D32_filter_DXDY(const SkBitmapProcState& s,
tomhudson@google.comae29b882012-03-06 14:59:04 +0000198 const uint32_t xy[], int count, SkPMColor colors[]);
tomhudson@google.com06a73132012-02-22 18:30:43 +0000199void ClampX_ClampY_filter_scale(const SkBitmapProcState& s, uint32_t xy[],
200 int count, int x, int y);
201void ClampX_ClampY_nofilter_scale(const SkBitmapProcState& s, uint32_t xy[],
202 int count, int x, int y);
tomhudson@google.com5efaf262012-02-28 15:41:49 +0000203void ClampX_ClampY_filter_affine(const SkBitmapProcState& s,
204 uint32_t xy[], int count, int x, int y);
205void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s,
206 uint32_t xy[], int count, int x, int y);
reed@google.com78662282012-07-24 13:53:23 +0000207void S32_D16_filter_DX(const SkBitmapProcState& s,
208 const uint32_t* xy, int count, uint16_t* colors);
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +0000209
humper@google.comb0889472013-07-09 21:37:14 +0000210void highQualityFilter_ScaleOnly(const SkBitmapProcState &s, int x, int y,
211 SkPMColor *SK_RESTRICT colors, int count);
212void highQualityFilter(const SkBitmapProcState &s, int x, int y,
213 SkPMColor *SK_RESTRICT colors, int count);
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +0000214
humper@google.comb0889472013-07-09 21:37:14 +0000215
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216#endif