2018-05-29 20:13:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PromiseImageHelper_DEFINED
|
|
|
|
#define PromiseImageHelper_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkDeferredDisplayListRecorder.h"
|
|
|
|
#include "include/core/SkPromiseImageTexture.h"
|
2021-11-02 15:38:39 +00:00
|
|
|
#include "include/core/SkRefCnt.h"
|
2020-08-27 15:00:04 +00:00
|
|
|
#include "include/core/SkYUVAPixmaps.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/gpu/GrBackendSurface.h"
|
|
|
|
#include "include/private/SkTArray.h"
|
|
|
|
#include "src/core/SkCachedData.h"
|
|
|
|
#include "src/core/SkTLazy.h"
|
2018-05-29 20:13:26 +00:00
|
|
|
|
2020-10-20 14:11:53 +00:00
|
|
|
class GrDirectContext;
|
2018-05-29 20:13:26 +00:00
|
|
|
class SkImage;
|
2020-07-14 21:16:32 +00:00
|
|
|
class SkMipmap;
|
2018-05-29 20:13:26 +00:00
|
|
|
class SkPicture;
|
2020-07-13 17:17:09 +00:00
|
|
|
class SkTaskGroup;
|
2018-05-29 20:13:26 +00:00
|
|
|
|
2020-04-23 19:10:03 +00:00
|
|
|
// This class acts as a proxy for a GrBackendTexture that backs an image.
|
|
|
|
// Whenever a promise image is created for the image, the promise image receives a ref to
|
|
|
|
// potentially several of these objects. Once all the promise images receive their done
|
|
|
|
// callbacks this object is deleted - removing the GrBackendTexture from VRAM.
|
|
|
|
// Note that while the DDLs are being created in the threads, the PromiseImageHelper holds
|
|
|
|
// a ref on all the PromiseImageCallbackContexts. However, once all the threads are done
|
|
|
|
// it drops all of its refs (via "reset").
|
|
|
|
class PromiseImageCallbackContext : public SkRefCnt {
|
|
|
|
public:
|
2020-07-07 17:18:47 +00:00
|
|
|
PromiseImageCallbackContext(GrDirectContext* direct, GrBackendFormat backendFormat)
|
|
|
|
: fContext(direct)
|
2020-04-23 19:10:03 +00:00
|
|
|
, fBackendFormat(backendFormat) {}
|
|
|
|
|
2020-07-21 21:03:56 +00:00
|
|
|
~PromiseImageCallbackContext() override;
|
2020-04-23 19:10:03 +00:00
|
|
|
|
|
|
|
const GrBackendFormat& backendFormat() const { return fBackendFormat; }
|
|
|
|
|
|
|
|
void setBackendTexture(const GrBackendTexture& backendTexture);
|
|
|
|
|
2020-07-07 17:18:47 +00:00
|
|
|
void destroyBackendTexture();
|
2020-04-23 19:10:03 +00:00
|
|
|
|
|
|
|
sk_sp<SkPromiseImageTexture> fulfill() {
|
|
|
|
++fTotalFulfills;
|
|
|
|
return fPromiseImageTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void release() {
|
|
|
|
++fDoneCnt;
|
2021-01-19 17:11:07 +00:00
|
|
|
SkASSERT(fDoneCnt <= fNumImages);
|
2020-04-23 19:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wasAddedToImage() { fNumImages++; }
|
|
|
|
|
|
|
|
const SkPromiseImageTexture* promiseImageTexture() const {
|
|
|
|
return fPromiseImageTexture.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkPromiseImageTexture> PromiseImageFulfillProc(void* textureContext) {
|
|
|
|
auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
|
|
|
|
return callbackContext->fulfill();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PromiseImageReleaseProc(void* textureContext) {
|
|
|
|
auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
|
|
|
|
callbackContext->release();
|
|
|
|
callbackContext->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-07 17:18:47 +00:00
|
|
|
GrDirectContext* fContext;
|
2020-04-23 19:10:03 +00:00
|
|
|
GrBackendFormat fBackendFormat;
|
|
|
|
sk_sp<SkPromiseImageTexture> fPromiseImageTexture;
|
|
|
|
int fNumImages = 0;
|
|
|
|
int fTotalFulfills = 0;
|
|
|
|
int fDoneCnt = 0;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = SkRefCnt;
|
2020-04-23 19:10:03 +00:00
|
|
|
};
|
|
|
|
|
2018-05-29 20:13:26 +00:00
|
|
|
// This class consolidates tracking & extraction of the original image data from an skp,
|
|
|
|
// the upload of said data to the GPU and the fulfillment of promise images.
|
|
|
|
//
|
|
|
|
// The way this works is:
|
|
|
|
// the original skp is converted to SkData and all its image info is extracted into this
|
2021-03-29 17:29:40 +00:00
|
|
|
// class and only indices into this class are left in the SkData
|
|
|
|
// the PromiseImageCallbackContexts are created for each image
|
|
|
|
// the SkData is then reinflated into an SkPicture with promise images replacing all the indices
|
|
|
|
// (all in recreateSKP)
|
2018-05-29 20:13:26 +00:00
|
|
|
//
|
2021-03-29 17:29:40 +00:00
|
|
|
// Prior to replaying in threads, all the images are uploaded to the gpu
|
|
|
|
// (in uploadAllToGPU)
|
2018-05-29 20:13:26 +00:00
|
|
|
//
|
|
|
|
// This class is then reset - dropping all of its refs on the PromiseImageCallbackContexts
|
|
|
|
//
|
|
|
|
// Each done callback unrefs its PromiseImageCallbackContext so, once all the promise images
|
2018-09-27 18:26:47 +00:00
|
|
|
// are done, the PromiseImageCallbackContext is freed and its GrBackendTexture removed
|
2018-05-29 20:13:26 +00:00
|
|
|
// from VRAM
|
|
|
|
//
|
|
|
|
// Note: if DDLs are going to be replayed multiple times, the reset call can be delayed until
|
|
|
|
// all the replaying is complete. This will pin the GrBackendTextures in VRAM.
|
|
|
|
class DDLPromiseImageHelper {
|
|
|
|
public:
|
2020-09-01 19:01:15 +00:00
|
|
|
DDLPromiseImageHelper(const SkYUVAPixmapInfo::SupportedDataTypes& supportedYUVADataTypes)
|
|
|
|
: fSupportedYUVADataTypes(supportedYUVADataTypes) {}
|
2019-02-28 15:03:03 +00:00
|
|
|
~DDLPromiseImageHelper() = default;
|
2018-05-29 20:13:26 +00:00
|
|
|
|
2021-03-29 17:29:40 +00:00
|
|
|
// Convert the input SkPicture into a new one which has promise images rather than live
|
|
|
|
// images.
|
|
|
|
sk_sp<SkPicture> recreateSKP(GrDirectContext*, SkPicture*);
|
2020-02-14 17:36:37 +00:00
|
|
|
|
2020-07-07 17:18:47 +00:00
|
|
|
void uploadAllToGPU(SkTaskGroup*, GrDirectContext*);
|
|
|
|
void deleteAllFromGPU(SkTaskGroup*, GrDirectContext*);
|
2018-05-29 20:13:26 +00:00
|
|
|
|
2021-03-29 17:29:40 +00:00
|
|
|
// Remove this class' refs on the promise images and the PromiseImageCallbackContexts
|
|
|
|
void reset() {
|
|
|
|
fImageInfo.reset();
|
|
|
|
fPromiseImages.reset();
|
|
|
|
}
|
2018-05-29 20:13:26 +00:00
|
|
|
|
|
|
|
private:
|
2021-03-29 17:29:40 +00:00
|
|
|
void createCallbackContexts(GrDirectContext*);
|
|
|
|
// reinflate a deflated SKP, replacing all the indices with promise images.
|
|
|
|
sk_sp<SkPicture> reinflateSKP(sk_sp<GrContextThreadSafeProxy>, SkData* deflatedSKP);
|
|
|
|
|
2018-05-29 20:13:26 +00:00
|
|
|
// This is the information extracted into this class from the parsing of the skp file.
|
|
|
|
// Once it has all been uploaded to the GPU and distributed to the promise images, it
|
|
|
|
// is all dropped via "reset".
|
|
|
|
class PromiseImageInfo {
|
|
|
|
public:
|
2020-04-17 20:20:55 +00:00
|
|
|
PromiseImageInfo(int index, uint32_t originalUniqueID, const SkImageInfo& ii);
|
|
|
|
PromiseImageInfo(PromiseImageInfo&& other);
|
|
|
|
~PromiseImageInfo();
|
2018-09-27 18:26:47 +00:00
|
|
|
|
|
|
|
int index() const { return fIndex; }
|
|
|
|
uint32_t originalUniqueID() const { return fOriginalUniqueID; }
|
2020-08-25 16:40:32 +00:00
|
|
|
bool isYUV() const { return fYUVAPixmaps.isValid(); }
|
2018-09-27 18:26:47 +00:00
|
|
|
|
2021-03-03 23:12:56 +00:00
|
|
|
SkISize overallDimensions() const { return fImageInfo.dimensions(); }
|
2018-09-27 18:26:47 +00:00
|
|
|
SkColorType overallColorType() const { return fImageInfo.colorType(); }
|
|
|
|
SkAlphaType overallAlphaType() const { return fImageInfo.alphaType(); }
|
|
|
|
sk_sp<SkColorSpace> refOverallColorSpace() const { return fImageInfo.refColorSpace(); }
|
|
|
|
|
2020-11-10 19:17:58 +00:00
|
|
|
const SkYUVAInfo& yuvaInfo() const { return fYUVAPixmaps.yuvaInfo(); }
|
|
|
|
|
2018-09-27 18:26:47 +00:00
|
|
|
const SkPixmap& yuvPixmap(int index) const {
|
|
|
|
SkASSERT(this->isYUV());
|
2020-08-25 16:40:32 +00:00
|
|
|
return fYUVAPixmaps.planes()[index];
|
2018-09-27 18:26:47 +00:00
|
|
|
}
|
2020-04-17 20:20:55 +00:00
|
|
|
|
|
|
|
const SkBitmap& baseLevel() const {
|
2018-09-27 18:26:47 +00:00
|
|
|
SkASSERT(!this->isYUV());
|
2020-04-17 20:20:55 +00:00
|
|
|
return fBaseLevel;
|
2018-09-27 18:26:47 +00:00
|
|
|
}
|
2020-04-17 20:20:55 +00:00
|
|
|
// This returns an array of all the available mipLevels - suitable for passing into
|
|
|
|
// createBackendTexture.
|
2020-08-07 17:05:14 +00:00
|
|
|
std::unique_ptr<SkPixmap[]> normalMipLevels() const;
|
2020-04-17 20:20:55 +00:00
|
|
|
int numMipLevels() const;
|
2018-09-27 18:26:47 +00:00
|
|
|
|
|
|
|
void setCallbackContext(int index, sk_sp<PromiseImageCallbackContext> callbackContext) {
|
2021-01-11 19:40:44 +00:00
|
|
|
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
|
2018-09-27 18:26:47 +00:00
|
|
|
fCallbackContexts[index] = callbackContext;
|
|
|
|
}
|
2019-01-10 17:09:52 +00:00
|
|
|
PromiseImageCallbackContext* callbackContext(int index) const {
|
2021-01-11 19:40:44 +00:00
|
|
|
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
|
2018-09-27 18:26:47 +00:00
|
|
|
return fCallbackContexts[index].get();
|
|
|
|
}
|
|
|
|
sk_sp<PromiseImageCallbackContext> refCallbackContext(int index) const {
|
2021-01-11 19:40:44 +00:00
|
|
|
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
|
2018-09-27 18:26:47 +00:00
|
|
|
return fCallbackContexts[index];
|
|
|
|
}
|
|
|
|
|
2022-04-18 12:58:50 +00:00
|
|
|
GrMipmapped mipmapped(int index) const {
|
2020-04-17 20:20:55 +00:00
|
|
|
if (this->isYUV()) {
|
2020-07-21 13:27:25 +00:00
|
|
|
return GrMipmapped::kNo;
|
2020-04-17 20:20:55 +00:00
|
|
|
}
|
2020-07-21 13:27:25 +00:00
|
|
|
return fMipLevels ? GrMipmapped::kYes : GrMipmapped::kNo;
|
2020-04-17 20:20:55 +00:00
|
|
|
}
|
2020-02-14 17:36:37 +00:00
|
|
|
const GrBackendFormat& backendFormat(int index) const {
|
2021-01-11 19:40:44 +00:00
|
|
|
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
|
2020-02-14 17:36:37 +00:00
|
|
|
return fCallbackContexts[index]->backendFormat();
|
|
|
|
}
|
2019-01-11 21:03:19 +00:00
|
|
|
const SkPromiseImageTexture* promiseTexture(int index) const {
|
2021-01-11 19:40:44 +00:00
|
|
|
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
|
2019-01-11 21:03:19 +00:00
|
|
|
return fCallbackContexts[index]->promiseImageTexture();
|
2018-09-27 18:26:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:16:32 +00:00
|
|
|
void setMipLevels(const SkBitmap& baseLevel, std::unique_ptr<SkMipmap> mipLevels);
|
2018-09-27 18:26:47 +00:00
|
|
|
|
2020-07-29 01:06:43 +00:00
|
|
|
/** Takes ownership of the plane data. */
|
2020-08-27 15:00:04 +00:00
|
|
|
void setYUVPlanes(SkYUVAPixmaps yuvaPixmaps) { fYUVAPixmaps = std::move(yuvaPixmaps); }
|
2020-08-25 16:40:32 +00:00
|
|
|
|
2018-09-27 18:26:47 +00:00
|
|
|
private:
|
|
|
|
const int fIndex; // index in the 'fImageInfo' array
|
|
|
|
const uint32_t fOriginalUniqueID; // original ID for deduping
|
|
|
|
|
|
|
|
const SkImageInfo fImageInfo; // info for the overarching image
|
|
|
|
|
2020-04-17 20:20:55 +00:00
|
|
|
// CPU-side cache of a normal SkImage's mipmap levels
|
|
|
|
SkBitmap fBaseLevel;
|
2020-07-14 21:16:32 +00:00
|
|
|
std::unique_ptr<SkMipmap> fMipLevels;
|
2018-09-27 18:26:47 +00:00
|
|
|
|
|
|
|
// CPU-side cache of a YUV SkImage's contents
|
2020-08-27 15:00:04 +00:00
|
|
|
SkYUVAPixmaps fYUVAPixmaps;
|
2018-09-27 18:26:47 +00:00
|
|
|
|
2018-10-29 20:26:02 +00:00
|
|
|
// Up to SkYUVASizeInfo::kMaxCount for a YUVA image. Only one for a normal image.
|
2021-01-11 19:40:44 +00:00
|
|
|
sk_sp<PromiseImageCallbackContext> fCallbackContexts[SkYUVAInfo::kMaxPlanes];
|
2018-05-29 20:13:26 +00:00
|
|
|
};
|
|
|
|
|
2021-03-03 23:12:56 +00:00
|
|
|
struct DeserialImageProcContext {
|
|
|
|
sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy;
|
2021-03-29 17:29:40 +00:00
|
|
|
DDLPromiseImageHelper* fHelper;
|
2018-05-29 20:13:26 +00:00
|
|
|
};
|
|
|
|
|
2020-07-07 17:18:47 +00:00
|
|
|
static void CreateBETexturesForPromiseImage(GrDirectContext*, PromiseImageInfo*);
|
2020-11-10 19:17:58 +00:00
|
|
|
static void DeleteBETexturesForPromiseImage(PromiseImageInfo*);
|
2020-02-14 17:36:37 +00:00
|
|
|
|
|
|
|
static sk_sp<SkImage> CreatePromiseImages(const void* rawData, size_t length, void* ctxIn);
|
2018-05-29 20:13:26 +00:00
|
|
|
|
|
|
|
bool isValidID(int id) const { return id >= 0 && id < fImageInfo.count(); }
|
|
|
|
const PromiseImageInfo& getInfo(int id) const { return fImageInfo[id]; }
|
2020-07-07 17:18:47 +00:00
|
|
|
void uploadImage(GrDirectContext*, PromiseImageInfo*);
|
2018-05-29 20:13:26 +00:00
|
|
|
|
|
|
|
// returns -1 if not found
|
|
|
|
int findImage(SkImage* image) const;
|
|
|
|
|
|
|
|
// returns -1 on failure
|
|
|
|
int addImage(SkImage* image);
|
|
|
|
|
|
|
|
// returns -1 on failure
|
|
|
|
int findOrDefineImage(SkImage* image);
|
|
|
|
|
2020-09-01 19:01:15 +00:00
|
|
|
SkYUVAPixmapInfo::SupportedDataTypes fSupportedYUVADataTypes;
|
2021-03-29 17:29:40 +00:00
|
|
|
SkTArray<PromiseImageInfo> fImageInfo;
|
|
|
|
|
|
|
|
// TODO: review the use of 'fPromiseImages' - it doesn't seem useful/necessary
|
|
|
|
SkTArray<sk_sp<SkImage>> fPromiseImages; // All the promise images in the
|
|
|
|
// reconstituted picture
|
2018-05-29 20:13:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|