Home | History | Annotate | Download | only in gpu
      1 
      2 /*
      3  * Copyright 2010 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 #ifndef SkGpuDevice_DEFINED
     10 #define SkGpuDevice_DEFINED
     11 
     12 #include "SkGr.h"
     13 #include "SkBitmap.h"
     14 #include "SkDevice.h"
     15 #include "SkPicture.h"
     16 #include "SkRegion.h"
     17 #include "SkSurface.h"
     18 #include "GrContext.h"
     19 #include "GrSurfacePriv.h"
     20 
     21 struct SkDrawProcs;
     22 struct GrSkDrawProcs;
     23 
     24 class GrAccelData;
     25 struct GrCachedLayer;
     26 class GrTextContext;
     27 
     28 /**
     29  *  Subclass of SkBaseDevice, which directs all drawing to the GrGpu owned by the
     30  *  canvas.
     31  */
     32 class SK_API SkGpuDevice : public SkBaseDevice {
     33 public:
     34     enum Flags {
     35         kNeedClear_Flag = 1 << 0,  //!< Surface requires an initial clear
     36     };
     37 
     38     /**
     39      * Creates an SkGpuDevice from a GrRenderTarget.
     40      */
     41     static SkGpuDevice* Create(GrRenderTarget* target, const SkSurfaceProps*, unsigned flags = 0);
     42 
     43     /**
     44      * Creates an SkGpuDevice from a GrRenderTarget whose texture width/height is
     45      * different than its actual width/height (e.g., approx-match scratch texture).
     46      */
     47     static SkGpuDevice* Create(GrRenderTarget* target, int width, int height,
     48                                const SkSurfaceProps*, unsigned flags = 0);
     49 
     50     /**
     51      * New device that will create an offscreen renderTarget based on the ImageInfo and
     52      * sampleCount. The Budgeted param controls whether the device's backing store counts against
     53      * the resource cache budget. On failure, returns NULL.
     54      */
     55     static SkGpuDevice* Create(GrContext*, SkSurface::Budgeted, const SkImageInfo&,
     56                                int sampleCount, const SkSurfaceProps*, unsigned flags = 0);
     57 
     58     virtual ~SkGpuDevice();
     59 
     60     SkGpuDevice* cloneDevice(const SkSurfaceProps& props) {
     61         SkBaseDevice* dev = this->onCreateDevice(CreateInfo(this->imageInfo(), kPossible_TileUsage,
     62                                                             props.pixelGeometry()),
     63                                                  NULL);
     64         return static_cast<SkGpuDevice*>(dev);
     65     }
     66 
     67     GrContext* context() const { return fContext; }
     68 
     69     // set all pixels to 0
     70     void clearAll();
     71 
     72     void replaceRenderTarget(bool shouldRetainContent);
     73 
     74     GrRenderTarget* accessRenderTarget() override;
     75 
     76     SkImageInfo imageInfo() const override {
     77         return fLegacyBitmap.info();
     78     }
     79 
     80     const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
     81 
     82     void drawPaint(const SkDraw&, const SkPaint& paint) override;
     83     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
     84                             const SkPoint[], const SkPaint& paint) override;
     85     virtual void drawRect(const SkDraw&, const SkRect& r,
     86                           const SkPaint& paint) override;
     87     virtual void drawRRect(const SkDraw&, const SkRRect& r,
     88                            const SkPaint& paint) override;
     89     virtual void drawDRRect(const SkDraw& draw, const SkRRect& outer,
     90                             const SkRRect& inner, const SkPaint& paint) override;
     91     virtual void drawOval(const SkDraw&, const SkRect& oval,
     92                           const SkPaint& paint) override;
     93     virtual void drawPath(const SkDraw&, const SkPath& path,
     94                           const SkPaint& paint, const SkMatrix* prePathMatrix,
     95                           bool pathIsMutable) override;
     96     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
     97                             const SkMatrix&, const SkPaint&) override;
     98     virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
     99                                 const SkRect* srcOrNull, const SkRect& dst,
    100                                 const SkPaint& paint,
    101                                 SkCanvas::DrawBitmapRectFlags flags) override;
    102     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
    103                             int x, int y, const SkPaint& paint) override;
    104     virtual void drawText(const SkDraw&, const void* text, size_t len,
    105                           SkScalar x, SkScalar y, const SkPaint&) override;
    106     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
    107                              const SkScalar pos[], int scalarsPerPos,
    108                              const SkPoint& offset, const SkPaint&) override;
    109     virtual void drawTextBlob(const SkDraw&, const SkTextBlob*, SkScalar x, SkScalar y,
    110                               const SkPaint& paint, SkDrawFilter* drawFilter) override;
    111     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
    112                               const SkPoint verts[], const SkPoint texs[],
    113                               const SkColor colors[], SkXfermode* xmode,
    114                               const uint16_t indices[], int indexCount,
    115                               const SkPaint&) override;
    116     virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
    117                             const SkPaint&) override;
    118     void drawImage(const SkDraw&, const SkImage*, SkScalar x, SkScalar y, const SkPaint&) override;
    119     void drawImageRect(const SkDraw&, const SkImage*, const SkRect* src, const SkRect& dst,
    120                        const SkPaint&) override;
    121 
    122     void flush() override;
    123 
    124     void onAttachToCanvas(SkCanvas* canvas) override;
    125     void onDetachFromCanvas() override;
    126 
    127     const SkBitmap& onAccessBitmap() override;
    128 
    129     bool canHandleImageFilter(const SkImageFilter*) override;
    130     virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
    131                              const SkImageFilter::Context&,
    132                              SkBitmap*, SkIPoint*) override;
    133 
    134     bool filterTexture(GrContext*, GrTexture*, int width, int height, const SkImageFilter*,
    135                        const SkImageFilter::Context&,
    136                        SkBitmap* result, SkIPoint* offset);
    137 
    138 protected:
    139     bool onReadPixels(const SkImageInfo&, void*, size_t, int, int) override;
    140     bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) override;
    141     bool onShouldDisableLCD(const SkPaint&) const override;
    142 
    143     /**  PRIVATE / EXPERIMENTAL -- do not call */
    144     virtual bool EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture,
    145                                           const SkMatrix*, const SkPaint*) override;
    146 
    147 private:
    148     GrContext*                      fContext;
    149     GrSkDrawProcs*                  fDrawProcs;
    150     SkAutoTUnref<const SkClipStack> fClipStack;
    151     SkIPoint                        fClipOrigin;
    152     GrClip                          fClip;
    153     GrTextContext*                  fTextContext;
    154     SkSurfaceProps                  fSurfaceProps;
    155     GrRenderTarget*                 fRenderTarget;
    156     // remove when our clients don't rely on accessBitmap()
    157     SkBitmap                        fLegacyBitmap;
    158     bool                            fNeedClear;
    159 
    160     SkGpuDevice(GrRenderTarget*, int width, int height, const SkSurfaceProps*, unsigned flags);
    161 
    162     SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
    163 
    164     SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&) override;
    165 
    166     SkImageFilter::Cache* getImageFilterCache() override;
    167 
    168     bool forceConservativeRasterClip() const override { return true; }
    169 
    170     // sets the render target and clip on context
    171     void prepareDraw(const SkDraw&);
    172 
    173     /**
    174      * Implementation for both drawBitmap and drawBitmapRect.
    175      */
    176     void drawBitmapCommon(const SkDraw&,
    177                           const SkBitmap& bitmap,
    178                           const SkRect* srcRectPtr,
    179                           const SkSize* dstSizePtr,      // ignored iff srcRectPtr == NULL
    180                           const SkPaint&,
    181                           SkCanvas::DrawBitmapRectFlags flags);
    182 
    183     /**
    184      * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
    185      * matrix, clip, and the device's render target has already been set on GrContext.
    186      */
    187 
    188     // The tileSize and clippedSrcRect will be valid only if true is returned.
    189     bool shouldTileBitmap(const SkBitmap& bitmap,
    190                           const SkMatrix& viewMatrix,
    191                           const GrTextureParams& sampler,
    192                           const SkRect* srcRectPtr,
    193                           int maxTileSize,
    194                           int* tileSize,
    195                           SkIRect* clippedSrcRect) const;
    196     void internalDrawBitmap(const SkBitmap&,
    197                             const SkMatrix& viewMatrix,
    198                             const SkRect&,
    199                             const GrTextureParams& params,
    200                             const SkPaint& paint,
    201                             SkCanvas::DrawBitmapRectFlags flags,
    202                             bool bicubic,
    203                             bool needsTextureDomain);
    204     void drawTiledBitmap(const SkBitmap& bitmap,
    205                          const SkMatrix& viewMatrix,
    206                          const SkRect& srcRect,
    207                          const SkIRect& clippedSrcRect,
    208                          const GrTextureParams& params,
    209                          const SkPaint& paint,
    210                          SkCanvas::DrawBitmapRectFlags flags,
    211                          int tileSize,
    212                          bool bicubic);
    213 
    214     void internalDrawPath(const SkPath& origSrcPath, const SkPaint& paint,
    215                           const SkMatrix& origViewMatrix, const SkMatrix* prePathMatrix,
    216                           const SkIRect& clipBounds, bool pathIsMutable);
    217 
    218     bool drawDashLine(const SkPoint pts[2], const SkPaint& paint);
    219 
    220     static SkPicture::AccelData::Key ComputeAccelDataKey();
    221 
    222     static GrRenderTarget* CreateRenderTarget(GrContext*, SkSurface::Budgeted, const SkImageInfo&,
    223                                               int sampleCount);
    224 
    225     friend class GrAtlasTextContext;
    226     friend class GrTextContext;
    227     typedef SkBaseDevice INHERITED;
    228 };
    229 
    230 #endif
    231