summaryrefslogtreecommitdiffstats
path: root/skia/sgl/SkBitmapProcShader.cpp
blob: 8685c9f813a26438f59e03dbac2fef77c632055b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "SkBitmapProcShader.h"
#include "SkColorPriv.h"
#include "SkPixelRef.h"

bool SkBitmapProcShader::CanDo(const SkBitmap& bm, TileMode tx, TileMode ty) {
    switch (bm.config()) {
        case SkBitmap::kA8_Config:
        case SkBitmap::kRGB_565_Config:
        case SkBitmap::kIndex8_Config:
        case SkBitmap::kARGB_8888_Config:
    //        if (tx == ty && (kClamp_TileMode == tx || kRepeat_TileMode == tx))
                return true;
        default:
            break;
    }
    return false;
}

SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src,
                                       TileMode tmx, TileMode tmy) {
    fRawBitmap = src;
    fState.fTileModeX = (uint8_t)tmx;
    fState.fTileModeY = (uint8_t)tmy;
}

SkBitmapProcShader::SkBitmapProcShader(SkFlattenableReadBuffer& buffer)
        : INHERITED(buffer) {
    fRawBitmap.unflatten(buffer);
    fState.fTileModeX = buffer.readU8();
    fState.fTileModeY = buffer.readU8();
}

void SkBitmapProcShader::beginSession() {
    this->INHERITED::beginSession();

    fRawBitmap.lockPixels();
}

void SkBitmapProcShader::endSession() {
    fRawBitmap.unlockPixels();

    this->INHERITED::endSession();
}

bool SkBitmapProcShader::asABitmap(SkBitmap* texture, SkMatrix* texM,
                                   TileMode xy[]) {
    if (texture) {
        *texture = fRawBitmap;
    }
    if (texM) {
        texM->reset();
    }
    if (xy) {
        xy[0] = (TileMode)fState.fTileModeX;
        xy[1] = (TileMode)fState.fTileModeY;
    }
    return true;
}

void SkBitmapProcShader::flatten(SkFlattenableWriteBuffer& buffer) {
    this->INHERITED::flatten(buffer);

    fRawBitmap.flatten(buffer);
    buffer.write8(fState.fTileModeX);
    buffer.write8(fState.fTileModeY);
}

bool SkBitmapProcShader::setContext(const SkBitmap& device,
                                    const SkPaint& paint,
                                    const SkMatrix& matrix) {
    // do this first, so we have a correct inverse matrix
    if (!this->INHERITED::setContext(device, paint, matrix)) {
        return false;
    }

    fState.fOrigBitmap = fRawBitmap;
    fState.fOrigBitmap.lockPixels();
    if (fState.fOrigBitmap.getPixels() == NULL) {
        fState.fOrigBitmap.unlockPixels();
        return false;
    }

    if (!fState.chooseProcs(this->getTotalInverse(), paint)) {
        return false;
    }

    bool bitmapIsOpaque = fState.fBitmap->isOpaque();
    
    // filtering doesn't guarantee that opaque stays opaque (finite precision)
    // so pretend we're not opaque if we're being asked to filter. If we had
    // more blit-procs, we could specialize on opaque src, and just OR in 0xFF
    // after the filter to be sure...
    if (paint.isFilterBitmap()) {
        bitmapIsOpaque = false;
    }

    // update fFlags
    fFlags = 0; // this should happen in SkShader.cpp

    if (bitmapIsOpaque && (255 == this->getPaintAlpha())) {
        fFlags |= kOpaqueAlpha_Flag;
    }

    switch (fState.fBitmap->config()) {
        case SkBitmap::kRGB_565_Config:
            fFlags |= (kHasSpan16_Flag | kIntrinsicly16_Flag);
            break;
        case SkBitmap::kIndex8_Config:
        case SkBitmap::kARGB_8888_Config:
            if (bitmapIsOpaque) {
                fFlags |= kHasSpan16_Flag;
            }
            break;
        case SkBitmap::kA8_Config:
            break;  // never set kHasSpan16_Flag
        default:
            break;
    }
    return true;
}

#define BUF_MAX     128

void SkBitmapProcShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
    uint32_t buffer[BUF_MAX];

    const SkBitmapProcState&        state = fState;
    SkBitmapProcState::MatrixProc   mproc = state.fMatrixProc;
    SkBitmapProcState::SampleProc32 sproc = state.fSampleProc32;
    int max = fState.fDoFilter ? (BUF_MAX >> 1) : BUF_MAX;

    SkASSERT(state.fBitmap->getPixels());
    SkASSERT(state.fBitmap->pixelRef() == NULL ||
             state.fBitmap->pixelRef()->getLockCount());

    for (;;) {
        int n = count;
        if (n > max) {
            n = max;
        }
        mproc(state, buffer, n, x, y);
        sproc(state, buffer, n, dstC);
        
        if ((count -= n) == 0) {
            break;
        }
        x += n;
        dstC += n;
    }
}

void SkBitmapProcShader::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
    uint32_t buffer[BUF_MAX];
    
    const SkBitmapProcState&        state = fState;
    SkBitmapProcState::MatrixProc   mproc = state.fMatrixProc;
    SkBitmapProcState::SampleProc16 sproc = state.fSampleProc16;
    int max = fState.fDoFilter ? (BUF_MAX >> 1) : BUF_MAX;

    SkASSERT(state.fBitmap->getPixels());
    SkASSERT(state.fBitmap->pixelRef() == NULL ||
             state.fBitmap->pixelRef()->getLockCount());

    for (;;) {
        int n = count;
        if (n > max) {
            n = max;
        }
        mproc(state, buffer, n, x, y);
        sproc(state, buffer, n, dstC);
        
        if ((count -= n) == 0) {
            break;
        }
        x += n;
        dstC += n;
    }
}

///////////////////////////////////////////////////////////////////////////////

#include "SkTemplatesPriv.h"

SkShader* SkShader::CreateBitmapShader(const SkBitmap& src,
                                       TileMode tmx, TileMode tmy,
                                       void* storage, size_t storageSize) {
    SkShader* shader;
    SK_PLACEMENT_NEW_ARGS(shader, SkBitmapProcShader, storage,
                          storageSize, (src, tmx, tmy));
    return shader;
}

static SkFlattenable::Registrar gBitmapProcShaderReg("SkBitmapProcShader",
                                               SkBitmapProcShader::CreateProc);