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 "SkRegion.h"
     18 #include "GrContext.h"
     19 
     20 struct SkDrawProcs;
     21 struct GrSkDrawProcs;
     22 class GrTextContext;
     23 
     24 /**
     25  *  Subclass of SkDevice, which directs all drawing to the GrGpu owned by the
     26  *  canvas.
     27  */
     28 class SK_API SkGpuDevice : public SkDevice {
     29 public:
     30     /**
     31      *  New device that will create an offscreen renderTarget based on the
     32      *  config, width, height, and sampleCount. The device's storage will not
     33      *  count against the GrContext's texture cache budget. The device's pixels
     34      *  will be uninitialized.
     35      */
     36     SkGpuDevice(GrContext*, SkBitmap::Config, int width, int height, int sampleCount = 0);
     37 
     38     /**
     39      *  New device that will render to the specified renderTarget.
     40      */
     41     SkGpuDevice(GrContext*, GrRenderTarget*);
     42 
     43     /**
     44      *  New device that will render to the texture (as a rendertarget).
     45      *  The GrTexture's asRenderTarget() must be non-NULL or device will not
     46      *  function.
     47      */
     48     SkGpuDevice(GrContext*, GrTexture*);
     49 
     50     virtual ~SkGpuDevice();
     51 
     52     GrContext* context() const { return fContext; }
     53 
     54     virtual SkGpuRenderTarget* accessRenderTarget() SK_OVERRIDE;
     55 
     56     // overrides from SkDevice
     57 
     58     virtual void clear(SkColor color) SK_OVERRIDE;
     59     virtual void writePixels(const SkBitmap& bitmap, int x, int y,
     60                              SkCanvas::Config8888 config8888) SK_OVERRIDE;
     61 
     62     virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
     63     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
     64                             const SkPoint[], const SkPaint& paint) SK_OVERRIDE;
     65     virtual void drawRect(const SkDraw&, const SkRect& r,
     66                           const SkPaint& paint) SK_OVERRIDE;
     67     virtual void drawOval(const SkDraw&, const SkRect& oval,
     68                           const SkPaint& paint) SK_OVERRIDE;
     69     virtual void drawPath(const SkDraw&, const SkPath& path,
     70                           const SkPaint& paint, const SkMatrix* prePathMatrix,
     71                           bool pathIsMutable) SK_OVERRIDE;
     72     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
     73                             const SkIRect* srcRectOrNull,
     74                             const SkMatrix&, const SkPaint&) SK_OVERRIDE;
     75     virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
     76                                 const SkRect* srcOrNull, const SkRect& dst,
     77                                 const SkPaint& paint) SK_OVERRIDE;
     78     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
     79                             int x, int y, const SkPaint& paint);
     80     virtual void drawText(const SkDraw&, const void* text, size_t len,
     81                           SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
     82     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
     83                              const SkScalar pos[], SkScalar constY,
     84                              int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
     85     virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
     86                                 const SkPath& path, const SkMatrix* matrix,
     87                                 const SkPaint&) SK_OVERRIDE;
     88     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
     89                               const SkPoint verts[], const SkPoint texs[],
     90                               const SkColor colors[], SkXfermode* xmode,
     91                               const uint16_t indices[], int indexCount,
     92                               const SkPaint&) SK_OVERRIDE;
     93     virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
     94                             const SkPaint&) SK_OVERRIDE;
     95     virtual bool filterTextFlags(const SkPaint&, TextFlags*) SK_OVERRIDE;
     96 
     97     virtual void flush();
     98 
     99     virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE;
    100     virtual void onDetachFromCanvas() SK_OVERRIDE;
    101 
    102     /**
    103      * Make's this device's rendertarget current in the underlying 3D API.
    104      * Also implicitly flushes.
    105      */
    106     virtual void makeRenderTargetCurrent();
    107 
    108     virtual bool canHandleImageFilter(SkImageFilter*) SK_OVERRIDE;
    109     virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&,
    110                              SkBitmap*, SkIPoint*) SK_OVERRIDE;
    111 
    112     class SkAutoCachedTexture; // used internally
    113 
    114 protected:
    115     // overrides from SkDevice
    116     virtual bool onReadPixels(const SkBitmap& bitmap,
    117                               int x, int y,
    118                               SkCanvas::Config8888 config8888) SK_OVERRIDE;
    119 
    120 private:
    121     GrContext*      fContext;
    122 
    123     GrSkDrawProcs*  fDrawProcs;
    124 
    125     GrClipData      fClipData;
    126 
    127     // state for our render-target
    128     GrRenderTarget*     fRenderTarget;
    129     bool                fNeedClear;
    130 
    131     // called from rt and tex cons
    132     void initFromRenderTarget(GrContext*, GrRenderTarget*, bool cached);
    133 
    134     // used by createCompatibleDevice
    135     SkGpuDevice(GrContext*, GrTexture* texture, bool needClear);
    136 
    137     // override from SkDevice
    138     virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
    139                                                int width, int height,
    140                                                bool isOpaque,
    141                                                Usage usage) SK_OVERRIDE;
    142 
    143     SkDrawProcs* initDrawForText(GrTextContext*);
    144     bool bindDeviceAsTexture(GrPaint* paint);
    145 
    146     // sets the render target, clip, and matrix on GrContext. Use forceIdenity to override
    147     // SkDraw's matrix and draw in device coords.
    148     void prepareDraw(const SkDraw&, bool forceIdentity);
    149 
    150     /**
    151      * Implementation for both drawBitmap and drawBitmapRect.
    152      */
    153     void drawBitmapCommon(const SkDraw&,
    154                           const SkBitmap& bitmap,
    155                           const SkRect* srcRectPtr,
    156                           const SkMatrix&,
    157                           const SkPaint&);
    158 
    159     /**
    160      * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
    161      * matrix has already been set on GrContext
    162      */
    163     bool shouldTileBitmap(const SkBitmap& bitmap,
    164                           const GrTextureParams& sampler,
    165                           const SkRect* srcRectPtr) const;
    166     void internalDrawBitmap(const SkBitmap&,
    167                             const SkRect&,
    168                             const SkMatrix&,
    169                             const GrTextureParams& params,
    170                             GrPaint* grPaint);
    171     void drawTiledBitmap(const SkBitmap& bitmap,
    172                          const SkRect& srcRect,
    173                          const SkMatrix& m,
    174                          const GrTextureParams& params,
    175                          GrPaint* grPaint);
    176 
    177     /**
    178      * Returns non-initialized instance.
    179      */
    180     GrTextContext* getTextContext();
    181 
    182     typedef SkDevice INHERITED;
    183 };
    184 
    185 #endif
    186