Use the ManagedBackendTexture helper in fiddle_main

Change-Id: I84dd14cc3328bf2f9beb0c0ac17a4f98fb62baba
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/273017
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
This commit is contained in:
Robert Phillips 2020-12-08 11:51:02 -05:00 committed by Skia Commit-Bot
parent b6853e2c09
commit b87975c133
5 changed files with 78 additions and 88 deletions

View File

@ -10,6 +10,8 @@
// implementation of the GetDrawOptions() and draw() functions.
#include "tools/fiddle/fiddle_main.h"
#include "tools/gpu/ManagedBackendTexture.h"
DrawOptions GetDrawOptions() {
// path *should* be absolute.
static const char path[] = "resources/images/color_wheel.png";
@ -28,18 +30,20 @@ void draw(SkCanvas* canvas) {
if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
sk_sp<SkImage> tmp = SkImage::MakeFromTexture(dContext,
backEndTexture,
backEndTexture->texture(),
kTopLeft_GrSurfaceOrigin,
kRGBA_8888_SkColorType,
kOpaque_SkAlphaType,
nullptr);
// TODO: this sampleCnt parameter here should match that set in the options!
sk_sp<SkSurface> tmp2 = SkSurface::MakeFromBackendTexture(dContext,
backEndTextureRenderTarget,
kTopLeft_GrSurfaceOrigin,
0, kRGBA_8888_SkColorType,
nullptr, nullptr);
constexpr int kSampleCnt = 0;
sk_sp<SkSurface> tmp2 = SkSurface::MakeFromBackendTexture(
dContext,
backEndTextureRenderTarget->texture(),
kTopLeft_GrSurfaceOrigin,
kSampleCnt,
kRGBA_8888_SkColorType,
nullptr, nullptr);
// Note: this surface should only be renderable (i.e., not textureable)
sk_sp<SkSurface> tmp3 = SkSurface::MakeFromBackendRenderTarget(dContext,

View File

@ -26,18 +26,16 @@ static DEFINE_double(frame, 1.0,
#include "src/gpu/GrDirectContextPriv.h"
#include "src/gpu/GrGpu.h"
#include "src/gpu/GrRenderTarget.h"
#include "tools/gpu/ManagedBackendTexture.h"
#include "tools/gpu/gl/GLTestContext.h"
// Globals externed in fiddle_main.h
sk_sp<GrTexture> backingTexture; // not externed
GrBackendTexture backEndTexture;
sk_sp<sk_gpu_test::ManagedBackendTexture> backEndTexture;
sk_sp<sk_gpu_test::ManagedBackendTexture> backEndTextureRenderTarget;
sk_sp<GrRenderTarget> backingRenderTarget; // not externed
GrBackendRenderTarget backEndRenderTarget;
sk_sp<GrTexture> backingTextureRenderTarget; // not externed
GrBackendTexture backEndTextureRenderTarget;
SkBitmap source;
sk_sp<SkImage> image;
double duration; // The total duration of the animation in seconds.
@ -119,21 +117,17 @@ static SkCanvas* prepare_canvas(SkCanvas * canvas) {
}
#ifdef SK_GL
static bool setup_backend_objects(GrDirectContext* context,
static bool setup_backend_objects(GrDirectContext* dContext,
const SkBitmap& bm,
const DrawOptions& options) {
if (!context) {
if (!dContext) {
fputs("Context is null.\n", stderr);
return false;
}
auto resourceProvider = context->priv().resourceProvider();
// This config must match the SkColorType used in draw.cpp in the SkImage and Surface factories
auto format = resourceProvider->caps()->getDefaultBackendFormat(
SkColorTypeToGrColorType(kRGBA_8888_SkColorType), GrRenderable::kNo);
auto renderableFormat = resourceProvider->caps()->getDefaultBackendFormat(
SkColorTypeToGrColorType(kRGBA_8888_SkColorType), GrRenderable::kYes);
GrBackendFormat renderableFormat = dContext->defaultBackendFormat(kRGBA_8888_SkColorType,
GrRenderable::kYes);
if (!bm.empty()) {
SkPixmap originalPixmap;
@ -155,48 +149,34 @@ static bool setup_backend_objects(GrDirectContext* context,
}
pixmap = &rgbaPixmap;
}
int mipLevelCount = GrMipmapped::kYes == options.fMipMapping
? SkMipmap::ComputeLevelCount(bm.width(), bm.height())
: 1;
std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
texels[0].fPixels = pixmap->addr();
texels[0].fRowBytes = pixmap->rowBytes();
for (int i = 1; i < mipLevelCount; i++) {
texels[i].fPixels = nullptr;
texels[i].fRowBytes = 0;
}
backingTexture = resourceProvider->createTexture(
bm.dimensions(), format, GrColorType::kRGBA_8888, GrRenderable::kNo, 1,
SkBudgeted::kNo, options.fMipMapping, GrProtected::kNo, texels.get());
if (!backingTexture) {
fputs("Failed to create backingTexture.\n", stderr);
return false;
}
backEndTexture = backingTexture->getBackendTexture();
if (!backEndTexture.isValid()) {
fputs("BackingTexture is invalid.\n", stderr);
backEndTexture = sk_gpu_test::ManagedBackendTexture::MakeFromPixmap(dContext,
*pixmap,
options.fMipMapping,
GrRenderable::kNo,
GrProtected::kNo);
if (!backEndTexture) {
fputs("Failed to create backEndTexture.\n", stderr);
return false;
}
}
SkISize offscreenDims = {options.fOffScreenWidth, options.fOffScreenHeight};
SkAutoTMalloc<uint32_t> data(offscreenDims.area());
sk_memset32(data.get(), 0, offscreenDims.area());
{
auto resourceProvider = dContext->priv().resourceProvider();
SkISize offscreenDims = {options.fOffScreenWidth, options.fOffScreenHeight};
SkAutoTMalloc<uint32_t> data(offscreenDims.area());
sk_memset32(data.get(), 0, offscreenDims.area());
// This backend object should be renderable but not textureable. Given the limitations
// of how we're creating it though it will wind up being secretly textureable.
// We use this fact to initialize it with data but don't allow mipmaps
GrMipLevel level0 = { data.get(), offscreenDims.width()*sizeof(uint32_t) };
constexpr int kSampleCnt = 0;
sk_sp<GrTexture> tmp = resourceProvider->createTexture(
offscreenDims, renderableFormat, GrColorType::kRGBA_8888, GrRenderable::kYes,
options.fOffScreenSampleCount, SkBudgeted::kNo, GrMipMapped::kNo,
GrProtected::kNo, &level0);
kSampleCnt, SkBudgeted::kNo, GrMipMapped::kNo, GrProtected::kNo, &level0);
if (!tmp || !tmp->asRenderTarget()) {
fputs("GrTexture is invalid.\n", stderr);
return false;
@ -212,37 +192,21 @@ static bool setup_backend_objects(GrDirectContext* context,
}
{
int mipLevelCount =
GrMipmapped::kYes == options.fOffScreenMipMapping
? SkMipmap::ComputeLevelCount(offscreenDims.width(), offscreenDims.height())
: 1;
std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
texels[0].fPixels = data.get();
texels[0].fRowBytes = offscreenDims.width()*sizeof(uint32_t);
for (int i = 1; i < mipLevelCount; i++) {
texels[i].fPixels = nullptr;
texels[i].fRowBytes = 0;
}
backingTextureRenderTarget = resourceProvider->createTexture(
offscreenDims, renderableFormat, GrColorType::kRGBA_8888, GrRenderable::kYes,
options.fOffScreenSampleCount, SkBudgeted::kNo, options.fOffScreenMipMapping,
GrProtected::kNo, texels.get());
if (!backingTextureRenderTarget || !backingTextureRenderTarget->asRenderTarget()) {
fputs("backingTextureRenderTarget is invalid.\n", stderr);
return false;
}
backEndTextureRenderTarget = backingTextureRenderTarget->getBackendTexture();
if (!backEndTextureRenderTarget.isValid()) {
fputs("backEndTextureRenderTarget is invalid.\n", stderr);
backEndTextureRenderTarget = sk_gpu_test::ManagedBackendTexture::MakeWithData(
dContext,
options.fOffScreenWidth,
options.fOffScreenHeight,
renderableFormat,
SkColors::kTransparent,
options.fOffScreenMipMapping,
GrRenderable::kYes,
GrProtected::kNo);
if (!backEndTextureRenderTarget) {
fputs("Failed to create backendTextureRenderTarget.\n", stderr);
return false;
}
}
return true;
}
#endif
@ -315,6 +279,8 @@ int main(int argc, char** argv) {
}
}
#endif
#ifdef SK_SUPPORT_PDF
if (options.pdf) {
SkDynamicMemoryWStream pdfStream;
auto document = SkPDF::MakeDocument(&pdfStream);
@ -325,6 +291,8 @@ int main(int argc, char** argv) {
pdfData = pdfStream.detachAsData();
}
}
#endif
if (options.skp) {
auto size = SkSize::Make(options.size);
SkPictureRecorder recorder;

View File

@ -23,25 +23,26 @@
#include <memory>
#include <sstream>
extern GrBackendTexture backEndTexture;
namespace sk_gpu_test {
class GLTestContext;
class ManagedBackendTexture;
} // namespace sk_gpu_test
extern sk_sp<sk_gpu_test::ManagedBackendTexture> backEndTexture;
extern GrBackendRenderTarget backEndRenderTarget;
extern GrBackendTexture backEndTextureRenderTarget;
extern sk_sp<sk_gpu_test::ManagedBackendTexture> backEndTextureRenderTarget;
extern SkBitmap source;
extern sk_sp<SkImage> image;
extern double duration; // The total duration of the animation in seconds.
extern double frame; // A value in [0, 1] of where we are in the animation.
namespace sk_gpu_test {
class GLTestContext;
} // namespace sk_gpu_test
struct DrawOptions {
DrawOptions(int w, int h, bool r, bool g, bool p, bool k, bool srgb, bool f16,
bool textOnly, const char* s,
GrMipmapped mipMapping,
int offScreenWidth,
int offScreenHeight,
int offScreenSampleCount,
int deprecated, // TODO(jcgregorio): remove
GrMipmapped offScreenMipMapping)
: size(SkISize::Make(w, h))
, raster(r)
@ -55,7 +56,6 @@ struct DrawOptions {
, fMipMapping(mipMapping)
, fOffScreenWidth(offScreenWidth)
, fOffScreenHeight(offScreenHeight)
, fOffScreenSampleCount(offScreenSampleCount)
, fOffScreenMipMapping(offScreenMipMapping) {
// F16 mode is only valid for color correct backends.
SkASSERT(srgb || !f16);
@ -79,7 +79,6 @@ struct DrawOptions {
// Parameters for an GPU offscreen resource exposed as a GrBackendRenderTarget
int fOffScreenWidth;
int fOffScreenHeight;
int fOffScreenSampleCount;
// TODO: should we also expose stencilBits here? How about the config?
GrMipmapped fOffScreenMipMapping; // only applicable if the offscreen is also textureable

View File

@ -68,15 +68,28 @@ sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromInfo(GrDirectContext
}
sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromBitmap(GrDirectContext* dContext,
const SkBitmap& bitmap,
const SkBitmap& src,
GrMipmapped mipmapped,
GrRenderable renderable,
GrProtected isProtected) {
std::vector<SkPixmap> levels({bitmap.pixmap()});
SkPixmap srcPixmap;
if (!src.peekPixels(&srcPixmap)) {
return nullptr;
}
return MakeFromPixmap(dContext, srcPixmap, mipmapped, renderable, isProtected);
}
sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromPixmap(GrDirectContext* dContext,
const SkPixmap& src,
GrMipmapped mipmapped,
GrRenderable renderable,
GrProtected isProtected) {
std::vector<SkPixmap> levels({src});
std::unique_ptr<SkMipmap> mm;
if (mipmapped == GrMipmapped::kYes) {
mm.reset(SkMipmap::Build(bitmap, nullptr));
mm.reset(SkMipmap::Build(src, nullptr));
if (!mm) {
return nullptr;
}

View File

@ -51,6 +51,12 @@ public:
GrRenderable,
GrProtected = GrProtected::kNo);
static sk_sp<ManagedBackendTexture> MakeFromPixmap(GrDirectContext*,
const SkPixmap&,
GrMipmapped,
GrRenderable,
GrProtected = GrProtected::kNo);
/** GrGpuFinishedProc or image/surface release proc. */
static void ReleaseProc(void* context);