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 "SkImage_Base.h" | 9 #include "SkImage_Base.h" |
10 #include "SkSpecialImage.h" | 10 #include "SkSpecialImage.h" |
11 #include "SkSpecialSurface.h" | 11 #include "SkSpecialSurface.h" |
12 | 12 |
13 /////////////////////////////////////////////////////////////////////////////// | 13 /////////////////////////////////////////////////////////////////////////////// |
14 class SkSpecialImage_Base : public SkSpecialImage { | 14 class SkSpecialImage_Base : public SkSpecialImage { |
15 public: | 15 public: |
16 SkSpecialImage_Base(SkImageFilter::Proxy* proxy, const SkIRect& subset, uint
32_t uniqueID) | 16 SkSpecialImage_Base(SkImageFilter::Proxy* proxy, const SkIRect& subset, uint
32_t uniqueID) |
17 : INHERITED(proxy, subset, uniqueID) { | 17 : INHERITED(proxy, subset, uniqueID) { |
18 } | 18 } |
19 virtual ~SkSpecialImage_Base() { } | 19 virtual ~SkSpecialImage_Base() { } |
20 | 20 |
21 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const
= 0; | 21 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const
= 0; |
22 | 22 |
23 virtual bool onPeekPixels(SkPixmap*) const { return false; } | 23 virtual bool onPeekPixels(SkPixmap*) const { return false; } |
24 | 24 |
25 virtual GrTexture* onPeekTexture() const { return nullptr; } | 25 virtual GrTexture* onPeekTexture() const { return nullptr; } |
26 | 26 |
| 27 virtual bool onGetROPixels(SkBitmap*) const = 0; |
| 28 |
27 // Delete this entry point ASAP (see skbug.com/4965) | 29 // Delete this entry point ASAP (see skbug.com/4965) |
28 virtual bool getBitmap(SkBitmap* result) const = 0; | 30 virtual bool getBitmap(SkBitmap* result) const = 0; |
29 | 31 |
30 virtual SkSpecialSurface* onNewSurface(const SkImageInfo& info) const { retu
rn nullptr; } | 32 virtual SkSpecialSurface* onNewSurface(const SkImageInfo& info) const { retu
rn nullptr; } |
31 | 33 |
32 private: | 34 private: |
33 typedef SkSpecialImage INHERITED; | 35 typedef SkSpecialImage INHERITED; |
34 }; | 36 }; |
35 | 37 |
36 /////////////////////////////////////////////////////////////////////////////// | 38 /////////////////////////////////////////////////////////////////////////////// |
37 static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) { | 39 static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) { |
38 return static_cast<const SkSpecialImage_Base*>(image); | 40 return static_cast<const SkSpecialImage_Base*>(image); |
39 } | 41 } |
40 | 42 |
41 void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPain
t* paint) const { | 43 void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPain
t* paint) const { |
42 return as_SIB(this)->onDraw(canvas, x, y, paint); | 44 return as_SIB(this)->onDraw(canvas, x, y, paint); |
43 } | 45 } |
44 | 46 |
45 bool SkSpecialImage::peekPixels(SkPixmap* pixmap) const { | 47 bool SkSpecialImage::peekPixels(SkPixmap* pixmap) const { |
46 return as_SIB(this)->onPeekPixels(pixmap); | 48 return as_SIB(this)->onPeekPixels(pixmap); |
47 } | 49 } |
48 | 50 |
49 GrTexture* SkSpecialImage::peekTexture() const { | 51 GrTexture* SkSpecialImage::peekTexture() const { |
50 return as_SIB(this)->onPeekTexture(); | 52 return as_SIB(this)->onPeekTexture(); |
51 } | 53 } |
52 | 54 |
| 55 bool SkSpecialImage::getROPixels(SkBitmap* result) const { |
| 56 return as_SIB(this)->onGetROPixels(result); |
| 57 } |
| 58 |
53 SkSpecialSurface* SkSpecialImage::newSurface(const SkImageInfo& info) const { | 59 SkSpecialSurface* SkSpecialImage::newSurface(const SkImageInfo& info) const { |
54 return as_SIB(this)->onNewSurface(info); | 60 return as_SIB(this)->onNewSurface(info); |
55 } | 61 } |
56 | 62 |
57 #if SK_SUPPORT_GPU | 63 #if SK_SUPPORT_GPU |
58 #include "SkGr.h" | 64 #include "SkGr.h" |
59 #include "SkGrPixelRef.h" | 65 #include "SkGrPixelRef.h" |
60 #endif | 66 #endif |
61 | 67 |
62 SkSpecialImage* SkSpecialImage::internal_fromBM(SkImageFilter::Proxy* proxy, | 68 SkSpecialImage* SkSpecialImage::internal_fromBM(SkImageFilter::Proxy* proxy, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 bool onPeekPixels(SkPixmap* pixmap) const override { | 134 bool onPeekPixels(SkPixmap* pixmap) const override { |
129 return fImage->peekPixels(pixmap); | 135 return fImage->peekPixels(pixmap); |
130 } | 136 } |
131 | 137 |
132 GrTexture* onPeekTexture() const override { return as_IB(fImage.get())->peek
Texture(); } | 138 GrTexture* onPeekTexture() const override { return as_IB(fImage.get())->peek
Texture(); } |
133 | 139 |
134 bool getBitmap(SkBitmap* result) const override { | 140 bool getBitmap(SkBitmap* result) const override { |
135 return false; | 141 return false; |
136 } | 142 } |
137 | 143 |
| 144 bool onGetROPixels(SkBitmap* result) const override { |
| 145 return false; |
| 146 } |
| 147 |
138 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | 148 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
139 #if SK_SUPPORT_GPU | 149 #if SK_SUPPORT_GPU |
140 GrTexture* texture = as_IB(fImage.get())->peekTexture(); | 150 GrTexture* texture = as_IB(fImage.get())->peekTexture(); |
141 if (texture) { | 151 if (texture) { |
142 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); | 152 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); |
143 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 153 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
144 | 154 |
145 return SkSpecialSurface::NewRenderTarget(this->proxy(), texture->get
Context(), desc); | 155 return SkSpecialSurface::NewRenderTarget(this->proxy(), texture->get
Context(), desc); |
146 } | 156 } |
147 #endif | 157 #endif |
148 return SkSpecialSurface::NewRaster(this->proxy(), info, nullptr); | 158 return SkSpecialSurface::NewRaster(this->proxy(), info, nullptr); |
149 } | 159 } |
150 | 160 |
151 private: | 161 private: |
152 SkAutoTUnref<const SkImage> fImage; | 162 SkAutoTUnref<const SkImage> fImage; |
153 | 163 |
154 typedef SkSpecialImage_Base INHERITED; | 164 typedef SkSpecialImage_Base INHERITED; |
155 }; | 165 }; |
156 | 166 |
157 #ifdef SK_DEBUG | 167 #ifdef SK_DEBUG |
158 static bool rect_fits(const SkIRect& rect, int width, int height) { | 168 static bool rect_fits(const SkIRect& rect, int width, int height) { |
| 169 if (0 == width && 0 == height) { |
| 170 SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == r
ect.fBottom); |
| 171 return true; |
| 172 } |
| 173 |
159 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && | 174 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && |
160 rect.fRight >= 0 && rect.fRight <= width && | 175 rect.fRight >= 0 && rect.fRight <= width && |
161 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && | 176 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && |
162 rect.fBottom >= 0 && rect.fBottom <= height; | 177 rect.fBottom >= 0 && rect.fBottom <= height; |
163 } | 178 } |
164 #endif | 179 #endif |
165 | 180 |
166 SkSpecialImage* SkSpecialImage::NewFromImage(const SkIRect& subset, const SkImag
e* image) { | 181 SkSpecialImage* SkSpecialImage::NewFromImage(const SkIRect& subset, const SkImag
e* image) { |
167 SkASSERT(rect_fits(subset, image->width(), image->height())); | 182 SkASSERT(rect_fits(subset, image->width(), image->height())); |
168 return new SkSpecialImage_Image(nullptr, subset, image); | 183 return new SkSpecialImage_Image(nullptr, subset, image); |
169 } | 184 } |
170 | 185 |
171 /////////////////////////////////////////////////////////////////////////////// | 186 /////////////////////////////////////////////////////////////////////////////// |
172 #include "SkBitmap.h" | 187 #include "SkBitmap.h" |
173 #include "SkImageInfo.h" | 188 #include "SkImageInfo.h" |
174 #include "SkPixelRef.h" | 189 #include "SkPixelRef.h" |
175 | 190 |
176 class SkSpecialImage_Raster : public SkSpecialImage_Base { | 191 class SkSpecialImage_Raster : public SkSpecialImage_Base { |
177 public: | 192 public: |
178 SkSpecialImage_Raster(SkImageFilter::Proxy* proxy, const SkIRect& subset, co
nst SkBitmap& bm) | 193 SkSpecialImage_Raster(SkImageFilter::Proxy* proxy, const SkIRect& subset, co
nst SkBitmap& bm) |
179 : INHERITED(proxy, subset, bm.getGenerationID()) | 194 : INHERITED(proxy, subset, bm.getGenerationID()) |
180 , fBitmap(bm) { | 195 , fBitmap(bm) { |
181 if (bm.pixelRef()->isPreLocked()) { | 196 if (bm.pixelRef() && bm.pixelRef()->isPreLocked()) { |
182 // we only preemptively lock if there is no chance of triggering som
ething expensive | 197 // we only preemptively lock if there is no chance of triggering som
ething expensive |
183 // like a lazy decode or imagegenerator. PreLocked means it is flat
pixels already. | 198 // like a lazy decode or imagegenerator. PreLocked means it is flat
pixels already. |
184 fBitmap.lockPixels(); | 199 fBitmap.lockPixels(); |
185 } | 200 } |
186 } | 201 } |
187 | 202 |
188 ~SkSpecialImage_Raster() override { } | 203 ~SkSpecialImage_Raster() override { } |
189 | 204 |
190 bool isOpaque() const override { return fBitmap.isOpaque(); } | 205 bool isOpaque() const override { return fBitmap.isOpaque(); } |
191 | 206 |
(...skipping 20 matching lines...) Expand all Loading... |
212 return true; | 227 return true; |
213 } | 228 } |
214 return false; | 229 return false; |
215 } | 230 } |
216 | 231 |
217 bool getBitmap(SkBitmap* result) const override { | 232 bool getBitmap(SkBitmap* result) const override { |
218 *result = fBitmap; | 233 *result = fBitmap; |
219 return true; | 234 return true; |
220 } | 235 } |
221 | 236 |
| 237 bool onGetROPixels(SkBitmap* result) const override { |
| 238 *result = fBitmap; |
| 239 return true; |
| 240 } |
| 241 |
222 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | 242 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
223 return SkSpecialSurface::NewRaster(this->proxy(), info, nullptr); | 243 return SkSpecialSurface::NewRaster(this->proxy(), info, nullptr); |
224 } | 244 } |
225 | 245 |
226 private: | 246 private: |
227 SkBitmap fBitmap; | 247 SkBitmap fBitmap; |
228 | 248 |
229 typedef SkSpecialImage_Base INHERITED; | 249 typedef SkSpecialImage_Base INHERITED; |
230 }; | 250 }; |
231 | 251 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 this->width(), this->heig
ht(), | 298 this->width(), this->heig
ht(), |
279 this->isOpaque()); | 299 this->isOpaque()); |
280 if (!result->setInfo(info)) { | 300 if (!result->setInfo(info)) { |
281 return false; | 301 return false; |
282 } | 302 } |
283 | 303 |
284 result->setPixelRef(new SkGrPixelRef(info, fTexture))->unref(); | 304 result->setPixelRef(new SkGrPixelRef(info, fTexture))->unref(); |
285 return true; | 305 return true; |
286 } | 306 } |
287 | 307 |
| 308 bool onGetROPixels(SkBitmap* result) const override { |
| 309 |
| 310 const SkImageInfo info = SkImageInfo::MakeN32(this->width(), |
| 311 this->height(), |
| 312 this->isOpaque() ? kOpaque
_SkAlphaType |
| 313 : kPremul
_SkAlphaType); |
| 314 if (!result->tryAllocPixels(info)) { |
| 315 return false; |
| 316 } |
| 317 |
| 318 if (!fTexture->readPixels(0, 0, result->width(), result->height(), kSkia
8888_GrPixelConfig, |
| 319 result->getPixels(), result->rowBytes())) { |
| 320 return false; |
| 321 } |
| 322 |
| 323 result->pixelRef()->setImmutable(); |
| 324 return true; |
| 325 } |
| 326 |
288 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | 327 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
289 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); | 328 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); |
290 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 329 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
291 | 330 |
292 return SkSpecialSurface::NewRenderTarget(this->proxy(), fTexture->getCon
text(), desc); | 331 return SkSpecialSurface::NewRenderTarget(this->proxy(), fTexture->getCon
text(), desc); |
293 } | 332 } |
294 | 333 |
295 private: | 334 private: |
296 SkAutoTUnref<GrTexture> fTexture; | 335 SkAutoTUnref<GrTexture> fTexture; |
297 const SkAlphaType fAlphaType; | 336 const SkAlphaType fAlphaType; |
(...skipping 14 matching lines...) Expand all Loading... |
312 | 351 |
313 SkSpecialImage* SkSpecialImage::NewFromGpu(SkImageFilter::Proxy* proxy, | 352 SkSpecialImage* SkSpecialImage::NewFromGpu(SkImageFilter::Proxy* proxy, |
314 const SkIRect& subset, | 353 const SkIRect& subset, |
315 uint32_t uniqueID, | 354 uint32_t uniqueID, |
316 GrTexture* tex, | 355 GrTexture* tex, |
317 SkAlphaType at) { | 356 SkAlphaType at) { |
318 return nullptr; | 357 return nullptr; |
319 } | 358 } |
320 | 359 |
321 #endif | 360 #endif |
OLD | NEW |