1/*
2 * Copyright 2012 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 SkImage_Base_DEFINED
9#define SkImage_Base_DEFINED
10
11#include "SkAtomics.h"
12#include "SkImage.h"
13#include "SkSurface.h"
14
15#include <new>
16
17class GrTextureParams;
18class SkImageCacherator;
19
20enum {
21    kNeedNewImageUniqueID = 0
22};
23
24class SkImage_Base : public SkImage {
25public:
26    SkImage_Base(int width, int height, uint32_t uniqueID);
27    virtual ~SkImage_Base();
28
29    virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const { return nullptr; }
30
31    // Default impl calls onDraw
32    virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
33                              int srcX, int srcY, CachingHint) const;
34
35    virtual GrTexture* peekTexture() const { return nullptr; }
36    virtual SkImageCacherator* peekCacherator() const { return nullptr; }
37
38    // return a read-only copy of the pixels. We promise to not modify them,
39    // but only inspect them (or encode them).
40    virtual bool getROPixels(SkBitmap*, CachingHint = kAllow_CachingHint) const = 0;
41
42    virtual SkSurface* onNewSurface(const SkImageInfo& info) const {
43        return SkSurface::NewRaster(info);
44    }
45
46    // Caller must call unref when they are done.
47    virtual GrTexture* asTextureRef(GrContext*, const GrTextureParams&) const = 0;
48
49    virtual SkImage* onNewSubset(const SkIRect&) const = 0;
50
51    // If a ctx is specified, then only gpu-specific formats are requested.
52    virtual SkData* onRefEncoded(GrContext*) const { return nullptr; }
53
54    virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
55
56    virtual bool onIsLazyGenerated() const { return false; }
57
58    // Return a bitmap suitable for passing to image-filters
59    // For now, that means wrapping textures into SkGrPixelRefs...
60    virtual bool asBitmapForImageFilters(SkBitmap* bitmap) const {
61        return this->getROPixels(bitmap, kAllow_CachingHint);
62    }
63
64    // Call when this image is part of the key to a resourcecache entry. This allows the cache
65    // to know automatically those entries can be purged when this SkImage deleted.
66    void notifyAddedToCache() const {
67        fAddedToCache.store(true);
68    }
69
70private:
71    // Set true by caches when they cache content that's derived from the current pixels.
72    mutable SkAtomic<bool> fAddedToCache;
73
74    typedef SkImage INHERITED;
75};
76
77static inline SkImage_Base* as_IB(SkImage* image) {
78    return static_cast<SkImage_Base*>(image);
79}
80
81static inline const SkImage_Base* as_IB(const SkImage* image) {
82    return static_cast<const SkImage_Base*>(image);
83}
84
85#endif
86