blob: adb6750f459a81172af6389c1b3106710fd3854b [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
reed05a56472016-03-02 09:49:02 -080088private:
89 SkBitmapProcState* fState;
90
91 typedef BitmapProcInfoContext INHERITED;
92};
93
94///////////////////////////////////////////////////////////////////////////////////////////////////
reed7a4d8472015-09-15 13:33:58 -070095
Florin Malita4aed1382017-05-25 10:38:07 -040096SkShaderBase::Context* SkBitmapProcLegacyShader::MakeContext(
97 const SkShaderBase& shader, TileMode tmx, TileMode tmy,
Herb Derby83e939b2017-02-07 14:25:11 -050098 const SkBitmapProvider& provider, const ContextRec& rec, SkArenaAlloc* alloc)
99{
reed05a56472016-03-02 09:49:02 -0800100 SkMatrix totalInverse;
101 // Do this first, so we know the matrix can be inverted.
Florin Malita26368c32017-05-08 13:03:24 -0400102 if (!shader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &totalInverse)) {
reed05a56472016-03-02 09:49:02 -0800103 return nullptr;
104 }
105
Mike Klein0fddb2d2017-07-20 08:59:19 -0400106 SkBitmapProcState* state = alloc->make<SkBitmapProcState>(provider, tmx, tmy);
107 if (!state->setup(totalInverse, *rec.fPaint)) {
108 return nullptr;
Mike Reedfb499092017-06-26 13:53:32 +0000109 }
Mike Klein0fddb2d2017-07-20 08:59:19 -0400110 return alloc->make<BitmapProcShaderContext>(shader, rec, state);
reed05a56472016-03-02 09:49:02 -0800111}