ProxyUtils.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2018 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/gpu/GrBackendSurface.h"
  8. #include "src/gpu/GrContextPriv.h"
  9. #include "src/gpu/GrDrawingManager.h"
  10. #include "src/gpu/GrGpu.h"
  11. #include "src/gpu/GrProxyProvider.h"
  12. #include "src/gpu/SkGr.h"
  13. #include "tools/gpu/ProxyUtils.h"
  14. namespace sk_gpu_test {
  15. sk_sp<GrTextureProxy> MakeTextureProxyFromData(GrContext* context,
  16. GrRenderable renderable,
  17. int width,
  18. int height,
  19. GrColorType colorType, SkAlphaType alphaType,
  20. GrSurfaceOrigin origin,
  21. const void* data, size_t rowBytes) {
  22. if (context->priv().abandoned()) {
  23. return nullptr;
  24. }
  25. const GrCaps* caps = context->priv().caps();
  26. const GrBackendFormat format = caps->getBackendFormatFromColorType(colorType);
  27. if (!format.isValid()) {
  28. return nullptr;
  29. }
  30. sk_sp<GrTextureProxy> proxy;
  31. if (kBottomLeft_GrSurfaceOrigin == origin) {
  32. // We (soon will) only support using kBottomLeft with wrapped textures.
  33. auto backendTex = context->createBackendTexture(
  34. width, height, format, SkColors::kTransparent, GrMipMapped::kNo, renderable,
  35. GrProtected::kNo);
  36. if (!backendTex.isValid()) {
  37. return nullptr;
  38. }
  39. // Adopt ownership so our caller doesn't have to worry about deleting the backend texture.
  40. if (GrRenderable::kYes == renderable) {
  41. proxy = context->priv().proxyProvider()->wrapRenderableBackendTexture(
  42. backendTex, origin, 1, colorType, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo,
  43. nullptr, nullptr);
  44. } else {
  45. proxy = context->priv().proxyProvider()->wrapBackendTexture(
  46. backendTex, colorType, origin, kAdopt_GrWrapOwnership,
  47. GrWrapCacheable::kNo, kRW_GrIOType);
  48. }
  49. if (!proxy) {
  50. context->deleteBackendTexture(backendTex);
  51. return nullptr;
  52. }
  53. } else {
  54. GrPixelConfig config = GrColorTypeToPixelConfig(colorType);
  55. if (!context->priv().caps()->isConfigTexturable(config)) {
  56. return nullptr;
  57. }
  58. GrSurfaceDesc desc;
  59. desc.fConfig = config;
  60. desc.fWidth = width;
  61. desc.fHeight = height;
  62. proxy = context->priv().proxyProvider()->createProxy(format, desc, renderable, 1, origin,
  63. SkBackingFit::kExact, SkBudgeted::kYes,
  64. GrProtected::kNo);
  65. if (!proxy) {
  66. return nullptr;
  67. }
  68. }
  69. auto sContext = context->priv().makeWrappedSurfaceContext(proxy, colorType, alphaType, nullptr);
  70. if (!sContext) {
  71. return nullptr;
  72. }
  73. if (!sContext->writePixels({colorType, alphaType, nullptr, width, height}, data, rowBytes,
  74. {0, 0}, context)) {
  75. return nullptr;
  76. }
  77. return proxy;
  78. }
  79. } // namespace sk_gpu_test