aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkBitmap_scroll.cpp
blob: e9c886f4a0e7c43d5a1762fdf6f514660adf87e1 (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

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkBitmap.h"
#include "SkRegion.h"

bool SkBitmap::scrollRect(const SkIRect* subset, int dx, int dy,
                          SkRegion* inval) const
{
    if (this->isImmutable()) {
        return false;
    }

    if (NULL != subset) {
        SkBitmap tmp;

        return  this->extractSubset(&tmp, *subset) &&
                // now call again with no rectangle
                tmp.scrollRect(NULL, dx, dy, inval);
    }

    int shift;

    switch (this->config()) {
    case kIndex8_Config:
    case kA8_Config:
        shift = 0;
        break;
    case kARGB_4444_Config:
    case kRGB_565_Config:
        shift = 1;
        break;
    case kARGB_8888_Config:
        shift = 2;
        break;
    default:
        // can't scroll this config
        return false;
    }

    int width = this->width();
    int height = this->height();

    // check if there's nothing to do
    if ((dx | dy) == 0 || width <= 0 || height <= 0) {
        if (NULL != inval) {
            inval->setEmpty();
        }
        return true;
    }

    // compute the inval region now, before we see if there are any pixels
    if (NULL != inval) {
        SkIRect r;

        r.set(0, 0, width, height);
        // initial the region with the entire bounds
        inval->setRect(r);
        // do the "scroll"
        r.offset(dx, dy);

        // check if we scrolled completely away
        if (!SkIRect::Intersects(r, inval->getBounds())) {
            // inval has already been updated...
            return true;
        }

        // compute the dirty area
        inval->op(r, SkRegion::kDifference_Op);
    }

    SkAutoLockPixels    alp(*this);
    // if we have no pixels, just return (inval is already updated)
    // don't call readyToDraw(), since we don't require a colortable per se
    if (this->getPixels() == NULL) {
        return true;
    }

    char*       dst = (char*)this->getPixels();
    const char* src = dst;
    int         rowBytes = (int)this->rowBytes();    // need rowBytes to be signed

    if (dy <= 0) {
        src -= dy * rowBytes;
        height += dy;
    } else {
        dst += dy * rowBytes;
        height -= dy;
        // now jump src/dst to the last scanline
        src += (height - 1) * rowBytes;
        dst += (height - 1) * rowBytes;
        // now invert rowbytes so we copy backwards in the loop
        rowBytes = -rowBytes;
    }

    if (dx <= 0) {
        src -= dx << shift;
        width += dx;
    } else {
        dst += dx << shift;
        width -= dx;
    }

    // If the X-translation would push it completely beyond the region,
    // then there's nothing to draw.
    if (width <= 0) {
        return true;
    }

    width <<= shift;    // now width is the number of bytes to move per line
    while (--height >= 0) {
        memmove(dst, src, width);
        dst += rowBytes;
        src += rowBytes;
    }

    this->notifyPixelsChanged();
    return true;
}