reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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" |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 22 | #include "SkBitmap.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | #include "SkDevice.h" |
| 24 | #include "SkRegion.h" |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 25 | #include "GrContext.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | |
| 27 | struct SkDrawProcs; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | struct GrSkDrawProcs; |
| 29 | class GrTextContext; |
| 30 | |
| 31 | /** |
| 32 | * Subclass of SkDevice, which directs all drawing to the GrGpu owned by the |
| 33 | * canvas. |
| 34 | */ |
bsalomon@google.com | 9182610 | 2011-03-21 19:51:57 +0000 | [diff] [blame] | 35 | class SK_API SkGpuDevice : public SkDevice { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | public: |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 37 | /** |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 38 | * New device that will create an offscreen renderTarget based on the |
| 39 | * config, width, height. |
| 40 | * |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 41 | * usage is a special flag that should only be set by SkCanvas |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 42 | * internally. |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 43 | */ |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 44 | SkGpuDevice(GrContext*, SkBitmap::Config, |
| 45 | int width, int height, |
| 46 | SkDevice::Usage usage = SkDevice::kGeneral_Usage); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * New device that will render to the specified renderTarget. |
| 50 | */ |
| 51 | SkGpuDevice(GrContext*, GrRenderTarget*); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 52 | |
| 53 | /** |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 54 | * New device that will render to the texture (as a rendertarget). |
| 55 | * The GrTexture's asRenderTarget() must be non-NULL or device will not |
| 56 | * function. |
| 57 | */ |
| 58 | SkGpuDevice(GrContext*, GrTexture*); |
| 59 | |
| 60 | /** |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 61 | * Magic value that can be passed to constructor. Causes |
| 62 | * the device to infer rendertarget from underlying 3D API (e.g. GL or D3D). |
| 63 | * This isn't a valid pointer, don't attempt to dereference. |
| 64 | */ |
| 65 | static GrRenderTarget* Current3DApiRenderTarget(); |
| 66 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 67 | virtual ~SkGpuDevice(); |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 68 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 69 | GrContext* context() const { return fContext; } |
| 70 | |
| 71 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 72 | * Override from SkGpuDevice, so we can set our FBO to be the render target |
| 73 | * The canvas parameter must be a SkGpuCanvas |
| 74 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 75 | virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&, |
| 76 | const SkClipStack& clipStack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 77 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 78 | virtual SkGpuRenderTarget* accessRenderTarget() { |
| 79 | return (SkGpuRenderTarget*)fRenderTarget; |
| 80 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 81 | |
| 82 | // overrides from SkDevice |
| 83 | |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 84 | virtual void clear(SkColor color); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 85 | virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap); |
| 86 | virtual void writePixels(const SkBitmap& bitmap, int x, int y); |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 87 | |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 88 | virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip, |
| 89 | const SkClipStack&); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 90 | |
| 91 | virtual void drawPaint(const SkDraw&, const SkPaint& paint); |
| 92 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, |
| 93 | const SkPoint[], const SkPaint& paint); |
| 94 | virtual void drawRect(const SkDraw&, const SkRect& r, |
| 95 | const SkPaint& paint); |
| 96 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| 97 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 98 | bool pathIsMutable); |
| 99 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
| 100 | const SkIRect* srcRectOrNull, |
| 101 | const SkMatrix& matrix, const SkPaint& paint); |
| 102 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 103 | int x, int y, const SkPaint& paint); |
| 104 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 105 | SkScalar x, SkScalar y, const SkPaint& paint); |
| 106 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 107 | const SkScalar pos[], SkScalar constY, |
| 108 | int scalarsPerPos, const SkPaint& paint); |
| 109 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 110 | const SkPath& path, const SkMatrix* matrix, |
| 111 | const SkPaint& paint); |
| 112 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, |
| 113 | const SkPoint verts[], const SkPoint texs[], |
| 114 | const SkColor colors[], SkXfermode* xmode, |
| 115 | const uint16_t indices[], int indexCount, |
| 116 | const SkPaint& paint); |
| 117 | virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, |
| 118 | const SkPaint&); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 119 | virtual bool filterTextFlags(const SkPaint& paint, TextFlags*); |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 120 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 121 | virtual void flush() { fContext->flush(false); } |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | * Make's this device's rendertarget current in the underlying 3D API. |
| 125 | * Also implicitly flushes. |
| 126 | */ |
| 127 | virtual void makeRenderTargetCurrent(); |
| 128 | |
| 129 | protected: |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 130 | typedef GrContext::TextureCacheEntry TexCache; |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 131 | enum TexType { |
| 132 | kBitmap_TexType, |
| 133 | kDeviceRenderTarget_TexType, |
| 134 | kSaveLayerDeviceRenderTarget_TexType |
| 135 | }; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 136 | TexCache lockCachedTexture(const SkBitmap& bitmap, |
| 137 | const GrSamplerState& sampler, |
| 138 | TexType type = kBitmap_TexType); |
| 139 | void unlockCachedTexture(TexCache); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 140 | |
| 141 | class SkAutoCachedTexture { |
| 142 | public: |
| 143 | SkAutoCachedTexture(); |
| 144 | SkAutoCachedTexture(SkGpuDevice* device, |
| 145 | const SkBitmap& bitmap, |
| 146 | const GrSamplerState& sampler, |
| 147 | GrTexture** texture); |
| 148 | ~SkAutoCachedTexture(); |
| 149 | |
| 150 | GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&); |
| 151 | |
| 152 | private: |
| 153 | SkGpuDevice* fDevice; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 154 | TexCache fTex; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 155 | }; |
| 156 | friend class SkAutoTexCache; |
| 157 | |
| 158 | private: |
| 159 | GrContext* fContext; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 160 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 161 | GrSkDrawProcs* fDrawProcs; |
| 162 | |
| 163 | // state for our offscreen render-target |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 164 | TexCache fCache; |
| 165 | GrTexture* fTexture; |
| 166 | GrRenderTarget* fRenderTarget; |
| 167 | bool fNeedClear; |
| 168 | bool fNeedPrepareRenderTarget; |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 169 | |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 170 | // called from rt and tex cons |
| 171 | void initFromRenderTarget(GrContext*, GrRenderTarget*); |
| 172 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 173 | // doesn't set the texture/sampler/matrix state |
| 174 | // caller needs to null out GrPaint's texture if |
| 175 | // non-textured drawing is desired. |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 176 | // Set constantColor to true if a constant color |
| 177 | // will be used. This is an optimization, and can |
| 178 | // always be set to false. constantColor should |
| 179 | // never be true if justAlpha is true. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 180 | bool skPaint2GrPaintNoShader(const SkPaint& skPaint, |
| 181 | bool justAlpha, |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 182 | GrPaint* grPaint, |
| 183 | bool constantColor); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 184 | |
| 185 | // uses the SkShader to setup paint, act used to |
| 186 | // hold lock on cached texture and free it when |
| 187 | // destroyed. |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 188 | // If there is no shader, constantColor will |
| 189 | // be passed to skPaint2GrPaintNoShader. Otherwise |
| 190 | // it is ignored. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 191 | bool skPaint2GrPaintShader(const SkPaint& skPaint, |
| 192 | SkAutoCachedTexture* act, |
| 193 | const SkMatrix& ctm, |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 194 | GrPaint* grPaint, |
| 195 | bool constantColor); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 196 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 197 | // override from SkDevice |
| 198 | virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config, |
| 199 | int width, int height, |
| 200 | bool isOpaque, |
| 201 | Usage usage); |
| 202 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 203 | SkDrawProcs* initDrawForText(GrTextContext*); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 204 | bool bindDeviceAsTexture(GrPaint* paint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 205 | |
| 206 | void prepareRenderTarget(const SkDraw&); |
| 207 | void internalDrawBitmap(const SkDraw&, const SkBitmap&, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 208 | const SkIRect&, const SkMatrix&, GrPaint* grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 209 | |
| 210 | typedef SkDevice INHERITED; |
| 211 | }; |
| 212 | |
| 213 | #endif |
| 214 | |