Beispiel #1
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);
                }
            }
        }
 /// <summary>
 /// Clears all data from this layer.
 /// </summary>
 public void Clear()
 {
     _scene.Clear();
 }
Beispiel #3
0
        /// <summary>
        /// Removes all objects from this layer.
        /// </summary>
        public void Clear()
        {
            _scene.Clear();

            this.RaiseLayerChanged();
        }
        /// <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);
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Render the current complete scene.
        /// </summary>
        void Render()
        {
            try {
                RectangleF rect = _rect;

                lock (this.Map) {
                    //lock (_cacheRenderer) { // make sure only on thread at the same time is using the renderer.
                    // create the view.
                    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);

                    if (view.Equals(_previousRenderedZoom))
                    {
                        return;
                    }

                    if (rect.Width == 0)               // only render if a proper size is known.
                    {
                        return;
                    }

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

                    long before = DateTime.Now.Ticks;
                    //				OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information,
                    //				                                "Rendering Start");

                    // 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.
                    int imageWidth  = (int)(rect.Width * _extra * _scaleFactor);
                    int imageHeight = (int)(rect.Height * _extra * _scaleFactor);

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

                    // notify the map that the view has changed.
                    this.Map.ViewChanged((float)this.Map.Projection.ToZoomFactor(this.MapZoom), 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.MapZoom);

                    // does the rendering.
                    bool complete = _cacheRenderer.Render(new CGContextWrapper(gctx,
                                                                               new RectangleF(0, 0, (int)(rect.Width * _extra), (int)(rect.Height * _extra))),
                                                          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.MapZoom);

                    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.
                            _cachedScene.Clear();
                            _cachedScene.AddImage(0, float.MinValue, float.MaxValue, view.Rectangle, new byte[0], gctx.ToImage());
                        }
                        this.InvokeOnMainThread(InvalidateMap);

                        // store the previous view.
                        _previousRenderedZoom = view;
                    }

                    long after = DateTime.Now.Ticks;
                    //				if (!complete) {
                    //					OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information,"Rendering CANCELLED!",
                    //					                                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);
                    //				}
                    //}
                }
            }
            catch (Exception ex) {
                _cacheRenderer.Reset();
            }
        }