aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/SkImageGeneratorUtils.cpp
blob: 928d052ec6514e97671ba9e4bdde9424497a7a84 (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
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkImageGeneratorUtils.h"
#include "SkBitmap.h"

class GeneratorFromEmpty : public SkImageGenerator {
public:
    GeneratorFromEmpty(const SkImageInfo& info) : SkImageGenerator(info) {}
};

SkImageGenerator* SkImageGeneratorUtils::NewEmpty(const SkImageInfo& info) {
    return new GeneratorFromEmpty(info);
}

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

class GeneratorFromBitmap : public SkImageGenerator {
public:
    GeneratorFromBitmap(const SkBitmap& bm) : SkImageGenerator(bm.info()), fBM(bm) {
        if (!bm.isImmutable()) {
            bm.copyTo(&fBM);
        }
    }

protected:
    bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
                     SkPMColor*, int*) override {
        return fBM.readPixels(info, pixels, rowBytes, 0, 0);
    }

private:
    SkBitmap fBM;
};

SkImageGenerator* SkImageGeneratorUtils::NewFromBitmap(const SkBitmap& bm) {
    return new GeneratorFromBitmap(bm);
}

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

#if SK_SUPPORT_GPU

#include "GrContext.h"
#include "GrTexture.h"
#include "SkGrPriv.h"

class GeneratorFromTexture : public SkImageGenerator {
public:
    GeneratorFromTexture(GrContext* ctx, GrTexture* tex, const SkImageInfo& info)
        : SkImageGenerator(info), fCtx(ctx), fTexture(tex)
    {}

protected:
    GrTexture* onGenerateTexture(GrContext* ctx, const SkIRect* subset) override {
        if (ctx) {
            SkASSERT(ctx == fCtx.get());
        }

        if (!subset) {
            return SkRef(fTexture.get());
        }
        // need to copy the subset into a new texture
        GrSurfaceDesc desc = fTexture->desc();
        desc.fWidth = subset->width();
        desc.fHeight = subset->height();

        GrTexture* dst = fCtx->textureProvider()->createTexture(desc, false);
        fCtx->copySurface(dst, fTexture, *subset, SkIPoint::Make(0, 0));
        return dst;
    }
private:
    SkAutoTUnref<GrContext> fCtx;
    SkAutoTUnref<GrTexture> fTexture;
};
#endif

SkImageGenerator* SkImageGeneratorUtils::NewFromTexture(GrContext* ctx, GrTexture* tex) {
#if SK_SUPPORT_GPU
    if (ctx && tex) {
        const GrSurfaceDesc desc = tex->desc();

        SkColorType ct;
        SkColorProfileType cpt;
        if (!GrPixelConfig2ColorAndProfileType(desc.fConfig, &ct, &cpt)) {
            return nullptr;
        }
        const SkAlphaType at = kPremul_SkAlphaType; // take isOpaque from caller?
        SkImageInfo info = SkImageInfo::Make(desc.fWidth, desc.fHeight, ct, at, cpt);
        return new GeneratorFromTexture(ctx, tex, info);
    }
#endif
    return nullptr;
}

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

#include "SkImage.h"

class GeneratorFromImage : public SkImageGenerator {
public:
    GeneratorFromImage(const SkImage* image, const SkImageInfo& info)
        : SkImageGenerator(info), fImage(image) {}

protected:
    bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
                     SkPMColor*, int*) override {
        return fImage->readPixels(info, pixels, rowBytes, 0, 0);
    }

    GrTexture* onGenerateTexture(GrContext* ctx, const SkIRect* subset) override {
        // waiting on https://code.google.com/p/skia/issues/detail?id=4233
        return nullptr;
    }

private:
    SkAutoTUnref<const SkImage> fImage;
};

SkImageGenerator* SkImageGeneratorUtils::NewFromImage(const SkImage* image) {
    if (image) {
        const SkColorType ct = kN32_SkColorType;
        const SkAlphaType at = image->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
        const SkImageInfo info = SkImageInfo::Make(image->width(), image->height(), ct, at);
        return new GeneratorFromImage(image, info);
    }
    return nullptr;
}