blob: a1f4df6cdbd76c62b5069d8dc8eba37dba6a79cf [file] [log] [blame]
Brian Salomon58389b92018-03-07 13:01:25 -05001/*
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
8#include "ProxyUtils.h"
Brian Salomon02bd2952018-03-07 15:20:21 -05009#include "GrBackendSurface.h"
Brian Salomon58389b92018-03-07 13:01:25 -050010#include "GrContextPriv.h"
11#include "GrDrawingManager.h"
Brian Salomon02bd2952018-03-07 15:20:21 -050012#include "GrGpu.h"
Brian Salomon58389b92018-03-07 13:01:25 -050013#include "GrProxyProvider.h"
14
15namespace sk_gpu_test {
16
17sk_sp<GrTextureProxy> MakeTextureProxyFromData(GrContext* context, bool isRT, int width, int height,
Robert Phillips646f6372018-09-25 09:31:10 -040018 GrColorType colorType, GrSRGBEncoded srgbEncoded,
Brian Salomon58389b92018-03-07 13:01:25 -050019 GrSurfaceOrigin origin, const void* data,
20 size_t rowBytes) {
Khushalc421ca12018-06-26 14:38:34 -070021 if (context->abandoned()) {
Robert Phillips6d363702018-06-25 08:53:09 -040022 return nullptr;
23 }
24
Brian Salomon02bd2952018-03-07 15:20:21 -050025 sk_sp<GrTextureProxy> proxy;
26 if (kBottomLeft_GrSurfaceOrigin == origin) {
27 // We (soon will) only support using kBottomLeft with wrapped textures.
28 auto backendTex = context->contextPriv().getGpu()->createTestingOnlyBackendTexture(
Robert Phillips646f6372018-09-25 09:31:10 -040029 nullptr, width, height, colorType, isRT, GrMipMapped::kNo);
Brian Salomon02bd2952018-03-07 15:20:21 -050030 if (!backendTex.isValid()) {
31 return nullptr;
32 }
33 // Adopt ownership so our caller doesn't have to worry about deleting the backend texture.
34 if (isRT) {
35 proxy = context->contextPriv().proxyProvider()->wrapRenderableBackendTexture(
36 backendTex, origin, 1, kAdopt_GrWrapOwnership);
37 } else {
38 proxy = context->contextPriv().proxyProvider()->wrapBackendTexture(
Brian Salomonc67c31c2018-12-06 10:00:03 -050039 backendTex, origin, kAdopt_GrWrapOwnership, kRW_GrIOType);
Brian Salomon02bd2952018-03-07 15:20:21 -050040 }
41
42 if (!proxy) {
Brian Salomon26102cb2018-03-09 09:33:19 -050043 context->contextPriv().getGpu()->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomon02bd2952018-03-07 15:20:21 -050044 return nullptr;
45 }
46
47 } else {
Robert Phillips646f6372018-09-25 09:31:10 -040048 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, srgbEncoded);
49 if (!context->contextPriv().caps()->isConfigTexturable(config)) {
50 return nullptr;
51 }
52
Greg Daniel4065d452018-11-16 15:43:41 -050053 const GrBackendFormat format =
54 context->contextPriv().caps()->getBackendFormatFromGrColorType(colorType,
55 srgbEncoded);
56 if (!format.isValid()) {
57 return nullptr;
58 }
59
Brian Salomon02bd2952018-03-07 15:20:21 -050060 GrSurfaceDesc desc;
61 desc.fConfig = config;
62 desc.fWidth = width;
63 desc.fHeight = height;
64 desc.fFlags = isRT ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
65 proxy = context->contextPriv().proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -050066 format, desc, origin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomon02bd2952018-03-07 15:20:21 -050067 if (!proxy) {
68 return nullptr;
69 }
Brian Salomon58389b92018-03-07 13:01:25 -050070 }
71 auto sContext = context->contextPriv().makeWrappedSurfaceContext(proxy, nullptr);
72 if (!sContext) {
73 return nullptr;
74 }
Robert Phillips646f6372018-09-25 09:31:10 -040075 if (!context->contextPriv().writeSurfacePixels(sContext.get(), 0, 0, width, height, colorType,
76 nullptr, data, rowBytes)) {
Brian Salomon58389b92018-03-07 13:01:25 -050077 return nullptr;
78 }
79 return proxy;
80}
81
82} // namespace sk_gpu_test