Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/gpu/GrBackendSurface.h" |
| 9 | #include "src/gpu/GrContextPriv.h" |
| 10 | #include "src/gpu/GrDrawingManager.h" |
| 11 | #include "src/gpu/GrGpu.h" |
| 12 | #include "src/gpu/GrProxyProvider.h" |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 13 | #include "src/gpu/SkGr.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tools/gpu/ProxyUtils.h" |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 15 | |
| 16 | namespace sk_gpu_test { |
| 17 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 18 | sk_sp<GrTextureProxy> MakeTextureProxyFromData(GrContext* context, GrRenderable renderable, |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 19 | int width, int height, |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 20 | GrColorType colorType, GrSRGBEncoded srgbEncoded, |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 21 | GrSurfaceOrigin origin, |
| 22 | const void* data, size_t rowBytes) { |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 23 | if (context->priv().abandoned()) { |
Robert Phillips | 6d36370 | 2018-06-25 08:53:09 -0400 | [diff] [blame] | 24 | return nullptr; |
| 25 | } |
| 26 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 27 | const GrCaps* caps = context->priv().caps(); |
| 28 | |
| 29 | const GrBackendFormat format = caps->getBackendFormatFromGrColorType(colorType, srgbEncoded); |
| 30 | if (!format.isValid()) { |
| 31 | return nullptr; |
| 32 | } |
| 33 | |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 34 | sk_sp<GrTextureProxy> proxy; |
| 35 | if (kBottomLeft_GrSurfaceOrigin == origin) { |
| 36 | // We (soon will) only support using kBottomLeft with wrapped textures. |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 37 | auto backendTex = context->createBackendTexture( |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 38 | width, height, format, GrMipMapped::kNo, renderable); |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 39 | if (!backendTex.isValid()) { |
| 40 | return nullptr; |
| 41 | } |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 42 | |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 43 | // Adopt ownership so our caller doesn't have to worry about deleting the backend texture. |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 44 | if (GrRenderable::kYes == renderable) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 45 | proxy = context->priv().proxyProvider()->wrapRenderableBackendTexture( |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 46 | backendTex, origin, 1, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, nullptr, |
| 47 | nullptr); |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 48 | } else { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 49 | proxy = context->priv().proxyProvider()->wrapBackendTexture( |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 50 | backendTex, origin, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType); |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | if (!proxy) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame^] | 54 | context->deleteBackendTexture(backendTex); |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | } else { |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 59 | GrPixelConfig config = GrColorTypeToPixelConfig(colorType, srgbEncoded); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 60 | if (!context->priv().caps()->isConfigTexturable(config)) { |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 61 | return nullptr; |
| 62 | } |
| 63 | |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 64 | GrSurfaceDesc desc; |
| 65 | desc.fConfig = config; |
| 66 | desc.fWidth = width; |
| 67 | desc.fHeight = height; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 68 | desc.fFlags = GrRenderable::kYes == renderable ? kRenderTarget_GrSurfaceFlag |
| 69 | : kNone_GrSurfaceFlags; |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 70 | proxy = context->priv().proxyProvider()->createProxy( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 71 | format, desc, origin, SkBackingFit::kExact, SkBudgeted::kYes); |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 72 | if (!proxy) { |
| 73 | return nullptr; |
| 74 | } |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 75 | } |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 76 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 77 | auto sContext = context->priv().makeWrappedSurfaceContext(proxy, nullptr); |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 78 | if (!sContext) { |
| 79 | return nullptr; |
| 80 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 81 | if (!context->priv().writeSurfacePixels(sContext.get(), 0, 0, width, height, colorType, |
| 82 | nullptr, data, rowBytes)) { |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 83 | return nullptr; |
| 84 | } |
| 85 | return proxy; |
| 86 | } |
| 87 | |
| 88 | } // namespace sk_gpu_test |