blob: d157a73595106fbf1580829e1cf9396eaf663bc0 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/shaders/SkBitmapProcShader.h"
Herb Derby83e939b2017-02-07 14:25:11 -05009
Ben Wagner729a23f2019-05-17 16:29:34 -040010#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkBitmapProcState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkXfermodePriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
reed05a56472016-03-02 09:49:02 -080014static bool only_scale_and_translate(const SkMatrix& matrix) {
15 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
16 return (matrix.getType() & ~mask) == 0;
17}
18
Florin Malita4aed1382017-05-25 10:38:07 -040019class BitmapProcInfoContext : public SkShaderBase::Context {
reed05a56472016-03-02 09:49:02 -080020public:
reedd8829012016-03-04 11:07:43 -080021 // The info has been allocated elsewhere, but we are responsible for calling its destructor.
Florin Malita4aed1382017-05-25 10:38:07 -040022 BitmapProcInfoContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec,
reed05a56472016-03-02 09:49:02 -080023 SkBitmapProcInfo* info)
24 : INHERITED(shader, rec)
25 , fInfo(info)
26 {
27 fFlags = 0;
28 if (fInfo->fPixmap.isOpaque() && (255 == this->getPaintAlpha())) {
Florin Malita4aed1382017-05-25 10:38:07 -040029 fFlags |= SkShaderBase::kOpaqueAlpha_Flag;
reed05a56472016-03-02 09:49:02 -080030 }
31
32 if (1 == fInfo->fPixmap.height() && only_scale_and_translate(this->getTotalInverse())) {
Florin Malita4aed1382017-05-25 10:38:07 -040033 fFlags |= SkShaderBase::kConstInY32_Flag;
reed05a56472016-03-02 09:49:02 -080034 }
35 }
36
reed05a56472016-03-02 09:49:02 -080037 uint32_t getFlags() const override { return fFlags; }
38
39private:
40 SkBitmapProcInfo* fInfo;
41 uint32_t fFlags;
42
Florin Malita4aed1382017-05-25 10:38:07 -040043 typedef SkShaderBase::Context INHERITED;
reed05a56472016-03-02 09:49:02 -080044};
45
46///////////////////////////////////////////////////////////////////////////////////////////////////
47
48class BitmapProcShaderContext : public BitmapProcInfoContext {
49public:
Florin Malita4aed1382017-05-25 10:38:07 -040050 BitmapProcShaderContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec,
reed05a56472016-03-02 09:49:02 -080051 SkBitmapProcState* state)
52 : INHERITED(shader, rec, state)
53 , fState(state)
54 {}
55
56 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override {
57 const SkBitmapProcState& state = *fState;
58 if (state.getShaderProc32()) {
59 state.getShaderProc32()(&state, x, y, dstC, count);
60 return;
61 }
62
63 const int BUF_MAX = 128;
64 uint32_t buffer[BUF_MAX];
65 SkBitmapProcState::MatrixProc mproc = state.getMatrixProc();
66 SkBitmapProcState::SampleProc32 sproc = state.getSampleProc32();
67 const int max = state.maxCountForBufferSize(sizeof(buffer[0]) * BUF_MAX);
68
69 SkASSERT(state.fPixmap.addr());
70
71 for (;;) {
72 int n = SkTMin(count, max);
73 SkASSERT(n > 0 && n < BUF_MAX*2);
74 mproc(state, buffer, n, x, y);
75 sproc(state, buffer, n, dstC);
76
77 if ((count -= n) == 0) {
78 break;
79 }
80 SkASSERT(count > 0);
81 x += n;
82 dstC += n;
83 }
84 }
85
reed05a56472016-03-02 09:49:02 -080086private:
87 SkBitmapProcState* fState;
88
89 typedef BitmapProcInfoContext INHERITED;
90};
91
92///////////////////////////////////////////////////////////////////////////////////////////////////
reed7a4d8472015-09-15 13:33:58 -070093
Florin Malita4aed1382017-05-25 10:38:07 -040094SkShaderBase::Context* SkBitmapProcLegacyShader::MakeContext(
Mike Reedfae8fce2019-04-03 10:27:45 -040095 const SkShaderBase& shader, SkTileMode tmx, SkTileMode tmy,
Mike Reed64acf4f2019-08-01 15:35:20 -040096 const SkImage_Base* image, const ContextRec& rec, SkArenaAlloc* alloc)
Herb Derby83e939b2017-02-07 14:25:11 -050097{
reed05a56472016-03-02 09:49:02 -080098 SkMatrix totalInverse;
99 // Do this first, so we know the matrix can be inverted.
Florin Malita26368c32017-05-08 13:03:24 -0400100 if (!shader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &totalInverse)) {
reed05a56472016-03-02 09:49:02 -0800101 return nullptr;
102 }
103
Mike Reed64acf4f2019-08-01 15:35:20 -0400104 SkBitmapProcState* state = alloc->make<SkBitmapProcState>(image, tmx, tmy);
Mike Klein0fddb2d2017-07-20 08:59:19 -0400105 if (!state->setup(totalInverse, *rec.fPaint)) {
106 return nullptr;
Mike Reedfb499092017-06-26 13:53:32 +0000107 }
Mike Klein0fddb2d2017-07-20 08:59:19 -0400108 return alloc->make<BitmapProcShaderContext>(shader, rec, state);
reed05a56472016-03-02 09:49:02 -0800109}