Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 59 additions & 17 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static File getPhotoCacheDir(@NonNull Context context) {
* Returns a directory with the given name in the private cache directory of the application to
* use to store retrieved media and thumbnails.
*
* @param context A context.
* @param context A context.
* @param cacheName The name of the subdirectory in which to store the cache.
* @see #getPhotoCacheDir(android.content.Context)
*/
Expand Down Expand Up @@ -181,13 +181,16 @@ public static File getPhotoCacheDir(@NonNull Context context, @NonNull String ca
*/
@NonNull
// Double checked locking is safe here.
// TODO: Glide源码-Glide.with(context)--获取单例Glide
@SuppressWarnings("GuardedBy")
public static Glide get(@NonNull Context context) {
//双重检查锁DCL
if (glide == null) {
GeneratedAppGlideModule annotationGeneratedModule =
getAnnotationGeneratedGlideModules(context.getApplicationContext());
synchronized (Glide.class) {
if (glide == null) {
//检查并初始化Glide
checkAndInitializeGlide(context, annotationGeneratedModule);
}
}
Expand All @@ -196,6 +199,7 @@ public static Glide get(@NonNull Context context) {
return glide;
}

// TODO: Glide源码-Glide.with(context)--获取单例Glide
@GuardedBy("Glide.class")
private static void checkAndInitializeGlide(
@NonNull Context context, @Nullable GeneratedAppGlideModule generatedAppGlideModule) {
Expand All @@ -207,14 +211,15 @@ private static void checkAndInitializeGlide(
+ " use the provided Glide instance instead");
}
isInitializing = true;
// TODO: Glide源码-Glide.with(context)--获取单例Glide-初始化
initializeGlide(context, generatedAppGlideModule);
isInitializing = false;
}

/**
* @deprecated Use {@link #init(Context, GlideBuilder)} to get a singleton compatible with Glide's
* generated API.
* <p>This method will be removed in a future version of Glide.
* generated API.
* <p>This method will be removed in a future version of Glide.
*/
@VisibleForTesting
@Deprecated
Expand Down Expand Up @@ -260,12 +265,15 @@ public static void tearDown() {
}
}

// TODO: Glide源码-Glide.with(context)--获取单例Glide-初始化
@GuardedBy("Glide.class")
private static void initializeGlide(
@NonNull Context context, @Nullable GeneratedAppGlideModule generatedAppGlideModule) {
//使用 GlideBuilder 建造者模式创建glide
initializeGlide(context, new GlideBuilder(), generatedAppGlideModule);
}

// TODO: Glide源码-Glide.with(context)--获取单例Glide-初始化
@GuardedBy("Glide.class")
@SuppressWarnings("deprecation")
private static void initializeGlide(
Expand Down Expand Up @@ -304,13 +312,15 @@ private static void initializeGlide(
annotationGeneratedModule != null
? annotationGeneratedModule.getRequestManagerFactory()
: null;
//设置RequestManagerFactory工厂
builder.setRequestManagerFactory(factory);
for (com.bumptech.glide.module.GlideModule module : manifestModules) {
module.applyOptions(applicationContext, builder);
}
if (annotationGeneratedModule != null) {
annotationGeneratedModule.applyOptions(applicationContext, builder);
}
// TODO: Glide源码-Glide.with(context)--获取单例Glide-建造者模式->build
Glide glide = builder.build(applicationContext);
for (com.bumptech.glide.module.GlideModule module : manifestModules) {
try {
Expand Down Expand Up @@ -372,6 +382,7 @@ private static void throwIncorrectGlideModule(Exception e) {
e);
}

// TODO: Glide源码-Glide.with(context)--获取单例Glide-构造函数
@SuppressWarnings("PMD.UnusedFormalParameter")
Glide(
@NonNull Context context,
Expand All @@ -395,7 +406,7 @@ private static void throwIncorrectGlideModule(Exception e) {
this.defaultRequestOptionsFactory = defaultRequestOptionsFactory;

final Resources resources = context.getResources();

//组件注册
registry = new Registry();
registry.register(new DefaultImageHeaderParser());
// Right now we're only using this parser for HEIF images, which are only supported on OMR1+.
Expand All @@ -412,6 +423,7 @@ private static void throwIncorrectGlideModule(Exception e) {
VideoDecoder.parcel(bitmapPool);

// TODO(judds): Make ParcelFileDescriptorBitmapDecoder work with ImageDecoder.
//文件,流等转Bitmap操作
Downsampler downsampler =
new Downsampler(
registry.getImageHeaderParsers(), resources.getDisplayMetrics(), bitmapPool, arrayPool);
Expand Down Expand Up @@ -443,10 +455,19 @@ private static void throwIncorrectGlideModule(Exception e) {
ContentResolver contentResolver = context.getContentResolver();

registry
//encoderRegistry
//byte写入文件File
.append(ByteBuffer.class, new ByteBufferEncoder())
//encoderRegistry
//将流写入磁盘的Encoder
.append(InputStream.class, new StreamEncoder(arrayPool))

/* Bitmaps */
//decoderRegistry
//byte->Bitmap
.append(Registry.BUCKET_BITMAP, ByteBuffer.class, Bitmap.class, byteBufferBitmapDecoder)
//decoderRegistry
//Stream->Bitmap
.append(Registry.BUCKET_BITMAP, InputStream.class, Bitmap.class, streamBitmapDecoder);

if (ParcelFileDescriptorRewinder.isSupported()) {
Expand Down Expand Up @@ -525,6 +546,7 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
}

registry
//用于从 Android 资源 ID 加载InputStream的工厂
.append(int.class, InputStream.class, resourceLoaderStreamFactory)
.append(int.class, ParcelFileDescriptor.class, resourceLoaderFileDescriptorFactory)
.append(Integer.class, InputStream.class, resourceLoaderStreamFactory)
Expand All @@ -533,8 +555,10 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
.append(int.class, AssetFileDescriptor.class, resourceLoaderAssetFileDescriptorFactory)
.append(Integer.class, AssetFileDescriptor.class, resourceLoaderAssetFileDescriptorFactory)
.append(int.class, Uri.class, resourceLoaderUriFactory)
//从Uri加载
.append(String.class, InputStream.class, new DataUrlLoader.StreamFactory<String>())
.append(Uri.class, InputStream.class, new DataUrlLoader.StreamFactory<Uri>())
//从String加载InputStream
.append(String.class, InputStream.class, new StringLoader.StreamFactory())
.append(String.class, ParcelFileDescriptor.class, new StringLoader.FileDescriptorFactory())
.append(
Expand Down Expand Up @@ -567,6 +591,7 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
.append(Uri.class, InputStream.class, new UrlUriLoader.StreamFactory())
.append(URL.class, InputStream.class, new UrlLoader.StreamFactory())
.append(Uri.class, File.class, new MediaStoreFileLoader.Factory(context))
//用于平移GlideUrl (HTTP / HTTPS URLS)到InputStream数据。
.append(GlideUrl.class, InputStream.class, new HttpGlideUrlLoader.Factory())
.append(byte[].class, ByteBuffer.class, new ByteArrayLoader.ByteBufferFactory())
.append(byte[].class, InputStream.class, new ByteArrayLoader.StreamFactory())
Expand All @@ -592,8 +617,9 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
BitmapDrawable.class,
new BitmapDrawableDecoder<>(resources, byteBufferVideoDecoder));
}

//创建了一个ImageViewTargetFactory
ImageViewTargetFactory imageViewTargetFactory = new ImageViewTargetFactory();
//创建了一个GlideContext对象
glideContext =
new GlideContext(
context,
Expand Down Expand Up @@ -636,7 +662,9 @@ public ArrayPool getArrayPool() {
return arrayPool;
}

/** @return The context associated with this instance. */
/**
* @return The context associated with this instance.
*/
@NonNull
public Context getContext() {
return glideContext.getBaseContext();
Expand Down Expand Up @@ -669,7 +697,7 @@ GlideContext getGlideContext() {
* every rotation.
*
* @param bitmapAttributeBuilders The list of {@link Builder Builders} representing individual
* sizes and configurations of {@link Bitmap}s to be pre-filled.
* sizes and configurations of {@link Bitmap}s to be pre-filled.
*/
@SuppressWarnings("unused") // Public API
public synchronized void preFillBitmapPool(
Expand Down Expand Up @@ -731,7 +759,9 @@ public void clearDiskCache() {
engine.clearDiskCache();
}

/** Internal method. */
/**
* Internal method.
*/
@NonNull
public RequestManagerRetriever getRequestManagerRetriever() {
return requestManagerRetriever;
Expand Down Expand Up @@ -762,6 +792,7 @@ public MemoryCategory setMemoryCategory(@NonNull MemoryCategory memoryCategory)
return oldCategory;
}

// TODO: Glide源码-Glide.with(context)--获取RequestManagerRetriever
@NonNull
private static RequestManagerRetriever getRetriever(@Nullable Context context) {
// Context could be null for other reasons (ie the user passes in null), but in practice it will
Expand All @@ -771,6 +802,7 @@ private static RequestManagerRetriever getRetriever(@Nullable Context context) {
"You cannot start a load on a not yet attached View or a Fragment where getActivity() "
+ "returns null (which usually occurs when getActivity() is called before the Fragment "
+ "is attached or after the Fragment is destroyed).");
// TODO: Glide源码-Glide.with(context)--获取单例Glide并且获取其对应的retriever
return Glide.get(context).getRequestManagerRetriever();
}

Expand All @@ -786,7 +818,8 @@ private static RequestManagerRetriever getRetriever(@Nullable Context context) {
* the same vein, if the resource will be used in a view in an activity, the load should be
* started with {@link #with(android.app.Activity)}}.
*
* <p>This method is appropriate for resources that will be used outside of the normal fragment or
* <p>This method is appropriate for resources that will be used outside of the normal fragment
* or
* activity lifecycle (For example in services, or for notification thumbnails).
*
* @param context Any context, will not be retained.
Expand All @@ -796,6 +829,7 @@ private static RequestManagerRetriever getRetriever(@Nullable Context context) {
* @see #with(androidx.fragment.app.Fragment)
* @see #with(androidx.fragment.app.FragmentActivity)
*/
// TODO: Glide源码-Glide.with(context)返回RequestManager流程
@NonNull
public static RequestManager with(@NonNull Context context) {
return getRetriever(context).get(context);
Expand All @@ -808,15 +842,20 @@ public static RequestManager with(@NonNull Context context) {
* @param activity The activity to use.
* @return A RequestManager for the given activity that can be used to start a load.
*/

// TODO: Glide源码-Glide.with(context)--返回RequestManager流程
@NonNull
public static RequestManager with(@NonNull Activity activity) {
return getRetriever(activity).get(activity);
// TODO: Glide源码-Glide.with(context)--获取RequestManagerRetriever,然后获取RequestManager
return getRetriever(activity)
// TODO: Glide源码-Glide.with(context)--调用RequestManagerRetriever.get()获取RequestManager
.get(activity);
}

/**
* Begin a load with Glide that will tied to the give {@link
* androidx.fragment.app.FragmentActivity}'s lifecycle and that uses the given {@link
* androidx.fragment.app.FragmentActivity}'s default options.
* Begin a load with Glide that will tied to the give {@link androidx.fragment.app.FragmentActivity}'s
* lifecycle and that uses the given {@link androidx.fragment.app.FragmentActivity}'s default
* options.
*
* @param activity The activity to use.
* @return A RequestManager for the given FragmentActivity that can be used to start a load.
Expand Down Expand Up @@ -845,8 +884,7 @@ public static RequestManager with(@NonNull Fragment fragment) {
* @param fragment The fragment to use.
* @return A RequestManager for the given Fragment that can be used to start a load.
* @deprecated Prefer support Fragments and {@link #with(Fragment)} instead, {@link
* android.app.Fragment} will be deprecated. See
* https://github.com/android/android-ktx/pull/161#issuecomment-363270555.
* android.app.Fragment} will be deprecated. See https://github.com/android/android-ktx/pull/161#issuecomment-363270555.
*/
@SuppressWarnings("deprecation")
@Deprecated
Expand Down Expand Up @@ -935,10 +973,14 @@ public void onLowMemory() {
clearMemory();
}

/** Creates a new instance of {@link RequestOptions}. */
/**
* Creates a new instance of {@link RequestOptions}.
*/
public interface RequestOptionsFactory {

/** Returns a non-null {@link RequestOptions} object. */
/**
* Returns a non-null {@link RequestOptions} object.
*/
@NonNull
RequestOptions build();
}
Expand Down
1 change: 1 addition & 0 deletions library/src/main/java/com/bumptech/glide/GlideBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ Glide build(@NonNull Context context) {
if (bitmapPool == null) {
int size = memorySizeCalculator.getBitmapPoolSize();
if (size > 0) {
//TODO BitmapPool-图片缓存池
bitmapPool = new LruBitmapPool(size);
} else {
bitmapPool = new BitmapPoolAdapter();
Expand Down
1 change: 1 addition & 0 deletions library/src/main/java/com/bumptech/glide/GlideContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public <T> TransitionOptions<?, T> getDefaultTransitionOptions(@NonNull Class<T>
@NonNull
public <X> ViewTarget<ImageView, X> buildImageViewTarget(
@NonNull ImageView imageView, @NonNull Class<X> transcodeClass) {
//构建ImageViewTarget
return imageViewTargetFactory.buildTarget(imageView, transcodeClass);
}

Expand Down
Loading