Glide原理之into过程

由于into过程篇幅过长,不利于阅读,因此把Engine#load过程单独拆出来分析。

1.RequestBuilder#into

  /**
   * Sets the {@link ImageView} the resource will be loaded into, cancels any existing loads into
   * the view, and frees any resources Glide may have previously loaded into the view so they may be
   * reused.
   *
   * @see RequestManager#clear(Target)
   * @param view The view to cancel previous loads for and load the new resource into.
   * @return The {@link com.bumptech.glide.request.target.Target} used to wrap the given {@link
   *     ImageView}.
   */
  @NonNull
  public ViewTarget into(@NonNull ImageView view) {
    //非主线程调用抛异常
    Util.assertMainThread();
    //Imageviw不能为null
    Preconditions.checkNotNull(view);
   
    BaseRequestOptions requestOptions = this;
    if (!requestOptions.isTransformationSet()
        && requestOptions.isTransformationAllowed()
        && view.getScaleType() != null) {
      // Clone in this method so that if we use this RequestBuilder to load into a View and then
      // into a different target, we don't retain the transformation applied based on the previous
      // View's scale type.
      //根据ScaleType克隆不同的RequestOptions对象,为的是去除上一次参数的影响。
      switch (view.getScaleType()) {
        case CENTER_CROP:
          requestOptions = requestOptions.clone().optionalCenterCrop();
          break;
        case CENTER_INSIDE:
          requestOptions = requestOptions.clone().optionalCenterInside();
          break;
        case FIT_CENTER:
        case FIT_START:
        case FIT_END:
          requestOptions = requestOptions.clone().optionalFitCenter();
          break;
        case FIT_XY:
          requestOptions = requestOptions.clone().optionalCenterInside();
          break;
        case CENTER:
        case MATRIX:
        default:
          // Do nothing.
      }
    }
    //glideContext.buildImageViewTarget(view, transcodeClass)得到的结果是DrawableImageViewTarget对象
    return into(
        glideContext.buildImageViewTarget(view, transcodeClass),
        /*targetListener=*/ null,
        requestOptions,
        Executors.mainThreadExecutor());
  }



 private > Y into(
      @NonNull Y target,
      @Nullable RequestListener targetListener,
      BaseRequestOptions options,
      Executor callbackExecutor) {
    //Imageview为空抛出异常
    Preconditions.checkNotNull(target);
    //未设置model参数抛出异常
    if (!isModelSet) {
      throw new IllegalArgumentException("You must call #load() before calling #into()");
    }
    //构建Request请求
    Request request = buildRequest(target, targetListener, options, callbackExecutor);
    //获取ViewTarget缓存的请求
    Request previous = target.getRequest();
    //如果Imageview中缓存的上一个request对象新创建的request内容相同,且未设置不使用内存缓存且previous请求未完成,则复用previous请求。
    if (request.isEquivalentTo(previous)
        && !isSkipMemoryCacheWithCompletePreviousRequest(options, previous)) {
      //释放新创建的请求。
      request.recycle();
      //如果previous请求是完成状态,则重新发送请求。这样做的目的是优化了比如占位符等相关的设置,复用了之前请求里对view的设置
      if (!Preconditions.checkNotNull(previous).isRunning()) {
        previous.begin();
      }
      return target;
    }
    //清楚View中缓存的request对象
    requestManager.clear(target);
    //在View#settag方法中缓存request对象
    target.setRequest(request);
    //发送请求
    requestManager.track(target, request);

    return target;
  }

2.RequestManager#track

  synchronized void track(@NonNull Target target, @NonNull Request request) {
    //添加到TargetTracker,用于执行生命周期回调
    targetTracker.track(target);
    //开始请求
    requestTracker.runRequest(request);
  }

  

3.RequestTracker#runRequest

  public void runRequest(@NonNull Request request) {
    //缓存request请求
    requests.add(request);
    if (!isPaused) {
      //执行请求
      request.begin();
    } else {
      request.clear();
      if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "Paused, delaying request");
      }
      //暂停状态不发送请求,添加到PendingRequest对象,调用resumeRequests方法会重新执行请求。
      //pendingRequests队列是为了强引用暂停状态下提交的request请求防止被gc。
      pendingRequests.add(request);
    }
  }

Request是一个接口,那么他的实现类是什么呢,我们回到第一步的buildRequest(target, targetListener, options, callbackExecutor)方法。

4. RequestBuilder#buildRequest

private Request buildRequest(
      Target target,
      @Nullable RequestListener targetListener,
      BaseRequestOptions requestOptions,
      Executor callbackExecutor) {
    //transitionOptions是图片渲染时的过度效果,不设置时,会先去取transcodeClass父类的,取不到使用默认的
    return buildRequestRecursive(
        target,
        targetListener,
        /*parentCoordinator=*/ null,
        transitionOptions,
        requestOptions.getPriority(),
        requestOptions.getOverrideWidth(),
        requestOptions.getOverrideHeight(),
        requestOptions,
        //回调线程池,在主线程执行
        callbackExecutor);
  }

5.RequestBuilder#buildRequestRecursive

private Request buildRequestRecursive(
      Target target,
      @Nullable RequestListener targetListener,
      @Nullable RequestCoordinator parentCoordinator,
      TransitionOptions transitionOptions,
      Priority priority,
      int overrideWidth,
      int overrideHeight,
      BaseRequestOptions requestOptions,
      Executor callbackExecutor) {

    // Build the ErrorRequestCoordinator first if necessary so we can update parentCoordinator.
    ErrorRequestCoordinator errorRequestCoordinator = null;
    //如果设置了errorBuilder,构建errorRequestCoordinator对象
    if (errorBuilder != null) {
      errorRequestCoordinator = new ErrorRequestCoordinator(parentCoordinator);
      parentCoordinator = errorRequestCoordinator;
    }
    //构建缩略图请求
    Request mainRequest =
        buildThumbnailRequestRecursive(
            target,
            targetListener,
            parentCoordinator,
            transitionOptions,
            priority,
            overrideWidth,
            overrideHeight,
            requestOptions,
            callbackExecutor);
    //未设置error请求,返回mainRequest
    if (errorRequestCoordinator == null) {
      return mainRequest;
    }

    int errorOverrideWidth = errorBuilder.getOverrideWidth();
    int errorOverrideHeight = errorBuilder.getOverrideHeight();
    if (Util.isValidDimensions(overrideWidth, overrideHeight) && !errorBuilder.isValidOverride()) {
      errorOverrideWidth = requestOptions.getOverrideWidth();
      errorOverrideHeight = requestOptions.getOverrideHeight();
    }

    Request errorRequest =
        errorBuilder.buildRequestRecursive(
            target,
            targetListener,
            errorRequestCoordinator,
            errorBuilder.transitionOptions,
            errorBuilder.getPriority(),
            errorOverrideWidth,
            errorOverrideHeight,
            errorBuilder,
            callbackExecutor);
    errorRequestCoordinator.setRequests(mainRequest, errorRequest);
    return errorRequestCoordinator;
  }

无论是errorBuilder还是还是正常的builder都会调用到buildThumbnailRequestRecursive方法。

6. RequestBuilder#buildThumbnailRequestRecursive

private Request buildThumbnailRequestRecursive(
      Target target,
      RequestListener targetListener,
      @Nullable RequestCoordinator parentCoordinator,
      TransitionOptions transitionOptions,
      Priority priority,
      int overrideWidth,
      int overrideHeight,
      BaseRequestOptions requestOptions,
      Executor callbackExecutor) {
    if (thumbnailBuilder != null) {
      //设置了缩略图的RequestBuilder
      // Recursive case: contains a potentially recursive thumbnail request builder.
      if (isThumbnailBuilt) {
        throw new IllegalStateException(
            "You cannot use a request as both the main request and a "
                + "thumbnail, consider using clone() on the request(s) passed to thumbnail()");
      }

      TransitionOptions thumbTransitionOptions =
          thumbnailBuilder.transitionOptions;

      // Apply our transition by default to thumbnail requests but avoid overriding custom options
      // that may have been applied on the thumbnail request explicitly.
      if (thumbnailBuilder.isDefaultTransitionOptionsSet) {
        thumbTransitionOptions = transitionOptions;
      }

      Priority thumbPriority =
          thumbnailBuilder.isPrioritySet()
              ? thumbnailBuilder.getPriority()
              : getThumbnailPriority(priority);

      int thumbOverrideWidth = thumbnailBuilder.getOverrideWidth();
      int thumbOverrideHeight = thumbnailBuilder.getOverrideHeight();
      if (Util.isValidDimensions(overrideWidth, overrideHeight)
          && !thumbnailBuilder.isValidOverride()) {
        thumbOverrideWidth = requestOptions.getOverrideWidth();
        thumbOverrideHeight = requestOptions.getOverrideHeight();
      }
      //管理全尺寸和缩略图两个请求
      ThumbnailRequestCoordinator coordinator = new ThumbnailRequestCoordinator(parentCoordinator);
      //全尺寸图片请求
      Request fullRequest =
          obtainRequest(
              target,
              targetListener,
              requestOptions,
              coordinator,
              transitionOptions,
              priority,
              overrideWidth,
              overrideHeight,
              callbackExecutor);
      isThumbnailBuilt = true;
      //缩略图请求
      Request thumbRequest =
          thumbnailBuilder.buildRequestRecursive(
              target,
              targetListener,
              coordinator,
              thumbTransitionOptions,
              thumbPriority,
              thumbOverrideWidth,
              thumbOverrideHeight,
              thumbnailBuilder,
              callbackExecutor);
      isThumbnailBuilt = false;
      coordinator.setRequests(fullRequest, thumbRequest);
      return coordinator;
    } else if (thumbSizeMultiplier != null) {
      //设置缩略图百分比参数thumbSizeMultiplier,则会创建缩略图请求
      ThumbnailRequestCoordinator coordinator = new ThumbnailRequestCoordinator(parentCoordinator);
      Request fullRequest =
          obtainRequest(
              target,
              targetListener,
              requestOptions,
              coordinator,
              transitionOptions,
              priority,
              overrideWidth,
              overrideHeight,
              callbackExecutor);
      BaseRequestOptions thumbnailOptions =
          requestOptions.clone().sizeMultiplier(thumbSizeMultiplier);

      Request thumbnailRequest =
          obtainRequest(
              target,
              targetListener,
              thumbnailOptions,
              coordinator,
              transitionOptions,
              getThumbnailPriority(priority),
              overrideWidth,
              overrideHeight,
              callbackExecutor);

      coordinator.setRequests(fullRequest, thumbnailRequest);
      return coordinator;
    } else {
      // 没有缩略图请求,直接构建全尺寸图片请求
      return obtainRequest(
          target,
          targetListener,
          requestOptions,
          parentCoordinator,
          transitionOptions,
          priority,
          overrideWidth,
          overrideHeight,
          callbackExecutor);
    }
  }

无论是缩略图请求还是全尺寸请求又或是error请求,他们都是调用的obtainRequest方法创建的请求。

7.RequestBuilder#obtainRequest

private Request obtainRequest(
      Target target,
      RequestListener targetListener,
      BaseRequestOptions requestOptions,
      RequestCoordinator requestCoordinator,
      TransitionOptions transitionOptions,
      Priority priority,
      int overrideWidth,
      int overrideHeight,
      Executor callbackExecutor) {
    return SingleRequest.obtain(
        context,
        glideContext,
        model,
        transcodeClass,
        requestOptions,
        overrideWidth,
        overrideHeight,
        priority,
        target,
        targetListener,
        requestListeners,
        requestCoordinator,
        glideContext.getEngine(),
        transitionOptions.getTransitionFactory(),
        callbackExecutor);
  }

Request有多个实现类,我们使用的是SingleRquest方法。所以我们的begin方法就是调用的SingleRequest#begin方法

8. SingleRequest#begin

@Override
  public synchronized void begin() {
    assertNotCallingCallbacks();
    //检查request是否被回收
    stateVerifier.throwIfRecycled();
    startTime = LogTime.getLogTime();
    //model对象为null,加载失败
    if (model == null) {
      if (Util.isValidDimensions(overrideWidth, overrideHeight)) {
        width = overrideWidth;
        height = overrideHeight;
      }
      // Only log at more verbose log levels if the user has set a fallback drawable, because
      // fallback Drawables indicate the user expects null models occasionally.
      int logLevel = getFallbackDrawable() == null ? Log.WARN : Log.DEBUG;
      onLoadFailed(new GlideException("Received null model"), logLevel);
      return;
    }
    //运行状态不能调用start
    if (status == Status.RUNNING) {
      throw new IllegalArgumentException("Cannot restart a running request");
    }
    //如果是完成状态直接回调
    if (status == Status.COMPLETE) {
      onResourceReady(resource, DataSource.MEMORY_CACHE);
      return;
    }
    //非COMPLETE、RUNNING状态设置为WAITING_FOR_SIZE状态,作为新请求使用
    status = Status.WAITING_FOR_SIZE;
    if (Util.isValidDimensions(overrideWidth, overrideHeight)) {
      //设置过overrideWidth和overrideHeight直接调用onSizeReady
      onSizeReady(overrideWidth, overrideHeight);
    } else {
      //计算overrideWidth和overrideHeight大小,计算完成回调onSizeReady
      target.getSize(this);
    }
    //回调onLoadStarted方法
    if ((status == Status.RUNNING || status == Status.WAITING_FOR_SIZE)
        && canNotifyStatusChanged()) {
      target.onLoadStarted(getPlaceholderDrawable());
    }
    if (IS_VERBOSE_LOGGABLE) {
      logV("finished run method in " + LogTime.getElapsedMillis(startTime));
    }
  }

9. SingleRequest#onSizeReady

 /** A callback method that should never be invoked directly. */
  @Override
  public synchronized void onSizeReady(int width, int height) {
    stateVerifier.throwIfRecycled();
    if (IS_VERBOSE_LOGGABLE) {
      logV("Got onSizeReady in " + LogTime.getElapsedMillis(startTime));
    }
    if (status != Status.WAITING_FOR_SIZE) {
      return;
    }
    //修改为RUNNING状态
    status = Status.RUNNING;

    float sizeMultiplier = requestOptions.getSizeMultiplier();
    this.width = maybeApplySizeMultiplier(width, sizeMultiplier);
    this.height = maybeApplySizeMultiplier(height, sizeMultiplier);

    if (IS_VERBOSE_LOGGABLE) {
      logV("finished setup for calling load in " + LogTime.getElapsedMillis(startTime));
    }
    loadStatus =
        engine.load(
            glideContext,
            model,
            requestOptions.getSignature(),
            this.width,
            this.height,
            requestOptions.getResourceClass(),
            transcodeClass,
            priority,
            requestOptions.getDiskCacheStrategy(),
            requestOptions.getTransformations(),
            requestOptions.isTransformationRequired(),
            requestOptions.isScaleOnlyOrNoTransform(),
            requestOptions.getOptions(),
            requestOptions.isMemoryCacheable(),
            requestOptions.getUseUnlimitedSourceGeneratorsPool(),
            requestOptions.getUseAnimationPool(),
            requestOptions.getOnlyRetrieveFromCache(),
            //ResourceCallback对象,资源加载成功会回调
            this,
            callbackExecutor);

    // This is a hack that's only useful for testing right now where loads complete synchronously
    // even though under any executor running on any thread but the main thread, the load would
    // have completed asynchronously.
    if (status != Status.RUNNING) {
      loadStatus = null;
    }
    if (IS_VERBOSE_LOGGABLE) {
      logV("finished onSizeReady in " + LogTime.getElapsedMillis(startTime));
    }
  }

Engine#load方法加载成功后会回调SingleRequest#onResourceReady方法,SingleRequest实现了ResourceCallback接口

10. SingleRequest#onResourceReady

 @Override
  public synchronized void onResourceReady(Resource resource, DataSource dataSource) {
    stateVerifier.throwIfRecycled();
    loadStatus = null;
    //如果resource为空,则回调加载失败,并抛出异常
    if (resource == null) {
      GlideException exception =
          new GlideException(
              "Expected to receive a Resource with an "
                  + "object of "
                  + transcodeClass
                  + " inside, but instead got null.");
      onLoadFailed(exception);
      return;
    }

    Object received = resource.get();
    if (received == null || !transcodeClass.isAssignableFrom(received.getClass())) {
      releaseResource(resource);
      GlideException exception =
          new GlideException(
              "Expected to receive an object of "
                  + transcodeClass
                  + " but instead"
                  + " got "
                  + (received != null ? received.getClass() : "")
                  + "{"
                  + received
                  + "} inside"
                  + " "
                  + "Resource{"
                  + resource
                  + "}."
                  + (received != null
                      ? ""
                      : " "
                          + "To indicate failure return a null Resource "
                          + "object, rather than a Resource object containing null data."));
      onLoadFailed(exception);
      return;
    }
    
    if (!canSetResource()) {
      releaseResource(resource);
      // We can't put the status to complete before asking canSetResource().
      status = Status.COMPLETE;
      return;
    }

    onResourceReady((Resource) resource, (R) received, dataSource);
  }

 /**
   * Internal {@link #onResourceReady(Resource, DataSource)} where arguments are known to be safe.
   *
   * @param resource original {@link Resource}, never null
   * @param result object returned by {@link Resource#get()}, checked for type and never null
   *     
   */
  private synchronized void onResourceReady(Resource resource, R result, DataSource dataSource) {
    // We must call isFirstReadyResource before setting status.
    boolean isFirstResource = isFirstReadyResource();
    status = Status.COMPLETE;
    this.resource = resource;

    if (glideContext.getLogLevel() <= Log.DEBUG) {
      Log.d(
          GLIDE_TAG,
          "Finished loading "
              + result.getClass().getSimpleName()
              + " from "
              + dataSource
              + " for "
              + model
              + " with size ["
              + width
              + "x"
              + height
              + "] in "
              + LogTime.getElapsedMillis(startTime)
              + " ms");
    }

    isCallingCallbacks = true;
    try {
      //这个状态就是为了判断,回调方法onResourceReady是否返回true,
      //如为true,则不执行图片渲染到指定View。即target.onResourceReady(result, animation)方法
      boolean anyListenerHandledUpdatingTarget = false;
      if (requestListeners != null) {
        for (RequestListener listener : requestListeners) {
          anyListenerHandledUpdatingTarget |=
              listener.onResourceReady(result, model, target, dataSource, isFirstResource);
        }
      }
      anyListenerHandledUpdatingTarget |=
          targetListener != null
              && targetListener.onResourceReady(result, model, target, dataSource, isFirstResource);

      if (!anyListenerHandledUpdatingTarget) {
        Transition animation = animationFactory.build(dataSource, isFirstResource);
        //这个地方调用的是ImageViewTarget#onResourceReady方法。
        target.onResourceReady(result, animation);
      }
    } finally {
      isCallingCallbacks = false;
    }

    notifyLoadSuccess();
  }

在前面文章with过程中,我们讲到target是DrawableImageViewTarget对象,DrawableImageViewTarget父类为ImageViewTarget,因此这个地方调用的是ImageViewTarget#onResourceReady方法。

11. ImageViewTarget#onResourceReady

  @Override
  public void onResourceReady(@NonNull Z resource, @Nullable Transition transition) {
    if (transition == null || !transition.transition(resource, this)) {
      setResourceInternal(resource);
    } else {
      maybeUpdateAnimatable(resource);
    }
  }


  private void setResourceInternal(@Nullable Z resource) {
    // Order matters here. Set the resource first to make sure that the Drawable has a valid and
    // non-null Callback before starting it.
    setResource(resource);
    maybeUpdateAnimatable(resource);
  }

  private void maybeUpdateAnimatable(@Nullable Z resource) {
    if (resource instanceof Animatable) {
      animatable = (Animatable) resource;
      animatable.start();
    } else {
      animatable = null;
    }
  }

setResource方法调用了view.setImageDrawable(resource)这样图片就显示到了ImageView。

接下来我们会分析Engine#load过程,这是Glide的核心部分。

你可能感兴趣的:(Glide,Android,Glide,into,Glide源码)