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 
     10 
     11 #ifndef SkGpuDevice_DEFINED
     12 #define SkGpuDevice_DEFINED
     13 
     14 #include "SkGr.h"
     15 #include "SkBitmap.h"
     16 #include "SkDevice.h"
     17 #include "SkPicture.h"
     18 #include "SkRegion.h"
     19 #include "GrContext.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         kCached_Flag    = 1 << 1,  //!< Surface is cached and needs to be unlocked when released
     37         kDFFonts_Flag   = 1 << 2,  //!< Surface should render fonts using signed distance fields
     38     };
     39 
     40     /**
     41      * Creates an SkGpuDevice from a GrSurface. This will fail if the surface is not a render
     42      * target. The caller owns a ref on the returned device. If the surface is cached,
     43      * the kCached_Flag should be specified to make the device responsible for unlocking
     44      * the surface when it is released.
     45      */
     46     static SkGpuDevice* Create(GrSurface* surface, const SkSurfaceProps&, unsigned flags = 0);
     47 
     48     /**
     49      *  New device that will create an offscreen renderTarget based on the
     50      *  ImageInfo and sampleCount. The device's storage will not
     51      *  count against the GrContext's texture cache budget. The device's pixels
     52      *  will be uninitialized. On failure, returns NULL.
     53      */
     54     static SkGpuDevice* Create(GrContext*, const SkImageInfo&, const SkSurfaceProps&,
     55                                int sampleCount);
     56 
     57     virtual ~SkGpuDevice();
     58 
     59     GrContext* context() const { return fContext; }
     60 
     61     virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
     62 
     63     virtual SkImageInfo imageInfo() const SK_OVERRIDE {
     64         return fRenderTarget ? fRenderTarget->info() : SkImageInfo::MakeUnknown();
     65     }
     66 
     67     virtual void clear(SkColor color) SK_OVERRIDE;
     68     virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
     69     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
     70                             const SkPoint[], const SkPaint& paint) SK_OVERRIDE;
     71     virtual void drawRect(const SkDraw&, const SkRect& r,
     72                           const SkPaint& paint) SK_OVERRIDE;
     73     virtual void drawRRect(const SkDraw&, const SkRRect& r,
     74                            const SkPaint& paint) SK_OVERRIDE;
     75     virtual void drawDRRect(const SkDraw& draw, const SkRRect& outer,
     76                             const SkRRect& inner, const SkPaint& paint) SK_OVERRIDE;
     77     virtual void drawOval(const SkDraw&, const SkRect& oval,
     78                           const SkPaint& paint) SK_OVERRIDE;
     79     virtual void drawPath(const SkDraw&, const SkPath& path,
     80                           const SkPaint& paint, const SkMatrix* prePathMatrix,
     81                           bool pathIsMutable) SK_OVERRIDE;
     82     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
     83                             const SkMatrix&, const SkPaint&) SK_OVERRIDE;
     84     virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
     85                                 const SkRect* srcOrNull, const SkRect& dst,
     86                                 const SkPaint& paint,
     87                                 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
     88     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
     89                             int x, int y, const SkPaint& paint);
     90     virtual void drawText(const SkDraw&, const void* text, size_t len,
     91                           SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
     92     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
     93                              const SkScalar pos[], SkScalar constY,
     94                              int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
     95     virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
     96                                 const SkPath& path, const SkMatrix* matrix,
     97                                 const SkPaint&) SK_OVERRIDE;
     98     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
     99                               const SkPoint verts[], const SkPoint texs[],
    100                               const SkColor colors[], SkXfermode* xmode,
    101                               const uint16_t indices[], int indexCount,
    102                               const SkPaint&) SK_OVERRIDE;
    103     virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
    104                             const SkPaint&) SK_OVERRIDE;
    105     virtual bool filterTextFlags(const SkPaint&, TextFlags*) SK_OVERRIDE;
    106 
    107     virtual void flush() SK_OVERRIDE;
    108 
    109     virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE;
    110     virtual void onDetachFromCanvas() SK_OVERRIDE;
    111 
    112     virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
    113 
    114     virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE;
    115     virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
    116                              const SkImageFilter::Context&,
    117                              SkBitmap*, SkIPoint*) SK_OVERRIDE;
    118 
    119     class SkAutoCachedTexture; // used internally
    120 
    121 
    122 protected:
    123     virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int, int) SK_OVERRIDE;
    124     virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) SK_OVERRIDE;
    125 
    126     /**  PRIVATE / EXPERIMENTAL -- do not call */
    127     virtual void EXPERIMENTAL_optimize(const SkPicture* picture) SK_OVERRIDE;
    128     /**  PRIVATE / EXPERIMENTAL -- do not call */
    129     virtual bool EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture,
    130                                           const SkMatrix*, const SkPaint*) SK_OVERRIDE;
    131 
    132 private:
    133     GrContext*      fContext;
    134 
    135     GrSkDrawProcs*  fDrawProcs;
    136 
    137     GrClipData      fClipData;
    138 
    139     GrTextContext*  fMainTextContext;
    140     GrTextContext*  fFallbackTextContext;
    141 
    142     // state for our render-target
    143     GrRenderTarget*     fRenderTarget;
    144     bool                fNeedClear;
    145 
    146     // remove when our clients don't rely on accessBitmap()
    147     SkBitmap fLegacyBitmap;
    148 
    149     SkGpuDevice(GrSurface*, const SkSurfaceProps&, unsigned flags = 0);
    150 
    151     virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
    152 
    153     virtual SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&) SK_OVERRIDE;
    154 
    155     virtual SkImageFilter::Cache* getImageFilterCache() SK_OVERRIDE;
    156 
    157     // temporarily change the return to false, until we understand the issues with filters and persp
    158     virtual bool forceConservativeRasterClip() const SK_OVERRIDE { return true; }
    159 
    160     // sets the render target, clip, and matrix on GrContext. Use forceIdenity to override
    161     // SkDraw's matrix and draw in device coords.
    162     void prepareDraw(const SkDraw&, bool forceIdentity);
    163 
    164     /**
    165      * Implementation for both drawBitmap and drawBitmapRect.
    166      */
    167     void drawBitmapCommon(const SkDraw&,
    168                           const SkBitmap& bitmap,
    169                           const SkRect* srcRectPtr,
    170                           const SkSize* dstSizePtr,      // ignored iff srcRectPtr == NULL
    171                           const SkPaint&,
    172                           SkCanvas::DrawBitmapRectFlags flags);
    173 
    174     /**
    175      * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
    176      * matrix, clip, and the device's render target has already been set on GrContext.
    177      */
    178 
    179     // The tileSize and clippedSrcRect will be valid only if true is returned.
    180     bool shouldTileBitmap(const SkBitmap& bitmap,
    181                           const GrTextureParams& sampler,
    182                           const SkRect* srcRectPtr,
    183                           int maxTileSize,
    184                           int* tileSize,
    185                           SkIRect* clippedSrcRect) const;
    186     void internalDrawBitmap(const SkBitmap&,
    187                             const SkRect&,
    188                             const GrTextureParams& params,
    189                             const SkPaint& paint,
    190                             SkCanvas::DrawBitmapRectFlags flags,
    191                             bool bicubic,
    192                             bool needsTextureDomain);
    193     void drawTiledBitmap(const SkBitmap& bitmap,
    194                          const SkRect& srcRect,
    195                          const SkIRect& clippedSrcRect,
    196                          const GrTextureParams& params,
    197                          const SkPaint& paint,
    198                          SkCanvas::DrawBitmapRectFlags flags,
    199                          int tileSize,
    200                          bool bicubic);
    201 
    202     bool drawDashLine(const SkPoint pts[2], const SkPaint& paint);
    203 
    204     static SkPicture::AccelData::Key ComputeAccelDataKey();
    205 
    206     typedef SkBaseDevice INHERITED;
    207 };
    208 
    209 #endif
    210