OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 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 | 8 |
9 #include "SkGr.h" | 9 #include "SkGr.h" |
10 | 10 |
11 #include "GrCaps.h" | 11 #include "GrCaps.h" |
12 #include "GrContext.h" | 12 #include "GrContext.h" |
13 #include "GrTextureParamsAdjuster.h" | 13 #include "GrTextureParamsAdjuster.h" |
14 #include "GrGpuResourcePriv.h" | 14 #include "GrGpuResourcePriv.h" |
15 #include "GrXferProcessor.h" | 15 #include "GrXferProcessor.h" |
16 #include "GrYUVProvider.h" | 16 #include "GrYUVProvider.h" |
17 | 17 |
18 #include "SkColorFilter.h" | 18 #include "SkColorFilter.h" |
19 #include "SkConfig8888.h" | 19 #include "SkConfig8888.h" |
20 #include "SkCanvas.h" | 20 #include "SkCanvas.h" |
21 #include "SkData.h" | 21 #include "SkData.h" |
22 #include "SkErrorInternals.h" | 22 #include "SkErrorInternals.h" |
23 #include "SkGrPixelRef.h" | 23 #include "SkGrPixelRef.h" |
24 #include "SkMessageBus.h" | 24 #include "SkMessageBus.h" |
25 #include "SkMath.h" | |
26 #include "SkMipMapLevel.h" | |
25 #include "SkPixelRef.h" | 27 #include "SkPixelRef.h" |
26 #include "SkResourceCache.h" | 28 #include "SkResourceCache.h" |
27 #include "SkTextureCompressor.h" | 29 #include "SkTextureCompressor.h" |
30 #include "SkTypes.h" | |
28 #include "SkYUVPlanesCache.h" | 31 #include "SkYUVPlanesCache.h" |
29 #include "effects/GrBicubicEffect.h" | 32 #include "effects/GrBicubicEffect.h" |
30 #include "effects/GrConstColorProcessor.h" | 33 #include "effects/GrConstColorProcessor.h" |
31 #include "effects/GrDitherEffect.h" | 34 #include "effects/GrDitherEffect.h" |
32 #include "effects/GrPorterDuffXferProcessor.h" | 35 #include "effects/GrPorterDuffXferProcessor.h" |
33 #include "effects/GrXfermodeFragmentProcessor.h" | 36 #include "effects/GrXfermodeFragmentProcessor.h" |
34 #include "effects/GrYUVtoRGBEffect.h" | 37 #include "effects/GrYUVtoRGBEffect.h" |
35 | 38 |
36 #ifndef SK_IGNORE_ETC1_SUPPORT | 39 #ifndef SK_IGNORE_ETC1_SUPPORT |
37 # include "ktx.h" | 40 # include "ktx.h" |
38 # include "etc1.h" | 41 # include "etc1.h" |
39 #endif | 42 #endif |
40 | 43 |
41 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) { | 44 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) { |
42 GrSurfaceDesc desc; | 45 GrSurfaceDesc desc; |
43 desc.fFlags = kNone_GrSurfaceFlags; | 46 desc.fFlags = kNone_GrSurfaceFlags; |
44 desc.fWidth = info.width(); | 47 desc.fWidth = info.width(); |
45 desc.fHeight = info.height(); | 48 desc.fHeight = info.height(); |
46 desc.fConfig = SkImageInfo2GrPixelConfig(info); | 49 desc.fConfig = SkImageInfo2GrPixelConfig(info); |
47 desc.fSampleCnt = 0; | 50 desc.fSampleCnt = 0; |
48 return desc; | 51 return desc; |
49 } | 52 } |
50 | 53 |
51 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& ima geBounds) { | 54 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& ima geBounds, bool isMipMapped) { |
52 SkASSERT(key); | 55 SkASSERT(key); |
53 SkASSERT(imageID); | 56 SkASSERT(imageID); |
54 SkASSERT(!imageBounds.isEmpty()); | 57 SkASSERT(!imageBounds.isEmpty()); |
55 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomai n(); | 58 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomai n(); |
56 GrUniqueKey::Builder builder(key, kImageIDDomain, 5); | 59 GrUniqueKey::Builder builder(key, kImageIDDomain, 5); |
57 builder[0] = imageID; | 60 builder[0] = imageID; |
58 builder[1] = imageBounds.fLeft; | 61 builder[1] = imageBounds.fLeft; |
59 builder[2] = imageBounds.fTop; | 62 builder[2] = imageBounds.fTop; |
60 builder[3] = imageBounds.fRight; | 63 builder[3] = imageBounds.fRight; |
61 builder[4] = imageBounds.fBottom; | 64 builder[4] = imageBounds.fBottom; |
65 builder[5] = isMipMapped; | |
bsalomon
2015/10/28 16:40:45
This will cause us to potentially create two versi
| |
66 } | |
67 | |
68 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& ima geBounds) { | |
69 const bool isMipMapped = false; | |
70 GrMakeKeyFromImageID(key, imageID, imageBounds, isMipMapped); | |
62 } | 71 } |
63 | 72 |
64 GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data, | 73 GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data, |
65 int expectedW, int expectedH, | 74 int expectedW, int expectedH, |
66 const void** outStartOfDataToUp load) { | 75 const void** outStartOfDataToUp load) { |
67 *outStartOfDataToUpload = nullptr; | 76 *outStartOfDataToUpload = nullptr; |
68 #ifndef SK_IGNORE_ETC1_SUPPORT | 77 #ifndef SK_IGNORE_ETC1_SUPPORT |
69 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) { | 78 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) { |
70 return kUnknown_GrPixelConfig; | 79 return kUnknown_GrPixelConfig; |
71 } | 80 } |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
269 return nullptr; | 278 return nullptr; |
270 } | 279 } |
271 | 280 |
272 return ctx->textureProvider()->createTexture(desc, true, bitmap->getPixels() , | 281 return ctx->textureProvider()->createTexture(desc, true, bitmap->getPixels() , |
273 bitmap->rowBytes()); | 282 bitmap->rowBytes()); |
274 } | 283 } |
275 | 284 |
276 | 285 |
277 //////////////////////////////////////////////////////////////////////////////// | 286 //////////////////////////////////////////////////////////////////////////////// |
278 | 287 |
288 static GrTexture* generate_mipmaps(GrContext* ctx, | |
289 const SkBitmap& bitmap) { | |
290 SkASSERT(sizeof(int) <= sizeof(uint32_t)); | |
291 const uint32_t baseWidth = static_cast<uint32_t>(bitmap.width()); | |
292 const uint32_t baseHeight = static_cast<uint32_t>(bitmap.height()); | |
293 | |
294 // OpenGL's spec requires that each mipmap level has height/width equal to | |
295 // max(1, floor(original_height / 2^i) | |
296 // (or original_height) where i is the mipmap level. | |
297 // Keep scaling down until both axes are size 1. | |
298 | |
299 const uint32_t largestAxis = SkTMax(baseWidth, baseHeight); | |
300 const int leadingZeros = SkCLZ(largestAxis); | |
301 // If the value 00011010 has 3 leading 0s, it has 5 significant bits | |
302 // (the bits which are not leading zeros) | |
303 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros; | |
304 if (significantBits < 0) | |
305 { | |
306 return nullptr; | |
307 } | |
308 const uint32_t unsignedSignificantBits = static_cast<uint32_t>(significantBi ts); | |
309 const uint32_t mipLevelCount = unsignedSignificantBits; | |
310 | |
311 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info()); | |
312 const bool isMipMapped = mipLevelCount > 1; | |
313 desc.fIsMipMapped = isMipMapped; | |
314 | |
315 SkAutoPixmapUnlock srcUnlocker; | |
316 if (!bitmap.requestLock(&srcUnlocker)) { | |
317 return nullptr; | |
318 } | |
319 const SkPixmap& srcPixmap = srcUnlocker.pixmap(); | |
320 if (nullptr == srcPixmap.addr()) { | |
321 return nullptr; | |
322 } | |
323 | |
324 SkTArray<SkMipMapLevel> texels(mipLevelCount); | |
325 SkMipMapLevel baseLevel(srcPixmap.addr(), bitmap.rowBytes(), baseWidth, base Height); | |
326 texels.push_back(baseLevel); | |
327 | |
328 SkTArray<SkBitmap> mipLevelBitmaps(mipLevelCount - 1); | |
329 mipLevelBitmaps.push_back_n(mipLevelCount - 1); | |
330 | |
331 for (uint32_t i = 1; i < mipLevelCount; i++) { | |
332 SkBitmap& currentMipBitmap = mipLevelBitmaps[i - 1]; | |
333 | |
334 uint32_t twoToTheMipLevel = 1 << (i + 1); | |
335 uint32_t currentMipLevelWidth = SkTMax(1u, baseWidth / twoToTheMipLevel) ; | |
336 uint32_t currentMipLevelHeight = SkTMax(1u, baseHeight / twoToTheMipLeve l); | |
337 | |
338 SkImageInfo info = SkImageInfo::Make(currentMipLevelWidth, currentMipLev elHeight, | |
339 currentMipBitmap.colorType(), | |
340 bitmap.alphaType()); | |
341 if (!currentMipBitmap.tryAllocPixels(info)) | |
342 { | |
343 return nullptr; | |
344 } | |
345 | |
346 SkCanvas canvas(currentMipBitmap); | |
347 canvas.clear(SK_ColorTRANSPARENT); | |
348 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0)); | |
349 | |
350 SkMipMapLevel currentMipLevel(currentMipBitmap.getPixels(), | |
351 currentMipBitmap.rowBytes(), | |
352 currentMipLevelWidth, currentMipLevelHeigh t); | |
353 texels.push_back(currentMipLevel); | |
354 } | |
355 | |
356 return ctx->textureProvider()->createTexture(desc, true, texels); | |
357 } | |
358 | |
279 class Bitmap_GrTextureParamsAdjuster : public GrTextureParamsAdjuster { | 359 class Bitmap_GrTextureParamsAdjuster : public GrTextureParamsAdjuster { |
280 public: | 360 public: |
281 Bitmap_GrTextureParamsAdjuster(const SkBitmap& bitmap) | 361 Bitmap_GrTextureParamsAdjuster(const SkBitmap& bitmap) |
282 : INHERITED(bitmap.width(), bitmap.height()) | 362 : INHERITED(bitmap.width(), bitmap.height()) |
283 , fBitmap(bitmap) | 363 , fBitmap(bitmap) |
284 { | 364 { |
285 if (!bitmap.isVolatile()) { | 365 if (!bitmap.isVolatile()) { |
286 SkIPoint origin = bitmap.pixelRefOrigin(); | 366 SkIPoint origin = bitmap.pixelRefOrigin(); |
287 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.widt h(), | 367 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.widt h(), |
288 bitmap.height()); | 368 bitmap.height()); |
289 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGeneration ID(), subset); | 369 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGeneration ID(), subset); |
290 } | 370 } |
291 } | 371 } |
292 | 372 |
293 protected: | 373 protected: |
294 GrTexture* peekOriginalTexture() override { return fBitmap.getTexture(); } | 374 GrTexture* peekOriginalTexture() override { return fBitmap.getTexture(); } |
295 | 375 |
296 GrTexture* refOriginalTexture(GrContext* ctx) override { | 376 GrTexture* refOriginalTexture(GrContext* ctx, const GrTextureParams& params) override { |
297 GrTexture* tex = fBitmap.getTexture(); | 377 GrTexture* tex = fBitmap.getTexture(); |
298 if (tex) { | 378 if (tex) { |
299 return SkRef(tex); | 379 return SkRef(tex); |
300 } | 380 } |
301 | 381 |
302 if (fOriginalKey.isValid()) { | 382 if (fOriginalKey.isValid()) { |
303 tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(fOriginal Key); | 383 tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(fOriginal Key); |
304 if (tex) { | 384 if (tex) { |
305 return tex; | 385 return tex; |
306 } | 386 } |
307 } | 387 } |
308 | 388 |
309 tex = GrUploadBitmapToTexture(ctx, fBitmap); | 389 if (params.filterMode() == GrTextureParams::kMipMap_FilterMode) { |
390 // If the SkBitmap is already backed by a texture, we do not want to read the contents | |
391 // back to the cpu and generate the mipmap only to send it back to t he GPU. | |
392 SkASSERT(!tex); | |
393 tex = generate_mipmaps(ctx, fBitmap); | |
394 } else { | |
395 tex = GrUploadBitmapToTexture(ctx, fBitmap); | |
396 } | |
310 if (tex && fOriginalKey.isValid()) { | 397 if (tex && fOriginalKey.isValid()) { |
311 tex->resourcePriv().setUniqueKey(fOriginalKey); | 398 tex->resourcePriv().setUniqueKey(fOriginalKey); |
312 InstallInvalidator(fOriginalKey, fBitmap.pixelRef()); | 399 InstallInvalidator(fOriginalKey, fBitmap.pixelRef()); |
313 } | 400 } |
314 return tex; | 401 return tex; |
315 } | 402 } |
316 | 403 |
317 void makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) overrid e { | 404 void makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) overrid e { |
318 if (fOriginalKey.isValid()) { | 405 if (fOriginalKey.isValid()) { |
319 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); | 406 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
668 SkErrorInternals::SetError( kInvalidPaint_SkError, | 755 SkErrorInternals::SetError( kInvalidPaint_SkError, |
669 "Sorry, I don't understand the filtering " | 756 "Sorry, I don't understand the filtering " |
670 "mode you asked for. Falling back to " | 757 "mode you asked for. Falling back to " |
671 "MIPMaps."); | 758 "MIPMaps."); |
672 textureFilterMode = GrTextureParams::kMipMap_FilterMode; | 759 textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
673 break; | 760 break; |
674 | 761 |
675 } | 762 } |
676 return textureFilterMode; | 763 return textureFilterMode; |
677 } | 764 } |
OLD | NEW |