Home | History | Annotate | Download | only in gpu
      1 /*
      2     Copyright 2010 Google Inc.
      3 
      4     Licensed under the Apache License, Version 2.0 (the "License");
      5     you may not use this file except in compliance with the License.
      6     You may obtain a copy of the License at
      7 
      8          http://www.apache.org/licenses/LICENSE-2.0
      9 
     10     Unless required by applicable law or agreed to in writing, software
     11     distributed under the License is distributed on an "AS IS" BASIS,
     12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13     See the License for the specific language governing permissions and
     14     limitations under the License.
     15  */
     16 
     17 
     18 #ifndef SkGpuDevice_DEFINED
     19 #define SkGpuDevice_DEFINED
     20 
     21 #include "SkGr.h"
     22 #include "SkDevice.h"
     23 #include "SkRegion.h"
     24 
     25 struct SkDrawProcs;
     26 struct GrSkDrawProcs;
     27 class GrTextContext;
     28 
     29 /**
     30  *  Subclass of SkDevice, which directs all drawing to the GrGpu owned by the
     31  *  canvas.
     32  */
     33 class SK_API SkGpuDevice : public SkDevice {
     34 public:
     35     /**
     36      * The SkGpuDevice will render to the GrRenderTarget, or if the paremeter is
     37      * null it will create its own render target and manage that target's
     38      * lifetime.
     39      */
     40     SkGpuDevice(GrContext*,
     41                 const SkBitmap& bitmap,
     42                 GrRenderTarget* renderTargetOrNull);
     43 
     44     /**
     45      * Magic value that can be passed to constructor. Causes
     46      * the device to infer rendertarget from underlying 3D API (e.g. GL or D3D).
     47      * This isn't a valid pointer, don't attempt to dereference.
     48      */
     49     static GrRenderTarget* Current3DApiRenderTarget();
     50 
     51     virtual ~SkGpuDevice();
     52 
     53     GrContext* context() const { return fContext; }
     54 
     55     /**
     56      *  If this device was built for rendering as a layer (i.e. offscreen),
     57      *  then this will return the platform-specific handle to that GPU resource.
     58      *  For example, in OpenGL, this will return the FBO's texture ID.
     59      *  If this device was not built for rendering as a layer, then 0
     60      *  is returned.
     61      */
     62     intptr_t getLayerTextureHandle() const;
     63 
     64     // call to set the clip to the specified rect
     65     void scissor(const SkIRect&);
     66 
     67     /**
     68      *  Override from SkGpuDevice, so we can set our FBO to be the render target
     69      *  The canvas parameter must be a SkGpuCanvas
     70      */
     71     virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&,
     72                            const SkClipStack& clipStack);
     73 
     74     virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; }
     75 
     76     // overrides from SkDevice
     77 
     78     virtual void clear(SkColor color);
     79     virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
     80     virtual void writePixels(const SkBitmap& bitmap, int x, int y);
     81 
     82     virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
     83                                const SkClipStack&);
     84 
     85     virtual void drawPaint(const SkDraw&, const SkPaint& paint);
     86     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
     87                             const SkPoint[], const SkPaint& paint);
     88     virtual void drawRect(const SkDraw&, const SkRect& r,
     89                           const SkPaint& paint);
     90     virtual void drawPath(const SkDraw&, const SkPath& path,
     91                           const SkPaint& paint, const SkMatrix* prePathMatrix,
     92                           bool pathIsMutable);
     93     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
     94                             const SkIRect* srcRectOrNull,
     95                             const SkMatrix& matrix, const SkPaint& paint);
     96     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
     97                             int x, int y, const SkPaint& paint);
     98     virtual void drawText(const SkDraw&, const void* text, size_t len,
     99                           SkScalar x, SkScalar y, const SkPaint& paint);
    100     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
    101                              const SkScalar pos[], SkScalar constY,
    102                              int scalarsPerPos, const SkPaint& paint);
    103     virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
    104                                 const SkPath& path, const SkMatrix* matrix,
    105                                 const SkPaint& paint);
    106     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
    107                               const SkPoint verts[], const SkPoint texs[],
    108                               const SkColor colors[], SkXfermode* xmode,
    109                               const uint16_t indices[], int indexCount,
    110                               const SkPaint& paint);
    111     virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
    112                             const SkPaint&);
    113     virtual bool filterTextFlags(const SkPaint& paint, TextFlags*);
    114 
    115     virtual void flush() { fContext->flush(false); }
    116 
    117     /**
    118      * Make's this device's rendertarget current in the underlying 3D API.
    119      * Also implicitly flushes.
    120      */
    121     virtual void makeRenderTargetCurrent();
    122 
    123 protected:
    124     // override
    125     virtual SkDeviceFactory* onNewDeviceFactory();
    126 
    127     class TexCache;
    128     TexCache* lockCachedTexture(const SkBitmap& bitmap,
    129                                 const GrSamplerState& sampler,
    130                                 GrTexture** texture,
    131                                 bool forDeviceRenderTarget = false);
    132     void unlockCachedTexture(TexCache*);
    133 
    134     class SkAutoCachedTexture {
    135     public:
    136         SkAutoCachedTexture();
    137         SkAutoCachedTexture(SkGpuDevice* device,
    138                             const SkBitmap& bitmap,
    139                             const GrSamplerState& sampler,
    140                             GrTexture** texture);
    141         ~SkAutoCachedTexture();
    142 
    143         GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&);
    144 
    145     private:
    146         SkGpuDevice*    fDevice;
    147         TexCache*       fTex;
    148     };
    149     friend class SkAutoTexCache;
    150 
    151 private:
    152     GrContext*      fContext;
    153 
    154     GrSkDrawProcs*  fDrawProcs;
    155 
    156     // state for our offscreen render-target
    157     TexCache*       fCache;
    158     GrTexture*      fTexture;
    159     GrRenderTarget* fRenderTarget;
    160     bool            fNeedClear;
    161     bool            fNeedPrepareRenderTarget;
    162 
    163     // doesn't set the texture/sampler/matrix state
    164     // caller needs to null out GrPaint's texture if
    165     // non-textured drawing is desired.
    166     // Set constantColor to true if a constant color
    167     // will be used.  This is an optimization, and can
    168     // always be set to false. constantColor should
    169     // never be true if justAlpha is true.
    170     bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
    171                                  bool justAlpha,
    172                                  GrPaint* grPaint,
    173                                  bool constantColor);
    174 
    175     // uses the SkShader to setup paint, act used to
    176     // hold lock on cached texture and free it when
    177     // destroyed.
    178     // If there is no shader, constantColor will
    179     // be passed to skPaint2GrPaintNoShader.  Otherwise
    180     // it is ignored.
    181     bool skPaint2GrPaintShader(const SkPaint& skPaint,
    182                                SkAutoCachedTexture* act,
    183                                const SkMatrix& ctm,
    184                                GrPaint* grPaint,
    185                                bool constantColor);
    186 
    187     SkDrawProcs* initDrawForText(GrTextContext*);
    188     bool bindDeviceAsTexture(GrPaint* paint);
    189 
    190     void prepareRenderTarget(const SkDraw&);
    191     void internalDrawBitmap(const SkDraw&, const SkBitmap&,
    192                             const SkIRect&, const SkMatrix&, GrPaint* grPaint);
    193 
    194     typedef SkDevice INHERITED;
    195 };
    196 
    197 #endif
    198 
    199