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     /**
     32      * Creates an SkGpuDevice from a GrSurface. This will fail if the surface is not a render
     33      * target. The caller owns a ref on the returned device.
     34      */
     35     static SkGpuDevice* Create(GrSurface* surface);
     36 
     37     /**
     38      *  New device that will create an offscreen renderTarget based on the
     39      *  config, width, height, and sampleCount. The device's storage will not
     40      *  count against the GrContext's texture cache budget. The device's pixels
     41      *  will be uninitialized. TODO: This can fail, replace with a factory function.
     42      */
     43     SkGpuDevice(GrContext*, SkBitmap::Config, int width, int height, int sampleCount = 0);
     44 
     45     /**
     46      *  New device that will render to the specified renderTarget.
     47      *  DEPRECATED: Use Create(surface)
     48      */
     49     SkGpuDevice(GrContext*, GrRenderTarget*);
     50 
     51     /**
     52      *  New device that will render to the texture (as a rendertarget).
     53      *  The GrTexture's asRenderTarget() must be non-NULL or device will not
     54      *  function.
     55      *  DEPRECATED: Use Create(surface)
     56      */
     57     SkGpuDevice(GrContext*, GrTexture*);
     58 
     59     virtual ~SkGpuDevice();
     60 
     61     GrContext* context() const { return fContext; }
     62 
     63     virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
     64 
     65     // overrides from SkDevice
     66 
     67     virtual void clear(SkColor color) SK_OVERRIDE;
     68     virtual void writePixels(const SkBitmap& bitmap, int x, int y,
     69                              SkCanvas::Config8888 config8888) SK_OVERRIDE;
     70 
     71     virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
     72     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
     73                             const SkPoint[], const SkPaint& paint) SK_OVERRIDE;
     74     virtual void drawRect(const SkDraw&, const SkRect& r,
     75                           const SkPaint& paint) SK_OVERRIDE;
     76     virtual void drawRRect(const SkDraw&, const SkRRect& r,
     77                            const SkPaint& paint) SK_OVERRIDE;
     78     virtual void drawOval(const SkDraw&, const SkRect& oval,
     79                           const SkPaint& paint) SK_OVERRIDE;
     80     virtual void drawPath(const SkDraw&, const SkPath& path,
     81                           const SkPaint& paint, const SkMatrix* prePathMatrix,
     82                           bool pathIsMutable) SK_OVERRIDE;
     83     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
     84                             const SkMatrix&, const SkPaint&) SK_OVERRIDE;
     85     virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
     86                                 const SkRect* srcOrNull, const SkRect& dst,
     87                                 const SkPaint& paint) 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&, SkDevice*, int x, int y,
    104                             const SkPaint&) SK_OVERRIDE;
    105     virtual bool filterTextFlags(const SkPaint&, TextFlags*) SK_OVERRIDE;
    106 
    107     virtual void flush();
    108 
    109     virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE;
    110     virtual void onDetachFromCanvas() SK_OVERRIDE;
    111 
    112     /**
    113      * Make's this device's rendertarget current in the underlying 3D API.
    114      * Also implicitly flushes.
    115      */
    116     virtual void makeRenderTargetCurrent();
    117 
    118     virtual bool canHandleImageFilter(SkImageFilter*) SK_OVERRIDE;
    119     virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&,
    120                              SkBitmap*, SkIPoint*) SK_OVERRIDE;
    121 
    122     class SkAutoCachedTexture; // used internally
    123 
    124 protected:
    125     // overrides from SkDevice
    126     virtual bool onReadPixels(const SkBitmap& bitmap,
    127                               int x, int y,
    128                               SkCanvas::Config8888 config8888) SK_OVERRIDE;
    129 
    130 private:
    131     GrContext*      fContext;
    132 
    133     GrSkDrawProcs*  fDrawProcs;
    134 
    135     GrClipData      fClipData;
    136 
    137     // state for our render-target
    138     GrRenderTarget*     fRenderTarget;
    139     bool                fNeedClear;
    140 
    141     // called from rt and tex cons
    142     void initFromRenderTarget(GrContext*, GrRenderTarget*, bool cached);
    143 
    144     // used by createCompatibleDevice
    145     SkGpuDevice(GrContext*, GrTexture* texture, bool needClear);
    146 
    147     // override from SkDevice
    148     virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
    149                                                int width, int height,
    150                                                bool isOpaque,
    151                                                Usage usage) SK_OVERRIDE;
    152 
    153     SkDrawProcs* initDrawForText(GrTextContext*);
    154 
    155     // sets the render target, clip, and matrix on GrContext. Use forceIdenity to override
    156     // SkDraw's matrix and draw in device coords.
    157     void prepareDraw(const SkDraw&, bool forceIdentity);
    158 
    159     /**
    160      * Implementation for both drawBitmap and drawBitmapRect.
    161      */
    162     void drawBitmapCommon(const SkDraw&,
    163                           const SkBitmap& bitmap,
    164                           const SkRect* srcRectPtr,
    165                           const SkMatrix&,
    166                           const SkPaint&);
    167 
    168     /**
    169      * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
    170      * matrix has already been set on GrContext
    171      */
    172     bool shouldTileBitmap(const SkBitmap& bitmap,
    173                           const GrTextureParams& sampler,
    174                           const SkRect* srcRectPtr) const;
    175     void internalDrawBitmap(const SkBitmap&,
    176                             const SkRect&,
    177                             const SkMatrix&,
    178                             const GrTextureParams& params,
    179                             const SkPaint& paint);
    180     void drawTiledBitmap(const SkBitmap& bitmap,
    181                          const SkRect& srcRect,
    182                          const SkMatrix& m,
    183                          const GrTextureParams& params,
    184                          const SkPaint& paint);
    185 
    186     /**
    187      * Returns non-initialized instance.
    188      */
    189     GrTextContext* getTextContext();
    190 
    191     typedef SkDevice INHERITED;
    192 };
    193 
    194 #endif
    195