blob: 402fa2fcff2cfb05bcb71ad964877be6b8e858d0 [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
reed05a56472016-03-02 09:49:02 -080087private:
88 SkBitmapProcState* fState;
89
90 typedef BitmapProcInfoContext INHERITED;
91};
92
93///////////////////////////////////////////////////////////////////////////////////////////////////
reed7a4d8472015-09-15 13:33:58 -070094
Florin Malita4aed1382017-05-25 10:38:07 -040095SkShaderBase::Context* SkBitmapProcLegacyShader::MakeContext(
96 const SkShaderBase& shader, TileMode tmx, TileMode tmy,
Herb Derby83e939b2017-02-07 14:25:11 -050097 const SkBitmapProvider& provider, const ContextRec& rec, SkArenaAlloc* alloc)
98{
reed05a56472016-03-02 09:49:02 -080099 SkMatrix totalInverse;
100 // Do this first, so we know the matrix can be inverted.
Florin Malita26368c32017-05-08 13:03:24 -0400101 if (!shader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &totalInverse)) {
reed05a56472016-03-02 09:49:02 -0800102 return nullptr;
103 }
104
Mike Klein0fddb2d2017-07-20 08:59:19 -0400105 SkBitmapProcState* state = alloc->make<SkBitmapProcState>(provider, tmx, tmy);
106 if (!state->setup(totalInverse, *rec.fPaint)) {
107 return nullptr;
Mike Reedfb499092017-06-26 13:53:32 +0000108 }
Mike Klein0fddb2d2017-07-20 08:59:19 -0400109 return alloc->make<BitmapProcShaderContext>(shader, rec, state);
reed05a56472016-03-02 09:49:02 -0800110}