blob: 165c7db4c85f075b2ec991eb2d321c81f2b54997 [file] [log] [blame]
Romain Guycf51a412013-04-08 19:40:31 -07001/*
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 Craik96a5c4c2015-01-27 15:46:35 -080017#include "PixelBuffer.h"
Romain Guycf51a412013-04-08 19:40:31 -070018
Romain Guy9e6f3ac2013-06-20 16:31:35 -070019#include "Debug.h"
Romain Guycf51a412013-04-08 19:40:31 -070020#include "Extensions.h"
Romain Guycf51a412013-04-08 19:40:31 -070021#include "Properties.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080022#include "renderstate/RenderState.h"
John Reck2de77712016-01-20 11:09:53 -080023#include "utils/GLUtils.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080024
25#include <utils/Log.h>
Romain Guycf51a412013-04-08 19:40:31 -070026
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// CPU pixel buffer
32///////////////////////////////////////////////////////////////////////////////
33
34class CpuPixelBuffer: public PixelBuffer {
35public:
36 CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
Romain Guycf51a412013-04-08 19:40:31 -070037
Chris Craikd41c4d82015-01-05 15:51:13 -080038 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
Romain Guycf51a412013-04-08 19:40:31 -070039
Chris Craikd41c4d82015-01-05 15:51:13 -080040 uint8_t* getMappedPointer() const override;
Romain Guycf51a412013-04-08 19:40:31 -070041
Chris Craikd41c4d82015-01-05 15:51:13 -080042 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
Romain Guycf51a412013-04-08 19:40:31 -070043
John Reckf3ad3242016-02-24 15:36:35 -080044protected:
45 void unmap() override;
46
Romain Guycf51a412013-04-08 19:40:31 -070047private:
Chris Craik51d6a3d2014-12-22 17:16:56 -080048 std::unique_ptr<uint8_t[]> mBuffer;
Romain Guycf51a412013-04-08 19:40:31 -070049};
50
Chris Craik51d6a3d2014-12-22 17:16:56 -080051CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
52 : PixelBuffer(format, width, height)
53 , mBuffer(new uint8_t[width * height * formatSize(format)]) {
Romain Guycf51a412013-04-08 19:40:31 -070054}
55
56uint8_t* CpuPixelBuffer::map(AccessMode mode) {
57 if (mAccessMode == kAccessMode_None) {
58 mAccessMode = mode;
59 }
Chris Craik51d6a3d2014-12-22 17:16:56 -080060 return mBuffer.get();
Romain Guycf51a412013-04-08 19:40:31 -070061}
62
63void CpuPixelBuffer::unmap() {
64 mAccessMode = kAccessMode_None;
65}
66
67uint8_t* CpuPixelBuffer::getMappedPointer() const {
Chris Craik51d6a3d2014-12-22 17:16:56 -080068 return mAccessMode == kAccessMode_None ? nullptr : mBuffer.get();
Romain Guycf51a412013-04-08 19:40:31 -070069}
70
71void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
72 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
Chris Craik51d6a3d2014-12-22 17:16:56 -080073 mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]);
Romain Guycf51a412013-04-08 19:40:31 -070074}
75
76///////////////////////////////////////////////////////////////////////////////
77// GPU pixel buffer
78///////////////////////////////////////////////////////////////////////////////
79
80class GpuPixelBuffer: public PixelBuffer {
81public:
82 GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
83 ~GpuPixelBuffer();
84
Chris Craikd41c4d82015-01-05 15:51:13 -080085 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
Romain Guycf51a412013-04-08 19:40:31 -070086
Chris Craikd41c4d82015-01-05 15:51:13 -080087 uint8_t* getMappedPointer() const override;
Romain Guycf51a412013-04-08 19:40:31 -070088
Chris Craikd41c4d82015-01-05 15:51:13 -080089 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
Romain Guycf51a412013-04-08 19:40:31 -070090
John Reckf3ad3242016-02-24 15:36:35 -080091protected:
92 void unmap() override;
93
Romain Guycf51a412013-04-08 19:40:31 -070094private:
95 GLuint mBuffer;
96 uint8_t* mMappedPointer;
97 Caches& mCaches;
98};
99
Chris Craik96a5c4c2015-01-27 15:46:35 -0800100GpuPixelBuffer::GpuPixelBuffer(GLenum format,
101 uint32_t width, uint32_t height)
Chris Craikd41c4d82015-01-05 15:51:13 -0800102 : PixelBuffer(format, width, height)
103 , mMappedPointer(nullptr)
Chris Craik96a5c4c2015-01-27 15:46:35 -0800104 , mCaches(Caches::getInstance()){
Romain Guycf51a412013-04-08 19:40:31 -0700105 glGenBuffers(1, &mBuffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800106
Chris Craik44eb2c02015-01-29 09:45:09 -0800107 mCaches.pixelBufferState().bind(mBuffer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800108 glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW);
Chris Craik44eb2c02015-01-29 09:45:09 -0800109 mCaches.pixelBufferState().unbind();
Romain Guycf51a412013-04-08 19:40:31 -0700110}
111
112GpuPixelBuffer::~GpuPixelBuffer() {
113 glDeleteBuffers(1, &mBuffer);
114}
115
116uint8_t* GpuPixelBuffer::map(AccessMode mode) {
117 if (mAccessMode == kAccessMode_None) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800118 mCaches.pixelBufferState().bind(mBuffer);
Romain Guycf51a412013-04-08 19:40:31 -0700119 mMappedPointer = (uint8_t*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode);
John Reck2de77712016-01-20 11:09:53 -0800120 if (CC_UNLIKELY(!mMappedPointer)) {
121 GLUtils::dumpGLErrors();
122 LOG_ALWAYS_FATAL("Failed to map PBO");
Romain Guy9e6f3ac2013-06-20 16:31:35 -0700123 }
Romain Guycf51a412013-04-08 19:40:31 -0700124 mAccessMode = mode;
John Reckf3ad3242016-02-24 15:36:35 -0800125 mCaches.pixelBufferState().unbind();
Romain Guycf51a412013-04-08 19:40:31 -0700126 }
127
128 return mMappedPointer;
129}
130
131void GpuPixelBuffer::unmap() {
132 if (mAccessMode != kAccessMode_None) {
133 if (mMappedPointer) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800134 mCaches.pixelBufferState().bind(mBuffer);
Romain Guy03c00b52013-06-20 18:30:28 -0700135 GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
136 if (status == GL_FALSE) {
137 ALOGE("Corrupted GPU pixel buffer");
138 }
Romain Guycf51a412013-04-08 19:40:31 -0700139 }
140 mAccessMode = kAccessMode_None;
Chris Craikd41c4d82015-01-05 15:51:13 -0800141 mMappedPointer = nullptr;
Romain Guycf51a412013-04-08 19:40:31 -0700142 }
143}
144
145uint8_t* GpuPixelBuffer::getMappedPointer() const {
146 return mMappedPointer;
147}
148
149void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
150 // If the buffer is not mapped, unmap() will not bind it
Chris Craik44eb2c02015-01-29 09:45:09 -0800151 mCaches.pixelBufferState().bind(mBuffer);
Romain Guycf51a412013-04-08 19:40:31 -0700152 unmap();
153 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat,
Kévin PETIT73fc5582014-02-13 11:03:40 +0000154 GL_UNSIGNED_BYTE, reinterpret_cast<void*>(offset));
John Reckf3ad3242016-02-24 15:36:35 -0800155 mCaches.pixelBufferState().unbind();
Romain Guycf51a412013-04-08 19:40:31 -0700156}
157
158///////////////////////////////////////////////////////////////////////////////
159// Factory
160///////////////////////////////////////////////////////////////////////////////
161
Chris Craik96a5c4c2015-01-27 15:46:35 -0800162PixelBuffer* PixelBuffer::create(GLenum format,
163 uint32_t width, uint32_t height, BufferType type) {
Romain Guyf9f00162013-05-09 11:50:12 -0700164 if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) {
165 return new GpuPixelBuffer(format, width, height);
Romain Guycf51a412013-04-08 19:40:31 -0700166 }
167 return new CpuPixelBuffer(format, width, height);
168}
169
170}; // namespace uirenderer
171}; // namespace android