Home | History | Annotate | Download | only in image
      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 #if SK_SUPPORT_GPU
     16     #include "GrTextureProxy.h"
     17 
     18     class GrTexture;
     19 #endif
     20 
     21 #include <new>
     22 
     23 class GrSamplerState;
     24 class SkImageCacherator;
     25 
     26 enum {
     27     kNeedNewImageUniqueID = 0
     28 };
     29 
     30 class SkImage_Base : public SkImage {
     31 public:
     32     virtual ~SkImage_Base();
     33 
     34     // User: returns image info for this SkImage.
     35     // Implementors: if you can not return the value, return an invalid ImageInfo with w=0 & h=0
     36     // & unknown color space.
     37     virtual SkImageInfo onImageInfo() const = 0;
     38     virtual SkAlphaType onAlphaType() const = 0;
     39 
     40     virtual bool onPeekPixels(SkPixmap*) const { return false; }
     41 
     42     virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
     43 
     44     virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
     45                               int srcX, int srcY, CachingHint) const = 0;
     46 
     47     virtual GrContext* context() const { return nullptr; }
     48 #if SK_SUPPORT_GPU
     49     virtual GrTextureProxy* peekProxy() const { return nullptr; }
     50     virtual sk_sp<GrTextureProxy> asTextureProxyRef() const { return nullptr; }
     51     virtual sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*, const GrSamplerState&,
     52                                                     SkColorSpace*, sk_sp<SkColorSpace>*,
     53                                                     SkScalar scaleAdjust[2]) const = 0;
     54     virtual sk_sp<GrTextureProxy> refPinnedTextureProxy(uint32_t* uniqueID) const {
     55         return nullptr;
     56     }
     57     virtual GrBackendObject onGetTextureHandle(bool flushPendingGrContextIO,
     58                                                GrSurfaceOrigin* origin) const {
     59         return 0;
     60     }
     61     virtual GrTexture* onGetTexture() const { return nullptr; }
     62 #endif
     63     virtual SkImageCacherator* peekCacherator() const { return nullptr; }
     64 
     65     // return a read-only copy of the pixels. We promise to not modify them,
     66     // but only inspect them (or encode them).
     67     virtual bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace,
     68                              CachingHint = kAllow_CachingHint) const = 0;
     69 
     70     virtual sk_sp<SkImage> onMakeSubset(const SkIRect&) const = 0;
     71 
     72     virtual SkData* onRefEncoded() const { return nullptr; }
     73 
     74     virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
     75 
     76     // True for picture-backed and codec-backed
     77     virtual bool onIsLazyGenerated() const { return false; }
     78 
     79     // True only for generators that operate directly on gpu (e.g. picture-generators)
     80     virtual bool onCanLazyGenerateOnGPU() const { return false; }
     81 
     82     // Call when this image is part of the key to a resourcecache entry. This allows the cache
     83     // to know automatically those entries can be purged when this SkImage deleted.
     84     void notifyAddedToCache() const {
     85         fAddedToCache.store(true);
     86     }
     87 
     88     virtual bool onIsValid(GrContext*) const = 0;
     89 
     90     virtual bool onPinAsTexture(GrContext*) const { return false; }
     91     virtual void onUnpinAsTexture(GrContext*) const {}
     92 
     93     virtual sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>, SkColorType,
     94                                             SkTransferFunctionBehavior) const = 0;
     95 protected:
     96     SkImage_Base(int width, int height, uint32_t uniqueID);
     97 
     98 private:
     99     // Set true by caches when they cache content that's derived from the current pixels.
    100     mutable SkAtomic<bool> fAddedToCache;
    101 
    102     typedef SkImage INHERITED;
    103 };
    104 
    105 static inline SkImage_Base* as_IB(SkImage* image) {
    106     return static_cast<SkImage_Base*>(image);
    107 }
    108 
    109 static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
    110     return static_cast<SkImage_Base*>(image.get());
    111 }
    112 
    113 static inline const SkImage_Base* as_IB(const SkImage* image) {
    114     return static_cast<const SkImage_Base*>(image);
    115 }
    116 
    117 #endif
    118