Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 17 | #include "PixelBuffer.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 18 | |
Romain Guy | 9e6f3ac | 2013-06-20 16:31:35 -0700 | [diff] [blame] | 19 | #include "Debug.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 20 | #include "Extensions.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 21 | #include "Properties.h" |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 22 | #include "renderstate/RenderState.h" |
John Reck | 2de7771 | 2016-01-20 11:09:53 -0800 | [diff] [blame] | 23 | #include "utils/GLUtils.h" |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 24 | |
| 25 | #include <utils/Log.h> |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | // CPU pixel buffer |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 34 | class CpuPixelBuffer : public PixelBuffer { |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 35 | public: |
| 36 | CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 37 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 38 | uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 39 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 40 | void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 41 | |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame] | 42 | protected: |
| 43 | void unmap() override; |
| 44 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 45 | private: |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 46 | std::unique_ptr<uint8_t[]> mBuffer; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 49 | CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height) |
| 50 | : PixelBuffer(format, width, height) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 51 | , mBuffer(new uint8_t[width * height * formatSize(format)]) {} |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 52 | |
| 53 | uint8_t* CpuPixelBuffer::map(AccessMode mode) { |
| 54 | if (mAccessMode == kAccessMode_None) { |
| 55 | mAccessMode = mode; |
| 56 | } |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 57 | return mBuffer.get(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void CpuPixelBuffer::unmap() { |
| 61 | mAccessMode = kAccessMode_None; |
| 62 | } |
| 63 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 64 | void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 65 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat, GL_UNSIGNED_BYTE, |
| 66 | &mBuffer[offset]); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | // GPU pixel buffer |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 73 | class GpuPixelBuffer : public PixelBuffer { |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 74 | public: |
| 75 | GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height); |
| 76 | ~GpuPixelBuffer(); |
| 77 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 78 | uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 79 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 80 | void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 81 | |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame] | 82 | protected: |
| 83 | void unmap() override; |
| 84 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 85 | private: |
| 86 | GLuint mBuffer; |
| 87 | uint8_t* mMappedPointer; |
| 88 | Caches& mCaches; |
| 89 | }; |
| 90 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 91 | GpuPixelBuffer::GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 92 | : PixelBuffer(format, width, height) |
| 93 | , mMappedPointer(nullptr) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 94 | , mCaches(Caches::getInstance()) { |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 95 | glGenBuffers(1, &mBuffer); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 96 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 97 | mCaches.pixelBufferState().bind(mBuffer); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 98 | glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 99 | mCaches.pixelBufferState().unbind(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | GpuPixelBuffer::~GpuPixelBuffer() { |
| 103 | glDeleteBuffers(1, &mBuffer); |
| 104 | } |
| 105 | |
| 106 | uint8_t* GpuPixelBuffer::map(AccessMode mode) { |
| 107 | if (mAccessMode == kAccessMode_None) { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 108 | mCaches.pixelBufferState().bind(mBuffer); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 109 | mMappedPointer = (uint8_t*)glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode); |
John Reck | 2de7771 | 2016-01-20 11:09:53 -0800 | [diff] [blame] | 110 | if (CC_UNLIKELY(!mMappedPointer)) { |
| 111 | GLUtils::dumpGLErrors(); |
| 112 | LOG_ALWAYS_FATAL("Failed to map PBO"); |
Romain Guy | 9e6f3ac | 2013-06-20 16:31:35 -0700 | [diff] [blame] | 113 | } |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 114 | mAccessMode = mode; |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame] | 115 | mCaches.pixelBufferState().unbind(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return mMappedPointer; |
| 119 | } |
| 120 | |
| 121 | void GpuPixelBuffer::unmap() { |
| 122 | if (mAccessMode != kAccessMode_None) { |
| 123 | if (mMappedPointer) { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 124 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 125 | GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); |
| 126 | if (status == GL_FALSE) { |
| 127 | ALOGE("Corrupted GPU pixel buffer"); |
| 128 | } |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 129 | } |
| 130 | mAccessMode = kAccessMode_None; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 131 | mMappedPointer = nullptr; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 135 | void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) { |
| 136 | // If the buffer is not mapped, unmap() will not bind it |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 137 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 138 | unmap(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 139 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat, GL_UNSIGNED_BYTE, |
| 140 | reinterpret_cast<void*>(offset)); |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame] | 141 | mCaches.pixelBufferState().unbind(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /////////////////////////////////////////////////////////////////////////////// |
| 145 | // Factory |
| 146 | /////////////////////////////////////////////////////////////////////////////// |
| 147 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 148 | PixelBuffer* PixelBuffer::create(GLenum format, uint32_t width, uint32_t height, BufferType type) { |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 149 | if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) { |
| 150 | return new GpuPixelBuffer(format, width, height); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 151 | } |
| 152 | return new CpuPixelBuffer(format, width, height); |
| 153 | } |
| 154 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 155 | }; // namespace uirenderer |
| 156 | }; // namespace android |