blob: d89bba2616c8fce8dc51f5d3761344b1cbee069d [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Phillips9dbcdcc2019-05-13 10:40:06 -040013#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tools/gpu/ProxyUtils.h"
Brian Salomon58389b92018-03-07 13:01:25 -050015
16namespace sk_gpu_test {
17
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040018sk_sp<GrTextureProxy> MakeTextureProxyFromData(GrContext* context, GrRenderable renderable,
Robert Phillips69893702019-02-22 11:16:30 -050019 int width, int height,
Robert Phillips646f6372018-09-25 09:31:10 -040020 GrColorType colorType, GrSRGBEncoded srgbEncoded,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040021 GrSurfaceOrigin origin,
22 const void* data, size_t rowBytes) {
Robert Phillips69893702019-02-22 11:16:30 -050023 if (context->priv().abandoned()) {
Robert Phillips6d363702018-06-25 08:53:09 -040024 return nullptr;
25 }
26
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040027 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 Salomon02bd2952018-03-07 15:20:21 -050034 sk_sp<GrTextureProxy> proxy;
35 if (kBottomLeft_GrSurfaceOrigin == origin) {
36 // We (soon will) only support using kBottomLeft with wrapped textures.
Robert Phillips80626792019-06-04 07:16:10 -040037 auto backendTex = context->priv().createBackendTexture(
38 width, height, format, SkColors::kTransparent, GrMipMapped::kNo, renderable);
Brian Salomon02bd2952018-03-07 15:20:21 -050039 if (!backendTex.isValid()) {
40 return nullptr;
41 }
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040042
Brian Salomon02bd2952018-03-07 15:20:21 -050043 // Adopt ownership so our caller doesn't have to worry about deleting the backend texture.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040044 if (GrRenderable::kYes == renderable) {
Robert Phillips9da87e02019-02-04 13:26:26 -050045 proxy = context->priv().proxyProvider()->wrapRenderableBackendTexture(
Greg Daniel8ce79912019-02-05 10:08:43 -050046 backendTex, origin, 1, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, nullptr,
47 nullptr);
Brian Salomon02bd2952018-03-07 15:20:21 -050048 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -050049 proxy = context->priv().proxyProvider()->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050050 backendTex, origin, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Brian Salomon02bd2952018-03-07 15:20:21 -050051 }
52
53 if (!proxy) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040054 context->deleteBackendTexture(backendTex);
Brian Salomon02bd2952018-03-07 15:20:21 -050055 return nullptr;
56 }
57
58 } else {
Robert Phillips646f6372018-09-25 09:31:10 -040059 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, srgbEncoded);
Robert Phillips9da87e02019-02-04 13:26:26 -050060 if (!context->priv().caps()->isConfigTexturable(config)) {
Robert Phillips646f6372018-09-25 09:31:10 -040061 return nullptr;
62 }
63
Brian Salomon02bd2952018-03-07 15:20:21 -050064 GrSurfaceDesc desc;
65 desc.fConfig = config;
66 desc.fWidth = width;
67 desc.fHeight = height;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040068 desc.fFlags = GrRenderable::kYes == renderable ? kRenderTarget_GrSurfaceFlag
69 : kNone_GrSurfaceFlags;
Robert Phillips9da87e02019-02-04 13:26:26 -050070 proxy = context->priv().proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -050071 format, desc, origin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomon02bd2952018-03-07 15:20:21 -050072 if (!proxy) {
73 return nullptr;
74 }
Brian Salomon58389b92018-03-07 13:01:25 -050075 }
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040076
Robert Phillips9da87e02019-02-04 13:26:26 -050077 auto sContext = context->priv().makeWrappedSurfaceContext(proxy, nullptr);
Brian Salomon58389b92018-03-07 13:01:25 -050078 if (!sContext) {
79 return nullptr;
80 }
Robert Phillips9da87e02019-02-04 13:26:26 -050081 if (!context->priv().writeSurfacePixels(sContext.get(), 0, 0, width, height, colorType,
82 nullptr, data, rowBytes)) {
Brian Salomon58389b92018-03-07 13:01:25 -050083 return nullptr;
84 }
85 return proxy;
86}
87
88} // namespace sk_gpu_test