2015-08-13 20:32:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkImageCacherator_DEFINED
|
|
|
|
#define SkImageCacherator_DEFINED
|
|
|
|
|
|
|
|
#include "SkImageGenerator.h"
|
2015-08-26 21:16:43 +00:00
|
|
|
#include "SkMutex.h"
|
|
|
|
#include "SkTemplates.h"
|
2015-08-13 20:32:39 +00:00
|
|
|
|
|
|
|
class GrContext;
|
2015-10-20 14:58:01 +00:00
|
|
|
class GrTextureParams;
|
|
|
|
class GrUniqueKey;
|
2015-08-13 20:32:39 +00:00
|
|
|
class SkBitmap;
|
2015-09-18 15:07:31 +00:00
|
|
|
class SkImage;
|
2015-08-13 20:32:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal class to manage caching the output of an ImageGenerator.
|
|
|
|
*/
|
|
|
|
class SkImageCacherator {
|
|
|
|
public:
|
|
|
|
// Takes ownership of the generator
|
2015-08-18 18:16:09 +00:00
|
|
|
static SkImageCacherator* NewFromGenerator(SkImageGenerator*, const SkIRect* subset = nullptr);
|
2015-08-13 21:06:46 +00:00
|
|
|
|
2015-08-18 18:16:09 +00:00
|
|
|
const SkImageInfo& info() const { return fInfo; }
|
|
|
|
uint32_t uniqueID() const { return fUniqueID; }
|
2015-08-13 20:32:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* On success (true), bitmap will point to the pixels for this generator. If this returns
|
|
|
|
* false, the bitmap will be reset to empty.
|
2015-09-18 15:07:31 +00:00
|
|
|
*
|
|
|
|
* If not NULL, the client will be notified (->notifyAddedToCache()) when resources are
|
|
|
|
* added to the cache on its behalf.
|
2015-08-13 20:32:39 +00:00
|
|
|
*/
|
2015-11-23 20:32:16 +00:00
|
|
|
bool lockAsBitmap(SkBitmap*, const SkImage* client,
|
|
|
|
SkImage::CachingHint = SkImage::kAllow_CachingHint);
|
2015-08-13 20:32:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a ref() on the texture produced by this generator. The caller must call unref()
|
2015-08-27 14:41:13 +00:00
|
|
|
* when it is done. Will return nullptr on failure.
|
2015-08-13 20:32:39 +00:00
|
|
|
*
|
2015-09-18 15:07:31 +00:00
|
|
|
* If not NULL, the client will be notified (->notifyAddedToCache()) when resources are
|
|
|
|
* added to the cache on its behalf.
|
|
|
|
*
|
2015-08-13 21:06:46 +00:00
|
|
|
* The caller is responsible for calling texture->unref() when they are done.
|
2015-08-13 20:32:39 +00:00
|
|
|
*/
|
2016-06-06 20:10:58 +00:00
|
|
|
GrTexture* lockAsTexture(GrContext*, const GrTextureParams&,
|
|
|
|
SkSourceGammaTreatment gammaTreatment, const SkImage* client,
|
2015-11-23 20:32:16 +00:00
|
|
|
SkImage::CachingHint = SkImage::kAllow_CachingHint);
|
2015-08-13 20:32:39 +00:00
|
|
|
|
2015-08-26 21:16:43 +00:00
|
|
|
/**
|
|
|
|
* If the underlying src naturally is represented by an encoded blob (in SkData), this returns
|
|
|
|
* a ref to that data. If not, it returns null.
|
2016-01-05 17:16:19 +00:00
|
|
|
*
|
|
|
|
* If a GrContext is specified, then the caller is only interested in gpu-specific encoded
|
|
|
|
* formats, so others (e.g. PNG) can just return nullptr.
|
2015-08-26 21:16:43 +00:00
|
|
|
*/
|
2016-01-05 17:16:19 +00:00
|
|
|
SkData* refEncoded(GrContext*);
|
2015-08-26 21:16:43 +00:00
|
|
|
|
2015-11-24 19:44:47 +00:00
|
|
|
// Only return true if the generate has already been cached.
|
|
|
|
bool lockAsBitmapOnlyIfAlreadyCached(SkBitmap*);
|
|
|
|
// Call the underlying generator directly
|
|
|
|
bool directGeneratePixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
|
|
|
|
int srcX, int srcY);
|
|
|
|
|
2015-08-13 20:32:39 +00:00
|
|
|
private:
|
2015-08-18 18:16:09 +00:00
|
|
|
SkImageCacherator(SkImageGenerator*, const SkImageInfo&, const SkIPoint&, uint32_t uniqueID);
|
2015-08-13 21:06:46 +00:00
|
|
|
|
2015-08-26 21:16:43 +00:00
|
|
|
bool generateBitmap(SkBitmap*);
|
2015-11-23 20:32:16 +00:00
|
|
|
bool tryLockAsBitmap(SkBitmap*, const SkImage*, SkImage::CachingHint);
|
2015-09-30 19:21:45 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2015-10-20 14:58:01 +00:00
|
|
|
// Returns the texture. If the cacherator is generating the texture and wants to cache it,
|
|
|
|
// it should use the passed in key (if the key is valid).
|
2015-11-23 20:32:16 +00:00
|
|
|
GrTexture* lockTexture(GrContext*, const GrUniqueKey& key, const SkImage* client,
|
2016-06-06 20:10:58 +00:00
|
|
|
SkImage::CachingHint, bool willBeMipped, SkSourceGammaTreatment);
|
2015-09-30 19:21:45 +00:00
|
|
|
#endif
|
2015-08-13 20:32:39 +00:00
|
|
|
|
2015-08-26 21:16:43 +00:00
|
|
|
class ScopedGenerator {
|
|
|
|
SkImageCacherator* fCacher;
|
|
|
|
public:
|
|
|
|
ScopedGenerator(SkImageCacherator* cacher) : fCacher(cacher) {
|
|
|
|
fCacher->fMutexForGenerator.acquire();
|
|
|
|
}
|
|
|
|
~ScopedGenerator() {
|
|
|
|
fCacher->fMutexForGenerator.release();
|
|
|
|
}
|
|
|
|
SkImageGenerator* operator->() const { return fCacher->fNotThreadSafeGenerator; }
|
|
|
|
operator SkImageGenerator*() const { return fCacher->fNotThreadSafeGenerator; }
|
|
|
|
};
|
|
|
|
|
|
|
|
SkMutex fMutexForGenerator;
|
|
|
|
SkAutoTDelete<SkImageGenerator> fNotThreadSafeGenerator;
|
|
|
|
|
2015-08-18 18:16:09 +00:00
|
|
|
const SkImageInfo fInfo;
|
|
|
|
const SkIPoint fOrigin;
|
|
|
|
const uint32_t fUniqueID;
|
2015-09-30 19:21:45 +00:00
|
|
|
|
2015-12-08 18:53:43 +00:00
|
|
|
friend class GrImageTextureMaker;
|
2015-08-13 20:32:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|