1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CC_RESOURCES_RESOURCE_PROVIDER_H_
6 #define CC_RESOURCES_RESOURCE_PROVIDER_H_
14 #include "base/basictypes.h"
15 #include "base/callback.h"
16 #include "base/containers/hash_tables.h"
17 #include "base/memory/linked_ptr.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/threading/thread_checker.h"
20 #include "base/trace_event/memory_allocator_dump.h"
21 #include "base/trace_event/memory_dump_provider.h"
22 #include "cc/base/cc_export.h"
23 #include "cc/base/resource_id.h"
24 #include "cc/output/context_provider.h"
25 #include "cc/output/output_surface.h"
26 #include "cc/resources/release_callback_impl.h"
27 #include "cc/resources/resource_format.h"
28 #include "cc/resources/return_callback.h"
29 #include "cc/resources/shared_bitmap.h"
30 #include "cc/resources/single_release_callback_impl.h"
31 #include "cc/resources/texture_mailbox.h"
32 #include "cc/resources/transferable_resource.h"
33 #include "third_party/khronos/GLES2/gl2.h"
34 #include "third_party/khronos/GLES2/gl2ext.h"
35 #include "third_party/skia/include/core/SkBitmap.h"
36 #include "third_party/skia/include/core/SkCanvas.h"
37 #include "ui/gfx/geometry/size.h"
38 #include "ui/gfx/gpu_memory_buffer.h"
43 class GpuMemoryBufferManager
;
55 class BlockingTaskRunner
;
58 class SharedBitmapManager
;
60 // This class is not thread-safe and can only be called from the thread it was
61 // created on (in practice, the impl thread).
62 class CC_EXPORT ResourceProvider
63 : public base::trace_event::MemoryDumpProvider
{
68 typedef std::vector
<ResourceId
> ResourceIdArray
;
69 typedef base::hash_set
<ResourceId
> ResourceIdSet
;
70 typedef base::hash_map
<ResourceId
, ResourceId
> ResourceIdMap
;
72 TEXTURE_HINT_DEFAULT
= 0x0,
73 TEXTURE_HINT_IMMUTABLE
= 0x1,
74 TEXTURE_HINT_FRAMEBUFFER
= 0x2,
75 TEXTURE_HINT_IMMUTABLE_FRAMEBUFFER
=
76 TEXTURE_HINT_IMMUTABLE
| TEXTURE_HINT_FRAMEBUFFER
79 RESOURCE_TYPE_GL_TEXTURE
,
83 static scoped_ptr
<ResourceProvider
> Create(
84 OutputSurface
* output_surface
,
85 SharedBitmapManager
* shared_bitmap_manager
,
86 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
87 BlockingTaskRunner
* blocking_main_thread_task_runner
,
88 int highp_threshold_min
,
89 bool use_rgba_4444_texture_format
,
90 size_t id_allocation_chunk_size
,
91 const std::vector
<unsigned>& use_image_texture_targets
);
92 ~ResourceProvider() override
;
94 void DidLoseOutputSurface() { lost_output_surface_
= true; }
96 int max_texture_size() const { return max_texture_size_
; }
97 ResourceFormat
memory_efficient_texture_format() const {
98 return use_rgba_4444_texture_format_
? RGBA_4444
: best_texture_format_
;
100 ResourceFormat
best_texture_format() const { return best_texture_format_
; }
101 ResourceFormat
best_render_buffer_format() const {
102 return best_render_buffer_format_
;
104 ResourceFormat
yuv_resource_format() const { return yuv_resource_format_
; }
105 bool use_sync_query() const { return use_sync_query_
; }
106 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager() {
107 return gpu_memory_buffer_manager_
;
109 size_t num_resources() const { return resources_
.size(); }
111 // Checks whether a resource is in use by a consumer.
112 bool InUseByConsumer(ResourceId id
);
114 bool IsLost(ResourceId id
);
116 void LoseResourceForTesting(ResourceId id
);
117 void EnableReadLockFencesForTesting(ResourceId id
);
119 // Producer interface.
121 ResourceType
default_resource_type() const { return default_resource_type_
; }
122 ResourceType
GetResourceType(ResourceId id
);
124 // Creates a resource of the default resource type.
125 ResourceId
CreateResource(const gfx::Size
& size
,
128 ResourceFormat format
);
130 // Creates a resource which is tagged as being managed for GPU memory
131 // accounting purposes.
132 ResourceId
CreateManagedResource(const gfx::Size
& size
,
136 ResourceFormat format
);
138 // You can also explicitly create a specific resource type.
139 ResourceId
CreateGLTexture(const gfx::Size
& size
,
144 ResourceFormat format
);
146 ResourceId
CreateBitmap(const gfx::Size
& size
, GLint wrap_mode
);
147 // Wraps an IOSurface into a GL resource.
148 ResourceId
CreateResourceFromIOSurface(const gfx::Size
& size
,
149 unsigned io_surface_id
);
151 // Wraps an external texture mailbox into a GL resource.
152 ResourceId
CreateResourceFromTextureMailbox(
153 const TextureMailbox
& mailbox
,
154 scoped_ptr
<SingleReleaseCallbackImpl
> release_callback_impl
);
156 ResourceId
CreateResourceFromTextureMailbox(
157 const TextureMailbox
& mailbox
,
158 scoped_ptr
<SingleReleaseCallbackImpl
> release_callback_impl
,
159 bool read_lock_fences_enabled
);
161 void DeleteResource(ResourceId id
);
163 // Update pixels from image, copying source_rect (in image) to dest_offset (in
165 void CopyToResource(ResourceId id
,
166 const uint8_t* image
,
167 const gfx::Size
& image_size
);
169 // Only flush the command buffer if supported.
170 // Returns true if the shallow flush occurred, false otherwise.
171 bool ShallowFlushIfSupported();
173 // Creates accounting for a child. Returns a child ID.
174 int CreateChild(const ReturnCallback
& return_callback
);
176 // Destroys accounting for the child, deleting all accounted resources.
177 void DestroyChild(int child
);
179 // Sets whether resources need sync points set on them when returned to this
180 // child. Defaults to true.
181 void SetChildNeedsSyncPoints(int child
, bool needs_sync_points
);
183 // Gets the child->parent resource ID map.
184 const ResourceIdMap
& GetChildToParentMap(int child
) const;
186 // Prepares resources to be transfered to the parent, moving them to
187 // mailboxes and serializing meta-data into TransferableResources.
188 // Resources are not removed from the ResourceProvider, but are marked as
190 void PrepareSendToParent(const ResourceIdArray
& resources
,
191 TransferableResourceArray
* transferable_resources
);
193 // Receives resources from a child, moving them from mailboxes. Resource IDs
194 // passed are in the child namespace, and will be translated to the parent
195 // namespace, added to the child->parent map.
196 // This adds the resources to the working set in the ResourceProvider without
197 // declaring which resources are in use. Use DeclareUsedResourcesFromChild
198 // after calling this method to do that. All calls to ReceiveFromChild should
199 // be followed by a DeclareUsedResourcesFromChild.
200 // NOTE: if the sync_point is set on any TransferableResource, this will
202 void ReceiveFromChild(
203 int child
, const TransferableResourceArray
& transferable_resources
);
205 // Once a set of resources have been received, they may or may not be used.
206 // This declares what set of resources are currently in use from the child,
207 // releasing any other resources back to the child.
208 void DeclareUsedResourcesFromChild(int child
,
209 const ResourceIdSet
& resources_from_child
);
211 // Receives resources from the parent, moving them from mailboxes. Resource
212 // IDs passed are in the child namespace.
213 // NOTE: if the sync_point is set on any TransferableResource, this will
215 void ReceiveReturnsFromParent(
216 const ReturnedResourceArray
& transferable_resources
);
218 // The following lock classes are part of the ResourceProvider API and are
219 // needed to read and write the resource contents. The user must ensure
220 // that they only use GL locks on GL resources, etc, and this is enforced
222 class CC_EXPORT ScopedReadLockGL
{
224 ScopedReadLockGL(ResourceProvider
* resource_provider
,
225 ResourceId resource_id
);
226 virtual ~ScopedReadLockGL();
228 unsigned texture_id() const { return resource_
->gl_id
; }
229 GLenum
target() const { return resource_
->target
; }
232 ResourceProvider
* resource_provider_
;
233 ResourceId resource_id_
;
236 const ResourceProvider::Resource
* resource_
;
238 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL
);
241 class CC_EXPORT ScopedSamplerGL
: public ScopedReadLockGL
{
243 ScopedSamplerGL(ResourceProvider
* resource_provider
,
244 ResourceId resource_id
,
246 ScopedSamplerGL(ResourceProvider
* resource_provider
,
247 ResourceId resource_id
,
250 ~ScopedSamplerGL() override
;
252 GLenum
target() const { return target_
; }
258 DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL
);
261 class CC_EXPORT ScopedWriteLockGL
{
263 ScopedWriteLockGL(ResourceProvider
* resource_provider
,
264 ResourceId resource_id
);
265 ~ScopedWriteLockGL();
267 unsigned texture_id() const { return texture_id_
; }
270 ResourceProvider
* resource_provider_
;
271 ResourceProvider::Resource
* resource_
;
272 unsigned texture_id_
;
274 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL
);
277 class CC_EXPORT ScopedReadLockSoftware
{
279 ScopedReadLockSoftware(ResourceProvider
* resource_provider
,
280 ResourceId resource_id
);
281 ~ScopedReadLockSoftware();
283 const SkBitmap
* sk_bitmap() const {
287 GLint
wrap_mode() const { return wrap_mode_
; }
289 bool valid() const { return !!sk_bitmap_
.getPixels(); }
292 ResourceProvider
* resource_provider_
;
293 ResourceId resource_id_
;
297 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware
);
300 class CC_EXPORT ScopedWriteLockSoftware
{
302 ScopedWriteLockSoftware(ResourceProvider
* resource_provider
,
303 ResourceId resource_id
);
304 ~ScopedWriteLockSoftware();
306 SkBitmap
& sk_bitmap() { return sk_bitmap_
; }
307 bool valid() const { return !!sk_bitmap_
.getPixels(); }
310 ResourceProvider
* resource_provider_
;
311 ResourceProvider::Resource
* resource_
;
313 base::ThreadChecker thread_checker_
;
315 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware
);
318 class CC_EXPORT ScopedWriteLockGpuMemoryBuffer
{
320 ScopedWriteLockGpuMemoryBuffer(ResourceProvider
* resource_provider
,
321 ResourceId resource_id
);
322 ~ScopedWriteLockGpuMemoryBuffer();
324 gfx::GpuMemoryBuffer
* GetGpuMemoryBuffer();
327 ResourceProvider
* resource_provider_
;
328 ResourceProvider::Resource
* resource_
;
329 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
330 gfx::GpuMemoryBuffer
* gpu_memory_buffer_
;
332 ResourceFormat format_
;
333 base::ThreadChecker thread_checker_
;
335 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGpuMemoryBuffer
);
338 class CC_EXPORT ScopedWriteLockGr
{
340 ScopedWriteLockGr(ResourceProvider
* resource_provider
,
341 ResourceId resource_id
);
342 ~ScopedWriteLockGr();
344 void InitSkSurface(bool use_distance_field_text
,
345 bool can_use_lcd_text
,
346 int msaa_sample_count
);
347 void ReleaseSkSurface();
349 SkSurface
* sk_surface() { return sk_surface_
.get(); }
350 ResourceProvider::Resource
* resource() { return resource_
; }
353 ResourceProvider
* resource_provider_
;
354 ResourceProvider::Resource
* resource_
;
355 base::ThreadChecker thread_checker_
;
356 skia::RefPtr
<SkSurface
> sk_surface_
;
358 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGr
);
361 class Fence
: public base::RefCounted
<Fence
> {
365 virtual void Set() = 0;
366 virtual bool HasPassed() = 0;
367 virtual void Wait() = 0;
370 friend class base::RefCounted
<Fence
>;
374 DISALLOW_COPY_AND_ASSIGN(Fence
);
377 class SynchronousFence
: public ResourceProvider::Fence
{
379 explicit SynchronousFence(gpu::gles2::GLES2Interface
* gl
);
381 // Overridden from Fence:
383 bool HasPassed() override
;
384 void Wait() override
;
386 // Returns true if fence has been set but not yet synchornized.
387 bool has_synchronized() const { return has_synchronized_
; }
390 ~SynchronousFence() override
;
394 gpu::gles2::GLES2Interface
* gl_
;
395 bool has_synchronized_
;
397 DISALLOW_COPY_AND_ASSIGN(SynchronousFence
);
400 // Acquire pixel buffer for resource. The pixel buffer can be used to
401 // set resource pixels without performing unnecessary copying.
402 void AcquirePixelBuffer(ResourceId resource
);
403 void ReleasePixelBuffer(ResourceId resource
);
404 // Map/unmap the acquired pixel buffer.
405 uint8_t* MapPixelBuffer(ResourceId id
, int* stride
);
406 void UnmapPixelBuffer(ResourceId id
);
407 // Asynchronously update pixels from acquired pixel buffer.
408 void BeginSetPixels(ResourceId id
);
409 void ForceSetPixelsToComplete(ResourceId id
);
410 bool DidSetPixelsComplete(ResourceId id
);
412 // For tests only! This prevents detecting uninitialized reads.
413 // Use SetPixels or LockForWrite to allocate implicitly.
414 void AllocateForTesting(ResourceId id
);
417 void CreateForTesting(ResourceId id
);
419 GLenum
TargetForTesting(ResourceId id
);
421 // Sets the current read fence. If a resource is locked for read
422 // and has read fences enabled, the resource will not allow writes
423 // until this fence has passed.
424 void SetReadLockFence(Fence
* fence
) { current_read_lock_fence_
= fence
; }
426 // Indicates if we can currently lock this resource for write.
427 bool CanLockForWrite(ResourceId id
);
429 void WaitSyncPointIfNeeded(ResourceId id
);
431 static GLint
GetActiveTextureUnit(gpu::gles2::GLES2Interface
* gl
);
433 OutputSurface
* output_surface() { return output_surface_
; }
435 void ValidateResource(ResourceId id
) const;
437 GLenum
GetImageTextureTarget(ResourceFormat format
);
439 // base::trace_event::MemoryDumpProvider implementation.
440 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs
& args
,
441 base::trace_event::ProcessMemoryDump
* pmd
) override
;
443 int tracing_id() const { return tracing_id_
; }
446 ResourceProvider(OutputSurface
* output_surface
,
447 SharedBitmapManager
* shared_bitmap_manager
,
448 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
449 BlockingTaskRunner
* blocking_main_thread_task_runner
,
450 int highp_threshold_min
,
451 bool use_rgba_4444_texture_format
,
452 size_t id_allocation_chunk_size
,
453 const std::vector
<unsigned>& use_image_texture_targets
);
458 enum Origin
{ INTERNAL
, EXTERNAL
, DELEGATED
};
461 Resource(unsigned texture_id
,
462 const gfx::Size
& size
,
469 ResourceFormat format
);
470 Resource(uint8_t* pixels
,
471 SharedBitmap
* bitmap
,
472 const gfx::Size
& size
,
476 Resource(const SharedBitmapId
& bitmap_id
,
477 const gfx::Size
& size
,
484 // Pixel buffer used for set pixels without unnecessary copying.
485 unsigned gl_pixel_buffer_id
;
486 // Query used to determine when asynchronous set pixels complete.
487 unsigned gl_upload_query_id
;
488 // Query used to determine when read lock fence has passed.
489 unsigned gl_read_lock_query_id
;
490 TextureMailbox mailbox
;
491 ReleaseCallbackImpl release_callback_impl
;
493 int lock_for_read_count
;
496 bool dirty_image
: 1;
497 bool locked_for_write
: 1;
499 bool marked_for_deletion
: 1;
501 bool read_lock_fences_enabled
: 1;
502 bool has_shared_bitmap_id
: 1;
503 scoped_refptr
<Fence
> read_lock_fence
;
507 // TODO(skyostil): Use a separate sampler object for filter state.
508 GLenum original_filter
;
511 unsigned bound_image_id
;
516 ResourceFormat format
;
517 SharedBitmapId shared_bitmap_id
;
518 SharedBitmap
* shared_bitmap
;
519 gfx::GpuMemoryBuffer
* gpu_memory_buffer
;
521 typedef base::hash_map
<ResourceId
, Resource
> ResourceMap
;
527 ResourceIdMap child_to_parent_map
;
528 ResourceIdMap parent_to_child_map
;
529 ReturnCallback return_callback
;
530 bool marked_for_deletion
;
531 bool needs_sync_points
;
533 typedef base::hash_map
<int, Child
> ChildMap
;
535 bool ReadLockFenceHasPassed(const Resource
* resource
) {
536 return !resource
->read_lock_fence
.get() ||
537 resource
->read_lock_fence
->HasPassed();
540 Resource
* InsertResource(ResourceId id
, const Resource
& resource
);
541 Resource
* GetResource(ResourceId id
);
542 const Resource
* LockForRead(ResourceId id
);
543 void UnlockForRead(ResourceId id
);
544 Resource
* LockForWrite(ResourceId id
);
545 void UnlockForWrite(Resource
* resource
);
547 static void PopulateSkBitmapWithResource(SkBitmap
* sk_bitmap
,
548 const Resource
* resource
);
550 void TransferResource(gpu::gles2::GLES2Interface
* gl
,
552 TransferableResource
* resource
);
557 void DeleteResourceInternal(ResourceMap::iterator it
, DeleteStyle style
);
558 void DeleteAndReturnUnusedResourcesToChild(ChildMap::iterator child_it
,
560 const ResourceIdArray
& unused
);
561 void DestroyChildInternal(ChildMap::iterator it
, DeleteStyle style
);
562 void LazyCreate(Resource
* resource
);
563 void LazyAllocate(Resource
* resource
);
565 void BindImageForSampling(Resource
* resource
);
566 // Binds the given GL resource to a texture target for sampling using the
567 // specified filter for both minification and magnification. Returns the
568 // texture target used. The resource must be locked for reading.
569 GLenum
BindForSampling(ResourceId resource_id
, GLenum unit
, GLenum filter
);
571 // Returns NULL if the output_surface_ does not have a ContextProvider.
572 gpu::gles2::GLES2Interface
* ContextGL() const;
573 class GrContext
* GrContext(bool worker_context
) const;
575 OutputSurface
* output_surface_
;
576 SharedBitmapManager
* shared_bitmap_manager_
;
577 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
578 BlockingTaskRunner
* blocking_main_thread_task_runner_
;
579 bool lost_output_surface_
;
580 int highp_threshold_min_
;
582 ResourceMap resources_
;
586 ResourceType default_resource_type_
;
587 bool use_texture_storage_ext_
;
588 bool use_texture_format_bgra_
;
589 bool use_texture_usage_hint_
;
590 bool use_compressed_texture_etc1_
;
591 ResourceFormat yuv_resource_format_
;
592 int max_texture_size_
;
593 ResourceFormat best_texture_format_
;
594 ResourceFormat best_render_buffer_format_
;
596 base::ThreadChecker thread_checker_
;
598 scoped_refptr
<Fence
> current_read_lock_fence_
;
599 bool use_rgba_4444_texture_format_
;
601 const size_t id_allocation_chunk_size_
;
602 scoped_ptr
<IdAllocator
> texture_id_allocator_
;
603 scoped_ptr
<IdAllocator
> buffer_id_allocator_
;
605 bool use_sync_query_
;
606 std::vector
<unsigned> use_image_texture_targets_
;
608 // A process-unique ID used for disambiguating memory dumps from different
609 // resource providers.
612 DISALLOW_COPY_AND_ASSIGN(ResourceProvider
);
617 #endif // CC_RESOURCES_RESOURCE_PROVIDER_H_