Ejemplo n.º 1
0
        /// <summary>
        /// Triggers rendering.
        /// </summary>
        public void TriggerRendering(bool force)
        {
            if (this.SurfaceWidth == 0)
            { // nothing to render yet!
                return;
            }

            // create the view that would be use for rendering.
            View2D view = _cacheRenderer.Create((int)(this.SurfaceWidth * _extra), (int)(this.SurfaceHeight * _extra),
                                                this.Map, (float)this.Map.Projection.ToZoomFactor(this.MapZoom),
                                                this.MapCenter, _invertX, _invertY, this.MapTilt);

            // ... and compare to the previous rendered view.
            if (!force && _previouslyRenderedView != null &&
                view.Equals(_previouslyRenderedView))
            {
                return;
            }
            _previouslyRenderedView = view;

            // end existing rendering thread.
            if (_renderingThread != null &&
                _renderingThread.IsAlive)
            {
                if (_cacheRenderer.IsRunning)
                {
                    this.Map.ViewChangedCancel();
                    _cacheRenderer.CancelAndWait();
                }
            }

            // start new rendering thread.
            _renderingThread = new Thread(new ThreadStart(Render));
            _renderingThread.Start();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Notifies change
        /// </summary>
        internal void TriggerRendering()
        {
            if (_rect.Width == 0)
            {
                return;
            }

            if (Monitor.TryEnter(_cacheRenderer, 1000))
            {             // entered the exclusive lock area.
                try
                {
                    // create the view that would be use for rendering.
                    float  size = System.Math.Max(_rect.Width, _rect.Height);
                    View2D view = _cacheRenderer.Create((int)(size * _extra), (int)(size * _extra),
                                                        this.Map, (float)this.Map.Projection.ToZoomFactor(this.MapZoom),
                                                        this.MapCenter, _invertX, _invertY, this.MapTilt);

                    // ... and compare to the previous rendered view.
                    if (_previouslyRenderedView != null &&
                        view.Equals(_previouslyRenderedView))
                    {
                        _listener.NotifyRenderSuccess(view, this.MapZoom, 0);
                        return;
                    }
                    _previouslyRenderedView = view;

                    // end existing rendering thread.
                    if (_renderingThread != null &&
                        _renderingThread.IsAlive)
                    {
                        if (_cacheRenderer.IsRunning)
                        {
                            _cacheRenderer.CancelAndWait();
                        }
                    }

                    // start new rendering thread.
                    _renderingThread = new Thread(new ThreadStart(Render));
                    _renderingThread.Start();
                }
                finally
                {
                    Monitor.Exit(_cacheRenderer);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Triggers rendering.
        /// </summary>
        public void TriggerRendering(bool force)
        {
            try
            {
                if (this.SurfaceWidth == 0)
                { // nothing to render yet!
                    return;
                }

                // create the view that would be use for rendering.
                View2D view = _cacheRenderer.Create((int)(this.SurfaceWidth * _extra), (int)(this.SurfaceHeight * _extra),
                                                    this.Map, (float)this.Map.Projection.ToZoomFactor(this.MapZoom),
                                                    this.MapCenter, _invertX, _invertY, this.MapTilt);

                // ... and compare to the previous rendered view.
                if (!force && _previouslyRenderedView != null &&
                    view.Equals(_previouslyRenderedView))
                {
                    return;
                }
                _previouslyRenderedView = view;

                // end existing rendering thread.
                if (_renderingThread != null &&
                    _renderingThread.IsAlive)
                {
                    if (_cacheRenderer.IsRunning)
                    {
                        this.Map.ViewChangedCancel();
                        _cacheRenderer.CancelAndWait();
                    }
                }

                // start new rendering thread.
                _renderingThread = new Thread(new ThreadStart(Render));
                _renderingThread.Start();
            }
            catch (Exception ex)
            {
                OsmSharp.Logging.Log.TraceEvent("MapViewSurface", TraceEventType.Critical,
                                                string.Format("An unhandled exception occured:{0}", ex.ToString()));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Notifies change
        /// </summary>
        /// <param name="touch">If set to <c>true</c> change was trigger by touch.</param>
        internal void Change(bool touch)
        {
            if (_rect.Width == 0)
            {
                return;
            }

            lock (_cacheRenderer) {
                // create the view that would be use for rendering.
                View2D view = _cacheRenderer.Create((int)(_rect.Width * _extra), (int)(_rect.Height * _extra),
                                                    this.Map, (float)this.Map.Projection.ToZoomFactor(this.MapZoom),
                                                    this.MapCenter, _invertX, _invertY, this.MapTilt);

                // ... and compare to the previous rendered view.
                if (_previousRenderedZoom != null &&
                    view.Equals(_previousRenderedZoom))
                {
                    return;
                }
                _previousRenderedZoom = view;

                // end existing rendering thread.
                if (_renderingThread != null &&
                    _renderingThread.IsAlive)
                {
                    if (_cacheRenderer.IsRunning)
                    {
                        _cacheRenderer.CancelAndWait();
                    }

                    _renderingThread.Abort();
                }

                // start new rendering thread.
                _renderingThread = new Thread(new ThreadStart(Render));
                _renderingThread.Start();

                // raise touched event.
                if (touch)
                {
                    this.RaiseMapTouched();
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Render the current complete scene.
        /// </summary>
        void Render()
        {
            if (_cacheRenderer.IsRunning)
            {
                _cacheRenderer.CancelAndWait();
            }

            if (_rect == null)               // only render if a proper size is known.
            {
                return;
            }

            lock (_cacheRenderer) {             // make sure only on thread at the same time is using the renderer.
                double extra = 1.25;

                // build the layers list.
                var layers = new List <ILayer> ();
                for (int layerIdx = 0; layerIdx < this.Map.LayerCount; layerIdx++)
                {
                    layers.Add(this.Map [layerIdx]);
                }

                // add the internal layer.
                // TODO: create marker layer.

                // create a new bitmap context.
                CGColorSpace space            = CGColorSpace.CreateDeviceRGB();
                int          bytesPerPixel    = 4;
                int          bytesPerRow      = bytesPerPixel * (int)_rect.Width;
                int          bitsPerComponent = 8;
                if (_bytescache == null)
                {
                    _bytescache = new byte[bytesPerRow * (int)_rect.Height];
                }
                CGBitmapContext gctx = new CGBitmapContext(null, (int)_rect.Width, (int)_rect.Height,
                                                           bitsPerComponent, bytesPerRow,
                                                           space,                  // kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast
                                                           CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Big);

                // create the view.
                View2D view = _cacheRenderer.Create(_rect.Width, _rect.Height,
                                                    this.Map, (float)this.Map.Projection.ToZoomFactor(this.MapZoomLevel),
                                                    this.MapCenter, false, true);
                long before = DateTime.Now.Ticks;
                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information,
                                                "Rendering Start");

                // notify the map that the view has changed.
                this.Map.ViewChanged((float)this.Map.Projection.ToZoomFactor(this.MapZoomLevel), this.MapCenter,
                                     view);
                long afterViewChanged = DateTime.Now.Ticks;
                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information,
                                                "View change took: {0}ms @ zoom level {1}",
                                                (new TimeSpan(afterViewChanged - before).TotalMilliseconds), this.MapZoomLevel);

//				// add the current canvas to the scene.
//				uint canvasId = _scene.AddImage (-1, float.MinValue, float.MaxValue,
//				                                 view.Left, view.Top, view.Right, view.Bottom, new byte[0], _canvasBitmap);

                // does the rendering.
                bool complete = _cacheRenderer.Render(new CGContextWrapper(gctx, new RectangleF(0, 0, _rect.Width, _rect.Height)),
                                                      layers, view);

                long afterRendering = DateTime.Now.Ticks;
                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information,
                                                "Rendering took: {0}ms @ zoom level {1}",
                                                (new TimeSpan(afterRendering - afterViewChanged).TotalMilliseconds), this.MapZoomLevel);
                if (complete)                   // there was no cancellation, the rendering completely finished.
                // add the result to the scene cache.
                {
                    lock (_cachedScene) {
                        // add the newly rendered image again.
                        //this.Layer.Contents = gctx.ToImage ();

                        BoxF2D rectangle = view.OuterBox;

                        _cachedScene.Clear();
                        _cachedScene.AddImage(0, float.MinValue, float.MaxValue,
                                              rectangle.Min[0], rectangle.Min[1], rectangle.Max[0], rectangle.Max[1], new byte[0], gctx.ToImage());
//						_cachedScene.AddImage (0, float.MinValue, float.MaxValue,
//						                       view.Left, view.Top, view.Right, view.Bottom, new byte[0], _layer);
                    }

                    this.InvokeOnMainThread(Test);
                }

                long after = DateTime.Now.Ticks;
                if (!complete)
                {
                    OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information, "Rendering in {0}ms after cancellation!",
                                                    new TimeSpan(after - before).TotalMilliseconds);
                }
                else
                {
                    OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information, "Rendering in {0}ms",
                                                    new TimeSpan(after - before).TotalMilliseconds);
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Renders the current complete scene.
        /// </summary>
        void Render()
        {
            if (_cacheRenderer.IsRunning)
            {
                _cacheRenderer.CancelAndWait();
            }

            // make sure only on thread at the same time is using the renderer.
            lock (_cacheRenderer) {
                double extra = 1.25;

                // build the layers list.
                var layers = new List <ILayer> ();
                for (int layerIdx = 0; layerIdx < this.Map.LayerCount; layerIdx++)
                {
                    // get the layer.
                    layers.Add(this.Map[layerIdx]);
                }

                // add the internal layers.
                layers.Add(_makerLayer);

                // create a new cache if size has changed.
                if (_canvasBitmap == null ||
                    _canvasBitmap.Width != (int)(this.Width * extra) ||
                    _canvasBitmap.Height != (int)(this.Height * extra))
                {
                    // create a bitmap and render there.
                    _canvasBitmap = global::Android.Graphics.Bitmap.CreateBitmap((int)(this.Width * extra),
                                                                                 (int)(this.Height * extra),
                                                                                 global::Android.Graphics.Bitmap.Config.Argb8888);
                }
                else
                {
                    // clear the cache???
                }

                // create and reset the canvas.
                global::Android.Graphics.Canvas canvas = new global::Android.Graphics.Canvas(_canvasBitmap);
                canvas.DrawColor(new global::Android.Graphics.Color(
                                     SimpleColor.FromKnownColor(KnownColor.Transparent).Value));

                // create the view.
                View2D view = _cacheRenderer.Create(canvas.Width, canvas.Height,
                                                    this.Map, (float)this.Map.Projection.ToZoomFactor(this.MapZoomLevel),
                                                    this.MapCenter, _invertX, _invertY);
                long before = DateTime.Now.Ticks;

                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information,
                                                "Rendering Start");

                // notify the map that the view has changed.
                this.Map.ViewChanged((float)this.Map.Projection.ToZoomFactor(this.MapZoomLevel), this.MapCenter,
                                     view);
                long afterViewChanged = DateTime.Now.Ticks;
                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information,
                                                "View change took: {0}ms @ zoom level {1}",
                                                (new TimeSpan(afterViewChanged - before).TotalMilliseconds), this.MapZoomLevel);

                // add the current canvas to the scene.
                double left     = view.LeftTop [0];
                double right    = view.RightTop [0];
                double top      = view.LeftTop [1];
                double bottom   = view.LeftBottom [1];
                uint   canvasId = _scene.AddImage(-1, float.MinValue, float.MaxValue,
                                                  left, top, right, bottom, new byte[0], _canvasBitmap);

                // does the rendering.
                bool complete = _cacheRenderer.Render(canvas, layers, view);

                long afterRendering = DateTime.Now.Ticks;
                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information,
                                                "Rendering took: {0}ms @ zoom level {1}",
                                                (new TimeSpan(afterRendering - afterViewChanged).TotalMilliseconds), this.MapZoomLevel);
                if (complete)
                {                 // there was no cancellation, the rendering completely finished.
                    // add the result to the scene cache.
                    lock (_scene) {
//						if (_previousCache.HasValue) {
//							_scene.Remove (_previousCache.Value);
//						}
//						_scene.Remove (canvasId);
//
                        // add the newly rendered image again.
                        _scene.Clear();
                        //_previousCache =
                        BoxF2D viewBox = view.OuterBox;
                        _scene.AddImage(0, float.MinValue, float.MaxValue,
                                        viewBox.Min[0], viewBox.Min[1], viewBox.Max[0], viewBox.Max[1], new byte[0], _canvasBitmap);

                        // switch cache and canvas to prevent re-allocation of bitmaps.
                        global::Android.Graphics.Bitmap newCanvas = _cache;
                        _cache        = _canvasBitmap;
                        _canvasBitmap = newCanvas;
                    }
                }

                this.PostInvalidate();

                long after = DateTime.Now.Ticks;
                if (!complete)
                {
                    OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information, "Rendering in {0}ms after cancellation!",
                                                    new TimeSpan(after - before).TotalMilliseconds);
                }
                else
                {
                    OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information, "Rendering in {0}ms",
                                                    new TimeSpan(after - before).TotalMilliseconds);
                }
            }
        }