blob: d5a354e36aca7b9a03579ae99b252647ca4cb53a [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"
reed@google.comcee9dcb2013-09-13 16:04:49 +000016#include "SkPaint.h"
reed@google.com602a1d72013-07-23 19:13:54 +000017#include "SkScaledImageCache.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
reed@google.com77613a52012-03-14 12:21:17 +000019#define FractionalInt_IS_64BIT
reed@google.com4bc0a9d2012-03-07 21:47:41 +000020
21#ifdef FractionalInt_IS_64BIT
22 typedef SkFixed48 SkFractionalInt;
23 #define SkScalarToFractionalInt(x) SkScalarToFixed48(x)
24 #define SkFractionalIntToFixed(x) SkFixed48ToFixed(x)
reed@google.com411215a2012-03-08 20:13:46 +000025 #define SkFixedToFractionalInt(x) SkFixedToFixed48(x)
reed@google.com4bc0a9d2012-03-07 21:47:41 +000026 #define SkFractionalIntToInt(x) SkFixed48ToInt(x)
27#else
28 typedef SkFixed SkFractionalInt;
29 #define SkScalarToFractionalInt(x) SkScalarToFixed(x)
30 #define SkFractionalIntToFixed(x) (x)
reed@google.com411215a2012-03-08 20:13:46 +000031 #define SkFixedToFractionalInt(x) (x)
reed@google.com4bc0a9d2012-03-07 21:47:41 +000032 #define SkFractionalIntToInt(x) ((x) >> 16)
33#endif
34
reed@android.com8a1c16f2008-12-17 15:59:43 +000035class SkPaint;
humper@google.comdd850882013-07-19 20:52:12 +000036struct SkConvolutionProcs;
reed@android.com8a1c16f2008-12-17 15:59:43 +000037
38struct SkBitmapProcState {
skia.committer@gmail.comfa1bd5f2013-07-13 07:00:56 +000039
reed@google.com602a1d72013-07-23 19:13:54 +000040 SkBitmapProcState(): fScaledCacheID(NULL), fBitmapFilter(NULL) {}
reed@google.com1e182252013-07-24 20:10:42 +000041 ~SkBitmapProcState();
commit-bot@chromium.orge81d1bc2013-07-10 20:42:12 +000042
reed@android.com7a99eb12009-07-16 01:13:14 +000043 typedef void (*ShaderProc32)(const SkBitmapProcState&, int x, int y,
44 SkPMColor[], int count);
45
46 typedef void (*ShaderProc16)(const SkBitmapProcState&, int x, int y,
47 uint16_t[], int count);
48
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 typedef void (*MatrixProc)(const SkBitmapProcState&,
50 uint32_t bitmapXY[],
51 int count,
52 int x, int y);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000053
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 typedef void (*SampleProc32)(const SkBitmapProcState&,
55 const uint32_t[],
56 int count,
57 SkPMColor colors[]);
58
59 typedef void (*SampleProc16)(const SkBitmapProcState&,
60 const uint32_t[],
61 int count,
62 uint16_t colors[]);
skia.committer@gmail.com1f3c7382013-07-20 07:00:58 +000063
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 typedef U16CPU (*FixedTileProc)(SkFixed); // returns 0..0xFFFF
reed@google.comf444e8c2012-03-09 19:59:46 +000065 typedef U16CPU (*FixedTileLowBitsProc)(SkFixed, int); // returns 0..0xF
reed@android.com07d1f002009-08-13 19:35:48 +000066 typedef U16CPU (*IntTileProc)(int value, int count); // returns 0..count-1
reed@android.com7a99eb12009-07-16 01:13:14 +000067
humper@google.com9c96d4b2013-07-14 01:44:59 +000068 const SkBitmap* fBitmap; // chooseProcs - orig or scaled
69 SkMatrix fInvMatrix; // chooseProcs
reed@android.comeef375b2009-08-03 14:45:45 +000070 SkMatrix::MapXYProc fInvProc; // chooseProcs
71
reed@google.com4bc0a9d2012-03-07 21:47:41 +000072 SkFractionalInt fInvSxFractionalInt;
reed@google.com411215a2012-03-08 20:13:46 +000073 SkFractionalInt fInvKyFractionalInt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 FixedTileProc fTileProcX; // chooseProcs
76 FixedTileProc fTileProcY; // chooseProcs
reed@google.comf444e8c2012-03-09 19:59:46 +000077 FixedTileLowBitsProc fTileLowBitsProcX; // chooseProcs
78 FixedTileLowBitsProc fTileLowBitsProcY; // chooseProcs
reed@android.com07d1f002009-08-13 19:35:48 +000079 IntTileProc fIntTileProcY; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 SkFixed fFilterOneX;
81 SkFixed fFilterOneY;
skia.committer@gmail.com1f3c7382013-07-20 07:00:58 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 SkPMColor fPaintPMColor; // chooseProcs - A8 config
reed@android.comeef375b2009-08-03 14:45:45 +000084 SkFixed fInvSx; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 SkFixed fInvKy; // chooseProcs
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 uint16_t fAlphaScale; // chooseProcs
87 uint8_t fInvType; // chooseProcs
88 uint8_t fTileModeX; // CONSTRUCTOR
89 uint8_t fTileModeY; // CONSTRUCTOR
reed@google.com9cfc83c2013-07-22 17:18:18 +000090 uint8_t fFilterLevel; // chooseProcs
humper@google.com9c96d4b2013-07-14 01:44:59 +000091
commit-bot@chromium.org6e5671d2014-04-23 16:16:55 +000092 /** The shader will let us know when we can release some of our resources
93 * like scaled bitmaps.
94 */
95
96 void endContext();
97
reed@android.comc9a1d4b2009-08-03 15:05:55 +000098 /** Platforms implement this, and can optionally overwrite only the
99 following fields:
100
101 fShaderProc32
102 fShaderProc16
103 fMatrixProc
104 fSampleProc32
105 fSampleProc32
106
107 They will already have valid function pointers, so a platform that does
108 not have an accelerated version can just leave that field as is. A valid
109 implementation can do nothing (see SkBitmapProcState_opts_none.cpp)
110 */
111 void platformProcs();
skia.committer@gmail.com1f3c7382013-07-20 07:00:58 +0000112
humper@google.com138ebc32013-07-19 20:20:04 +0000113 /** Platforms can also optionally overwrite the convolution functions
114 if we have SIMD versions of them.
115 */
skia.committer@gmail.com1f3c7382013-07-20 07:00:58 +0000116
reed@google.comfed04b32013-09-05 20:31:17 +0000117 void platformConvolutionProcs(SkConvolutionProcs*);
reed@google.com4c69a062013-05-23 20:11:56 +0000118
reed@android.com258cb222010-04-14 13:36:33 +0000119 /** Given the byte size of the index buffer to be passed to the matrix proc,
120 return the maximum number of resulting pixels that can be computed
121 (i.e. the number of SkPMColor values to be written by the sample proc).
122 This routine takes into account that filtering and scale-vs-affine
123 affect the amount of buffer space needed.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000124
reed@android.com4c128c42009-08-14 13:54:37 +0000125 Only valid to call after chooseProcs (setContext) has been called. It is
126 safe to call this inside the shader's shadeSpan() method.
127 */
128 int maxCountForBufferSize(size_t bufferSize) const;
129
reed@google.com9fe287b2012-03-27 15:54:28 +0000130 // If a shader proc is present, then the corresponding matrix/sample procs
131 // are ignored
132 ShaderProc32 getShaderProc32() const { return fShaderProc32; }
133 ShaderProc16 getShaderProc16() const { return fShaderProc16; }
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +0000134
humper@google.comb0889472013-07-09 21:37:14 +0000135 SkBitmapFilter* getBitmapFilter() const { return fBitmapFilter; }
reed@google.com9fe287b2012-03-27 15:54:28 +0000136
137#ifdef SK_DEBUG
138 MatrixProc getMatrixProc() const;
139#else
140 MatrixProc getMatrixProc() const { return fMatrixProc; }
141#endif
142 SampleProc32 getSampleProc32() const { return fSampleProc32; }
143 SampleProc16 getSampleProc16() const { return fSampleProc16; }
144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145private:
reed@android.comeef375b2009-08-03 14:45:45 +0000146 friend class SkBitmapProcShader;
147
reed@google.com9fe287b2012-03-27 15:54:28 +0000148 ShaderProc32 fShaderProc32; // chooseProcs
149 ShaderProc16 fShaderProc16; // chooseProcs
150 // These are used if the shaderproc is NULL
151 MatrixProc fMatrixProc; // chooseProcs
152 SampleProc32 fSampleProc32; // chooseProcs
153 SampleProc16 fSampleProc16; // chooseProcs
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000154
reed@android.comeef375b2009-08-03 14:45:45 +0000155 SkBitmap fOrigBitmap; // CONSTRUCTOR
humper@google.com9c96d4b2013-07-14 01:44:59 +0000156 SkBitmap fScaledBitmap; // chooseProcs
reed@android.comeef375b2009-08-03 14:45:45 +0000157
reed@google.com602a1d72013-07-23 19:13:54 +0000158 SkScaledImageCache::ID* fScaledCacheID;
159
reed@android.com07d1f002009-08-13 19:35:48 +0000160 MatrixProc chooseMatrixProc(bool trivial_matrix);
reed@android.comeef375b2009-08-03 14:45:45 +0000161 bool chooseProcs(const SkMatrix& inv, const SkPaint&);
reed@google.com9a4c7462012-10-12 18:21:37 +0000162 ShaderProc32 chooseShaderProc32();
skia.committer@gmail.comfa1bd5f2013-07-13 07:00:56 +0000163
reed@google.comcee9dcb2013-09-13 16:04:49 +0000164 // returns false if we did not try to scale the image. In that case, we
165 // will need to "lock" its pixels some other way.
166 bool possiblyScaleImage();
167
168 // returns false if we failed to "lock" the pixels at all. Typically this
169 // means we have to abort the shader.
170 bool lockBaseBitmap();
humper@google.com9c96d4b2013-07-14 01:44:59 +0000171
humper@google.com138ebc32013-07-19 20:20:04 +0000172 SkBitmapFilter* fBitmapFilter;
skia.committer@gmail.comb0a327e2012-11-21 02:02:25 +0000173
mtklein@google.com0dc546c2013-08-26 16:21:35 +0000174 // If supported, sets fShaderProc32 and fShaderProc16 and returns true,
175 // otherwise returns false.
176 bool setBitmapFilterProcs();
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +0000177
reed@google.com6bb92bc2012-11-20 19:45:16 +0000178 // Return false if we failed to setup for fast translate (e.g. overflow)
179 bool setupForTranslate();
reed@google.com9fe287b2012-03-27 15:54:28 +0000180
181#ifdef SK_DEBUG
182 static void DebugMatrixProc(const SkBitmapProcState&,
183 uint32_t[], int count, int x, int y);
184#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185};
186
reed@android.com07d1f002009-08-13 19:35:48 +0000187/* Macros for packing and unpacking pairs of 16bit values in a 32bit uint.
188 Used to allow access to a stream of uint16_t either one at a time, or
189 2 at a time by unpacking a uint32_t
190 */
191#ifdef SK_CPU_BENDIAN
192 #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec))
193 #define UNPACK_PRIMARY_SHORT(packed) ((uint32_t)(packed) >> 16)
194 #define UNPACK_SECONDARY_SHORT(packed) ((packed) & 0xFFFF)
195#else
196 #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16))
197 #define UNPACK_PRIMARY_SHORT(packed) ((packed) & 0xFFFF)
198 #define UNPACK_SECONDARY_SHORT(packed) ((uint32_t)(packed) >> 16)
199#endif
200
201#ifdef SK_DEBUG
202 static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) {
203 SkASSERT((uint16_t)pri == pri);
204 SkASSERT((uint16_t)sec == sec);
205 return PACK_TWO_SHORTS(pri, sec);
206 }
207#else
208 #define pack_two_shorts(pri, sec) PACK_TWO_SHORTS(pri, sec)
209#endif
210
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +0000211// These functions are generated via macros, but are exposed here so that
212// platformProcs may test for them by name.
213void S32_opaque_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
214 int count, SkPMColor colors[]);
senorblanco@chromium.orgf3f0bd72009-12-10 22:46:31 +0000215void S32_alpha_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
216 int count, SkPMColor colors[]);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000217void S32_opaque_D32_filter_DXDY(const SkBitmapProcState& s,
mtklein@google.com0dc546c2013-08-26 16:21:35 +0000218 const uint32_t xy[], int count, SkPMColor colors[]);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000219void S32_alpha_D32_filter_DXDY(const SkBitmapProcState& s,
mtklein@google.com0dc546c2013-08-26 16:21:35 +0000220 const uint32_t xy[], int count, SkPMColor colors[]);
tomhudson@google.com06a73132012-02-22 18:30:43 +0000221void ClampX_ClampY_filter_scale(const SkBitmapProcState& s, uint32_t xy[],
222 int count, int x, int y);
223void ClampX_ClampY_nofilter_scale(const SkBitmapProcState& s, uint32_t xy[],
224 int count, int x, int y);
tomhudson@google.com5efaf262012-02-28 15:41:49 +0000225void ClampX_ClampY_filter_affine(const SkBitmapProcState& s,
226 uint32_t xy[], int count, int x, int y);
227void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s,
228 uint32_t xy[], int count, int x, int y);
reed@google.com78662282012-07-24 13:53:23 +0000229void S32_D16_filter_DX(const SkBitmapProcState& s,
mtklein@google.com0dc546c2013-08-26 16:21:35 +0000230 const uint32_t* xy, int count, uint16_t* colors);
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +0000231
mtklein@google.com0dc546c2013-08-26 16:21:35 +0000232void highQualityFilter32(const SkBitmapProcState &s, int x, int y,
233 SkPMColor *SK_RESTRICT colors, int count);
234void highQualityFilter16(const SkBitmapProcState &s, int x, int y,
235 uint16_t *SK_RESTRICT colors, int count);
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +0000236
humper@google.comb0889472013-07-09 21:37:14 +0000237
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238#endif