blob: 5410447d6d2d60c69bb0452c8615c7e3dbd3d33e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
bungemanf3c15b72015-08-19 11:56:48 -07007
8#include "SkBitmapProcShader.h"
Herb Derby83e939b2017-02-07 14:25:11 -05009
10#include "SkArenaAlloc.h"
bungemanf3c15b72015-08-19 11:56:48 -070011#include "SkBitmapProcState.h"
reed013e9e32015-09-15 14:46:27 -070012#include "SkBitmapProvider.h"
Mike Reedd4706732016-11-15 16:44:34 -050013#include "SkXfermodePriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
reed05a56472016-03-02 09:49:02 -080015static 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
Florin Malita4aed1382017-05-25 10:38:07 -040020class BitmapProcInfoContext : public SkShaderBase::Context {
reed05a56472016-03-02 09:49:02 -080021public:
reedd8829012016-03-04 11:07:43 -080022 // The info has been allocated elsewhere, but we are responsible for calling its destructor.
Florin Malita4aed1382017-05-25 10:38:07 -040023 BitmapProcInfoContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec,
reed05a56472016-03-02 09:49:02 -080024 SkBitmapProcInfo* info)
25 : INHERITED(shader, rec)
26 , fInfo(info)
27 {
28 fFlags = 0;
29 if (fInfo->fPixmap.isOpaque() && (255 == this->getPaintAlpha())) {
Florin Malita4aed1382017-05-25 10:38:07 -040030 fFlags |= SkShaderBase::kOpaqueAlpha_Flag;
reed05a56472016-03-02 09:49:02 -080031 }
32
33 if (1 == fInfo->fPixmap.height() && only_scale_and_translate(this->getTotalInverse())) {
Florin Malita4aed1382017-05-25 10:38:07 -040034 fFlags |= SkShaderBase::kConstInY32_Flag;
reed05a56472016-03-02 09:49:02 -080035 }
36 }
37
reed05a56472016-03-02 09:49:02 -080038 uint32_t getFlags() const override { return fFlags; }
39
40private:
41 SkBitmapProcInfo* fInfo;
42 uint32_t fFlags;
43
Florin Malita4aed1382017-05-25 10:38:07 -040044 typedef SkShaderBase::Context INHERITED;
reed05a56472016-03-02 09:49:02 -080045};
46
47///////////////////////////////////////////////////////////////////////////////////////////////////
48
49class BitmapProcShaderContext : public BitmapProcInfoContext {
50public:
Florin Malita4aed1382017-05-25 10:38:07 -040051 BitmapProcShaderContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec,
reed05a56472016-03-02 09:49:02 -080052 SkBitmapProcState* state)
53 : INHERITED(shader, rec, state)
54 , fState(state)
55 {}
56
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
87 ShadeProc asAShadeProc(void** ctx) override {
88 if (fState->getShaderProc32()) {
89 *ctx = fState;
90 return (ShadeProc)fState->getShaderProc32();
91 }
92 return nullptr;
93 }
94
95private:
96 SkBitmapProcState* fState;
97
98 typedef BitmapProcInfoContext INHERITED;
99};
100
101///////////////////////////////////////////////////////////////////////////////////////////////////
reedd8829012016-03-04 11:07:43 -0800102#include "SkLinearBitmapPipeline.h"
103#include "SkPM4f.h"
reed05a56472016-03-02 09:49:02 -0800104
reedd8829012016-03-04 11:07:43 -0800105class LinearPipelineContext : public BitmapProcInfoContext {
106public:
Florin Malita4aed1382017-05-25 10:38:07 -0400107 LinearPipelineContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec,
Herb Derbye836b782017-03-02 14:39:32 -0500108 SkBitmapProcInfo* info, SkArenaAlloc* alloc)
109 : INHERITED(shader, rec, info), fAllocator{alloc}
reedd8829012016-03-04 11:07:43 -0800110 {
herb69076fe2016-04-12 14:07:59 -0700111 // Save things off in case we need to build a blitter pipeline.
112 fSrcPixmap = info->fPixmap;
113 fAlpha = SkColorGetA(info->fPaintColor) / 255.0f;
herb69076fe2016-04-12 14:07:59 -0700114 fFilterQuality = info->fFilterQuality;
115 fMatrixTypeMask = info->fRealInvMatrix.getType();
116
Herb Derbye836b782017-03-02 14:39:32 -0500117 fShaderPipeline = alloc->make<SkLinearBitmapPipeline>(
herb57a69dc2016-05-19 14:19:23 -0700118 info->fRealInvMatrix, info->fFilterQuality,
119 info->fTileModeX, info->fTileModeY,
120 info->fPaintColor,
Herb Derbydfa492e2016-11-17 21:12:36 -0500121 info->fPixmap,
Herb Derbye836b782017-03-02 14:39:32 -0500122 fAllocator);
reedd8829012016-03-04 11:07:43 -0800123
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 //
Mike Reed6a015542016-11-09 10:38:09 -0500127 fSrcModeProc = SkXfermode::GetD32Proc(SkBlendMode::kSrc, 0);
reedd8829012016-03-04 11:07:43 -0800128 }
129
reedd8829012016-03-04 11:07:43 -0800130 void shadeSpan4f(int x, int y, SkPM4f dstC[], int count) override {
herb69076fe2016-04-12 14:07:59 -0700131 fShaderPipeline->shadeSpan4f(x, y, dstC, count);
reedd8829012016-03-04 11:07:43 -0800132 }
133
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);
herb69076fe2016-04-12 14:07:59 -0700140 fShaderPipeline->shadeSpan4f(x, y, tmp, n);
Mike Reed6a015542016-11-09 10:38:09 -0500141 fSrcModeProc(SkBlendMode::kSrc, dstC, tmp, n, nullptr);
reedd8829012016-03-04 11:07:43 -0800142 dstC += n;
143 x += n;
144 count -= n;
145 }
146 }
147
148private:
Herb Derbye836b782017-03-02 14:39:32 -0500149 // Store the allocator from the context creation incase we are asked to build a blitter.
150 SkArenaAlloc* fAllocator;
Herb Derbydfa492e2016-11-17 21:12:36 -0500151 SkLinearBitmapPipeline* fShaderPipeline;
Herb Derbydfa492e2016-11-17 21:12:36 -0500152 SkXfermode::D32Proc fSrcModeProc;
153 SkPixmap fSrcPixmap;
154 float fAlpha;
155 SkMatrix::TypeMask fMatrixTypeMask;
156 SkFilterQuality fFilterQuality;
reedd8829012016-03-04 11:07:43 -0800157
158 typedef BitmapProcInfoContext INHERITED;
159};
160
161///////////////////////////////////////////////////////////////////////////////////////////////////
162
Florin Malita4aed1382017-05-25 10:38:07 -0400163static bool choose_linear_pipeline(const SkShaderBase::ContextRec& rec, const SkImageInfo& srcInfo) {
reedd8829012016-03-04 11:07:43 -0800164 // If we get here, we can reasonably use either context, respect the caller's preference
165 //
herba4e4b792016-06-04 13:27:10 -0700166 bool needsPremul = srcInfo.alphaType() == kUnpremul_SkAlphaType;
167 bool needsSwizzle = srcInfo.bytesPerPixel() == 4 && srcInfo.colorType() != kN32_SkColorType;
Florin Malita4aed1382017-05-25 10:38:07 -0400168 return SkShaderBase::ContextRec::kPM4f_DstType == rec.fPreferredDstType
herba4e4b792016-06-04 13:27:10 -0700169 || needsPremul || needsSwizzle;
reedd8829012016-03-04 11:07:43 -0800170}
171
reed320a40d2016-08-02 06:12:06 -0700172size_t SkBitmapProcLegacyShader::ContextSize(const ContextRec& rec, const SkImageInfo& srcInfo) {
reedd8829012016-03-04 11:07:43 -0800173 size_t size0 = sizeof(BitmapProcShaderContext) + sizeof(SkBitmapProcState);
174 size_t size1 = sizeof(LinearPipelineContext) + sizeof(SkBitmapProcInfo);
reed320a40d2016-08-02 06:12:06 -0700175 size_t s = SkTMax(size0, size1);
176 return s;
reed7a4d8472015-09-15 13:33:58 -0700177}
178
Florin Malita4aed1382017-05-25 10:38:07 -0400179SkShaderBase::Context* SkBitmapProcLegacyShader::MakeContext(
180 const SkShaderBase& shader, TileMode tmx, TileMode tmy,
Herb Derby83e939b2017-02-07 14:25:11 -0500181 const SkBitmapProvider& provider, const ContextRec& rec, SkArenaAlloc* alloc)
182{
reed05a56472016-03-02 09:49:02 -0800183 SkMatrix totalInverse;
184 // Do this first, so we know the matrix can be inverted.
Florin Malita26368c32017-05-08 13:03:24 -0400185 if (!shader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &totalInverse)) {
reed05a56472016-03-02 09:49:02 -0800186 return nullptr;
187 }
188
herb6eff52a2016-03-23 09:00:33 -0700189 // Decide if we can/want to use the new linear pipeline
reedd8829012016-03-04 11:07:43 -0800190 bool useLinearPipeline = choose_linear_pipeline(rec, provider.info());
reedcd660e12016-03-03 09:36:50 -0800191
reedd8829012016-03-04 11:07:43 -0800192 if (useLinearPipeline) {
Herb Derby83e939b2017-02-07 14:25:11 -0500193 SkBitmapProcInfo* info = alloc->make<SkBitmapProcInfo>(provider, tmx, tmy);
reedd8829012016-03-04 11:07:43 -0800194 if (!info->init(totalInverse, *rec.fPaint)) {
reedd8829012016-03-04 11:07:43 -0800195 return nullptr;
196 }
herb670f01f2016-05-13 10:04:46 -0700197
Herb Derbye836b782017-03-02 14:39:32 -0500198 return alloc->make<LinearPipelineContext>(shader, rec, info, alloc);
reedd8829012016-03-04 11:07:43 -0800199 } else {
Herb Derby83e939b2017-02-07 14:25:11 -0500200 SkBitmapProcState* state = alloc->make<SkBitmapProcState>(provider, tmx, tmy);
reedd8829012016-03-04 11:07:43 -0800201 if (!state->setup(totalInverse, *rec.fPaint)) {
reedd8829012016-03-04 11:07:43 -0800202 return nullptr;
203 }
Herb Derby83e939b2017-02-07 14:25:11 -0500204 return alloc->make<BitmapProcShaderContext>(shader, rec, state);
reedd8829012016-03-04 11:07:43 -0800205 }
reed05a56472016-03-02 09:49:02 -0800206}