blob: 2fdece98987618e2d452bb25e217f1ea8797fba8 [file] [log] [blame]
Stan Iliev564ca3e2018-09-04 22:00:00 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <EGL/egl.h>
20#include <EGL/eglext.h>
21
22#include <gui/BufferQueueDefs.h>
23
24#include <SkImage.h>
25#include <cutils/compiler.h>
26#include <gui/BufferItem.h>
27#include <system/graphics.h>
Stan Iliev902ce2a2019-03-07 18:13:55 -050028
29namespace GrAHardwareBufferUtils {
30typedef void* DeleteImageCtx;
31typedef void (*DeleteImageProc)(DeleteImageCtx);
32}
Stan Iliev564ca3e2018-09-04 22:00:00 +000033
34namespace android {
35
36namespace uirenderer {
37class RenderState;
38}
39
Stan Ilievee3754a2019-03-15 12:07:35 -040040class AutoBackendTextureRelease;
Stan Iliev564ca3e2018-09-04 22:00:00 +000041class SurfaceTexture;
42
43/*
44 * ImageConsumer implements the parts of SurfaceTexture that deal with
45 * images consumed by HWUI view system.
46 */
47class ImageConsumer {
48public:
49 sk_sp<SkImage> dequeueImage(bool* queueEmpty, SurfaceTexture& cb,
50 uirenderer::RenderState& renderState);
51
52 /**
53 * onAcquireBufferLocked amends the ConsumerBase method to update the
54 * mImageSlots array in addition to the ConsumerBase behavior.
55 */
56 void onAcquireBufferLocked(BufferItem* item);
57
58 /**
59 * onReleaseBufferLocked amends the ConsumerBase method to update the
60 * mImageSlots array in addition to the ConsumerBase.
61 */
62 void onReleaseBufferLocked(int slot);
63
64 /**
65 * onFreeBufferLocked frees up the given buffer slot. If the slot has been
66 * initialized this will release the reference to the GraphicBuffer in that
67 * slot and destroy the SkImage in that slot. Otherwise it has no effect.
68 */
69 void onFreeBufferLocked(int slotIndex);
70
71private:
72 /**
73 * ImageSlot contains the information and object references that
74 * ImageConsumer maintains about a BufferQueue buffer slot.
75 */
Stan Iliev902ce2a2019-03-07 18:13:55 -050076 class ImageSlot {
77 public:
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040078 ImageSlot() : mDataspace(HAL_DATASPACE_UNKNOWN), mEglFence(EGL_NO_SYNC_KHR) {}
Stan Iliev564ca3e2018-09-04 22:00:00 +000079
Stan Iliev902ce2a2019-03-07 18:13:55 -050080 ~ImageSlot() { clear(); }
81
82 void createIfNeeded(sp<GraphicBuffer> graphicBuffer, android_dataspace dataspace,
83 bool forceCreate, GrContext* context);
Stan Ilievee3754a2019-03-15 12:07:35 -040084
Stan Iliev902ce2a2019-03-07 18:13:55 -050085 void clear();
86
87 inline EGLSyncKHR& eglFence() { return mEglFence; }
88
Stan Ilievee3754a2019-03-15 12:07:35 -040089 sk_sp<SkImage> getImage();
Stan Iliev902ce2a2019-03-07 18:13:55 -050090
91 private:
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040092 // the dataspace associated with the current image
93 android_dataspace mDataspace;
94
Stan Iliev564ca3e2018-09-04 22:00:00 +000095 /**
96 * mEglFence is the EGL sync object that must signal before the buffer
97 * associated with this buffer slot may be dequeued.
98 */
99 EGLSyncKHR mEglFence;
100
Stan Ilievee3754a2019-03-15 12:07:35 -0400101 /**
102 * mTextureRelease may outlive ImageConsumer, if the last ref is held by an SkImage.
103 * ImageConsumer holds one ref to mTextureRelease, which is decremented by "clear".
104 */
105 AutoBackendTextureRelease* mTextureRelease = nullptr;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000106 };
107
108 /**
109 * ImageConsumer stores the SkImages that have been allocated by the BufferQueue
110 * for each buffer slot. It is initialized to null pointers, and gets
111 * filled in with the result of BufferQueue::acquire when the
112 * client dequeues a buffer from a
113 * slot that has not yet been used. The buffer allocated to a slot will also
114 * be replaced if the requested buffer usage or geometry differs from that
115 * of the buffer allocated to a slot.
116 */
117 ImageSlot mImageSlots[BufferQueueDefs::NUM_BUFFER_SLOTS];
118};
119
Chris Blume7b8a8082018-11-30 15:51:58 -0800120} /* namespace android */