Skip to content

Commit

Permalink
Make SkGpuDevice hold a GrRecordingContext (take 2)
Browse files Browse the repository at this point in the history
This makes the code reflect what is actually going on. During DDL
recording the SkGpuDevice only holds a recording context.

This can't land until the following Chrome-side CL lands:

https://chromium-review.googlesource.com/c/chromium/src/+/2277964 (Add GrContext.h include to skia renderer for upcoming Skia roll)

Reviewed-on: https://skia-review.googlesource.com/c/skia/+/299867
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Reviewed-by: Adlai Holler <adlai@google.com>
Change-Id: I6ef3896f5a270a4fa7af37f9121f68a66653cce2
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300896
  • Loading branch information
rphilli authored and Skia Commit-Bot committed Jul 9, 2020
1 parent 112a716 commit 16bf7d3
Show file tree
Hide file tree
Showing 23 changed files with 231 additions and 121 deletions.
4 changes: 2 additions & 2 deletions bench/GrMipMapBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class GrMipMapBench: public Benchmark {

void onDraw(int loops, SkCanvas* canvas) override {
if (!fSurface) {
GrContext* context = canvas->getGrContext();
if (nullptr == context) {
auto context = canvas->recordingContext();
if (!context) {
return;
}
auto srgb = SkColorSpace::MakeSRGB();
Expand Down
4 changes: 1 addition & 3 deletions gm/dftext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

#include <string.h>

class GrContext;

class DFTextGM : public skiagm::GM {
public:
DFTextGM() {
Expand All @@ -59,7 +57,7 @@ class DFTextGM : public skiagm::GM {
SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f };

// set up offscreen rendering with distance field text
GrContext* ctx = inputCanvas->getGrContext();
auto ctx = inputCanvas->recordingContext();
SkISize size = onISize();
SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType,
inputCanvas->imageInfo().refColorSpace());
Expand Down
6 changes: 2 additions & 4 deletions gm/dftext_blob_persp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

#include <initializer_list>

class GrContext;

/**
* This GM tests reusing the same text blobs with distance fields rendering using various
* combinations of perspective and non-perspetive matrices, scissor clips, and different x,y params
Expand Down Expand Up @@ -60,8 +58,8 @@ class DFTextBlobPerspGM : public skiagm::GM {
}

void onDraw(SkCanvas* inputCanvas) override {
// set up offscreen rendering with distance field text
GrContext* ctx = inputCanvas->getGrContext();
// set up offscreen rendering with distance field text
auto ctx = inputCanvas->recordingContext();
SkISize size = this->onISize();
if (!inputCanvas->getBaseLayerSize().isEmpty()) {
size = inputCanvas->getBaseLayerSize();
Expand Down
39 changes: 27 additions & 12 deletions gm/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include <functional>
#include <utility>

class GrContext;
class GrRenderTargetContext;

static void drawContents(SkSurface* surface, SkColor fillC) {
Expand Down Expand Up @@ -160,7 +159,7 @@ class ImageGM : public skiagm::GM {
SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
sk_sp<SkSurface> surf0(SkSurface::MakeRasterDirect(info, fBuffer, RB));
sk_sp<SkSurface> surf1(SkSurface::MakeRaster(info));
sk_sp<SkSurface> surf2(SkSurface::MakeRenderTarget(canvas->getGrContext(),
sk_sp<SkSurface> surf2(SkSurface::MakeRenderTarget(canvas->recordingContext(),
SkBudgeted::kNo, info));

test_surface(canvas, surf0.get(), true);
Expand Down Expand Up @@ -222,34 +221,50 @@ static void draw_contents(SkCanvas* canvas) {
canvas->drawCircle(50, 50, 35, paint);
}

static sk_sp<SkImage> make_raster(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
static sk_sp<SkImage> make_raster(const SkImageInfo& info,
GrRecordingContext*,
void (*draw)(SkCanvas*)) {
auto surface(SkSurface::MakeRaster(info));
draw(surface->getCanvas());
return surface->makeImageSnapshot();
}

static sk_sp<SkImage> make_picture(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
static sk_sp<SkImage> make_picture(const SkImageInfo& info,
GrRecordingContext*,
void (*draw)(SkCanvas*)) {
SkPictureRecorder recorder;
draw(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height())));
return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
info.dimensions(), nullptr, nullptr, SkImage::BitDepth::kU8,
SkColorSpace::MakeSRGB());
}

static sk_sp<SkImage> make_codec(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
static sk_sp<SkImage> make_codec(const SkImageInfo& info,
GrRecordingContext*,
void (*draw)(SkCanvas*)) {
sk_sp<SkImage> image(make_raster(info, nullptr, draw));
return SkImage::MakeFromEncoded(image->encodeToData());
}

static sk_sp<SkImage> make_gpu(const SkImageInfo& info, GrContext* ctx, void (*draw)(SkCanvas*)) {
if (!ctx) { return nullptr; }
static sk_sp<SkImage> make_gpu(const SkImageInfo& info,
GrRecordingContext* ctx,
void (*draw)(SkCanvas*)) {
if (!ctx) {
return nullptr;
}

auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
if (!surface) { return nullptr; }
if (!surface) {
return nullptr;
}

draw(surface->getCanvas());
return surface->makeImageSnapshot();
}

typedef sk_sp<SkImage> (*ImageMakerProc)(const SkImageInfo&, GrContext*, void (*)(SkCanvas*));
typedef sk_sp<SkImage> (*ImageMakerProc)(const SkImageInfo&,
GrRecordingContext*,
void (*)(SkCanvas*));

class ScalePixelsGM : public skiagm::GM {
public:
Expand All @@ -271,7 +286,7 @@ class ScalePixelsGM : public skiagm::GM {
make_codec, make_raster, make_picture, make_codec, make_gpu,
};
for (auto& proc : procs) {
sk_sp<SkImage> image(proc(info, canvas->getGrContext(), draw_contents));
sk_sp<SkImage> image(proc(info, canvas->recordingContext(), draw_contents));
if (image) {
show_scaled_pixels(canvas, image.get());
}
Expand Down Expand Up @@ -337,8 +352,8 @@ DEF_SIMPLE_GPU_GM(new_texture_image, context, rtc, canvas, 280, 60) {
SkImage::BitDepth::kU8, srgbColorSpace);
},
// Create a texture image
[direct, render_image]() -> sk_sp<SkImage> {
auto surface(SkSurface::MakeRenderTarget(direct, SkBudgeted::kYes,
[context, render_image]() -> sk_sp<SkImage> {
auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kYes,
SkImageInfo::MakeS32(kSize, kSize,
kPremul_SkAlphaType)));
if (!surface) {
Expand Down
22 changes: 14 additions & 8 deletions gm/image_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

#include <utility>

class GrContext;

static void draw_something(SkCanvas* canvas, const SkRect& bounds) {
SkPaint paint;
paint.setAntiAlias(true);
Expand All @@ -42,16 +40,20 @@ static void draw_something(SkCanvas* canvas, const SkRect& bounds) {
canvas->drawOval(bounds, paint);
}

typedef sk_sp<SkImage> (*ImageMakerProc)(GrContext*, SkPicture*, const SkImageInfo&);
typedef sk_sp<SkImage> (*ImageMakerProc)(GrRecordingContext*, SkPicture*, const SkImageInfo&);

static sk_sp<SkImage> make_raster(GrContext*, SkPicture* pic, const SkImageInfo& info) {
static sk_sp<SkImage> make_raster(GrRecordingContext*,
SkPicture* pic,
const SkImageInfo& info) {
auto surface(SkSurface::MakeRaster(info));
surface->getCanvas()->clear(0);
surface->getCanvas()->drawPicture(pic);
return surface->makeImageSnapshot();
}

static sk_sp<SkImage> make_texture(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) {
static sk_sp<SkImage> make_texture(GrRecordingContext* ctx,
SkPicture* pic,
const SkImageInfo& info) {
if (!ctx) {
return nullptr;
}
Expand All @@ -64,13 +66,17 @@ static sk_sp<SkImage> make_texture(GrContext* ctx, SkPicture* pic, const SkImage
return surface->makeImageSnapshot();
}

static sk_sp<SkImage> make_pict_gen(GrContext*, SkPicture* pic, const SkImageInfo& info) {
static sk_sp<SkImage> make_pict_gen(GrRecordingContext*,
SkPicture* pic,
const SkImageInfo& info) {
return SkImage::MakeFromPicture(sk_ref_sp(pic), info.dimensions(), nullptr, nullptr,
SkImage::BitDepth::kU8,
SkColorSpace::MakeSRGB());
}

static sk_sp<SkImage> make_encode_gen(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) {
static sk_sp<SkImage> make_encode_gen(GrRecordingContext* ctx,
SkPicture* pic,
const SkImageInfo& info) {
sk_sp<SkImage> src(make_raster(ctx, pic, info));
if (!src) {
return nullptr;
Expand Down Expand Up @@ -135,7 +141,7 @@ class ImageShaderGM : public skiagm::GM {
const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);

for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
sk_sp<SkImage> image(gProcs[i](canvas->getGrContext(), fPicture.get(), info));
sk_sp<SkImage> image(gProcs[i](canvas->recordingContext(), fPicture.get(), info));
if (image) {
this->testImage(canvas, image.get());
}
Expand Down
2 changes: 1 addition & 1 deletion gm/imagemasksubset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const MakerT makers[] = {
// SkImage_Gpu
[](SkCanvas* c, const SkImageInfo& info) -> sk_sp<SkImage> {
sk_sp<SkSurface> surface;
surface = SkSurface::MakeRenderTarget(c->getGrContext(), SkBudgeted::kNo, info);
surface = SkSurface::MakeRenderTarget(c->recordingContext(), SkBudgeted::kNo, info);
return make_mask(surface ? surface : SkSurface::MakeRaster(info));
},

Expand Down
6 changes: 2 additions & 4 deletions gm/simple_magnification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@

#include <utility>

class GrContext;

static sk_sp<SkImage> make_image(GrContext* context, int size, GrSurfaceOrigin origin) {
static sk_sp<SkImage> make_image(GrRecordingContext* context, int size, GrSurfaceOrigin origin) {
if (context) {
SkImageInfo ii = SkImageInfo::Make(size, size, kN32_SkColorType, kPremul_SkAlphaType);
sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, 0,
Expand Down Expand Up @@ -114,7 +112,7 @@ class SimpleMagnificationGM : public skiagm::GM {
}

DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
GrContext* context = canvas->getGrContext();
auto context = canvas->recordingContext();

sk_sp<SkImage> bottomLImg = make_image(context, kImgSize, kBottomLeft_GrSurfaceOrigin);
sk_sp<SkImage> topLImg = make_image(context, kImgSize, kTopLeft_GrSurfaceOrigin);
Expand Down
8 changes: 4 additions & 4 deletions gm/surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include "include/utils/SkTextUtils.h"
#include "tools/ToolUtils.h"

class GrContext;

#define W 200
#define H 100

Expand All @@ -43,7 +41,9 @@ static sk_sp<SkShader> make_shader() {
return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
}

static sk_sp<SkSurface> make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo) {
static sk_sp<SkSurface> make_surface(GrRecordingContext* ctx,
const SkImageInfo& info,
SkPixelGeometry geo) {
SkSurfaceProps props(0, geo);
if (ctx) {
return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
Expand Down Expand Up @@ -83,7 +83,7 @@ class SurfacePropsGM : public skiagm::GM {
}

void onDraw(SkCanvas* canvas) override {
GrContext* ctx = canvas->getGrContext();
auto ctx = canvas->recordingContext();

// must be opaque to have a hope of testing LCD text
const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
Expand Down
4 changes: 1 addition & 3 deletions gm/textblobmixedsizes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

#include <string.h>

class GrContext;

namespace skiagm {
class TextBlobMixedSizes : public GM {
public:
Expand Down Expand Up @@ -107,7 +105,7 @@ class TextBlobMixedSizes : public GM {
sk_sp<SkSurface> surface;
if (fUseDFT) {
// Create a new Canvas to enable DFT
GrContext* ctx = inputCanvas->getGrContext();
auto ctx = inputCanvas->recordingContext();
SkISize size = onISize();
sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace();
SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(),
Expand Down
23 changes: 21 additions & 2 deletions include/core/SkSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ class SK_API SkSurface : public SkRefCnt {
@param shouldCreateWithMips hint that SkSurface will host mip map images
@return SkSurface if all parameters are valid; otherwise, nullptr
*/
static sk_sp<SkSurface> MakeRenderTarget(GrRecordingContext* context, SkBudgeted budgeted,
const SkImageInfo& imageInfo,
int sampleCount, GrSurfaceOrigin surfaceOrigin,
const SkSurfaceProps* surfaceProps,
bool shouldCreateWithMips = false);

/** Deprecated.
*/
static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
const SkImageInfo& imageInfo,
int sampleCount, GrSurfaceOrigin surfaceOrigin,
Expand Down Expand Up @@ -391,13 +399,19 @@ class SK_API SkSurface : public SkRefCnt {
fonts; may be nullptr
@return SkSurface if all parameters are valid; otherwise, nullptr
*/
static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
static sk_sp<SkSurface> MakeRenderTarget(GrRecordingContext* context, SkBudgeted budgeted,
const SkImageInfo& imageInfo, int sampleCount,
const SkSurfaceProps* surfaceProps) {
return MakeRenderTarget(context, budgeted, imageInfo, sampleCount,
kBottomLeft_GrSurfaceOrigin, surfaceProps);
}

/** Deprecated.
*/
static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
const SkImageInfo& imageInfo, int sampleCount,
const SkSurfaceProps* surfaceProps);

/** Returns SkSurface on GPU indicated by context. Allocates memory for
pixels, based on the width, height, and SkColorType in SkImageInfo. budgeted
selects whether allocation for pixels is tracked by context. imageInfo
Expand All @@ -411,7 +425,7 @@ class SK_API SkSurface : public SkRefCnt {
of raster surface; width, or height, or both, may be zero
@return SkSurface if all parameters are valid; otherwise, nullptr
*/
static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
static sk_sp<SkSurface> MakeRenderTarget(GrRecordingContext* context, SkBudgeted budgeted,
const SkImageInfo& imageInfo) {
if (!imageInfo.width() || !imageInfo.height()) {
return nullptr;
Expand All @@ -420,6 +434,11 @@ class SK_API SkSurface : public SkRefCnt {
nullptr);
}

/** Deprecated.
*/
static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
const SkImageInfo& imageInfo);

/** Returns SkSurface on GPU indicated by context that is compatible with the provided
characterization. budgeted selects whether allocation for pixels is tracked by context.
Expand Down
15 changes: 2 additions & 13 deletions include/gpu/GrContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,14 @@ class SK_API GrContext : public GrRecordingContext {
* Can a SkSurface be created with the given color type. To check whether MSAA is supported
* use maxSurfaceSampleCountForColorType().
*/
bool colorTypeSupportedAsSurface(SkColorType colorType) const {
if (kR16G16_unorm_SkColorType == colorType ||
kA16_unorm_SkColorType == colorType ||
kA16_float_SkColorType == colorType ||
kR16G16_float_SkColorType == colorType ||
kR16G16B16A16_unorm_SkColorType == colorType ||
kGray_8_SkColorType == colorType) {
return false;
}

return this->maxSurfaceSampleCountForColorType(colorType) > 0;
}
using GrRecordingContext::colorTypeSupportedAsSurface;

/**
* Gets the maximum supported sample count for a color type. 1 is returned if only non-MSAA
* rendering is supported for the color type. 0 is returned if rendering to this color type
* is not supported at all.
*/
int maxSurfaceSampleCountForColorType(SkColorType) const;
using GrRecordingContext::maxSurfaceSampleCountForColorType;

///////////////////////////////////////////////////////////////////////////
// Misc.
Expand Down
24 changes: 24 additions & 0 deletions include/gpu/GrRecordingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ class GrRecordingContext : public GrImageContext {
*/
bool abandoned() override { return INHERITED::abandoned(); }

/*
* Can a SkSurface be created with the given color type. To check whether MSAA is supported
* use maxSurfaceSampleCountForColorType().
*/
SK_API bool colorTypeSupportedAsSurface(SkColorType colorType) const {
if (kR16G16_unorm_SkColorType == colorType ||
kA16_unorm_SkColorType == colorType ||
kA16_float_SkColorType == colorType ||
kR16G16_float_SkColorType == colorType ||
kR16G16B16A16_unorm_SkColorType == colorType ||
kGray_8_SkColorType == colorType) {
return false;
}

return this->maxSurfaceSampleCountForColorType(colorType) > 0;
}

/**
* Gets the maximum supported sample count for a color type. 1 is returned if only non-MSAA
* rendering is supported for the color type. 0 is returned if rendering to this color type
* is not supported at all.
*/
SK_API int maxSurfaceSampleCountForColorType(SkColorType) const;

// Provides access to functions that aren't part of the public API.
GrRecordingContextPriv priv();
const GrRecordingContextPriv priv() const;
Expand Down

0 comments on commit 16bf7d3

Please sign in to comment.