SkImage_Generator.cpp revision 476506d070dbc59b158acc1a00c34bff95ab2968
1/*
2 * Copyright 2015 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 */
7
8#include "SkImage_Base.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkData.h"
12#include "SkImageCacherator.h"
13#include "SkImagePriv.h"
14#include "SkPixelRef.h"
15#include "SkSurface.h"
16
17class SkImage_Generator : public SkImage_Base {
18public:
19    SkImage_Generator(SkImageCacherator* cache)
20        : INHERITED(cache->info().width(), cache->info().height(), cache->uniqueID(), NULL)
21        , fCache(cache) // take ownership
22    {}
23
24    bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY) const override;
25    const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const override;
26    SkData* onRefEncoded() const override;
27    bool isOpaque() const override { return fCache->info().isOpaque(); }
28    SkImage* onNewSubset(const SkIRect&) const override;
29    bool getROPixels(SkBitmap*) const override;
30    GrTexture* asTextureRef(GrContext*, SkImageUsageType) const override;
31
32    SkShader* onNewShader(SkShader::TileMode,
33                          SkShader::TileMode,
34                          const SkMatrix* localMatrix) const override;
35
36    bool onIsLazyGenerated() const override { return true; }
37
38private:
39    SkAutoTDelete<SkImageCacherator> fCache;
40
41    typedef SkImage_Base INHERITED;
42};
43
44///////////////////////////////////////////////////////////////////////////////
45
46SkShader* SkImage_Generator::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY,
47                                         const SkMatrix* localMatrix) const {
48    // TODO: need a native Shader that takes Cacherator (or this image) so we can natively return
49    // textures as output from the shader.
50    SkBitmap bm;
51    if (this->getROPixels(&bm)) {
52        return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix);
53    }
54    return nullptr;
55}
56
57bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
58                                     int srcX, int srcY) const {
59    SkBitmap bm;
60    if (this->getROPixels(&bm)) {
61        return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
62    }
63    return false;
64}
65
66const void* SkImage_Generator::onPeekPixels(SkImageInfo* infoPtr, size_t* rowBytesPtr) const {
67    return NULL;
68}
69
70SkData* SkImage_Generator::onRefEncoded() const {
71    return fCache->refEncoded();
72}
73
74bool SkImage_Generator::getROPixels(SkBitmap* bitmap) const {
75    return fCache->lockAsBitmap(bitmap, this);
76}
77
78GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, SkImageUsageType usage) const {
79    return fCache->lockAsTexture(ctx, usage, this);
80}
81
82SkImage* SkImage_Generator::onNewSubset(const SkIRect& subset) const {
83    // TODO: make this lazy, by wrapping the subset inside a new generator or something
84    // For now, we do effectively what we did before, make it a raster
85
86    const SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(),
87                                      this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
88    SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
89    if (!surface) {
90        return nullptr;
91    }
92    surface->getCanvas()->clear(0);
93    surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToScalar(-subset.y()),
94                                    nullptr);
95    return surface->newImageSnapshot();
96}
97
98SkImage* SkImage::NewFromGenerator(SkImageGenerator* generator, const SkIRect* subset) {
99    SkImageCacherator* cache = SkImageCacherator::NewFromGenerator(generator, subset);
100    if (!cache) {
101        return nullptr;
102    }
103    return new SkImage_Generator(cache);
104}
105