Home | History | Annotate | Download | only in mock
      1 /*
      2  * Copyright 2017 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 "GrMockGpu.h"
      9 #include "GrMockBuffer.h"
     10 #include "GrMockCaps.h"
     11 #include "GrMockGpuCommandBuffer.h"
     12 #include "GrMockStencilAttachment.h"
     13 #include "GrMockTexture.h"
     14 
     15 int GrMockGpu::NextInternalTextureID() {
     16     static int gID = 0;
     17     return sk_atomic_inc(&gID) + 1;
     18 }
     19 
     20 int GrMockGpu::NextExternalTextureID() {
     21     // We use negative ints for the "testing only external textures" so they can easily be
     22     // identified when debugging.
     23     static int gID = 0;
     24     return sk_atomic_dec(&gID) - 1;
     25 }
     26 
     27 sk_sp<GrGpu> GrMockGpu::Make(GrBackendContext backendContext,
     28                              const GrContextOptions& contextOptions, GrContext* context) {
     29     return Make(reinterpret_cast<const GrMockOptions*>(backendContext), contextOptions, context);
     30 }
     31 
     32 sk_sp<GrGpu> GrMockGpu::Make(const GrMockOptions* mockOptions,
     33                              const GrContextOptions& contextOptions, GrContext* context) {
     34     static const GrMockOptions kDefaultOptions = GrMockOptions();
     35     if (!mockOptions) {
     36         mockOptions = &kDefaultOptions;
     37     }
     38     return sk_sp<GrGpu>(new GrMockGpu(context, *mockOptions, contextOptions));
     39 }
     40 
     41 
     42 GrGpuRTCommandBuffer* GrMockGpu::createCommandBuffer(
     43                                             GrRenderTarget* rt, GrSurfaceOrigin origin,
     44                                             const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
     45                                             const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) {
     46     return new GrMockGpuRTCommandBuffer(this, rt, origin);
     47 }
     48 
     49 GrGpuTextureCommandBuffer* GrMockGpu::createCommandBuffer(GrTexture* texture,
     50                                                           GrSurfaceOrigin origin) {
     51     return new GrMockGpuTextureCommandBuffer(texture, origin);
     52 }
     53 
     54 
     55 void GrMockGpu::submitCommandBuffer(const GrMockGpuRTCommandBuffer* cmdBuffer) {
     56     for (int i = 0; i < cmdBuffer->numDraws(); ++i) {
     57         fStats.incNumDraws();
     58     }
     59 }
     60 
     61 GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options,
     62                      const GrContextOptions& contextOptions)
     63         : INHERITED(context) {
     64     fCaps.reset(new GrMockCaps(contextOptions, options));
     65 }
     66 
     67 sk_sp<GrTexture> GrMockGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
     68                                             const GrMipLevel texels[], int mipLevelCount) {
     69     GrMipMapsStatus mipMapsStatus = mipLevelCount > 1 ? GrMipMapsStatus::kValid
     70                                                       : GrMipMapsStatus::kNotAllocated;
     71     GrMockTextureInfo info;
     72     info.fID = NextInternalTextureID();
     73     if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
     74         return sk_sp<GrTexture>(
     75                 new GrMockTextureRenderTarget(this, budgeted, desc, mipMapsStatus, info));
     76     }
     77     return sk_sp<GrTexture>(new GrMockTexture(this, budgeted, desc, mipMapsStatus, info));
     78 }
     79 
     80 GrBuffer* GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrBufferType type,
     81                                     GrAccessPattern accessPattern, const void*) {
     82     return new GrMockBuffer(this, sizeInBytes, type, accessPattern);
     83 }
     84 
     85 GrStencilAttachment* GrMockGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
     86                                                                        int width,
     87                                                                        int height) {
     88     static constexpr int kBits = 8;
     89     fStats.incStencilAttachmentCreates();
     90     return new GrMockStencilAttachment(this, width, height, kBits, rt->numColorSamples());
     91 }
     92 
     93 GrBackendTexture GrMockGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
     94                                                             GrPixelConfig config, bool isRT,
     95                                                             GrMipMapped) {
     96     GrMockTextureInfo info;
     97     info.fID = NextExternalTextureID();
     98     fOutstandingTestingOnlyTextureIDs.add(info.fID);
     99     return GrBackendTexture(w, h, config, info);
    100 }
    101 
    102 bool GrMockGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
    103     SkASSERT(kMock_GrBackend == tex.backend());
    104 
    105     const GrMockTextureInfo* info = tex.getMockTextureInfo();
    106     if (!info) {
    107         return false;
    108     }
    109 
    110     return fOutstandingTestingOnlyTextureIDs.contains(info->fID);
    111 }
    112 
    113 void GrMockGpu::deleteTestingOnlyBackendTexture(GrBackendTexture* tex, bool abandonTexture) {
    114     SkASSERT(kMock_GrBackend == tex->backend());
    115 
    116     const GrMockTextureInfo* info = tex->getMockTextureInfo();
    117     if (info) {
    118         fOutstandingTestingOnlyTextureIDs.remove(info->fID);
    119     }
    120 }
    121