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 "cc/base/cc_export.h"
21 #include "cc/base/resource_id.h"
22 #include "cc/output/context_provider.h"
23 #include "cc/output/output_surface.h"
24 #include "cc/resources/release_callback_impl.h"
25 #include "cc/resources/resource_format.h"
26 #include "cc/resources/return_callback.h"
27 #include "cc/resources/shared_bitmap.h"
28 #include "cc/resources/single_release_callback_impl.h"
29 #include "cc/resources/texture_mailbox.h"
30 #include "cc/resources/transferable_resource.h"
31 #include "third_party/khronos/GLES2/gl2.h"
32 #include "third_party/khronos/GLES2/gl2ext.h"
33 #include "third_party/skia/include/core/SkBitmap.h"
34 #include "third_party/skia/include/core/SkCanvas.h"
35 #include "ui/gfx/geometry/size.h"
40 class GpuMemoryBufferManager
;
47 class GpuMemoryBuffer
;
53 class BlockingTaskRunner
;
56 class SharedBitmapManager
;
57 class TextureUploader
;
59 // This class is not thread-safe and can only be called from the thread it was
60 // created on (in practice, the impl thread).
61 class CC_EXPORT ResourceProvider
{
66 typedef std::vector
<ResourceId
> ResourceIdArray
;
67 typedef base::hash_set
<ResourceId
> ResourceIdSet
;
68 typedef base::hash_map
<ResourceId
, ResourceId
> ResourceIdMap
;
70 TEXTURE_HINT_DEFAULT
= 0x0,
71 TEXTURE_HINT_IMMUTABLE
= 0x1,
72 TEXTURE_HINT_FRAMEBUFFER
= 0x2,
73 TEXTURE_HINT_IMMUTABLE_FRAMEBUFFER
=
74 TEXTURE_HINT_IMMUTABLE
| TEXTURE_HINT_FRAMEBUFFER
77 RESOURCE_TYPE_GL_TEXTURE
,
81 static scoped_ptr
<ResourceProvider
> Create(
82 OutputSurface
* output_surface
,
83 SharedBitmapManager
* shared_bitmap_manager
,
84 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
85 BlockingTaskRunner
* blocking_main_thread_task_runner
,
86 int highp_threshold_min
,
87 bool use_rgba_4444_texture_format
,
88 size_t id_allocation_chunk_size
);
89 virtual ~ResourceProvider();
91 void DidLoseOutputSurface() { lost_output_surface_
= true; }
93 int max_texture_size() const { return max_texture_size_
; }
94 ResourceFormat
memory_efficient_texture_format() const {
95 return use_rgba_4444_texture_format_
? RGBA_4444
: best_texture_format_
;
97 ResourceFormat
best_texture_format() const { return best_texture_format_
; }
98 ResourceFormat
yuv_resource_format() const { return yuv_resource_format_
; }
99 bool use_sync_query() const { return use_sync_query_
; }
100 size_t num_resources() const { return resources_
.size(); }
102 // Checks whether a resource is in use by a consumer.
103 bool InUseByConsumer(ResourceId id
);
105 bool IsLost(ResourceId id
);
106 bool AllowOverlay(ResourceId id
);
108 // Producer interface.
110 ResourceType
default_resource_type() const { return default_resource_type_
; }
111 ResourceType
GetResourceType(ResourceId id
);
113 // Creates a resource of the default resource type.
114 ResourceId
CreateResource(const gfx::Size
& size
,
117 ResourceFormat format
);
119 // Creates a resource which is tagged as being managed for GPU memory
120 // accounting purposes.
121 ResourceId
CreateManagedResource(const gfx::Size
& size
,
125 ResourceFormat format
);
127 // You can also explicitly create a specific resource type.
128 ResourceId
CreateGLTexture(const gfx::Size
& size
,
133 ResourceFormat format
);
135 ResourceId
CreateBitmap(const gfx::Size
& size
, GLint wrap_mode
);
136 // Wraps an IOSurface into a GL resource.
137 ResourceId
CreateResourceFromIOSurface(const gfx::Size
& size
,
138 unsigned io_surface_id
);
140 // Wraps an external texture mailbox into a GL resource.
141 ResourceId
CreateResourceFromTextureMailbox(
142 const TextureMailbox
& mailbox
,
143 scoped_ptr
<SingleReleaseCallbackImpl
> release_callback_impl
);
145 void DeleteResource(ResourceId id
);
147 // Update pixels from image, copying source_rect (in image) to dest_offset (in
149 // NOTE: DEPRECATED. Use CopyToResource() instead.
150 void SetPixels(ResourceId id
,
151 const uint8_t* image
,
152 const gfx::Rect
& image_rect
,
153 const gfx::Rect
& source_rect
,
154 const gfx::Vector2d
& dest_offset
);
155 void CopyToResource(ResourceId id
,
156 const uint8_t* image
,
157 const gfx::Size
& image_size
);
159 // Check upload status.
160 size_t NumBlockingUploads();
161 void MarkPendingUploadsAsNonBlocking();
162 size_t EstimatedUploadsPerTick();
164 void ReleaseCachedData();
165 base::TimeTicks
EstimatedUploadCompletionTime(size_t uploads_per_tick
);
167 // Only flush the command buffer if supported.
168 // Returns true if the shallow flush occurred, false otherwise.
169 bool ShallowFlushIfSupported();
171 // Creates accounting for a child. Returns a child ID.
172 int CreateChild(const ReturnCallback
& return_callback
);
174 // Destroys accounting for the child, deleting all accounted resources.
175 void DestroyChild(int child
);
177 // Sets whether resources need sync points set on them when returned to this
178 // child. Defaults to true.
179 void SetChildNeedsSyncPoints(int child
, bool needs_sync_points
);
181 // Gets the child->parent resource ID map.
182 const ResourceIdMap
& GetChildToParentMap(int child
) const;
184 // Prepares resources to be transfered to the parent, moving them to
185 // mailboxes and serializing meta-data into TransferableResources.
186 // Resources are not removed from the ResourceProvider, but are marked as
188 void PrepareSendToParent(const ResourceIdArray
& resources
,
189 TransferableResourceArray
* transferable_resources
);
191 // Receives resources from a child, moving them from mailboxes. Resource IDs
192 // passed are in the child namespace, and will be translated to the parent
193 // namespace, added to the child->parent map.
194 // This adds the resources to the working set in the ResourceProvider without
195 // declaring which resources are in use. Use DeclareUsedResourcesFromChild
196 // after calling this method to do that. All calls to ReceiveFromChild should
197 // be followed by a DeclareUsedResourcesFromChild.
198 // NOTE: if the sync_point is set on any TransferableResource, this will
200 void ReceiveFromChild(
201 int child
, const TransferableResourceArray
& transferable_resources
);
203 // Once a set of resources have been received, they may or may not be used.
204 // This declares what set of resources are currently in use from the child,
205 // releasing any other resources back to the child.
206 void DeclareUsedResourcesFromChild(int child
,
207 const ResourceIdSet
& resources_from_child
);
209 // Receives resources from the parent, moving them from mailboxes. Resource
210 // IDs passed are in the child namespace.
211 // NOTE: if the sync_point is set on any TransferableResource, this will
213 void ReceiveReturnsFromParent(
214 const ReturnedResourceArray
& transferable_resources
);
216 // The following lock classes are part of the ResourceProvider API and are
217 // needed to read and write the resource contents. The user must ensure
218 // that they only use GL locks on GL resources, etc, and this is enforced
220 class CC_EXPORT ScopedReadLockGL
{
222 ScopedReadLockGL(ResourceProvider
* resource_provider
,
223 ResourceId resource_id
);
224 virtual ~ScopedReadLockGL();
226 unsigned texture_id() const { return resource_
->gl_id
; }
227 GLenum
target() const { return resource_
->target
; }
230 ResourceProvider
* resource_provider_
;
231 ResourceId resource_id_
;
234 const ResourceProvider::Resource
* resource_
;
236 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL
);
239 class CC_EXPORT ScopedSamplerGL
: public ScopedReadLockGL
{
241 ScopedSamplerGL(ResourceProvider
* resource_provider
,
242 ResourceId resource_id
,
244 ScopedSamplerGL(ResourceProvider
* resource_provider
,
245 ResourceId resource_id
,
248 ~ScopedSamplerGL() override
;
250 GLenum
target() const { return target_
; }
256 DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL
);
259 class CC_EXPORT ScopedWriteLockGL
{
261 ScopedWriteLockGL(ResourceProvider
* resource_provider
,
262 ResourceId resource_id
);
263 ~ScopedWriteLockGL();
265 unsigned texture_id() const { return texture_id_
; }
268 ResourceProvider
* resource_provider_
;
269 ResourceProvider::Resource
* resource_
;
270 unsigned texture_id_
;
272 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL
);
275 class CC_EXPORT ScopedReadLockSoftware
{
277 ScopedReadLockSoftware(ResourceProvider
* resource_provider
,
278 ResourceId resource_id
);
279 ~ScopedReadLockSoftware();
281 const SkBitmap
* sk_bitmap() const {
285 GLint
wrap_mode() const { return wrap_mode_
; }
287 bool valid() const { return !!sk_bitmap_
.getPixels(); }
290 ResourceProvider
* resource_provider_
;
291 ResourceId resource_id_
;
295 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware
);
298 class CC_EXPORT ScopedWriteLockSoftware
{
300 ScopedWriteLockSoftware(ResourceProvider
* resource_provider
,
301 ResourceId resource_id
);
302 ~ScopedWriteLockSoftware();
304 SkBitmap
& sk_bitmap() { return sk_bitmap_
; }
305 bool valid() const { return !!sk_bitmap_
.getPixels(); }
308 ResourceProvider
* resource_provider_
;
309 ResourceProvider::Resource
* resource_
;
311 base::ThreadChecker thread_checker_
;
313 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware
);
316 class CC_EXPORT ScopedWriteLockGpuMemoryBuffer
{
318 ScopedWriteLockGpuMemoryBuffer(ResourceProvider
* resource_provider
,
319 ResourceId resource_id
);
320 ~ScopedWriteLockGpuMemoryBuffer();
322 gfx::GpuMemoryBuffer
* GetGpuMemoryBuffer();
325 ResourceProvider
* resource_provider_
;
326 ResourceProvider::Resource
* resource_
;
327 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
328 gfx::GpuMemoryBuffer
* gpu_memory_buffer_
;
330 ResourceFormat format_
;
331 base::ThreadChecker thread_checker_
;
333 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGpuMemoryBuffer
);
336 class CC_EXPORT ScopedWriteLockGr
{
338 ScopedWriteLockGr(ResourceProvider
* resource_provider
,
339 ResourceId resource_id
);
340 ~ScopedWriteLockGr();
342 void InitSkSurface(bool use_distance_field_text
,
343 bool can_use_lcd_text
,
344 int msaa_sample_count
);
345 void ReleaseSkSurface();
347 SkSurface
* sk_surface() { return sk_surface_
.get(); }
348 ResourceProvider::Resource
* resource() { return resource_
; }
351 ResourceProvider
* resource_provider_
;
352 ResourceProvider::Resource
* resource_
;
353 base::ThreadChecker thread_checker_
;
354 skia::RefPtr
<SkSurface
> sk_surface_
;
356 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGr
);
359 class Fence
: public base::RefCounted
<Fence
> {
363 virtual void Set() = 0;
364 virtual bool HasPassed() = 0;
365 virtual void Wait() = 0;
368 friend class base::RefCounted
<Fence
>;
372 DISALLOW_COPY_AND_ASSIGN(Fence
);
375 class SynchronousFence
: public ResourceProvider::Fence
{
377 explicit SynchronousFence(gpu::gles2::GLES2Interface
* gl
);
379 // Overridden from Fence:
381 bool HasPassed() override
;
382 void Wait() override
;
384 // Returns true if fence has been set but not yet synchornized.
385 bool has_synchronized() const { return has_synchronized_
; }
388 ~SynchronousFence() override
;
392 gpu::gles2::GLES2Interface
* gl_
;
393 bool has_synchronized_
;
395 DISALLOW_COPY_AND_ASSIGN(SynchronousFence
);
398 // Acquire pixel buffer for resource. The pixel buffer can be used to
399 // set resource pixels without performing unnecessary copying.
400 void AcquirePixelBuffer(ResourceId resource
);
401 void ReleasePixelBuffer(ResourceId resource
);
402 // Map/unmap the acquired pixel buffer.
403 uint8_t* MapPixelBuffer(ResourceId id
, int* stride
);
404 void UnmapPixelBuffer(ResourceId id
);
405 // Asynchronously update pixels from acquired pixel buffer.
406 void BeginSetPixels(ResourceId id
);
407 void ForceSetPixelsToComplete(ResourceId id
);
408 bool DidSetPixelsComplete(ResourceId id
);
410 // For tests only! This prevents detecting uninitialized reads.
411 // Use SetPixels or LockForWrite to allocate implicitly.
412 void AllocateForTesting(ResourceId id
);
415 void CreateForTesting(ResourceId id
);
417 GLenum
TargetForTesting(ResourceId id
);
419 // Sets the current read fence. If a resource is locked for read
420 // and has read fences enabled, the resource will not allow writes
421 // until this fence has passed.
422 void SetReadLockFence(Fence
* fence
) { current_read_lock_fence_
= fence
; }
424 // Indicates if we can currently lock this resource for write.
425 bool CanLockForWrite(ResourceId id
);
427 // Copy |rect| pixels from source to destination.
428 void CopyResource(ResourceId source_id
,
430 const gfx::Rect
& rect
);
432 void WaitSyncPointIfNeeded(ResourceId id
);
434 void WaitReadLockIfNeeded(ResourceId id
);
436 static GLint
GetActiveTextureUnit(gpu::gles2::GLES2Interface
* gl
);
438 OutputSurface
* output_surface() { return output_surface_
; }
440 void ValidateResource(ResourceId id
) const;
443 ResourceProvider(OutputSurface
* output_surface
,
444 SharedBitmapManager
* shared_bitmap_manager
,
445 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
446 BlockingTaskRunner
* blocking_main_thread_task_runner
,
447 int highp_threshold_min
,
448 bool use_rgba_4444_texture_format
,
449 size_t id_allocation_chunk_size
);
454 enum Origin
{ INTERNAL
, EXTERNAL
, DELEGATED
};
457 Resource(unsigned texture_id
,
458 const gfx::Size
& size
,
465 ResourceFormat format
);
466 Resource(uint8_t* pixels
,
467 SharedBitmap
* bitmap
,
468 const gfx::Size
& size
,
472 Resource(const SharedBitmapId
& bitmap_id
,
473 const gfx::Size
& size
,
480 // Pixel buffer used for set pixels without unnecessary copying.
481 unsigned gl_pixel_buffer_id
;
482 // Query used to determine when asynchronous set pixels complete.
483 unsigned gl_upload_query_id
;
484 // Query used to determine when read lock fence has passed.
485 unsigned gl_read_lock_query_id
;
486 TextureMailbox mailbox
;
487 ReleaseCallbackImpl release_callback_impl
;
489 int lock_for_read_count
;
492 bool dirty_image
: 1;
493 bool locked_for_write
: 1;
495 bool marked_for_deletion
: 1;
496 bool pending_set_pixels
: 1;
497 bool set_pixels_completion_forced
: 1;
499 bool read_lock_fences_enabled
: 1;
500 bool has_shared_bitmap_id
: 1;
501 bool allow_overlay
: 1;
502 scoped_refptr
<Fence
> read_lock_fence
;
506 // TODO(skyostil): Use a separate sampler object for filter state.
507 GLenum original_filter
;
510 unsigned bound_image_id
;
515 ResourceFormat format
;
516 SharedBitmapId shared_bitmap_id
;
517 SharedBitmap
* shared_bitmap
;
518 gfx::GpuMemoryBuffer
* gpu_memory_buffer
;
520 typedef base::hash_map
<ResourceId
, Resource
> ResourceMap
;
526 ResourceIdMap child_to_parent_map
;
527 ResourceIdMap parent_to_child_map
;
528 ReturnCallback return_callback
;
529 bool marked_for_deletion
;
530 bool needs_sync_points
;
532 typedef base::hash_map
<int, Child
> ChildMap
;
534 bool ReadLockFenceHasPassed(const Resource
* resource
) {
535 return !resource
->read_lock_fence
.get() ||
536 resource
->read_lock_fence
->HasPassed();
539 Resource
* InsertResource(ResourceId id
, const Resource
& resource
);
540 Resource
* GetResource(ResourceId id
);
541 const Resource
* LockForRead(ResourceId id
);
542 void UnlockForRead(ResourceId id
);
543 Resource
* LockForWrite(ResourceId id
);
544 void UnlockForWrite(Resource
* resource
);
546 static void PopulateSkBitmapWithResource(SkBitmap
* sk_bitmap
,
547 const Resource
* resource
);
549 void TransferResource(gpu::gles2::GLES2Interface
* gl
,
551 TransferableResource
* resource
);
556 void DeleteResourceInternal(ResourceMap::iterator it
, DeleteStyle style
);
557 void DeleteAndReturnUnusedResourcesToChild(ChildMap::iterator child_it
,
559 const ResourceIdArray
& unused
);
560 void DestroyChildInternal(ChildMap::iterator it
, DeleteStyle style
);
561 void LazyCreate(Resource
* resource
);
562 void LazyAllocate(Resource
* resource
);
564 void BindImageForSampling(Resource
* resource
);
565 // Binds the given GL resource to a texture target for sampling using the
566 // specified filter for both minification and magnification. Returns the
567 // texture target used. The resource must be locked for reading.
568 GLenum
BindForSampling(ResourceId resource_id
, GLenum unit
, GLenum filter
);
570 // Returns NULL if the output_surface_ does not have a ContextProvider.
571 gpu::gles2::GLES2Interface
* ContextGL() const;
572 class GrContext
* GrContext(bool worker_context
) const;
574 OutputSurface
* output_surface_
;
575 SharedBitmapManager
* shared_bitmap_manager_
;
576 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
577 BlockingTaskRunner
* blocking_main_thread_task_runner_
;
578 bool lost_output_surface_
;
579 int highp_threshold_min_
;
581 ResourceMap resources_
;
585 ResourceType default_resource_type_
;
586 bool use_texture_storage_ext_
;
587 bool use_texture_format_bgra_
;
588 bool use_texture_usage_hint_
;
589 bool use_compressed_texture_etc1_
;
590 ResourceFormat yuv_resource_format_
;
591 scoped_ptr
<TextureUploader
> texture_uploader_
;
592 int max_texture_size_
;
593 ResourceFormat best_texture_format_
;
595 base::ThreadChecker thread_checker_
;
597 scoped_refptr
<Fence
> current_read_lock_fence_
;
598 bool use_rgba_4444_texture_format_
;
600 const size_t id_allocation_chunk_size_
;
601 scoped_ptr
<IdAllocator
> texture_id_allocator_
;
602 scoped_ptr
<IdAllocator
> buffer_id_allocator_
;
604 bool use_sync_query_
;
605 // Fence used for CopyResource if CHROMIUM_sync_query is not supported.
606 scoped_refptr
<SynchronousFence
> synchronous_fence_
;
608 DISALLOW_COPY_AND_ASSIGN(ResourceProvider
);
611 // TODO(epenner): Move these format conversions to resource_format.h
612 // once that builds on mac (npapi.h currently #includes OpenGL.h).
613 inline unsigned BitsPerPixel(ResourceFormat format
) {
632 inline GLenum
GLDataType(ResourceFormat format
) {
633 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
634 static const unsigned format_gl_data_type
[RESOURCE_FORMAT_MAX
+ 1] = {
635 GL_UNSIGNED_BYTE
, // RGBA_8888
636 GL_UNSIGNED_SHORT_4_4_4_4
, // RGBA_4444
637 GL_UNSIGNED_BYTE
, // BGRA_8888
638 GL_UNSIGNED_BYTE
, // ALPHA_8
639 GL_UNSIGNED_BYTE
, // LUMINANCE_8
640 GL_UNSIGNED_SHORT_5_6_5
, // RGB_565,
641 GL_UNSIGNED_BYTE
, // ETC1
642 GL_UNSIGNED_BYTE
// RED_8
644 return format_gl_data_type
[format
];
647 inline GLenum
GLDataFormat(ResourceFormat format
) {
648 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
649 static const unsigned format_gl_data_format
[RESOURCE_FORMAT_MAX
+ 1] = {
650 GL_RGBA
, // RGBA_8888
651 GL_RGBA
, // RGBA_4444
652 GL_BGRA_EXT
, // BGRA_8888
654 GL_LUMINANCE
, // LUMINANCE_8
656 GL_ETC1_RGB8_OES
, // ETC1
659 return format_gl_data_format
[format
];
662 inline GLenum
GLInternalFormat(ResourceFormat format
) {
663 return GLDataFormat(format
);
668 #endif // CC_RESOURCES_RESOURCE_PROVIDER_H_