1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkBitmapProcShader.h"
9
10 #include "SkArenaAlloc.h"
11 #include "SkBitmapProcState.h"
12 #include "SkBitmapProvider.h"
13 #include "SkXfermodePriv.h"
14
only_scale_and_translate(const SkMatrix & matrix)15 static bool only_scale_and_translate(const SkMatrix& matrix) {
16 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
17 return (matrix.getType() & ~mask) == 0;
18 }
19
20 class BitmapProcInfoContext : public SkShader::Context {
21 public:
22 // The info has been allocated elsewhere, but we are responsible for calling its destructor.
BitmapProcInfoContext(const SkShader & shader,const SkShader::ContextRec & rec,SkBitmapProcInfo * info)23 BitmapProcInfoContext(const SkShader& shader, const SkShader::ContextRec& rec,
24 SkBitmapProcInfo* info)
25 : INHERITED(shader, rec)
26 , fInfo(info)
27 {
28 fFlags = 0;
29 if (fInfo->fPixmap.isOpaque() && (255 == this->getPaintAlpha())) {
30 fFlags |= SkShader::kOpaqueAlpha_Flag;
31 }
32
33 if (1 == fInfo->fPixmap.height() && only_scale_and_translate(this->getTotalInverse())) {
34 fFlags |= SkShader::kConstInY32_Flag;
35 }
36 }
37
getFlags() const38 uint32_t getFlags() const override { return fFlags; }
39
40 private:
41 SkBitmapProcInfo* fInfo;
42 uint32_t fFlags;
43
44 typedef SkShader::Context INHERITED;
45 };
46
47 ///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 class BitmapProcShaderContext : public BitmapProcInfoContext {
50 public:
BitmapProcShaderContext(const SkShader & shader,const SkShader::ContextRec & rec,SkBitmapProcState * state)51 BitmapProcShaderContext(const SkShader& shader, const SkShader::ContextRec& rec,
52 SkBitmapProcState* state)
53 : INHERITED(shader, rec, state)
54 , fState(state)
55 {}
56
shadeSpan(int x,int y,SkPMColor dstC[],int count)57 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override {
58 const SkBitmapProcState& state = *fState;
59 if (state.getShaderProc32()) {
60 state.getShaderProc32()(&state, x, y, dstC, count);
61 return;
62 }
63
64 const int BUF_MAX = 128;
65 uint32_t buffer[BUF_MAX];
66 SkBitmapProcState::MatrixProc mproc = state.getMatrixProc();
67 SkBitmapProcState::SampleProc32 sproc = state.getSampleProc32();
68 const int max = state.maxCountForBufferSize(sizeof(buffer[0]) * BUF_MAX);
69
70 SkASSERT(state.fPixmap.addr());
71
72 for (;;) {
73 int n = SkTMin(count, max);
74 SkASSERT(n > 0 && n < BUF_MAX*2);
75 mproc(state, buffer, n, x, y);
76 sproc(state, buffer, n, dstC);
77
78 if ((count -= n) == 0) {
79 break;
80 }
81 SkASSERT(count > 0);
82 x += n;
83 dstC += n;
84 }
85 }
86
asAShadeProc(void ** ctx)87 ShadeProc asAShadeProc(void** ctx) override {
88 if (fState->getShaderProc32()) {
89 *ctx = fState;
90 return (ShadeProc)fState->getShaderProc32();
91 }
92 return nullptr;
93 }
94
95 private:
96 SkBitmapProcState* fState;
97
98 typedef BitmapProcInfoContext INHERITED;
99 };
100
101 ///////////////////////////////////////////////////////////////////////////////////////////////////
102 #include "SkLinearBitmapPipeline.h"
103 #include "SkPM4f.h"
104
105 class LinearPipelineContext : public BitmapProcInfoContext {
106 public:
LinearPipelineContext(const SkShader & shader,const SkShader::ContextRec & rec,SkBitmapProcInfo * info,SkArenaAlloc * alloc)107 LinearPipelineContext(const SkShader& shader, const SkShader::ContextRec& rec,
108 SkBitmapProcInfo* info, SkArenaAlloc* alloc)
109 : INHERITED(shader, rec, info), fAllocator{alloc}
110 {
111 // Save things off in case we need to build a blitter pipeline.
112 fSrcPixmap = info->fPixmap;
113 fAlpha = SkColorGetA(info->fPaintColor) / 255.0f;
114 fFilterQuality = info->fFilterQuality;
115 fMatrixTypeMask = info->fRealInvMatrix.getType();
116
117 fShaderPipeline = alloc->make<SkLinearBitmapPipeline>(
118 info->fRealInvMatrix, info->fFilterQuality,
119 info->fTileModeX, info->fTileModeY,
120 info->fPaintColor,
121 info->fPixmap,
122 fAllocator);
123
124 // To implement the old shadeSpan entry-point, we need to efficiently convert our native
125 // floats into SkPMColor. The SkXfermode::D32Procs do exactly that.
126 //
127 fSrcModeProc = SkXfermode::GetD32Proc(SkBlendMode::kSrc, 0);
128 }
129
shadeSpan4f(int x,int y,SkPM4f dstC[],int count)130 void shadeSpan4f(int x, int y, SkPM4f dstC[], int count) override {
131 fShaderPipeline->shadeSpan4f(x, y, dstC, count);
132 }
133
shadeSpan(int x,int y,SkPMColor dstC[],int count)134 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override {
135 const int N = 128;
136 SkPM4f tmp[N];
137
138 while (count > 0) {
139 const int n = SkTMin(count, N);
140 fShaderPipeline->shadeSpan4f(x, y, tmp, n);
141 fSrcModeProc(SkBlendMode::kSrc, dstC, tmp, n, nullptr);
142 dstC += n;
143 x += n;
144 count -= n;
145 }
146 }
147
onChooseBlitProcs(const SkImageInfo & dstInfo,BlitState * state)148 bool onChooseBlitProcs(const SkImageInfo& dstInfo, BlitState* state) override {
149 if ((fBlitterPipeline = SkLinearBitmapPipeline::ClonePipelineForBlitting(
150 *fShaderPipeline,
151 fMatrixTypeMask,
152 fFilterQuality, fSrcPixmap,
153 fAlpha, state->fMode, dstInfo, fAllocator)))
154 {
155 state->fStorage[0] = fBlitterPipeline;
156 state->fBlitBW = &LinearPipelineContext::ForwardToPipeline;
157
158 return true;
159 }
160
161 return false;
162 }
163
ForwardToPipeline(BlitState * state,int x,int y,const SkPixmap & dst,int count)164 static void ForwardToPipeline(BlitState* state, int x, int y, const SkPixmap& dst, int count) {
165 SkLinearBitmapPipeline* pipeline = static_cast<SkLinearBitmapPipeline*>(state->fStorage[0]);
166 void* addr = dst.writable_addr32(x, y);
167 pipeline->blitSpan(x, y, addr, count);
168 }
169
170 private:
171 // Store the allocator from the context creation incase we are asked to build a blitter.
172 SkArenaAlloc* fAllocator;
173 SkLinearBitmapPipeline* fShaderPipeline;
174 SkLinearBitmapPipeline* fBlitterPipeline;
175 SkXfermode::D32Proc fSrcModeProc;
176 SkPixmap fSrcPixmap;
177 float fAlpha;
178 SkMatrix::TypeMask fMatrixTypeMask;
179 SkFilterQuality fFilterQuality;
180
181 typedef BitmapProcInfoContext INHERITED;
182 };
183
184 ///////////////////////////////////////////////////////////////////////////////////////////////////
185
choose_linear_pipeline(const SkShader::ContextRec & rec,const SkImageInfo & srcInfo)186 static bool choose_linear_pipeline(const SkShader::ContextRec& rec, const SkImageInfo& srcInfo) {
187 // If we get here, we can reasonably use either context, respect the caller's preference
188 //
189 bool needsPremul = srcInfo.alphaType() == kUnpremul_SkAlphaType;
190 bool needsSwizzle = srcInfo.bytesPerPixel() == 4 && srcInfo.colorType() != kN32_SkColorType;
191 return SkShader::ContextRec::kPM4f_DstType == rec.fPreferredDstType
192 || needsPremul || needsSwizzle;
193 }
194
ContextSize(const ContextRec & rec,const SkImageInfo & srcInfo)195 size_t SkBitmapProcLegacyShader::ContextSize(const ContextRec& rec, const SkImageInfo& srcInfo) {
196 size_t size0 = sizeof(BitmapProcShaderContext) + sizeof(SkBitmapProcState);
197 size_t size1 = sizeof(LinearPipelineContext) + sizeof(SkBitmapProcInfo);
198 size_t s = SkTMax(size0, size1);
199 return s;
200 }
201
MakeContext(const SkShader & shader,TileMode tmx,TileMode tmy,const SkBitmapProvider & provider,const ContextRec & rec,SkArenaAlloc * alloc)202 SkShader::Context* SkBitmapProcLegacyShader::MakeContext(
203 const SkShader& shader, TileMode tmx, TileMode tmy,
204 const SkBitmapProvider& provider, const ContextRec& rec, SkArenaAlloc* alloc)
205 {
206 SkMatrix totalInverse;
207 // Do this first, so we know the matrix can be inverted.
208 if (!shader.computeTotalInverse(rec, &totalInverse)) {
209 return nullptr;
210 }
211
212 // Decide if we can/want to use the new linear pipeline
213 bool useLinearPipeline = choose_linear_pipeline(rec, provider.info());
214
215 if (useLinearPipeline) {
216 SkBitmapProcInfo* info = alloc->make<SkBitmapProcInfo>(provider, tmx, tmy);
217 if (!info->init(totalInverse, *rec.fPaint)) {
218 return nullptr;
219 }
220
221 return alloc->make<LinearPipelineContext>(shader, rec, info, alloc);
222 } else {
223 SkBitmapProcState* state = alloc->make<SkBitmapProcState>(provider, tmx, tmy);
224 if (!state->setup(totalInverse, *rec.fPaint)) {
225 return nullptr;
226 }
227 return alloc->make<BitmapProcShaderContext>(shader, rec, state);
228 }
229 }
230