diff --git a/BUILD.gn b/BUILD.gn index 2a8b8d30c1..6e24d3dec0 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -904,6 +904,7 @@ if (skia_enable_tools) { testonly = true deps = [ ":flags", + ":gpu_tool_utils", ":skia", ":skia.h", ] diff --git a/include/gpu/GrBackendSurface.h b/include/gpu/GrBackendSurface.h index c9d326bc36..ce6ad194ac 100644 --- a/include/gpu/GrBackendSurface.h +++ b/include/gpu/GrBackendSurface.h @@ -96,6 +96,9 @@ private: class SK_API GrBackendRenderTarget { public: + // Creates an invalid backend texture. + GrBackendRenderTarget() : fConfig(kUnknown_GrPixelConfig) {} + GrBackendRenderTarget(int width, int height, int sampleCnt, @@ -127,6 +130,9 @@ public: const GrVkImageInfo* getVkImageInfo() const; #endif + // Returns true if the backend texture has been initialized. + bool isValid() const { return fConfig != kUnknown_GrPixelConfig; } + private: // Friending for access to the GrPixelConfig friend class SkSurface; diff --git a/tests/GrMipMappedTest.cpp b/tests/GrMipMappedTest.cpp index c2a0df2e5d..ff9d76d044 100644 --- a/tests/GrMipMappedTest.cpp +++ b/tests/GrMipMappedTest.cpp @@ -43,7 +43,7 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrWrappedMipMappedTest, reporter, ctxInfo) { // so we don't send any. However, we pretend there is data for the checks below which is // fine since we are never actually using these textures for any work on the gpu. GrBackendObject backendHandle = context->getGpu()->createTestingOnlyBackendTexture( - nullptr, 8, 8, kRGBA_8888_GrPixelConfig, isRT, mipMapped); + nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig, isRT, mipMapped); GrBackend backend = context->contextPriv().getBackend(); GrBackendTexture backendTex = GrTest::CreateBackendTexture(backend, diff --git a/tools/fiddle/draw.cpp b/tools/fiddle/draw.cpp index 8e94883313..31aa9d0a81 100644 --- a/tools/fiddle/draw.cpp +++ b/tools/fiddle/draw.cpp @@ -26,4 +26,30 @@ void draw(SkCanvas* canvas) { &matrix)); canvas->drawPaint(paint); SkDebugf("This is text output: %d", 2); + + GrContext* context = canvas->getGrContext(); + if (context) { + sk_sp tmp = SkImage::MakeFromTexture(context, + backEndTexture, + kTopLeft_GrSurfaceOrigin, + kOpaque_SkAlphaType, + nullptr); + + sk_sp tmp2 = SkSurface::MakeFromBackendTexture(context, + backEndTextureRenderTarget, + kTopLeft_GrSurfaceOrigin, + 0, nullptr, nullptr); + + sk_sp tmp3 = SkSurface::MakeFromBackendRenderTarget(context, + backEndRenderTarget, + kTopLeft_GrSurfaceOrigin, + nullptr, nullptr); + + sk_sp tmp4 = SkSurface::MakeFromBackendTextureAsRenderTarget( + context, + backEndTextureRenderTarget, + kTopLeft_GrSurfaceOrigin, + 0, nullptr, nullptr); + } + } diff --git a/tools/fiddle/fiddle_main.cpp b/tools/fiddle/fiddle_main.cpp index 6ceffc9b9c..b815be10f7 100644 --- a/tools/fiddle/fiddle_main.cpp +++ b/tools/fiddle/fiddle_main.cpp @@ -11,13 +11,30 @@ #include #include "SkCommandLineFlags.h" +#include "SkUtils.h" #include "fiddle_main.h" DEFINE_double(duration, 1.0, "The total duration, in seconds, of the animation we are drawing."); DEFINE_double(frame, 1.0, "A double value in [0, 1] that specifies the point in animation to draw."); +#include "GrBackendSurface.h" +#include "GrContextPriv.h" +#include "GrGpu.h" + +#include "GrTest.h" + + // Globals externed in fiddle_main.h +sk_sp backingTexture; // not externed +GrBackendTexture backEndTexture; + +sk_sp backingRenderTarget; // not externed +GrBackendRenderTarget backEndRenderTarget; + +sk_sp backingTextureRenderTarget; // not externed +GrBackendTexture backEndTextureRenderTarget; + SkBitmap source; sk_sp image; double duration; // The total duration of the animation in seconds. @@ -98,6 +115,103 @@ static SkCanvas* prepare_canvas(SkCanvas * canvas) { return canvas; } +static bool setup_backend_objects(GrContext* context, const SkBitmap& bm, + int width, int height, int sampleCnt) { + if (!context) { + return false; + } + + GrBackend backend = context->contextPriv().getBackend(); + const GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig; + + GrSurfaceDesc backingDesc; + backingDesc.fFlags = kNone_GrSurfaceFlags; + backingDesc.fOrigin = kTopLeft_GrSurfaceOrigin; + backingDesc.fWidth = bm.width(); + backingDesc.fHeight = bm.height(); + backingDesc.fConfig = kConfig; + backingDesc.fSampleCnt = 0; + + if (!bm.empty()) { + GrMipLevel level0 = { bm.getPixels(), bm.rowBytes() }; + + backingTexture = context->resourceProvider()->createTexture( + backingDesc, SkBudgeted::kNo, + &level0, 1, + SkDestinationSurfaceColorMode::kLegacy); + if (!backingTexture) { + return false; + } + + backEndTexture = GrTest::CreateBackendTexture(backend, + backingDesc.fWidth, + backingDesc.fHeight, + kConfig, + GrMipMapped::kNo, + backingTexture->getTextureHandle()); + if (!backEndTexture.isValid()) { + return false; + } + } + + backingDesc.fFlags = kRenderTarget_GrSurfaceFlag; + backingDesc.fWidth = width; + backingDesc.fHeight = height; + backingDesc.fSampleCnt = sampleCnt; + + SkAutoTMalloc data(width * height); + sk_memset32(data.get(), 0, width * height); + + GrMipLevel level0 = { data.get(), width*sizeof(uint32_t) }; + + { + sk_sp tmp = context->resourceProvider()->createTexture( + backingDesc, SkBudgeted::kNo, + &level0, 1, + SkDestinationSurfaceColorMode::kLegacy); + if (!tmp || !tmp->asRenderTarget()) { + return false; + } + + backingRenderTarget = sk_ref_sp(tmp->asRenderTarget()); + + backEndRenderTarget = GrTest::CreateBackendRenderTarget( + backend, + backingDesc.fWidth, + backingDesc.fHeight, + backingDesc.fSampleCnt, 0, + kConfig, + backingRenderTarget->getRenderTargetHandle()); + if (!backEndRenderTarget.isValid()) { + return false; + } + } + + { + backingTextureRenderTarget = context->resourceProvider()->createTexture( + backingDesc, SkBudgeted::kNo, + &level0, 1, + SkDestinationSurfaceColorMode::kLegacy); + if (!backingTextureRenderTarget || !backingTextureRenderTarget->asRenderTarget()) { + return false; + } + + backEndTextureRenderTarget = GrTest::CreateBackendTexture( + backend, + backingDesc.fWidth, + backingDesc.fHeight, + kConfig, + GrMipMapped::kNo, + backingTextureRenderTarget->getTextureHandle()); + if (!backEndTextureRenderTarget.isValid()) { + return false; + } + } + + + return true; +} + int main(int argc, char** argv) { SkCommandLineFlags::Parse(argc, argv); duration = FLAGS_duration; @@ -122,8 +236,7 @@ int main(int argc, char** argv) { perror("Unable to decode the source image."); return 1; } - SkAssertResult(image->asLegacyBitmap( - &source, SkImage::kRO_LegacyBitmapMode)); + SkAssertResult(image->asLegacyBitmap(&source, SkImage::kRO_LegacyBitmapMode)); } } sk_sp rasterData, gpuData, pdfData, skpData; @@ -145,10 +258,16 @@ int main(int argc, char** argv) { rasterData = encode_snapshot(rasterSurface); } if (options.gpu) { - auto grContext = create_grcontext(gGLDriverInfo); + sk_sp grContext = create_grcontext(gGLDriverInfo); if (!grContext) { fputs("Unable to get GrContext.\n", stderr); } else { + if (!setup_backend_objects(grContext.get(), source, + options.size.width(), options.size.height(), 0)) { + fputs("Unable to create backend objects.\n", stderr); + exit(1); + } + auto surface = SkSurface::MakeRenderTarget(grContext.get(), SkBudgeted::kNo, info); if (!surface) { fputs("Unable to get render surface.\n", stderr); diff --git a/tools/fiddle/fiddle_main.h b/tools/fiddle/fiddle_main.h index fb9d409482..97fa071913 100644 --- a/tools/fiddle/fiddle_main.h +++ b/tools/fiddle/fiddle_main.h @@ -22,6 +22,9 @@ #include +extern GrBackendTexture backEndTexture; +extern GrBackendRenderTarget backEndRenderTarget; +extern GrBackendTexture backEndTextureRenderTarget; extern SkBitmap source; extern sk_sp image; extern double duration; // The total duration of the animation in seconds. diff --git a/tools/gpu/GrTest.cpp b/tools/gpu/GrTest.cpp index 32e2cfa91d..83e5aa7f3b 100644 --- a/tools/gpu/GrTest.cpp +++ b/tools/gpu/GrTest.cpp @@ -78,6 +78,27 @@ GrBackendTexture CreateBackendTexture(GrBackend backend, int width, int height, } } +GrBackendRenderTarget CreateBackendRenderTarget(GrBackend backend, int width, int height, + int sampleCnt, int stencilBits, + GrPixelConfig config, + GrBackendObject handle) { + switch (backend) { +#ifdef SK_VULKAN + case kVulkan_GrBackend: { + GrVkImageInfo* vkInfo = (GrVkImageInfo*)(handle); + return GrBackendRenderTarget(width, height, sampleCnt, stencilBits, *vkInfo); + } +#endif + case kOpenGL_GrBackend: { + GrGLFramebufferInfo* glInfo = (GrGLFramebufferInfo*)(handle); + return GrBackendRenderTarget(width, height, sampleCnt, stencilBits, config, *glInfo); + } + case kMock_GrBackend: // fall through + default: + return GrBackendRenderTarget(); + } +} + } // namespace GrTest bool GrSurfaceProxy::isWrapped_ForTesting() const { diff --git a/tools/gpu/GrTest.h b/tools/gpu/GrTest.h index 612245f07b..1134932ad9 100644 --- a/tools/gpu/GrTest.h +++ b/tools/gpu/GrTest.h @@ -20,6 +20,10 @@ namespace GrTest { GrBackendTexture CreateBackendTexture(GrBackend, int width, int height, GrPixelConfig, GrMipMapped, GrBackendObject); + + GrBackendRenderTarget CreateBackendRenderTarget(GrBackend, int width, int height, + int sampleCnt, int stencilBits, + GrPixelConfig, GrBackendObject); }; #endif