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

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: Rebasing Created 5 years, 1 month 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 "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"
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {} 280 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
278 private: 281 private:
279 GrUniqueKeyInvalidatedMessage fMsg; 282 GrUniqueKeyInvalidatedMessage fMsg;
280 283
281 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>:: Post(fMsg); } 284 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>:: Post(fMsg); }
282 }; 285 };
283 286
284 pixelRef->addGenIDChangeListener(new Invalidator(key)); 287 pixelRef->addGenIDChangeListener(new Invalidator(key));
285 } 288 }
286 289
290 static GrTexture* generate_mipmaps(GrContext* ctx,
291 const SkBitmap& bitmap) {
292 SkASSERT(sizeof(int) <= sizeof(uint32_t));
293 const uint32_t baseWidth = static_cast<uint32_t>(bitmap.width());
294 const uint32_t baseHeight = static_cast<uint32_t>(bitmap.height());
295
296 // OpenGL's spec requires that each mipmap level has height/width equal to
297 // max(1, floor(original_height / 2^i)
298 // (or original_height) where i is the mipmap level.
299 // Keep scaling down until both axes are size 1.
300
301 const uint32_t largestAxis = SkTMax(baseWidth, baseHeight);
302 const int leadingZeros = SkCLZ(largestAxis);
303 // If the value 00011010 has 3 leading 0s, it has 5 significant bits
304 // (the bits which are not leading zeros)
305 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
306 if (significantBits < 0)
307 {
308 return nullptr;
309 }
310 const uint32_t unsignedSignificantBits = static_cast<uint32_t>(significantBi ts);
311 const uint32_t mipLevelCount = unsignedSignificantBits;
312
313 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
314 const bool isMipMapped = mipLevelCount > 1;
315 desc.fIsMipMapped = isMipMapped;
316
317 SkAutoPixmapUnlock srcUnlocker;
318 if (!bitmap.requestLock(&srcUnlocker)) {
319 return nullptr;
320 }
321 const SkPixmap& srcPixmap = srcUnlocker.pixmap();
322 if (nullptr == srcPixmap.addr()) {
323 return nullptr;
324 }
325
326 SkTArray<SkMipMapLevel> texels(mipLevelCount);
327 SkMipMapLevel baseLevel(srcPixmap.addr(), bitmap.rowBytes(), baseWidth, base Height);
328 texels.push_back(baseLevel);
329
330 SkTArray<SkBitmap> mipLevelBitmaps(mipLevelCount - 1);
331 mipLevelBitmaps.push_back_n(mipLevelCount - 1);
332
333 for (uint32_t i = 1; i < mipLevelCount; i++) {
334 SkBitmap& currentMipBitmap = mipLevelBitmaps[i - 1];
335
336 uint32_t twoToTheMipLevel = 1 << (i + 1);
337 uint32_t currentMipLevelWidth = SkTMax(1u, baseWidth / twoToTheMipLevel) ;
338 uint32_t currentMipLevelHeight = SkTMax(1u, baseHeight / twoToTheMipLeve l);
339
340 SkImageInfo info = SkImageInfo::Make(currentMipLevelWidth, currentMipLev elHeight,
341 currentMipBitmap.colorType(),
342 bitmap.alphaType());
343 if (!currentMipBitmap.tryAllocPixels(info))
344 {
345 return nullptr;
346 }
347
348 SkCanvas canvas(currentMipBitmap);
349 canvas.clear(SK_ColorTRANSPARENT);
350 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0));
351
352 SkMipMapLevel currentMipLevel(currentMipBitmap.getPixels(),
353 currentMipBitmap.rowBytes(),
354 currentMipLevelWidth, currentMipLevelHeigh t);
355 texels.push_back(currentMipLevel);
356 }
357
358 return ctx->textureProvider()->createTexture(desc, true, texels);
359 }
360
287 class RasterBitmap_GrTextureMaker : public GrTextureMaker { 361 class RasterBitmap_GrTextureMaker : public GrTextureMaker {
288 public: 362 public:
289 RasterBitmap_GrTextureMaker(const SkBitmap& bitmap) 363 RasterBitmap_GrTextureMaker(const SkBitmap& bitmap)
290 : INHERITED(bitmap.width(), bitmap.height()) 364 : INHERITED(bitmap.width(), bitmap.height())
291 , fBitmap(bitmap) 365 , fBitmap(bitmap)
292 { 366 {
293 SkASSERT(!bitmap.getTexture()); 367 SkASSERT(!bitmap.getTexture());
294 if (!bitmap.isVolatile()) { 368 if (!bitmap.isVolatile()) {
295 SkIPoint origin = bitmap.pixelRefOrigin(); 369 SkIPoint origin = bitmap.pixelRefOrigin();
296 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.widt h(), 370 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.widt h(),
297 bitmap.height()); 371 bitmap.height());
298 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGeneration ID(), subset); 372 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGeneration ID(), subset);
299 } 373 }
300 } 374 }
301 375
302 protected: 376 protected:
303 GrTexture* refOriginalTexture(GrContext* ctx) override { 377 GrTexture* refOriginalTexture(GrContext* ctx, const GrTextureParams& params) override {
bsalomon 2015/11/02 16:01:28 Rather than passing the full params could be pass
cblume 2015/11/03 02:10:22 Done.
304 GrTexture* tex; 378 GrTexture* tex;
305 379
306 if (fOriginalKey.isValid()) { 380 if (fOriginalKey.isValid()) {
307 tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(fOriginal Key); 381 tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(fOriginal Key);
308 if (tex) { 382 if (tex) {
309 return tex; 383 return tex;
310 } 384 }
311 } 385 }
312 386
313 tex = GrUploadBitmapToTexture(ctx, fBitmap); 387 if (params.filterMode() == GrTextureParams::kMipMap_FilterMode) {
388 // If the SkBitmap is already backed by a texture, we do not want to read the contents
389 // back to the cpu and generate the mipmap only to send it back to t he GPU.
390 SkASSERT(!tex);
bsalomon 2015/11/02 16:01:28 How does this assert work? It seems like if we get
cblume 2015/11/03 02:10:22 I think this is a mistake on my part. This assert
391 tex = generate_mipmaps(ctx, fBitmap);
392 } else {
393 tex = GrUploadBitmapToTexture(ctx, fBitmap);
394 }
314 if (tex && fOriginalKey.isValid()) { 395 if (tex && fOriginalKey.isValid()) {
315 tex->resourcePriv().setUniqueKey(fOriginalKey); 396 tex->resourcePriv().setUniqueKey(fOriginalKey);
316 install_bmp_key_invalidator(fOriginalKey, fBitmap.pixelRef()); 397 install_bmp_key_invalidator(fOriginalKey, fBitmap.pixelRef());
317 } 398 }
318 return tex; 399 return tex;
319 } 400 }
320 401
321 void makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) overrid e { 402 void makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) overrid e {
322 if (fOriginalKey.isValid()) { 403 if (fOriginalKey.isValid()) {
323 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); 404 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
684 SkErrorInternals::SetError( kInvalidPaint_SkError, 765 SkErrorInternals::SetError( kInvalidPaint_SkError,
685 "Sorry, I don't understand the filtering " 766 "Sorry, I don't understand the filtering "
686 "mode you asked for. Falling back to " 767 "mode you asked for. Falling back to "
687 "MIPMaps."); 768 "MIPMaps.");
688 textureFilterMode = GrTextureParams::kMipMap_FilterMode; 769 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
689 break; 770 break;
690 771
691 } 772 }
692 return textureFilterMode; 773 return textureFilterMode;
693 } 774 }
OLDNEW
« src/gpu/GrSurface.cpp ('K') | « src/gpu/SkGpuDevice.cpp ('k') | src/gpu/SkGrPixelRef.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698