Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2016 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 "GrTextureRenderTargetProxy.h"
      9 
     10 #include "GrCaps.h"
     11 #include "GrTexture.h"
     12 #include "GrRenderTarget.h"
     13 #include "GrSurfaceProxyPriv.h"
     14 
     15 // Deferred version
     16 // This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
     17 // GrRenderTargetProxy) so its constructor must be explicitly called.
     18 GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps,
     19                                                        const GrSurfaceDesc& desc,
     20                                                        SkBackingFit fit,
     21                                                        SkBudgeted budgeted,
     22                                                        uint32_t flags)
     23         : GrSurfaceProxy(desc, fit, budgeted, flags)
     24         // for now textures w/ data are always wrapped
     25         , GrTextureProxy(desc, fit, budgeted, nullptr, 0, flags)
     26         , GrRenderTargetProxy(caps, desc, fit, budgeted, flags) {
     27 }
     28 
     29 // Lazy-callback version
     30 GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
     31                                                        const GrSurfaceDesc& desc,
     32                                                        GrMipMapped mipMapped,
     33                                                        SkBackingFit fit,
     34                                                        SkBudgeted budgeted,
     35                                                        uint32_t flags)
     36         : GrSurfaceProxy(std::move(callback), desc, fit, budgeted, flags)
     37         // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
     38         // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
     39         , GrTextureProxy(LazyInstantiateCallback(), desc, mipMapped, fit, budgeted, flags)
     40         , GrRenderTargetProxy(LazyInstantiateCallback(), desc, fit, budgeted, flags) {
     41 }
     42 
     43 // Wrapped version
     44 // This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
     45 // GrRenderTargetProxy) so its constructor must be explicitly called.
     46 GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
     47                                                        GrSurfaceOrigin origin)
     48         : GrSurfaceProxy(surf, origin, SkBackingFit::kExact)
     49         , GrTextureProxy(surf, origin)
     50         , GrRenderTargetProxy(surf, origin) {
     51     SkASSERT(surf->asTexture());
     52     SkASSERT(surf->asRenderTarget());
     53 }
     54 
     55 size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
     56     int colorSamplesPerPixel = this->numColorSamples();
     57     if (colorSamplesPerPixel > 1) {
     58         // Add one to account for the resolve buffer.
     59         ++colorSamplesPerPixel += 1;
     60     }
     61 
     62     // TODO: do we have enough information to improve this worst case estimate?
     63     return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
     64                                   colorSamplesPerPixel, this->mipMapped(), !this->priv().isExact());
     65 }
     66 
     67 bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
     68     if (LazyState::kNot != this->lazyInstantiationState()) {
     69         return false;
     70     }
     71     static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
     72 
     73     const GrUniqueKey& key = this->getUniqueKey();
     74 
     75     if (!this->instantiateImpl(resourceProvider, this->numStencilSamples(), this->needsStencil(),
     76                                kFlags, this->mipMapped(), this->mipColorMode(),
     77                                key.isValid() ? &key : nullptr)) {
     78         return false;
     79     }
     80     if (key.isValid()) {
     81         SkASSERT(key == this->getUniqueKey());
     82     }
     83 
     84     SkASSERT(fTarget->asRenderTarget());
     85     SkASSERT(fTarget->asTexture());
     86 
     87     return true;
     88 }
     89 
     90 sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
     91                                                     GrResourceProvider* resourceProvider) const {
     92     static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
     93 
     94     sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numStencilSamples(),
     95                                                        this->needsStencil(), kFlags,
     96                                                        this->mipMapped(), this->mipColorMode());
     97     if (!surface) {
     98         return nullptr;
     99     }
    100     SkASSERT(surface->asRenderTarget());
    101     SkASSERT(surface->asTexture());
    102 
    103     return surface;
    104 }
    105 
    106 #ifdef SK_DEBUG
    107 void GrTextureRenderTargetProxy::validateLazyTexture(const GrTexture* texture) {
    108     SkASSERT(texture->asRenderTarget());
    109     SkASSERT(texture->asRenderTarget()->numStencilSamples() == this->numStencilSamples());
    110     SkASSERT(GrMipMapped::kNo == this->mipMapped());
    111 }
    112 #endif
    113 
    114