diff options
author | Likai Ding <likaid@codeaurora.org> | 2017-02-16 14:54:04 +0800 |
---|---|---|
committer | Gerrit - the friendly Code Review server <code-review@localhost> | 2017-05-23 01:27:00 -0700 |
commit | b7c13c2cf4b31539a9156a39ac7d2ab2bff58631 (patch) | |
tree | 1caf1b639e48450860e3223457862980be34499a /src/com/android/gallery3d/app | |
parent | 472c1037da60022fc4c38fd90b26cf601991ba56 (diff) | |
download | android_packages_apps_Gallery2-b7c13c2cf4b31539a9156a39ac7d2ab2bff58631.tar.gz android_packages_apps_Gallery2-b7c13c2cf4b31539a9156a39ac7d2ab2bff58631.tar.bz2 android_packages_apps_Gallery2-b7c13c2cf4b31539a9156a39ac7d2ab2bff58631.zip |
Gallery: support GDepth and new DDM API
- a GDepth parser
- cleaned up 3D effect code
- fixed 3D view orientation
- merged dual camera filter code
- removed MPO code
Change-Id: I0a23abbe684082c290d0357908cd7e49636c5e56
CRs-Fixed: 1108834
Diffstat (limited to 'src/com/android/gallery3d/app')
9 files changed, 137 insertions, 838 deletions
diff --git a/src/com/android/gallery3d/app/GalleryActivity.java b/src/com/android/gallery3d/app/GalleryActivity.java index 95883afd5..0c6fbe49b 100755 --- a/src/com/android/gallery3d/app/GalleryActivity.java +++ b/src/com/android/gallery3d/app/GalleryActivity.java @@ -19,26 +19,17 @@ package com.android.gallery3d.app; -import android.Manifest; import android.app.Dialog; import android.content.ContentResolver; -import android.content.ContentUris; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.content.Intent; import android.content.UriMatcher; import android.content.pm.PackageManager; -import android.database.Cursor; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.graphics.Color; import android.net.Uri; -import android.os.Build; import android.os.Bundle; -import android.os.Handler; -import android.os.Message; -import android.provider.MediaStore; import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout.DrawerListener; import android.text.TextUtils; @@ -60,22 +51,14 @@ import android.widget.Toast; import android.widget.Toolbar; import org.codeaurora.gallery.R; -import com.android.gallery3d.common.ApiHelper.SystemProperties; import com.android.gallery3d.common.Utils; import com.android.gallery3d.data.DataManager; import com.android.gallery3d.data.MediaItem; import com.android.gallery3d.data.MediaSet; import com.android.gallery3d.data.Path; -import com.android.gallery3d.filtershow.cache.ImageLoader; -import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine; -import com.android.gallery3d.mpo.MpoParser; import com.android.gallery3d.picasasource.PicasaSource; -import com.android.gallery3d.util.Future; import com.android.gallery3d.util.GalleryUtils; -import com.android.gallery3d.util.ThreadPool.Job; -import com.android.gallery3d.util.ThreadPool.JobContext; -import java.util.ArrayList; import java.util.Locale; public final class GalleryActivity extends AbstractGalleryActivity implements OnCancelListener { @@ -142,11 +125,6 @@ public final class GalleryActivity extends AbstractGalleryActivity implements On } else { initializeByIntent(); } - - boolean ddmBulk = SystemProperties.getBoolean("persist.gallery.dualcam.ddmbulk", false); - if(ddmBulk) - startBulkMpoProcess(); - mSavedInstanceState = null; } @@ -605,7 +583,6 @@ public final class GalleryActivity extends AbstractGalleryActivity implements On @Override protected void onDestroy() { - cancelBulkMpoProcess(); super.onDestroy(); } @@ -636,152 +613,4 @@ public final class GalleryActivity extends AbstractGalleryActivity implements On } return super.onGenericMotionEvent(event); } - - private Future<?> mMpoTask; - private Toast mToast; - private String mToastStr; - private Handler mHandler = new Handler(new Handler.Callback() { - @Override - public boolean handleMessage(Message msg) { - String toastMsg = null; - - switch(msg.what) { - case 0: - startBulkMpoProcess(); - break; - case 1: - toastMsg = "MPO bulk process START"; - break; - case 2: - toastMsg = "MPO bulk process CANCELLED"; - break; - case 3: - toastMsg = "MPO bulk process DONE"; - break; - case 4: - toastMsg = "MPO bulk process FAILED"; - break; - case 5: - toastMsg = "MPO bulk processing image (" + msg.arg1 + "/" + msg.arg2 + ")"; - break; - } - - if(toastMsg != null) { - mToastStr = toastMsg; - runOnUiThread(new Runnable() { - @Override - public void run() { - if(mToast == null) { - mToast = Toast.makeText(GalleryActivity.this, mToastStr, Toast.LENGTH_SHORT); - } else { - mToast.setText(mToastStr); - } - mToast.show(); - } - }); - } - return false; - } - }); - - private void startBulkMpoProcess() { - try { - mMpoTask = getBatchServiceThreadPoolIfAvailable().submit(new BatchMpoJob(this)); - } catch (Exception e) { - mHandler.sendEmptyMessageDelayed(0, 50); - } - } - - private void cancelBulkMpoProcess() { - if(mMpoTask != null) { - mMpoTask.cancel(); - mMpoTask = null; - } - } - - private class BatchMpoJob implements Job<Void> { - private Context mContext; - private ContentResolver mContentResolver; - - public BatchMpoJob(Context context) { - mContext = context; - mContentResolver = mContext.getContentResolver(); - } - - @Override - public Void run(JobContext jc) { - mHandler.sendEmptyMessage(1); - - Cursor allPhotos = mContentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, - new String[]{MediaStore.Images.ImageColumns._ID}, null, null, null); - - if(allPhotos != null) { - try { - boolean cancelled = false; - int count = allPhotos.getCount(); - while(allPhotos.moveToNext()) { - if (jc.isCancelled()) { - cancelled = true; - break; - } - - int position = allPhotos.getPosition() + 1; - long id = allPhotos.getLong(0); - Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); - loadMpo(mContext, uri); - mHandler.sendMessage(mHandler.obtainMessage(5, position, count)); - } - - if(cancelled) - mHandler.sendEmptyMessage(2); - else - mHandler.sendEmptyMessage(3); - } finally { - allPhotos.close(); - } - } else { - mHandler.sendEmptyMessage(4); - } - return null; - } - - private boolean loadMpo(Context context, Uri uri) { - boolean loaded = false; - MpoParser parser = MpoParser.parse(context, uri); - byte[] primaryMpoData = parser.readImgData(true); - byte[] auxiliaryMpoData = parser.readImgData(false); - - if(primaryMpoData != null && auxiliaryMpoData != null) { - Bitmap primaryBm = BitmapFactory.decodeByteArray(primaryMpoData, 0, primaryMpoData.length); - primaryMpoData = null; - - if(primaryBm == null) { - return false; - } - - // check for pre-generated dm file - String mpoFilepath = ImageLoader.getLocalPathFromUri(context, uri); - // read auxiliary image and generate depth map. - Bitmap auxiliaryBm = BitmapFactory.decodeByteArray(auxiliaryMpoData, 0, auxiliaryMpoData.length); - auxiliaryMpoData = null; - - if(auxiliaryBm == null) { - primaryBm.recycle(); - primaryBm = null; - return false; - } - - DualCameraNativeEngine.getInstance().initDepthMap( - primaryBm, auxiliaryBm, mpoFilepath, - DualCameraNativeEngine.getInstance().getCalibFilepath(context), - DualCameraNativeEngine.DEFAULT_BRIGHTNESS_INTENSITY); - - primaryBm.recycle(); - primaryBm = null; - auxiliaryBm.recycle(); - auxiliaryBm = null; - } - return loaded; - } - } } diff --git a/src/com/android/gallery3d/app/PhotoPage.java b/src/com/android/gallery3d/app/PhotoPage.java index a665b1427..a33f9d621 100755 --- a/src/com/android/gallery3d/app/PhotoPage.java +++ b/src/com/android/gallery3d/app/PhotoPage.java @@ -68,8 +68,7 @@ import com.android.gallery3d.data.SnailItem; import com.android.gallery3d.data.SnailSource; import com.android.gallery3d.filtershow.FilterShowActivity; import com.android.gallery3d.filtershow.imageshow.MasterImage; -import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine; -import com.android.gallery3d.mpo.MpoParser; +import com.android.gallery3d.filtershow.tools.DualCameraEffect; import com.android.gallery3d.ui.DetailsHelper; import com.android.gallery3d.ui.DetailsHelper.CloseListener; import com.android.gallery3d.ui.DetailsHelper.DetailsSource; @@ -79,6 +78,7 @@ import com.android.gallery3d.ui.MenuExecutor; import com.android.gallery3d.ui.PhotoView; import com.android.gallery3d.ui.SelectionManager; import com.android.gallery3d.ui.SynchronizedHandler; +import com.android.gallery3d.util.GDepth; import com.android.gallery3d.util.GalleryUtils; import com.android.gallery3d.util.UsageStatistics; @@ -217,8 +217,7 @@ public abstract class PhotoPage extends ActivityState implements private ThreeDButton m3DButton; private boolean bShow3DButton; - private LoadMpoDataTask mLoadMpoTask = new LoadMpoDataTask();; - private ParseMpoDataTask mParseMpoDateTask = new ParseMpoDataTask(); + private ParseGDepthTask mParseGDepthTask; private final PanoramaSupportCallback mUpdatePanoramaMenuItemsCallback = new PanoramaSupportCallback() { @Override @@ -870,7 +869,7 @@ public abstract class PhotoPage extends ActivityState implements updateMenuOperations(); refreshBottomControlsWhenReady(); - parseMpoData(); + parseGDepth(); if (mShowDetails) { mDetailsHelper.reloadDetails(); } @@ -1465,7 +1464,7 @@ public abstract class PhotoPage extends ActivityState implements } if (null != mModel) { mModel.setCurrentPhoto(path, mCurrentIndex); - parseMpoData(); + parseGDepth(); } } } @@ -1511,20 +1510,21 @@ public abstract class PhotoPage extends ActivityState implements } if (path != null) { mModel.setCurrentPhoto(Path.fromString(path), index); - parseMpoData(); + parseGDepth(); } } } } - private void parseMpoData() { + private void parseGDepth() { bShow3DButton = false; - if (DualCameraNativeEngine.getInstance().isLibLoaded()) { - if (mParseMpoDateTask.getStatus() != AsyncTask.Status.FINISHED) { - boolean r = mParseMpoDateTask.cancel(true); + if (DualCameraEffect.isSupported()) { + if (mParseGDepthTask != null + && mParseGDepthTask.getStatus() != AsyncTask.Status.FINISHED) { + mParseGDepthTask.cancel(true); } - mParseMpoDateTask = new ParseMpoDataTask(); - mParseMpoDateTask.execute(); + mParseGDepthTask = new ParseGDepthTask(); + mParseGDepthTask.execute(mCurrentPhoto.getContentUri()); } } @@ -1821,60 +1821,17 @@ public abstract class PhotoPage extends ActivityState implements } } - public class ParseMpoDataTask extends AsyncTask<Void, Void, Void> { - private byte[] mPrimaryImgData = null; - private byte[] mAuxImgData = null; - + private class ParseGDepthTask extends AsyncTask<Uri, Void, Boolean> { @Override - protected Void doInBackground(Void... params) { - if (mCurrentPhoto == null)return null; - MpoParser parser = MpoParser.parse(mActivity, mCurrentPhoto.getContentUri()); - mPrimaryImgData = parser.readImgData(true); - mAuxImgData = parser.readImgData(false); - return null; - } - - @Override - protected void onPostExecute(Void result) { - if (isCancelled()){ - bShow3DButton = false; - m3DButton.refresh(); - return; - } - if(mPrimaryImgData == null || - mAuxImgData == null) { - // parse failed - bShow3DButton = false; - } else { - if (mLoadMpoTask.getStatus() != Status.FINISHED) { - boolean r = mLoadMpoTask.cancel(true); - } - bShow3DButton = true; - mLoadMpoTask = new LoadMpoDataTask(); - mLoadMpoTask.execute(mPrimaryImgData, mAuxImgData); - } - m3DButton.refresh(); - } - } - - private class LoadMpoDataTask extends AsyncTask<byte[], Void, Boolean> { - - @Override - protected Boolean doInBackground(byte[]... params) { - return MasterImage.getImage().loadMpo(mActivity, params[0], params[1], mCurrentPhoto.getContentUri()); + protected Boolean doInBackground(Uri... params) { + GDepth.Parser parser = new GDepth.Parser(); + return parser.parse(mActivity, params[0]); } @Override protected void onPostExecute(Boolean result) { - if (isCancelled()){ - bShow3DButton = false; - m3DButton.refresh(); - return; - } - if (!result) { - bShow3DButton = result; - m3DButton.refresh(); - } + bShow3DButton = !isCancelled() && result; + m3DButton.refresh(); } } } diff --git a/src/com/android/gallery3d/app/dualcam3d/Effect.java b/src/com/android/gallery3d/app/dualcam3d/Effect.java deleted file mode 100644 index 0f9cde4cb..000000000 --- a/src/com/android/gallery3d/app/dualcam3d/Effect.java +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Copyright (c) 2016, The Linux Foundation. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * * Neither the name of The Linux Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN - * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package com.android.gallery3d.app.dualcam3d; - -import android.graphics.Bitmap; -import android.util.Log; - -import com.android.gallery3d.app.dualcam3d.threed.Controller; -import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine; - -public class Effect implements GLView.Listener { - private static final String TAG = "Effect"; - public static final int INTENSITY_MAX = 10; - public static final float DEFAULT_BRIGHTNESS_INTENSITY = 1.0f; - private static final int PREVIEW_DIMENSION_MAX = 1080; - - enum Type { - NONE, FOCUS, HALO, SKETCH, FUSION, ZOOM, - MOTION, BW, BLACKBOARD, WHITEBOARD, - POSTERIZE, NEGATIVE, THREE_DIMENSIONAL - } - - private final ThreeDimensionalActivity mActivity; - private Bitmap mBitmap; - private Type mType = Type.NONE; - private int mX; - private int mY; - private float mIntensity = 0.5f; - private boolean mIsPreview = true; - - private int mScale; - - private Controller mController; - - private Thread mThread; - private Boolean mCancelled; - - public Effect(ThreeDimensionalActivity activity) { - mActivity = activity; - } - - public void setBitmap(Bitmap bitmap, int scale) { - mBitmap = bitmap; - scaleBitmap(scale); - } - - public static int scale(int width, int height) { - int scale = 1; - while (width / scale > PREVIEW_DIMENSION_MAX || height / scale > PREVIEW_DIMENSION_MAX) { - scale *= 2; - } - return scale > 1 ? scale : 1; - } - - private void scaleBitmap(int scale) { - if (scale != 1) { - mScale = scale; - } else if (mBitmap != null) { - int width = mBitmap.getWidth(), height = mBitmap.getHeight(); - mX = width / 2; - mY = height / 2; - mScale = scale(width, height); - if (mScale != 1) { - mBitmap = Bitmap.createScaledBitmap(mBitmap, - width / mScale, height / mScale, false); - } - } - mActivity.sendMessage(ThreeDimensionalActivity.MSG_UPDATE_IMAGE, mBitmap); - } - - public void set(Type type) { - if (type == Type.THREE_DIMENSIONAL) { - new Thread(new Runnable() { - @Override - public void run() { - DualCameraNativeEngine.DepthMap3D map = DualCameraNativeEngine.getInstance() - .getDepthMap3D(mBitmap); - if (map != null) { - Log.d(TAG, "got 3d map"); - mActivity.sendMessage(ThreeDimensionalActivity.MSG_UPDATE_IMAGE, mBitmap); - mActivity.sendMessage(ThreeDimensionalActivity.MSG_UPDATE_3D_DEPTH_MAP, map); - } else { - Log.e(TAG, "cannot get 3d map"); - } - } - }).start(); - if (mController == null) { - mController = mActivity.getController(); - } - mController.start(); - } else { - if (mController != null) { - mController.stop(true); - } - mActivity.sendMessage(ThreeDimensionalActivity.MSG_UPDATE_3D_DEPTH_MAP, null); - request(); - } - mType = type; - } - - private void setCoordination(float x, float y) { - if (x >= 0 && y >= 0 && x < mBitmap.getWidth() && y < mBitmap.getHeight()) { - mX = (int) (x * mScale); - mY = (int) (y * mScale); - } - } - - public void setIntensity(float intensity) { - if (intensity < 0) intensity = 0; - if (intensity > INTENSITY_MAX) intensity = 1; - if (intensity != mIntensity) { - mIntensity = intensity / INTENSITY_MAX; - request(); - } - } - - private synchronized void request() { - notify(); - } - - - public void recycle() { - if (mBitmap != null) { - mBitmap.recycle(); - } - } - - @Override - public void onMove(float deltaX, float deltaY) { - if (mType == Type.THREE_DIMENSIONAL && mController != null) { - mController.onMove(deltaX, deltaY); - } - } - - @Override - public void onClick(float x, float y) { - if (mType != Type.THREE_DIMENSIONAL) { - setCoordination(x, y); - request(); - } - } - - @Override - public void onLayout(int width, int height) { - } - - public void resume() { - if (mController != null && mType == Type.THREE_DIMENSIONAL) { - mController.start(); - } - mCancelled = false; - mThread = new Thread(new Runnable() { - @Override - public void run() { - while (!mCancelled) { - synchronized (Effect.this) { - try { - Effect.this.wait(); - } catch (InterruptedException ignored) { - } - } - } - } - }); - mThread.start(); - } - - public void pause() { - if (mController != null) { - mController.stop(false); - } - if (mThread != null) { - mCancelled = true; - synchronized (this) { - notify(); - } - try { - mThread.join(); - } catch (InterruptedException ignored) { - } - mThread = null; - } - } -} diff --git a/src/com/android/gallery3d/app/dualcam3d/GLView.java b/src/com/android/gallery3d/app/dualcam3d/GLView.java index 181d3e095..dc0111040 100644 --- a/src/com/android/gallery3d/app/dualcam3d/GLView.java +++ b/src/com/android/gallery3d/app/dualcam3d/GLView.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, The Linux Foundation. All rights reserved. + * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -38,14 +38,12 @@ import android.util.AttributeSet; import android.view.MotionEvent; import android.view.Surface; -import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine; - public class GLView extends GLSurfaceView { private static final String TAG = "GLView"; private final com.android.gallery3d.app.dualcam3d.gl.Renderer mRenderer; private Bitmap mBitmap; - private DualCameraNativeEngine.DepthMap3D mDepthMap; + private Bitmap mDepthMap; private int mRotation; public GLView(Context context) { @@ -64,7 +62,7 @@ public class GLView extends GLSurfaceView { super.onResume(); setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); mRenderer.setImageBitmap(mBitmap); - mRenderer.set3DEffectDepthMap(mDepthMap); + mRenderer.setDepthMap(mDepthMap); requestRender(); } @@ -72,19 +70,19 @@ public class GLView extends GLSurfaceView { public void onPause() { super.onPause(); mRenderer.setImageBitmap(null); - mRenderer.set3DEffectDepthMap(null); + mRenderer.setDepthMap(null); } public void setImageBitmap(Bitmap bitmap) { mBitmap = bitmap; mRenderer.setImageBitmap(bitmap); - mRenderer.set3DEffectDepthMap(null); + mRenderer.setDepthMap(null); requestRender(); } - public void set3DEffectDepthMap(DualCameraNativeEngine.DepthMap3D map) { + public void setDepthMap(Bitmap map) { mDepthMap = map; - mRenderer.set3DEffectDepthMap(map); + mRenderer.setDepthMap(map); requestRender(); } @@ -126,9 +124,7 @@ public class GLView extends GLSurfaceView { public interface Listener { void onMove(float deltaX, float deltaY); - void onClick(float x, float y); - void onLayout(int width, int height); } diff --git a/src/com/android/gallery3d/app/dualcam3d/ThreeDimensionalActivity.java b/src/com/android/gallery3d/app/dualcam3d/ThreeDimensionalActivity.java index 18fe36394..4a4be556f 100644 --- a/src/com/android/gallery3d/app/dualcam3d/ThreeDimensionalActivity.java +++ b/src/com/android/gallery3d/app/dualcam3d/ThreeDimensionalActivity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, The Linux Foundation. All rights reserved. + * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -31,58 +31,25 @@ package com.android.gallery3d.app.dualcam3d; import android.app.Activity; import android.content.Intent; import android.content.res.Configuration; -import android.graphics.Bitmap; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; -import android.os.Handler; -import android.os.Message; import android.util.Log; import android.view.Window; import android.widget.LinearLayout; -import com.android.gallery3d.app.dualcam3d.mpo.Task; import com.android.gallery3d.app.dualcam3d.threed.Controller; -import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine; +import com.android.gallery3d.filtershow.cache.ImageLoader; +import com.android.gallery3d.util.GDepth; import org.codeaurora.gallery.R; public class ThreeDimensionalActivity extends Activity { private static final String TAG = ThreeDimensionalActivity.class.getSimpleName(); - final static int MSG_UPDATE_IMAGE = 1; - final static int MSG_UPDATE_3D_DEPTH_MAP = 2; - private final static int MSG_IMAGE_LOADED = 3; - private final static int MSG_FINISH = 4; - - private Effect mEffect; private GLView mImageView; - private Task mTask; - - private final Handler mHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - switch (msg.what) { - case MSG_UPDATE_IMAGE: - if (mImageView != null) { - mImageView.setImageBitmap((Bitmap) msg.obj); - } - break; - case MSG_UPDATE_3D_DEPTH_MAP: - if (mImageView != null) { - mImageView.set3DEffectDepthMap((DualCameraNativeEngine.DepthMap3D) msg.obj); - } - break; - case MSG_IMAGE_LOADED: - mEffect.setBitmap((Bitmap) msg.obj, msg.arg1); - break; - case MSG_FINISH: - mEffect.set(Effect.Type.THREE_DIMENSIONAL); - break; - default: - break; - } - } - }; + private Controller mController; + private LoadImageTask mTask; @Override protected void onCreate(Bundle savedInstanceState) { @@ -90,10 +57,8 @@ public class ThreeDimensionalActivity extends Activity { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_three_dimensional); - - mEffect = new Effect(this); - processIntent(); init(); + processIntent(); } @Override @@ -104,39 +69,41 @@ public class ThreeDimensionalActivity extends Activity { private void init() { mImageView = (GLView) findViewById(R.id.image); - mImageView.setListener(mEffect); mImageView.setRotation(getWindowManager().getDefaultDisplay().getRotation()); + mController = new Controller(mImageView, (LinearLayout) findViewById(R.id.mode_3d)); + mImageView.setListener(new GLView.Listener() { + @Override + public void onMove(float deltaX, float deltaY) { + mController.onMove(deltaX, deltaY); + } + + @Override + public void onClick(float x, float y) {} + + @Override + public void onLayout(int width, int height) {} + }); } @Override protected void onResume() { super.onResume(); mImageView.onResume(); - if (mEffect != null) { - mEffect.resume(); - } + mController.start(); } @Override protected void onPause() { super.onPause(); + mController.stop(); mImageView.onPause(); - if (mEffect != null) { - mEffect.pause(); - } } @Override protected void onDestroy() { - if (DualCameraNativeEngine.getInstance().isLibLoaded()) { - DualCameraNativeEngine.getInstance().releaseDepthMap(); - } - if (mEffect != null) { - mEffect.recycle(); - mEffect = null; - } + mTask.cancel(true); + mTask = null; mImageView.recycle(); - mTask.cancel(); super.onDestroy(); } @@ -147,44 +114,39 @@ public class ThreeDimensionalActivity extends Activity { finish(); return; } - startLoadImage(uri); + mTask = new LoadImageTask(); + mTask.execute(uri); } - private void startLoadImage(Uri uri) { - if (DualCameraNativeEngine.getInstance().isLibLoaded()) { - mTask = new Task(this, uri, new Task.Listener() { - @Override - public void onBitmapLoaded(Bitmap bitmap, int scale) { - if (bitmap != null) { - mHandler.obtainMessage(MSG_IMAGE_LOADED, scale, 0, bitmap).sendToTarget(); - } else { - finish(); + private class LoadImageTask extends AsyncTask<Uri, Void, GDepth.Image> { + @Override + protected GDepth.Image doInBackground(Uri... params) { + GDepth.Parser parser = new GDepth.Parser(); + if (parser.parse(ThreeDimensionalActivity.this, params[0])) { + GDepth.Image image = parser.decode(); + if (image.valid()) { + int orientation = ImageLoader.getMetadataOrientation( + ThreeDimensionalActivity.this, params[0]); + if (orientation != ImageLoader.ORI_NORMAL) { + image.bitmap = ImageLoader.orientBitmap(image.bitmap, orientation); + image.depthMap = ImageLoader.orientBitmap(image.depthMap, orientation); } + return image; } + } + return null; + } - @Override - public int onScale(int width, int height) { - return Effect.scale(width, height); - } - - @Override - public void onFinish(boolean result) { - if (!result) { - finish(); - } else { - sendMessage(MSG_FINISH, null); - } + @Override + protected void onPostExecute(GDepth.Image image) { + if (!isCancelled()) { + if (image != null && image.valid()) { + mImageView.setImageBitmap(image.bitmap); + mImageView.setDepthMap(image.depthMap); + } else { + finish(); } - }); - mTask.start(Effect.DEFAULT_BRIGHTNESS_INTENSITY); + } } } - - public void sendMessage(int what, Object obj) { - mHandler.obtainMessage(what, obj).sendToTarget(); - } - - public Controller getController() { - return new Controller(mImageView, (LinearLayout) findViewById(R.id.mode_3d)); - } } diff --git a/src/com/android/gallery3d/app/dualcam3d/gl/Mesh.java b/src/com/android/gallery3d/app/dualcam3d/gl/Mesh.java index 72d29ad9a..cbc2fa310 100644 --- a/src/com/android/gallery3d/app/dualcam3d/gl/Mesh.java +++ b/src/com/android/gallery3d/app/dualcam3d/gl/Mesh.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, The Linux Foundation. All rights reserved. + * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -28,9 +28,11 @@ */ package com.android.gallery3d.app.dualcam3d.gl; +import android.graphics.Bitmap; import android.opengl.GLES20; +import android.util.Log; -import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine; +import com.android.gallery3d.filtershow.tools.DualCameraEffect; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -92,8 +94,7 @@ class Mesh { return ((depth + Settings.CAMERA_POSITION) * TAN_HALF_FOV); } - public void update(DualCameraNativeEngine.DepthMap3D depthMap, int width, int height, - float depth) { + public void update(Bitmap depthMap, int width, int height, float depth) { final int resolutionH = depthMap == null ? 1 : Settings.MESH_RESOLUTION_H; final int resolutionV = depthMap == null ? 1 : Settings.MESH_RESOLUTION_V; indexLength = resolutionH * resolutionV * 2 * 3; @@ -105,6 +106,15 @@ class Mesh { rewindBuffers(); + int[] pixels = null; + int depthMapWidth = 0; + if (depthMap != null) { + depthMapWidth = depthMap.getWidth(); + int depthMapHeight = depthMap.getHeight(); + pixels = new int[depthMapWidth * depthMapHeight]; + depthMap.getPixels(pixels, 0, depthMapWidth, 0, 0, depthMapWidth, depthMapHeight); + } + for (int v = 0; v <= resolutionV; ++v) { float vV = v / (float) resolutionV; float v2 = sizeV - 2 * sizeV * vV; @@ -115,7 +125,7 @@ class Mesh { int depthValue = 0; if (depthMap != null) { int x = (int) (vH * (width - 1)); - depthValue = depthMap.pixels[y * depthMap.width + x]; + depthValue = pixels[y * depthMapWidth + x] >> 24; } vertices.put(-scale + 2 * scale * vH); vertices.put(v2); diff --git a/src/com/android/gallery3d/app/dualcam3d/gl/Renderer.java b/src/com/android/gallery3d/app/dualcam3d/gl/Renderer.java index c2795f93d..3000fb1aa 100644 --- a/src/com/android/gallery3d/app/dualcam3d/gl/Renderer.java +++ b/src/com/android/gallery3d/app/dualcam3d/gl/Renderer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, The Linux Foundation. All rights reserved. + * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -35,7 +35,6 @@ import android.opengl.GLSurfaceView; import android.opengl.Matrix; import android.util.Log; -import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; @@ -49,7 +48,7 @@ public class Renderer implements GLSurfaceView.Renderer { private float mOffsetY; private Bitmap mImageBitmap; - private DualCameraNativeEngine.DepthMap3D mDepthMap; + private Bitmap mDepthMap; private boolean mImageChanged; private boolean mDepthMapChanged; @@ -72,7 +71,7 @@ public class Renderer implements GLSurfaceView.Renderer { setImageInvertMatrix(); } - public void set3DEffectDepthMap(DualCameraNativeEngine.DepthMap3D map) { + public void setDepthMap(Bitmap map) { mDepthMap = map; mDepthMapChanged = true; if (map == null) { @@ -110,8 +109,8 @@ public class Renderer implements GLSurfaceView.Renderer { mImageChanged = false; } if (mDepthMapChanged) { - int width = mDepthMap == null ? mImageBitmap.getWidth() : mDepthMap.width; - int height = mDepthMap == null ? mImageBitmap.getHeight() : mDepthMap.height; + int width = mDepthMap == null ? mImageBitmap.getWidth() : mDepthMap.getWidth(); + int height = mDepthMap == null ? mImageBitmap.getHeight() : mDepthMap.getHeight(); mMesh.update(mDepthMap, width, height, Settings.FOREGROUND_POSITION); mDepthMapChanged = false; } diff --git a/src/com/android/gallery3d/app/dualcam3d/mpo/Task.java b/src/com/android/gallery3d/app/dualcam3d/mpo/Task.java deleted file mode 100644 index d449cb82d..000000000 --- a/src/com/android/gallery3d/app/dualcam3d/mpo/Task.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * Copyright (c) 2016, The Linux Foundation. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * * Neither the name of The Linux Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN - * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package com.android.gallery3d.app.dualcam3d.mpo; - -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Point; -import android.net.Uri; -import android.util.Log; - -import com.android.gallery3d.filtershow.cache.ImageLoader; -import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine; -import com.android.gallery3d.mpo.MpoParser; - -import java.io.OutputStream; - -public class Task { - private static final String TAG = "ParseMpoTask"; - - public interface Listener { - void onBitmapLoaded(Bitmap bitmap, int scale); - - int onScale(int width, int height); - - void onFinish(boolean result); - } - - private final Context mContext; - private final Uri mUri; - private final Listener mListener; - private boolean mCancelled; - - private Thread mThread1; - private Thread mThread2; - - public Task(final Context context, final Uri uri, final Listener listener) { - mContext = context; - mUri = uri; - mListener = listener; - mCancelled = false; - } - - public void start(final float brIntensity) { - mThread1 = new Thread(new Runnable() { - @Override - public void run() { - boolean result = parse(brIntensity); - mListener.onFinish(result); - } - }); - mThread1.start(); - } - - public void cancel() { - mCancelled = true; - try { - if (mThread1 != null) { - mThread1.join(); - } - if (mThread2 != null) { - mThread2.join(); - } - } catch (InterruptedException ignored) { - } - } - - private boolean parse(final float brIntensity) { - final Bitmap[] bitmaps = new Bitmap[2]; - final byte[][] data = new byte[2][]; - - MpoParser parser = MpoParser.parse(mContext, mUri); - if (mCancelled) return false; - final boolean primaryForDisplay = parser.isPrimaryForDisplay(); - if (!primaryForDisplay) { - decodeDisplayImg(parser); - try { - mThread2.join(); - mThread2 = null; - } catch (InterruptedException ignored) { - } - } - - final Runnable runnable = new Runnable() { - @Override - public void run() { - bitmaps[0] = BitmapFactory.decodeByteArray(data[0], 0, data[0].length); - data[0] = null; - if (!mCancelled && primaryForDisplay && brIntensity == 0) { - mListener.onBitmapLoaded(bitmaps[0], 1); - } - } - }; - data[0] = parser.readImgData(true); - if (mCancelled || data[0] == null) return false; - if (primaryForDisplay) { - mThread2 = new Thread(runnable); - mThread2.start(); - } else { - runnable.run(); - } - - data[1] = parser.readImgData(false); - if (mCancelled || data[1] == null) return false; - bitmaps[1] = BitmapFactory.decodeByteArray(data[1], 0, data[1].length); - data[1] = null; - if (bitmaps[1] == null) return false; - - if (primaryForDisplay) { - try { - mThread2.join(); - mThread2 = null; - } catch (InterruptedException ignored) { - } - } - return loadDepthMap(bitmaps, brIntensity, primaryForDisplay); - } - - private void decodeDisplayImg(final MpoParser parser) { - mThread2 = new Thread(new Runnable() { - @Override - public void run() { - BitmapFactory.Options o = new BitmapFactory.Options(); - o.inJustDecodeBounds = true; - byte[] data = parser.readImgData(0); - if (data == null && !mCancelled) { - mListener.onBitmapLoaded(null, 1); - return; - } - if (mCancelled) return; - BitmapFactory.decodeByteArray(data, 0, data.length, o); - o.inSampleSize = mListener.onScale(o.outWidth, o.outHeight); - o.inJustDecodeBounds = false; - if (mCancelled) return; - Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, o); - if (mCancelled) return; - mListener.onBitmapLoaded(bitmap, o.inSampleSize); - } - }); - mThread2.start(); - } - - private boolean loadDepthMap(Bitmap[] bitmaps, float brIntensity, boolean primaryForDisplay) { - // check for pre-generated dm file - String mpoFilepath = ImageLoader.getLocalPathFromUri(mContext, mUri); - if (mpoFilepath == null) { - Log.d(TAG, "Could not get file path from " + mUri); - return false; - } - DualCameraNativeEngine engine = DualCameraNativeEngine.getInstance(); - - if (mCancelled) return false; - boolean ok = engine.initDepthMap(bitmaps[0], bitmaps[1], mpoFilepath, - engine.getCalibFilepath(mContext), brIntensity); - bitmaps[1].recycle(); - if (!ok) return false; - - Point size = new Point(); - ok = engine.getDepthMapSize(size); - if (ok) { - Log.d(TAG, "ddm size: " + size.x + "x" + size.y); - if (size.x == 0 || size.y == 0) { - Log.w(TAG, "invalid ddm size: " + size.x + "x" + size.y); - } else { - Bitmap depthMap = Bitmap.createBitmap(size.x, size.y, Bitmap.Config.ALPHA_8); - if (engine.getDepthMap(depthMap)) { - if (!mCancelled && brIntensity != 0 && primaryForDisplay) { - scaleBitmap(bitmaps[0]); - } - return true; - } else { - Log.w(TAG, "getDepthMap returned false"); - } - } - } else { - Log.w(TAG, "getDepthMapSize returned false"); - } - return false; - } - - private void save(Bitmap bitmap, String name) { - try { - OutputStream os = mContext.openFileOutput(name, Context.MODE_PRIVATE); - bitmap.compress(Bitmap.CompressFormat.JPEG, 95, os); - os.close(); - } catch (Exception ignored) { - } - } - - private void scaleBitmap(Bitmap bitmap) { - int width = bitmap.getWidth(), height = bitmap.getHeight(); - int scale = mListener.onScale(width, height); - width /= scale; - height /= scale; - - Bitmap b = Bitmap.createScaledBitmap(bitmap, width, height, false); - int[] roi = new int[4]; - boolean result = DualCameraNativeEngine.getInstance().getPrimaryImg(0, 0, roi, true, b); - if (result && roi[2] != width && roi[3] != height) { - b = Bitmap.createBitmap(b, roi[0], roi[1], roi[2], roi[3]); - } - mListener.onBitmapLoaded(b, scale); - } -} diff --git a/src/com/android/gallery3d/app/dualcam3d/threed/Controller.java b/src/com/android/gallery3d/app/dualcam3d/threed/Controller.java index ff86d2bf5..a3c6fdacf 100644 --- a/src/com/android/gallery3d/app/dualcam3d/threed/Controller.java +++ b/src/com/android/gallery3d/app/dualcam3d/threed/Controller.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, The Linux Foundation. All rights reserved. + * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -30,7 +30,6 @@ package com.android.gallery3d.app.dualcam3d.threed; import android.view.View; -import android.widget.Button; import android.widget.ImageButton; import android.widget.LinearLayout; @@ -39,7 +38,6 @@ import com.android.gallery3d.app.dualcam3d.gl.Renderer; import org.codeaurora.gallery.R; - public class Controller implements Gyro.Listener { private static final String TAG = "Controller"; private static final float ANGLE_PER_PIXEL = (float) Math.toRadians(0.03f); @@ -52,24 +50,21 @@ public class Controller implements Gyro.Listener { public Controller(GLView glView, LinearLayout modeView) { mGLView = glView; mModeView = modeView; - View.OnClickListener listener = new View.OnClickListener() { - @Override - public void onClick(View v) { - int id = v.getId(); - switch (id) { - case R.id.mode_gyro: - startGyro(); - break; - case R.id.mode_auto: - startAuto(); - break; - case R.id.mode_touch: - stop(false); - break; - case R.id.three_dimensional: - start(); - break; - } + View.OnClickListener listener = v -> { + int id = v.getId(); + switch (id) { + case R.id.mode_gyro: + startGyro(); + break; + case R.id.mode_auto: + startAuto(); + break; + case R.id.mode_touch: + stop(); + break; + case R.id.three_dimensional: + start(); + break; } }; for (int i = modeView.getChildCount() - 1; i >= 0; --i) { @@ -79,7 +74,7 @@ public class Controller implements Gyro.Listener { } private boolean startGyro() { - stop(false); + stop(); if (mGyro == null) { mGyro = new Gyro(mGLView.getContext()); if (mGyro.start()) { @@ -94,7 +89,7 @@ public class Controller implements Gyro.Listener { } private void startAuto() { - stop(false); + stop(); if (mAuto == null) { mAuto = new Auto(); mAuto.start(mGLView); @@ -105,10 +100,9 @@ public class Controller implements Gyro.Listener { if (!startGyro()) { startAuto(); } -// mModeView.setVisibility(View.VISIBLE); } - public void stop(boolean hide) { + public void stop() { if (mGyro != null) { mGyro.stop(); mGyro = null; @@ -117,9 +111,6 @@ public class Controller implements Gyro.Listener { mAuto.stop(); mAuto = null; } -// if (hide) { -// mModeView.setVisibility(View.INVISIBLE); -// } } @Override @@ -128,7 +119,7 @@ public class Controller implements Gyro.Listener { } public void onMove(float deltaX, float deltaY) { - stop(false); + stop(); mGLView.setOffsetDelta(deltaX * ANGLE_PER_PIXEL, deltaY * ANGLE_PER_PIXEL); } @@ -138,29 +129,26 @@ public class Controller implements Gyro.Listener { public void start(final GLView glView) { mStop = false; - mThread = new Thread(new Runnable() { - @Override - public void run() { - float x = 0, y = 0, speed = 0.003f, angle = 0.87f; - double deltaX = angle * speed; - double deltaY = Math.sqrt(speed * speed - deltaX * deltaX); - - while (!mStop) { - x += deltaX; - y += deltaY; - glView.setOffset(x, y); - - if (x >= Renderer.THETA_MAX || x <= -Renderer.THETA_MAX) { - deltaX = -deltaX; - } - if (y >= Renderer.THETA_MAX || y <= -Renderer.THETA_MAX) { - deltaY = -deltaY; - } - try { - Thread.sleep(15); - } catch (InterruptedException e) { - // ignore - } + mThread = new Thread(() -> { + float x = 0, y = 0, speed = 0.003f, angle = 0.87f; + double deltaX = angle * speed; + double deltaY = Math.sqrt(speed * speed - deltaX * deltaX); + + while (!mStop) { + x += deltaX; + y += deltaY; + glView.setOffset(x, y); + + if (x >= Renderer.THETA_MAX || x <= -Renderer.THETA_MAX) { + deltaX = -deltaX; + } + if (y >= Renderer.THETA_MAX || y <= -Renderer.THETA_MAX) { + deltaY = -deltaY; + } + try { + Thread.sleep(15); + } catch (InterruptedException e) { + // ignore } } }); |