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