Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2016 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 GrContextPriv_DEFINED
      9 #define GrContextPriv_DEFINED
     10 
     11 #include "GrContext.h"
     12 #include "GrSurfaceContext.h"
     13 #include "text/GrAtlasManager.h"
     14 
     15 class GrBackendRenderTarget;
     16 class GrOnFlushCallbackObject;
     17 class GrSemaphore;
     18 class GrSurfaceProxy;
     19 class GrTextureContext;
     20 
     21 class SkDeferredDisplayList;
     22 
     23 /** Class that adds methods to GrContext that are only intended for use internal to Skia.
     24     This class is purely a privileged window into GrContext. It should never have additional
     25     data members or virtual methods. */
     26 class GrContextPriv {
     27 public:
     28     /**
     29      * Create a GrContext without a resource cache
     30      */
     31     static sk_sp<GrContext> MakeDDL(sk_sp<GrContextThreadSafeProxy>);
     32 
     33     GrDrawingManager* drawingManager() { return fContext->fDrawingManager.get(); }
     34 
     35     sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
     36                                                       sk_sp<SkColorSpace> = nullptr,
     37                                                       const SkSurfaceProps* = nullptr);
     38 
     39     sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrSurfaceDesc&,
     40                                                        GrMipMapped,
     41                                                        SkBackingFit,
     42                                                        SkBudgeted,
     43                                                        sk_sp<SkColorSpace> colorSpace = nullptr,
     44                                                        const SkSurfaceProps* = nullptr);
     45 
     46     sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
     47                                                       GrSurfaceOrigin origin,
     48                                                       sk_sp<SkColorSpace> colorSpace);
     49 
     50     sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
     51                                                          const GrBackendTexture& tex,
     52                                                          GrSurfaceOrigin origin,
     53                                                          int sampleCnt,
     54                                                          sk_sp<SkColorSpace> colorSpace,
     55                                                          const SkSurfaceProps* = nullptr);
     56 
     57     sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
     58                                                               const GrBackendRenderTarget&,
     59                                                               GrSurfaceOrigin origin,
     60                                                               sk_sp<SkColorSpace> colorSpace,
     61                                                               const SkSurfaceProps* = nullptr);
     62 
     63     sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
     64                                                                  const GrBackendTexture& tex,
     65                                                                  GrSurfaceOrigin origin,
     66                                                                  int sampleCnt,
     67                                                                  sk_sp<SkColorSpace> colorSpace,
     68                                                                  const SkSurfaceProps* = nullptr);
     69 
     70     bool disableGpuYUVConversion() const { return fContext->fDisableGpuYUVConversion; }
     71     bool sharpenMipmappedTextures() const { return fContext->fSharpenMipmappedTextures; }
     72 
     73     /**
     74      * Call to ensure all drawing to the context has been issued to the
     75      * underlying 3D API.
     76      * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
     77      * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
     78      * provided then all current work will be flushed.
     79      */
     80     void flush(GrSurfaceProxy*);
     81 
     82     /**
     83      * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
     84      *
     85      * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
     86      * ensure its lifetime is tied to that of the context.
     87      */
     88     void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
     89 
     90     void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
     91 
     92     /**
     93      * After this returns any pending writes to the surface will have been issued to the
     94      * backend 3D API.
     95      */
     96     void flushSurfaceWrites(GrSurfaceProxy*);
     97 
     98     /**
     99      * After this returns any pending reads or writes to the surface will have been issued to the
    100      * backend 3D API.
    101      */
    102     void flushSurfaceIO(GrSurfaceProxy*);
    103 
    104     /**
    105      * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
    106      * if necessary.
    107      *
    108      * It is not necessary to call this before reading the render target via Skia/GrContext.
    109      * GrContext will detect when it must perform a resolve before reading pixels back from the
    110      * surface or using it as a texture.
    111      */
    112     void prepareSurfaceForExternalIO(GrSurfaceProxy*);
    113 
    114    /**
    115     * These flags can be used with the read/write pixels functions below.
    116     */
    117     enum PixelOpsFlags {
    118         /** The GrContext will not be flushed before the surface read or write. This means that
    119             the read or write may occur before previous draws have executed. */
    120         kDontFlush_PixelOpsFlag = 0x1,
    121         /** Any surface writes should be flushed to the backend 3D API after the surface operation
    122             is complete */
    123         kFlushWrites_PixelOp = 0x2,
    124         /** The src for write or dst read is unpremultiplied. This is only respected if both the
    125             config src and dst configs are an RGBA/BGRA 8888 format. */
    126         kUnpremul_PixelOpsFlag  = 0x4,
    127     };
    128 
    129     /**
    130      * Reads a rectangle of pixels from a surface.
    131      * @param src           the surface context to read from.
    132      * @param left          left edge of the rectangle to read (inclusive)
    133      * @param top           top edge of the rectangle to read (inclusive)
    134      * @param width         width of rectangle to read in pixels.
    135      * @param height        height of rectangle to read in pixels.
    136      * @param dstConfig     the pixel config of the destination buffer
    137      * @param dstColorSpace color space of the destination buffer
    138      * @param buffer        memory to read the rectangle into.
    139      * @param rowBytes      number of bytes bewtween consecutive rows. Zero means rows are tightly
    140      *                      packed.
    141      * @param pixelOpsFlags see PixelOpsFlags enum above.
    142      *
    143      * @return true if the read succeeded, false if not. The read can fail because of an unsupported
    144      *         pixel configs
    145      */
    146     bool readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, int height,
    147                            GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
    148                            size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
    149 
    150     /**
    151      * Writes a rectangle of pixels to a surface. There are currently two versions of this.
    152      * writeSurfacePixels() is the older version which will be replaced by the more robust and
    153      * maintainable (but perhaps slower) writeSurfacePixels2().
    154      *
    155      * @param dst           the surface context to write to.
    156      * @param left          left edge of the rectangle to write (inclusive)
    157      * @param top           top edge of the rectangle to write (inclusive)
    158      * @param width         width of rectangle to write in pixels.
    159      * @param height        height of rectangle to write in pixels.
    160      * @param srcColorType  the color type of the source buffer
    161      * @param srcColorSpace color space of the source buffer
    162      * @param buffer        memory to read pixels from
    163      * @param rowBytes      number of bytes between consecutive rows. Zero
    164      *                      means rows are tightly packed.
    165      * @param pixelOpsFlags see PixelOpsFlags enum above.
    166      * @return true if the write succeeded, false if not. The write can fail because of an
    167      *         unsupported combination of surface and src configs.
    168      */
    169     bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
    170                             GrColorType srcColorType, SkColorSpace* srcColorSpace,
    171                             const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
    172     bool writeSurfacePixels2(GrSurfaceContext* dst, int left, int top, int width, int height,
    173                              GrColorType srcColorType, SkColorSpace* srcColorSpace,
    174                              const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
    175 
    176     GrBackend getBackend() const { return fContext->fBackend; }
    177 
    178     SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
    179 
    180     GrProxyProvider* proxyProvider() { return fContext->fProxyProvider; }
    181     const GrProxyProvider* proxyProvider() const { return fContext->fProxyProvider; }
    182 
    183     GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
    184     const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
    185 
    186     GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
    187 
    188     GrGpu* getGpu() { return fContext->fGpu.get(); }
    189     const GrGpu* getGpu() const { return fContext->fGpu.get(); }
    190 
    191     GrGlyphCache* getGlyphCache() { return fContext->fGlyphCache; }
    192     GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
    193 
    194     GrRestrictedAtlasManager* getRestrictedAtlasManager() {
    195         return fContext->onGetRestrictedAtlasManager();
    196     }
    197 
    198     // This accessor should only ever be called by the GrOpFlushState.
    199     GrAtlasManager* getFullAtlasManager() {
    200         return fContext->onGetFullAtlasManager();
    201     }
    202 
    203     void moveOpListsToDDL(SkDeferredDisplayList*);
    204     void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
    205 
    206 private:
    207     explicit GrContextPriv(GrContext* context) : fContext(context) {}
    208     GrContextPriv(const GrContextPriv&); // unimpl
    209     GrContextPriv& operator=(const GrContextPriv&); // unimpl
    210 
    211     // No taking addresses of this type.
    212     const GrContextPriv* operator&() const;
    213     GrContextPriv* operator&();
    214 
    215     GrContext* fContext;
    216 
    217     friend class GrContext; // to construct/copy this type.
    218 };
    219 
    220 inline GrContextPriv GrContext::contextPriv() { return GrContextPriv(this); }
    221 
    222 inline const GrContextPriv GrContext::contextPriv () const {
    223     return GrContextPriv(const_cast<GrContext*>(this));
    224 }
    225 
    226 #endif
    227