1/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkSmallAllocator.h"
9#include "SkSpriteBlitter.h"
10
11SkSpriteBlitter::SkSpriteBlitter(const SkBitmap& source)
12        : fSource(&source) {
13    fSource->lockPixels();
14}
15
16SkSpriteBlitter::~SkSpriteBlitter() {
17    fSource->unlockPixels();
18}
19
20void SkSpriteBlitter::setup(const SkBitmap& device, int left, int top,
21                            const SkPaint& paint) {
22    fDevice = &device;
23    fLeft = left;
24    fTop = top;
25    fPaint = &paint;
26}
27
28#ifdef SK_DEBUG
29void SkSpriteBlitter::blitH(int x, int y, int width) {
30    SkDEBUGFAIL("how did we get here?");
31}
32
33void SkSpriteBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
34                                const int16_t runs[]) {
35    SkDEBUGFAIL("how did we get here?");
36}
37
38void SkSpriteBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
39    SkDEBUGFAIL("how did we get here?");
40}
41
42void SkSpriteBlitter::blitMask(const SkMask&, const SkIRect& clip) {
43    SkDEBUGFAIL("how did we get here?");
44}
45#endif
46
47///////////////////////////////////////////////////////////////////////////////
48
49// returning null means the caller will call SkBlitter::Choose() and
50// have wrapped the source bitmap inside a shader
51SkBlitter* SkBlitter::ChooseSprite(const SkBitmap& device, const SkPaint& paint,
52        const SkBitmap& source, int left, int top, SkTBlitterAllocator* allocator) {
53    /*  We currently ignore antialiasing and filtertype, meaning we will take our
54        special blitters regardless of these settings. Ignoring filtertype seems fine
55        since by definition there is no scale in the matrix. Ignoring antialiasing is
56        a bit of a hack, since we "could" pass in the fractional left/top for the bitmap,
57        and respect that by blending the edges of the bitmap against the device. To support
58        this we could either add more special blitters here, or detect antialiasing in the
59        paint and return null if it is set, forcing the client to take the slow shader case
60        (which does respect soft edges).
61    */
62    SkASSERT(allocator != NULL);
63
64    SkSpriteBlitter* blitter;
65
66    switch (device.colorType()) {
67        case kRGB_565_SkColorType:
68            blitter = SkSpriteBlitter::ChooseD16(source, paint, allocator);
69            break;
70        case kN32_SkColorType:
71            blitter = SkSpriteBlitter::ChooseD32(source, paint, allocator);
72            break;
73        default:
74            blitter = NULL;
75            break;
76    }
77
78    if (blitter) {
79        blitter->setup(device, left, top, paint);
80    }
81    return blitter;
82}
83