GrGpu.h revision f2361d2d93c200cd4555b5e8ecea4531801abaaa
1/*
2 * Copyright 2011 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#ifndef GrGpu_DEFINED
9#define GrGpu_DEFINED
10
11#include "GrGpuCommandBuffer.h"
12#include "GrProgramDesc.h"
13#include "GrSwizzle.h"
14#include "GrAllocator.h"
15#include "GrTextureParamsAdjuster.h"
16#include "GrTypes.h"
17#include "GrXferProcessor.h"
18#include "SkPath.h"
19#include "SkTArray.h"
20#include <map>
21
22class GrBatchTracker;
23class GrBuffer;
24class GrContext;
25struct GrContextOptions;
26class GrGLContext;
27class GrMesh;
28class GrNonInstancedVertices;
29class GrPath;
30class GrPathRange;
31class GrPathRenderer;
32class GrPathRendererChain;
33class GrPathRendering;
34class GrPipeline;
35class GrPrimitiveProcessor;
36class GrRenderTarget;
37class GrStencilAttachment;
38class GrStencilSettings;
39class GrSurface;
40class GrTexture;
41
42namespace gr_instanced { class InstancedRendering; }
43
44class GrGpu : public SkRefCnt {
45public:
46    /**
47     * Create an instance of GrGpu that matches the specified backend. If the requested backend is
48     * not supported (at compile-time or run-time) this returns nullptr. The context will not be
49     * fully constructed and should not be used by GrGpu until after this function returns.
50     */
51    static GrGpu* Create(GrBackend, GrBackendContext, const GrContextOptions&, GrContext* context);
52
53    ////////////////////////////////////////////////////////////////////////////
54
55    GrGpu(GrContext* context);
56    ~GrGpu() override;
57
58    GrContext* getContext() { return fContext; }
59    const GrContext* getContext() const { return fContext; }
60
61    /**
62     * Gets the capabilities of the draw target.
63     */
64    const GrCaps* caps() const { return fCaps.get(); }
65
66    GrPathRendering* pathRendering() { return fPathRendering.get();  }
67
68    enum class DisconnectType {
69        // No cleanup should be attempted, immediately cease making backend API calls
70        kAbandon,
71        // Free allocated resources (not known by GrResourceCache) before returning and
72        // ensure no backend backend 3D API calls will be made after disconnect() returns.
73        kCleanup,
74    };
75
76    // Called by GrContext when the underlying backend context is already or will be destroyed
77    // before GrContext.
78    virtual void disconnect(DisconnectType);
79
80    /**
81     * The GrGpu object normally assumes that no outsider is setting state
82     * within the underlying 3D API's context/device/whatever. This call informs
83     * the GrGpu that the state was modified and it shouldn't make assumptions
84     * about the state.
85     */
86    void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; }
87
88    /**
89     * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can
90     * be used as a render target by calling GrTexture::asRenderTarget(). Not all
91     * pixel configs can be used as render targets. Support for configs as textures
92     * or render targets can be checked using GrCaps.
93     *
94     * @param desc        describes the texture to be created.
95     * @param budgeted    does this texture count against the resource cache budget?
96     * @param texels      array of mipmap levels containing texel data to load.
97     *                    Each level begins with full-size palette data for paletted textures.
98     *                    For compressed formats the level contains the compressed pixel data.
99     *                    Otherwise, it contains width*height texels. If there is only one
100     *                    element and it contains nullptr fPixels, texture data is
101     *                    uninitialized.
102     * @return    The texture object if successful, otherwise nullptr.
103     */
104    GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
105                             const SkTArray<GrMipLevel>& texels);
106
107    /**
108     * Simplified createTexture() interface for when there is no initial texel data to upload.
109     */
110    GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) {
111        return this->createTexture(desc, budgeted, SkTArray<GrMipLevel>());
112    }
113
114    /** Simplified createTexture() interface for when there is only a base level */
115    GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, const void* level0Data,
116                             size_t rowBytes) {
117        SkASSERT(level0Data);
118        GrMipLevel level = { level0Data, rowBytes };
119        SkSTArray<1, GrMipLevel> array;
120        array.push_back() = level;
121        return this->createTexture(desc, budgeted, array);
122    }
123
124    /**
125     * Implements GrTextureProvider::wrapBackendTexture
126     */
127    GrTexture* wrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership);
128
129    /**
130     * Implements GrTextureProvider::wrapBackendRenderTarget
131     */
132    GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&, GrWrapOwnership);
133
134    /**
135     * Implements GrTextureProvider::wrapBackendTextureAsRenderTarget
136     */
137    GrRenderTarget* wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc&);
138
139    /**
140     * Creates a buffer in GPU memory. For a client-side buffer use GrBuffer::CreateCPUBacked.
141     *
142     * @param size            size of buffer to create.
143     * @param intendedType    hint to the graphics subsystem about what the buffer will be used for.
144     * @param accessPattern   hint to the graphics subsystem about how the data will be accessed.
145     * @param data            optional data with which to initialize the buffer.
146     *
147     * @return the buffer if successful, otherwise nullptr.
148     */
149    GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern accessPattern,
150                           const void* data = nullptr);
151
152    /**
153     * Creates an instanced rendering object if it is supported on this platform.
154     */
155    gr_instanced::InstancedRendering* createInstancedRendering();
156
157    /**
158     * Resolves MSAA.
159     */
160    void resolveRenderTarget(GrRenderTarget* target);
161
162    /** Info struct returned by getReadPixelsInfo about performing intermediate draws before
163        reading pixels for performance or correctness. */
164    struct ReadPixelTempDrawInfo {
165        /** If the GrGpu is requesting that the caller do a draw to an intermediate surface then
166            this is descriptor for the temp surface. The draw should always be a rect with
167            dst 0,0,w,h. */
168        GrSurfaceDesc   fTempSurfaceDesc;
169        /** Indicates whether there is a performance advantage to using an exact match texture
170            (in terms of width and height) for the intermediate texture instead of approximate. */
171        SkBackingFit    fTempSurfaceFit;
172        /** Swizzle to apply during the draw. This is used to compensate for either feature or
173            performance limitations in the underlying 3D API. */
174        GrSwizzle       fSwizzle;
175        /** The config that should be used to read from the temp surface after the draw. This may be
176            different than the original read config in order to compensate for swizzling. The
177            read data will effectively be in the original read config. */
178        GrPixelConfig   fReadConfig;
179    };
180
181    /** Describes why an intermediate draw must/should be performed before readPixels. */
182    enum DrawPreference {
183        /** On input means that the caller would proceed without draw if the GrGpu doesn't request
184            one.
185            On output means that the GrGpu is not requesting a draw. */
186        kNoDraw_DrawPreference,
187        /** Means that the client would prefer a draw for performance of the readback but
188            can satisfy a straight readPixels call on the inputs without an intermediate draw.
189            getReadPixelsInfo will never set the draw preference to this value but may leave
190            it set. */
191        kCallerPrefersDraw_DrawPreference,
192        /** On output means that GrGpu would prefer a draw for performance of the readback but
193            can satisfy a straight readPixels call on the inputs without an intermediate draw. The
194            caller of getReadPixelsInfo should never specify this on intput. */
195        kGpuPrefersDraw_DrawPreference,
196        /** On input means that the caller requires a draw to do a transformation and there is no
197            CPU fallback.
198            On output means that GrGpu can only satisfy the readPixels request if the intermediate
199            draw is performed.
200          */
201        kRequireDraw_DrawPreference
202    };
203
204    /**
205     * Used to negotiate whether and how an intermediate draw should or must be performed before
206     * a readPixels call. If this returns false then GrGpu could not deduce an intermediate draw
207     * that would allow a successful readPixels call. The passed width, height, and rowBytes,
208     * must be non-zero and already reflect clipping to the src bounds.
209     */
210    bool getReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
211                           GrPixelConfig readConfig, DrawPreference*, ReadPixelTempDrawInfo*);
212
213    /** Info struct returned by getWritePixelsInfo about performing an intermediate draw in order
214        to write pixels to a GrSurface for either performance or correctness reasons. */
215    struct WritePixelTempDrawInfo {
216        /** If the GrGpu is requesting that the caller upload to an intermediate surface and draw
217            that to the dst then this is the descriptor for the intermediate surface. The caller
218            should upload the pixels such that the upper left pixel of the upload rect is at 0,0 in
219            the intermediate surface.*/
220        GrSurfaceDesc   fTempSurfaceDesc;
221        /** Swizzle to apply during the draw. This is used to compensate for either feature or
222            performance limitations in the underlying 3D API. */
223        GrSwizzle       fSwizzle;
224        /** The config that should be specified when uploading the *original* data to the temp
225            surface before the draw. This may be different than the original src data config in
226            order to compensate for swizzling that will occur when drawing. */
227        GrPixelConfig   fWriteConfig;
228    };
229
230    /**
231     * Used to negotiate whether and how an intermediate surface should be used to write pixels to
232     * a GrSurface. If this returns false then GrGpu could not deduce an intermediate draw
233     * that would allow a successful transfer of the src pixels to the dst. The passed width,
234     * height, and rowBytes, must be non-zero and already reflect clipping to the dst bounds.
235     */
236    bool getWritePixelsInfo(GrSurface* dstSurface, int width, int height,
237                            GrPixelConfig srcConfig, DrawPreference*, WritePixelTempDrawInfo*);
238
239    /**
240     * Reads a rectangle of pixels from a render target.
241     *
242     * @param surface       The surface to read from
243     * @param left          left edge of the rectangle to read (inclusive)
244     * @param top           top edge of the rectangle to read (inclusive)
245     * @param width         width of rectangle to read in pixels.
246     * @param height        height of rectangle to read in pixels.
247     * @param config        the pixel config of the destination buffer
248     * @param buffer        memory to read the rectangle into.
249     * @param rowBytes      the number of bytes between consecutive rows. Zero
250     *                      means rows are tightly packed.
251     * @param invertY       buffer should be populated bottom-to-top as opposed
252     *                      to top-to-bottom (skia's usual order)
253     *
254     * @return true if the read succeeded, false if not. The read can fail
255     *              because of a unsupported pixel config or because no render
256     *              target is currently set.
257     */
258    bool readPixels(GrSurface* surface,
259                    int left, int top, int width, int height,
260                    GrPixelConfig config, void* buffer, size_t rowBytes);
261
262    /**
263     * Updates the pixels in a rectangle of a surface.
264     *
265     * @param surface       The surface to write to.
266     * @param left          left edge of the rectangle to write (inclusive)
267     * @param top           top edge of the rectangle to write (inclusive)
268     * @param width         width of rectangle to write in pixels.
269     * @param height        height of rectangle to write in pixels.
270     * @param config        the pixel config of the source buffer
271     * @param texels        array of mipmap levels containing texture data
272     */
273    bool writePixels(GrSurface* surface,
274                     int left, int top, int width, int height,
275                     GrPixelConfig config,
276                     const SkTArray<GrMipLevel>& texels);
277
278    /**
279     * This function is a shim which creates a SkTArray<GrMipLevel> of size 1.
280     * It then calls writePixels with that SkTArray.
281     *
282     * @param buffer   memory to read pixels from.
283     * @param rowBytes number of bytes between consecutive rows. Zero
284     *                 means rows are tightly packed.
285     */
286    bool writePixels(GrSurface* surface,
287                     int left, int top, int width, int height,
288                     GrPixelConfig config, const void* buffer,
289                     size_t rowBytes);
290
291    /**
292     * Updates the pixels in a rectangle of a surface using a buffer
293     *
294     * @param surface          The surface to write to.
295     * @param left             left edge of the rectangle to write (inclusive)
296     * @param top              top edge of the rectangle to write (inclusive)
297     * @param width            width of rectangle to write in pixels.
298     * @param height           height of rectangle to write in pixels.
299     * @param config           the pixel config of the source buffer
300     * @param transferBuffer   GrBuffer to read pixels from (type must be "kCpuToGpu")
301     * @param offset           offset from the start of the buffer
302     * @param rowBytes         number of bytes between consecutive rows. Zero
303     *                         means rows are tightly packed.
304     */
305    bool transferPixels(GrSurface* surface,
306                        int left, int top, int width, int height,
307                        GrPixelConfig config, GrBuffer* transferBuffer,
308                        size_t offset, size_t rowBytes, GrFence* fence);
309
310    /**
311     * This is can be called before allocating a texture to be a dst for copySurface. This is only
312     * used for doing dst copies needed in blends, thus the src is always a GrRenderTarget. It will
313     * populate the origin, config, and flags fields of the desc such that copySurface can
314     * efficiently succeed.
315     */
316    virtual bool initDescForDstCopy(const GrRenderTarget* src, GrSurfaceDesc* desc) const = 0;
317
318    // After the client interacts directly with the 3D context state the GrGpu
319    // must resync its internal state and assumptions about 3D context state.
320    // Each time this occurs the GrGpu bumps a timestamp.
321    // state of the 3D context
322    // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
323    // a billion years.
324    typedef uint64_t ResetTimestamp;
325
326    // This timestamp is always older than the current timestamp
327    static const ResetTimestamp kExpiredTimestamp = 0;
328    // Returns a timestamp based on the number of times the context was reset.
329    // This timestamp can be used to lazily detect when cached 3D context state
330    // is dirty.
331    ResetTimestamp getResetTimestamp() const { return fResetTimestamp; }
332
333    // Called to perform a surface to surface copy. Fallbacks to issuing a draw from the src to dst
334    // take place at the GrOpList level and this function implement faster copy paths. The rect
335    // and point are pre-clipped. The src rect and implied dst rect are guaranteed to be within the
336    // src/dst bounds and non-empty.
337    bool copySurface(GrSurface* dst,
338                     GrSurface* src,
339                     const SkIRect& srcRect,
340                     const SkIPoint& dstPoint);
341
342    struct MultisampleSpecs {
343        MultisampleSpecs(uint8_t uniqueID, int effectiveSampleCnt, const SkPoint* locations)
344            : fUniqueID(uniqueID),
345              fEffectiveSampleCnt(effectiveSampleCnt),
346              fSampleLocations(locations) {}
347
348        // Nonzero ID that uniquely identifies these multisample specs.
349        uint8_t          fUniqueID;
350        // The actual number of samples the GPU will run. NOTE: this value can be greater than the
351        // the render target's sample count.
352        int              fEffectiveSampleCnt;
353        // If sample locations are supported, points to the subpixel locations at which the GPU will
354        // sample. Pixel center is at (.5, .5), and (0, 0) indicates the top left corner.
355        const SkPoint*   fSampleLocations;
356    };
357
358    // Finds a render target's multisample specs. The stencil settings are only needed to flush the
359    // draw state prior to querying multisample information; they should not have any effect on the
360    // multisample information itself.
361    const MultisampleSpecs& getMultisampleSpecs(GrRenderTarget*, const GrStencilSettings&);
362
363    // Creates a GrGpuCommandBuffer in which the GrOpList can send draw commands to instead of
364    // directly to the Gpu object.
365    virtual GrGpuCommandBuffer* createCommandBuffer(
366            GrRenderTarget* target,
367            const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
368            const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) = 0;
369
370    // Called by GrOpList when flushing.
371    // Provides a hook for post-flush actions (e.g. PLS reset and Vulkan command buffer submits).
372    virtual void finishOpList() {}
373
374    virtual GrFence SK_WARN_UNUSED_RESULT insertFence() const = 0;
375    virtual bool waitFence(GrFence, uint64_t timeout = 1000) const = 0;
376    virtual void deleteFence(GrFence) const = 0;
377
378    ///////////////////////////////////////////////////////////////////////////
379    // Debugging and Stats
380
381    class Stats {
382    public:
383#if GR_GPU_STATS
384        Stats() { this->reset(); }
385
386        void reset() {
387            fRenderTargetBinds = 0;
388            fShaderCompilations = 0;
389            fTextureCreates = 0;
390            fTextureUploads = 0;
391            fTransfersToTexture = 0;
392            fStencilAttachmentCreates = 0;
393            fNumDraws = 0;
394            fNumFailedDraws = 0;
395        }
396
397        int renderTargetBinds() const { return fRenderTargetBinds; }
398        void incRenderTargetBinds() { fRenderTargetBinds++; }
399        int shaderCompilations() const { return fShaderCompilations; }
400        void incShaderCompilations() { fShaderCompilations++; }
401        int textureCreates() const { return fTextureCreates; }
402        void incTextureCreates() { fTextureCreates++; }
403        int textureUploads() const { return fTextureUploads; }
404        void incTextureUploads() { fTextureUploads++; }
405        int transfersToTexture() const { return fTransfersToTexture; }
406        void incTransfersToTexture() { fTransfersToTexture++; }
407        void incStencilAttachmentCreates() { fStencilAttachmentCreates++; }
408        void incNumDraws() { fNumDraws++; }
409        void incNumFailedDraws() { ++fNumFailedDraws; }
410        void dump(SkString*);
411        void dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values);
412        int numDraws() const { return fNumDraws; }
413        int numFailedDraws() const { return fNumFailedDraws; }
414    private:
415        int fRenderTargetBinds;
416        int fShaderCompilations;
417        int fTextureCreates;
418        int fTextureUploads;
419        int fTransfersToTexture;
420        int fStencilAttachmentCreates;
421        int fNumDraws;
422        int fNumFailedDraws;
423#else
424        void dump(SkString*) {}
425        void dumpKeyValuePairs(SkTArray<SkString>*, SkTArray<double>*) {}
426        void incRenderTargetBinds() {}
427        void incShaderCompilations() {}
428        void incTextureCreates() {}
429        void incTextureUploads() {}
430        void incTransfersToTexture() {}
431        void incStencilAttachmentCreates() {}
432        void incNumDraws() {}
433        void incNumFailedDraws() {}
434#endif
435    };
436
437    Stats* stats() { return &fStats; }
438
439    /** Creates a texture directly in the backend API without wrapping it in a GrTexture. This is
440        only to be used for testing (particularly for testing the methods that import an externally
441        created texture into Skia. Must be matched with a call to deleteTestingOnlyTexture(). */
442    virtual GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
443                                                            GrPixelConfig config,
444                                                            bool isRenderTarget = false) = 0;
445    /** Check a handle represents an actual texture in the backend API that has not been freed. */
446    virtual bool isTestingOnlyBackendTexture(GrBackendObject) const = 0;
447    /** If ownership of the backend texture has been transferred pass true for abandonTexture. This
448        will do any necessary cleanup of the handle without freeing the texture in the backend
449        API. */
450    virtual void deleteTestingOnlyBackendTexture(GrBackendObject,
451                                                 bool abandonTexture = false) = 0;
452
453    // width and height may be larger than rt (if underlying API allows it).
454    // Returns nullptr if compatible sb could not be created, otherwise the caller owns the ref on
455    // the GrStencilAttachment.
456    virtual GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
457                                                                        int width,
458                                                                        int height) = 0;
459    // clears target's entire stencil buffer to 0
460    virtual void clearStencil(GrRenderTarget* target) = 0;
461
462    // draws an outline rectangle for debugging/visualization purposes.
463    virtual void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) = 0;
464
465    // Determines whether a texture will need to be rescaled in order to be used with the
466    // GrTextureParams. This variation is called when the caller will create a new texture using the
467    // texture provider from a non-texture src (cpu-backed image, ...).
468    bool makeCopyForTextureParams(int width, int height, const GrTextureParams&,
469                                 GrTextureProducer::CopyParams*) const;
470
471    // Like the above but this variation should be called when the caller is not creating the
472    // original texture but rather was handed the original texture. It adds additional checks
473    // relevant to original textures that were created external to Skia via
474    // GrTextureProvider::wrap methods.
475    bool makeCopyForTextureParams(GrTexture* texture, const GrTextureParams& params,
476                                  GrTextureProducer::CopyParams* copyParams) const {
477        if (this->makeCopyForTextureParams(texture->width(), texture->height(), params,
478                                           copyParams)) {
479            return true;
480        }
481        return this->onMakeCopyForTextureParams(texture, params, copyParams);
482    }
483
484    // This is only to be used in GL-specific tests.
485    virtual const GrGLContext* glContextForTesting() const { return nullptr; }
486
487    // This is only to be used by testing code
488    virtual void resetShaderCacheForTesting() const {}
489
490    void handleDirtyContext() {
491        if (fResetBits) {
492            this->resetContext();
493        }
494    }
495
496protected:
497    static void ElevateDrawPreference(GrGpu::DrawPreference* preference,
498                                      GrGpu::DrawPreference elevation) {
499        GR_STATIC_ASSERT(GrGpu::kCallerPrefersDraw_DrawPreference > GrGpu::kNoDraw_DrawPreference);
500        GR_STATIC_ASSERT(GrGpu::kGpuPrefersDraw_DrawPreference >
501                         GrGpu::kCallerPrefersDraw_DrawPreference);
502        GR_STATIC_ASSERT(GrGpu::kRequireDraw_DrawPreference >
503                         GrGpu::kGpuPrefersDraw_DrawPreference);
504        *preference = SkTMax(*preference, elevation);
505    }
506
507    // Handles cases where a surface will be updated without a call to flushRenderTarget
508    void didWriteToSurface(GrSurface* surface, const SkIRect* bounds, uint32_t mipLevels = 1) const;
509
510    Stats                                   fStats;
511    SkAutoTDelete<GrPathRendering>          fPathRendering;
512    // Subclass must initialize this in its constructor.
513    SkAutoTUnref<const GrCaps>    fCaps;
514
515    typedef SkTArray<SkPoint, true> SamplePattern;
516
517private:
518    // called when the 3D context state is unknown. Subclass should emit any
519    // assumed 3D context state and dirty any state cache.
520    virtual void onResetContext(uint32_t resetBits) = 0;
521
522    // Called before certain draws in order to guarantee coherent results from dst reads.
523    virtual void xferBarrier(GrRenderTarget*, GrXferBarrierType) = 0;
524
525    // overridden by backend-specific derived class to create objects.
526    // Texture size and sample size will have already been validated in base class before
527    // onCreateTexture/CompressedTexture are called.
528    virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
529                                       SkBudgeted budgeted,
530                                       const SkTArray<GrMipLevel>& texels) = 0;
531    virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
532                                                 SkBudgeted budgeted,
533                                                 const SkTArray<GrMipLevel>& texels) = 0;
534
535    virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership) = 0;
536    virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
537                                                      GrWrapOwnership) = 0;
538    virtual GrRenderTarget* onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc&) = 0;
539    virtual GrBuffer* onCreateBuffer(size_t size, GrBufferType intendedType, GrAccessPattern,
540                                     const void* data) = 0;
541
542    virtual gr_instanced::InstancedRendering* onCreateInstancedRendering() = 0;
543
544    virtual bool onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams&,
545                                            GrTextureProducer::CopyParams*) const { return false; }
546
547    virtual bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight,
548                                     size_t rowBytes, GrPixelConfig readConfig, DrawPreference*,
549                                     ReadPixelTempDrawInfo*) = 0;
550    virtual bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
551                                      GrPixelConfig srcConfig, DrawPreference*,
552                                      WritePixelTempDrawInfo*) = 0;
553
554    // overridden by backend-specific derived class to perform the surface read
555    virtual bool onReadPixels(GrSurface*,
556                              int left, int top,
557                              int width, int height,
558                              GrPixelConfig,
559                              void* buffer,
560                              size_t rowBytes) = 0;
561
562    // overridden by backend-specific derived class to perform the surface write
563    virtual bool onWritePixels(GrSurface*,
564                               int left, int top, int width, int height,
565                               GrPixelConfig config,
566                               const SkTArray<GrMipLevel>& texels) = 0;
567
568    // overridden by backend-specific derived class to perform the surface write
569    virtual bool onTransferPixels(GrSurface*,
570                                  int left, int top, int width, int height,
571                                  GrPixelConfig config, GrBuffer* transferBuffer,
572                                  size_t offset, size_t rowBytes) = 0;
573
574    // overridden by backend-specific derived class to perform the resolve
575    virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
576
577    // overridden by backend specific derived class to perform the copy surface
578    virtual bool onCopySurface(GrSurface* dst,
579                               GrSurface* src,
580                               const SkIRect& srcRect,
581                               const SkIPoint& dstPoint) = 0;
582
583    // overridden by backend specific derived class to perform the multisample queries
584    virtual void onGetMultisampleSpecs(GrRenderTarget*, const GrStencilSettings&,
585                                       int* effectiveSampleCnt, SamplePattern*) = 0;
586
587    void resetContext() {
588        this->onResetContext(fResetBits);
589        fResetBits = 0;
590        ++fResetTimestamp;
591    }
592
593    struct SamplePatternComparator {
594        bool operator()(const SamplePattern&, const SamplePattern&) const;
595    };
596
597    typedef std::map<SamplePattern, uint8_t, SamplePatternComparator> MultisampleSpecsIdMap;
598
599    ResetTimestamp                         fResetTimestamp;
600    uint32_t                               fResetBits;
601    MultisampleSpecsIdMap                  fMultisampleSpecsIdMap;
602    SkSTArray<1, MultisampleSpecs, true>   fMultisampleSpecs;
603    // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
604    GrContext*                             fContext;
605
606    friend class GrPathRendering;
607    friend class gr_instanced::InstancedRendering;
608    typedef SkRefCnt INHERITED;
609};
610
611#endif
612