diff options
author | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2009-06-16 01:50:25 +0000 |
---|---|---|
committer | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2009-06-16 01:50:25 +0000 |
commit | ad687ceae225a8bd5af2acbca5382b6147af29d7 (patch) | |
tree | b8c38765f78a443ba783b794a6906e663ec3619f | |
parent | 31d1c64bd54dd2b216846a30da4cc2e96765ec5f (diff) |
add sample for mipmaps
git-svn-id: http://skia.googlecode.com/svn/trunk@218 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | samplecode/SampleMipMap.cpp | 136 | ||||
-rw-r--r-- | xcode/sampleapp/SampleApp.xcodeproj/project.pbxproj | 4 |
2 files changed, 140 insertions, 0 deletions
diff --git a/samplecode/SampleMipMap.cpp b/samplecode/SampleMipMap.cpp new file mode 100644 index 0000000000..025cb0714a --- /dev/null +++ b/samplecode/SampleMipMap.cpp @@ -0,0 +1,136 @@ +#include "SampleCode.h" +#include "SkView.h" +#include "SkCanvas.h" +#include "SkDevice.h" +#include "SkPaint.h" + +static SkBitmap createBitmap(int n) { + SkBitmap bitmap; + bitmap.setConfig(SkBitmap::kARGB_8888_Config, n, n); + bitmap.allocPixels(); + bitmap.eraseColor(0); + + SkCanvas canvas(bitmap); + SkRect r; + r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n)); + SkPaint paint; + paint.setAntiAlias(true); + + paint.setColor(SK_ColorRED); + canvas.drawOval(r, paint); + paint.setColor(SK_ColorBLUE); + paint.setStrokeWidth(SkIntToScalar(n)/15); + paint.setStyle(SkPaint::kStroke_Style); + canvas.drawLine(0, 0, r.fRight, r.fBottom, paint); + canvas.drawLine(0, r.fBottom, r.fRight, 0, paint); + + return bitmap; +} + +class MipMapView : public SkView { + SkBitmap fBitmap; + enum { + N = 128 + }; +public: + MipMapView() { + fBitmap = createBitmap(N); + + fWidth = N; + fDW = -1; + } + +protected: + // overrides from SkEventSink + virtual bool onQuery(SkEvent* evt) { + if (SampleCode::TitleQ(*evt)) { + SampleCode::TitleR(evt, "MapMaps"); + return true; + } + return this->INHERITED::onQuery(evt); + } + + void drawN(SkCanvas* canvas, const SkBitmap& bitmap) { + SkAutoCanvasRestore acr(canvas, true); + for (int i = N; i > 1; i >>= 1) { + canvas->drawBitmap(bitmap, 0, 0, NULL); + canvas->translate(SkIntToScalar(N + 8), 0); + canvas->scale(SK_ScalarHalf, SK_ScalarHalf); + } + } + + void drawN2(SkCanvas* canvas, const SkBitmap& bitmap) { + SkBitmap bg; + bg.setConfig(SkBitmap::kARGB_8888_Config, N, N); + bg.allocPixels(); + + SkAutoCanvasRestore acr(canvas, true); + for (int i = 0; i < N-2; i++) { + bg.eraseColor(0); + SkCanvas c(bg); + c.scale(SK_Scalar1 / (1 << i), SK_Scalar1 / (1 << i)); + c.drawBitmap(bitmap, 0, 0, NULL); + + canvas->save(); + canvas->scale(SkIntToScalar(1 << i), SkIntToScalar(1 << i)); + canvas->drawBitmap(bg, 0, 0, NULL); + canvas->restore(); + canvas->translate(SkIntToScalar(N + 8), 0); + } + } + + void drawBG(SkCanvas* canvas) { + canvas->drawColor(SK_ColorWHITE); + } + + virtual void onDraw(SkCanvas* canvas) { + this->drawBG(canvas); + + canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); + + drawN2(canvas, fBitmap); + + canvas->translate(0, SkIntToScalar(N + 8)); + SkBitmap bitmap(fBitmap); + bitmap.buildMipMap(); + drawN2(canvas, bitmap); + + fWidth += fDW; + if (fDW > 0 && fWidth > N) { + fDW = -fDW; + fWidth = N; + } else if (fDW < 0 && fWidth < 8) { + fDW = -fDW; + fWidth = 8; + } + + SkRect dst; + dst.set(0, 0, SkIntToScalar(fWidth), SkIntToScalar(fWidth)); + + SkPaint paint; + paint.setFilterBitmap(true); + paint.setAntiAlias(true); + + canvas->translate(0, SkIntToScalar(N + 8)); + canvas->drawBitmapRect(fBitmap, NULL, dst, NULL); + canvas->translate(SkIntToScalar(N + 8), 0); + canvas->drawBitmapRect(fBitmap, NULL, dst, &paint); + canvas->translate(SkIntToScalar(N + 8), 0); + canvas->drawBitmapRect(bitmap, NULL, dst, NULL); + canvas->translate(SkIntToScalar(N + 8), 0); + canvas->drawBitmapRect(bitmap, NULL, dst, &paint); + + this->inval(NULL); + } + +private: + int fWidth, fDW; + + typedef SkView INHERITED; +}; + +////////////////////////////////////////////////////////////////////////////// + +static SkView* MyFactory() { return new MipMapView; } +static SkViewRegister reg(MyFactory); + diff --git a/xcode/sampleapp/SampleApp.xcodeproj/project.pbxproj b/xcode/sampleapp/SampleApp.xcodeproj/project.pbxproj index c67031436d..0ca9d9f286 100644 --- a/xcode/sampleapp/SampleApp.xcodeproj/project.pbxproj +++ b/xcode/sampleapp/SampleApp.xcodeproj/project.pbxproj @@ -79,6 +79,7 @@ 2762F66D0FCCCABE002BD8B4 /* SkFlipPixelRef.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2762F66B0FCCCABE002BD8B4 /* SkFlipPixelRef.cpp */; }; 2762F66E0FCCCABE002BD8B4 /* SkPageFlipper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2762F66C0FCCCABE002BD8B4 /* SkPageFlipper.cpp */; }; 2762F67D0FCCCB01002BD8B4 /* SamplePageFlip.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2762F6770FCCCB01002BD8B4 /* SamplePageFlip.cpp */; }; + 2794C04F0FE72903009AD112 /* SampleMipMap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2794C04E0FE72903009AD112 /* SampleMipMap.cpp */; }; 8D0C4E8D0486CD37000505A6 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 0867D6AAFE840B52C02AAC07 /* InfoPlist.strings */; }; 8D0C4E8E0486CD37000505A6 /* main.nib in Resources */ = {isa = PBXBuildFile; fileRef = 02345980000FD03B11CA0E72 /* main.nib */; }; 8D0C4E920486CD37000505A6 /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 20286C33FDCF999611CA2CEA /* Carbon.framework */; }; @@ -211,6 +212,7 @@ 2762F6770FCCCB01002BD8B4 /* SamplePageFlip.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SamplePageFlip.cpp; path = ../../samplecode/SamplePageFlip.cpp; sourceTree = SOURCE_ROOT; }; 2762F6780FCCCB01002BD8B4 /* SamplePolyToPoly.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SamplePolyToPoly.cpp; path = ../../samplecode/SamplePolyToPoly.cpp; sourceTree = SOURCE_ROOT; }; 2762F6790FCCCB01002BD8B4 /* SampleTests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SampleTests.cpp; path = ../../samplecode/SampleTests.cpp; sourceTree = SOURCE_ROOT; }; + 2794C04E0FE72903009AD112 /* SampleMipMap.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SampleMipMap.cpp; path = ../../samplecode/SampleMipMap.cpp; sourceTree = SOURCE_ROOT; }; 32DBCF6D0370B57F00C91783 /* CICarbonSample_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CICarbonSample_Prefix.pch; sourceTree = "<group>"; }; 4A9504C8FFE6A3BC11CA0CBA /* ApplicationServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ApplicationServices.framework; path = /System/Library/Frameworks/ApplicationServices.framework; sourceTree = "<absolute>"; }; 4A9504CAFFE6A41611CA0CBA /* CoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreServices.framework; path = /System/Library/Frameworks/CoreServices.framework; sourceTree = "<absolute>"; }; @@ -267,6 +269,7 @@ 008C4D970F77DAEE0056981C /* SampleHairline.cpp */, 0041CE1F0F00A12400695E8C /* SampleCamera.cpp */, 0041CE200F00A12400695E8C /* SampleCircle.cpp */, + 2794C04E0FE72903009AD112 /* SampleMipMap.cpp */, 0041CE210F00A12400695E8C /* SampleCode.h */, 0041CE220F00A12400695E8C /* SampleCull.cpp */, 0041CE230F00A12400695E8C /* SampleDither.cpp */, @@ -571,6 +574,7 @@ 00A7295D0FD8397600D5051F /* SampleAll.cpp in Sources */, 000A99820FD97526007E45BD /* SampleArc.cpp in Sources */, 00AF77B00FE2EA2D007F9650 /* SampleTestGL.cpp in Sources */, + 2794C04F0FE72903009AD112 /* SampleMipMap.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; |