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 "SkBitmapDevice.h"
     17 #include "SkRegion.h"
     18 #include "GrContext.h"
     19 
     20 struct SkDrawProcs;
     21 struct GrSkDrawProcs;
     22 class GrTextContext;
     23 
     24 /**
     25  *  Subclass of SkBitmapDevice, which directs all drawing to the GrGpu owned by the
     26  *  canvas.
     27  */
     28 class SK_API SkGpuDevice : public SkBitmapDevice {
     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      *  DEPRECATED -- need to make this private, call Create(surface)
     47      *  New device that will render to the specified renderTarget.
     48      */
     49     SkGpuDevice(GrContext*, GrRenderTarget*);
     50 
     51     /**
     52      *  DEPRECATED -- need to make this private, call Create(surface)
     53      *  New device that will render to the texture (as a rendertarget).
     54      *  The GrTexture's asRenderTarget() must be non-NULL or device will not
     55      *  function.
     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 SkBaseDevice
     66     virtual uint32_t getDeviceCapabilities() SK_OVERRIDE {
     67         return 0;
     68     }
     69     virtual int width() const SK_OVERRIDE {
     70         return NULL == fRenderTarget ? 0 : fRenderTarget->width();
     71     }
     72     virtual int height() const SK_OVERRIDE {
     73         return NULL == fRenderTarget ? 0 : fRenderTarget->height();
     74     }
     75     virtual bool isOpaque() const SK_OVERRIDE {
     76         return NULL == fRenderTarget ? false
     77                                      : kRGB_565_GrPixelConfig == fRenderTarget->config();
     78     }
     79     virtual SkBitmap::Config config() const SK_OVERRIDE;
     80 
     81     virtual void clear(SkColor color) SK_OVERRIDE;
     82     virtual void writePixels(const SkBitmap& bitmap, int x, int y,
     83                              SkCanvas::Config8888 config8888) SK_OVERRIDE;
     84 
     85     virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
     86     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
     87                             const SkPoint[], const SkPaint& paint) SK_OVERRIDE;
     88     virtual void drawRect(const SkDraw&, const SkRect& r,
     89                           const SkPaint& paint) SK_OVERRIDE;
     90     virtual void drawRRect(const SkDraw&, const SkRRect& r,
     91                            const SkPaint& paint) SK_OVERRIDE;
     92     virtual void drawOval(const SkDraw&, const SkRect& oval,
     93                           const SkPaint& paint) SK_OVERRIDE;
     94     virtual void drawPath(const SkDraw&, const SkPath& path,
     95                           const SkPaint& paint, const SkMatrix* prePathMatrix,
     96                           bool pathIsMutable) SK_OVERRIDE;
     97     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
     98                             const SkMatrix&, const SkPaint&) SK_OVERRIDE;
     99     virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
    100                                 const SkRect* srcOrNull, const SkRect& dst,
    101                                 const SkPaint& paint,
    102                                 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
    103     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
    104                             int x, int y, const SkPaint& paint);
    105     virtual void drawText(const SkDraw&, const void* text, size_t len,
    106                           SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
    107     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
    108                              const SkScalar pos[], SkScalar constY,
    109                              int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
    110     virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
    111                                 const SkPath& path, const SkMatrix* matrix,
    112                                 const SkPaint&) SK_OVERRIDE;
    113     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
    114                               const SkPoint verts[], const SkPoint texs[],
    115                               const SkColor colors[], SkXfermode* xmode,
    116                               const uint16_t indices[], int indexCount,
    117                               const SkPaint&) SK_OVERRIDE;
    118     virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
    119                             const SkPaint&) SK_OVERRIDE;
    120     virtual bool filterTextFlags(const SkPaint&, TextFlags*) SK_OVERRIDE;
    121 
    122     virtual void flush() SK_OVERRIDE;
    123 
    124     virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE;
    125     virtual void onDetachFromCanvas() SK_OVERRIDE;
    126 
    127     /**
    128      * Make's this device's rendertarget current in the underlying 3D API.
    129      * Also implicitly flushes.
    130      */
    131     virtual void makeRenderTargetCurrent();
    132 
    133     virtual bool canHandleImageFilter(SkImageFilter*) SK_OVERRIDE;
    134     virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&,
    135                              SkBitmap*, SkIPoint*) SK_OVERRIDE;
    136 
    137     class SkAutoCachedTexture; // used internally
    138 
    139 protected:
    140     // overrides from SkBaseDevice
    141     virtual bool onReadPixels(const SkBitmap& bitmap,
    142                               int x, int y,
    143                               SkCanvas::Config8888 config8888) SK_OVERRIDE;
    144 
    145 private:
    146     GrContext*      fContext;
    147 
    148     GrSkDrawProcs*  fDrawProcs;
    149 
    150     GrClipData      fClipData;
    151 
    152     // state for our render-target
    153     GrRenderTarget*     fRenderTarget;
    154     bool                fNeedClear;
    155 
    156     // called from rt and tex cons
    157     void initFromRenderTarget(GrContext*, GrRenderTarget*, bool cached);
    158 
    159     // used by createCompatibleDevice
    160     SkGpuDevice(GrContext*, GrTexture* texture, bool needClear);
    161 
    162     // override from SkBaseDevice
    163     virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config,
    164                                                    int width, int height,
    165                                                    bool isOpaque,
    166                                                    Usage usage) SK_OVERRIDE;
    167 
    168     SkDrawProcs* initDrawForText(GrTextContext*);
    169 
    170     // sets the render target, clip, and matrix on GrContext. Use forceIdenity to override
    171     // SkDraw's matrix and draw in device coords.
    172     void prepareDraw(const SkDraw&, bool forceIdentity);
    173 
    174     /**
    175      * Implementation for both drawBitmap and drawBitmapRect.
    176      */
    177     void drawBitmapCommon(const SkDraw&,
    178                           const SkBitmap& bitmap,
    179                           const SkRect* srcRectPtr,
    180                           const SkMatrix&,
    181                           const SkPaint&,
    182                           SkCanvas::DrawBitmapRectFlags flags);
    183 
    184     /**
    185      * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
    186      * matrix, clip, and the device's render target has already been set on GrContext.
    187      */
    188 
    189     // The tileSize and clippedSrcRect will be valid only if true is returned.
    190     bool shouldTileBitmap(const SkBitmap& bitmap,
    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 SkRect&,
    198                             const GrTextureParams& params,
    199                             const SkPaint& paint,
    200                             SkCanvas::DrawBitmapRectFlags flags,
    201                             bool bicubic);
    202     void drawTiledBitmap(const SkBitmap& bitmap,
    203                          const SkRect& srcRect,
    204                          const SkIRect& clippedSrcRect,
    205                          const GrTextureParams& params,
    206                          const SkPaint& paint,
    207                          SkCanvas::DrawBitmapRectFlags flags,
    208                          int tileSize,
    209                          bool bicubic);
    210 
    211     /**
    212      * Returns non-initialized instance.
    213      */
    214     GrTextContext* getTextContext();
    215 
    216     typedef SkBitmapDevice INHERITED;
    217 };
    218 
    219 #endif
    220