Add a convenience to downcast contexts into GrDirectContext

This pattern of needing a safe downcast will continue to grow as we
add more explicit use of GrDirectContext and it's causing long ternary
operations that span multiple lines.

Change-Id: I9e2ebe5156e4245524a52d7c92ed3a8509e53151
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300901
Commit-Queue: Adlai Holler <adlai@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
Auto-Submit: Adlai Holler <adlai@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
This commit is contained in:
Adlai Holler 2020-07-07 10:27:55 -04:00 committed by Skia Commit-Bot
parent c7b49bbb9b
commit e3ad527e3f
6 changed files with 14 additions and 15 deletions

View File

@ -25,8 +25,7 @@ bool GMBench::isSuitableFor(Backend backend) {
}
void GMBench::onPerCanvasPreDraw(SkCanvas* canvas) {
auto direct = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext()
: nullptr;
auto direct = GrAsDirectContext(canvas->recordingContext());
if (fGM->gpuSetup(direct, canvas) != skiagm::DrawResult::kOk) {
fGpuSetupFailed = true;

View File

@ -133,11 +133,9 @@ static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas,
bool doYUV420,
SkString* errorMsg,
int pad = 0) {
if (doYUV420) {
if (!canvas->recordingContext() || !canvas->recordingContext()->asDirectContext()) {
errorMsg->printf("YUV420 only supported on direct GPU for now.");
return skiagm::DrawResult::kSkip;
}
if (doYUV420 && !GrAsDirectContext(canvas->recordingContext())) {
errorMsg->printf("YUV420 only supported on direct GPU for now.");
return skiagm::DrawResult::kSkip;
}
if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
*errorMsg = "Not supported on recording/vector backends.";

View File

@ -89,4 +89,11 @@ private:
typedef SkRefCnt INHERITED;
};
/**
* Safely cast a base context to direct context.
*/
static inline GrDirectContext* GrAsDirectContext(GrContext_Base* base) {
return base ? base->asDirectContext() : nullptr;
}
#endif

View File

@ -132,8 +132,7 @@ static void init(Source* source, std::shared_ptr<skiagm::GM> gm) {
source->size = gm->getISize();
source->tweak = [gm](GrContextOptions* options) { gm->modifyGrContextOptions(options); };
source->draw = [gm](SkCanvas* canvas) {
auto direct = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext()
: nullptr;
auto direct = GrAsDirectContext(canvas->recordingContext());
SkString err;
switch (gm->gpuSetup(direct, canvas, &err)) {

View File

@ -309,9 +309,7 @@ ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOv
auto restore = testCtx->makeCurrentAndAutoRestore();
// CONTEXT TODO: makeGrContext should return an sk_sp<GrDirectContext>
auto tmp = testCtx->makeGrContext(grOptions);
if (tmp) {
grCtx = sk_ref_sp<GrDirectContext>(tmp->asDirectContext());
}
grCtx = sk_ref_sp(GrAsDirectContext(tmp.get()));
}
if (!grCtx.get()) {
return ContextInfo();

View File

@ -31,9 +31,7 @@ void GMSlide::gpuTeardown() {
void GMSlide::draw(SkCanvas* canvas) {
SkString msg;
auto direct = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext()
: nullptr;
auto direct = GrAsDirectContext(canvas->recordingContext());
auto result = fGM->gpuSetup(direct, canvas, &msg);
if (result != skiagm::GM::DrawResult::kOk) {
return;