Home | History | Annotate | Download | only in gpu
      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 
      8 #include "ProxyUtils.h"
      9 #include "GrBackendSurface.h"
     10 #include "GrContextPriv.h"
     11 #include "GrDrawingManager.h"
     12 #include "GrGpu.h"
     13 #include "GrProxyProvider.h"
     14 
     15 namespace sk_gpu_test {
     16 
     17 sk_sp<GrTextureProxy> MakeTextureProxyFromData(GrContext* context, bool isRT, int width, int height,
     18                                                GrColorType colorType, GrSRGBEncoded srgbEncoded,
     19                                                GrSurfaceOrigin origin, const void* data,
     20                                                size_t rowBytes) {
     21     if (context->abandoned()) {
     22         return nullptr;
     23     }
     24 
     25     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(
     29                 nullptr, width, height, colorType, isRT, GrMipMapped::kNo);
     30         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, GrWrapCacheable::kNo);
     37         } else {
     38             proxy = context->contextPriv().proxyProvider()->wrapBackendTexture(
     39                     backendTex, origin, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
     40         }
     41 
     42         if (!proxy) {
     43             context->contextPriv().getGpu()->deleteTestingOnlyBackendTexture(backendTex);
     44             return nullptr;
     45         }
     46 
     47     } else {
     48         GrPixelConfig config = GrColorTypeToPixelConfig(colorType, srgbEncoded);
     49         if (!context->contextPriv().caps()->isConfigTexturable(config)) {
     50             return nullptr;
     51         }
     52 
     53         const GrBackendFormat format =
     54                 context->contextPriv().caps()->getBackendFormatFromGrColorType(colorType,
     55                                                                                srgbEncoded);
     56         if (!format.isValid()) {
     57             return nullptr;
     58         }
     59 
     60         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(
     66                 format, desc, origin, SkBackingFit::kExact, SkBudgeted::kYes);
     67         if (!proxy) {
     68             return nullptr;
     69         }
     70     }
     71     auto sContext = context->contextPriv().makeWrappedSurfaceContext(proxy, nullptr);
     72     if (!sContext) {
     73         return nullptr;
     74     }
     75     if (!context->contextPriv().writeSurfacePixels(sContext.get(), 0, 0, width, height, colorType,
     76                                                    nullptr, data, rowBytes)) {
     77         return nullptr;
     78     }
     79     return proxy;
     80 }
     81 
     82 }  // namespace sk_gpu_test
     83