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-01-10 16:33:06 +00:00
|
|
|
#include "GrBackendSurface.h"
|
2019-01-10 17:09:52 +00:00
|
|
|
#include "SkBitmap.h"
|
2019-01-10 16:33:06 +00:00
|
|
|
#include "SkCachedData.h"
|
2019-01-10 17:09:52 +00:00
|
|
|
#include "SkDeferredDisplayListRecorder.h"
|
|
|
|
#include "SkPromiseImageTexture.h"
|
|
|
|
#include "SkTArray.h"
|
|
|
|
#include "SkTLazy.h"
|
2018-10-18 18:36:59 +00:00
|
|
|
#include "SkYUVAIndex.h"
|
2018-10-29 20:26:02 +00:00
|
|
|
#include "SkYUVASizeInfo.h"
|
2018-05-29 20:13:26 +00:00
|
|
|
|
|
|
|
class GrContext;
|
|
|
|
class SkImage;
|
|
|
|
class SkPicture;
|
2018-10-18 18:36:59 +00:00
|
|
|
struct SkYUVAIndex;
|
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
|
|
|
|
// class and only indices into this class are left in the SkData (via deflateSKP)
|
|
|
|
//
|
|
|
|
// Prior to replaying in threads, all the images stored in this class are uploaded to the
|
|
|
|
// gpu and PromiseImageCallbackContexts are created for them (via uploadAllToGPU)
|
|
|
|
//
|
|
|
|
// Each thread reinflates the SkData into an SkPicture replacing all the indices w/
|
|
|
|
// promise images (all using the same GrBackendTexture and getting a ref to the
|
|
|
|
// appropriate PromiseImageCallbackContext) (via reinflateSKP).
|
|
|
|
//
|
|
|
|
// 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:
|
2019-02-28 15:03:03 +00:00
|
|
|
DDLPromiseImageHelper() = default;
|
|
|
|
~DDLPromiseImageHelper() = default;
|
2018-05-29 20:13:26 +00:00
|
|
|
|
|
|
|
// Convert the SkPicture into SkData replacing all the SkImages with an index.
|
|
|
|
sk_sp<SkData> deflateSKP(const SkPicture* inputPicture);
|
|
|
|
|
|
|
|
void uploadAllToGPU(GrContext* context);
|
|
|
|
|
|
|
|
// reinflate a deflated SKP, replacing all the indices with promise images.
|
|
|
|
sk_sp<SkPicture> reinflateSKP(SkDeferredDisplayListRecorder*,
|
|
|
|
SkData* compressedPicture,
|
|
|
|
SkTArray<sk_sp<SkImage>>* promiseImages) const;
|
|
|
|
|
|
|
|
// Remove this class' refs on the PromiseImageCallbackContexts
|
|
|
|
void reset() { fImageInfo.reset(); }
|
|
|
|
|
|
|
|
private:
|
2018-09-27 18:26:47 +00:00
|
|
|
// This class acts as a proxy for a GrBackendTexture that is part of an image.
|
2018-05-29 20:13:26 +00:00
|
|
|
// Whenever a promise image is created for the image, the promise image receives a ref to
|
2018-09-27 18:26:47 +00:00
|
|
|
// potentially several of these objects. Once all the promise images receive their done
|
|
|
|
// callbacks this object is deleted - removing the GrBackendTexture from VRAM.
|
2018-05-29 20:13:26 +00:00
|
|
|
// 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:
|
|
|
|
PromiseImageCallbackContext(GrContext* context) : fContext(context) {}
|
|
|
|
|
|
|
|
~PromiseImageCallbackContext();
|
|
|
|
|
2019-01-10 17:09:52 +00:00
|
|
|
void setBackendTexture(const GrBackendTexture& backendTexture);
|
|
|
|
|
2019-01-11 21:03:19 +00:00
|
|
|
sk_sp<SkPromiseImageTexture> fulfill() {
|
|
|
|
SkASSERT(fPromiseImageTexture);
|
2019-01-10 17:09:52 +00:00
|
|
|
SkASSERT(fUnreleasedFulfills >= 0);
|
|
|
|
++fUnreleasedFulfills;
|
|
|
|
++fTotalFulfills;
|
2019-01-11 21:03:19 +00:00
|
|
|
return fPromiseImageTexture;
|
2018-05-29 20:13:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 17:09:52 +00:00
|
|
|
void release() {
|
|
|
|
SkASSERT(fUnreleasedFulfills > 0);
|
|
|
|
--fUnreleasedFulfills;
|
|
|
|
++fTotalReleases;
|
|
|
|
}
|
|
|
|
|
|
|
|
void done() {
|
|
|
|
++fDoneCnt;
|
|
|
|
SkASSERT(fDoneCnt <= fNumImages);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wasAddedToImage() { fNumImages++; }
|
|
|
|
|
2019-01-11 21:03:19 +00:00
|
|
|
const SkPromiseImageTexture* promiseImageTexture() const {
|
|
|
|
return fPromiseImageTexture.get();
|
|
|
|
}
|
2018-05-29 20:13:26 +00:00
|
|
|
|
|
|
|
private:
|
2019-01-10 17:09:52 +00:00
|
|
|
GrContext* fContext;
|
2019-01-11 21:03:19 +00:00
|
|
|
sk_sp<SkPromiseImageTexture> fPromiseImageTexture;
|
2019-01-10 17:09:52 +00:00
|
|
|
int fNumImages = 0;
|
|
|
|
int fTotalFulfills = 0;
|
|
|
|
int fTotalReleases = 0;
|
|
|
|
int fUnreleasedFulfills = 0;
|
|
|
|
int fDoneCnt = 0;
|
2018-05-29 20:13:26 +00:00
|
|
|
|
|
|
|
typedef SkRefCnt INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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:
|
2018-09-27 18:26:47 +00:00
|
|
|
PromiseImageInfo(int index, uint32_t originalUniqueID, const SkImageInfo& ii)
|
|
|
|
: fIndex(index)
|
|
|
|
, fOriginalUniqueID(originalUniqueID)
|
|
|
|
, fImageInfo(ii) {
|
|
|
|
}
|
|
|
|
~PromiseImageInfo() {}
|
|
|
|
|
|
|
|
int index() const { return fIndex; }
|
|
|
|
uint32_t originalUniqueID() const { return fOriginalUniqueID; }
|
|
|
|
bool isYUV() const { return SkToBool(fYUVData.get()); }
|
|
|
|
|
|
|
|
int overallWidth() const { return fImageInfo.width(); }
|
|
|
|
int overallHeight() const { return fImageInfo.height(); }
|
|
|
|
SkColorType overallColorType() const { return fImageInfo.colorType(); }
|
|
|
|
SkAlphaType overallAlphaType() const { return fImageInfo.alphaType(); }
|
|
|
|
sk_sp<SkColorSpace> refOverallColorSpace() const { return fImageInfo.refColorSpace(); }
|
|
|
|
|
|
|
|
SkYUVColorSpace yuvColorSpace() const {
|
|
|
|
SkASSERT(this->isYUV());
|
|
|
|
return fYUVColorSpace;
|
|
|
|
}
|
2018-10-18 18:36:59 +00:00
|
|
|
const SkYUVAIndex* yuvaIndices() const {
|
|
|
|
SkASSERT(this->isYUV());
|
|
|
|
return fYUVAIndices;
|
|
|
|
}
|
2018-09-27 18:26:47 +00:00
|
|
|
const SkPixmap& yuvPixmap(int index) const {
|
|
|
|
SkASSERT(this->isYUV());
|
2018-10-29 20:26:02 +00:00
|
|
|
SkASSERT(index >= 0 && index < SkYUVASizeInfo::kMaxCount);
|
2018-09-27 18:26:47 +00:00
|
|
|
return fYUVPlanes[index];
|
|
|
|
}
|
|
|
|
const SkBitmap& normalBitmap() const {
|
|
|
|
SkASSERT(!this->isYUV());
|
|
|
|
return fBitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setCallbackContext(int index, sk_sp<PromiseImageCallbackContext> callbackContext) {
|
2018-10-29 20:26:02 +00:00
|
|
|
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 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 {
|
2018-10-29 20:26:02 +00:00
|
|
|
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
|
2018-09-27 18:26:47 +00:00
|
|
|
return fCallbackContexts[index].get();
|
|
|
|
}
|
|
|
|
sk_sp<PromiseImageCallbackContext> refCallbackContext(int index) const {
|
2018-10-29 20:26:02 +00:00
|
|
|
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
|
2018-09-27 18:26:47 +00:00
|
|
|
return fCallbackContexts[index];
|
|
|
|
}
|
|
|
|
|
2019-01-11 21:03:19 +00:00
|
|
|
const SkPromiseImageTexture* promiseTexture(int index) const {
|
2018-10-29 20:26:02 +00:00
|
|
|
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
|
2019-01-11 21:03:19 +00:00
|
|
|
return fCallbackContexts[index]->promiseImageTexture();
|
2018-09-27 18:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setNormalBitmap(const SkBitmap& bm) { fBitmap = bm; }
|
|
|
|
|
2018-10-18 18:36:59 +00:00
|
|
|
void setYUVData(sk_sp<SkCachedData> yuvData,
|
|
|
|
SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
|
|
|
|
SkYUVColorSpace cs) {
|
2018-09-27 18:26:47 +00:00
|
|
|
fYUVData = yuvData;
|
2018-10-18 18:36:59 +00:00
|
|
|
memcpy(fYUVAIndices, yuvaIndices, sizeof(fYUVAIndices));
|
2018-09-27 18:26:47 +00:00
|
|
|
fYUVColorSpace = cs;
|
|
|
|
}
|
|
|
|
void addYUVPlane(int index, const SkImageInfo& ii, const void* plane, size_t widthBytes) {
|
|
|
|
SkASSERT(this->isYUV());
|
2018-10-29 20:26:02 +00:00
|
|
|
SkASSERT(index >= 0 && index < SkYUVASizeInfo::kMaxCount);
|
2018-09-27 18:26:47 +00:00
|
|
|
fYUVPlanes[index].reset(ii, plane, widthBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// CPU-side cache of a normal SkImage's contents
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
|
|
|
|
// CPU-side cache of a YUV SkImage's contents
|
|
|
|
sk_sp<SkCachedData> fYUVData; // when !null, this is a YUV image
|
|
|
|
SkYUVColorSpace fYUVColorSpace = kJPEG_SkYUVColorSpace;
|
2018-10-18 18:36:59 +00:00
|
|
|
SkYUVAIndex fYUVAIndices[SkYUVAIndex::kIndexCount];
|
2018-10-29 20:26:02 +00:00
|
|
|
SkPixmap fYUVPlanes[SkYUVASizeInfo::kMaxCount];
|
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.
|
|
|
|
sk_sp<PromiseImageCallbackContext> fCallbackContexts[SkYUVASizeInfo::kMaxCount];
|
2018-05-29 20:13:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This stack-based context allows each thread to re-inflate the image indices into
|
|
|
|
// promise images while still using the same GrBackendTexture.
|
|
|
|
struct PerRecorderContext {
|
|
|
|
SkDeferredDisplayListRecorder* fRecorder;
|
|
|
|
const DDLPromiseImageHelper* fHelper;
|
|
|
|
SkTArray<sk_sp<SkImage>>* fPromiseImages;
|
|
|
|
};
|
|
|
|
|
2019-01-11 21:03:19 +00:00
|
|
|
static sk_sp<SkPromiseImageTexture> PromiseImageFulfillProc(void* textureContext) {
|
2018-05-29 20:13:26 +00:00
|
|
|
auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
|
2019-01-11 21:03:19 +00:00
|
|
|
return callbackContext->fulfill();
|
2018-05-29 20:13:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 21:03:19 +00:00
|
|
|
static void PromiseImageReleaseProc(void* textureContext) {
|
2018-05-29 20:13:26 +00:00
|
|
|
auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
|
2019-01-10 17:09:52 +00:00
|
|
|
callbackContext->release();
|
2018-05-29 20:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PromiseImageDoneProc(void* textureContext) {
|
|
|
|
auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
|
2019-01-10 17:09:52 +00:00
|
|
|
callbackContext->done();
|
2018-05-29 20:13:26 +00:00
|
|
|
callbackContext->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkImage> PromiseImageCreator(const void* rawData, size_t length, void* ctxIn);
|
|
|
|
|
|
|
|
bool isValidID(int id) const { return id >= 0 && id < fImageInfo.count(); }
|
|
|
|
const PromiseImageInfo& getInfo(int id) const { return fImageInfo[id]; }
|
2019-01-10 17:09:52 +00:00
|
|
|
void uploadImage(GrContext*, 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);
|
|
|
|
|
|
|
|
SkTArray<PromiseImageInfo> fImageInfo;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|