aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrBatchFlushState.h
blob: be9d790597671b242b3aba2f5df94f883bbc9575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrBatchBuffer_DEFINED
#define GrBatchBuffer_DEFINED

#include "GrBufferAllocPool.h"
#include "batches/GrVertexBatch.h"

class GrResourceProvider;

/** Simple class that performs the upload on behalf of a GrBatchUploader. */
class GrBatchUploader::TextureUploader {
public:
    TextureUploader(GrGpu* gpu) : fGpu(gpu) { SkASSERT(gpu); }

    /**
        * Updates the pixels in a rectangle of a texture.
        *
        * @param left          left edge of the rectangle to write (inclusive)
        * @param top           top edge of the rectangle to write (inclusive)
        * @param width         width of rectangle to write in pixels.
        * @param height        height of rectangle to write in pixels.
        * @param config        the pixel config of the source buffer
        * @param buffer        memory to read pixels from
        * @param rowBytes      number of bytes between consecutive rows. Zero
        *                      means rows are tightly packed.
        */
    bool writeTexturePixels(GrTexture* texture,
                            int left, int top, int width, int height,
                            GrPixelConfig config, const void* buffer,
                            size_t rowBytes) {
        return fGpu->writePixels(texture, left, top, width, height, config, buffer, rowBytes);
    }

private:
    GrGpu* fGpu;
};

/** Tracks the state across all the GrBatches in a GrDrawTarget flush. */
class GrBatchFlushState {
public:
    GrBatchFlushState(GrGpu*, GrResourceProvider*);

    ~GrBatchFlushState() { this->reset(); }

    void advanceToken() { ++fCurrentToken; }

    void advanceLastFlushedToken() { ++fLastFlushedToken; }

    /** Inserts an upload to be executred after all batches in the flush prepared their draws
        but before the draws are executed to the backend 3D API. */
    void addASAPUpload(GrBatchUploader* upload) {
        fAsapUploads.push_back().reset(SkRef(upload));
    }

    const GrCaps& caps() const { return *fGpu->caps(); }
    GrResourceProvider* resourceProvider() const { return fResourceProvider; }

    /** Has the token been flushed to the backend 3D API. */
    bool hasTokenBeenFlushed(GrBatchToken token) const { return fLastFlushedToken >= token; }

    /** The current token advances once for every contiguous set of uninterrupted draws prepared
        by a batch. */
    GrBatchToken currentToken() const { return fCurrentToken; }

    /** The last token flushed to all the way to the backend API. */
    GrBatchToken lastFlushedToken() const { return fLastFlushedToken; }

    /** This is a magic token that can be used to indicate that an upload should occur before
        any draws for any batch in the current flush execute. */
    GrBatchToken asapToken() const { return fLastFlushedToken + 1; }

    void* makeVertexSpace(size_t vertexSize, int vertexCount,
                          const GrBuffer** buffer, int* startVertex);
    uint16_t* makeIndexSpace(int indexCount, const GrBuffer** buffer, int* startIndex);

    /** This is called after each batch has a chance to prepare its draws and before the draws
        are issued. */
    void preIssueDraws() {
        fVertexPool.unmap();
        fIndexPool.unmap();
        int uploadCount = fAsapUploads.count();
        for (int i = 0; i < uploadCount; i++) {
            fAsapUploads[i]->upload(&fUploader);
        }
        fAsapUploads.reset();
    }

    void putBackIndices(size_t indices) { fIndexPool.putBack(indices * sizeof(uint16_t)); }

    void putBackVertexSpace(size_t sizeInBytes) { fVertexPool.putBack(sizeInBytes); }

    GrBatchUploader::TextureUploader* uploader() { return &fUploader; }

    GrGpu* gpu() { return fGpu; }

    void reset() {
        fVertexPool.reset();
        fIndexPool.reset();
    }

private:
    GrGpu*                                          fGpu;
    GrBatchUploader::TextureUploader                fUploader;

    GrResourceProvider*                             fResourceProvider;

    GrVertexBufferAllocPool                         fVertexPool;
    GrIndexBufferAllocPool                          fIndexPool;

    SkTArray<SkAutoTUnref<GrBatchUploader>, true>   fAsapUploads;

    GrBatchToken                                    fCurrentToken;

    GrBatchToken                                    fLastFlushedToken;
};

/**
 * GrDrawBatch instances use this object to allocate space for their geometry and to issue the draws
 * that render their batch.
 */
class GrDrawBatch::Target {
public:
    Target(GrBatchFlushState* state, GrDrawBatch* batch) : fState(state), fBatch(batch) {}

    void upload(GrBatchUploader* upload) {
        if (this->asapToken() == upload->lastUploadToken()) {
            fState->addASAPUpload(upload);
        } else {
            fBatch->fInlineUploads.push_back().reset(SkRef(upload));
        }
    }

    bool hasTokenBeenFlushed(GrBatchToken token) const {
        return fState->hasTokenBeenFlushed(token);
    }
    GrBatchToken currentToken() const { return fState->currentToken(); }
    GrBatchToken asapToken() const { return fState->asapToken(); }

    const GrCaps& caps() const { return fState->caps(); }

    GrResourceProvider* resourceProvider() const { return fState->resourceProvider(); }

protected:
    GrDrawBatch* batch() { return fBatch; }
    GrBatchFlushState* state() { return fState; }

private:
    GrBatchFlushState*  fState;
    GrDrawBatch*        fBatch;
};

/** Extension of GrDrawBatch::Target for use by GrVertexBatch. Adds the ability to create vertex
    draws. */
class GrVertexBatch::Target : public GrDrawBatch::Target {
public:
    Target(GrBatchFlushState* state, GrVertexBatch* batch) : INHERITED(state, batch) {}

    void initDraw(const GrPrimitiveProcessor* primProc) {
        GrVertexBatch::DrawArray* draws = this->vertexBatch()->fDrawArrays.addToTail();
        draws->fPrimitiveProcessor.reset(primProc);
        this->state()->advanceToken();
    }

    void draw(const GrMesh& mesh) {
        this->vertexBatch()->fDrawArrays.tail()->fDraws.push_back(mesh);
    }

    void* makeVertexSpace(size_t vertexSize, int vertexCount,
                          const GrBuffer** buffer, int* startVertex) {
        return this->state()->makeVertexSpace(vertexSize, vertexCount, buffer, startVertex);
    }

    uint16_t* makeIndexSpace(int indexCount, const GrBuffer** buffer, int* startIndex) {
        return this->state()->makeIndexSpace(indexCount, buffer, startIndex);
    }

    /** Helpers for batches which over-allocate and then return data to the pool. */
    void putBackIndices(int indices) { this->state()->putBackIndices(indices); }
    void putBackVertices(int vertices, size_t vertexStride) {
        this->state()->putBackVertexSpace(vertices * vertexStride);
    }

private:
    GrVertexBatch* vertexBatch() { return static_cast<GrVertexBatch*>(this->batch()); }
    typedef GrDrawBatch::Target INHERITED;
};

#endif