# all files / ol/renderer/webgl/ imagelayer.js

28.24% Statements 37/131
0% Branches 0/43
16.67% Functions 2/12
28.24% Lines 37/131

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    3341×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×
    1×

    1×
    1×
    1×

    1×
    1×

    1×

    1×

    1×

    1×

    1×

     goog.provide('ol.renderer.webgl.ImageLayer');

    goog.require('ol');
    goog.require('ol.LayerType');
    goog.require('ol.ViewHint');
    goog.require('ol.dom');
    goog.require('ol.extent');
    goog.require('ol.functions');
    goog.require('ol.renderer.Type');
    goog.require('ol.renderer.webgl.Layer');
    goog.require('ol.source.ImageVector');
    goog.require('ol.transform');
    goog.require('ol.webgl');
    goog.require('ol.webgl.Context');

    /**
     * @constructor
     * @extends {ol.renderer.webgl.Layer}
     * @param {ol.renderer.webgl.Map} mapRenderer Map renderer.
     * @param {ol.layer.Image} imageLayer Tile layer.
     * @api
     */
    ol.renderer.webgl.ImageLayer = function(mapRenderer, imageLayer) {

      ol.renderer.webgl.Layer.call(this, mapRenderer, imageLayer);

      /**
       * The last rendered image.
       * @private
       * @type {?ol.ImageBase}
       */
      this.image_ = null;

      /**
       * @private
       * @type {CanvasRenderingContext2D}
       */
      this.hitCanvasContext_ = null;

      /**
       * @private
       * @type {?ol.Transform}
       */
      this.hitTransformationMatrix_ = null;

    };
    ol.inherits(ol.renderer.webgl.ImageLayer, ol.renderer.webgl.Layer);

    /**
     * Determine if this renderer handles the provided layer.
     * @param {ol.renderer.Type} type The renderer type.
     * @param {ol.layer.Layer} layer The candidate layer.
     * @return {boolean} The renderer can render the layer.
     */
    ol.renderer.webgl.ImageLayer['handles'] = function(type, layer) {
      return type === ol.renderer.Type.WEBGL && layer.getType() === ol.LayerType.IMAGE;
    };

    /**
     * Create a layer renderer.
     * @param {ol.renderer.Map} mapRenderer The map renderer.
     * @param {ol.layer.Layer} layer The layer to be rendererd.
     * @return {ol.renderer.webgl.ImageLayer} The layer renderer.
     */
    ol.renderer.webgl.ImageLayer['create'] = function(mapRenderer, layer) {
      return new ol.renderer.webgl.ImageLayer(
          /** @type {ol.renderer.webgl.Map} */ (mapRenderer),
          /** @type {ol.layer.Image} */ (layer)
      );
    };

    /**
     * @param {ol.ImageBase} image Image.
     * @private
     * @return {WebGLTexture} Texture.
     */
    ol.renderer.webgl.ImageLayer.prototype.createTexture_ = function(image) {

      // We meet the conditions to work with non-power of two textures.
      // http://www.khronos.org/webgl/wiki/WebGL_and_OpenGL_Differences#Non-Power_of_Two_Texture_Support
      // http://learningwebgl.com/blog/?p=2101

      var imageElement = image.getImage();
      var gl = this.mapRenderer.getGL();

      return ol.webgl.Context.createTexture(
          gl, imageElement, ol.webgl.CLAMP_TO_EDGE, ol.webgl.CLAMP_TO_EDGE);
    };

    /**
     * @inheritDoc
     */
    ol.renderer.webgl.ImageLayer.prototype.forEachFeatureAtCoordinate = function(coordinate, frameState, hitTolerance, callback, thisArg) {
      var layer = this.getLayer();
      var source = layer.getSource();
      var resolution = frameState.viewState.resolution;
      var rotation = frameState.viewState.rotation;
      var skippedFeatureUids = frameState.skippedFeatureUids;
      return source.forEachFeatureAtCoordinate(
          coordinate, resolution, rotation, hitTolerance, skippedFeatureUids,

          /**
           * @param {ol.Feature|ol.render.Feature} feature Feature.
           * @return {?} Callback result.
           */
          function(feature) {
            return callback.call(thisArg, feature, layer);
          });
    };

    /**
     * @inheritDoc
     */
    ol.renderer.webgl.ImageLayer.prototype.prepareFrame = function(frameState, layerState, context) {

      var gl = this.mapRenderer.getGL();

      var pixelRatio = frameState.pixelRatio;
      var viewState = frameState.viewState;
      var viewCenter = viewState.center;
      var viewResolution = viewState.resolution;
      var viewRotation = viewState.rotation;

      var image = this.image_;
      var texture = this.texture;
      var imageLayer = /** @type {ol.layer.Image} */ (this.getLayer());
      var imageSource = imageLayer.getSource();

      var hints = frameState.viewHints;

      var renderedExtent = frameState.extent;
      if (layerState.extent !== undefined) {
        renderedExtent = ol.extent.getIntersection(
            renderedExtent, layerState.extent);
      }
      if (!hints[ol.ViewHint.ANIMATING] && !hints[ol.ViewHint.INTERACTING] &&
          !ol.extent.isEmpty(renderedExtent)) {
        var projection = viewState.projection;
        if (!ol.ENABLE_RASTER_REPROJECTION) {
          var sourceProjection = imageSource.getProjection();
          if (sourceProjection) {
            projection = sourceProjection;
          }
        }
        var image_ = imageSource.getImage(renderedExtent, viewResolution,
            pixelRatio, projection);
        if (image_) {
          var loaded = this.loadImage(image_);
          if (loaded) {
            image = image_;
            texture = this.createTexture_(image_);
            if (this.texture) {
              /**
               * @param {WebGLRenderingContext} gl GL.
               * @param {WebGLTexture} texture Texture.
               */
              var postRenderFunction = function(gl, texture) {
                if (!gl.isContextLost()) {
                  gl.deleteTexture(texture);
                }
              }.bind(null, gl, this.texture);
              frameState.postRenderFunctions.push(
                  /** @type {ol.PostRenderFunction} */ (postRenderFunction)
              );
            }
          }
        }
      }

      if (image) {
        var canvas = this.mapRenderer.getContext().getCanvas();

        this.updateProjectionMatrix_(canvas.width, canvas.height,
            pixelRatio, viewCenter, viewResolution, viewRotation,
            image.getExtent());
        this.hitTransformationMatrix_ = null;

        // Translate and scale to flip the Y coord.
        var texCoordMatrix = this.texCoordMatrix;
        ol.transform.reset(texCoordMatrix);
        ol.transform.scale(texCoordMatrix, 1, -1);
        ol.transform.translate(texCoordMatrix, 0, -1);

        this.image_ = image;
        this.texture = texture;

        this.updateAttributions(frameState.attributions, image.getAttributions());
        this.updateLogos(frameState, imageSource);
      }

      return !!image;
    };

    /**
     * @param {number} canvasWidth Canvas width.
     * @param {number} canvasHeight Canvas height.
     * @param {number} pixelRatio Pixel ratio.
     * @param {ol.Coordinate} viewCenter View center.
     * @param {number} viewResolution View resolution.
     * @param {number} viewRotation View rotation.
     * @param {ol.Extent} imageExtent Image extent.
     * @private
     */
    ol.renderer.webgl.ImageLayer.prototype.updateProjectionMatrix_ = function(canvasWidth, canvasHeight, pixelRatio,
        viewCenter, viewResolution, viewRotation, imageExtent) {

      var canvasExtentWidth = canvasWidth * viewResolution;
      var canvasExtentHeight = canvasHeight * viewResolution;

      var projectionMatrix = this.projectionMatrix;
      ol.transform.reset(projectionMatrix);
      ol.transform.scale(projectionMatrix,
          pixelRatio * 2 / canvasExtentWidth,
          pixelRatio * 2 / canvasExtentHeight);
      ol.transform.rotate(projectionMatrix, -viewRotation);
      ol.transform.translate(projectionMatrix,
          imageExtent[0] - viewCenter[0],
          imageExtent[1] - viewCenter[1]);
      ol.transform.scale(projectionMatrix,
          (imageExtent[2] - imageExtent[0]) / 2,
          (imageExtent[3] - imageExtent[1]) / 2);
      ol.transform.translate(projectionMatrix, 1, 1);

    };

    /**
     * @inheritDoc
     */
    ol.renderer.webgl.ImageLayer.prototype.hasFeatureAtCoordinate = function(coordinate, frameState) {
      var hasFeature = this.forEachFeatureAtCoordinate(
          coordinate, frameState, 0, ol.functions.TRUE, this);
      return hasFeature !== undefined;
    };

    /**
     * @inheritDoc
     */
    ol.renderer.webgl.ImageLayer.prototype.forEachLayerAtPixel = function(pixel, frameState, callback, thisArg) {
      if (!this.image_ || !this.image_.getImage()) {
        return undefined;
      }

      if (this.getLayer().getSource() instanceof ol.source.ImageVector) {
        // for ImageVector sources use the original hit-detection logic,
        // so that for example also transparent polygons are detected
        var coordinate = ol.transform.apply(
            frameState.pixelToCoordinateTransform, pixel.slice());
        var hasFeature = this.forEachFeatureAtCoordinate(
            coordinate, frameState, 0, ol.functions.TRUE, this);

        if (hasFeature) {
          return callback.call(thisArg, this.getLayer(), null);
        } else {
          return undefined;
        }
      } else {
        var imageSize =
            [this.image_.getImage().width, this.image_.getImage().height];

        if (!this.hitTransformationMatrix_) {
          this.hitTransformationMatrix_ = this.getHitTransformationMatrix_(
              frameState.size, imageSize);
        }

        var pixelOnFrameBuffer = ol.transform.apply(
            this.hitTransformationMatrix_, pixel.slice());

        if (pixelOnFrameBuffer[0] < 0 || pixelOnFrameBuffer[0] > imageSize[0] ||
            pixelOnFrameBuffer[1] < 0 || pixelOnFrameBuffer[1] > imageSize[1]) {
          // outside the image, no need to check
          return undefined;
        }

        if (!this.hitCanvasContext_) {
          this.hitCanvasContext_ = ol.dom.createCanvasContext2D(1, 1);
        }

        this.hitCanvasContext_.clearRect(0, 0, 1, 1);
        this.hitCanvasContext_.drawImage(this.image_.getImage(),
            pixelOnFrameBuffer[0], pixelOnFrameBuffer[1], 1, 1, 0, 0, 1, 1);

        var imageData = this.hitCanvasContext_.getImageData(0, 0, 1, 1).data;
        if (imageData[3] > 0) {
          return callback.call(thisArg, this.getLayer(),  imageData);
        } else {
          return undefined;
        }
      }
    };

    /**
     * The transformation matrix to get the pixel on the image for a
     * pixel on the map.
     * @param {ol.Size} mapSize The map size.
     * @param {ol.Size} imageSize The image size.
     * @return {ol.Transform} The transformation matrix.
     * @private
     */
    ol.renderer.webgl.ImageLayer.prototype.getHitTransformationMatrix_ = function(mapSize, imageSize) {
      // the first matrix takes a map pixel, flips the y-axis and scales to
      // a range between -1 ... 1
      var mapCoordTransform = ol.transform.create();
      ol.transform.translate(mapCoordTransform, -1, -1);
      ol.transform.scale(mapCoordTransform, 2 / mapSize[0], 2 / mapSize[1]);
      ol.transform.translate(mapCoordTransform, 0, mapSize[1]);
      ol.transform.scale(mapCoordTransform, 1, -1);

      // the second matrix is the inverse of the projection matrix used in the
      // shader for drawing
      var projectionMatrixInv = ol.transform.invert(this.projectionMatrix.slice());

      // the third matrix scales to the image dimensions and flips the y-axis again
      var transform = ol.transform.create();
      ol.transform.translate(transform, 0, imageSize[1]);
      ol.transform.scale(transform, 1, -1);
      ol.transform.scale(transform, imageSize[0] / 2, imageSize[1] / 2);
      ol.transform.translate(transform, 1, 1);

      ol.transform.multiply(transform, projectionMatrixInv);
      ol.transform.multiply(transform, mapCoordTransform);

      return transform;
    };