SkImageCacherator.h revision afa95e270c64c9777647b6c58b796750ced57c39
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#ifndef SkImageCacherator_DEFINED
9#define SkImageCacherator_DEFINED
10
11#include "SkImageGenerator.h"
12#include "SkMutex.h"
13#include "SkTemplates.h"
14
15class GrContext;
16class GrTextureParams;
17class SkBitmap;
18class SkImage;
19
20/*
21 *  Internal class to manage caching the output of an ImageGenerator.
22 */
23class SkImageCacherator {
24public:
25    // Takes ownership of the generator
26    static SkImageCacherator* NewFromGenerator(SkImageGenerator*, const SkIRect* subset = nullptr);
27
28    const SkImageInfo& info() const { return fInfo; }
29    uint32_t uniqueID() const { return fUniqueID; }
30
31    /**
32     *  On success (true), bitmap will point to the pixels for this generator. If this returns
33     *  false, the bitmap will be reset to empty.
34     *
35     *  If not NULL, the client will be notified (->notifyAddedToCache()) when resources are
36     *  added to the cache on its behalf.
37     */
38    bool lockAsBitmap(SkBitmap*, const SkImage* client);
39
40    /**
41     *  Returns a ref() on the texture produced by this generator. The caller must call unref()
42     *  when it is done. Will return nullptr on failure.
43     *
44     *  If not NULL, the client will be notified (->notifyAddedToCache()) when resources are
45     *  added to the cache on its behalf.
46     *
47     *  The caller is responsible for calling texture->unref() when they are done.
48     */
49    GrTexture* lockAsTexture(GrContext*, const GrTextureParams&, const SkImage* client);
50
51    /**
52     *  If the underlying src naturally is represented by an encoded blob (in SkData), this returns
53     *  a ref to that data. If not, it returns null.
54     */
55    SkData* refEncoded();
56
57private:
58    SkImageCacherator(SkImageGenerator*, const SkImageInfo&, const SkIPoint&, uint32_t uniqueID);
59
60    bool generateBitmap(SkBitmap*);
61    bool tryLockAsBitmap(SkBitmap*, const SkImage*);
62#if SK_SUPPORT_GPU
63    GrTexture* lockUnstretchedTexture(GrContext*, const GrTextureParams&, const SkImage* client);
64#endif
65
66    class ScopedGenerator {
67        SkImageCacherator* fCacher;
68    public:
69        ScopedGenerator(SkImageCacherator* cacher) : fCacher(cacher) {
70            fCacher->fMutexForGenerator.acquire();
71        }
72        ~ScopedGenerator() {
73            fCacher->fMutexForGenerator.release();
74        }
75        SkImageGenerator* operator->() const { return fCacher->fNotThreadSafeGenerator; }
76        operator SkImageGenerator*() const { return fCacher->fNotThreadSafeGenerator; }
77    };
78
79    SkMutex                         fMutexForGenerator;
80    SkAutoTDelete<SkImageGenerator> fNotThreadSafeGenerator;
81
82    const SkImageInfo   fInfo;
83    const SkIPoint      fOrigin;
84    const uint32_t      fUniqueID;
85
86    friend class Cacherator_GrTextureMaker;
87};
88
89#endif
90