OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file | 5 * found in the LICENSE file |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkSpecialImage.h" | 9 #include "SkSpecialImage.h" |
10 #include "SkSpecialSurface.h" | 10 #include "SkSpecialSurface.h" |
11 | 11 |
12 /////////////////////////////////////////////////////////////////////////////// | 12 /////////////////////////////////////////////////////////////////////////////// |
13 class SkSpecialImage_Base : public SkSpecialImage { | 13 class SkSpecialImage_Base : public SkSpecialImage { |
14 public: | 14 public: |
15 SkSpecialImage_Base(const SkIRect& subset) : INHERITED(subset) { } | 15 SkSpecialImage_Base(SkImageFilter::Proxy* proxy, const SkIRect& subset, uint 32_t uniqueID) |
16 : INHERITED(proxy, subset, uniqueID) { | |
17 } | |
16 virtual ~SkSpecialImage_Base() { } | 18 virtual ~SkSpecialImage_Base() { } |
17 | 19 |
18 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0; | 20 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0; |
19 | 21 |
20 virtual bool onPeekPixels(SkPixmap*) const { return false; } | 22 virtual bool onPeekPixels(SkPixmap*) const { return false; } |
21 | 23 |
22 virtual GrTexture* onPeekTexture() const { return nullptr; } | 24 virtual GrTexture* onPeekTexture() const { return nullptr; } |
23 | 25 |
26 // Delete this entry point ASAP | |
Stephen White
2016/02/17 21:44:02
Log a bug, and reference it here? (see below)
robertphillips
2016/02/18 13:18:39
Done.
| |
27 virtual bool getBitmap(SkBitmap* result) const = 0; | |
28 | |
24 virtual SkSpecialSurface* onNewSurface(const SkImageInfo& info) const { retu rn nullptr; } | 29 virtual SkSpecialSurface* onNewSurface(const SkImageInfo& info) const { retu rn nullptr; } |
25 | 30 |
26 private: | 31 private: |
27 typedef SkSpecialImage INHERITED; | 32 typedef SkSpecialImage INHERITED; |
28 }; | 33 }; |
29 | 34 |
30 /////////////////////////////////////////////////////////////////////////////// | 35 /////////////////////////////////////////////////////////////////////////////// |
31 static inline const SkSpecialImage_Base* as_IB(const SkSpecialImage* image) { | 36 static inline const SkSpecialImage_Base* as_IB(const SkSpecialImage* image) { |
32 return static_cast<const SkSpecialImage_Base*>(image); | 37 return static_cast<const SkSpecialImage_Base*>(image); |
33 } | 38 } |
34 | 39 |
35 void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPain t* paint) const { | 40 void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPain t* paint) const { |
36 return as_IB(this)->onDraw(canvas, x, y, paint); | 41 return as_IB(this)->onDraw(canvas, x, y, paint); |
37 } | 42 } |
38 | 43 |
39 bool SkSpecialImage::peekPixels(SkPixmap* pixmap) const { | 44 bool SkSpecialImage::peekPixels(SkPixmap* pixmap) const { |
40 return as_IB(this)->onPeekPixels(pixmap); | 45 return as_IB(this)->onPeekPixels(pixmap); |
41 } | 46 } |
42 | 47 |
43 GrTexture* SkSpecialImage::peekTexture() const { | 48 GrTexture* SkSpecialImage::peekTexture() const { |
44 return as_IB(this)->onPeekTexture(); | 49 return as_IB(this)->onPeekTexture(); |
45 } | 50 } |
46 | 51 |
47 SkSpecialSurface* SkSpecialImage::newSurface(const SkImageInfo& info) const { | 52 SkSpecialSurface* SkSpecialImage::newSurface(const SkImageInfo& info) const { |
48 return as_IB(this)->onNewSurface(info); | 53 return as_IB(this)->onNewSurface(info); |
49 } | 54 } |
50 | 55 |
56 #if SK_SUPPORT_GPU | |
57 #include "SkGr.h" | |
58 #include "SkGrPixelRef.h" | |
59 #endif | |
60 | |
61 SkSpecialImage* SkSpecialImage::internal_fromBM(SkImageFilter::Proxy* proxy, | |
62 const SkBitmap& src) { | |
63 // Need to test offset case! | |
Stephen White
2016/02/17 21:44:02
You mean an offset in the pixelref? Log a bug mayb
robertphillips
2016/02/18 13:18:39
Right - Done.
| |
64 if (src.getTexture()) { | |
65 return SkSpecialImage::NewFromGpu(proxy, | |
66 src.bounds(), | |
67 src.getGenerationID(), | |
68 src.getTexture()); | |
69 } | |
70 | |
71 return SkSpecialImage::NewFromRaster(proxy, src.bounds(), src); | |
72 } | |
73 | |
74 bool SkSpecialImage::internal_getBM(SkBitmap* result) { | |
75 const SkSpecialImage_Base* ib = as_IB(this); | |
76 | |
77 // TODO: need to test offset case! | |
78 return ib->getBitmap(result); | |
79 } | |
80 | |
81 #include "SkBitmapDevice.h" | |
82 #if SK_SUPPORT_GPU | |
83 #include "SkGpuDevice.h" | |
Stephen White
2016/02/17 21:44:02
Is this still needed? If not, remove it. If so, co
robertphillips
2016/02/18 13:18:39
Done.
| |
84 #endif | |
85 | |
86 SkImageFilter::Proxy* SkSpecialImage::internal_getProxy() { | |
87 SkASSERT(fProxy); | |
88 return fProxy; | |
89 } | |
90 | |
51 /////////////////////////////////////////////////////////////////////////////// | 91 /////////////////////////////////////////////////////////////////////////////// |
52 #include "SkImage.h" | 92 #include "SkImage.h" |
53 #if SK_SUPPORT_GPU | 93 #if SK_SUPPORT_GPU |
54 #include "SkGr.h" | |
55 #include "SkGrPriv.h" | 94 #include "SkGrPriv.h" |
56 #endif | 95 #endif |
57 | 96 |
58 class SkSpecialImage_Image : public SkSpecialImage_Base { | 97 class SkSpecialImage_Image : public SkSpecialImage_Base { |
59 public: | 98 public: |
60 SkSpecialImage_Image(const SkIRect& subset, const SkImage* image) | 99 SkSpecialImage_Image(SkImageFilter::Proxy* proxy, const SkIRect& subset, con st SkImage* image) |
61 : INHERITED(subset) | 100 : INHERITED(proxy, subset, image->uniqueID()) |
62 , fImage(SkRef(image)) { | 101 , fImage(SkRef(image)) { |
63 } | 102 } |
64 | 103 |
65 ~SkSpecialImage_Image() override { } | 104 ~SkSpecialImage_Image() override { } |
66 | 105 |
106 bool isOpaque() const override { return fImage->isOpaque(); } | |
107 | |
108 size_t getSize() const override { | |
109 if (fImage->getTexture()) { | |
110 return fImage->getTexture()->gpuMemorySize(); | |
111 } else { | |
112 SkImageInfo info; | |
113 size_t rowBytes; | |
114 | |
115 if (fImage->peekPixels(&info, &rowBytes)) { | |
116 return info.height() * rowBytes; | |
117 } | |
118 } | |
119 return 0; | |
120 } | |
121 | |
67 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { | 122 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
68 SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset ().height()); | 123 SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset ().height()); |
69 | 124 |
70 canvas->drawImageRect(fImage, this->subset(), | 125 canvas->drawImageRect(fImage, this->subset(), |
71 dst, paint, SkCanvas::kStrict_SrcRectConstraint); | 126 dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
72 } | 127 } |
73 | 128 |
74 bool onPeekPixels(SkPixmap* pixmap) const override { | 129 bool onPeekPixels(SkPixmap* pixmap) const override { |
75 return fImage->peekPixels(pixmap); | 130 return fImage->peekPixels(pixmap); |
76 } | 131 } |
77 | 132 |
78 GrTexture* onPeekTexture() const override { return fImage->getTexture(); } | 133 GrTexture* onPeekTexture() const override { return fImage->getTexture(); } |
79 | 134 |
135 bool getBitmap(SkBitmap* result) const override { | |
136 return false; | |
137 } | |
138 | |
80 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | 139 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
81 #if SK_SUPPORT_GPU | 140 #if SK_SUPPORT_GPU |
82 GrTexture* texture = fImage->getTexture(); | 141 GrTexture* texture = fImage->getTexture(); |
83 if (texture) { | 142 if (texture) { |
84 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); | 143 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); |
85 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 144 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
86 | 145 |
87 return SkSpecialSurface::NewRenderTarget(texture->getContext(), desc ); | 146 return SkSpecialSurface::NewRenderTarget(this->proxy(), texture->get Context(), desc); |
88 } | 147 } |
89 #endif | 148 #endif |
90 return SkSpecialSurface::NewRaster(info, nullptr); | 149 return SkSpecialSurface::NewRaster(this->proxy(), info, nullptr); |
91 } | 150 } |
92 | 151 |
93 private: | 152 private: |
94 SkAutoTUnref<const SkImage> fImage; | 153 SkAutoTUnref<const SkImage> fImage; |
95 | 154 |
96 typedef SkSpecialImage_Base INHERITED; | 155 typedef SkSpecialImage_Base INHERITED; |
97 }; | 156 }; |
98 | 157 |
99 #ifdef SK_DEBUG | 158 #ifdef SK_DEBUG |
100 static bool rect_fits(const SkIRect& rect, int width, int height) { | 159 static bool rect_fits(const SkIRect& rect, int width, int height) { |
101 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && | 160 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && |
102 rect.fRight >= 0 && rect.fRight <= width && | 161 rect.fRight >= 0 && rect.fRight <= width && |
103 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && | 162 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && |
104 rect.fBottom >= 0 && rect.fBottom <= height; | 163 rect.fBottom >= 0 && rect.fBottom <= height; |
105 } | 164 } |
106 #endif | 165 #endif |
107 | 166 |
108 SkSpecialImage* SkSpecialImage::NewFromImage(const SkIRect& subset, const SkImag e* image) { | 167 SkSpecialImage* SkSpecialImage::NewFromImage(const SkIRect& subset, const SkImag e* image) { |
109 SkASSERT(rect_fits(subset, image->width(), image->height())); | 168 SkASSERT(rect_fits(subset, image->width(), image->height())); |
110 return new SkSpecialImage_Image(subset, image); | 169 return new SkSpecialImage_Image(nullptr, subset, image); |
111 } | 170 } |
112 | 171 |
113 /////////////////////////////////////////////////////////////////////////////// | 172 /////////////////////////////////////////////////////////////////////////////// |
114 #include "SkBitmap.h" | 173 #include "SkBitmap.h" |
115 #include "SkImageInfo.h" | 174 #include "SkImageInfo.h" |
116 #include "SkPixelRef.h" | 175 #include "SkPixelRef.h" |
117 | 176 |
118 class SkSpecialImage_Raster : public SkSpecialImage_Base { | 177 class SkSpecialImage_Raster : public SkSpecialImage_Base { |
119 public: | 178 public: |
120 SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm) | 179 SkSpecialImage_Raster(SkImageFilter::Proxy* proxy, const SkIRect& subset, co nst SkBitmap& bm) |
121 : INHERITED(subset) | 180 : INHERITED(proxy, subset, bm.getGenerationID()) |
122 , fBitmap(bm) { | 181 , fBitmap(bm) { |
123 if (bm.pixelRef()->isPreLocked()) { | 182 if (bm.pixelRef()->isPreLocked()) { |
124 // we only preemptively lock if there is no chance of triggering som ething expensive | 183 // we only preemptively lock if there is no chance of triggering som ething expensive |
125 // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. | 184 // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. |
126 fBitmap.lockPixels(); | 185 fBitmap.lockPixels(); |
127 } | 186 } |
128 } | 187 } |
129 | 188 |
130 ~SkSpecialImage_Raster() override { } | 189 ~SkSpecialImage_Raster() override { } |
131 | 190 |
191 bool isOpaque() const override { return fBitmap.isOpaque(); } | |
192 | |
193 size_t getSize() const override { return fBitmap.getSize(); } | |
194 | |
132 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { | 195 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
133 SkRect dst = SkRect::MakeXYWH(x, y, | 196 SkRect dst = SkRect::MakeXYWH(x, y, |
134 this->subset().width(), this->subset().hei ght()); | 197 this->subset().width(), this->subset().hei ght()); |
135 | 198 |
136 canvas->drawBitmapRect(fBitmap, this->subset(), | 199 canvas->drawBitmapRect(fBitmap, this->subset(), |
137 dst, paint, SkCanvas::kStrict_SrcRectConstraint); | 200 dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
138 } | 201 } |
139 | 202 |
140 bool onPeekPixels(SkPixmap* pixmap) const override { | 203 bool onPeekPixels(SkPixmap* pixmap) const override { |
141 const SkImageInfo info = fBitmap.info(); | 204 const SkImageInfo info = fBitmap.info(); |
142 if ((kUnknown_SkColorType == info.colorType()) || !fBitmap.getPixels()) { | 205 if ((kUnknown_SkColorType == info.colorType()) || !fBitmap.getPixels()) { |
143 return false; | 206 return false; |
144 } | 207 } |
145 const void* pixels = fBitmap.getPixels(); | 208 const void* pixels = fBitmap.getPixels(); |
146 if (pixels) { | 209 if (pixels) { |
147 if (pixmap) { | 210 if (pixmap) { |
148 pixmap->reset(info, pixels, fBitmap.rowBytes()); | 211 pixmap->reset(info, pixels, fBitmap.rowBytes()); |
149 } | 212 } |
150 return true; | 213 return true; |
151 } | 214 } |
152 return false; | 215 return false; |
153 } | 216 } |
154 | 217 |
218 bool getBitmap(SkBitmap* result) const override { | |
219 *result = fBitmap; | |
220 return true; | |
221 } | |
222 | |
155 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | 223 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
156 return SkSpecialSurface::NewRaster(info, nullptr); | 224 return SkSpecialSurface::NewRaster(this->proxy(), info, nullptr); |
157 } | 225 } |
158 | 226 |
159 private: | 227 private: |
160 SkBitmap fBitmap; | 228 SkBitmap fBitmap; |
161 | 229 |
162 typedef SkSpecialImage_Base INHERITED; | 230 typedef SkSpecialImage_Base INHERITED; |
163 }; | 231 }; |
164 | 232 |
165 SkSpecialImage* SkSpecialImage::NewFromRaster(const SkIRect& subset, const SkBit map& bm) { | 233 SkSpecialImage* SkSpecialImage::NewFromRaster(SkImageFilter::Proxy* proxy, |
234 const SkIRect& subset, | |
235 const SkBitmap& bm) { | |
166 SkASSERT(nullptr == bm.getTexture()); | 236 SkASSERT(nullptr == bm.getTexture()); |
167 SkASSERT(rect_fits(subset, bm.width(), bm.height())); | 237 SkASSERT(rect_fits(subset, bm.width(), bm.height())); |
168 return new SkSpecialImage_Raster(subset, bm); | 238 return new SkSpecialImage_Raster(proxy, subset, bm); |
169 } | 239 } |
170 | 240 |
171 #if SK_SUPPORT_GPU | 241 #if SK_SUPPORT_GPU |
172 /////////////////////////////////////////////////////////////////////////////// | 242 /////////////////////////////////////////////////////////////////////////////// |
173 #include "GrTexture.h" | 243 #include "GrTexture.h" |
174 | 244 |
175 class SkSpecialImage_Gpu : public SkSpecialImage_Base { | 245 class SkSpecialImage_Gpu : public SkSpecialImage_Base { |
176 public: | 246 public: |
177 SkSpecialImage_Gpu(const SkIRect& subset, GrTexture* tex) | 247 SkSpecialImage_Gpu(SkImageFilter::Proxy* proxy, const SkIRect& subset, |
178 : INHERITED(subset) | 248 uint32_t uniqueID, GrTexture* tex, SkAlphaType at) |
179 , fTexture(SkRef(tex)) { | 249 : INHERITED(proxy, subset, uniqueID) |
250 , fTexture(SkRef(tex)) | |
251 , fAlphaType(at) { | |
180 } | 252 } |
181 | 253 |
182 ~SkSpecialImage_Gpu() override { } | 254 ~SkSpecialImage_Gpu() override { } |
183 | 255 |
256 bool isOpaque() const override { | |
257 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaqu e_SkAlphaType; | |
258 } | |
259 | |
260 size_t getSize() const override { return fTexture->gpuMemorySize(); } | |
261 | |
184 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { | 262 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
185 SkRect dst = SkRect::MakeXYWH(x, y, | 263 SkRect dst = SkRect::MakeXYWH(x, y, |
186 this->subset().width(), this->subset().hei ght()); | 264 this->subset().width(), this->subset().hei ght()); |
187 | 265 |
188 SkBitmap bm; | 266 SkBitmap bm; |
189 | 267 |
190 static const bool kUnknownOpacity = false; | |
191 GrWrapTextureInBitmap(fTexture, | 268 GrWrapTextureInBitmap(fTexture, |
192 fTexture->width(), fTexture->height(), kUnknownOpa city, &bm); | 269 fTexture->width(), fTexture->height(), this->isOpa que(), &bm); |
193 | 270 |
194 canvas->drawBitmapRect(bm, this->subset(), | 271 canvas->drawBitmapRect(bm, this->subset(), |
195 dst, paint, SkCanvas::kStrict_SrcRectConstraint); | 272 dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
196 } | 273 } |
197 | 274 |
198 GrTexture* onPeekTexture() const override { return fTexture; } | 275 GrTexture* onPeekTexture() const override { return fTexture; } |
199 | 276 |
277 bool getBitmap(SkBitmap* result) const override { | |
278 const SkImageInfo info = GrMakeInfoFromTexture(fTexture, | |
279 this->width(), this->heig ht(), | |
280 this->isOpaque()); | |
281 if (!result->setInfo(info)) { | |
282 return false; | |
283 } | |
284 | |
285 result->setPixelRef(new SkGrPixelRef(info, fTexture))->unref(); | |
286 return true; | |
287 } | |
288 | |
200 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | 289 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
201 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); | 290 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); |
202 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 291 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
203 | 292 |
204 return SkSpecialSurface::NewRenderTarget(fTexture->getContext(), desc); | 293 return SkSpecialSurface::NewRenderTarget(this->proxy(), fTexture->getCon text(), desc); |
205 } | 294 } |
206 | 295 |
207 private: | 296 private: |
208 SkAutoTUnref<GrTexture> fTexture; | 297 SkAutoTUnref<GrTexture> fTexture; |
298 const SkAlphaType fAlphaType; | |
209 | 299 |
210 typedef SkSpecialImage_Base INHERITED; | 300 typedef SkSpecialImage_Base INHERITED; |
211 }; | 301 }; |
212 | 302 |
213 SkSpecialImage* SkSpecialImage::NewFromGpu(const SkIRect& subset, GrTexture* tex ) { | 303 SkSpecialImage* SkSpecialImage::NewFromGpu(SkImageFilter::Proxy* proxy, |
304 const SkIRect& subset, | |
305 uint32_t uniqueID, | |
306 GrTexture* tex, | |
307 SkAlphaType at) { | |
214 SkASSERT(rect_fits(subset, tex->width(), tex->height())); | 308 SkASSERT(rect_fits(subset, tex->width(), tex->height())); |
215 return new SkSpecialImage_Gpu(subset, tex); | 309 return new SkSpecialImage_Gpu(proxy, subset, uniqueID, tex, at); |
216 } | 310 } |
217 | 311 |
218 #else | 312 #else |
219 | 313 |
220 SkSpecialImage* SkSpecialImage::NewFromGpu(const SkIRect& subset, GrTexture* tex ) { | 314 SkSpecialImage* SkSpecialImage::NewFromGpu(SkImageFilter::Proxy* proxy, |
315 const SkIRect& subset, | |
316 uint32_t uniqueID, | |
317 GrTexture* tex, | |
318 SkAlphaType at) { | |
221 return nullptr; | 319 return nullptr; |
222 } | 320 } |
223 | 321 |
224 #endif | 322 #endif |
OLD | NEW |