blob: 91697e2f1b393555eabfd75a98db96b780f9eb7b [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 Reed43e498e2017-06-08 20:26:41 -040013#include "SkPM4fPriv.h"
Mike Reedd4706732016-11-15 16:44:34 -050014#include "SkXfermodePriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
reed05a56472016-03-02 09:49:02 -080016static bool only_scale_and_translate(const SkMatrix& matrix) {
17 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
18 return (matrix.getType() & ~mask) == 0;
19}
20
Florin Malita4aed1382017-05-25 10:38:07 -040021class BitmapProcInfoContext : public SkShaderBase::Context {
reed05a56472016-03-02 09:49:02 -080022public:
reedd8829012016-03-04 11:07:43 -080023 // The info has been allocated elsewhere, but we are responsible for calling its destructor.
Florin Malita4aed1382017-05-25 10:38:07 -040024 BitmapProcInfoContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec,
reed05a56472016-03-02 09:49:02 -080025 SkBitmapProcInfo* info)
26 : INHERITED(shader, rec)
27 , fInfo(info)
28 {
29 fFlags = 0;
30 if (fInfo->fPixmap.isOpaque() && (255 == this->getPaintAlpha())) {
Florin Malita4aed1382017-05-25 10:38:07 -040031 fFlags |= SkShaderBase::kOpaqueAlpha_Flag;
reed05a56472016-03-02 09:49:02 -080032 }
33
34 if (1 == fInfo->fPixmap.height() && only_scale_and_translate(this->getTotalInverse())) {
Florin Malita4aed1382017-05-25 10:38:07 -040035 fFlags |= SkShaderBase::kConstInY32_Flag;
reed05a56472016-03-02 09:49:02 -080036 }
37 }
38
reed05a56472016-03-02 09:49:02 -080039 uint32_t getFlags() const override { return fFlags; }
40
41private:
42 SkBitmapProcInfo* fInfo;
43 uint32_t fFlags;
44
Florin Malita4aed1382017-05-25 10:38:07 -040045 typedef SkShaderBase::Context INHERITED;
reed05a56472016-03-02 09:49:02 -080046};
47
48///////////////////////////////////////////////////////////////////////////////////////////////////
49
50class BitmapProcShaderContext : public BitmapProcInfoContext {
51public:
Florin Malita4aed1382017-05-25 10:38:07 -040052 BitmapProcShaderContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec,
reed05a56472016-03-02 09:49:02 -080053 SkBitmapProcState* state)
54 : INHERITED(shader, rec, state)
55 , fState(state)
56 {}
57
58 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override {
59 const SkBitmapProcState& state = *fState;
60 if (state.getShaderProc32()) {
61 state.getShaderProc32()(&state, x, y, dstC, count);
62 return;
63 }
64
65 const int BUF_MAX = 128;
66 uint32_t buffer[BUF_MAX];
67 SkBitmapProcState::MatrixProc mproc = state.getMatrixProc();
68 SkBitmapProcState::SampleProc32 sproc = state.getSampleProc32();
69 const int max = state.maxCountForBufferSize(sizeof(buffer[0]) * BUF_MAX);
70
71 SkASSERT(state.fPixmap.addr());
72
73 for (;;) {
74 int n = SkTMin(count, max);
75 SkASSERT(n > 0 && n < BUF_MAX*2);
76 mproc(state, buffer, n, x, y);
77 sproc(state, buffer, n, dstC);
78
79 if ((count -= n) == 0) {
80 break;
81 }
82 SkASSERT(count > 0);
83 x += n;
84 dstC += n;
85 }
86 }
87
88 ShadeProc asAShadeProc(void** ctx) override {
89 if (fState->getShaderProc32()) {
90 *ctx = fState;
91 return (ShadeProc)fState->getShaderProc32();
92 }
93 return nullptr;
94 }
95
96private:
97 SkBitmapProcState* fState;
98
99 typedef BitmapProcInfoContext INHERITED;
100};
101
102///////////////////////////////////////////////////////////////////////////////////////////////////
reedd8829012016-03-04 11:07:43 -0800103#include "SkLinearBitmapPipeline.h"
104#include "SkPM4f.h"
reed05a56472016-03-02 09:49:02 -0800105
reedd8829012016-03-04 11:07:43 -0800106class LinearPipelineContext : public BitmapProcInfoContext {
107public:
Florin Malita4aed1382017-05-25 10:38:07 -0400108 LinearPipelineContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec,
Herb Derbye836b782017-03-02 14:39:32 -0500109 SkBitmapProcInfo* info, SkArenaAlloc* alloc)
110 : INHERITED(shader, rec, info), fAllocator{alloc}
reedd8829012016-03-04 11:07:43 -0800111 {
herb69076fe2016-04-12 14:07:59 -0700112 // Save things off in case we need to build a blitter pipeline.
113 fSrcPixmap = info->fPixmap;
114 fAlpha = SkColorGetA(info->fPaintColor) / 255.0f;
herb69076fe2016-04-12 14:07:59 -0700115 fFilterQuality = info->fFilterQuality;
116 fMatrixTypeMask = info->fRealInvMatrix.getType();
117
Herb Derbye836b782017-03-02 14:39:32 -0500118 fShaderPipeline = alloc->make<SkLinearBitmapPipeline>(
herb57a69dc2016-05-19 14:19:23 -0700119 info->fRealInvMatrix, info->fFilterQuality,
120 info->fTileModeX, info->fTileModeY,
121 info->fPaintColor,
Herb Derbydfa492e2016-11-17 21:12:36 -0500122 info->fPixmap,
Herb Derbye836b782017-03-02 14:39:32 -0500123 fAllocator);
reedd8829012016-03-04 11:07:43 -0800124 }
125
reedd8829012016-03-04 11:07:43 -0800126 void shadeSpan4f(int x, int y, SkPM4f dstC[], int count) override {
herb69076fe2016-04-12 14:07:59 -0700127 fShaderPipeline->shadeSpan4f(x, y, dstC, count);
reedd8829012016-03-04 11:07:43 -0800128 }
129
130 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override {
131 const int N = 128;
132 SkPM4f tmp[N];
133
134 while (count > 0) {
135 const int n = SkTMin(count, N);
herb69076fe2016-04-12 14:07:59 -0700136 fShaderPipeline->shadeSpan4f(x, y, tmp, n);
Mike Reed43e498e2017-06-08 20:26:41 -0400137 // now convert to SkPMColor
138 for (int i = 0; i < n; ++i) {
139 dstC[i] = Sk4f_toL32(tmp[i].to4f_pmorder());
140 }
reedd8829012016-03-04 11:07:43 -0800141 dstC += n;
142 x += n;
143 count -= n;
144 }
145 }
146
147private:
Herb Derbye836b782017-03-02 14:39:32 -0500148 // Store the allocator from the context creation incase we are asked to build a blitter.
149 SkArenaAlloc* fAllocator;
Herb Derbydfa492e2016-11-17 21:12:36 -0500150 SkLinearBitmapPipeline* fShaderPipeline;
Herb Derbydfa492e2016-11-17 21:12:36 -0500151 SkPixmap fSrcPixmap;
152 float fAlpha;
153 SkMatrix::TypeMask fMatrixTypeMask;
154 SkFilterQuality fFilterQuality;
reedd8829012016-03-04 11:07:43 -0800155
156 typedef BitmapProcInfoContext INHERITED;
157};
158
159///////////////////////////////////////////////////////////////////////////////////////////////////
160
Florin Malita4aed1382017-05-25 10:38:07 -0400161static bool choose_linear_pipeline(const SkShaderBase::ContextRec& rec, const SkImageInfo& srcInfo) {
reedd8829012016-03-04 11:07:43 -0800162 // If we get here, we can reasonably use either context, respect the caller's preference
163 //
herba4e4b792016-06-04 13:27:10 -0700164 bool needsPremul = srcInfo.alphaType() == kUnpremul_SkAlphaType;
165 bool needsSwizzle = srcInfo.bytesPerPixel() == 4 && srcInfo.colorType() != kN32_SkColorType;
Florin Malita4aed1382017-05-25 10:38:07 -0400166 return SkShaderBase::ContextRec::kPM4f_DstType == rec.fPreferredDstType
herba4e4b792016-06-04 13:27:10 -0700167 || needsPremul || needsSwizzle;
reedd8829012016-03-04 11:07:43 -0800168}
169
reed320a40d2016-08-02 06:12:06 -0700170size_t SkBitmapProcLegacyShader::ContextSize(const ContextRec& rec, const SkImageInfo& srcInfo) {
reedd8829012016-03-04 11:07:43 -0800171 size_t size0 = sizeof(BitmapProcShaderContext) + sizeof(SkBitmapProcState);
172 size_t size1 = sizeof(LinearPipelineContext) + sizeof(SkBitmapProcInfo);
reed320a40d2016-08-02 06:12:06 -0700173 size_t s = SkTMax(size0, size1);
174 return s;
reed7a4d8472015-09-15 13:33:58 -0700175}
176
Florin Malita4aed1382017-05-25 10:38:07 -0400177SkShaderBase::Context* SkBitmapProcLegacyShader::MakeContext(
178 const SkShaderBase& shader, TileMode tmx, TileMode tmy,
Herb Derby83e939b2017-02-07 14:25:11 -0500179 const SkBitmapProvider& provider, const ContextRec& rec, SkArenaAlloc* alloc)
180{
reed05a56472016-03-02 09:49:02 -0800181 SkMatrix totalInverse;
182 // Do this first, so we know the matrix can be inverted.
Florin Malita26368c32017-05-08 13:03:24 -0400183 if (!shader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &totalInverse)) {
reed05a56472016-03-02 09:49:02 -0800184 return nullptr;
185 }
186
herb6eff52a2016-03-23 09:00:33 -0700187 // Decide if we can/want to use the new linear pipeline
reedd8829012016-03-04 11:07:43 -0800188 bool useLinearPipeline = choose_linear_pipeline(rec, provider.info());
reedcd660e12016-03-03 09:36:50 -0800189
reedd8829012016-03-04 11:07:43 -0800190 if (useLinearPipeline) {
Herb Derby83e939b2017-02-07 14:25:11 -0500191 SkBitmapProcInfo* info = alloc->make<SkBitmapProcInfo>(provider, tmx, tmy);
reedd8829012016-03-04 11:07:43 -0800192 if (!info->init(totalInverse, *rec.fPaint)) {
reedd8829012016-03-04 11:07:43 -0800193 return nullptr;
194 }
herb670f01f2016-05-13 10:04:46 -0700195
Herb Derbye836b782017-03-02 14:39:32 -0500196 return alloc->make<LinearPipelineContext>(shader, rec, info, alloc);
reedd8829012016-03-04 11:07:43 -0800197 } else {
Herb Derby83e939b2017-02-07 14:25:11 -0500198 SkBitmapProcState* state = alloc->make<SkBitmapProcState>(provider, tmx, tmy);
reedd8829012016-03-04 11:07:43 -0800199 if (!state->setup(totalInverse, *rec.fPaint)) {
reedd8829012016-03-04 11:07:43 -0800200 return nullptr;
201 }
Herb Derby83e939b2017-02-07 14:25:11 -0500202 return alloc->make<BitmapProcShaderContext>(shader, rec, state);
reedd8829012016-03-04 11:07:43 -0800203 }
reed05a56472016-03-02 09:49:02 -0800204}