From 8cae124af2142687a6833dbaab8a43df6dd67b43 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Thu, 10 Sep 2009 14:32:16 -0700 Subject: [PATCH] Various cleanup around resources and nine-patches. Remove the stuff that doesn't use preloaded drawables when in compatibility mode, since this works fine ever since we were able to deal with drawables in a different density than the canvas. Change the snapshot function on View to return a snapshot at the same size that will actually be drawn on screen (when in compatibility mode), to be able to show scaling artifacts and all. This change was original an attempt to fix issue #2101917: Text field edges appears to be improperly rounded. That turns out to probably be something deeper in the graphics system, but also included here is the debugging code I did to try to track down the problem to make it easy to turn on again later. Change-Id: I34bfca629639c7ff103f3989d88874112ef778d9 --- .../android/content/res/AssetManager.java | 1 - core/java/android/content/res/Resources.java | 10 +------ core/java/android/view/View.java | 16 +++++++--- core/jni/android/graphics/NinePatch.cpp | 29 +++++++++++++++++++ core/jni/android/graphics/NinePatchImpl.cpp | 28 +++++++++--------- .../graphics/drawable/NinePatchDrawable.java | 6 ++++ 6 files changed, 63 insertions(+), 27 deletions(-) diff --git a/core/java/android/content/res/AssetManager.java b/core/java/android/content/res/AssetManager.java index 0bc8a9d2e4827..0d43b2a83c72f 100644 --- a/core/java/android/content/res/AssetManager.java +++ b/core/java/android/content/res/AssetManager.java @@ -24,7 +24,6 @@ import android.util.TypedValue; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.util.Locale; /** * Provides access to an application's raw asset files; see {@link Resources} diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java index 7d412a7b5554c..ba5c9ed18a43e 100644 --- a/core/java/android/content/res/Resources.java +++ b/core/java/android/content/res/Resources.java @@ -66,8 +66,6 @@ public class Resources { = new SparseArray(); private static boolean mPreloaded; - private final LongSparseArray mPreloadedDrawables; - /*package*/ final TypedValue mTmpValue = new TypedValue(); // These are protected by the mTmpValue lock. @@ -158,11 +156,6 @@ public class Resources { } updateConfiguration(config, metrics); assets.ensureStringBlocks(); - if (mCompatibilityInfo.isScalingRequired()) { - mPreloadedDrawables = emptySparseArray(); - } else { - mPreloadedDrawables = sPreloadedDrawables; - } } /** @@ -1669,7 +1662,7 @@ public class Resources { return dr; } - Drawable.ConstantState cs = mPreloadedDrawables.get(key); + Drawable.ConstantState cs = sPreloadedDrawables.get(key); if (cs != null) { dr = cs.newDrawable(); } else { @@ -1976,7 +1969,6 @@ public class Resources { mMetrics.setToDefaults(); updateConfiguration(null, null); mAssets.ensureStringBlocks(); - mPreloadedDrawables = sPreloadedDrawables; mCompatibilityInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO; } } diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 2f17bbc71ced9..f63c2f1162e16 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -6038,16 +6038,23 @@ public class View implements Drawable.Callback, KeyEvent.Callback, Accessibility * some form of this public, but should think about the API. */ Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor) { - final int width = mRight - mLeft; - final int height = mBottom - mTop; + int width = mRight - mLeft; + int height = mBottom - mTop; - Bitmap bitmap = Bitmap.createBitmap(width, height, quality); + final AttachInfo attachInfo = mAttachInfo; + final float scale = attachInfo.mApplicationScale; + width = (int) ((width * scale) + 0.5f); + height = (int) ((height * scale) + 0.5f); + + Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, + height > 0 ? height : 1, quality); if (bitmap == null) { throw new OutOfMemoryError(); } + bitmap.setDensity(getResources().getDisplayMetrics().densityDpi); + Canvas canvas; - final AttachInfo attachInfo = mAttachInfo; if (attachInfo != null) { canvas = attachInfo.mCanvas; if (canvas == null) { @@ -6070,6 +6077,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, Accessibility computeScroll(); final int restoreCount = canvas.save(); + canvas.scale(scale, scale); canvas.translate(-mScrollX, -mScrollY); // Temporarily remove the dirty mask diff --git a/core/jni/android/graphics/NinePatch.cpp b/core/jni/android/graphics/NinePatch.cpp index fd5271e4c18b0..50df0f72a5456 100644 --- a/core/jni/android/graphics/NinePatch.cpp +++ b/core/jni/android/graphics/NinePatch.cpp @@ -1,4 +1,25 @@ +/* +** +** Copyright 2006, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +#define LOG_TAG "9patch" +#define LOG_NDEBUG 1 + #include +#include #include "SkCanvas.h" #include "SkRegion.h" @@ -62,6 +83,9 @@ public: if (destDensity == srcDensity || destDensity == 0 || srcDensity == 0) { + LOGV("Drawing unscaled 9-patch: (%g,%g)-(%g,%g)", + SkScalarToFloat(bounds.fLeft), SkScalarToFloat(bounds.fTop), + SkScalarToFloat(bounds.fRight), SkScalarToFloat(bounds.fBottom)); NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL); } else { canvas->save(); @@ -74,6 +98,11 @@ public: bounds.fBottom = SkScalarDiv(bounds.fBottom-bounds.fTop, scale); bounds.fLeft = bounds.fTop = 0; + LOGV("Drawing scaled 9-patch: (%g,%g)-(%g,%g) srcDensity=%d destDensity=%d", + SkScalarToFloat(bounds.fLeft), SkScalarToFloat(bounds.fTop), + SkScalarToFloat(bounds.fRight), SkScalarToFloat(bounds.fBottom), + srcDensity, destDensity); + NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL); canvas->restore(); diff --git a/core/jni/android/graphics/NinePatchImpl.cpp b/core/jni/android/graphics/NinePatchImpl.cpp index 32d9b5789acd3..ff24a87fd2268 100644 --- a/core/jni/android/graphics/NinePatchImpl.cpp +++ b/core/jni/android/graphics/NinePatchImpl.cpp @@ -16,8 +16,10 @@ */ #define LOG_TAG "NinePatch" +#define LOG_NDEBUG 1 #include +#include #include "SkBitmap.h" #include "SkCanvas.h" @@ -25,7 +27,7 @@ #include "SkPaint.h" #include "SkUnPreMultiply.h" -#define USE_TRACEx +#define USE_TRACE #ifdef USE_TRACE static bool gTrace; @@ -130,10 +132,10 @@ void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, SkASSERT(canvas || outRegion); -#if 0 +#ifdef USE_TRACE if (canvas) { const SkMatrix& m = canvas->getTotalMatrix(); - SkDebugf("ninepatch [%g %g %g] [%g %g %g]\n", + LOGV("ninepatch [%g %g %g] [%g %g %g]\n", SkScalarToFloat(m[0]), SkScalarToFloat(m[1]), SkScalarToFloat(m[2]), SkScalarToFloat(m[3]), SkScalarToFloat(m[4]), SkScalarToFloat(m[5])); } @@ -141,10 +143,10 @@ void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, #ifdef USE_TRACE if (gTrace) { - SkDEBUGF(("======== ninepatch bounds [%g %g]\n", SkScalarToFloat(bounds.width()), SkScalarToFloat(bounds.height()))); - SkDEBUGF(("======== ninepatch paint bm [%d,%d]\n", bitmap.width(), bitmap.height())); - SkDEBUGF(("======== ninepatch xDivs [%d,%d]\n", chunk.xDivs[0], chunk.xDivs[1])); - SkDEBUGF(("======== ninepatch yDivs [%d,%d]\n", chunk.yDivs[0], chunk.yDivs[1])); + LOGV("======== ninepatch bounds [%g %g]\n", SkScalarToFloat(bounds.width()), SkScalarToFloat(bounds.height())); + LOGV("======== ninepatch paint bm [%d,%d]\n", bitmap.width(), bitmap.height()); + LOGV("======== ninepatch xDivs [%d,%d]\n", chunk.xDivs[0], chunk.xDivs[1]); + LOGV("======== ninepatch yDivs [%d,%d]\n", chunk.yDivs[0], chunk.yDivs[1]); } #endif @@ -153,7 +155,7 @@ void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, (paint && paint->getXfermode() == NULL && paint->getAlpha() == 0)) { #ifdef USE_TRACE - if (gTrace) SkDEBUGF(("======== abort ninepatch draw\n")); + if (gTrace) LOGV("======== abort ninepatch draw\n"); #endif return; } @@ -198,8 +200,8 @@ void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, } int numFixedYPixelsRemaining = bitmapHeight - numStretchyYPixelsRemaining; -#if 0 - SkDebugf("NinePatch [%d %d] bounds [%g %g %g %g] divs [%d %d]\n", +#ifdef USE_TRACE + LOGV("NinePatch [%d %d] bounds [%g %g %g %g] divs [%d %d]\n", bitmap.width(), bitmap.height(), SkScalarToFloat(bounds.fLeft), SkScalarToFloat(bounds.fTop), SkScalarToFloat(bounds.width()), SkScalarToFloat(bounds.height()), @@ -302,13 +304,13 @@ void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, goto nextDiv; } if (canvas) { -#if 0 - SkDebugf("-- src [%d %d %d %d] dst [%g %g %g %g]\n", +#ifdef USE_TRACE + LOGV("-- src [%d %d %d %d] dst [%g %g %g %g]\n", src.fLeft, src.fTop, src.width(), src.height(), SkScalarToFloat(dst.fLeft), SkScalarToFloat(dst.fTop), SkScalarToFloat(dst.width()), SkScalarToFloat(dst.height())); if (2 == src.width() && SkIntToScalar(5) == dst.width()) { - SkDebugf("--- skip patch\n"); + LOGV("--- skip patch\n"); } #endif drawStretchyPatch(canvas, src, dst, bitmap, *paint, initColor, diff --git a/graphics/java/android/graphics/drawable/NinePatchDrawable.java b/graphics/java/android/graphics/drawable/NinePatchDrawable.java index 924e19e94f806..997efb813a6da 100644 --- a/graphics/java/android/graphics/drawable/NinePatchDrawable.java +++ b/graphics/java/android/graphics/drawable/NinePatchDrawable.java @@ -21,6 +21,7 @@ import android.content.res.Resources; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.DisplayMetrics; +import android.util.Log; import android.util.TypedValue; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -170,6 +171,11 @@ public class NinePatchDrawable extends Drawable { @Override public void draw(Canvas canvas) { + if (false) { + float[] pts = new float[2]; + canvas.getMatrix().mapPoints(pts); + Log.v("9patch", "Drawing 9-patch @ " + pts[0] + "," + pts[1] + ": " + getBounds()); + } mNinePatch.draw(canvas, getBounds(), mPaint); }