Skip to content

Commit 6714e67

Browse files
Jean-Baptiste QueruAndroid Code Review
authored andcommitted
Merge "Ninepatch tweaks for better interop"
2 parents 4770c79 + 9faa34e commit 6714e67

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

core/jni/Android.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ LOCAL_SRC_FILES:= \
9696
android/graphics/Movie.cpp \
9797
android/graphics/NinePatch.cpp \
9898
android/graphics/NinePatchImpl.cpp \
99+
android/graphics/NinePatchPeeker.cpp \
99100
android/graphics/Paint.cpp \
100101
android/graphics/Path.cpp \
101102
android/graphics/PathMeasure.cpp \
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "NinePatchPeeker.h"
18+
19+
#include "SkBitmap.h"
20+
21+
using namespace android;
22+
23+
bool NinePatchPeeker::peek(const char tag[], const void* data, size_t length) {
24+
if (strcmp("npTc", tag) == 0 && length >= sizeof(Res_png_9patch)) {
25+
Res_png_9patch* patch = (Res_png_9patch*) data;
26+
size_t patchSize = patch->serializedSize();
27+
assert(length == patchSize);
28+
// You have to copy the data because it is owned by the png reader
29+
Res_png_9patch* patchNew = (Res_png_9patch*) malloc(patchSize);
30+
memcpy(patchNew, patch, patchSize);
31+
// this relies on deserialization being done in place
32+
Res_png_9patch::deserialize(patchNew);
33+
patchNew->fileToDevice();
34+
if (fPatchIsValid) {
35+
free(fPatch);
36+
}
37+
fPatch = patchNew;
38+
//printf("9patch: (%d,%d)-(%d,%d)\n",
39+
// fPatch.sizeLeft, fPatch.sizeTop,
40+
// fPatch.sizeRight, fPatch.sizeBottom);
41+
fPatchIsValid = true;
42+
43+
// now update our host to force index or 32bit config
44+
// 'cause we don't want 565 predithered, since as a 9patch, we know
45+
// we will be stretched, and therefore we want to dither afterwards.
46+
static const SkBitmap::Config gNo565Pref[] = {
47+
SkBitmap::kIndex8_Config,
48+
SkBitmap::kIndex8_Config,
49+
SkBitmap::kARGB_8888_Config,
50+
SkBitmap::kARGB_8888_Config,
51+
SkBitmap::kARGB_8888_Config,
52+
SkBitmap::kARGB_8888_Config,
53+
};
54+
fHost->setPrefConfigTable(gNo565Pref);
55+
} else {
56+
fPatch = NULL;
57+
}
58+
return true; // keep on decoding
59+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef NinePatchPeeker_h
18+
#define NinePatchPeeker_h
19+
20+
#include "SkImageDecoder.h"
21+
#include <utils/ResourceTypes.h>
22+
23+
using namespace android;
24+
25+
class NinePatchPeeker : public SkImageDecoder::Peeker {
26+
SkImageDecoder* fHost;
27+
public:
28+
NinePatchPeeker(SkImageDecoder* host) {
29+
// the host lives longer than we do, so a raw ptr is safe
30+
fHost = host;
31+
fPatchIsValid = false;
32+
}
33+
34+
~NinePatchPeeker() {
35+
if (fPatchIsValid) {
36+
free(fPatch);
37+
}
38+
}
39+
40+
bool fPatchIsValid;
41+
Res_png_9patch* fPatch;
42+
43+
virtual bool peek(const char tag[], const void* data, size_t length);
44+
};
45+
46+
#endif // NinePatchPeeker_h

0 commit comments

Comments
 (0)