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
15int GrMockGpu::NextInternalTextureID() {
16    static int gID = 0;
17    return sk_atomic_inc(&gID) + 1;
18}
19
20int 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
27sk_sp<GrGpu> GrMockGpu::Make(GrBackendContext backendContext,
28                             const GrContextOptions& contextOptions, GrContext* context) {
29    return Make(reinterpret_cast<const GrMockOptions*>(backendContext), contextOptions, context);
30}
31
32sk_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
42GrGpuRTCommandBuffer* GrMockGpu::createCommandBuffer(
43                                            GrRenderTarget* rt, GrSurfaceOrigin origin,
44                                            const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
45                                            const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) {
46    return new GrMockGpuRTCommandBuffer(this, rt, origin);
47}
48
49GrGpuTextureCommandBuffer* GrMockGpu::createCommandBuffer(GrTexture* texture,
50                                                          GrSurfaceOrigin origin) {
51    return new GrMockGpuTextureCommandBuffer(texture, origin);
52}
53
54
55void GrMockGpu::submitCommandBuffer(const GrMockGpuRTCommandBuffer* cmdBuffer) {
56    for (int i = 0; i < cmdBuffer->numDraws(); ++i) {
57        fStats.incNumDraws();
58    }
59}
60
61GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options,
62                     const GrContextOptions& contextOptions)
63        : INHERITED(context) {
64    fCaps.reset(new GrMockCaps(contextOptions, options));
65}
66
67sk_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.fConfig = desc.fConfig;
73    info.fID = NextInternalTextureID();
74    if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
75        return sk_sp<GrTexture>(
76                new GrMockTextureRenderTarget(this, budgeted, desc, mipMapsStatus, info));
77    }
78    return sk_sp<GrTexture>(new GrMockTexture(this, budgeted, desc, mipMapsStatus, info));
79}
80
81GrBuffer* GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrBufferType type,
82                                    GrAccessPattern accessPattern, const void*) {
83    return new GrMockBuffer(this, sizeInBytes, type, accessPattern);
84}
85
86GrStencilAttachment* GrMockGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
87                                                                       int width,
88                                                                       int height) {
89    static constexpr int kBits = 8;
90    fStats.incStencilAttachmentCreates();
91    return new GrMockStencilAttachment(this, width, height, kBits, rt->numColorSamples());
92}
93
94GrBackendTexture GrMockGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
95                                                            GrPixelConfig config, bool isRT,
96                                                            GrMipMapped) {
97    GrMockTextureInfo info;
98    info.fConfig = config;
99    info.fID = NextExternalTextureID();
100    fOutstandingTestingOnlyTextureIDs.add(info.fID);
101    return GrBackendTexture(w, h, config, info);
102}
103
104bool GrMockGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
105    SkASSERT(kMock_GrBackend == tex.backend());
106
107    const GrMockTextureInfo* info = tex.getMockTextureInfo();
108    if (!info) {
109        return false;
110    }
111
112    return fOutstandingTestingOnlyTextureIDs.contains(info->fID);
113}
114
115void GrMockGpu::deleteTestingOnlyBackendTexture(GrBackendTexture* tex, bool abandonTexture) {
116    SkASSERT(kMock_GrBackend == tex->backend());
117
118    const GrMockTextureInfo* info = tex->getMockTextureInfo();
119    if (info) {
120        fOutstandingTestingOnlyTextureIDs.remove(info->fID);
121    }
122}
123