diff options
author | borenet@google.com <borenet@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-09-14 14:34:28 +0000 |
---|---|---|
committer | borenet@google.com <borenet@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-09-14 14:34:28 +0000 |
commit | e21795e277aa7942a152c4f9921b7d4bd9076406 (patch) | |
tree | 4d890c5ef122b6909d0931d428027a1fd3a9843d /tools/picture_utils.cpp | |
parent | 71e21ba2afc78ae56cb72cfc5a64858cc2144a0a (diff) |
Enable automatic rescaling in bench_pictures
bench_pictures with "--device gpu" is failing because we're trying to allocate
too much GPU memory. Move the recently-added scaling code into picture_utils
and share it between render_pictures and bench_pictures.
Review URL: https://codereview.appspot.com/6495125
git-svn-id: http://skia.googlecode.com/svn/trunk@5543 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tools/picture_utils.cpp')
-rw-r--r-- | tools/picture_utils.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tools/picture_utils.cpp b/tools/picture_utils.cpp index b9bedbb504..2dd1f4ec45 100644 --- a/tools/picture_utils.cpp +++ b/tools/picture_utils.cpp @@ -6,9 +6,11 @@ */ #include "picture_utils.h" +#include "SkCanvas.h" #include "SkColorPriv.h" #include "SkBitmap.h" #include "SkPicture.h" +#include "SkRefCnt.h" #include "SkString.h" #include "SkStream.h" @@ -90,4 +92,47 @@ namespace sk_tools { bitmap->allocPixels(); bitmap->eraseColor(0); } + + bool area_too_big(int w, int h, SkISize* newSize) { + // just a guess, based on what seems to fail on smaller android devices + static const int64_t kMaxAreaForMemory = 16 * 1024 * 1024; + + if ((int64_t)w * h > kMaxAreaForMemory) { + do { + w >>= 1; + h >>= 1; + } while ((int64_t)w * h > kMaxAreaForMemory); + if (0 == w) { + w = 1; + } + if (0 == h) { + h = 1; + } + newSize->set(w, h); + return true; + } + return false; + } + + void resize_if_needed(SkAutoTUnref<SkPicture>* aur) { + SkISize newSize; + SkPicture* picture = aur->get(); + if (area_too_big(picture->width(), picture->height(), &newSize)) { + SkPicture* pic = SkNEW(SkPicture); + picture->ref(); + aur->reset(pic); + + SkCanvas* canvas = pic->beginRecording(newSize.width(), + newSize.height()); + SkScalar scale = SkIntToScalar(newSize.width()) / picture->width(); + canvas->scale(scale, scale); + canvas->drawPicture(*picture); + pic->endRecording(); + + SkDebugf( + "... rescaling to [%d %d] to avoid overly large allocations\n", + newSize.width(), newSize.height()); + picture->unref(); + } + } } |