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 #include "SkSpecialImage.h" |
7 | 8 |
8 #include "SkSpecialImage.h" | 9 #if SK_SUPPORT_GPU |
9 #include "SkBitmap.h" | 10 #include "GrTexture.h" |
10 #include "SkImage.h" | 11 #include "GrTextureParams.h" |
| 12 #include "SkGr.h" |
| 13 #endif |
| 14 |
11 #include "SkBitmapCache.h" | 15 #include "SkBitmapCache.h" |
12 #include "SkCanvas.h" | 16 #include "SkCanvas.h" |
13 #include "SkImage_Base.h" | 17 #include "SkImage_Base.h" |
14 #include "SkSpecialSurface.h" | 18 #include "SkSpecialSurface.h" |
15 #include "SkSurfacePriv.h" | 19 #include "SkSurfacePriv.h" |
16 #include "SkPixelRef.h" | |
17 | |
18 #if SK_SUPPORT_GPU | |
19 #include "GrContext.h" | |
20 #include "GrTexture.h" | |
21 #include "GrTextureParams.h" | |
22 #include "SkGr.h" | |
23 #include "SkGrPixelRef.h" | |
24 #include "SkGrPriv.h" | |
25 #endif | |
26 | 20 |
27 // Currently the raster imagefilters can only handle certain imageinfos. Call th
is to know if | 21 // Currently the raster imagefilters can only handle certain imageinfos. Call th
is to know if |
28 // a given info is supported. | 22 // a given info is supported. |
29 static bool valid_for_imagefilters(const SkImageInfo& info) { | 23 static bool valid_for_imagefilters(const SkImageInfo& info) { |
30 // no support for other swizzles/depths yet | 24 // no support for other swizzles/depths yet |
31 return info.colorType() == kN32_SkColorType; | 25 return info.colorType() == kN32_SkColorType; |
32 } | 26 } |
33 | 27 |
34 /////////////////////////////////////////////////////////////////////////////// | 28 /////////////////////////////////////////////////////////////////////////////// |
35 class SkSpecialImage_Base : public SkSpecialImage { | 29 class SkSpecialImage_Base : public SkSpecialImage { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 } | 153 } |
160 | 154 |
161 sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const { | 155 sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const { |
162 return as_SIB(this)->onMakeSubset(subset); | 156 return as_SIB(this)->onMakeSubset(subset); |
163 } | 157 } |
164 | 158 |
165 sk_sp<SkImage> SkSpecialImage::makeTightSubset(const SkIRect& subset) const { | 159 sk_sp<SkImage> SkSpecialImage::makeTightSubset(const SkIRect& subset) const { |
166 return as_SIB(this)->onMakeTightSubset(subset); | 160 return as_SIB(this)->onMakeTightSubset(subset); |
167 } | 161 } |
168 | 162 |
| 163 #if SK_SUPPORT_GPU |
| 164 #include "SkGr.h" |
| 165 #include "SkGrPixelRef.h" |
| 166 #endif |
| 167 |
| 168 /////////////////////////////////////////////////////////////////////////////// |
| 169 #include "SkImage.h" |
| 170 #if SK_SUPPORT_GPU |
| 171 #include "GrContext.h" |
| 172 #include "SkGrPriv.h" |
| 173 #endif |
| 174 |
| 175 class SkSpecialImage_Image : public SkSpecialImage_Base { |
| 176 public: |
| 177 SkSpecialImage_Image(const SkIRect& subset, |
| 178 sk_sp<SkImage> image, |
| 179 const SkSurfaceProps* props) |
| 180 : INHERITED(subset, image->uniqueID(), props) |
| 181 , fImage(image) { |
| 182 } |
| 183 |
| 184 ~SkSpecialImage_Image() override { } |
| 185 |
| 186 bool isOpaque() const override { return fImage->isOpaque(); } |
| 187 |
| 188 size_t getSize() const override { |
| 189 #if SK_SUPPORT_GPU |
| 190 if (GrTexture* texture = as_IB(fImage.get())->peekTexture()) { |
| 191 return texture->gpuMemorySize(); |
| 192 } else |
| 193 #endif |
| 194 { |
| 195 SkPixmap pm; |
| 196 if (fImage->peekPixels(&pm)) { |
| 197 return pm.height() * pm.rowBytes(); |
| 198 } |
| 199 } |
| 200 return 0; |
| 201 } |
| 202 |
| 203 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint)
const override { |
| 204 SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset
().height()); |
| 205 |
| 206 canvas->drawImageRect(fImage.get(), this->subset(), |
| 207 dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 208 } |
| 209 |
| 210 bool onGetROPixels(SkBitmap* bm) const override { |
| 211 return as_IB(fImage)->getROPixels(bm); |
| 212 } |
| 213 |
| 214 GrTexture* onPeekTexture() const override { return as_IB(fImage)->peekTextur
e(); } |
| 215 |
| 216 #if SK_SUPPORT_GPU |
| 217 sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override { |
| 218 return sk_sp<GrTexture>(as_IB(fImage)->asTextureRef(context, |
| 219 GrTextureParams::Cla
mpNoFilter(), |
| 220 SkSourceGammaTreatme
nt::kRespect)); |
| 221 } |
| 222 #endif |
| 223 |
| 224 bool getBitmapDeprecated(SkBitmap* result) const override { |
| 225 #if SK_SUPPORT_GPU |
| 226 if (GrTexture* texture = as_IB(fImage.get())->peekTexture()) { |
| 227 const SkImageInfo info = GrMakeInfoFromTexture(texture, |
| 228 fImage->width(), fIma
ge->height(), |
| 229 fImage->isOpaque()); |
| 230 if (!result->setInfo(info)) { |
| 231 return false; |
| 232 } |
| 233 |
| 234 result->setPixelRef(new SkGrPixelRef(info, texture))->unref(); |
| 235 return true; |
| 236 } |
| 237 #endif |
| 238 |
| 239 return as_IB(fImage.get())->asBitmapForImageFilters(result); |
| 240 } |
| 241 |
| 242 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const overrid
e { |
| 243 #if SK_SUPPORT_GPU |
| 244 GrTexture* texture = as_IB(fImage.get())->peekTexture(); |
| 245 if (texture) { |
| 246 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *texture->get
Context()->caps()); |
| 247 |
| 248 return SkSpecialSurface::MakeRenderTarget(texture->getContext(), |
| 249 info.width(), |
| 250 info.height(), |
| 251 config); |
| 252 } |
| 253 #endif |
| 254 return SkSpecialSurface::MakeRaster(info, nullptr); |
| 255 } |
| 256 |
| 257 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
| 258 sk_sp<SkImage> subsetImg(fImage->makeSubset(subset)); |
| 259 if (!subsetImg) { |
| 260 return nullptr; |
| 261 } |
| 262 |
| 263 return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(subset.width(), sub
set.height()), |
| 264 subsetImg, |
| 265 &this->props()); |
| 266 } |
| 267 |
| 268 sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override { |
| 269 return fImage->makeSubset(subset); |
| 270 } |
| 271 |
| 272 sk_sp<SkSurface> onMakeTightSurface(const SkImageInfo& info) const override
{ |
| 273 #if SK_SUPPORT_GPU |
| 274 GrTexture* texture = as_IB(fImage.get())->peekTexture(); |
| 275 if (texture) { |
| 276 return SkSurface::MakeRenderTarget(texture->getContext(), SkBudgeted
::kYes, info); |
| 277 } |
| 278 #endif |
| 279 return SkSurface::MakeRaster(info, nullptr); |
| 280 } |
| 281 |
| 282 private: |
| 283 sk_sp<SkImage> fImage; |
| 284 |
| 285 typedef SkSpecialImage_Base INHERITED; |
| 286 }; |
| 287 |
169 #ifdef SK_DEBUG | 288 #ifdef SK_DEBUG |
170 static bool rect_fits(const SkIRect& rect, int width, int height) { | 289 static bool rect_fits(const SkIRect& rect, int width, int height) { |
171 if (0 == width && 0 == height) { | 290 if (0 == width && 0 == height) { |
172 SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == r
ect.fBottom); | 291 SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == r
ect.fBottom); |
173 return true; | 292 return true; |
174 } | 293 } |
175 | 294 |
176 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && | 295 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && |
177 rect.fRight >= 0 && rect.fRight <= width && | 296 rect.fRight >= 0 && rect.fRight <= width && |
178 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && | 297 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && |
179 rect.fBottom >= 0 && rect.fBottom <= height; | 298 rect.fBottom >= 0 && rect.fBottom <= height; |
180 } | 299 } |
181 #endif | 300 #endif |
182 | 301 |
183 sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(const SkIRect& subset, | 302 sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(const SkIRect& subset, |
184 sk_sp<SkImage> image, | 303 sk_sp<SkImage> image, |
185 const SkSurfaceProps* props)
{ | 304 const SkSurfaceProps* props)
{ |
186 SkASSERT(rect_fits(subset, image->width(), image->height())); | 305 SkASSERT(rect_fits(subset, image->width(), image->height())); |
187 | 306 |
188 if (GrTexture* texture = as_IB(image)->peekTexture()) { | 307 if (valid_for_imagefilters(as_IB(image.get())->onImageInfo())) { |
189 return MakeFromGpu(subset, image->uniqueID(), sk_ref_sp(texture), props)
; | 308 return sk_make_sp<SkSpecialImage_Image>(subset, image, props); |
190 } else { | 309 } else { |
191 SkBitmap bm; | 310 return nullptr; |
192 if (as_IB(image)->getROPixels(&bm)) { | |
193 return MakeFromRaster(subset, bm, props); | |
194 } | |
195 } | 311 } |
196 return nullptr; | |
197 } | 312 } |
198 | 313 |
199 /////////////////////////////////////////////////////////////////////////////// | 314 /////////////////////////////////////////////////////////////////////////////// |
| 315 #include "SkBitmap.h" |
| 316 #include "SkImageInfo.h" |
| 317 #include "SkPixelRef.h" |
200 | 318 |
201 class SkSpecialImage_Raster : public SkSpecialImage_Base { | 319 class SkSpecialImage_Raster : public SkSpecialImage_Base { |
202 public: | 320 public: |
203 SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSur
faceProps* props) | 321 SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSur
faceProps* props) |
204 : INHERITED(subset, bm.getGenerationID(), props) | 322 : INHERITED(subset, bm.getGenerationID(), props) |
205 , fBitmap(bm) { | 323 , fBitmap(bm) { |
206 if (bm.pixelRef() && bm.pixelRef()->isPreLocked()) { | 324 if (bm.pixelRef() && bm.pixelRef()->isPreLocked()) { |
207 // we only preemptively lock if there is no chance of triggering som
ething expensive | 325 // we only preemptively lock if there is no chance of triggering som
ething expensive |
208 // like a lazy decode or imagegenerator. PreLocked means it is flat
pixels already. | 326 // like a lazy decode or imagegenerator. PreLocked means it is flat
pixels already. |
209 fBitmap.lockPixels(); | 327 fBitmap.lockPixels(); |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(const SkIRect& subset, | 562 sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(const SkIRect& subset, |
445 uint32_t uniqueID, | 563 uint32_t uniqueID, |
446 sk_sp<GrTexture> tex, | 564 sk_sp<GrTexture> tex, |
447 const SkSurfaceProps* props, | 565 const SkSurfaceProps* props, |
448 SkAlphaType at) { | 566 SkAlphaType at) { |
449 SkASSERT(rect_fits(subset, tex->width(), tex->height())); | 567 SkASSERT(rect_fits(subset, tex->width(), tex->height())); |
450 return sk_make_sp<SkSpecialImage_Gpu>(subset, uniqueID, std::move(tex), at,
props); | 568 return sk_make_sp<SkSpecialImage_Gpu>(subset, uniqueID, std::move(tex), at,
props); |
451 } | 569 } |
452 | 570 |
453 #endif | 571 #endif |
OLD | NEW |