skia2/tools/DDLPromiseImageHelper.h

247 lines
9.8 KiB
C
Raw Normal View History

/*
* 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
#include "include/core/SkBitmap.h"
#include "include/core/SkDeferredDisplayListRecorder.h"
#include "include/core/SkPromiseImageTexture.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkYUVAPixmaps.h"
#include "include/gpu/GrBackendSurface.h"
#include "include/private/SkTArray.h"
#include "src/core/SkCachedData.h"
#include "src/core/SkTLazy.h"
class GrDirectContext;
class SkImage;
class SkMipmap;
class SkPicture;
class SkTaskGroup;
// 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:
PromiseImageCallbackContext(GrDirectContext* direct, GrBackendFormat backendFormat)
: fContext(direct)
, fBackendFormat(backendFormat) {}
~PromiseImageCallbackContext() override;
const GrBackendFormat& backendFormat() const { return fBackendFormat; }
void setBackendTexture(const GrBackendTexture& backendTexture);
void destroyBackendTexture();
sk_sp<SkPromiseImageTexture> fulfill() {
++fTotalFulfills;
return fPromiseImageTexture;
}
void release() {
++fDoneCnt;
Reland "Test all YUVA image factories with different encoded origins." This reverts commit f1650efc557e75520f810d21f5f574f1ece005fe. Reason for revert: fix for cpu/ddl configs Original change's description: > Revert "Test all YUVA image factories with different encoded origins." > > This reverts commit 2ba80af000b7efa1c54616bd12edc627e5cb45af. > > Reason for revert: new test fails ddl and cpu configs on imggen > > Original change's description: > > Test all YUVA image factories with different encoded origins. > > > > Now that SkImage_GpuYUVA stores a GrYUVATextureProxies it supports > > encoded origins. > > > > Modify wacky_yuv_format GMs to use different origins and remove > > restriction in SkImage::MakeFromYUVAPixmaps. > > > > Bug: skia:10632 > > Change-Id: I02477d592b7baba164944d629eeac48223698c10 > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/353623 > > Reviewed-by: Jim Van Verth <jvanverth@google.com> > > Commit-Queue: Brian Salomon <bsalomon@google.com> > > TBR=jvanverth@google.com,bsalomon@google.com > > Change-Id: If909ee4769cc1c74e1682a5e2870ec85a83f65c5 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:10632 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/354661 > Reviewed-by: Brian Salomon <bsalomon@google.com> > Commit-Queue: Brian Salomon <bsalomon@google.com> TBR=jvanverth@google.com,bsalomon@google.com # Not skipping CQ checks because this is a reland. Bug: skia:10632 Change-Id: Iafe79ab5b3ce0ff9e3a4007e5d8fbc44edded196 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/355630 Reviewed-by: Brian Salomon <bsalomon@google.com> Reviewed-by: Jim Van Verth <jvanverth@google.com> Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-01-19 17:11:07 +00:00
SkASSERT(fDoneCnt <= fNumImages);
}
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:
GrDirectContext* fContext;
GrBackendFormat fBackendFormat;
sk_sp<SkPromiseImageTexture> fPromiseImageTexture;
int fNumImages = 0;
int fTotalFulfills = 0;
int fDoneCnt = 0;
using INHERITED = SkRefCnt;
};
// 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
// 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)
//
// Prior to replaying in threads, all the images are uploaded to the gpu
// (in uploadAllToGPU)
//
// 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
// are done, the PromiseImageCallbackContext is freed and its GrBackendTexture removed
// 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) {}
~DDLPromiseImageHelper() = default;
// Convert the input SkPicture into a new one which has promise images rather than live
// images.
sk_sp<SkPicture> recreateSKP(GrDirectContext*, SkPicture*);
void uploadAllToGPU(SkTaskGroup*, GrDirectContext*);
void deleteAllFromGPU(SkTaskGroup*, GrDirectContext*);
// Remove this class' refs on the promise images and the PromiseImageCallbackContexts
void reset() {
fImageInfo.reset();
fPromiseImages.reset();
}
private:
void createCallbackContexts(GrDirectContext*);
// reinflate a deflated SKP, replacing all the indices with promise images.
sk_sp<SkPicture> reinflateSKP(sk_sp<GrContextThreadSafeProxy>, SkData* deflatedSKP);
// 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:
PromiseImageInfo(int index, uint32_t originalUniqueID, const SkImageInfo& ii);
PromiseImageInfo(PromiseImageInfo&& other);
~PromiseImageInfo();
int index() const { return fIndex; }
uint32_t originalUniqueID() const { return fOriginalUniqueID; }
bool isYUV() const { return fYUVAPixmaps.isValid(); }
Reland "Support sharing promise images between DDLs" This reverts commit 38b9a4bc3e3c11e0f17545d15e714a74c10211e3. Reason for revert: Fixed ASAN, TSAN, and other bugs via other CLs. Original change's description: > Revert "Support sharing promise images between DDLs" > > This reverts commit 07e11d48cb02ba9a3845e653c1772c25021e65a3. > > Reason for revert: Broke DDL3_ASAN and DDL3_TSAN > > Original change's description: > > Support sharing promise images between DDLs > > > > - Migrate our code to SkImage::MakePromiseTexture > > - Have DDLTileHelper share one SKP and one set of promise images across all tiles. > > - Disallow on-the-fly allocation of mips for promise textures. > > > > Bug: skia:10286 > > Change-Id: Ie35976958454fc520f3c9d860e6285441260c9f7 > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/291938 > > Commit-Queue: Adlai Holler <adlai@google.com> > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > TBR=robertphillips@google.com,adlai@google.com > > Change-Id: I939b14875d1a20e4a92eab94680adcfe9596ad81 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:10286 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/375738 > Reviewed-by: Adlai Holler <adlai@google.com> > Commit-Queue: Adlai Holler <adlai@google.com> Bug: skia:10286 Change-Id: Ibfd7dfcd72f10a4e29a87fa8c610f2dfd018e0db Cq-Include-Trybots: luci.skia.skia.primary:Test-Ubuntu18-Clang-Golo-GPU-QuadroP400-x86_64-Debug-All-DDL3_ASAN,Test-Ubuntu18-Clang-Golo-GPU-QuadroP400-x86_64-Release-All-DDL3_TSAN Reviewed-on: https://skia-review.googlesource.com/c/skia/+/375739 Reviewed-by: Robert Phillips <robertphillips@google.com> Commit-Queue: Adlai Holler <adlai@google.com>
2021-03-03 23:12:56 +00:00
SkISize overallDimensions() const { return fImageInfo.dimensions(); }
SkColorType overallColorType() const { return fImageInfo.colorType(); }
SkAlphaType overallAlphaType() const { return fImageInfo.alphaType(); }
sk_sp<SkColorSpace> refOverallColorSpace() const { return fImageInfo.refColorSpace(); }
const SkYUVAInfo& yuvaInfo() const { return fYUVAPixmaps.yuvaInfo(); }
const SkPixmap& yuvPixmap(int index) const {
SkASSERT(this->isYUV());
return fYUVAPixmaps.planes()[index];
}
const SkBitmap& baseLevel() const {
SkASSERT(!this->isYUV());
return fBaseLevel;
}
// This returns an array of all the available mipLevels - suitable for passing into
// createBackendTexture.
std::unique_ptr<SkPixmap[]> normalMipLevels() const;
int numMipLevels() const;
void setCallbackContext(int index, sk_sp<PromiseImageCallbackContext> callbackContext) {
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
fCallbackContexts[index] = callbackContext;
}
PromiseImageCallbackContext* callbackContext(int index) const {
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
return fCallbackContexts[index].get();
}
sk_sp<PromiseImageCallbackContext> refCallbackContext(int index) const {
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
return fCallbackContexts[index];
}
GrMipmapped mipmapped(int index) const {
if (this->isYUV()) {
return GrMipmapped::kNo;
}
return fMipLevels ? GrMipmapped::kYes : GrMipmapped::kNo;
}
const GrBackendFormat& backendFormat(int index) const {
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
return fCallbackContexts[index]->backendFormat();
}
const SkPromiseImageTexture* promiseTexture(int index) const {
SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
return fCallbackContexts[index]->promiseImageTexture();
}
void setMipLevels(const SkBitmap& baseLevel, std::unique_ptr<SkMipmap> mipLevels);
/** Takes ownership of the plane data. */
void setYUVPlanes(SkYUVAPixmaps yuvaPixmaps) { fYUVAPixmaps = std::move(yuvaPixmaps); }
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 mipmap levels
SkBitmap fBaseLevel;
std::unique_ptr<SkMipmap> fMipLevels;
// CPU-side cache of a YUV SkImage's contents
SkYUVAPixmaps fYUVAPixmaps;
// Up to SkYUVASizeInfo::kMaxCount for a YUVA image. Only one for a normal image.
sk_sp<PromiseImageCallbackContext> fCallbackContexts[SkYUVAInfo::kMaxPlanes];
};
Reland "Support sharing promise images between DDLs" This reverts commit 38b9a4bc3e3c11e0f17545d15e714a74c10211e3. Reason for revert: Fixed ASAN, TSAN, and other bugs via other CLs. Original change's description: > Revert "Support sharing promise images between DDLs" > > This reverts commit 07e11d48cb02ba9a3845e653c1772c25021e65a3. > > Reason for revert: Broke DDL3_ASAN and DDL3_TSAN > > Original change's description: > > Support sharing promise images between DDLs > > > > - Migrate our code to SkImage::MakePromiseTexture > > - Have DDLTileHelper share one SKP and one set of promise images across all tiles. > > - Disallow on-the-fly allocation of mips for promise textures. > > > > Bug: skia:10286 > > Change-Id: Ie35976958454fc520f3c9d860e6285441260c9f7 > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/291938 > > Commit-Queue: Adlai Holler <adlai@google.com> > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > TBR=robertphillips@google.com,adlai@google.com > > Change-Id: I939b14875d1a20e4a92eab94680adcfe9596ad81 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:10286 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/375738 > Reviewed-by: Adlai Holler <adlai@google.com> > Commit-Queue: Adlai Holler <adlai@google.com> Bug: skia:10286 Change-Id: Ibfd7dfcd72f10a4e29a87fa8c610f2dfd018e0db Cq-Include-Trybots: luci.skia.skia.primary:Test-Ubuntu18-Clang-Golo-GPU-QuadroP400-x86_64-Debug-All-DDL3_ASAN,Test-Ubuntu18-Clang-Golo-GPU-QuadroP400-x86_64-Release-All-DDL3_TSAN Reviewed-on: https://skia-review.googlesource.com/c/skia/+/375739 Reviewed-by: Robert Phillips <robertphillips@google.com> Commit-Queue: Adlai Holler <adlai@google.com>
2021-03-03 23:12:56 +00:00
struct DeserialImageProcContext {
sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy;
DDLPromiseImageHelper* fHelper;
};
static void CreateBETexturesForPromiseImage(GrDirectContext*, PromiseImageInfo*);
static void DeleteBETexturesForPromiseImage(PromiseImageInfo*);
static sk_sp<SkImage> CreatePromiseImages(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]; }
void uploadImage(GrDirectContext*, PromiseImageInfo*);
// 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;
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
};
#endif