blob: 31b9dddb95bea0972f2c7bde05ca937fa34a3e86 [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.
Robert Phillips9da87e02019-02-04 13:26:26 -050028 auto backendTex = context->priv().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) {
Robert Phillips9da87e02019-02-04 13:26:26 -050035 proxy = context->priv().proxyProvider()->wrapRenderableBackendTexture(
Greg Daniel8ce79912019-02-05 10:08:43 -050036 backendTex, origin, 1, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, nullptr,
37 nullptr);
Brian Salomon02bd2952018-03-07 15:20:21 -050038 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -050039 proxy = context->priv().proxyProvider()->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050040 backendTex, origin, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Brian Salomon02bd2952018-03-07 15:20:21 -050041 }
42
43 if (!proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050044 context->priv().getGpu()->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomon02bd2952018-03-07 15:20:21 -050045 return nullptr;
46 }
47
48 } else {
Robert Phillips646f6372018-09-25 09:31:10 -040049 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, srgbEncoded);
Robert Phillips9da87e02019-02-04 13:26:26 -050050 if (!context->priv().caps()->isConfigTexturable(config)) {
Robert Phillips646f6372018-09-25 09:31:10 -040051 return nullptr;
52 }
53
Greg Daniel4065d452018-11-16 15:43:41 -050054 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -050055 context->priv().caps()->getBackendFormatFromGrColorType(colorType,
56 srgbEncoded);
Greg Daniel4065d452018-11-16 15:43:41 -050057 if (!format.isValid()) {
58 return nullptr;
59 }
60
Brian Salomon02bd2952018-03-07 15:20:21 -050061 GrSurfaceDesc desc;
62 desc.fConfig = config;
63 desc.fWidth = width;
64 desc.fHeight = height;
65 desc.fFlags = isRT ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Robert Phillips9da87e02019-02-04 13:26:26 -050066 proxy = context->priv().proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -050067 format, desc, origin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomon02bd2952018-03-07 15:20:21 -050068 if (!proxy) {
69 return nullptr;
70 }
Brian Salomon58389b92018-03-07 13:01:25 -050071 }
Robert Phillips9da87e02019-02-04 13:26:26 -050072 auto sContext = context->priv().makeWrappedSurfaceContext(proxy, nullptr);
Brian Salomon58389b92018-03-07 13:01:25 -050073 if (!sContext) {
74 return nullptr;
75 }
Robert Phillips9da87e02019-02-04 13:26:26 -050076 if (!context->priv().writeSurfacePixels(sContext.get(), 0, 0, width, height, colorType,
77 nullptr, data, rowBytes)) {
Brian Salomon58389b92018-03-07 13:01:25 -050078 return nullptr;
79 }
80 return proxy;
81}
82
83} // namespace sk_gpu_test