Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: src/gpu/SkGr.cpp

Issue 1249543003: Creating functions for uploading a mipmapped texture. (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Storing the max mipmap level in the texture. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "GrImageIDTextureAdjuster.h" 15 #include "GrImageIDTextureAdjuster.h"
16 #include "GrXferProcessor.h" 16 #include "GrXferProcessor.h"
17 #include "GrYUVProvider.h" 17 #include "GrYUVProvider.h"
18 18
19 #include "SkColorFilter.h" 19 #include "SkColorFilter.h"
20 #include "SkConfig8888.h" 20 #include "SkConfig8888.h"
21 #include "SkCanvas.h" 21 #include "SkCanvas.h"
22 #include "SkData.h" 22 #include "SkData.h"
23 #include "SkErrorInternals.h" 23 #include "SkErrorInternals.h"
24 #include "SkGrPixelRef.h" 24 #include "SkGrPixelRef.h"
25 #include "SkMessageBus.h" 25 #include "SkMessageBus.h"
26 #include "SkMath.h"
27 #include "SkMipMapLevel.h"
26 #include "SkPixelRef.h" 28 #include "SkPixelRef.h"
27 #include "SkResourceCache.h" 29 #include "SkResourceCache.h"
28 #include "SkTextureCompressor.h" 30 #include "SkTextureCompressor.h"
31 #include "SkTypes.h"
29 #include "SkYUVPlanesCache.h" 32 #include "SkYUVPlanesCache.h"
30 #include "effects/GrBicubicEffect.h" 33 #include "effects/GrBicubicEffect.h"
31 #include "effects/GrConstColorProcessor.h" 34 #include "effects/GrConstColorProcessor.h"
32 #include "effects/GrDitherEffect.h" 35 #include "effects/GrDitherEffect.h"
33 #include "effects/GrPorterDuffXferProcessor.h" 36 #include "effects/GrPorterDuffXferProcessor.h"
34 #include "effects/GrXfermodeFragmentProcessor.h" 37 #include "effects/GrXfermodeFragmentProcessor.h"
35 #include "effects/GrYUVtoRGBEffect.h" 38 #include "effects/GrYUVtoRGBEffect.h"
36 39
37 #ifndef SK_IGNORE_ETC1_SUPPORT 40 #ifndef SK_IGNORE_ETC1_SUPPORT
38 # include "ktx.h" 41 # include "ktx.h"
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {} 281 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
279 private: 282 private:
280 GrUniqueKeyInvalidatedMessage fMsg; 283 GrUniqueKeyInvalidatedMessage fMsg;
281 284
282 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>:: Post(fMsg); } 285 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>:: Post(fMsg); }
283 }; 286 };
284 287
285 pixelRef->addGenIDChangeListener(new Invalidator(key)); 288 pixelRef->addGenIDChangeListener(new Invalidator(key));
286 } 289 }
287 290
291 GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& b itmap)
292 {
293 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
294 if (kIndex_8_SkColorType != bitmap.colorType() && !bitmap.readyToDraw()) {
295 GrTexture *texture = load_etc1_texture(ctx, bitmap, desc);
296 if (texture) {
297 return texture;
298 }
299 }
300
301 GrTexture *texture = create_texture_from_yuv(ctx, bitmap, desc);
302 if (texture) {
303 return texture;
304 }
305
306 SkASSERT(sizeof(int) <= sizeof(uint32_t));
307 if (bitmap.width() < 0 || bitmap.height() < 0) {
308 return nullptr;
309 }
310 const uint32_t baseWidth = static_cast<uint32_t>(bitmap.width());
311 const uint32_t baseHeight = static_cast<uint32_t>(bitmap.height());
312
313 // OpenGL's spec requires that each mipmap level have height/width equal to
314 // max(1, floor(original_height / 2^i)
315 // (or original_width) where i is the mipmap level.
316 // Continue scaling down until both axes are size 1.
317
318 const uint32_t largestAxis = SkTMax(baseWidth, baseHeight);
319 const int leadingZeros = SkCLZ(largestAxis);
320 // If the value 00011010 has 3 leading 0s then it has 5 significant bits
321 // (the bits which are not leading zeros)
322 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
323 // This is making the assumption that the size of a byte is 8 bits
324 // and that sizeof(uint32_t)'s implementation-defined behavior is 4.
325 if (significantBits < 0) {
326 return nullptr;
327 }
328 const int mipLevelCount = significantBits;
329
330 const bool isMipMapped = mipLevelCount > 1;
331 desc.fIsMipMapped = isMipMapped;
332
333 SkTArray<SkBitmap> mipLevelBitmaps(mipLevelCount - 1);
334 mipLevelBitmaps.push_back_n(mipLevelCount - 1);
335
336 SkTArray<SkMipMapLevel> texels(mipLevelCount);
337
338 SkAutoLockPixels alp(bitmap);
339 if (!bitmap.readyToDraw()) {
340 return nullptr;
341 }
342
343 SkMipMapLevel baseLevel(bitmap.getPixels(), bitmap.rowBytes(), baseWidth, ba seHeight);
344 texels.push_back(baseLevel);
345
346 SkPaint paint;
347 paint.setFilterQuality(kMedium_SkFilterQuality);
348
349 for (int i = 1; i < mipLevelCount; i++) {
350 SkBitmap& currentMipBitmap = mipLevelBitmaps[i - 1];
351
352 uint32_t twoToTheMipLevel = 1 << i;
353 uint32_t currentMipLevelWidth = SkTMax(1u, baseWidth / twoToTheMipLevel) ;
354 uint32_t currentMipLevelHeight = SkTMax(1u, baseHeight / twoToTheMipLeve l);
355
356 SkImageInfo info = SkImageInfo::Make(currentMipLevelWidth, currentMipLev elHeight,
357 bitmap.colorType(), bitmap.alphaTyp e());
358 if (!currentMipBitmap.tryAllocPixels(info)) {
359 return nullptr;
360 }
361
362 SkCanvas canvas(currentMipBitmap);
363 canvas.clear(SK_ColorTRANSPARENT);
364
365 SkMatrix matrix;
366 matrix.setScale(static_cast<float>(currentMipLevelWidth) / baseWidth,
367 static_cast<float>(currentMipLevelHeight) / baseHeight);
368 canvas.save();
369 canvas.concat(matrix);
370 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint);
bsalomon 2016/01/14 13:57:23 Mike pointed out to me that this will generate MIP
Chris Blume 2016/01/20 06:15:08 I talked to Mike, asking for clarification on the
371 canvas.restore();
372
373 SkMipMapLevel currentMipLevel(currentMipBitmap.getPixels(),
374 currentMipBitmap.rowBytes(),
375 currentMipLevelWidth, currentMipLevelHeigh t);
376 texels.push_back(currentMipLevel);
377 }
378
379 return ctx->textureProvider()->createTexture(desc, true, texels);
380 }
381
288 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap, 382 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
289 const GrTextureParams& params) { 383 const GrTextureParams& params) {
290 if (bitmap.getTexture()) { 384 if (bitmap.getTexture()) {
291 return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr); 385 return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr);
292 } 386 }
293 return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params); 387 return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params);
294 } 388 }
295 389
296 /////////////////////////////////////////////////////////////////////////////// 390 ///////////////////////////////////////////////////////////////////////////////
297 391
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 SkErrorInternals::SetError( kInvalidPaint_SkError, 721 SkErrorInternals::SetError( kInvalidPaint_SkError,
628 "Sorry, I don't understand the filtering " 722 "Sorry, I don't understand the filtering "
629 "mode you asked for. Falling back to " 723 "mode you asked for. Falling back to "
630 "MIPMaps."); 724 "MIPMaps.");
631 textureFilterMode = GrTextureParams::kMipMap_FilterMode; 725 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
632 break; 726 break;
633 727
634 } 728 }
635 return textureFilterMode; 729 return textureFilterMode;
636 } 730 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698