Home | History | Annotate | Download | only in image
      1 /*
      2  * Copyright 2018 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_Lazy_DEFINED
      9 #define SkImage_Lazy_DEFINED
     10 
     11 #include "SkImage_Base.h"
     12 #include "SkMutex.h"
     13 
     14 #if SK_SUPPORT_GPU
     15 #include "GrTextureMaker.h"
     16 #endif
     17 
     18 class SharedGenerator;
     19 
     20 class SkImage_Lazy : public SkImage_Base {
     21 public:
     22     struct Validator {
     23         Validator(sk_sp<SharedGenerator>, const SkIRect* subset, const SkColorType* colorType,
     24                   sk_sp<SkColorSpace> colorSpace);
     25 
     26         operator bool() const { return fSharedGenerator.get(); }
     27 
     28         sk_sp<SharedGenerator> fSharedGenerator;
     29         SkImageInfo            fInfo;
     30         SkIPoint               fOrigin;
     31         sk_sp<SkColorSpace>    fColorSpace;
     32         uint32_t               fUniqueID;
     33     };
     34 
     35     SkImage_Lazy(Validator* validator);
     36     ~SkImage_Lazy() override;
     37 
     38     SkImageInfo onImageInfo() const override {
     39         return fInfo;
     40     }
     41 
     42     SkIRect onGetSubset() const override {
     43         return SkIRect::MakeXYWH(fOrigin.fX, fOrigin.fY, fInfo.width(), fInfo.height());
     44     }
     45 
     46     bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY,
     47                       CachingHint) const override;
     48 #if SK_SUPPORT_GPU
     49     sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*,
     50                                             const GrSamplerState&,
     51                                             SkScalar scaleAdjust[2]) const override;
     52     sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
     53                                   SkYUVColorSpace*, const void* planes[4]) override;
     54 #endif
     55     sk_sp<SkData> onRefEncoded() const override;
     56     sk_sp<SkImage> onMakeSubset(GrRecordingContext*, const SkIRect&) const override;
     57     bool getROPixels(SkBitmap*, CachingHint) const override;
     58     bool onIsLazyGenerated() const override { return true; }
     59     sk_sp<SkImage> onMakeColorTypeAndColorSpace(GrRecordingContext*,
     60                                                 SkColorType, sk_sp<SkColorSpace>) const override;
     61 
     62     bool onIsValid(GrContext*) const override;
     63 
     64 #if SK_SUPPORT_GPU
     65     // Returns the texture proxy. If we're going to generate and cache the texture, we should use
     66     // the passed in key (if the key is valid). If genType is AllowedTexGenType::kCheap and the
     67     // texture is not trivial to construct, returns nullptr.
     68     sk_sp<GrTextureProxy> lockTextureProxy(GrRecordingContext*,
     69                                            const GrUniqueKey& key,
     70                                            SkImage::CachingHint,
     71                                            bool willBeMipped,
     72                                            GrTextureMaker::AllowedTexGenType genType) const;
     73 
     74     void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) const;
     75 #endif
     76 
     77 private:
     78     class ScopedGenerator;
     79 
     80     sk_sp<SharedGenerator> fSharedGenerator;
     81     // Note that fInfo is not necessarily the info from the generator. It may be cropped by
     82     // onMakeSubset and its color type/space may be changed by onMakeColorTypeAndColorSpace.
     83     const SkImageInfo      fInfo;
     84     const SkIPoint         fOrigin;
     85 
     86     uint32_t fUniqueID;
     87 
     88     // Repeated calls to onMakeColorTypeAndColorSpace will result in a proliferation of unique IDs
     89     // and SkImage_Lazy instances. Cache the result of the last successful call.
     90     mutable SkMutex             fOnMakeColorTypeAndSpaceMutex;
     91     mutable sk_sp<SkImage>      fOnMakeColorTypeAndSpaceResult;
     92 
     93 #if SK_SUPPORT_GPU
     94     // When the SkImage_Lazy goes away, we will iterate over all the unique keys we've used and
     95     // send messages to the GrContexts to say the unique keys are no longer valid. The GrContexts
     96     // can then release the resources, conntected with the those unique keys, from their caches.
     97     mutable SkTDArray<GrUniqueKeyInvalidatedMessage*> fUniqueKeyInvalidatedMessages;
     98 #endif
     99 
    100     typedef SkImage_Base INHERITED;
    101 };
    102 
    103 #endif
    104